MCPcopy Create free account
hub / github.com/ElementsProject/elements / BuildWeirdRequestsTest

Function BuildWeirdRequestsTest

src/test/txrequest_tests.cpp:606–681  ·  view source on GitHub ↗

Add to scenario a test that involves RequestedTx() calls for txhashes not returned by GetRequestable. */

Source from the content-addressed store, hash-verified

604
605/** Add to scenario a test that involves RequestedTx() calls for txhashes not returned by GetRequestable. */
606void BuildWeirdRequestsTest(Scenario& scenario)
607{
608 auto peer1 = scenario.NewPeer();
609 auto peer2 = scenario.NewPeer();
610 auto gtxid1 = scenario.NewGTxid({{peer1, peer2}});
611 auto gtxid2 = scenario.NewGTxid({{peer2, peer1}});
612
613 // Announce gtxid1 by peer1.
614 scenario.ReceivedInv(peer1, gtxid1, true, MIN_TIME);
615 scenario.Check(peer1, {gtxid1}, 1, 0, 0, "q1");
616
617 // Announce gtxid2 by peer2.
618 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
619 scenario.ReceivedInv(peer2, gtxid2, true, MIN_TIME);
620 scenario.Check(peer1, {gtxid1}, 1, 0, 0, "q2");
621 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q3");
622
623 // We request gtxid2 from *peer1* - no effect.
624 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
625 scenario.RequestedTx(peer1, gtxid2.GetHash(), MAX_TIME);
626 scenario.Check(peer1, {gtxid1}, 1, 0, 0, "q4");
627 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q5");
628
629 // Now request gtxid1 from peer1 - marks it as REQUESTED.
630 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
631 auto expiryA = scenario.Now() + RandomTime8s();
632 scenario.RequestedTx(peer1, gtxid1.GetHash(), expiryA);
633 scenario.Check(peer1, {}, 0, 1, 0, "q6");
634 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q7");
635
636 // Request it a second time - nothing happens, as it's already REQUESTED.
637 auto expiryB = expiryA + RandomTime8s();
638 scenario.RequestedTx(peer1, gtxid1.GetHash(), expiryB);
639 scenario.Check(peer1, {}, 0, 1, 0, "q8");
640 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q9");
641
642 // Also announce gtxid1 from peer2 now, so that the txhash isn't forgotten when the peer1 request expires.
643 scenario.ReceivedInv(peer2, gtxid1, true, MIN_TIME);
644 scenario.Check(peer1, {}, 0, 1, 0, "q10");
645 scenario.Check(peer2, {gtxid2}, 2, 0, 0, "q11");
646
647 // When reaching expiryA, it expires (not expiryB, which is later).
648 scenario.AdvanceTime(expiryA - scenario.Now());
649 scenario.Check(peer1, {}, 0, 0, 1, "q12");
650 scenario.Check(peer2, {gtxid2, gtxid1}, 2, 0, 0, "q13");
651 scenario.CheckExpired(peer1, gtxid1);
652
653 // Requesting it yet again from peer1 doesn't do anything, as it's already COMPLETED.
654 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
655 scenario.RequestedTx(peer1, gtxid1.GetHash(), MAX_TIME);
656 scenario.Check(peer1, {}, 0, 0, 1, "q14");
657 scenario.Check(peer2, {gtxid2, gtxid1}, 2, 0, 0, "q15");
658
659 // Now announce gtxid2 from peer1.
660 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
661 scenario.ReceivedInv(peer1, gtxid2, true, MIN_TIME);
662 scenario.Check(peer1, {}, 1, 0, 1, "q16");
663 scenario.Check(peer2, {gtxid2, gtxid1}, 2, 0, 0, "q17");

Callers 1

TestInterleavedScenariosFunction · 0.85

Calls 12

InsecureRandBoolFunction · 0.85
RandomTime8sFunction · 0.85
NewPeerMethod · 0.80
NewGTxidMethod · 0.80
CheckExpiredMethod · 0.80
ReceivedInvMethod · 0.45
CheckMethod · 0.45
AdvanceTimeMethod · 0.45
RequestedTxMethod · 0.45
GetHashMethod · 0.45
NowMethod · 0.45
DisconnectedPeerMethod · 0.45

Tested by

no test coverage detected