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

Function BuildWtxidTest

src/test/txrequest_tests.cpp:490–557  ·  view source on GitHub ↗

Add to scenario a test that verifies behavior related to both txid and wtxid with the same * hash being announced. * * config is an integer in [0, 4) inclusive, and selects the variant of the test used. */

Source from the content-addressed store, hash-verified

488 * config is an integer in [0, 4) inclusive, and selects the variant of the test used.
489*/
490void BuildWtxidTest(Scenario& scenario, int config)
491{
492 scenario.SetTestName(strprintf("Wtxid(config=%i)", config));
493
494 auto peerT = scenario.NewPeer();
495 auto peerW = scenario.NewPeer();
496 auto txhash = scenario.NewTxHash();
497 auto txid{GenTxid::Txid(txhash)};
498 auto wtxid{GenTxid::Wtxid(txhash)};
499
500 auto reqtimeT = InsecureRandBool() ? MIN_TIME : scenario.Now() + RandomTime8s();
501 auto reqtimeW = InsecureRandBool() ? MIN_TIME : scenario.Now() + RandomTime8s();
502
503 // Announce txid first or wtxid first.
504 if (config & 1) {
505 scenario.ReceivedInv(peerT, txid, config & 2, reqtimeT);
506 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
507 scenario.ReceivedInv(peerW, wtxid, !(config & 2), reqtimeW);
508 } else {
509 scenario.ReceivedInv(peerW, wtxid, !(config & 2), reqtimeW);
510 if (InsecureRandBool()) scenario.AdvanceTime(RandomTime8s());
511 scenario.ReceivedInv(peerT, txid, config & 2, reqtimeT);
512 }
513
514 // Let time pass if needed, and check that the preferred announcement (txid or wtxid)
515 // is correctly to-be-requested (and with the correct wtxidness).
516 auto max_reqtime = std::max(reqtimeT, reqtimeW);
517 if (max_reqtime > scenario.Now()) scenario.AdvanceTime(max_reqtime - scenario.Now());
518 if (config & 2) {
519 scenario.Check(peerT, {txid}, 1, 0, 0, "w1");
520 scenario.Check(peerW, {}, 1, 0, 0, "w2");
521 } else {
522 scenario.Check(peerT, {}, 1, 0, 0, "w3");
523 scenario.Check(peerW, {wtxid}, 1, 0, 0, "w4");
524 }
525
526 // Let the preferred announcement be requested. It's not going to be delivered.
527 auto expiry = RandomTime8s();
528 if (config & 2) {
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");
532 } else {
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");
536 }
537
538 // After reaching expiration time of the preferred announcement, verify that the
539 // remaining one is requestable
540 scenario.AdvanceTime(expiry);
541 if (config & 2) {
542 scenario.Check(peerT, {}, 0, 0, 1, "w9");
543 scenario.Check(peerW, {wtxid}, 1, 0, 0, "w10");
544 scenario.CheckExpired(peerT, txid);
545 } else {
546 scenario.Check(peerT, {txid}, 1, 0, 0, "w11");
547 scenario.Check(peerW, {}, 0, 0, 1, "w12");

Callers 1

TestInterleavedScenariosFunction · 0.85

Calls 13

InsecureRandBoolFunction · 0.85
RandomTime8sFunction · 0.85
SetTestNameMethod · 0.80
NewPeerMethod · 0.80
NewTxHashMethod · 0.80
CheckExpiredMethod · 0.80
NowMethod · 0.45
ReceivedInvMethod · 0.45
AdvanceTimeMethod · 0.45
CheckMethod · 0.45
RequestedTxMethod · 0.45
GetHashMethod · 0.45

Tested by

no test coverage detected