Skip to content

Commit

Permalink
StateReader: remove CodeHash parameter (#12936)
Browse files Browse the repository at this point in the history
- moved empty-code-hash check to callers: `ibs` and `stateObject`
- removed CodeHash param from state reader interface (E3 does store code
by acc address. E2 did by code hash)
  • Loading branch information
AskAlexSharov authored Dec 2, 2024
1 parent 067f3b0 commit 73b12da
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/integration/commands/state_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func requestDomains(chainDb, stateDb kv.RwDB, ctx context.Context, readDomain st
}
case "code":
for _, addr := range addrs {
code, err := r.ReadAccountCode(libcommon.BytesToAddress(addr), 0, libcommon.Hash{})
code, err := r.ReadAccountCode(libcommon.BytesToAddress(addr), 0)
if err != nil {
logger.Error("failed to read code", "addr", addr, "err", err)
continue
Expand Down
11 changes: 4 additions & 7 deletions core/state/cached_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ func (cr *CachedReader) ReadAccountStorage(address common.Address, incarnation u

// ReadAccountCode is called when code of an account needs to be fetched from the state
// Usually, one of (address;incarnation) or codeHash is enough to uniquely identify the code
func (cr *CachedReader) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
if codeHash == emptyCodeHashH {
return nil, nil
}
func (cr *CachedReader) ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error) {
if c, ok := cr.cache.GetCode(address.Bytes(), incarnation); ok {
return c, nil
}
c, err := cr.r.ReadAccountCode(address, incarnation, codeHash)
c, err := cr.r.ReadAccountCode(address, incarnation)
if err != nil {
return nil, err
}
Expand All @@ -95,8 +92,8 @@ func (cr *CachedReader) ReadAccountCode(address common.Address, incarnation uint
return c, nil
}

func (cr *CachedReader) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
c, err := cr.ReadAccountCode(address, incarnation, codeHash)
func (cr *CachedReader) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
c, err := cr.ReadAccountCode(address, incarnation)
return len(c), err
}

Expand Down
11 changes: 3 additions & 8 deletions core/state/cached_reader3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package state

import (
"bytes"

"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/kv/kvcache"
Expand Down Expand Up @@ -83,19 +81,16 @@ func (r *CachedReader3) ReadAccountStorage(address common.Address, incarnation u
return enc, nil
}

func (r *CachedReader3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
if bytes.Equal(codeHash.Bytes(), emptyCodeHash) {
return nil, nil
}
func (r *CachedReader3) ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error) {
code, err := r.cache.GetCode(address[:])
if len(code) == 0 {
return nil, nil
}
return code, err
}

func (r *CachedReader3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
code, err := r.ReadAccountCode(address, incarnation, codeHash)
func (r *CachedReader3) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
code, err := r.ReadAccountCode(address, incarnation)
return len(code), err
}

Expand Down
4 changes: 2 additions & 2 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type StateReader interface {
ReadAccountData(address common.Address) (*accounts.Account, error)
ReadAccountDataForDebug(address common.Address) (*accounts.Account, error)
ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error)
ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error)
ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error)
ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error)
ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error)
ReadAccountIncarnation(address common.Address) (uint64, error)
}

