From adcecf3e088f420d339b87c8e594ba22f46eb820 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Fri, 26 Apr 2019 13:23:54 -0700 Subject: [PATCH] eth: fix accountrange at, unit test passing --- eth/api.go | 16 +++++++--------- eth/api_test.go | 12 ++++++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/eth/api.go b/eth/api.go index c72217aabe..3ca8cf716d 100644 --- a/eth/api.go +++ b/eth/api.go @@ -35,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" @@ -338,12 +337,12 @@ func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs, type AccountRangeResult struct { Addresses []common.Address `json:"addresses"` - Next common.Address `json:"next"` + Next common.Hash `json:"next"` } -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{}} +func accountRange(st state.Trie, start *common.Hash, maxResults int) (AccountRangeResult, error) { + it := trie.NewIterator(st.NodeIterator(start.Bytes())) + result := AccountRangeResult{Addresses: []common.Address{}, Next: common.Hash{}} if maxResults > AccountRangeMaxResults { maxResults = AccountRangeMaxResults @@ -359,7 +358,7 @@ func accountRange(st state.Trie, start *common.Address, maxResults int) (Account if it.Next() { if preimage := st.GetKey(it.Key); preimage != nil { - result.Next = common.BytesToAddress(preimage) + result.Next = common.BytesToHash(it.Key) } else { return AccountRangeResult{}, fmt.Errorf("preimage not found for 0x%s", hex.EncodeToString(it.Key)) } @@ -373,12 +372,11 @@ const ( ) // AccountRangeAt enumerates all accounts in the latest state -func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, startAddr *common.Address, maxResults int) (AccountRangeResult, error) { +func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, start *common.Hash, maxResults int) (AccountRangeResult, error) { var statedb *state.StateDB var err error block := api.eth.blockchain.CurrentBlock() - if len(block.Transactions()) == 0 { parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) if parent == nil { @@ -400,7 +398,7 @@ func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, startAddr *commo return AccountRangeResult{}, err } - return accountRange(trie, startAddr, maxResults) + return accountRange(trie, start, maxResults) } // StorageRangeResult is the result of a debug_storageRangeAt API call. diff --git a/eth/api_test.go b/eth/api_test.go index d7feacbd11..fd60165a01 100644 --- a/eth/api_test.go +++ b/eth/api_test.go @@ -27,13 +27,13 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" ) var dumper = spew.ConfigState{Indent: " "} -func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, start *common.Address, requestedNum int, expectedNum int) AccountRangeResult { +func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, start *common.Hash, requestedNum int, expectedNum int) AccountRangeResult { result, err := accountRange(*trie, start, requestedNum) if err != nil { t.Fatal(err) @@ -57,7 +57,7 @@ func TestAccountRangeAt(t *testing.T) { statedb = state.NewDatabase(ethdb.NewMemDatabase()) state, _ = state.New(common.Hash{}, statedb) addrs = [AccountRangeMaxResults * 2]common.Address{} - m = map[common.Address]bool{} + m = map[common.Address]bool{} ) for i := range addrs { @@ -81,15 +81,15 @@ func TestAccountRangeAt(t *testing.T) { } t.Logf("test getting number of results less than max") - accountRangeTest(t, &trie, state, &common.Address{0x0}, AccountRangeMaxResults / 2, AccountRangeMaxResults / 2) + accountRangeTest(t, &trie, state, &common.Hash{0x0}, AccountRangeMaxResults/2, AccountRangeMaxResults/2) t.Logf("test getting number of results greater than max %d", AccountRangeMaxResults) - accountRangeTest(t, &trie, state, &common.Address{0x0}, AccountRangeMaxResults * 2, AccountRangeMaxResults) + accountRangeTest(t, &trie, state, &common.Hash{0x0}, AccountRangeMaxResults*2, AccountRangeMaxResults) t.Logf("test pagination") // test pagination - firstResult := accountRangeTest(t, &trie, state, &common.Address{0x0}, AccountRangeMaxResults, AccountRangeMaxResults) + firstResult := accountRangeTest(t, &trie, state, &common.Hash{0x0}, AccountRangeMaxResults, AccountRangeMaxResults) t.Logf("test pagination 2") secondResult := accountRangeTest(t, &trie, state, &firstResult.Next, AccountRangeMaxResults, AccountRangeMaxResults)