From 89940c09c1ca5d8582538a60b024da3909abc21f Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Wed, 5 Dec 2018 14:11:15 +0000 Subject: [PATCH] eth: accountRangeAt: only allow enumerating accounts in latest block. make use of preimage db. --- eth/api.go | 28 +++++++++++++++------------- eth/api_test.go | 2 -- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/eth/api.go b/eth/api.go index e31bfbc6ef..509f79028a 100644 --- a/eth/api.go +++ b/eth/api.go @@ -28,6 +28,7 @@ import ( "strings" "time" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core" @@ -334,28 +335,29 @@ func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs, return results, nil } -type addressMap map[common.Hash]common.Address - type AccountRangeResult struct { - AddressMap addressMap `json:"addressMap"` + Addresses []common.Address `json:"addresses"` + Next common.Address `json:"next"` } -func accountRange(st state.Trie, start *common.Hash, maxResult int) (AccountRangeResult, error) { - it := trie.NewIterator(st.NodeIterator(start[:])) - result := AccountRangeResult{AddressMap: addressMap{}} +func accountRange(st state.Trie, start *common.Address, maxResult int) (AccountRangeResult, error) { + it := trie.NewIterator(st.NodeIterator(crypto.Keccak256(start[:]))) + result := AccountRangeResult{Addresses: []common.Address{}, Next: common.Address{}} for i := 0; i < maxResult && it.Next(); i++ { - key := st.GetKey(it.Key) - // If key is nil, that means it wasn't found in the preimage database. - // This is not a problem, because we still return the hash of the key together with - // address zero and the client can very easily determine that the hash of the addres zero - // is not matching, which means it wasn't found. - result.AddressMap[common.BytesToHash(it.Key)] = common.BytesToAddress(key) + if preimage := st.GetKey(it.Key); preimage != nil { + result.Addresses = append(result.Addresses, common.BytesToAddress(preimage)) + } } + + if it.Next() { + result.Next = common.BytesToAddress(st.GetKey(it.Key)) + } + return result, nil } //block hash or number, tx index, start address hash, max results -func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, blockNr rpc.BlockNumber, txIndex int, startAddr *common.Hash, maxResults int) (AccountRangeResult, error) { +func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, txIndex int, startAddr *common.Address, maxResults int) (AccountRangeResult, error) { var statedb *state.StateDB = nil var err error = nil var block = api.eth.blockchain.CurrentBlock() diff --git a/eth/api_test.go b/eth/api_test.go index 6276d2dd8d..cdd5bb8e34 100644 --- a/eth/api_test.go +++ b/eth/api_test.go @@ -19,9 +19,7 @@ package eth import ( "reflect" "testing" - "math/big" - "github.com/ethereum/go-ethereum/crypto" "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb"