MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / BuildWeirdRequestsTest

Method BuildWeirdRequestsTest

src/test/txrequest_tests.cpp:625–700  ·  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

623
624/** Add to scenario a test that involves RequestedTx() calls for txhashes not returned by GetRequestable. */
625void TxRequestTest::BuildWeirdRequestsTest(Scenario& scenario)
626{
627 auto peer1 = scenario.NewPeer();
628 auto peer2 = scenario.NewPeer();
629 auto gtxid1 = scenario.NewGTxid({{peer1, peer2}});
630 auto gtxid2 = scenario.NewGTxid({{peer2, peer1}});
631
632 // Announce gtxid1 by peer1.
633 scenario.ReceivedInv(peer1, gtxid1, true, MIN_TIME);
634 scenario.Check(peer1, {gtxid1}, 1, 0, 0, "q1");
635
636 // Announce gtxid2 by peer2.
637 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
638 scenario.ReceivedInv(peer2, gtxid2, true, MIN_TIME);
639 scenario.Check(peer1, {gtxid1}, 1, 0, 0, "q2");
640 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q3");
641
642 // We request gtxid2 from *peer1* - no effect.
643 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
644 scenario.RequestedTx(peer1, gtxid2.ToUint256(), MAX_TIME);
645 scenario.Check(peer1, {gtxid1}, 1, 0, 0, "q4");
646 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q5");
647
648 // Now request gtxid1 from peer1 - marks it as REQUESTED.
649 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
650 auto expiryA = scenario.Now() + RandomTime8s();
651 scenario.RequestedTx(peer1, gtxid1.ToUint256(), expiryA);
652 scenario.Check(peer1, {}, 0, 1, 0, "q6");
653 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q7");
654
655 // Request it a second time - nothing happens, as it's already REQUESTED.
656 auto expiryB = expiryA + RandomTime8s();
657 scenario.RequestedTx(peer1, gtxid1.ToUint256(), expiryB);
658 scenario.Check(peer1, {}, 0, 1, 0, "q8");
659 scenario.Check(peer2, {gtxid2}, 1, 0, 0, "q9");
660
661 // Also announce gtxid1 from peer2 now, so that the txhash isn't forgotten when the peer1 request expires.
662 scenario.ReceivedInv(peer2, gtxid1, true, MIN_TIME);
663 scenario.Check(peer1, {}, 0, 1, 0, "q10");
664 scenario.Check(peer2, {gtxid2}, 2, 0, 0, "q11");
665
666 // When reaching expiryA, it expires (not expiryB, which is later).
667 scenario.AdvanceTime(expiryA - scenario.Now());
668 scenario.Check(peer1, {}, 0, 0, 1, "q12");
669 scenario.Check(peer2, {gtxid2, gtxid1}, 2, 0, 0, "q13");
670 scenario.CheckExpired(peer1, gtxid1);
671
672 // Requesting it yet again from peer1 doesn't do anything, as it's already COMPLETED.
673 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
674 scenario.RequestedTx(peer1, gtxid1.ToUint256(), MAX_TIME);
675 scenario.Check(peer1, {}, 0, 0, 1, "q14");
676 scenario.Check(peer2, {gtxid2, gtxid1}, 2, 0, 0, "q15");
677
678 // Now announce gtxid2 from peer1.
679 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
680 scenario.ReceivedInv(peer1, gtxid2, true, MIN_TIME);
681 scenario.Check(peer1, {}, 1, 0, 1, "q16");
682 scenario.Check(peer2, {gtxid2, gtxid1}, 2, 0, 0, "q17");

Callers

nothing calls this directly

Calls 10

NewPeerMethod · 0.80
NewGTxidMethod · 0.80
randboolMethod · 0.80
CheckExpiredMethod · 0.80
ReceivedInvMethod · 0.45
CheckMethod · 0.45
AdvanceTimeMethod · 0.45
RequestedTxMethod · 0.45
NowMethod · 0.45
DisconnectedPeerMethod · 0.45

Tested by

no test coverage detected