Skip to content

Commit

Permalink
GetLatest: single key param (#13022)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Dec 7, 2024
1 parent 131b6d1 commit 836ce24
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 80 deletions.
2 changes: 1 addition & 1 deletion core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (d *Dumper) DumpToCollector(c DumpCollector, excludeCode, excludeStorage bo
account.CodeHash = hexutility.Bytes(acc.CodeHash.Bytes())

if !excludeCode {
r, _, err := ttx.GetLatest(kv.CodeDomain, k, nil)
r, _, err := ttx.GetLatest(kv.CodeDomain, k)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions core/state/rw_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (rs *StateV3) applyState(txTask *TxTask, domains *libstate.SharedDomains) e
for addr, increase := range txTask.BalanceIncreaseSet {
increase := increase
addrBytes := addr.Bytes()
enc0, step0, err := domains.GetLatest(kv.AccountsDomain, addrBytes, nil)
enc0, step0, err := domains.GetLatest(kv.AccountsDomain, addrBytes)
if err != nil {
return err
}
Expand Down Expand Up @@ -586,7 +586,7 @@ func (r *ReaderV3) SetTrace(trace bool) { r.trace = trace }
func (r *ReaderV3) ResetReadSet() {}

func (r *ReaderV3) ReadAccountData(address common.Address) (*accounts.Account, error) {
enc, _, err := r.tx.GetLatest(kv.AccountsDomain, address[:], nil)
enc, _, err := r.tx.GetLatest(kv.AccountsDomain, address[:])
if err != nil {
return nil, err
}
Expand All @@ -613,7 +613,7 @@ func (r *ReaderV3) ReadAccountDataForDebug(address common.Address) (*accounts.Ac

func (r *ReaderV3) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
r.composite = append(append(r.composite[:0], address[:]...), key.Bytes()...)
enc, _, err := r.tx.GetLatest(kv.StorageDomain, r.composite, nil)
enc, _, err := r.tx.GetLatest(kv.StorageDomain, r.composite)
if err != nil {
return nil, err
}
Expand All @@ -628,7 +628,7 @@ func (r *ReaderV3) ReadAccountStorage(address common.Address, incarnation uint64
}

func (r *ReaderV3) ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error) {
enc, _, err := r.tx.GetLatest(kv.CodeDomain, address[:], nil)
enc, _, err := r.tx.GetLatest(kv.CodeDomain, address[:])
if err != nil {
return nil, err
}
Expand All @@ -639,7 +639,7 @@ func (r *ReaderV3) ReadAccountCode(address common.Address, incarnation uint64) (
}

func (r *ReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
enc, _, err := r.tx.GetLatest(kv.CodeDomain, address[:], nil)
enc, _, err := r.tx.GetLatest(kv.CodeDomain, address[:])
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -681,7 +681,7 @@ func (r *ReaderParallelV3) SetTrace(trace bool) { r.trace = tra
func (r *ReaderParallelV3) ResetReadSet() { r.readLists = newReadList() }

func (r *ReaderParallelV3) ReadAccountData(address common.Address) (*accounts.Account, error) {
enc, _, err := r.sd.GetLatest(kv.AccountsDomain, address[:], nil)
enc, _, err := r.sd.GetLatest(kv.AccountsDomain, address[:])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -709,7 +709,7 @@ func (r *ReaderParallelV3) ReadAccountData(address common.Address) (*accounts.Ac
// ReadAccountDataForDebug - is like ReadAccountData, but without adding key to `readList`.
// Used to get `prev` account balance
func (r *ReaderParallelV3) ReadAccountDataForDebug(address common.Address) (*accounts.Account, error) {
enc, _, err := r.sd.GetLatest(kv.AccountsDomain, address[:], nil)
enc, _, err := r.sd.GetLatest(kv.AccountsDomain, address[:])
if err != nil {
return nil, err
}
Expand All @@ -732,7 +732,7 @@ func (r *ReaderParallelV3) ReadAccountDataForDebug(address common.Address) (*acc

func (r *ReaderParallelV3) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
r.composite = append(append(r.composite[:0], address[:]...), key.Bytes()...)
enc, _, err := r.sd.GetLatest(kv.StorageDomain, r.composite, nil)
enc, _, err := r.sd.GetLatest(kv.StorageDomain, r.composite)
if err != nil {
return nil, err
}
Expand All @@ -750,7 +750,7 @@ func (r *ReaderParallelV3) ReadAccountStorage(address common.Address, incarnatio
}

func (r *ReaderParallelV3) ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error) {
enc, _, err := r.sd.GetLatest(kv.CodeDomain, address[:], nil)
enc, _, err := r.sd.GetLatest(kv.CodeDomain, address[:])
if err != nil {
return nil, err
}
Expand All @@ -765,7 +765,7 @@ func (r *ReaderParallelV3) ReadAccountCode(address common.Address, incarnation u
}

func (r *ReaderParallelV3) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
enc, _, err := r.sd.GetLatest(kv.CodeDomain, address[:], nil)
enc, _, err := r.sd.GetLatest(kv.CodeDomain, address[:])
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/kv/kv_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ type (
)

type TemporalGetter interface {
GetLatest(name Domain, k, k2 []byte) (v []byte, step uint64, err error)
GetLatest(name Domain, k []byte) (v []byte, step uint64, err error)
}
type TemporalTx interface {
Tx
Expand Down
6 changes: 3 additions & 3 deletions erigon-lib/kv/kvcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ func (c *Coherent) Get(k []byte, tx kv.Tx, id uint64) (v []byte, err error) {

if c.cfg.StateV3 {
if len(k) == 20 {
v, _, err = tx.(kv.TemporalTx).GetLatest(kv.AccountsDomain, k, nil)
v, _, err = tx.(kv.TemporalTx).GetLatest(kv.AccountsDomain, k)
} else {
v, _, err = tx.(kv.TemporalTx).GetLatest(kv.StorageDomain, k, nil)
v, _, err = tx.(kv.TemporalTx).GetLatest(kv.StorageDomain, k)
}
} else {
v, err = tx.GetOne(kv.PlainState, k)
Expand Down Expand Up @@ -443,7 +443,7 @@ func (c *Coherent) GetCode(k []byte, tx kv.Tx, id uint64) (v []byte, err error)
c.codeMiss.Inc()

if c.cfg.StateV3 {
v, _, err = tx.(kv.TemporalTx).GetLatest(kv.CodeDomain, k, nil)
v, _, err = tx.(kv.TemporalTx).GetLatest(kv.CodeDomain, k)
} else {
v, err = tx.GetOne(kv.Code, k)
}
Expand Down
6 changes: 3 additions & 3 deletions erigon-lib/kv/kvcache/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ func (c *DummyCache) Len() int { return 0 }
func (c *DummyCache) Get(k []byte, tx kv.Tx, id uint64) ([]byte, error) {
if c.stateV3 {
if len(k) == 20 {
v, _, err := tx.(kv.TemporalTx).GetLatest(kv.AccountsDomain, k, nil)
v, _, err := tx.(kv.TemporalTx).GetLatest(kv.AccountsDomain, k)
return v, err
}
v, _, err := tx.(kv.TemporalTx).GetLatest(kv.StorageDomain, k, nil)
v, _, err := tx.(kv.TemporalTx).GetLatest(kv.StorageDomain, k)
return v, err
}
return tx.GetOne(kv.PlainState, k)
}
func (c *DummyCache) GetCode(k []byte, tx kv.Tx, id uint64) ([]byte, error) {
if c.stateV3 {
v, _, err := tx.(kv.TemporalTx).GetLatest(kv.CodeDomain, k, nil)
v, _, err := tx.(kv.TemporalTx).GetLatest(kv.CodeDomain, k)
return v, err
}
return tx.GetOne(kv.Code, k)
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/kv/membatchwithdb/memory_mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,9 @@ func (m *MemoryMutation) AggTx() any {
return m.db.(hasAggCtx).AggTx()
}

func (m *MemoryMutation) GetLatest(name kv.Domain, k, k2 []byte) (v []byte, step uint64, err error) {
func (m *MemoryMutation) GetLatest(name kv.Domain, k []byte) (v []byte, step uint64, err error) {
// panic("not supported")
return m.db.(kv.TemporalTx).GetLatest(name, k, k2)
return m.db.(kv.TemporalTx).GetLatest(name, k)
}

func (m *MemoryMutation) GetAsOf(name kv.Domain, k []byte, ts uint64) (v []byte, ok bool, err error) {
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/kv/remotedb/kv_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ func (tx *tx) GetAsOf(name kv.Domain, k []byte, ts uint64) (v []byte, ok bool, e
return reply.V, reply.Ok, nil
}

func (tx *tx) GetLatest(name kv.Domain, k, k2 []byte) (v []byte, step uint64, err error) {
reply, err := tx.db.remoteKV.GetLatest(tx.ctx, &remote.GetLatestReq{TxId: tx.id, Table: name.String(), K: k, K2: k2, Latest: true})
func (tx *tx) GetLatest(name kv.Domain, k []byte) (v []byte, step uint64, err error) {
reply, err := tx.db.remoteKV.GetLatest(tx.ctx, &remote.GetLatestReq{TxId: tx.id, Table: name.String(), K: k, Latest: true})
if err != nil {
return nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/kv/remotedbserver/remotedbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func (s *KvServer) GetLatest(_ context.Context, req *remote.GetLatestReq) (reply
return errors.New("server DB doesn't implement kv.Temporal interface")
}
if req.Latest {
reply.V, _, err = ttx.GetLatest(domainName, req.K, req.K2)
reply.V, _, err = ttx.GetLatest(domainName, req.K)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/kv/temporal/kv_temporal.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ func (tx *Tx) RangeAsOf(name kv.Domain, fromKey, toKey []byte, asOfTs uint64, as
return it, nil
}

func (tx *Tx) GetLatest(name kv.Domain, k, k2 []byte) (v []byte, step uint64, err error) {
v, step, ok, err := tx.filesTx.GetLatest(name, k, k2, tx.MdbxTx)
func (tx *Tx) GetLatest(name kv.Domain, k []byte) (v []byte, step uint64, err error) {
v, step, ok, err := tx.filesTx.GetLatest(name, k, tx.MdbxTx)
if err != nil {
return nil, step, err
}
Expand Down
6 changes: 3 additions & 3 deletions erigon-lib/state/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ func (ac *AggregatorRoTx) CanUnwindToBlockNum(tx kv.Tx) (uint64, error) {
return 0, err
}
if minUnwindale == math.MaxUint64 { // no unwindable block found
stateVal, _, _, err := ac.d[kv.CommitmentDomain].GetLatest(keyCommitmentState, nil, tx)
stateVal, _, _, err := ac.d[kv.CommitmentDomain].GetLatest(keyCommitmentState, tx)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -1853,8 +1853,8 @@ func (ac *AggregatorRoTx) getAsOfFile(name kv.Domain, key []byte, ts uint64) (v
func (ac *AggregatorRoTx) GetAsOf(tx kv.Tx, name kv.Domain, k []byte, ts uint64) (v []byte, ok bool, err error) {
return ac.d[name].GetAsOf(k, ts, tx)
}
func (ac *AggregatorRoTx) GetLatest(domain kv.Domain, k, k2 []byte, tx kv.Tx) (v []byte, step uint64, ok bool, err error) {
return ac.d[domain].GetLatest(k, k2, tx)
func (ac *AggregatorRoTx) GetLatest(domain kv.Domain, k []byte, tx kv.Tx) (v []byte, step uint64, ok bool, err error) {
return ac.d[domain].GetLatest(k, tx)
}

// search key in all files of all domains and print file names
Expand Down
8 changes: 4 additions & 4 deletions erigon-lib/state/aggregator_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ func Fuzz_AggregatorV3_Merge(f *testing.F) {
var v [8]byte
binary.BigEndian.PutUint64(v[:], txNum)
if txNum%135 == 0 {
pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey2, nil, rwTx)
pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey2, rwTx)
require.NoError(t, err)

err = domains.DomainPut(kv.CommitmentDomain, commKey2, nil, v[:], pv, step)
require.NoError(t, err)
otherMaxWrite = txNum
} else {
pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey1, nil, rwTx)
pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey1, rwTx)
require.NoError(t, err)

err = domains.DomainPut(kv.CommitmentDomain, commKey1, nil, v[:], pv, step)
Expand Down Expand Up @@ -159,13 +159,13 @@ func Fuzz_AggregatorV3_Merge(f *testing.F) {

dc := agg.BeginFilesRo()

v, _, ex, err := dc.GetLatest(kv.CommitmentDomain, commKey1, nil, roTx)
v, _, ex, err := dc.GetLatest(kv.CommitmentDomain, commKey1, roTx)
require.NoError(t, err)
require.Truef(t, ex, "key %x not found", commKey1)

require.EqualValues(t, maxWrite, binary.BigEndian.Uint64(v[:]))

v, _, ex, err = dc.GetLatest(kv.CommitmentDomain, commKey2, nil, roTx)
v, _, ex, err = dc.GetLatest(kv.CommitmentDomain, commKey2, roTx)
require.NoError(t, err)
require.Truef(t, ex, "key %x not found", commKey2)
dc.Close()
Expand Down
24 changes: 12 additions & 12 deletions erigon-lib/state/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func aggregatorV3_RestartOnDatadir(t *testing.T, rc runCfg) {
defer roTx.Rollback()

dc := anotherAgg.BeginFilesRo()
v, _, ex, err := dc.GetLatest(kv.CommitmentDomain, someKey, nil, roTx)
v, _, ex, err := dc.GetLatest(kv.CommitmentDomain, someKey, roTx)
require.NoError(t, err)
require.True(t, ex)
dc.Close()
Expand Down Expand Up @@ -471,7 +471,7 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
switch {
case r <= 33:
buf := types.EncodeAccountBytesV3(txNum, uint256.NewInt(txNum*100_000), nil, 0)
prev, step, err := domains.GetLatest(kv.AccountsDomain, key, nil)
prev, step, err := domains.GetLatest(kv.AccountsDomain, key)
require.NoError(t, err)

usedKeys[string(key)] = struct{}{}
Expand All @@ -491,7 +491,7 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
}
usedKeys[string(key)] = struct{}{}

prev, step, err := domains.GetLatest(kv.CodeDomain, key, nil)
prev, step, err := domains.GetLatest(kv.CodeDomain, key)
require.NoError(t, err)

err = domains.DomainPut(kv.CodeDomain, key, nil, codeUpd, prev, step)
Expand All @@ -511,7 +511,7 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
key = key[:length.Addr]
}

prev, step, err := domains.GetLatest(kv.AccountsDomain, key, nil)
prev, step, err := domains.GetLatest(kv.AccountsDomain, key)
require.NoError(t, err)
if prev == nil {
usedKeys[string(key)] = struct{}{}
Expand All @@ -528,7 +528,7 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
copy(sk[length.Addr:], loc)
usedKeys[string(sk)] = struct{}{}

prev, step, err := domains.GetLatest(kv.StorageDomain, sk[:length.Addr], sk[length.Addr:])
prev, step, err := domains.GetLatest(kv.StorageDomain, sk[:length.Addr])
require.NoError(t, err)

err = domains.DomainPut(kv.StorageDomain, sk[:length.Addr], sk[length.Addr:], uint256.NewInt(txNum).Bytes(), prev, step)
Expand Down Expand Up @@ -638,7 +638,7 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) {
if uint64(i+1) >= txs-aggStep {
continue // finishtx always stores last agg step in db which we deleted, so missing values which were not aggregated is expected
}
stored, _, _, err := ac.GetLatest(kv.AccountsDomain, key[:length.Addr], nil, newTx)
stored, _, _, err := ac.GetLatest(kv.AccountsDomain, key[:length.Addr], newTx)
require.NoError(t, err)
if len(stored) == 0 {
miss++
Expand All @@ -649,7 +649,7 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) {

require.EqualValues(t, i+1, int(nonce))

storedV, _, found, err := ac.GetLatest(kv.StorageDomain, key[:length.Addr], key[length.Addr:], newTx)
storedV, _, found, err := ac.GetLatest(kv.StorageDomain, key, newTx)
require.NoError(t, err)
require.True(t, found)
require.NotEmpty(t, storedV)
Expand Down Expand Up @@ -740,7 +740,7 @@ func TestAggregatorV3_ReplaceCommittedKeys(t *testing.T) {

addr, loc := keys[txNum-1-half][:length.Addr], keys[txNum-1-half][length.Addr:]

prev, step, _, err := ac.d[kv.StorageDomain].GetLatest(addr, loc, tx)
prev, step, _, err := ac.d[kv.StorageDomain].GetLatest(keys[txNum-1-half], tx)
require.NoError(t, err)
err = domains.DomainPut(kv.StorageDomain, addr, loc, []byte{addr[0], loc[0]}, prev, step)
require.NoError(t, err)
Expand All @@ -757,7 +757,7 @@ func TestAggregatorV3_ReplaceCommittedKeys(t *testing.T) {
defer aggCtx2.Close()

for i, key := range keys {
storedV, _, found, err := aggCtx2.d[kv.StorageDomain].GetLatest(key[:length.Addr], key[length.Addr:], tx)
storedV, _, found, err := aggCtx2.d[kv.StorageDomain].GetLatest(key, tx)
require.Truef(t, found, "key %x not found %d", key, i)
require.NoError(t, err)
require.EqualValues(t, key[0], storedV[0])
Expand Down Expand Up @@ -956,7 +956,7 @@ func TestAggregatorV3_SharedDomains(t *testing.T) {

for j := 0; j < len(keys); j++ {
buf := types.EncodeAccountBytesV3(uint64(i), uint256.NewInt(uint64(i*100_000)), nil, 0)
prev, step, err := domains.GetLatest(kv.AccountsDomain, keys[j], nil)
prev, step, err := domains.GetLatest(kv.AccountsDomain, keys[j])
require.NoError(t, err)

err = domains.DomainPut(kv.AccountsDomain, keys[j], nil, buf, prev, step)
Expand Down Expand Up @@ -991,7 +991,7 @@ func TestAggregatorV3_SharedDomains(t *testing.T) {

for j := 0; j < len(keys); j++ {
buf := types.EncodeAccountBytesV3(uint64(i), uint256.NewInt(uint64(i*100_000)), nil, 0)
prev, step, _, err := mc.GetLatest(kv.AccountsDomain, keys[j], nil, rwTx)
prev, step, _, err := mc.GetLatest(kv.AccountsDomain, keys[j], rwTx)
require.NoError(t, err)

err = domains.DomainPut(kv.AccountsDomain, keys[j], nil, buf, prev, step)
Expand Down Expand Up @@ -1028,7 +1028,7 @@ func TestAggregatorV3_SharedDomains(t *testing.T) {

for j := 0; j < len(keys); j++ {
buf := types.EncodeAccountBytesV3(uint64(i), uint256.NewInt(uint64(i*100_000)), nil, 0)
prev, step, _, err := mc.GetLatest(kv.AccountsDomain, keys[j], nil, rwTx)
prev, step, _, err := mc.GetLatest(kv.AccountsDomain, keys[j], rwTx)
require.NoError(t, err)

err = domains.DomainPut(kv.AccountsDomain, keys[j], nil, buf, prev, step)
Expand Down
9 changes: 2 additions & 7 deletions erigon-lib/state/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ func (dt *DomainRoTx) GetAsOf(key []byte, txNum uint64, roTx kv.Tx) ([]byte, boo
}
return v, v != nil, nil
}
v, _, _, err = dt.GetLatest(key, nil, roTx)
v, _, _, err = dt.GetLatest(key, roTx)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -1759,12 +1759,7 @@ func (dt *DomainRoTx) getLatestFromDb(key []byte, roTx kv.Tx) ([]byte, uint64, b

// GetLatest returns value, step in which the value last changed, and bool value which is true if the value
// is present, and false if it is not present (not set or deleted)
func (dt *DomainRoTx) GetLatest(key1, key2 []byte, roTx kv.Tx) ([]byte, uint64, bool, error) {
key := key1
if len(key2) > 0 {
key = append(append(dt.keyBuf[:0], key1...), key2...)
}

func (dt *DomainRoTx) GetLatest(key []byte, roTx kv.Tx) ([]byte, uint64, bool, error) {
var v []byte
var foundStep uint64
var found bool
Expand Down
Loading

0 comments on commit 836ce24

Please sign in to comment.