github.com/anthdm/go-ethereum@v1.8.4-0.20180412101906-60516c83b011/trie/sync_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "bytes" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/ethdb" 25 ) 26 27 // makeTestTrie create a sample test trie to test node-wise reconstruction. 28 func makeTestTrie() (*Database, *Trie, map[string][]byte) { 29 // Create an empty trie 30 diskdb, _ := ethdb.NewMemDatabase() 31 triedb := NewDatabase(diskdb) 32 trie, _ := New(common.Hash{}, triedb) 33 34 // Fill it with some arbitrary data 35 content := make(map[string][]byte) 36 for i := byte(0); i < 255; i++ { 37 // Map the same data under multiple keys 38 key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i} 39 content[string(key)] = val 40 trie.Update(key, val) 41 42 key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i} 43 content[string(key)] = val 44 trie.Update(key, val) 45 46 // Add some other data to inflate the trie 47 for j := byte(3); j < 13; j++ { 48 key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i} 49 content[string(key)] = val 50 trie.Update(key, val) 51 } 52 } 53 trie.Commit(nil) 54 55 // Return the generated trie 56 return triedb, trie, content 57 } 58 59 // checkTrieContents cross references a reconstructed trie with an expected data 60 // content map. 61 func checkTrieContents(t *testing.T, db *Database, root []byte, content map[string][]byte) { 62 // Check root availability and trie contents 63 trie, err := New(common.BytesToHash(root), db) 64 if err != nil { 65 t.Fatalf("failed to create trie at %x: %v", root, err) 66 } 67 if err := checkTrieConsistency(db, common.BytesToHash(root)); err != nil { 68 t.Fatalf("inconsistent trie at %x: %v", root, err) 69 } 70 for key, val := range content { 71 if have := trie.Get([]byte(key)); !bytes.Equal(have, val) { 72 t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val) 73 } 74 } 75 } 76 77 // checkTrieConsistency checks that all nodes in a trie are indeed present. 78 func checkTrieConsistency(db *Database, root common.Hash) error { 79 // Create and iterate a trie rooted in a subnode 80 trie, err := New(root, db) 81 if err != nil { 82 return nil // Consider a non existent state consistent 83 } 84 it := trie.NodeIterator(nil) 85 for it.Next(true) { 86 } 87 return it.Error() 88 } 89 90 // Tests that an empty trie is not scheduled for syncing. 91 func TestEmptyTrieSync(t *testing.T) { 92 diskdbA, _ := ethdb.NewMemDatabase() 93 triedbA := NewDatabase(diskdbA) 94 95 diskdbB, _ := ethdb.NewMemDatabase() 96 triedbB := NewDatabase(diskdbB) 97 98 emptyA, _ := New(common.Hash{}, triedbA) 99 emptyB, _ := New(emptyRoot, triedbB) 100 101 for i, trie := range []*Trie{emptyA, emptyB} { 102 diskdb, _ := ethdb.NewMemDatabase() 103 if req := NewTrieSync(trie.Hash(), diskdb, nil).Missing(1); len(req) != 0 { 104 t.Errorf("test %d: content requested for empty trie: %v", i, req) 105 } 106 } 107 } 108 109 // Tests that given a root hash, a trie can sync iteratively on a single thread, 110 // requesting retrieval tasks and returning all of them in one go. 111 func TestIterativeTrieSyncIndividual(t *testing.T) { testIterativeTrieSync(t, 1) } 112 func TestIterativeTrieSyncBatched(t *testing.T) { testIterativeTrieSync(t, 100) } 113 114 func testIterativeTrieSync(t *testing.T, batch int) { 115 // Create a random trie to copy 116 srcDb, srcTrie, srcData := makeTestTrie() 117 118 // Create a destination trie and sync with the scheduler 119 diskdb, _ := ethdb.NewMemDatabase() 120 triedb := NewDatabase(diskdb) 121 sched := NewTrieSync(srcTrie.Hash(), diskdb, nil) 122 123 queue := append([]common.Hash{}, sched.Missing(batch)...) 124 for len(queue) > 0 { 125 results := make([]SyncResult, len(queue)) 126 for i, hash := range queue { 127 data, err := srcDb.Node(hash) 128 if err != nil { 129 t.Fatalf("failed to retrieve node data for %x: %v", hash, err) 130 } 131 results[i] = SyncResult{hash, data} 132 } 133 if _, index, err := sched.Process(results); err != nil { 134 t.Fatalf("failed to process result #%d: %v", index, err) 135 } 136 if index, err := sched.Commit(diskdb); err != nil { 137 t.Fatalf("failed to commit data #%d: %v", index, err) 138 } 139 queue = append(queue[:0], sched.Missing(batch)...) 140 } 141 // Cross check that the two tries are in sync 142 checkTrieContents(t, triedb, srcTrie.Root(), srcData) 143 } 144 145 // Tests that the trie scheduler can correctly reconstruct the state even if only 146 // partial results are returned, and the others sent only later. 147 func TestIterativeDelayedTrieSync(t *testing.T) { 148 // Create a random trie to copy 149 srcDb, srcTrie, srcData := makeTestTrie() 150 151 // Create a destination trie and sync with the scheduler 152 diskdb, _ := ethdb.NewMemDatabase() 153 triedb := NewDatabase(diskdb) 154 sched := NewTrieSync(srcTrie.Hash(), diskdb, nil) 155 156 queue := append([]common.Hash{}, sched.Missing(10000)...) 157 for len(queue) > 0 { 158 // Sync only half of the scheduled nodes 159 results := make([]SyncResult, len(queue)/2+1) 160 for i, hash := range queue[:len(results)] { 161 data, err := srcDb.Node(hash) 162 if err != nil { 163 t.Fatalf("failed to retrieve node data for %x: %v", hash, err) 164 } 165 results[i] = SyncResult{hash, data} 166 } 167 if _, index, err := sched.Process(results); err != nil { 168 t.Fatalf("failed to process result #%d: %v", index, err) 169 } 170 if index, err := sched.Commit(diskdb); err != nil { 171 t.Fatalf("failed to commit data #%d: %v", index, err) 172 } 173 queue = append(queue[len(results):], sched.Missing(10000)...) 174 } 175 // Cross check that the two tries are in sync 176 checkTrieContents(t, triedb, srcTrie.Root(), srcData) 177 } 178 179 // Tests that given a root hash, a trie can sync iteratively on a single thread, 180 // requesting retrieval tasks and returning all of them in one go, however in a 181 // random order. 182 func TestIterativeRandomTrieSyncIndividual(t *testing.T) { testIterativeRandomTrieSync(t, 1) } 183 func TestIterativeRandomTrieSyncBatched(t *testing.T) { testIterativeRandomTrieSync(t, 100) } 184 185 func testIterativeRandomTrieSync(t *testing.T, batch int) { 186 // Create a random trie to copy 187 srcDb, srcTrie, srcData := makeTestTrie() 188 189 // Create a destination trie and sync with the scheduler 190 diskdb, _ := ethdb.NewMemDatabase() 191 triedb := NewDatabase(diskdb) 192 sched := NewTrieSync(srcTrie.Hash(), diskdb, nil) 193 194 queue := make(map[common.Hash]struct{}) 195 for _, hash := range sched.Missing(batch) { 196 queue[hash] = struct{}{} 197 } 198 for len(queue) > 0 { 199 // Fetch all the queued nodes in a random order 200 results := make([]SyncResult, 0, len(queue)) 201 for hash := range queue { 202 data, err := srcDb.Node(hash) 203 if err != nil { 204 t.Fatalf("failed to retrieve node data for %x: %v", hash, err) 205 } 206 results = append(results, SyncResult{hash, data}) 207 } 208 // Feed the retrieved results back and queue new tasks 209 if _, index, err := sched.Process(results); err != nil { 210 t.Fatalf("failed to process result #%d: %v", index, err) 211 } 212 if index, err := sched.Commit(diskdb); err != nil { 213 t.Fatalf("failed to commit data #%d: %v", index, err) 214 } 215 queue = make(map[common.Hash]struct{}) 216 for _, hash := range sched.Missing(batch) { 217 queue[hash] = struct{}{} 218 } 219 } 220 // Cross check that the two tries are in sync 221 checkTrieContents(t, triedb, srcTrie.Root(), srcData) 222 } 223 224 // Tests that the trie scheduler can correctly reconstruct the state even if only 225 // partial results are returned (Even those randomly), others sent only later. 226 func TestIterativeRandomDelayedTrieSync(t *testing.T) { 227 // Create a random trie to copy 228 srcDb, srcTrie, srcData := makeTestTrie() 229 230 // Create a destination trie and sync with the scheduler 231 diskdb, _ := ethdb.NewMemDatabase() 232 triedb := NewDatabase(diskdb) 233 sched := NewTrieSync(srcTrie.Hash(), diskdb, nil) 234 235 queue := make(map[common.Hash]struct{}) 236 for _, hash := range sched.Missing(10000) { 237 queue[hash] = struct{}{} 238 } 239 for len(queue) > 0 { 240 // Sync only half of the scheduled nodes, even those in random order 241 results := make([]SyncResult, 0, len(queue)/2+1) 242 for hash := range queue { 243 data, err := srcDb.Node(hash) 244 if err != nil { 245 t.Fatalf("failed to retrieve node data for %x: %v", hash, err) 246 } 247 results = append(results, SyncResult{hash, data}) 248 249 if len(results) >= cap(results) { 250 break 251 } 252 } 253 // Feed the retrieved results back and queue new tasks 254 if _, index, err := sched.Process(results); err != nil { 255 t.Fatalf("failed to process result #%d: %v", index, err) 256 } 257 if index, err := sched.Commit(diskdb); err != nil { 258 t.Fatalf("failed to commit data #%d: %v", index, err) 259 } 260 for _, result := range results { 261 delete(queue, result.Hash) 262 } 263 for _, hash := range sched.Missing(10000) { 264 queue[hash] = struct{}{} 265 } 266 } 267 // Cross check that the two tries are in sync 268 checkTrieContents(t, triedb, srcTrie.Root(), srcData) 269 } 270 271 // Tests that a trie sync will not request nodes multiple times, even if they 272 // have such references. 273 func TestDuplicateAvoidanceTrieSync(t *testing.T) { 274 // Create a random trie to copy 275 srcDb, srcTrie, srcData := makeTestTrie() 276 277 // Create a destination trie and sync with the scheduler 278 diskdb, _ := ethdb.NewMemDatabase() 279 triedb := NewDatabase(diskdb) 280 sched := NewTrieSync(srcTrie.Hash(), diskdb, nil) 281 282 queue := append([]common.Hash{}, sched.Missing(0)...) 283 requested := make(map[common.Hash]struct{}) 284 285 for len(queue) > 0 { 286 results := make([]SyncResult, len(queue)) 287 for i, hash := range queue { 288 data, err := srcDb.Node(hash) 289 if err != nil { 290 t.Fatalf("failed to retrieve node data for %x: %v", hash, err) 291 } 292 if _, ok := requested[hash]; ok { 293 t.Errorf("hash %x already requested once", hash) 294 } 295 requested[hash] = struct{}{} 296 297 results[i] = SyncResult{hash, data} 298 } 299 if _, index, err := sched.Process(results); err != nil { 300 t.Fatalf("failed to process result #%d: %v", index, err) 301 } 302 if index, err := sched.Commit(diskdb); err != nil { 303 t.Fatalf("failed to commit data #%d: %v", index, err) 304 } 305 queue = append(queue[:0], sched.Missing(0)...) 306 } 307 // Cross check that the two tries are in sync 308 checkTrieContents(t, triedb, srcTrie.Root(), srcData) 309 } 310 311 // Tests that at any point in time during a sync, only complete sub-tries are in 312 // the database. 313 func TestIncompleteTrieSync(t *testing.T) { 314 // Create a random trie to copy 315 srcDb, srcTrie, _ := makeTestTrie() 316 317 // Create a destination trie and sync with the scheduler 318 diskdb, _ := ethdb.NewMemDatabase() 319 triedb := NewDatabase(diskdb) 320 sched := NewTrieSync(srcTrie.Hash(), diskdb, nil) 321 322 added := []common.Hash{} 323 queue := append([]common.Hash{}, sched.Missing(1)...) 324 for len(queue) > 0 { 325 // Fetch a batch of trie nodes 326 results := make([]SyncResult, len(queue)) 327 for i, hash := range queue { 328 data, err := srcDb.Node(hash) 329 if err != nil { 330 t.Fatalf("failed to retrieve node data for %x: %v", hash, err) 331 } 332 results[i] = SyncResult{hash, data} 333 } 334 // Process each of the trie nodes 335 if _, index, err := sched.Process(results); err != nil { 336 t.Fatalf("failed to process result #%d: %v", index, err) 337 } 338 if index, err := sched.Commit(diskdb); err != nil { 339 t.Fatalf("failed to commit data #%d: %v", index, err) 340 } 341 for _, result := range results { 342 added = append(added, result.Hash) 343 } 344 // Check that all known sub-tries in the synced trie are complete 345 for _, root := range added { 346 if err := checkTrieConsistency(triedb, root); err != nil { 347 t.Fatalf("trie inconsistent: %v", err) 348 } 349 } 350 // Fetch the next batch to retrieve 351 queue = append(queue[:0], sched.Missing(1)...) 352 } 353 // Sanity check that removing any node from the database is detected 354 for _, node := range added[1:] { 355 key := node.Bytes() 356 value, _ := diskdb.Get(key) 357 358 diskdb.Delete(key) 359 if err := checkTrieConsistency(triedb, added[0]); err == nil { 360 t.Fatalf("trie inconsistency not caught, missing: %x", key) 361 } 362 diskdb.Put(key, value) 363 } 364 }