From 23f47b77a0c8a308e0a1a164db46561a97722da5 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 18 Mar 2019 13:21:56 -0700 Subject: [PATCH] eth: squash this --- eth/api.go | 12 +++++++----- eth/api_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/eth/api.go b/eth/api.go index 608a1141a0..1cdc2a4150 100644 --- a/eth/api.go +++ b/eth/api.go @@ -341,10 +341,15 @@ type AccountRangeResult struct { Next common.Address `json:"next"` } -func accountRange(st state.Trie, start *common.Address, maxResult int) (AccountRangeResult, error) { +func accountRange(st state.Trie, start *common.Address, maxResults 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++ { + + if maxResults > AccountRangeAtMaxResults { + maxResults = AccountRangeAtMaxResults + } + + for i := 0; i < maxResults && it.Next(); i++ { if preimage := st.GetKey(it.Key); preimage != nil { result.Addresses = append(result.Addresses, common.BytesToAddress(preimage)) } else { @@ -373,9 +378,6 @@ func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, startAddr *commo var err error block := api.eth.blockchain.CurrentBlock() - if maxResults > AccountRangeAtMaxResults { - maxResults = AccountRangeAtMaxResults - } if len(block.Transactions()) == 0 { parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) diff --git a/eth/api_test.go b/eth/api_test.go index cdd5bb8e34..9d7fe18398 100644 --- a/eth/api_test.go +++ b/eth/api_test.go @@ -17,6 +17,8 @@ package eth import ( + "math/big" + "fmt" "reflect" "testing" @@ -28,6 +30,31 @@ import ( var dumper = spew.ConfigState{Indent: " "} +func TestAccountRangeAt(t *testing.T) { + var ( + state, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) + addrs = [512]common.Address{} + ) + + for i := 0; i < 512; i++ { + addr := fmt.Sprintf("%x", i) + addrs[i] = common.HexToAddress(addr) + } + + for i := range addrs { + state.SetBalance(addrs[i], big.NewInt(1)) + } + + // test retrieving less than max results + +/* + result := accountRangeAt(state.Trie(), common.Address{0x0}, 128) + if len(result.Addresses) != 128 { + t.Fatalf("expected 128 results. Got %d", len(result.Addresses)) + } +*/ +} + func TestStorageRangeAt(t *testing.T) { // Create a state where account 0x010000... has a few storage entries. var (