handle errors correctly

This commit is contained in:
Simon Jentzsch 2018-10-18 12:59:00 +02:00
parent d9100736b4
commit e51f274f42
3 changed files with 24 additions and 13 deletions

View file

@ -18,6 +18,7 @@
package state package state
import ( import (
"errors"
"fmt" "fmt"
"math/big" "math/big"
"sort" "sort"
@ -264,22 +265,22 @@ func (n *ProofList) Put(key []byte, value []byte) error {
return nil return nil
} }
// returns the MerkleProof for a given Account // GetProof returns the MerkleProof for a given Account
func (self *StateDB) GetProof(a common.Address) [][]byte { func (self *StateDB) GetProof(a common.Address) ([][]byte, error) {
var proof ProofList var proof ProofList
self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
return proof return proof, err
} }
// returns the StorageProof for given key // GetProof returns the StorageProof for given key
func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) [][]byte { func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
trie := self.StorageTrie(a) trie := self.StorageTrie(a)
if trie == nil { if trie == nil {
return [][]byte{} return [][]byte{}, errors.New("storage trie for requested address does not exist")
} }
var proof ProofList var proof ProofList
trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
return proof return proof, err
} }
// GetCommittedState retrieves a value from the given account's committed storage trie. // GetCommittedState retrieves a value from the given account's committed storage trie.

View file

@ -35,8 +35,8 @@ type StateDB interface {
SetNonce(common.Address, uint64) SetNonce(common.Address, uint64)
GetCodeHash(common.Address) common.Hash GetCodeHash(common.Address) common.Hash
GetProof(common.Address) [][]byte GetProof(common.Address) ([][]byte, error)
GetStorageProof(common.Address, common.Hash) [][]byte GetStorageProof(common.Address, common.Hash) ([][]byte, error)
GetCode(common.Address) []byte GetCode(common.Address) []byte
SetCode(common.Address, []byte) SetCode(common.Address, []byte)
GetCodeSize(common.Address) int GetCodeSize(common.Address) int

View file

@ -541,15 +541,25 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
// create the proof for the storageKeys // create the proof for the storageKeys
for i, key := range storageKeys { for i, key := range storageKeys {
if storageTrie != nil { 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 { } else {
storageProof[i] = StorageResult{key, common.Hash{}, []string{}} storageProof[i] = StorageResult{key, common.Hash{}, []string{}}
} }
} }
// create the accountProof
accountProof, proofErr := state.GetProof(address)
if proofErr != nil {
return nil, proofErr
}
return &AccountResult{ return &AccountResult{
Address: address, Address: address,
AccountProof: common.ToHexArray(state.GetProof(address)), AccountProof: common.ToHexArray(accountProof),
Balance: (*hexutil.Big)(state.GetBalance(address)), Balance: (*hexutil.Big)(state.GetBalance(address)),
CodeHash: codeHash, CodeHash: codeHash,
Nonce: hexutil.Uint64(state.GetNonce(address)), Nonce: hexutil.Uint64(state.GetNonce(address)),