eth: clean up accountRange, add unit tests

This commit is contained in:
Jared Wasinger 2019-03-22 10:28:55 -07:00 committed by Guillaume Ballet
parent c74bd0edaf
commit 045772e70e
2 changed files with 16 additions and 15 deletions

View file

@ -345,8 +345,8 @@ func accountRange(st state.Trie, start *common.Address, maxResults int) (Account
it := trie.NewIterator(st.NodeIterator(crypto.Keccak256(start[:]))) it := trie.NewIterator(st.NodeIterator(crypto.Keccak256(start[:])))
result := AccountRangeResult{Addresses: []common.Address{}, Next: common.Address{}} result := AccountRangeResult{Addresses: []common.Address{}, Next: common.Address{}}
if maxResults > AccountRangeAtMaxResults { if maxResults > AccountRangeMaxResults {
maxResults = AccountRangeAtMaxResults maxResults = AccountRangeMaxResults
} }
for i := 0; i < maxResults && it.Next(); i++ { for i := 0; i < maxResults && it.Next(); i++ {
@ -369,7 +369,7 @@ func accountRange(st state.Trie, start *common.Address, maxResults int) (Account
} }
const ( const (
AccountRangeAtMaxResults = 256 AccountRangeMaxResults = 256
) )
// AccountRangeAt enumerates all accounts in the latest state // AccountRangeAt enumerates all accounts in the latest state

View file

@ -31,7 +31,7 @@ import (
var dumper = spew.ConfigState{Indent: " "} var dumper = spew.ConfigState{Indent: " "}
func accountRangeExpect(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.Address, requestedNum int, expectedNum int) AccountRangeResult {
result, err := accountRange(*trie, start, requestedNum) result, err := accountRange(*trie, start, requestedNum)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -54,15 +54,12 @@ func TestAccountRangeAt(t *testing.T) {
var ( var (
statedb = state.NewDatabase(ethdb.NewMemDatabase()) statedb = state.NewDatabase(ethdb.NewMemDatabase())
state, _ = state.New(common.Hash{}, statedb) state, _ = state.New(common.Hash{}, statedb)
addrs = [512]common.Address{} addrs = [AccountRangeMaxResults * 2]common.Address{}
) )
for i := 0; i < 512; i++ { for i := range addrs {
addr := fmt.Sprintf("%x", i) addr := fmt.Sprintf("%x", i)
addrs[i] = common.HexToAddress(addr) addrs[i] = common.HexToAddress(addr)
}
for i := range addrs {
state.SetBalance(addrs[i], big.NewInt(1)) state.SetBalance(addrs[i], big.NewInt(1))
} }
@ -74,15 +71,19 @@ func TestAccountRangeAt(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// test getting number of results less than max t.Logf("test getting number of results less than max")
accountRangeExpect(t, &trie, state, &common.Address{0x0}, 128, 128) accountRangeTest(t, &trie, state, &common.Address{0x0}, AccountRangeMaxResults / 2, AccountRangeMaxResults / 2)
// test getting number of results greater than max t.Logf("test getting number of results greater than max %d", AccountRangeMaxResults)
accountRangeExpect(t, &trie, state, &common.Address{0x0}, 512, 256) accountRangeTest(t, &trie, state, &common.Address{0x0}, AccountRangeMaxResults * 2, AccountRangeMaxResults)
t.Logf("test pagination")
// test pagination // test pagination
firstResult := accountRangeExpect(t, &trie, state, &common.Address{0x0}, 128, 128) firstResult := accountRangeTest(t, &trie, state, &common.Address{0x0}, AccountRangeMaxResults, AccountRangeMaxResults)
secondResult := accountRangeExpect(t, &trie, state, &firstResult.Next, 128, 128)
t.Logf("test pagination 2")
secondResult := accountRangeTest(t, &trie, state, &firstResult.Next, AccountRangeMaxResults, AccountRangeMaxResults)
for i := range firstResult.Addresses { for i := range firstResult.Addresses {
for j := range secondResult.Addresses { for j := range secondResult.Addresses {