@@ -33,29 +33,24 @@ import (
3333
3434var dumper = spew.ConfigState {Indent : " " }
3535
36- func accountRangeTest (t * testing.T , trie * state.Trie , statedb * state.StateDB , start * common.Hash , requestedNum int , expectedNum int ) AccountRangeResult {
37- result , err := accountRange (* trie , start , requestedNum )
38- if err != nil {
39- t .Fatal (err )
40- }
36+ func accountRangeTest (t * testing.T , trie * state.Trie , statedb * state.StateDB , start common.Hash , requestedNum int , expectedNum int ) state.IteratorDump {
37+ result := statedb .IteratorDump (true , true , false , start .Bytes (), requestedNum )
4138
4239 if len (result .Accounts ) != expectedNum {
43- t .Fatalf ("expected %d results. Got %d" , expectedNum , len (result .Accounts ))
40+ t .Fatalf ("expected %d results, got %d" , expectedNum , len (result .Accounts ))
4441 }
45-
46- for _ , address := range result .Accounts {
47- if address == nil {
48- t .Fatalf ("null address returned" )
42+ for address := range result .Accounts {
43+ if address == (common.Address {}) {
44+ t .Fatalf ("empty address returned" )
4945 }
50- if ! statedb .Exist (* address ) {
46+ if ! statedb .Exist (address ) {
5147 t .Fatalf ("account not found in state %s" , address .Hex ())
5248 }
5349 }
54-
5550 return result
5651}
5752
58- type resultHash []* common.Hash
53+ type resultHash []common.Hash
5954
6055func (h resultHash ) Len () int { return len (h ) }
6156func (h resultHash ) Swap (i , j int ) { h [i ], h [j ] = h [j ], h [i ] }
@@ -80,76 +75,47 @@ func TestAccountRange(t *testing.T) {
8075 m [addr ] = true
8176 }
8277 }
83-
8478 state .Commit (true )
8579 root := state .IntermediateRoot (true )
8680
8781 trie , err := statedb .OpenTrie (root )
8882 if err != nil {
8983 t .Fatal (err )
9084 }
91-
92- t .Logf ("test getting number of results less than max" )
93- accountRangeTest (t , & trie , state , & common.Hash {0x0 }, AccountRangeMaxResults / 2 , AccountRangeMaxResults / 2 )
94-
95- t .Logf ("test getting number of results greater than max %d" , AccountRangeMaxResults )
96- accountRangeTest (t , & trie , state , & common.Hash {0x0 }, AccountRangeMaxResults * 2 , AccountRangeMaxResults )
97-
98- t .Logf ("test with empty 'start' hash" )
99- accountRangeTest (t , & trie , state , nil , AccountRangeMaxResults , AccountRangeMaxResults )
100-
101- t .Logf ("test pagination" )
102-
85+ accountRangeTest (t , & trie , state , common.Hash {}, AccountRangeMaxResults / 2 , AccountRangeMaxResults / 2 )
10386 // test pagination
104- firstResult := accountRangeTest (t , & trie , state , & common.Hash {0x0 }, AccountRangeMaxResults , AccountRangeMaxResults )
105-
106- t .Logf ("test pagination 2" )
107- secondResult := accountRangeTest (t , & trie , state , & firstResult .Next , AccountRangeMaxResults , AccountRangeMaxResults )
87+ firstResult := accountRangeTest (t , & trie , state , common.Hash {}, AccountRangeMaxResults , AccountRangeMaxResults )
88+ secondResult := accountRangeTest (t , & trie , state , common .BytesToHash (firstResult .Next ), AccountRangeMaxResults , AccountRangeMaxResults )
10889
10990 hList := make (resultHash , 0 )
110- for h1 , addr1 := range firstResult .Accounts {
111- h := & common.Hash {}
112- h .SetBytes (h1 .Bytes ())
113- hList = append (hList , h )
114- for h2 , addr2 := range secondResult .Accounts {
115- // Make sure that the hashes aren't the same
116- if bytes .Equal (h1 .Bytes (), h2 .Bytes ()) {
117- t .Fatalf ("pagination test failed: results should not overlap" )
118- }
119-
120- // If either address is nil, then it makes no sense to compare
121- // them as they might be two different accounts.
122- if addr1 == nil || addr2 == nil {
123- continue
124- }
125-
126- // Since the two hashes are different, they should not have
127- // the same preimage, but let's check anyway in case there
128- // is a bug in the (hash, addr) map generation code.
129- if bytes .Equal (addr1 .Bytes (), addr2 .Bytes ()) {
130- t .Fatalf ("pagination test failed: addresses should not repeat" )
131- }
91+ for addr1 := range firstResult .Accounts {
92+ // If address is empty, then it makes no sense to compare
93+ // them as they might be two different accounts.
94+ if addr1 == (common.Address {}) {
95+ continue
96+ }
97+ if _ , duplicate := secondResult .Accounts [addr1 ]; duplicate {
98+ t .Fatalf ("pagination test failed: results should not overlap" )
13299 }
100+ hList = append (hList , crypto .Keccak256Hash (addr1 .Bytes ()))
133101 }
134-
135102 // Test to see if it's possible to recover from the middle of the previous
136103 // set and get an even split between the first and second sets.
137- t .Logf ("test random access pagination" )
138104 sort .Sort (hList )
139105 middleH := hList [AccountRangeMaxResults / 2 ]
140106 middleResult := accountRangeTest (t , & trie , state , middleH , AccountRangeMaxResults , AccountRangeMaxResults )
141- innone , infirst , insecond := 0 , 0 , 0
107+ missing , infirst , insecond := 0 , 0 , 0
142108 for h := range middleResult .Accounts {
143109 if _ , ok := firstResult .Accounts [h ]; ok {
144110 infirst ++
145111 } else if _ , ok := secondResult .Accounts [h ]; ok {
146112 insecond ++
147113 } else {
148- innone ++
114+ missing ++
149115 }
150116 }
151- if innone != 0 {
152- t .Fatalf ("%d hashes in the 'middle' set were neither in the first not the second set" , innone )
117+ if missing != 0 {
118+ t .Fatalf ("%d hashes in the 'middle' set were neither in the first not the second set" , missing )
153119 }
154120 if infirst != AccountRangeMaxResults / 2 {
155121 t .Fatalf ("Imbalance in the number of first-test results: %d != %d" , infirst , AccountRangeMaxResults / 2 )
@@ -164,20 +130,10 @@ func TestEmptyAccountRange(t *testing.T) {
164130 statedb = state .NewDatabase (rawdb .NewMemoryDatabase ())
165131 state , _ = state .New (common.Hash {}, statedb , nil )
166132 )
167-
168133 state .Commit (true )
169- root := state .IntermediateRoot (true )
170-
171- trie , err := statedb .OpenTrie (root )
172- if err != nil {
173- t .Fatal (err )
174- }
175-
176- results , err := accountRange (trie , & common.Hash {0x0 }, AccountRangeMaxResults )
177- if err != nil {
178- t .Fatalf ("Empty results should not trigger an error: %v" , err )
179- }
180- if results .Next != common .HexToHash ("0" ) {
134+ state .IntermediateRoot (true )
135+ results := state .IteratorDump (true , true , true , (common.Hash {}).Bytes (), AccountRangeMaxResults )
136+ if bytes .Equal (results .Next , (common.Hash {}).Bytes ()) {
181137 t .Fatalf ("Empty results should not return a second page" )
182138 }
183139 if len (results .Accounts ) != 0 {
0 commit comments