core/state: simplify storage trie update and commit (#28030)

This change improves function description and simplifies logic in statedb update and commit operations.
This commit is contained in:
rjl493456442
2023-08-31 14:33:18 -04:00
committed by GitHub
parent 53f3c2ae65
commit 0acc0a1f86
5 changed files with 88 additions and 129 deletions
+21 -22
View File
@@ -46,6 +46,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"github.com/tyler-smith/go-bip39"
)
@@ -674,9 +675,10 @@ 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))
storageTrie state.Trie
storageHash = types.EmptyRootHash
codeHash = types.EmptyCodeHash
storageTrie state.Trie
storageHash = types.EmptyRootHash
codeHash = types.EmptyCodeHash
)
// Deserialize all keys. This prevents state access on invalid input.
for i, hexKey := range storageKeys {
@@ -686,15 +688,18 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
return nil, err
}
}
state, _, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
state, header, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
if state == nil || err != nil {
return nil, err
}
if storageTrie, err = state.StorageTrie(address); err != nil {
return nil, err
if storageRoot := state.GetStorageRoot(address); storageRoot != types.EmptyRootHash && storageRoot != (common.Hash{}) {
id := trie.StorageTrieID(header.Root, crypto.Keccak256Hash(address.Bytes()), storageRoot)
tr, 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 {
@@ -727,14 +732,17 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
}
// Create the accountProof.
accountProof, proofErr := state.GetProof(address)
if proofErr != nil {
return nil, proofErr
tr, err := trie.NewStateTrie(trie.StateTrieID(header.Root), state.Database().TrieDB())
if err != nil {
return nil, err
}
var accountProof proofList
if err := tr.Prove(crypto.Keccak256(address.Bytes()), &accountProof); err != nil {
return nil, err
}
return &AccountResult{
Address: address,
AccountProof: toHexSlice(accountProof),
AccountProof: accountProof,
Balance: (*hexutil.Big)(state.GetBalance(address)),
CodeHash: codeHash,
Nonce: hexutil.Uint64(state.GetNonce(address)),
@@ -2245,12 +2253,3 @@ func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error {
}
return nil
}
// toHexSlice creates a slice of hex-strings based on []byte.
func toHexSlice(b [][]byte) []string {
r := make([]string, len(b))
for i := range b {
r[i] = hexutil.Encode(b[i])
}
return r
}