From e51f274f42efd56b83b0568105a3aa4e85d6f6b9 Mon Sep 17 00:00:00 2001 From: Simon Jentzsch Date: Thu, 18 Oct 2018 12:59:00 +0200 Subject: [PATCH] handle errors correctly --- core/state/statedb.go | 19 ++++++++++--------- core/vm/interface.go | 4 ++-- internal/ethapi/api.go | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index c76bcfb2b6..6d229f748a 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -18,6 +18,7 @@ package state import ( + "errors" "fmt" "math/big" "sort" @@ -264,22 +265,22 @@ func (n *ProofList) Put(key []byte, value []byte) error { return nil } -// returns the MerkleProof for a given Account -func (self *StateDB) GetProof(a common.Address) [][]byte { +// GetProof returns the MerkleProof for a given Account +func (self *StateDB) GetProof(a common.Address) ([][]byte, error) { var proof ProofList - self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) - return proof + err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) + return proof, err } -// returns the StorageProof for given key -func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) [][]byte { +// GetProof returns the StorageProof for given key +func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) { trie := self.StorageTrie(a) if trie == nil { - return [][]byte{} + return [][]byte{}, errors.New("storage trie for requested address does not exist") } var proof ProofList - trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) - return proof + err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) + return proof, err } // GetCommittedState retrieves a value from the given account's committed storage trie. diff --git a/core/vm/interface.go b/core/vm/interface.go index 5cefd54d7a..fe2f3d713e 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -35,8 +35,8 @@ type StateDB interface { SetNonce(common.Address, uint64) GetCodeHash(common.Address) common.Hash - GetProof(common.Address) [][]byte - GetStorageProof(common.Address, common.Hash) [][]byte + GetProof(common.Address) ([][]byte, error) + GetStorageProof(common.Address, common.Hash) ([][]byte, error) GetCode(common.Address) []byte SetCode(common.Address, []byte) GetCodeSize(common.Address) int diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 284039d3e6..fd60242e69 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -541,15 +541,25 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre // create the proof for the storageKeys for i, key := range storageKeys { if storageTrie != nil { - storageProof[i] = StorageResult{key, state.GetState(address, common.HexToHash(key)), common.ToHexArray(state.GetStorageProof(address, common.HexToHash(key)))} + proof, storageError := state.GetStorageProof(address, common.HexToHash(key)) + if storageError != nil { + return nil, storageError + } + storageProof[i] = StorageResult{key, state.GetState(address, common.HexToHash(key)), common.ToHexArray(proof)} } else { storageProof[i] = StorageResult{key, common.Hash{}, []string{}} } } + // create the accountProof + accountProof, proofErr := state.GetProof(address) + if proofErr != nil { + return nil, proofErr + } + return &AccountResult{ Address: address, - AccountProof: common.ToHexArray(state.GetProof(address)), + AccountProof: common.ToHexArray(accountProof), Balance: (*hexutil.Big)(state.GetBalance(address)), CodeHash: codeHash, Nonce: hexutil.Uint64(state.GetNonce(address)),