Tests that blocks arriving from various sources (multiple propagations, hash announces, etc) do not get scheduled for import multiple times.
(t *testing.T)
| 453 | // Tests that blocks arriving from various sources (multiple propagations, hash |
| 454 | // announces, etc) do not get scheduled for import multiple times. |
| 455 | func 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. |
nothing calls this directly
no test coverage detected