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

Function BuildPriorityTest

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

305 * config is an integer in [0, 32), which controls which variant of the test is used.
306 */
307void BuildPriorityTest(Scenario& scenario, int config)
308{
309 scenario.SetTestName(strprintf("Priority(config=%i)", config));
310
311 // Two peers. They will announce in order {peer1, peer2}.
312 auto peer1 = scenario.NewPeer(), peer2 = scenario.NewPeer();
313 // Construct a transaction that under random rules would be preferred by peer2 or peer1,
314 // depending on configuration.
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;
318
319 scenario.ReceivedInv(peer1, gtxid, pref1, MIN_TIME);
320 scenario.Check(peer1, {gtxid}, 1, 0, 0, "p1");
321 if (InsecureRandBool()) {
322 scenario.AdvanceTime(RandomTime8s());
323 scenario.Check(peer1, {gtxid}, 1, 0, 0, "p2");
324 }
325
326 scenario.ReceivedInv(peer2, gtxid, pref2, MIN_TIME);
327 bool stage2_prio =
328 // At this point, peer2 will be given priority if:
329 // - It is preferred and peer1 is not
330 (pref2 && !pref1) ||
331 // - They're in the same preference class,
332 // and the randomized priority favors peer2 over peer1.
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");
337 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
338 scenario.Check(otherpeer, {}, 1, 0, 0, "p5");
339 scenario.Check(priopeer, {gtxid}, 1, 0, 0, "p6");
340
341 // We possibly request from the selected peer.
342 if (config & 8) {
343 scenario.RequestedTx(priopeer, gtxid.GetHash(), MAX_TIME);
344 scenario.Check(priopeer, {}, 0, 1, 0, "p7");
345 scenario.Check(otherpeer, {}, 1, 0, 0, "p8");
346 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
347 }
348
349 // The peer which was selected (or requested from) now goes offline, or a NOTFOUND is received from them.
350 if (config & 16) {
351 scenario.DisconnectedPeer(priopeer);
352 } else {
353 scenario.ReceivedResponse(priopeer, gtxid.GetHash());
354 }
355 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
356 scenario.Check(priopeer, {}, 0, 0, !(config & 16), "p8");
357 scenario.Check(otherpeer, {gtxid}, 1, 0, 0, "p9");
358 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
359
360 // Now the other peer goes offline.
361 scenario.DisconnectedPeer(otherpeer);
362 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
363 scenario.Check(peer1, {}, 0, 0, 0, "p10");
364 scenario.Check(peer2, {}, 0, 0, 0, "p11");

Callers 1

TestInterleavedScenariosFunction · 0.85

Calls 12

InsecureRandBoolFunction · 0.85
RandomTime8sFunction · 0.85
SetTestNameMethod · 0.80
NewPeerMethod · 0.80
NewGTxidMethod · 0.80
ReceivedInvMethod · 0.45
CheckMethod · 0.45
AdvanceTimeMethod · 0.45
RequestedTxMethod · 0.45
GetHashMethod · 0.45
DisconnectedPeerMethod · 0.45
ReceivedResponseMethod · 0.45

Tested by

no test coverage detected