15#include <boost/test/unit_test.hpp>
21constexpr std::chrono::microseconds MIN_TIME = std::chrono::microseconds::min();
22constexpr std::chrono::microseconds MAX_TIME = std::chrono::microseconds::max();
23constexpr std::chrono::microseconds MICROSECOND = std::chrono::microseconds{1};
24constexpr std::chrono::microseconds NO_TIME = std::chrono::microseconds{0};
27using Action = std::pair<std::chrono::microseconds, std::function<void()>>;
39 std::vector<Action> actions;
42 std::set<NodeId> peerset;
45 std::set<uint256> txhashset;
50 std::multiset<std::pair<NodeId, GenTxid>> expired;
53std::chrono::microseconds RandomTime8s() {
return std::chrono::microseconds{1 +
InsecureRandBits(23)}; }
54std::chrono::microseconds RandomTime1y() {
return std::chrono::microseconds{1 +
InsecureRandBits(45)}; }
68 std::chrono::microseconds m_now;
69 std::string m_testname;
72 Scenario(Runner& runner, std::chrono::microseconds starttime) : m_runner(runner), m_now(starttime) {}
75 void SetTestName(std::string testname)
77 m_testname = std::move(testname);
81 void AdvanceTime(std::chrono::microseconds amount)
83 assert(amount.count() >= 0);
88 void ForgetTxHash(
const uint256& txhash)
90 auto& runner = m_runner;
91 runner.actions.emplace_back(m_now, [=,&runner]() {
92 runner.txrequest.ForgetTxHash(txhash);
93 runner.txrequest.SanityCheck();
98 void ReceivedInv(
NodeId peer,
const GenTxid& gtxid,
bool pref, std::chrono::microseconds reqtime)
100 auto& runner = m_runner;
101 runner.actions.emplace_back(m_now, [=,&runner]() {
102 runner.txrequest.ReceivedInv(peer, gtxid, pref, reqtime);
103 runner.txrequest.SanityCheck();
108 void DisconnectedPeer(
NodeId peer)
110 auto& runner = m_runner;
111 runner.actions.emplace_back(m_now, [=,&runner]() {
112 runner.txrequest.DisconnectedPeer(peer);
113 runner.txrequest.SanityCheck();
118 void RequestedTx(
NodeId peer,
const uint256& txhash, std::chrono::microseconds exptime)
120 auto& runner = m_runner;
121 runner.actions.emplace_back(m_now, [=,&runner]() {
122 runner.txrequest.RequestedTx(peer, txhash, exptime);
123 runner.txrequest.SanityCheck();
130 auto& runner = m_runner;
131 runner.actions.emplace_back(m_now, [=,&runner]() {
132 runner.txrequest.ReceivedResponse(peer, txhash);
133 runner.txrequest.SanityCheck();
148 void Check(
NodeId peer,
const std::vector<GenTxid>& expected,
size_t candidates,
size_t inflight,
149 size_t completed,
const std::string& checkname,
150 std::chrono::microseconds offset = std::chrono::microseconds{0})
152 const auto comment = m_testname +
" " + checkname;
153 auto& runner = m_runner;
154 const auto now = m_now;
155 assert(offset.count() <= 0);
156 runner.actions.emplace_back(m_now, [=,&runner]() {
157 std::vector<std::pair<NodeId, GenTxid>> expired_now;
158 auto ret = runner.txrequest.GetRequestable(peer, now + offset, &expired_now);
159 for (
const auto& entry : expired_now) runner.expired.insert(entry);
160 runner.txrequest.SanityCheck();
161 runner.txrequest.PostGetRequestableSanityCheck(now + offset);
162 size_t total = candidates + inflight + completed;
163 size_t real_total = runner.txrequest.Count(peer);
164 size_t real_candidates = runner.txrequest.CountCandidates(peer);
165 size_t real_inflight = runner.txrequest.CountInFlight(peer);
166 BOOST_CHECK_MESSAGE(real_total == total,
strprintf(
"[" + comment +
"] total %i (%i expected)", real_total, total));
167 BOOST_CHECK_MESSAGE(real_inflight == inflight,
strprintf(
"[" + comment +
"] inflight %i (%i expected)", real_inflight, inflight));
168 BOOST_CHECK_MESSAGE(real_candidates == candidates,
strprintf(
"[" + comment +
"] candidates %i (%i expected)", real_candidates, candidates));
169 BOOST_CHECK_MESSAGE(ret == expected,
"[" + comment +
"] mismatching requestables");
179 const auto& testname = m_testname;
180 auto& runner = m_runner;
181 runner.actions.emplace_back(m_now, [=,&runner]() {
182 auto it = runner.expired.find(std::pair<NodeId, GenTxid>{peer, gtxid});
183 BOOST_CHECK_MESSAGE(it != runner.expired.end(),
"[" + testname +
"] missing expiration");
184 if (it != runner.expired.end()) runner.expired.erase(it);
196 uint256 NewTxHash(
const std::vector<std::vector<NodeId>>& orders = {})
203 for (
const auto& order : orders) {
204 for (
size_t pos = 1; pos < order.size(); ++pos) {
205 uint64_t prio_prev = m_runner.txrequest.ComputePriority(ret, order[pos - 1],
true);
206 uint64_t prio_cur = m_runner.txrequest.ComputePriority(ret, order[pos],
true);
207 if (prio_prev <= prio_cur) {
215 ok = m_runner.txhashset.insert(ret).second;
222 GenTxid NewGTxid(
const std::vector<std::vector<NodeId>>& orders = {})
235 ok = m_runner.peerset.insert(ret).second;
240 std::chrono::microseconds Now()
const {
return m_now; }
247void BuildSingleTest(Scenario& scenario,
int config)
249 auto peer = scenario.NewPeer();
250 auto gtxid = scenario.NewGTxid();
251 bool immediate = config & 1;
252 bool preferred = config & 2;
253 auto delay = immediate ? NO_TIME : RandomTime8s();
255 scenario.SetTestName(
strprintf(
"Single(config=%i)", config));
258 scenario.ReceivedInv(peer, gtxid, preferred, immediate ? MIN_TIME : scenario.Now() + delay);
260 scenario.Check(peer, {gtxid}, 1, 0, 0,
"s1");
262 scenario.Check(peer, {}, 1, 0, 0,
"s2");
263 scenario.AdvanceTime(delay - MICROSECOND);
264 scenario.Check(peer, {}, 1, 0, 0,
"s3");
265 scenario.AdvanceTime(MICROSECOND);
266 scenario.Check(peer, {gtxid}, 1, 0, 0,
"s4");
270 scenario.AdvanceTime(RandomTime8s());
271 auto expiry = RandomTime8s();
272 scenario.Check(peer, {gtxid}, 1, 0, 0,
"s5");
273 scenario.RequestedTx(peer, gtxid.
GetHash(), scenario.Now() + expiry);
274 scenario.Check(peer, {}, 0, 1, 0,
"s6");
276 if ((config >> 3) == 1) {
277 scenario.AdvanceTime(expiry - MICROSECOND);
278 scenario.Check(peer, {}, 0, 1, 0,
"s7");
279 scenario.AdvanceTime(MICROSECOND);
280 scenario.Check(peer, {}, 0, 0, 0,
"s8");
281 scenario.CheckExpired(peer, gtxid);
284 scenario.AdvanceTime(std::chrono::microseconds{
InsecureRandRange(expiry.count())});
285 scenario.Check(peer, {}, 0, 1, 0,
"s9");
286 if ((config >> 3) == 3) {
287 scenario.ReceivedResponse(peer, gtxid.
GetHash());
288 scenario.Check(peer, {}, 0, 0, 0,
"s10");
295 scenario.DisconnectedPeer(peer);
297 scenario.ForgetTxHash(gtxid.
GetHash());
299 scenario.Check(peer, {}, 0, 0, 0,
"s11");
307void BuildPriorityTest(Scenario& scenario,
int config)
309 scenario.SetTestName(
strprintf(
"Priority(config=%i)", config));
312 auto peer1 = scenario.NewPeer(), peer2 = scenario.NewPeer();
315 bool prio1 = config & 1;
316 auto gtxid = prio1 ? scenario.NewGTxid({{peer1, peer2}}) : scenario.NewGTxid({{peer2, peer1}});
317 bool pref1 = config & 2, pref2 = config & 4;
319 scenario.ReceivedInv(peer1, gtxid, pref1, MIN_TIME);
320 scenario.Check(peer1, {gtxid}, 1, 0, 0,
"p1");
322 scenario.AdvanceTime(RandomTime8s());
323 scenario.Check(peer1, {gtxid}, 1, 0, 0,
"p2");
326 scenario.ReceivedInv(peer2, gtxid, pref2, MIN_TIME);
333 (pref1 == pref2 && !prio1);
334 NodeId priopeer = stage2_prio ? peer2 : peer1, otherpeer = stage2_prio ? peer1 : peer2;
335 scenario.Check(otherpeer, {}, 1, 0, 0,
"p3");
336 scenario.Check(priopeer, {gtxid}, 1, 0, 0,
"p4");
338 scenario.Check(otherpeer, {}, 1, 0, 0,
"p5");
339 scenario.Check(priopeer, {gtxid}, 1, 0, 0,
"p6");
343 scenario.RequestedTx(priopeer, gtxid.
GetHash(), MAX_TIME);
344 scenario.Check(priopeer, {}, 0, 1, 0,
"p7");
345 scenario.Check(otherpeer, {}, 1, 0, 0,
"p8");
351 scenario.DisconnectedPeer(priopeer);
353 scenario.ReceivedResponse(priopeer, gtxid.
GetHash());
356 scenario.Check(priopeer, {}, 0, 0, !(config & 16),
"p8");
357 scenario.Check(otherpeer, {gtxid}, 1, 0, 0,
"p9");
361 scenario.DisconnectedPeer(otherpeer);
363 scenario.Check(peer1, {}, 0, 0, 0,
"p10");
364 scenario.Check(peer2, {}, 0, 0, 0,
"p11");
369void BuildBigPriorityTest(Scenario& scenario,
int peers)
371 scenario.SetTestName(
strprintf(
"BigPriority(peers=%i)", peers));
374 std::map<NodeId, bool> preferred;
375 std::vector<NodeId> pref_peers, npref_peers;
377 int num_npref = peers - num_pref;
378 for (
int i = 0; i < num_pref; ++i) {
379 pref_peers.push_back(scenario.NewPeer());
380 preferred[pref_peers.back()] =
true;
382 for (
int i = 0; i < num_npref; ++i) {
383 npref_peers.push_back(scenario.NewPeer());
384 preferred[npref_peers.back()] =
false;
387 std::vector<NodeId> request_order;
388 for (
int i = 0; i < num_pref; ++i) request_order.push_back(pref_peers[i]);
389 for (
int i = 0; i < num_npref; ++i) request_order.push_back(npref_peers[i]);
392 std::vector<NodeId> announce_order = request_order;
397 auto gtxid = scenario.NewGTxid({pref_peers, npref_peers});
401 std::map<NodeId, std::chrono::microseconds> reqtimes;
402 auto reqtime = scenario.Now();
403 for (
int i = peers - 1; i >= 0; --i) {
404 reqtime += RandomTime8s();
405 reqtimes[request_order[i]] = reqtime;
409 for (
const auto peer : announce_order) {
410 scenario.ReceivedInv(peer, gtxid, preferred[peer], reqtimes[peer]);
412 for (
const auto peer : announce_order) {
413 scenario.Check(peer, {}, 1, 0, 0,
"b1");
418 for (
int i = peers - 1; i >= 0; --i) {
419 scenario.AdvanceTime(reqtimes[request_order[i]] - scenario.Now() - MICROSECOND);
420 scenario.Check(request_order[i], {}, 1, 0, 0,
"b2");
421 scenario.AdvanceTime(MICROSECOND);
422 scenario.Check(request_order[i], {gtxid}, 1, 0, 0,
"b3");
427 for (
int i = 0; i < peers; ++i) {
430 const auto peer = request_order[pos];
431 request_order.erase(request_order.begin() + pos);
433 scenario.DisconnectedPeer(peer);
434 scenario.Check(peer, {}, 0, 0, 0,
"b4");
436 scenario.ReceivedResponse(peer, gtxid.
GetHash());
437 scenario.Check(peer, {}, 0, 0, request_order.size() > 0,
"b5");
439 if (request_order.size()) {
440 scenario.Check(request_order[0], {gtxid}, 1, 0, 0,
"b6");
445 for (
const auto peer : announce_order) {
446 scenario.Check(peer, {}, 0, 0, 0,
"b7");
455void BuildRequestOrderTest(Scenario& scenario,
int config)
457 scenario.SetTestName(
strprintf(
"RequestOrder(config=%i)", config));
459 auto peer = scenario.NewPeer();
460 auto gtxid1 = scenario.NewGTxid();
461 auto gtxid2 = scenario.NewGTxid();
463 auto reqtime2 = scenario.Now() + RandomTime8s();
464 auto reqtime1 = reqtime2 + RandomTime8s();
466 scenario.ReceivedInv(peer, gtxid1, config & 1, reqtime1);
468 scenario.ReceivedInv(peer, gtxid2, config & 2, reqtime2);
470 scenario.AdvanceTime(reqtime2 - MICROSECOND - scenario.Now());
471 scenario.Check(peer, {}, 2, 0, 0,
"o1");
472 scenario.AdvanceTime(MICROSECOND);
473 scenario.Check(peer, {gtxid2}, 2, 0, 0,
"o2");
474 scenario.AdvanceTime(reqtime1 - MICROSECOND - scenario.Now());
475 scenario.Check(peer, {gtxid2}, 2, 0, 0,
"o3");
476 scenario.AdvanceTime(MICROSECOND);
479 scenario.Check(peer, {gtxid1, gtxid2}, 2, 0, 0,
"o4");
481 scenario.DisconnectedPeer(peer);
482 scenario.Check(peer, {}, 0, 0, 0,
"o5");
490void BuildWtxidTest(Scenario& scenario,
int config)
492 scenario.SetTestName(
strprintf(
"Wtxid(config=%i)", config));
494 auto peerT = scenario.NewPeer();
495 auto peerW = scenario.NewPeer();
496 auto txhash = scenario.NewTxHash();
500 auto reqtimeT =
InsecureRandBool() ? MIN_TIME : scenario.Now() + RandomTime8s();
501 auto reqtimeW =
InsecureRandBool() ? MIN_TIME : scenario.Now() + RandomTime8s();
505 scenario.ReceivedInv(peerT, txid, config & 2, reqtimeT);
507 scenario.ReceivedInv(peerW, wtxid, !(config & 2), reqtimeW);
509 scenario.ReceivedInv(peerW, wtxid, !(config & 2), reqtimeW);
511 scenario.ReceivedInv(peerT, txid, config & 2, reqtimeT);
516 auto max_reqtime = std::max(reqtimeT, reqtimeW);
517 if (max_reqtime > scenario.Now()) scenario.AdvanceTime(max_reqtime - scenario.Now());
519 scenario.Check(peerT, {txid}, 1, 0, 0,
"w1");
520 scenario.Check(peerW, {}, 1, 0, 0,
"w2");
522 scenario.Check(peerT, {}, 1, 0, 0,
"w3");
523 scenario.Check(peerW, {wtxid}, 1, 0, 0,
"w4");
527 auto expiry = RandomTime8s();
529 scenario.RequestedTx(peerT, txid.GetHash(), scenario.Now() + expiry);
530 scenario.Check(peerT, {}, 0, 1, 0,
"w5");
531 scenario.Check(peerW, {}, 1, 0, 0,
"w6");
533 scenario.RequestedTx(peerW, wtxid.GetHash(), scenario.Now() + expiry);
534 scenario.Check(peerT, {}, 1, 0, 0,
"w7");
535 scenario.Check(peerW, {}, 0, 1, 0,
"w8");
540 scenario.AdvanceTime(expiry);
542 scenario.Check(peerT, {}, 0, 0, 1,
"w9");
543 scenario.Check(peerW, {wtxid}, 1, 0, 0,
"w10");
544 scenario.CheckExpired(peerT, txid);
546 scenario.Check(peerT, {txid}, 1, 0, 0,
"w11");
547 scenario.Check(peerW, {}, 0, 0, 1,
"w12");
548 scenario.CheckExpired(peerW, wtxid);
554 scenario.ForgetTxHash(txhash);
555 scenario.Check(peerT, {}, 0, 0, 0,
"w13");
556 scenario.Check(peerW, {}, 0, 0, 0,
"w14");
560void BuildTimeBackwardsTest(Scenario& scenario)
562 auto peer1 = scenario.NewPeer();
563 auto peer2 = scenario.NewPeer();
564 auto gtxid = scenario.NewGTxid({{peer1, peer2}});
567 auto reqtime = scenario.Now() + RandomTime8s();
568 scenario.ReceivedInv(peer2, gtxid,
true, reqtime);
569 scenario.Check(peer2, {}, 1, 0, 0,
"r1");
570 scenario.AdvanceTime(reqtime - scenario.Now());
571 scenario.Check(peer2, {gtxid}, 1, 0, 0,
"r2");
573 scenario.Check(peer2, {}, 1, 0, 0,
"r3", -MICROSECOND);
575 scenario.Check(peer2, {gtxid}, 1, 0, 0,
"r4");
579 scenario.ReceivedInv(peer1, gtxid,
true, MAX_TIME);
580 scenario.Check(peer2, {gtxid}, 1, 0, 0,
"r5");
581 scenario.Check(peer1, {}, 1, 0, 0,
"r6");
585 auto expiry = scenario.Now() + RandomTime8s();
586 scenario.RequestedTx(peer1, gtxid.
GetHash(), expiry);
587 scenario.Check(peer1, {}, 0, 1, 0,
"r7");
588 scenario.Check(peer2, {}, 1, 0, 0,
"r8");
591 scenario.AdvanceTime(expiry - scenario.Now());
592 scenario.Check(peer1, {}, 0, 0, 1,
"r9");
593 scenario.Check(peer2, {gtxid}, 1, 0, 0,
"r10");
594 scenario.CheckExpired(peer1, gtxid);
595 scenario.Check(peer1, {}, 0, 0, 1,
"r11", -MICROSECOND);
596 scenario.Check(peer2, {gtxid}, 1, 0, 0,
"r12", -MICROSECOND);
600 scenario.DisconnectedPeer(peer2);
601 scenario.Check(peer1, {}, 0, 0, 0,
"r13");
602 scenario.Check(peer2, {}, 0, 0, 0,
"r14");
606void BuildWeirdRequestsTest(Scenario& scenario)
608 auto peer1 = scenario.NewPeer();
609 auto peer2 = scenario.NewPeer();
610 auto gtxid1 = scenario.NewGTxid({{peer1, peer2}});
611 auto gtxid2 = scenario.NewGTxid({{peer2, peer1}});
614 scenario.ReceivedInv(peer1, gtxid1,
true, MIN_TIME);
615 scenario.Check(peer1, {gtxid1}, 1, 0, 0,
"q1");
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");
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");
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");
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");
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");
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);
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");
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");
667 scenario.RequestedTx(peer1, gtxid2.GetHash(), MAX_TIME);
668 scenario.Check(peer1, {}, 0, 1, 1,
"q18");
669 scenario.Check(peer2, {gtxid1}, 2, 0, 0,
"q19");
673 scenario.RequestedTx(peer2, gtxid2.GetHash(), MAX_TIME);
674 scenario.Check(peer1, {}, 0, 0, 2,
"q20");
675 scenario.Check(peer2, {gtxid1}, 1, 1, 0,
"q21");
678 scenario.DisconnectedPeer(peer2);
679 scenario.Check(peer1, {}, 0, 0, 0,
"q22");
680 scenario.Check(peer2, {}, 0, 0, 0,
"q23");
683void TestInterleavedScenarios()
686 std::vector<std::function<void(Scenario&)>> builders;
688 for (
int n = 0; n < 64; ++n) {
689 builders.emplace_back([n](Scenario& scenario){ BuildWtxidTest(scenario, n); });
690 builders.emplace_back([n](Scenario& scenario){ BuildRequestOrderTest(scenario, n & 3); });
691 builders.emplace_back([n](Scenario& scenario){ BuildSingleTest(scenario, n & 31); });
692 builders.emplace_back([n](Scenario& scenario){ BuildPriorityTest(scenario, n & 31); });
693 builders.emplace_back([n](Scenario& scenario){ BuildBigPriorityTest(scenario, (n & 7) + 1); });
694 builders.emplace_back([](Scenario& scenario){ BuildTimeBackwardsTest(scenario); });
695 builders.emplace_back([](Scenario& scenario){ BuildWeirdRequestsTest(scenario); });
701 auto starttime = RandomTime1y();
703 while (builders.size()) {
706 auto scenario_start = starttime + RandomTime8s() + RandomTime8s() + RandomTime8s();
707 Scenario scenario(runner, scenario_start);
708 for (
int j = 0; builders.size() && j < 10; ++j) {
709 builders.back()(scenario);
716 std::stable_sort(runner.actions.begin(), runner.actions.end(), [](
const Action& a1,
const Action& a2) {
717 return a1.first < a2.first;
721 for (
auto& action : runner.actions) {
733 for (
int i = 0; i < 5; ++i) {
734 TestInterleavedScenarios();
A generic txid reference (txid or wtxid).
const uint256 & GetHash() const
static GenTxid Wtxid(const uint256 &hash)
static GenTxid Txid(const uint256 &hash)
Data structure to keep track of, and schedule, transaction downloads from peers.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
static uint64_t InsecureRandRange(uint64_t range)
static uint256 InsecureRand256()
static uint64_t InsecureRandBits(int bits)
static bool InsecureRandBool()
BOOST_AUTO_TEST_CASE(TxRequestTest)