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

Function TestEmptyBlockShortCircuit

protocols/cpc/fetcher/fetcher_test.go:606–638  ·  view source on GitHub ↗

Tests that if a block is empty (i.e. header only), no body request should be made, and instead the header should be assembled into a whole block in itself.

(t *testing.T)

Source from the content-addressed store, hash-verified

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.
606func TestEmptyBlockShortCircuit(t *testing.T) {
607 // Create a chain of blocks to import
608 hashes, blocks := makeChain(32, 0, genesis)
609
610 tester := newTester()
611 headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
612 bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
613
614 // Add a monitoring hook for all internal events
615 fetching := make(chan []common.Hash)
616 tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- hashes }
617
618 completing := make(chan []common.Hash)
619 tester.fetcher.completingHook = func(hashes []common.Hash) { completing <- hashes }
620
621 imported := make(chan *types.Block)
622 tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
623
624 // Iteratively announce blocks until all are imported
625 for i := len(hashes) - 2; i >= 0; i-- {
626 tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
627
628 // All announces should fetch the header
629 verifyFetchingEvent(t, fetching, true)
630
631 // Only blocks with data contents should request bodies
632 verifyCompletingEvent(t, completing, len(blocks[hashes[i]].Transactions()) > 0)
633
634 // Irrelevant of the construct, import should succeed
635 verifyImportEvent(t, imported, true)
636 }
637 verifyImportDone(t, imported)
638}
639
640// Tests that a peer is unable to use unbounded memory with sending infinite
641// block announcements to a node, but that even in the face of such an attack,

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected