Skip to content

Commit 97fb083

Browse files
simon-jentzschholiman
authored andcommitted
EIP-1186 eth_getProof (#17737)
* first impl of eth_getProof * fixed docu * added comments and refactored based on comments from holiman * created structs * handle errors correctly * change Value to *hexutil.Big in order to have the same output as parity * use ProofList as return type
1 parent cdf5982 commit 97fb083

4 files changed

Lines changed: 105 additions & 0 deletions

File tree

‎common/bytes.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ func ToHex(b []byte) string {
3131
return "0x" + hex
3232
}
3333

34+
// ToHexArray creates a array of hex-string based on []byte
35+
func ToHexArray(b [][]byte) []string {
36+
r := make([]string, len(b))
37+
for i := range b {
38+
r[i] = ToHex(b[i])
39+
}
40+
return r
41+
}
42+
3443
// FromHex returns the bytes represented by the hexadecimal string s.
3544
// s may be prefixed with "0x".
3645
func FromHex(s string) []byte {

‎core/state/statedb.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
package state
1919

2020
import (
21+
"errors"
2122
"fmt"
2223
"math/big"
2324
"sort"
2425
"sync"
2526

2627
"github.com/ethereum/go-ethereum/common"
2728
"github.com/ethereum/go-ethereum/core/types"
29+
"github.com/ethereum/go-ethereum/core/vm"
2830
"github.com/ethereum/go-ethereum/crypto"
2931
"github.com/ethereum/go-ethereum/log"
3032
"github.com/ethereum/go-ethereum/rlp"
@@ -256,6 +258,24 @@ func (self *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash
256258
return common.Hash{}
257259
}
258260

261+
// GetProof returns the MerkleProof for a given Account
262+
func (self *StateDB) GetProof(a common.Address) (vm.ProofList, error) {
263+
var proof vm.ProofList
264+
err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
265+
return proof, err
266+
}
267+
268+
// GetProof returns the StorageProof for given key
269+
func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) (vm.ProofList, error) {
270+
var proof vm.ProofList
271+
trie := self.StorageTrie(a)
272+
if trie == nil {
273+
return proof, errors.New("storage trie for requested address does not exist")
274+
}
275+
err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
276+
return proof, err
277+
}
278+
259279
// GetCommittedState retrieves a value from the given account's committed storage trie.
260280
func (self *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
261281
stateObject := self.getStateObject(addr)

‎core/vm/interface.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type StateDB interface {
3535
SetNonce(common.Address, uint64)
3636

3737
GetCodeHash(common.Address) common.Hash
38+
GetProof(common.Address) (ProofList, error)
39+
GetStorageProof(common.Address, common.Hash) (ProofList, error)
3840
GetCode(common.Address) []byte
3941
SetCode(common.Address, []byte)
4042
GetCodeSize(common.Address) int
@@ -78,3 +80,11 @@ type CallContext interface {
7880
// Create a new contract
7981
Create(env *EVM, me ContractRef, data []byte, gas, value *big.Int) ([]byte, common.Address, error)
8082
}
83+
84+
// MerkleProof
85+
type ProofList [][]byte
86+
87+
func (n *ProofList) Put(key []byte, value []byte) error {
88+
*n = append(*n, value)
89+
return nil
90+
}

‎internal/ethapi/api.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,72 @@ func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Add
508508
return (*hexutil.Big)(state.GetBalance(address)), state.Error()
509509
}
510510

511+
// Result structs for GetProof
512+
type AccountResult struct {
513+
Address common.Address `json:"address"`
514+
AccountProof []string `json:"accountProof"`
515+
Balance *hexutil.Big `json:"balance"`
516+
CodeHash common.Hash `json:"codeHash"`
517+
Nonce hexutil.Uint64 `json:"nonce"`
518+
StorageHash common.Hash `json:"storageHash"`
519+
StorageProof []StorageResult `json:"storageProof"`
520+
}
521+
type StorageResult struct {
522+
Key string `json:"key"`
523+
Value *hexutil.Big `json:"value"`
524+
Proof []string `json:"proof"`
525+
}
526+
527+
// GetProof returns the Merkle-proof for a given account and optionally some storage keys.
528+
func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Address, storageKeys []string, blockNr rpc.BlockNumber) (*AccountResult, error) {
529+
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
530+
if state == nil || err != nil {
531+
return nil, err
532+
}
533+
534+
storageTrie := state.StorageTrie(address)
535+
storageHash := types.EmptyRootHash
536+
codeHash := state.GetCodeHash(address)
537+
storageProof := make([]StorageResult, len(storageKeys))
538+
539+
// if we have a storageTrie, (which means the account exists), we can update the storagehash
540+
if storageTrie != nil {
541+
storageHash = storageTrie.Hash()
542+
} else {
543+
// no storageTrie means the account does not exist, so the codeHash is the hash of an empty bytearray.
544+
codeHash = crypto.Keccak256Hash(nil)
545+
}
546+
547+
// create the proof for the storageKeys
548+
for i, key := range storageKeys {
549+
if storageTrie != nil {
550+
proof, storageError := state.GetStorageProof(address, common.HexToHash(key))
551+
if storageError != nil {
552+
return nil, storageError
553+
}
554+
storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), common.ToHexArray(proof)}
555+
} else {
556+
storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}}
557+
}
558+
}
559+
560+
// create the accountProof
561+
accountProof, proofErr := state.GetProof(address)
562+
if proofErr != nil {
563+
return nil, proofErr
564+
}
565+
566+
return &AccountResult{
567+
Address: address,
568+
AccountProof: common.ToHexArray(accountProof),
569+
Balance: (*hexutil.Big)(state.GetBalance(address)),
570+
CodeHash: codeHash,
571+
Nonce: hexutil.Uint64(state.GetNonce(address)),
572+
StorageHash: storageHash,
573+
StorageProof: storageProof,
574+
}, state.Error()
575+
}
576+
511577
// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all
512578
// transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
513579
func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {

0 commit comments

Comments
 (0)