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

Function BuildBigPriorityTest

src/test/txrequest_tests.cpp:369–448  ·  view source on GitHub ↗

Add to scenario a randomized test in which N peers announce the same transaction, to verify * the order in which they are requested. */

Source from the content-addressed store, hash-verified

367/** Add to scenario a randomized test in which N peers announce the same transaction, to verify
368 * the order in which they are requested. */
369void BuildBigPriorityTest(Scenario& scenario, int peers)
370{
371 scenario.SetTestName(strprintf("BigPriority(peers=%i)", peers));
372
373 // We will have N peers announce the same transaction.
374 std::map<NodeId, bool> preferred;
375 std::vector<NodeId> pref_peers, npref_peers;
376 int num_pref = InsecureRandRange(peers + 1) ; // Some preferred, ...
377 int num_npref = peers - num_pref; // some not preferred.
378 for (int i = 0; i < num_pref; ++i) {
379 pref_peers.push_back(scenario.NewPeer());
380 preferred[pref_peers.back()] = true;
381 }
382 for (int i = 0; i < num_npref; ++i) {
383 npref_peers.push_back(scenario.NewPeer());
384 preferred[npref_peers.back()] = false;
385 }
386 // Make a list of all peers, in order of intended request order (concatenation of pref_peers and npref_peers).
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]);
390
391 // Determine the announcement order randomly.
392 std::vector<NodeId> announce_order = request_order;
393 Shuffle(announce_order.begin(), announce_order.end(), g_insecure_rand_ctx);
394
395 // Find a gtxid whose txhash prioritization is consistent with the required ordering within pref_peers and
396 // within npref_peers.
397 auto gtxid = scenario.NewGTxid({pref_peers, npref_peers});
398
399 // Decide reqtimes in opposite order of the expected request order. This means that as time passes we expect the
400 // to-be-requested-from-peer will change every time a subsequent reqtime is passed.
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;
406 }
407
408 // Actually announce from all peers simultaneously (but in announce_order).
409 for (const auto peer : announce_order) {
410 scenario.ReceivedInv(peer, gtxid, preferred[peer], reqtimes[peer]);
411 }
412 for (const auto peer : announce_order) {
413 scenario.Check(peer, {}, 1, 0, 0, "b1");
414 }
415
416 // Let time pass and observe the to-be-requested-from peer change, from nonpreferred to preferred, and from
417 // high priority to low priority within each class.
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");
423 }
424
425 // Peers now in random order go offline, or send NOTFOUNDs. At every point in time the new to-be-requested-from
426 // peer should be the best remaining one, so verify this after every response.

Callers 1

TestInterleavedScenariosFunction · 0.85

Calls 15

InsecureRandRangeFunction · 0.85
RandomTime8sFunction · 0.85
InsecureRandBoolFunction · 0.85
SetTestNameMethod · 0.80
NewPeerMethod · 0.80
NewGTxidMethod · 0.80
ShuffleFunction · 0.50
push_backMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
NowMethod · 0.45
ReceivedInvMethod · 0.45

Tested by

no test coverage detected