Skip to content

Commit

Permalink
internal/ethapi: fix regression in getstorage for eth_getProof
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Oct 16, 2023
1 parent ca99a74 commit d9f7ac9
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,6 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
keys = make([]common.Hash, len(storageKeys))
keyLengths = make([]int, len(storageKeys))
storageProof = make([]StorageResult, len(storageKeys))

storageRoot = types.EmptyRootHash
codeHash = types.EmptyCodeHash
)
// Deserialize all keys. This prevents state access on invalid input.
for i, hexKey := range storageKeys {
Expand All @@ -687,18 +684,22 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
return nil, err
}
}
state, header, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
if state == nil || err != nil {
statedb, header, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
if statedb == nil || err != nil {
return nil, err
}
codeHash = state.GetCodeHash(address)
storageRoot = state.GetStorageRoot(address)
codeHash := statedb.GetCodeHash(address)
storageRoot := statedb.GetStorageRoot(address)

if len(keys) > 0 && storageRoot != types.EmptyRootHash && storageRoot != (common.Hash{}) { // Create storage proof
id := trie.StorageTrieID(header.Root, crypto.Keccak256Hash(address.Bytes()), storageRoot)
storageTrie, err := trie.NewStateTrie(id, state.Database().TrieDB())
if err != nil {
return nil, err
if len(keys) > 0 {
var storageTrie state.Trie
if storageRoot != types.EmptyRootHash && storageRoot != (common.Hash{}) {
id := trie.StorageTrieID(header.Root, crypto.Keccak256Hash(address.Bytes()), storageRoot)
st, err := trie.NewStateTrie(id, statedb.Database().TrieDB())
if err != nil {
return nil, err
}
storageTrie = st
}
// Create the proofs for the storageKeys.
for i, key := range keys {
Expand All @@ -712,7 +713,6 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
} else {
outputKey = hexutil.Encode(key[:])
}

if storageTrie == nil {
storageProof[i] = StorageResult{outputKey, &hexutil.Big{}, []string{}}
continue
Expand All @@ -721,12 +721,12 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
if err := storageTrie.Prove(crypto.Keccak256(key.Bytes()), &proof); err != nil {
return nil, err
}
value := (*hexutil.Big)(state.GetState(address, key).Big())
value := (*hexutil.Big)(statedb.GetState(address, key).Big())
storageProof[i] = StorageResult{outputKey, value, proof}
}
}
// Create the accountProof.
tr, err := trie.NewStateTrie(trie.StateTrieID(header.Root), state.Database().TrieDB())
tr, err := trie.NewStateTrie(trie.StateTrieID(header.Root), statedb.Database().TrieDB())
if err != nil {
return nil, err
}
Expand All @@ -737,12 +737,12 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
return &AccountResult{
Address: address,
AccountProof: accountProof,
Balance: (*hexutil.Big)(state.GetBalance(address)),
Balance: (*hexutil.Big)(statedb.GetBalance(address)),
CodeHash: codeHash,
Nonce: hexutil.Uint64(state.GetNonce(address)),
Nonce: hexutil.Uint64(statedb.GetNonce(address)),
StorageHash: storageRoot,
StorageProof: storageProof,
}, state.Error()
}, statedb.Error()
}

// decodeHash parses a hex-encoded 32-byte hash. The input may optionally
Expand Down

0 comments on commit d9f7ac9

Please sign in to comment.