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

Function TestDistantAnnouncementDiscarding

protocols/cpc/fetcher/fetcher_test.go:526–561  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

524// head get discarded to prevent wasting resources on useless blocks from faulty
525// peers.
526func 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.

Callers

nothing calls this directly

Calls 10

makeChainFunction · 0.85
newTesterFunction · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
makeHeaderFetcherMethod · 0.80
makeBodyFetcherMethod · 0.80
NumberU64Method · 0.80
AddMethod · 0.65
NowMethod · 0.65
NotifyMethod · 0.45

Tested by

no test coverage detected