Tests that peers announcing blocks with invalid numbers (i.e. not matching the headers provided afterwards) get dropped as malicious.
(t *testing.T)
| 563 | // Tests that peers announcing blocks with invalid numbers (i.e. not matching |
| 564 | // the headers provided afterwards) get dropped as malicious. |
| 565 | func 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. |
nothing calls this directly
no test coverage detected