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

Method BuildPriorityTest

src/test/txrequest_tests.cpp:325–383  ·  view source on GitHub ↗

Add to scenario a test with a single tx announced by two peers, to verify the * right peer is selected for requests. * * config is an integer in [0, 32), which controls which variant of the test is used. */

Source from the content-addressed store, hash-verified

323 * config is an integer in [0, 32), which controls which variant of the test is used.
324 */
325void TxRequestTest::BuildPriorityTest(Scenario& scenario, int config)
326{
327 scenario.SetTestName(strprintf("Priority(config=%i)", config));
328
329 // Two peers. They will announce in order {peer1, peer2}.
330 auto peer1 = scenario.NewPeer(), peer2 = scenario.NewPeer();
331 // Construct a transaction that under random rules would be preferred by peer2 or peer1,
332 // depending on configuration.
333 bool prio1 = config & 1;
334 auto gtxid = prio1 ? scenario.NewGTxid({{peer1, peer2}}) : scenario.NewGTxid({{peer2, peer1}});
335 bool pref1 = config & 2, pref2 = config & 4;
336
337 scenario.ReceivedInv(peer1, gtxid, pref1, MIN_TIME);
338 scenario.Check(peer1, {gtxid}, 1, 0, 0, "p1");
339 if (m_rng.randbool()) {
340 scenario.AdvanceTime(RandomTime8s());
341 scenario.Check(peer1, {gtxid}, 1, 0, 0, "p2");
342 }
343
344 scenario.ReceivedInv(peer2, gtxid, pref2, MIN_TIME);
345 bool stage2_prio =
346 // At this point, peer2 will be given priority if:
347 // - It is preferred and peer1 is not
348 (pref2 && !pref1) ||
349 // - They're in the same preference class,
350 // and the randomized priority favors peer2 over peer1.
351 (pref1 == pref2 && !prio1);
352 NodeId priopeer = stage2_prio ? peer2 : peer1, otherpeer = stage2_prio ? peer1 : peer2;
353 scenario.Check(otherpeer, {}, 1, 0, 0, "p3");
354 scenario.Check(priopeer, {gtxid}, 1, 0, 0, "p4");
355 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
356 scenario.Check(otherpeer, {}, 1, 0, 0, "p5");
357 scenario.Check(priopeer, {gtxid}, 1, 0, 0, "p6");
358
359 // We possibly request from the selected peer.
360 if (config & 8) {
361 scenario.RequestedTx(priopeer, gtxid.ToUint256(), MAX_TIME);
362 scenario.Check(priopeer, {}, 0, 1, 0, "p7");
363 scenario.Check(otherpeer, {}, 1, 0, 0, "p8");
364 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
365 }
366
367 // The peer which was selected (or requested from) now goes offline, or a NOTFOUND is received from them.
368 if (config & 16) {
369 scenario.DisconnectedPeer(priopeer);
370 } else {
371 scenario.ReceivedResponse(priopeer, gtxid.ToUint256());
372 }
373 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
374 scenario.Check(priopeer, {}, 0, 0, !(config & 16), "p8");
375 scenario.Check(otherpeer, {gtxid}, 1, 0, 0, "p9");
376 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
377
378 // Now the other peer goes offline.
379 scenario.DisconnectedPeer(otherpeer);
380 if (m_rng.randbool()) scenario.AdvanceTime(RandomTime8s());
381 scenario.Check(peer1, {}, 0, 0, 0, "p10");
382 scenario.Check(peer2, {}, 0, 0, 0, "p11");

Callers

nothing calls this directly

Calls 10

SetTestNameMethod · 0.80
NewPeerMethod · 0.80
NewGTxidMethod · 0.80
randboolMethod · 0.80
ReceivedInvMethod · 0.45
CheckMethod · 0.45
AdvanceTimeMethod · 0.45
RequestedTxMethod · 0.45
DisconnectedPeerMethod · 0.45
ReceivedResponseMethod · 0.45

Tested by

no test coverage detected