|
17 | 17 | package eth |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "math/big" |
20 | 23 | "reflect" |
| 24 | + "sort" |
21 | 25 | "testing" |
22 | 26 |
|
23 | 27 | "github.com/davecgh/go-spew/spew" |
24 | 28 | "github.com/ethereum/go-ethereum/common" |
25 | 29 | "github.com/ethereum/go-ethereum/core/rawdb" |
26 | 30 | "github.com/ethereum/go-ethereum/core/state" |
| 31 | + "github.com/ethereum/go-ethereum/crypto" |
27 | 32 | ) |
28 | 33 |
|
29 | 34 | var dumper = spew.ConfigState{Indent: " "} |
30 | 35 |
|
| 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 | + } |
| 41 | + |
| 42 | + if len(result.Accounts) != expectedNum { |
| 43 | + t.Fatalf("expected %d results. Got %d", expectedNum, len(result.Accounts)) |
| 44 | + } |
| 45 | + |
| 46 | + for _, address := range result.Accounts { |
| 47 | + if address == nil { |
| 48 | + t.Fatalf("null address returned") |
| 49 | + } |
| 50 | + if !statedb.Exist(*address) { |
| 51 | + t.Fatalf("account not found in state %s", address.Hex()) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return result |
| 56 | +} |
| 57 | + |
| 58 | +type resultHash []*common.Hash |
| 59 | + |
| 60 | +func (h resultHash) Len() int { return len(h) } |
| 61 | +func (h resultHash) Swap(i, j int) { h[i], h[j] = h[j], h[i] } |
| 62 | +func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j].Bytes()) < 0 } |
| 63 | + |
| 64 | +func TestAccountRange(t *testing.T) { |
| 65 | + var ( |
| 66 | + statedb = state.NewDatabase(rawdb.NewMemoryDatabase()) |
| 67 | + state, _ = state.New(common.Hash{}, statedb) |
| 68 | + addrs = [AccountRangeMaxResults * 2]common.Address{} |
| 69 | + m = map[common.Address]bool{} |
| 70 | + ) |
| 71 | + |
| 72 | + for i := range addrs { |
| 73 | + hash := common.HexToHash(fmt.Sprintf("%x", i)) |
| 74 | + addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes()) |
| 75 | + addrs[i] = addr |
| 76 | + state.SetBalance(addrs[i], big.NewInt(1)) |
| 77 | + if _, ok := m[addr]; ok { |
| 78 | + t.Fatalf("bad") |
| 79 | + } else { |
| 80 | + m[addr] = true |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + state.Commit(true) |
| 85 | + root := state.IntermediateRoot(true) |
| 86 | + |
| 87 | + trie, err := statedb.OpenTrie(root) |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 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 | + |
| 103 | + // 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) |
| 108 | + |
| 109 | + 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 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Test to see if it's possible to recover from the middle of the previous |
| 136 | + // set and get an even split between the first and second sets. |
| 137 | + t.Logf("test random access pagination") |
| 138 | + sort.Sort(hList) |
| 139 | + middleH := hList[AccountRangeMaxResults/2] |
| 140 | + middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults) |
| 141 | + innone, infirst, insecond := 0, 0, 0 |
| 142 | + for h := range middleResult.Accounts { |
| 143 | + if _, ok := firstResult.Accounts[h]; ok { |
| 144 | + infirst++ |
| 145 | + } else if _, ok := secondResult.Accounts[h]; ok { |
| 146 | + insecond++ |
| 147 | + } else { |
| 148 | + innone++ |
| 149 | + } |
| 150 | + } |
| 151 | + if innone != 0 { |
| 152 | + t.Fatalf("%d hashes in the 'middle' set were neither in the first not the second set", innone) |
| 153 | + } |
| 154 | + if infirst != AccountRangeMaxResults/2 { |
| 155 | + t.Fatalf("Imbalance in the number of first-test results: %d != %d", infirst, AccountRangeMaxResults/2) |
| 156 | + } |
| 157 | + if insecond != AccountRangeMaxResults/2 { |
| 158 | + t.Fatalf("Imbalance in the number of second-test results: %d != %d", insecond, AccountRangeMaxResults/2) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestEmptyAccountRange(t *testing.T) { |
| 163 | + var ( |
| 164 | + statedb = state.NewDatabase(rawdb.NewMemoryDatabase()) |
| 165 | + state, _ = state.New(common.Hash{}, statedb) |
| 166 | + ) |
| 167 | + |
| 168 | + 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") { |
| 181 | + t.Fatalf("Empty results should not return a second page") |
| 182 | + } |
| 183 | + if len(results.Accounts) != 0 { |
| 184 | + t.Fatalf("Empty state should not return addresses: %v", results.Accounts) |
| 185 | + } |
| 186 | +} |
| 187 | + |
31 | 188 | func TestStorageRangeAt(t *testing.T) { |
32 | 189 | // Create a state where account 0x010000... has a few storage entries. |
33 | 190 | var ( |
|
0 commit comments