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
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.

View file

@ -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

View file

@ -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)),