internal/ethapi: fix codehash lookup in eth_getProof

This commit is contained in:
Martin Holst Swende 2023-10-16 19:52:48 +02:00
parent 1e55e52b26
commit ca99a741c6
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -676,8 +676,7 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
keyLengths = make([]int, len(storageKeys))
storageProof = make([]StorageResult, len(storageKeys))
storageTrie state.Trie
storageHash = types.EmptyRootHash
storageRoot = types.EmptyRootHash
codeHash = types.EmptyCodeHash
)
// Deserialize all keys. This prevents state access on invalid input.
@ -692,20 +691,15 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
if state == nil || err != nil {
return nil, err
}
if storageRoot := state.GetStorageRoot(address); storageRoot != types.EmptyRootHash && storageRoot != (common.Hash{}) {
codeHash = state.GetCodeHash(address)
storageRoot = state.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)
tr, err := trie.NewStateTrie(id, state.Database().TrieDB())
storageTrie, err := trie.NewStateTrie(id, state.Database().TrieDB())
if err != nil {
return nil, err
}
storageTrie = tr
}
// If we have a storageTrie, the account exists and we must update
// the storage root hash and the code hash.
if storageTrie != nil {
storageHash = storageTrie.Hash()
codeHash = state.GetCodeHash(address)
}
// Create the proofs for the storageKeys.
for i, key := range keys {
// Output key encoding is a bit special: if the input was a 32-byte hash, it is
@ -730,7 +724,7 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
value := (*hexutil.Big)(state.GetState(address, key).Big())
storageProof[i] = StorageResult{outputKey, value, proof}
}
}
// Create the accountProof.
tr, err := trie.NewStateTrie(trie.StateTrieID(header.Root), state.Database().TrieDB())
if err != nil {
@ -746,7 +740,7 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
Balance: (*hexutil.Big)(state.GetBalance(address)),
CodeHash: codeHash,
Nonce: hexutil.Uint64(state.GetNonce(address)),
StorageHash: storageHash,
StorageHash: storageRoot,
StorageProof: storageProof,
}, state.Error()
}