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

Function TestInvalidNumberAnnouncement

protocols/cpc/fetcher/fetcher_test.go:565–602  ·  view source on GitHub ↗

Tests that peers announcing blocks with invalid numbers (i.e. not matching the headers provided afterwards) get dropped as malicious.

(t *testing.T)

Source from the content-addressed store, hash-verified

563// Tests that peers announcing blocks with invalid numbers (i.e. not matching
564// the headers provided afterwards) get dropped as malicious.
565func TestInvalidNumberAnnouncement(t *testing.T) {
566 // Create a single block to import and check numbers against
567 hashes, blocks := makeChain(1, 0, genesis)
568
569 tester := newTester()
570 badHeaderFetcher := tester.makeHeaderFetcher("bad", blocks, -gatherSlack)
571 badBodyFetcher := tester.makeBodyFetcher("bad", blocks, 0)
572
573 imported := make(chan *types.Block)
574 tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
575
576 // Announce a block with a bad number, check for immediate drop
577 tester.fetcher.Notify("bad", hashes[0], 2, time.Now().Add(-arriveTimeout), badHeaderFetcher, badBodyFetcher)
578 verifyImportEvent(t, imported, false)
579
580 tester.lock.RLock()
581 dropped := tester.drops["bad"]
582 tester.lock.RUnlock()
583
584 if !dropped {
585 t.Fatalf("peer with invalid numbered announcement not dropped")
586 }
587
588 goodHeaderFetcher := tester.makeHeaderFetcher("good", blocks, -gatherSlack)
589 goodBodyFetcher := tester.makeBodyFetcher("good", blocks, 0)
590 // Make sure a good announcement passes without a drop
591 tester.fetcher.Notify("good", hashes[0], 1, time.Now().Add(-arriveTimeout), goodHeaderFetcher, goodBodyFetcher)
592 verifyImportEvent(t, imported, true)
593
594 tester.lock.RLock()
595 dropped = tester.drops["good"]
596 tester.lock.RUnlock()
597
598 if dropped {
599 t.Fatalf("peer with valid numbered announcement dropped")
600 }
601 verifyImportDone(t, imported)
602}
603
604// Tests that if a block is empty (i.e. header only), no body request should be
605// made, and instead the header should be assembled into a whole block in itself.

Callers

nothing calls this directly

Calls 9

makeChainFunction · 0.85
newTesterFunction · 0.85
verifyImportEventFunction · 0.85
verifyImportDoneFunction · 0.85
makeHeaderFetcherMethod · 0.80
makeBodyFetcherMethod · 0.80
AddMethod · 0.65
NowMethod · 0.65
NotifyMethod · 0.45

Tested by

no test coverage detected