From 65c4bb2caf5029c8f33651097b5091178cb0fcbb Mon Sep 17 00:00:00 2001 From: Simon Jentzsch Date: Thu, 18 Oct 2018 11:16:59 +0200 Subject: [PATCH] added comments and refactored based on comments from holiman --- core/state/statedb.go | 4 ++-- internal/ethapi/api.go | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 788449c9e2..c76bcfb2b6 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -268,7 +268,7 @@ func (n *ProofList) Put(key []byte, value []byte) error { func (self *StateDB) GetProof(a common.Address) [][]byte { var proof ProofList self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) - return [][]byte(proof) + return proof } // returns the StorageProof for given key @@ -279,7 +279,7 @@ func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) [][]byte } var proof ProofList trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) - return [][]byte(proof) + return proof } // GetCommittedState retrieves a value from the given account's committed storage trie. diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index cf947919f9..b8ee710b4c 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -514,28 +514,32 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre codeHash := state.GetCodeHash(address) storageProof := make([]map[string]interface{}, len(storageKeys)) + // if we have a storageTrie, (which means the account exists), we can update the storagehash if storageTrie != nil { storageHash = storageTrie.Hash() } else { + // no storageTrie means the account does not exist, so the codeHash is the hash of an empty bytearray. codeHash = crypto.Keccak256Hash(nil) } - for i := range storageKeys { + // create the proof for the storageKeys + for i, key := range storageKeys { if storageTrie != nil { storageProof[i] = map[string]interface{}{ - "key": storageKeys[i], - "value": state.GetState(address, common.HexToHash(storageKeys[i])), - "proof": common.ToHexArray(state.GetStorageProof(address, common.HexToHash(storageKeys[i]))), + "key": key, + "value": state.GetState(address, common.HexToHash(key)), + "proof": common.ToHexArray(state.GetStorageProof(address, common.HexToHash(key))), } } else { storageProof[i] = map[string]interface{}{ - "key": storageKeys[i], + "key": key, "value": common.Hash{}, "proof": []string{}, } } } + // fill results for the account fields := map[string]interface{}{ "address": address, "accountProof": common.ToHexArray(state.GetProof(address)),