From f5dbb4452f696397ec3558dec966bd0f2bb69f9a Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Sat, 25 Mar 2023 10:51:43 +0700 Subject: [PATCH] e3: DomainGetAsOf, DomainRange (#7177) --- cmd/rpcdaemon/commands/debug_api.go | 4 ++-- cmd/rpcdaemon/commands/debug_api_test.go | 6 +++--- core/state/history_reader_v3.go | 10 +++++----- core/state/temporal/kv_temporal.go | 17 ++++++++++++++++- go.mod | 2 +- go.sum | 4 ++-- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cmd/rpcdaemon/commands/debug_api.go b/cmd/rpcdaemon/commands/debug_api.go index 80479b1ce84..5a8e74e1bc2 100644 --- a/cmd/rpcdaemon/commands/debug_api.go +++ b/cmd/rpcdaemon/commands/debug_api.go @@ -345,7 +345,7 @@ func (api *PrivateDebugAPIImpl) AccountAt(ctx context.Context, blockHash common. return nil, err } ttx := tx.(kv.TemporalTx) - v, ok, err := ttx.DomainGet(temporal.AccountsDomain, address[:], nil, minTxNum+txIndex+1) + v, ok, err := ttx.DomainGetAsOf(temporal.AccountsDomain, address[:], nil, minTxNum+txIndex+1) if err != nil { return nil, err } @@ -362,7 +362,7 @@ func (api *PrivateDebugAPIImpl) AccountAt(ctx context.Context, blockHash common. result.Nonce = hexutil.Uint64(a.Nonce) result.CodeHash = a.CodeHash - code, _, err := ttx.DomainGet(temporal.CodeDomain, address[:], a.CodeHash[:], minTxNum+txIndex) + code, _, err := ttx.DomainGetAsOf(temporal.CodeDomain, address[:], a.CodeHash[:], minTxNum+txIndex) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/debug_api_test.go b/cmd/rpcdaemon/commands/debug_api_test.go index 1024c071af0..41d9de5e24f 100644 --- a/cmd/rpcdaemon/commands/debug_api_test.go +++ b/cmd/rpcdaemon/commands/debug_api_test.go @@ -428,7 +428,7 @@ func TestMapTxNum2BlockNum(t *testing.T) { } } t.Run("descend", func(t *testing.T) { - tx, err := m.DB.(kv.TemporalRoDb).BeginTemporalRo(m.Ctx) + tx, err := m.DB.(kv.TemporalRoDB).BeginTemporalRo(m.Ctx) require.NoError(t, err) defer tx.Rollback() @@ -440,7 +440,7 @@ func TestMapTxNum2BlockNum(t *testing.T) { checkIter(t, expectTxNums, txNumsIter) }) t.Run("ascend", func(t *testing.T) { - tx, err := m.DB.(kv.TemporalRoDb).BeginTemporalRo(m.Ctx) + tx, err := m.DB.(kv.TemporalRoDB).BeginTemporalRo(m.Ctx) require.NoError(t, err) defer tx.Rollback() @@ -452,7 +452,7 @@ func TestMapTxNum2BlockNum(t *testing.T) { checkIter(t, expectTxNums, txNumsIter) }) t.Run("ascend limit", func(t *testing.T) { - tx, err := m.DB.(kv.TemporalRoDb).BeginTemporalRo(m.Ctx) + tx, err := m.DB.(kv.TemporalRoDB).BeginTemporalRo(m.Ctx) require.NoError(t, err) defer tx.Rollback() diff --git a/core/state/history_reader_v3.go b/core/state/history_reader_v3.go index 2eeb3524f8e..d6e53e06129 100644 --- a/core/state/history_reader_v3.go +++ b/core/state/history_reader_v3.go @@ -32,7 +32,7 @@ func (hr *HistoryReaderV3) SetTxNum(txNum uint64) { hr.txNum = txNum } func (hr *HistoryReaderV3) SetTrace(trace bool) { hr.trace = trace } func (hr *HistoryReaderV3) ReadAccountData(address libcommon.Address) (*accounts.Account, error) { - enc, ok, err := hr.ttx.DomainGet(temporal.AccountsDomain, address.Bytes(), nil, hr.txNum) + enc, ok, err := hr.ttx.DomainGetAsOf(temporal.AccountsDomain, address.Bytes(), nil, hr.txNum) if err != nil || !ok || len(enc) == 0 { if hr.trace { fmt.Printf("ReadAccountData [%x] => []\n", address) @@ -53,7 +53,7 @@ func (hr *HistoryReaderV3) ReadAccountStorage(address libcommon.Address, incarna acc := make([]byte, 20+8) copy(acc, address.Bytes()) binary.BigEndian.PutUint64(acc[20:], incarnation) - enc, _, err := hr.ttx.DomainGet(temporal.StorageDomain, acc, key.Bytes(), hr.txNum) + enc, _, err := hr.ttx.DomainGetAsOf(temporal.StorageDomain, acc, key.Bytes(), hr.txNum) if hr.trace { fmt.Printf("ReadAccountStorage [%x] [%x] => [%x]\n", address, *key, enc) } @@ -64,7 +64,7 @@ func (hr *HistoryReaderV3) ReadAccountCode(address libcommon.Address, incarnatio if codeHash == emptyCodeHashH { return nil, nil } - code, _, err := hr.ttx.DomainGet(temporal.CodeDomain, address.Bytes(), codeHash.Bytes(), hr.txNum) + code, _, err := hr.ttx.DomainGetAsOf(temporal.CodeDomain, address.Bytes(), codeHash.Bytes(), hr.txNum) if hr.trace { fmt.Printf("ReadAccountCode [%x %x] => [%x]\n", address, codeHash, code) } @@ -72,12 +72,12 @@ func (hr *HistoryReaderV3) ReadAccountCode(address libcommon.Address, incarnatio } func (hr *HistoryReaderV3) ReadAccountCodeSize(address libcommon.Address, incarnation uint64, codeHash libcommon.Hash) (int, error) { - enc, _, err := hr.ttx.DomainGet(temporal.CodeDomain, address.Bytes(), codeHash.Bytes(), hr.txNum) + enc, _, err := hr.ttx.DomainGetAsOf(temporal.CodeDomain, address.Bytes(), codeHash.Bytes(), hr.txNum) return len(enc), err } func (hr *HistoryReaderV3) ReadAccountIncarnation(address libcommon.Address) (uint64, error) { - enc, ok, err := hr.ttx.DomainGet(temporal.AccountsDomain, address.Bytes(), nil, hr.txNum) + enc, ok, err := hr.ttx.DomainGetAsOf(temporal.AccountsDomain, address.Bytes(), nil, hr.txNum) if err != nil || !ok || len(enc) == 0 { if hr.trace { fmt.Printf("ReadAccountIncarnation [%x] => [0]\n", address) diff --git a/core/state/temporal/kv_temporal.go b/core/state/temporal/kv_temporal.go index a31490d9268..8e5b4166c54 100644 --- a/core/state/temporal/kv_temporal.go +++ b/core/state/temporal/kv_temporal.go @@ -278,7 +278,22 @@ func (tx *Tx) DomainRange(name kv.Domain, fromKey, toKey []byte, asOfTs uint64, return it, nil } -func (tx *Tx) DomainGet(name kv.Domain, key, key2 []byte, ts uint64) (v []byte, ok bool, err error) { +func (tx *Tx) DomainGet(name kv.Domain, key, key2 []byte) (v []byte, ok bool, err error) { + switch name { + case AccountsDomain: + v, err = tx.GetOne(kv.PlainState, key) + return v, v != nil, err + case StorageDomain: + v, err = tx.GetOne(kv.PlainState, append(common.Copy(key), key2...)) + return v, v != nil, err + case CodeDomain: + v, err = tx.GetOne(kv.Code, key2) + return v, v != nil, err + default: + panic(fmt.Sprintf("unexpected: %s", name)) + } +} +func (tx *Tx) DomainGetAsOf(name kv.Domain, key, key2 []byte, ts uint64) (v []byte, ok bool, err error) { switch name { case AccountsDomain: v, ok, err = tx.HistoryGet(AccountsHistory, key, ts) diff --git a/go.mod b/go.mod index 082c424e5e9..3230ab4cc99 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon go 1.19 require ( - github.com/ledgerwatch/erigon-lib v0.0.0-20230324081547-7a281bca0c7c + github.com/ledgerwatch/erigon-lib v0.0.0-20230325033216-5ae3af617b53 github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230306083105-1391330d62a3 github.com/ledgerwatch/log/v3 v3.7.0 github.com/ledgerwatch/secp256k1 v1.0.0 diff --git a/go.sum b/go.sum index 1e0854cb129..5a6d0a1e60c 100644 --- a/go.sum +++ b/go.sum @@ -519,8 +519,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= -github.com/ledgerwatch/erigon-lib v0.0.0-20230324081547-7a281bca0c7c h1:3TOKHeGE1DpoYbCe/K8USRA0IV7MjRyE9OzRWKlxI3Y= -github.com/ledgerwatch/erigon-lib v0.0.0-20230324081547-7a281bca0c7c/go.mod h1:dagOLJlpxlMcQX4a2KIdzyCBIMtk6+ccqikNQf2uDwA= +github.com/ledgerwatch/erigon-lib v0.0.0-20230325033216-5ae3af617b53 h1:FTIeWRzOoUqVMyJPKR91NJWG0THkfYAsss6SarUt7PI= +github.com/ledgerwatch/erigon-lib v0.0.0-20230325033216-5ae3af617b53/go.mod h1:CkP5qnLv68u1AAHHamS7TBgPmlPBn0aVcPrHi7njrIU= github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230306083105-1391330d62a3 h1:tfzawK1gIIgRjVZeANXOr0Ziu+kqCIBuKMe0TXfl5Aw= github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230306083105-1391330d62a3/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=