Expand Down
15 changes: 6 additions & 9 deletions core/state/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,7 @@ func TestChangeAccountCodeBetweenBlocks(t *testing.T) {
sd.SetTxNum(2)
sd.SetBlockNum(1)

oldCodeHash := libcommon.BytesToHash(crypto.Keccak256(oldCode))
trieCode, tcErr := r.ReadAccountCode(contract, 1, oldCodeHash)
trieCode, tcErr := r.ReadAccountCode(contract, 1)
assert.NoError(t, tcErr, "you can receive the new code")
assert.Equal(t, oldCode, trieCode, "new code should be received")

Expand All @@ -1379,8 +1378,7 @@ func TestChangeAccountCodeBetweenBlocks(t *testing.T) {
t.Errorf("error finalising 1st tx: %v", err)
}

newCodeHash := libcommon.BytesToHash(crypto.Keccak256(newCode))
trieCode, tcErr = r.ReadAccountCode(contract, 1, newCodeHash)
trieCode, tcErr = r.ReadAccountCode(contract, 1)
assert.NoError(t, tcErr, "you can receive the new code")
assert.Equal(t, newCode, trieCode, "new code should be received")

Expand Down Expand Up @@ -1417,12 +1415,11 @@ func TestCacheCodeSizeSeparately(t *testing.T) {
t.Errorf("error committing block: %v", err)
}

codeHash := libcommon.BytesToHash(crypto.Keccak256(code))
codeSize, err := r.ReadAccountCodeSize(contract, 1, codeHash)
codeSize, err := r.ReadAccountCodeSize(contract, 1)
assert.NoError(t, err, "you can receive the new code")
assert.Equal(t, len(code), codeSize, "new code should be received")

code2, err := r.ReadAccountCode(contract, 1, codeHash)
code2, err := r.ReadAccountCode(contract, 1)
assert.NoError(t, err, "you can receive the new code")
assert.Equal(t, code, code2, "new code should be received")
}
Expand Down Expand Up @@ -1461,13 +1458,13 @@ func TestCacheCodeSizeInTrie(t *testing.T) {
require.EqualValues(t, root, libcommon.CastToHash(r2))

codeHash := libcommon.BytesToHash(crypto.Keccak256(code))
codeSize, err := r.ReadAccountCodeSize(contract, 1, codeHash)
codeSize, err := r.ReadAccountCodeSize(contract, 1)
assert.NoError(t, err, "you can receive the code size ")
assert.Equal(t, len(code), codeSize, "you can receive the code size")

assert.NoError(t, tx.Delete(kv.Code, codeHash[:]), nil)

codeSize2, err := r.ReadAccountCodeSize(contract, 1, codeHash)
codeSize2, err := r.ReadAccountCodeSize(contract, 1)
assert.NoError(t, err, "you can still receive code size even with empty DB")
assert.Equal(t, len(code), codeSize2, "code size should be received even with empty DB")

Expand Down
9 changes: 3 additions & 6 deletions core/state/history_reader_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,17 @@ func (hr *HistoryReaderV3) ReadAccountStorage(address common.Address, incarnatio
return enc, err
}

func (hr *HistoryReaderV3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
if codeHash == emptyCodeHashH {
return nil, nil
}
func (hr *HistoryReaderV3) ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error) {
// must pass key2=Nil here: because Erigon4 does concatinate key1+key2 under the hood
//code, _, err := hr.ttx.GetAsOf(kv.CodeDomain, address.Bytes(), codeHash.Bytes(), hr.txNum)
code, _, err := hr.ttx.GetAsOf(kv.CodeDomain, address[:], nil, hr.txNum)
if hr.trace {
fmt.Printf("ReadAccountCode [%x %x] => [%x]\n", address, codeHash, code)
fmt.Printf("ReadAccountCode [%x] => [%x]\n", address, code)
}
return code, err
}

func (hr *HistoryReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
func (hr *HistoryReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
enc, _, err := hr.ttx.GetAsOf(kv.CodeDomain, address[:], nil, hr.txNum)
return len(enc), err
}
Expand Down
5 changes: 4 additions & 1 deletion core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,10 @@ func (sdb *IntraBlockState) GetCodeSize(addr libcommon.Address) (int, error) {
if stateObject.code != nil {
return len(stateObject.code), nil
}
l, err := sdb.stateReader.ReadAccountCodeSize(addr, stateObject.data.Incarnation, stateObject.data.CodeHash)
if stateObject.data.CodeHash == emptyCodeHashH {
return 0, nil
}
l, err := sdb.stateReader.ReadAccountCodeSize(addr, stateObject.data.Incarnation)
if err != nil {
sdb.setErrorUnsafe(err)
return l, err
Expand Down
11 changes: 4 additions & 7 deletions core/state/rw_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,7 @@ func (r *ReaderV3) ReadAccountStorage(address common.Address, incarnation uint64
return enc, nil
}

func (r *ReaderV3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
//if codeHash == emptyCodeHashH { // TODO: how often do we have this case on mainnet/bor-mainnet?
// return nil, nil
//}
func (r *ReaderV3) ReadAccountCode(address common.Address, incarnation uint64) ([]byte, error) {
enc, _, err := r.tx.GetLatest(kv.CodeDomain, address[:], nil)
if err != nil {
return nil, err
Expand All @@ -643,7 +640,7 @@ func (r *ReaderV3) ReadAccountCode(address common.Address, incarnation uint64, c
return enc, nil
}

func (r *ReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
func (r *ReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
enc, _, err := r.tx.GetLatest(kv.CodeDomain, address[:], nil)
if err != nil {
return 0, err
Expand Down Expand Up @@ -754,7 +751,7 @@ func (r *ReaderParallelV3) ReadAccountStorage(address common.Address, incarnatio
return enc, nil
}

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

func (r *ReaderParallelV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
func (r *ReaderParallelV3) ReadAccountCodeSize(address common.Address, incarnation uint64) (int, error) {
enc, _, err := r.sd.GetLatest(kv.CodeDomain, address[:], nil)
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (so *stateObject) Code() []byte {
if so.data.CodeHash == emptyCodeHashH {
return nil
}
code, err := so.db.stateReader.ReadAccountCode(so.Address(), so.data.Incarnation, so.data.CodeHash)
code, err := so.db.stateReader.ReadAccountCode(so.Address(), so.data.Incarnation)
if err != nil {
so.setError(fmt.Errorf("can't load code hash %x: %w", so.data.CodeHash, err))
}
Expand Down
2 changes: 1 addition & 1 deletion eth/stagedsync/stage_mining_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func filterBadTransactions(transactions []types.Transaction, chainID *uint256.In
if !account.IsEmptyCodeHash() {
isEoaCodeAllowed := false
if config.IsPrague(header.Time) {
code, err := simStateReader.ReadAccountCode(sender, account.Incarnation, account.CodeHash)
code, err := simStateReader.ReadAccountCode(sender, account.Incarnation)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/eth_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (api *APIImpl) GetCode(ctx context.Context, address libcommon.Address, bloc
if acc == nil || err != nil {
return hexutility.Bytes(""), nil
}
res, _ := reader.ReadAccountCode(address, acc.Incarnation, acc.CodeHash)
res, _ := reader.ReadAccountCode(address, acc.Incarnation)
if res == nil {
return hexutility.Bytes(""), nil
}
Expand Down

0 comments on commit 73b12da

Please sign in to comment.