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