Tests that announcements with numbers much lower or higher than out current head get discarded to prevent wasting resources on useless blocks from faulty peers.
(t *testing.T)
| 524 | // head get discarded to prevent wasting resources on useless blocks from faulty |
| 525 | // peers. |
| 526 | func TestDistantAnnouncementDiscarding(t *testing.T) { |
| 527 | // Create a long chain to import and define the discard boundaries |
| 528 | hashes, blocks := makeChain(3*maxQueueDist, 0, genesis) |
| 529 | head := hashes[len(hashes)/2] |
| 530 | |
| 531 | low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1 |
| 532 | |
| 533 | // Create a tester and simulate a head block being the middle of the above chain |
| 534 | tester := newTester() |
| 535 | |
| 536 | tester.lock.Lock() |
| 537 | tester.hashes = []common.Hash{head} |
| 538 | tester.blocks = map[common.Hash]*types.Block{head: blocks[head]} |
| 539 | tester.lock.Unlock() |
| 540 | |
| 541 | headerFetcher := tester.makeHeaderFetcher("lower", blocks, -gatherSlack) |
| 542 | bodyFetcher := tester.makeBodyFetcher("lower", blocks, 0) |
| 543 | |
| 544 | fetching := make(chan struct{}, 2) |
| 545 | tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- struct{}{} } |
| 546 | |
| 547 | // Ensure that a block with a lower number than the threshold is discarded |
| 548 | tester.fetcher.Notify("lower", hashes[low], blocks[hashes[low]].NumberU64(), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
| 549 | select { |
| 550 | case <-time.After(50 * time.Millisecond): |
| 551 | case <-fetching: |
| 552 | t.Fatalf("fetcher requested stale header") |
| 553 | } |
| 554 | // Ensure that a block with a higher number than the threshold is discarded |
| 555 | tester.fetcher.Notify("higher", hashes[high], blocks[hashes[high]].NumberU64(), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
| 556 | select { |
| 557 | case <-time.After(50 * time.Millisecond): |
| 558 | case <-fetching: |
| 559 | t.Fatalf("fetcher requested future header") |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | // Tests that peers announcing blocks with invalid numbers (i.e. not matching |
| 564 | // the headers provided afterwards) get dropped as malicious. |
nothing calls this directly
no test coverage detected