eth: fix accountrange at, unit test passing

This commit is contained in:
Jared Wasinger 2019-04-26 13:23:54 -07:00 committed by Guillaume Ballet
parent 49f6bc3ed9
commit adcecf3e08
2 changed files with 13 additions and 15 deletions

View file

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

View file

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