@@ -327,4 +327,124 @@ BOOST_FIXTURE_TEST_CASE(package_submission_tests, TestChain100Setup)
327
327
BOOST_CHECK (m_node.mempool ->exists (GenTxid::Txid (tx_child->GetHash ())));
328
328
}
329
329
}
330
+
331
+ // Tests for packages containing transactions that have same-txid-different-witness equivalents in
332
+ // the mempool.
333
+ BOOST_FIXTURE_TEST_CASE (package_witness_swap_tests, TestChain100Setup)
334
+ {
335
+ LOCK (cs_main);
336
+
337
+ // Transactions with a same-txid-different-witness transaction in the mempool should be ignored,
338
+ // and the mempool entry's wtxid returned.
339
+ CScript witnessScript = CScript () << OP_DROP << OP_TRUE;
340
+ CScript scriptPubKey = GetScriptForDestination (WitnessV0ScriptHash (witnessScript));
341
+ auto mtx_parent = CreateValidMempoolTransaction (/* input_transaction=*/ m_coinbase_txns[0 ], /* vout=*/ 0 ,
342
+ /* input_height=*/ 0 , /* input_signing_key=*/ coinbaseKey,
343
+ /* output_destination=*/ scriptPubKey,
344
+ /* output_amount=*/ CAmount (49 * COIN), /* submit=*/ false );
345
+ CTransactionRef ptx_parent = MakeTransactionRef (mtx_parent);
346
+
347
+ // Make two children with the same txid but different witnesses.
348
+ CScriptWitness witness1;
349
+ witness1.stack .push_back (std::vector<unsigned char >(1 ));
350
+ witness1.stack .push_back (std::vector<unsigned char >(witnessScript.begin (), witnessScript.end ()));
351
+
352
+ CScriptWitness witness2 (witness1);
353
+ witness2.stack .push_back (std::vector<unsigned char >(2 ));
354
+ witness2.stack .push_back (std::vector<unsigned char >(witnessScript.begin (), witnessScript.end ()));
355
+
356
+ CKey child_key;
357
+ child_key.MakeNewKey (true );
358
+ CScript child_locking_script = GetScriptForDestination (WitnessV0KeyHash (child_key.GetPubKey ()));
359
+ CMutableTransaction mtx_child1;
360
+ mtx_child1.nVersion = 1 ;
361
+ mtx_child1.vin .resize (1 );
362
+ mtx_child1.vin [0 ].prevout .hash = ptx_parent->GetHash ();
363
+ mtx_child1.vin [0 ].prevout .n = 0 ;
364
+ mtx_child1.vin [0 ].scriptSig = CScript ();
365
+ mtx_child1.vin [0 ].scriptWitness = witness1;
366
+ mtx_child1.vout .resize (1 );
367
+ mtx_child1.vout [0 ].nValue = CAmount (48 * COIN);
368
+ mtx_child1.vout [0 ].scriptPubKey = child_locking_script;
369
+
370
+ CMutableTransaction mtx_child2{mtx_child1};
371
+ mtx_child2.vin [0 ].scriptWitness = witness2;
372
+
373
+ CTransactionRef ptx_child1 = MakeTransactionRef (mtx_child1);
374
+ CTransactionRef ptx_child2 = MakeTransactionRef (mtx_child2);
375
+
376
+ // child1 and child2 have the same txid
377
+ BOOST_CHECK_EQUAL (ptx_child1->GetHash (), ptx_child2->GetHash ());
378
+ // child1 and child2 have different wtxids
379
+ BOOST_CHECK (ptx_child1->GetWitnessHash () != ptx_child2->GetWitnessHash ());
380
+
381
+ // Try submitting Package1{parent, child1} and Package2{parent, child2} where the children are
382
+ // same-txid-different-witness.
383
+ {
384
+ const auto submit_witness1 = ProcessNewPackage (m_node.chainman ->ActiveChainstate (), *m_node.mempool ,
385
+ {ptx_parent, ptx_child1}, /* test_accept=*/ false );
386
+ BOOST_CHECK_MESSAGE (submit_witness1.m_state .IsValid (),
387
+ " Package validation unexpectedly failed: " << submit_witness1.m_state .GetRejectReason ());
388
+ auto it_parent1 = submit_witness1.m_tx_results .find (ptx_parent->GetWitnessHash ());
389
+ auto it_child1 = submit_witness1.m_tx_results .find (ptx_child1->GetWitnessHash ());
390
+ BOOST_CHECK (it_parent1 != submit_witness1.m_tx_results .end ());
391
+ BOOST_CHECK_MESSAGE (it_parent1->second .m_state .IsValid (),
392
+ " Transaction unexpectedly failed: " << it_parent1->second .m_state .GetRejectReason ());
393
+ BOOST_CHECK (it_child1 != submit_witness1.m_tx_results .end ());
394
+ BOOST_CHECK_MESSAGE (it_child1->second .m_state .IsValid (),
395
+ " Transaction unexpectedly failed: " << it_child1->second .m_state .GetRejectReason ());
396
+
397
+ BOOST_CHECK (m_node.mempool ->exists (GenTxid::Txid (ptx_parent->GetHash ())));
398
+ BOOST_CHECK (m_node.mempool ->exists (GenTxid::Txid (ptx_child1->GetHash ())));
399
+
400
+ const auto submit_witness2 = ProcessNewPackage (m_node.chainman ->ActiveChainstate (), *m_node.mempool ,
401
+ {ptx_parent, ptx_child2}, /* test_accept=*/ false );
402
+ BOOST_CHECK_MESSAGE (submit_witness2.m_state .IsValid (),
403
+ " Package validation unexpectedly failed: " << submit_witness2.m_state .GetRejectReason ());
404
+ auto it_parent2_deduped = submit_witness2.m_tx_results .find (ptx_parent->GetWitnessHash ());
405
+ auto it_child2 = submit_witness2.m_tx_results .find (ptx_child2->GetWitnessHash ());
406
+ BOOST_CHECK (it_parent2_deduped != submit_witness2.m_tx_results .end ());
407
+ BOOST_CHECK (it_parent2_deduped->second .m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
408
+ BOOST_CHECK (it_child2 != submit_witness2.m_tx_results .end ());
409
+ BOOST_CHECK (it_child2->second .m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
410
+ BOOST_CHECK_EQUAL (ptx_child1->GetWitnessHash (), it_child2->second .m_other_wtxid .value ());
411
+
412
+ BOOST_CHECK (m_node.mempool ->exists (GenTxid::Txid (ptx_child2->GetHash ())));
413
+ BOOST_CHECK (!m_node.mempool ->exists (GenTxid::Wtxid (ptx_child2->GetWitnessHash ())));
414
+ }
415
+
416
+ // Try submitting Package1{child2, grandchild} where child2 is same-txid-different-witness as
417
+ // the in-mempool transaction, child1. Since child1 exists in the mempool and its outputs are
418
+ // available, child2 should be ignored and grandchild should be accepted.
419
+ //
420
+ // This tests a potential censorship vector in which an attacker broadcasts a competing package
421
+ // where a parent's witness is mutated. The honest package should be accepted despite the fact
422
+ // that we don't allow witness replacement.
423
+ CKey grandchild_key;
424
+ grandchild_key.MakeNewKey (true );
425
+ CScript grandchild_locking_script = GetScriptForDestination (WitnessV0KeyHash (grandchild_key.GetPubKey ()));
426
+ auto mtx_grandchild = CreateValidMempoolTransaction (/* input_transaction=*/ ptx_child2, /* vout=*/ 0 ,
427
+ /* input_height=*/ 0 , /* input_signing_key=*/ child_key,
428
+ /* output_destination=*/ grandchild_locking_script,
429
+ /* output_amount=*/ CAmount (47 * COIN), /* submit=*/ false );
430
+ CTransactionRef ptx_grandchild = MakeTransactionRef (mtx_grandchild);
431
+
432
+ // We already submitted child1 above.
433
+ {
434
+ const auto submit_spend_ignored = ProcessNewPackage (m_node.chainman ->ActiveChainstate (), *m_node.mempool ,
435
+ {ptx_child2, ptx_grandchild}, /* test_accept=*/ false );
436
+ BOOST_CHECK_MESSAGE (submit_spend_ignored.m_state .IsValid (),
437
+ " Package validation unexpectedly failed: " << submit_spend_ignored.m_state .GetRejectReason ());
438
+ auto it_child2_ignored = submit_spend_ignored.m_tx_results .find (ptx_child2->GetWitnessHash ());
439
+ auto it_grandchild = submit_spend_ignored.m_tx_results .find (ptx_grandchild->GetWitnessHash ());
440
+ BOOST_CHECK (it_child2_ignored != submit_spend_ignored.m_tx_results .end ());
441
+ BOOST_CHECK (it_child2_ignored->second .m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
442
+ BOOST_CHECK (it_grandchild != submit_spend_ignored.m_tx_results .end ());
443
+ BOOST_CHECK (it_grandchild->second .m_result_type == MempoolAcceptResult::ResultType::VALID);
444
+
445
+ BOOST_CHECK (m_node.mempool ->exists (GenTxid::Txid (ptx_child2->GetHash ())));
446
+ BOOST_CHECK (!m_node.mempool ->exists (GenTxid::Wtxid (ptx_child2->GetWitnessHash ())));
447
+ BOOST_CHECK (m_node.mempool ->exists (GenTxid::Wtxid (ptx_grandchild->GetWitnessHash ())));
448
+ }
449
+ }
330
450
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments