MCPcopy Create free account
hub / github.com/CPChain/chain / testIterativeRandomStateSync

Function testIterativeRandomStateSync

core/state/sync_test.go:206–242  ·  view source on GitHub ↗
(t *testing.T, batch int)

Source from the content-addressed store, hash-verified

204func TestIterativeRandomStateSyncBatched(t *testing.T) { testIterativeRandomStateSync(t, 100) }
205
206func testIterativeRandomStateSync(t *testing.T, batch int) {
207 // Create a random state to copy
208 srcDb, srcRoot, srcAccounts := makeTestState()
209
210 // Create a destination state and sync with the scheduler
211 dstDb := database.NewMemDatabase()
212 sched := NewStateSync(srcRoot, dstDb)
213
214 queue := make(map[common.Hash]struct{})
215 for _, hash := range sched.Missing(batch) {
216 queue[hash] = struct{}{}
217 }
218 for len(queue) > 0 {
219 // Fetch all the queued nodes in a random order
220 results := make([]trie.SyncResult, 0, len(queue))
221 for hash := range queue {
222 data, err := srcDb.TrieDB().Node(hash)
223 if err != nil {
224 t.Fatalf("failed to retrieve node data for %x", hash)
225 }
226 results = append(results, trie.SyncResult{Hash: hash, Data: data})
227 }
228 // Feed the retrieved results back and queue new tasks
229 if _, index, err := sched.Process(results); err != nil {
230 t.Fatalf("failed to process result #%d: %v", index, err)
231 }
232 if index, err := sched.Commit(dstDb); err != nil {
233 t.Fatalf("failed to commit data #%d: %v", index, err)
234 }
235 queue = make(map[common.Hash]struct{})
236 for _, hash := range sched.Missing(batch) {
237 queue[hash] = struct{}{}
238 }
239 }
240 // Cross check that the two states are in sync
241 checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
242}
243
244// Tests that the trie scheduler can correctly reconstruct the state even if only
245// partial results are returned (Even those randomly), others sent only later.

Calls 6

makeTestStateFunction · 0.85
NewStateSyncFunction · 0.85
checkStateAccountsFunction · 0.85
TrieDBMethod · 0.65
ProcessMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected