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

Function TestImportDeduplication

protocols/cpc/fetcher/fetcher_test.go:455–490  ·  view source on GitHub ↗

Tests that blocks arriving from various sources (multiple propagations, hash announces, etc) do not get scheduled for import multiple times.

(t *testing.T)

Source from the content-addressed store, hash-verified

453// Tests that blocks arriving from various sources (multiple propagations, hash
454// announces, etc) do not get scheduled for import multiple times.
455func TestImportDeduplication(t *testing.T) {
456 // Create two blocks to import (one for duplication, the other for stalling)
457 hashes, blocks := makeChain(2, 0, genesis)
458
459 // Create the tester and wrap the importer with a counter
460 tester := newTester()
461 headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
462 bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
463
464 counter := uint32(0)
465 tester.fetcher.insertChain = func(blocks types.Blocks) (int, error) {
466 atomic.AddUint32(&counter, uint32(len(blocks)))
467 return tester.insertChain(blocks)
468 }
469 // Instrument the fetching and imported events
470 fetching := make(chan []common.Hash)
471 imported := make(chan *types.Block, len(hashes)-1)
472 tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- hashes }
473 tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
474
475 // Announce the duplicating block, wait for retrieval, and also propagate directly
476 tester.fetcher.Notify("valid", hashes[0], 1, time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
477 <-fetching
478
479 tester.fetcher.Enqueue("valid", blocks[hashes[0]])
480 tester.fetcher.Enqueue("valid", blocks[hashes[0]])
481 tester.fetcher.Enqueue("valid", blocks[hashes[0]])
482
483 // Fill the missing block directly as if propagated, and check import uniqueness
484 tester.fetcher.Enqueue("valid", blocks[hashes[1]])
485 verifyImportCount(t, imported, 2)
486
487 if counter != 2 {
488 t.Fatalf("import invocation count mismatch: have %v, want %v", counter, 2)
489 }
490}
491
492// Tests that blocks with numbers much lower or higher than out current head get
493// discarded to prevent wasting resources on useless blocks from faulty peers.

Callers

nothing calls this directly

Calls 10

makeChainFunction · 0.85
newTesterFunction · 0.85
verifyImportCountFunction · 0.85
makeHeaderFetcherMethod · 0.80
makeBodyFetcherMethod · 0.80
EnqueueMethod · 0.80
AddMethod · 0.65
NowMethod · 0.65
insertChainMethod · 0.45
NotifyMethod · 0.45

Tested by

no test coverage detected