LCOV - code coverage report
Current view: top level - src/test - txpackage_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 94.1 % 862 811
Test Date: 2026-11-13 07:04:31 Functions: 100.0 % 20 20
Branches: 47.1 % 4142 1950

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <consensus/validation.h>
       6                 :             : #include <key_io.h>
       7                 :             : #include <policy/packages.h>
       8                 :             : #include <policy/policy.h>
       9                 :             : #include <policy/rbf.h>
      10                 :             : #include <primitives/transaction.h>
      11                 :             : #include <script/script.h>
      12                 :             : #include <serialize.h>
      13                 :             : #include <streams.h>
      14                 :             : #include <test/util/common.h>
      15                 :             : #include <test/util/random.h>
      16                 :             : #include <test/util/script.h>
      17                 :             : #include <test/util/setup_common.h>
      18                 :             : #include <util/strencodings.h>
      19                 :             : #include <test/util/txmempool.h>
      20                 :             : #include <validation.h>
      21                 :             : 
      22                 :             : #include <boost/test/unit_test.hpp>
      23                 :             : 
      24                 :             : using namespace util::hex_literals;
      25                 :             : 
      26                 :             : // A fee amount that is above 1sat/vB but below 5sat/vB for most transactions created within these
      27                 :             : // unit tests.
      28                 :             : static const CAmount low_fee_amt{200};
      29                 :             : 
      30                 :          18 : struct TxPackageTest : TestChain100Setup {
      31                 :             : // Create placeholder transactions that have no meaning.
      32                 :          29 : inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
      33                 :             : {
      34                 :          29 :     CMutableTransaction mtx = CMutableTransaction();
      35   [ +  -  +  - ]:          87 :     auto random_script = CScript() << ToByteVector(m_rng.rand256()) << ToByteVector(m_rng.rand256());
      36         [ +  + ]:        1205 :     for (size_t i{0}; i < num_inputs; ++i) {
      37         [ +  - ]:        1176 :         mtx.vin.emplace_back(COutPoint{Txid::FromUint256(m_rng.rand256()), 0}, random_script);
      38                 :             :     }
      39         [ +  + ]:        1205 :     for (size_t o{0}; o < num_outputs; ++o) {
      40         [ +  - ]:        1176 :         mtx.vout.emplace_back(1 * CENT, random_script);
      41                 :             :     }
      42         [ +  - ]:          58 :     return MakeTransactionRef(mtx);
      43                 :          58 : }
      44                 :             : }; // struct TxPackageTest
      45                 :             : 
      46                 :             : BOOST_FIXTURE_TEST_SUITE(txpackage_tests, TxPackageTest)
      47                 :             : 
      48   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_hash_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      49                 :             : {
      50                 :             :     // Random real segwit transaction
      51                 :           1 :     DataStream stream_1{
      52                 :           1 :         "02000000000101964b8aa63509579ca6086e6012eeaa4c2f4dd1e283da29b67c8eea38b3c6fd220000000000fdffffff0294c618000000000017a9145afbbb42f4e83312666d0697f9e66259912ecde38768fa2c0000000000160014897388a0889390fd0e153a22bb2cf9d8f019faf50247304402200547406380719f84d68cf4e96cc3e4a1688309ef475b150be2b471c70ea562aa02206d255f5acc40fd95981874d77201d2eb07883657ce1c796513f32b6079545cdf0121023ae77335cefcb5ab4c1dc1fb0d2acfece184e593727d7d5906c78e564c7c11d125cf0c00"_hex,
      53                 :           1 :     };
      54         [ +  - ]:           1 :     CTransaction tx_1(deserialize, TX_WITH_WITNESS, stream_1);
      55         [ +  - ]:           1 :     CTransactionRef ptx_1{MakeTransactionRef(tx_1)};
      56                 :             : 
      57                 :             :     // Random real nonsegwit transaction
      58                 :           1 :     DataStream stream_2{
      59         [ +  - ]:           1 :         "01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"_hex,
      60         [ +  - ]:           1 :     };
      61         [ +  - ]:           1 :     CTransaction tx_2(deserialize, TX_WITH_WITNESS, stream_2);
      62         [ +  - ]:           1 :     CTransactionRef ptx_2{MakeTransactionRef(tx_2)};
      63                 :             : 
      64                 :             :     // Random real segwit transaction
      65                 :           1 :     DataStream stream_3{
      66         [ +  - ]:           1 :         "0200000000010177862801f77c2c068a70372b4c435ef8dd621291c36a64eb4dd491f02218f5324600000000fdffffff014a0100000000000022512035ea312034cfac01e956a269f3bf147f569c2fbb00180677421262da042290d803402be713325ff285e66b0380f53f2fae0d0fb4e16f378a440fed51ce835061437566729d4883bc917632f3cff474d6384bc8b989961a1d730d4a87ed38ad28bd337b20f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7fac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a224342414c222c22616d74223a2236393639227d6821c1f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7f00000000"_hex,
      67         [ +  - ]:           1 :     };
      68         [ +  - ]:           1 :     CTransaction tx_3(deserialize, TX_WITH_WITNESS, stream_3);
      69         [ +  - ]:           1 :     CTransactionRef ptx_3{MakeTransactionRef(tx_3)};
      70                 :             : 
      71                 :             :     // It's easy to see that wtxids are sorted in lexicographical order:
      72                 :           1 :     constexpr Wtxid wtxid_1{"85cd1a31eb38f74ed5742ec9cb546712ab5aaf747de28a9168b53e846cbda17f"};
      73                 :           1 :     constexpr Wtxid wtxid_2{"b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"};
      74                 :           1 :     constexpr Wtxid wtxid_3{"e065bac15f62bb4e761d761db928ddee65a47296b2b776785abb912cdec474e3"};
      75   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(tx_1.GetWitnessHash(), wtxid_1);
      76   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(tx_2.GetWitnessHash(), wtxid_2);
      77   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(tx_3.GetWitnessHash(), wtxid_3);
      78                 :             : 
      79   [ +  -  +  -  :           2 :     BOOST_CHECK(wtxid_1.GetHex() < wtxid_2.GetHex());
          +  -  +  -  +  
                      - ]
      80   [ +  -  +  -  :           2 :     BOOST_CHECK(wtxid_2.GetHex() < wtxid_3.GetHex());
          +  -  +  -  +  
                      - ]
      81                 :             : 
      82                 :             :     // The txids are not (we want to test that sorting and hashing use wtxid, not txid):
      83                 :           1 :     constexpr Txid txid_1{"bd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69"};
      84                 :           1 :     constexpr Txid txid_2{"b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"};
      85                 :           1 :     constexpr Txid txid_3{"ee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93"};
      86   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(tx_1.GetHash(), txid_1);
      87   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(tx_2.GetHash(), txid_2);
      88   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(tx_3.GetHash(), txid_3);
      89                 :             : 
      90   [ +  -  +  -  :           2 :     BOOST_CHECK(txid_2.GetHex() < txid_1.GetHex());
          +  -  +  -  +  
                      - ]
      91                 :             : 
      92   [ +  -  +  -  :           2 :     BOOST_CHECK(txid_1.ToUint256() != wtxid_1.ToUint256());
                   +  - ]
      93   [ +  -  +  -  :           2 :     BOOST_CHECK(txid_2.ToUint256() == wtxid_2.ToUint256());
                   +  - ]
      94   [ +  -  +  -  :           2 :     BOOST_CHECK(txid_3.ToUint256() != wtxid_3.ToUint256());
                   +  - ]
      95                 :             : 
      96                 :             :     // We are testing that both functions compare using GetHex() and not uint256.
      97                 :             :     // (in this pair of wtxids, hex string order != uint256 order)
      98   [ +  -  +  -  :           2 :     BOOST_CHECK(wtxid_2 < wtxid_1);
                   +  - ]
      99                 :             :     // (in this pair of wtxids, hex string order == uint256 order)
     100   [ +  -  +  -  :           2 :     BOOST_CHECK(wtxid_2 < wtxid_3);
                   +  - ]
     101                 :             : 
     102                 :             :     // All permutations of the package containing ptx_1, ptx_2, ptx_3 have the same package hash
     103   [ +  +  +  -  :           4 :     std::vector<CTransactionRef> package_123{ptx_1, ptx_2, ptx_3};
             -  -  -  - ]
     104   [ +  +  +  -  :           4 :     std::vector<CTransactionRef> package_132{ptx_1, ptx_3, ptx_2};
             -  -  -  - ]
     105   [ +  +  +  -  :           4 :     std::vector<CTransactionRef> package_231{ptx_2, ptx_3, ptx_1};
             -  -  -  - ]
     106   [ +  +  +  -  :           4 :     std::vector<CTransactionRef> package_213{ptx_2, ptx_1, ptx_3};
             -  -  -  - ]
     107   [ +  +  +  -  :           4 :     std::vector<CTransactionRef> package_312{ptx_3, ptx_1, ptx_2};
             -  -  -  - ]
     108   [ +  +  +  -  :           4 :     std::vector<CTransactionRef> package_321{ptx_3, ptx_2, ptx_1};
             -  -  -  - ]
     109                 :             : 
     110   [ +  -  +  -  :           1 :     uint256 calculated_hash_123 = (HashWriter() << wtxid_1 << wtxid_2 << wtxid_3).GetSHA256();
          +  -  +  -  +  
                      - ]
     111                 :             : 
     112   [ +  -  +  -  :           1 :     uint256 hash_if_by_txid = (HashWriter() << wtxid_2 << wtxid_1 << wtxid_3).GetSHA256();
          +  -  +  -  +  
                      - ]
     113   [ +  -  +  -  :           3 :     BOOST_CHECK(hash_if_by_txid != calculated_hash_123);
             +  -  +  - ]
     114                 :             : 
     115   [ +  -  +  -  :           1 :     uint256 hash_if_use_txid = (HashWriter() << txid_2 << txid_1 << txid_3).GetSHA256();
          +  -  +  -  +  
                      - ]
     116   [ +  -  +  -  :           3 :     BOOST_CHECK(hash_if_use_txid != calculated_hash_123);
             +  -  +  - ]
     117                 :             : 
     118   [ +  -  +  -  :           1 :     uint256 hash_if_use_int_order = (HashWriter() << wtxid_2 << wtxid_1 << wtxid_3).GetSHA256();
          +  -  +  -  +  
                      - ]
     119   [ +  -  +  -  :           3 :     BOOST_CHECK(hash_if_use_int_order != calculated_hash_123);
             +  -  +  - ]
     120                 :             : 
     121   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_123));
                   +  - ]
     122   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_132));
                   +  - ]
     123   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_231));
                   +  - ]
     124   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_213));
                   +  - ]
     125   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_312));
                   +  - ]
     126   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_321));
                   +  - ]
     127   [ +  -  +  -  :          31 : }
          +  -  -  +  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  -  +  +  
             -  +  -  +  
                      - ]
     128                 :             : 
     129   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_sanitization_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     130                 :             : {
     131                 :             :     // Packages can't have more than 25 transactions.
     132                 :           1 :     Package package_too_many;
     133         [ +  - ]:           1 :     package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
     134         [ +  + ]:          27 :     for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
     135   [ +  -  +  - ]:          52 :         package_too_many.emplace_back(create_placeholder_tx(1, 1));
     136                 :             :     }
     137         [ +  - ]:           1 :     PackageValidationState state_too_many;
     138   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsWellFormedPackage(package_too_many, state_too_many));
             +  -  +  - ]
     139   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(state_too_many.GetResult(), PackageValidationResult::PCKG_POLICY);
     140   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(state_too_many.GetRejectReason(), "package-too-many-transactions");
                   +  - ]
     141                 :             : 
     142                 :             :     // Packages can't have a total weight of more than 404'000WU.
     143         [ +  - ]:           1 :     CTransactionRef large_ptx = create_placeholder_tx(150, 150);
     144                 :           1 :     Package package_too_large;
     145                 :           1 :     auto size_large = GetTransactionWeight(*large_ptx);
     146                 :           1 :     size_t total_weight{0};
     147         [ +  + ]:           6 :     while (total_weight <= MAX_PACKAGE_WEIGHT) {
     148         [ +  - ]:           4 :         package_too_large.push_back(large_ptx);
     149                 :           4 :         total_weight += size_large;
     150                 :             :     }
     151   [ +  -  -  +  :           2 :     BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
             +  -  +  - ]
     152         [ +  - ]:           1 :     PackageValidationState state_too_large;
     153   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsWellFormedPackage(package_too_large, state_too_large));
             +  -  +  - ]
     154   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(state_too_large.GetResult(), PackageValidationResult::PCKG_POLICY);
     155   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(state_too_large.GetRejectReason(), "package-too-large");
                   +  - ]
     156                 :             : 
     157                 :             :     // Packages can't contain transactions with the same txid.
     158                 :           1 :     Package package_duplicate_txids_empty;
     159         [ +  + ]:           4 :     for (auto i{0}; i < 3; ++i) {
     160         [ +  - ]:           3 :         CMutableTransaction empty_tx;
     161   [ +  -  +  - ]:           9 :         package_duplicate_txids_empty.emplace_back(MakeTransactionRef(empty_tx));
     162                 :           3 :     }
     163         [ +  - ]:           1 :     PackageValidationState state_duplicates;
     164   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsWellFormedPackage(package_duplicate_txids_empty, state_duplicates));
             +  -  +  - ]
     165   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(state_duplicates.GetResult(), PackageValidationResult::PCKG_POLICY);
     166   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(state_duplicates.GetRejectReason(), "package-contains-duplicates");
                   +  - ]
     167   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsConsistentPackage(package_duplicate_txids_empty));
             +  -  +  - ]
     168                 :             : 
     169                 :             :     // Packages can't have transactions spending the same prevout
     170         [ +  - ]:           1 :     CMutableTransaction tx_zero_1;
     171         [ +  - ]:           1 :     CMutableTransaction tx_zero_2;
     172         [ +  - ]:           1 :     COutPoint same_prevout{Txid::FromUint256(m_rng.rand256()), 0};
     173         [ +  - ]:           1 :     tx_zero_1.vin.emplace_back(same_prevout);
     174         [ +  - ]:           1 :     tx_zero_2.vin.emplace_back(same_prevout);
     175                 :             :     // Different vouts (not the same tx)
     176         [ +  - ]:           1 :     tx_zero_1.vout.emplace_back(CENT, P2WSH_OP_TRUE);
     177         [ +  - ]:           1 :     tx_zero_2.vout.emplace_back(2 * CENT, P2WSH_OP_TRUE);
     178   [ +  +  +  -  :           3 :     Package package_conflicts{MakeTransactionRef(tx_zero_1), MakeTransactionRef(tx_zero_2)};
             -  -  -  - ]
     179   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsConsistentPackage(package_conflicts));
             +  -  +  - ]
     180                 :             :     // Transactions are considered sorted when they have no dependencies.
     181   [ +  -  +  -  :           2 :     BOOST_CHECK(IsTopoSortedPackage(package_conflicts));
             +  -  +  - ]
     182         [ +  - ]:           1 :     PackageValidationState state_conflicts;
     183   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsWellFormedPackage(package_conflicts, state_conflicts));
             +  -  +  - ]
     184   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(state_conflicts.GetResult(), PackageValidationResult::PCKG_POLICY);
     185   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(state_conflicts.GetRejectReason(), "conflict-in-package");
                   +  - ]
     186                 :             : 
     187                 :             :     // IsConsistentPackage only cares about conflicts between transactions, not about a transaction
     188                 :             :     // conflicting with itself (i.e. duplicate prevouts in vin).
     189         [ +  - ]:           1 :     CMutableTransaction dup_tx;
     190         [ +  - ]:           1 :     const COutPoint rand_prevout{Txid::FromUint256(m_rng.rand256()), 0};
     191         [ +  - ]:           1 :     dup_tx.vin.emplace_back(rand_prevout);
     192         [ +  - ]:           1 :     dup_tx.vin.emplace_back(rand_prevout);
     193   [ +  +  +  -  :           2 :     Package package_with_dup_tx{MakeTransactionRef(dup_tx)};
             -  -  -  - ]
     194   [ +  -  +  -  :           2 :     BOOST_CHECK(IsConsistentPackage(package_with_dup_tx));
             +  -  +  - ]
     195   [ +  -  +  - ]:           1 :     package_with_dup_tx.emplace_back(create_placeholder_tx(1, 1));
     196   [ +  -  +  -  :           2 :     BOOST_CHECK(IsConsistentPackage(package_with_dup_tx));
                   +  - ]
     197   [ +  -  +  -  :          14 : }
          -  +  +  -  -  
          +  +  -  -  -  
                   -  - ]
     198                 :             : 
     199   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_validation_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     200                 :             : {
     201                 :           1 :     LOCK(cs_main);
     202         [ +  - ]:           1 :     unsigned int initialPoolSize = m_node.mempool->size();
     203                 :             : 
     204                 :             :     // Parent and Child Package
     205                 :           1 :     CKey parent_key = GenerateRandomKey();
     206   [ +  -  +  -  :           1 :     CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
                   +  - ]
     207         [ +  - ]:           2 :     auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
     208         [ +  - ]:           1 :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     209                 :             :                                                     /*output_destination=*/parent_locking_script,
     210         [ +  - ]:           2 :                                                     /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
     211         [ +  - ]:           1 :     CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
     212                 :             : 
     213                 :           1 :     CKey child_key = GenerateRandomKey();
     214   [ +  -  +  -  :           1 :     CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
                   +  - ]
     215         [ +  - ]:           2 :     auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
     216                 :             :                                                    /*input_height=*/101, /*input_signing_key=*/parent_key,
     217                 :             :                                                    /*output_destination=*/child_locking_script,
     218         [ +  - ]:           2 :                                                    /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
     219         [ +  - ]:           1 :     CTransactionRef tx_child = MakeTransactionRef(mtx_child);
     220   [ +  +  +  -  :           3 :     Package package_parent_child{tx_parent, tx_child};
             -  -  -  - ]
     221   [ +  -  +  - ]:           1 :     const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_parent_child, /*test_accept=*/true, /*client_maxfeerate=*/{});
     222   [ +  -  -  + ]:           1 :     if (auto err_parent_child{CheckPackageMempoolAcceptResult(package_parent_child, result_parent_child, /*expect_valid=*/true, nullptr)}) {
     223   [ #  #  #  #  :           0 :         BOOST_ERROR(err_parent_child.value());
                   #  # ]
     224                 :             :     } else {
     225                 :           1 :         auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
     226                 :           1 :         auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
     227                 :             : 
     228   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_parent)) == COIN);
          +  -  +  -  +  
                -  +  - ]
     229   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().size(), 1);
             -  +  +  - ]
     230   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().front(), tx_parent->GetWitnessHash());
                   +  - ]
     231                 :             : 
     232   [ +  -  +  -  :           2 :         BOOST_CHECK(it_child->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_child)) == COIN);
          +  -  +  -  +  
                -  +  - ]
     233   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().size(), 1);
             -  +  +  - ]
     234   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().front(), tx_child->GetWitnessHash());
                   +  - ]
     235                 :           0 :     }
     236                 :             :     // A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
     237         [ +  - ]:           1 :     CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
     238   [ +  -  +  -  :           2 :     BOOST_CHECK(GetVirtualTransactionSize(*giant_ptx) > DEFAULT_CLUSTER_SIZE_LIMIT_KVB * 1000);
             +  -  +  - ]
     239   [ +  +  +  -  :           2 :     Package package_single_giant{giant_ptx};
             -  -  -  - ]
     240   [ +  -  +  - ]:           1 :     auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_single_giant, /*test_accept=*/true, /*client_maxfeerate=*/{});
     241   [ +  -  -  + ]:           1 :     if (auto err_single_large{CheckPackageMempoolAcceptResult(package_single_giant, result_single_large, /*expect_valid=*/false, nullptr)}) {
     242   [ #  #  #  #  :           0 :         BOOST_ERROR(err_single_large.value());
                   #  # ]
     243                 :             :     } else {
     244   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
     245   [ +  -  -  +  :           2 :         BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
                   +  - ]
     246                 :           1 :         auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
     247   [ +  -  -  +  :           2 :         BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
                   +  - ]
     248                 :           0 :     }
     249                 :             : 
     250                 :             :     // Check that mempool size hasn't changed.
     251   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
                   +  - ]
     252   [ +  -  +  -  :          12 : }
          -  +  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     253                 :             : 
     254   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(noncontextual_package_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     255                 :             : {
     256                 :             :     // The signatures won't be verified so we can just use a placeholder
     257                 :           1 :     CKey placeholder_key = GenerateRandomKey();
     258   [ +  -  +  -  :           1 :     CScript spk = GetScriptForDestination(PKHash(placeholder_key.GetPubKey()));
                   +  - ]
     259                 :           1 :     CKey placeholder_key_2 = GenerateRandomKey();
     260   [ +  -  +  -  :           1 :     CScript spk2 = GetScriptForDestination(PKHash(placeholder_key_2.GetPubKey()));
                   +  - ]
     261                 :             : 
     262                 :             :     // Parent and Child Package
     263                 :           1 :     {
     264   [ +  -  +  - ]:           2 :         auto mtx_parent = CreateValidMempoolTransaction(m_coinbase_txns[0], 0, 0, coinbaseKey, spk,
     265         [ +  - ]:           2 :                                                         CAmount(49 * COIN), /*submit=*/false);
     266         [ +  - ]:           1 :         CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
     267                 :             : 
     268         [ +  - ]:           2 :         auto mtx_child = CreateValidMempoolTransaction(tx_parent, 0, 101, placeholder_key, spk2,
     269         [ +  - ]:           2 :                                                        CAmount(48 * COIN), /*submit=*/false);
     270         [ +  - ]:           1 :         CTransactionRef tx_child = MakeTransactionRef(mtx_child);
     271                 :             : 
     272         [ +  - ]:           1 :         PackageValidationState state;
     273   [ +  -  +  -  :           5 :         BOOST_CHECK(IsWellFormedPackage({tx_parent, tx_child}, state));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     274   [ +  -  +  -  :           5 :         BOOST_CHECK(!IsWellFormedPackage({tx_child, tx_parent}, state));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     275   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(state.GetResult(), PackageValidationResult::PCKG_POLICY);
     276   [ +  -  -  +  :           2 :         BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
                   +  - ]
     277   [ +  -  +  -  :           5 :         BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     278   [ +  -  +  -  :           5 :         BOOST_CHECK(IsChildWithParentsTree({tx_parent, tx_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     279   [ +  -  +  -  :           7 :         BOOST_CHECK(GetPackageHash({tx_parent}) != GetPackageHash({tx_child}));
          +  -  +  -  +  
          -  +  +  +  -  
          +  +  +  -  -  
          -  -  -  -  -  
                   -  - ]
     280   [ +  -  +  -  :           8 :         BOOST_CHECK(GetPackageHash({tx_child, tx_child}) != GetPackageHash({tx_child}));
          +  -  +  -  +  
          -  +  +  +  -  
          +  +  +  -  -  
          -  -  -  -  -  
                   -  - ]
     281   [ +  -  +  -  :           9 :         BOOST_CHECK(GetPackageHash({tx_child, tx_parent}) != GetPackageHash({tx_child, tx_child}));
          +  -  +  -  +  
          -  +  +  +  -  
          +  +  +  -  -  
          -  -  -  -  -  
                   -  - ]
     282   [ +  -  +  -  :           2 :         BOOST_CHECK(!IsChildWithParents({}));
             +  -  +  - ]
     283   [ +  -  +  -  :           2 :         BOOST_CHECK(!IsChildWithParentsTree({}));
                   +  - ]
     284   [ +  -  +  - ]:           4 :     }
     285                 :             : 
     286                 :             :     // 24 Parents and 1 Child
     287                 :           1 :     {
     288                 :           1 :         Package package;
     289         [ +  - ]:           1 :         CMutableTransaction child;
     290         [ +  + ]:          25 :         for (int i{0}; i < 24; ++i) {
     291   [ +  -  +  -  :          96 :             auto parent = MakeTransactionRef(CreateValidMempoolTransaction(m_coinbase_txns[i + 1],
             +  -  +  - ]
     292                 :          24 :                                              0, 0, coinbaseKey, spk, CAmount(48 * COIN), false));
     293         [ +  - ]:          24 :             package.emplace_back(parent);
     294         [ +  - ]:          24 :             child.vin.emplace_back(COutPoint(parent->GetHash(), 0));
     295                 :          24 :         }
     296         [ +  - ]:           1 :         child.vout.emplace_back(47 * COIN, spk2);
     297                 :             : 
     298                 :             :         // The child must be in the package.
     299   [ +  -  +  -  :           2 :         BOOST_CHECK(!IsChildWithParents(package));
                   +  - ]
     300                 :             : 
     301                 :             :         // The parents can be in any order.
     302                 :           1 :         FastRandomContext rng;
     303                 :           1 :         std::shuffle(package.begin(), package.end(), rng);
     304   [ +  -  +  -  :           2 :         package.push_back(MakeTransactionRef(child));
                   -  + ]
     305                 :             : 
     306         [ +  - ]:           1 :         PackageValidationState state;
     307   [ +  -  +  -  :           2 :         BOOST_CHECK(IsWellFormedPackage(package, state));
             +  -  +  - ]
     308   [ +  -  +  -  :           2 :         BOOST_CHECK(IsChildWithParents(package));
             +  -  +  - ]
     309   [ +  -  +  -  :           2 :         BOOST_CHECK(IsChildWithParentsTree(package));
                   +  - ]
     310                 :             : 
     311                 :           1 :         package.erase(package.begin());
     312   [ +  -  +  -  :           2 :         BOOST_CHECK(IsChildWithParents(package));
             +  -  +  - ]
     313                 :             : 
     314                 :             :         // The package cannot have unrelated transactions.
     315         [ +  - ]:           1 :         package.insert(package.begin(), m_coinbase_txns[0]);
     316   [ +  -  +  -  :           2 :         BOOST_CHECK(!IsChildWithParents(package));
                   +  - ]
     317                 :           2 :     }
     318                 :             : 
     319                 :             :     // 2 Parents and 1 Child where one parent depends on the other.
     320                 :           1 :     {
     321         [ +  - ]:           1 :         CMutableTransaction mtx_parent;
     322         [ +  - ]:           1 :         mtx_parent.vin.emplace_back(COutPoint(m_coinbase_txns[0]->GetHash(), 0));
     323         [ +  - ]:           1 :         mtx_parent.vout.emplace_back(20 * COIN, spk);
     324         [ +  - ]:           1 :         mtx_parent.vout.emplace_back(20 * COIN, spk2);
     325         [ +  - ]:           1 :         CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
     326                 :             : 
     327         [ +  - ]:           1 :         CMutableTransaction mtx_parent_also_child;
     328         [ +  - ]:           1 :         mtx_parent_also_child.vin.emplace_back(COutPoint(tx_parent->GetHash(), 0));
     329         [ +  - ]:           1 :         mtx_parent_also_child.vout.emplace_back(20 * COIN, spk);
     330         [ +  - ]:           1 :         CTransactionRef tx_parent_also_child = MakeTransactionRef(mtx_parent_also_child);
     331                 :             : 
     332         [ +  - ]:           1 :         CMutableTransaction mtx_child;
     333         [ +  - ]:           1 :         mtx_child.vin.emplace_back(COutPoint(tx_parent->GetHash(), 1));
     334         [ +  - ]:           1 :         mtx_child.vin.emplace_back(COutPoint(tx_parent_also_child->GetHash(), 0));
     335         [ +  - ]:           1 :         mtx_child.vout.emplace_back(39 * COIN, spk);
     336         [ +  - ]:           1 :         CTransactionRef tx_child = MakeTransactionRef(mtx_child);
     337                 :             : 
     338         [ +  - ]:           1 :         PackageValidationState state;
     339   [ +  -  +  -  :           5 :         BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     340   [ +  -  +  -  :           5 :         BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     341   [ +  -  +  -  :           6 :         BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child, tx_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     342   [ +  -  +  -  :           6 :         BOOST_CHECK(!IsChildWithParentsTree({tx_parent, tx_parent_also_child, tx_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     343                 :             :         // IsChildWithParents does not detect unsorted parents.
     344   [ +  -  +  -  :           6 :         BOOST_CHECK(IsChildWithParents({tx_parent_also_child, tx_parent, tx_child}));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     345   [ +  -  +  -  :           6 :         BOOST_CHECK(IsWellFormedPackage({tx_parent, tx_parent_also_child, tx_child}, state));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     346   [ +  -  +  -  :           6 :         BOOST_CHECK(!IsWellFormedPackage({tx_parent_also_child, tx_parent, tx_child}, state));
          +  -  +  -  +  
          +  +  -  -  -  
                   -  - ]
     347   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(state.GetResult(), PackageValidationResult::PCKG_POLICY);
     348   [ +  -  -  +  :           2 :         BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
                   +  - ]
     349   [ +  -  +  -  :           7 :     }
                   +  - ]
     350   [ +  -  +  -  :          54 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     351                 :             : 
     352   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_submission_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     353                 :             : {
     354                 :             :     // Mine blocks to mature coinbases.
     355                 :           1 :     mineBlocks(3);
     356                 :           1 :     MockMempoolMinFee(CFeeRate(5000), *m_node.mempool);
     357                 :           1 :     LOCK(cs_main);
     358         [ +  - ]:           1 :     unsigned int expected_pool_size = m_node.mempool->size();
     359                 :           1 :     CKey parent_key = GenerateRandomKey();
     360   [ +  -  +  -  :           1 :     CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
                   +  - ]
     361                 :             : 
     362                 :             :     // Unrelated transactions are not allowed in package submission.
     363                 :           1 :     Package package_unrelated;
     364         [ +  + ]:          11 :     for (size_t i{0}; i < 10; ++i) {
     365         [ +  - ]:          20 :         auto mtx = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[i + 25], /*input_vout=*/0,
     366         [ +  - ]:          10 :                                                  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     367                 :             :                                                  /*output_destination=*/parent_locking_script,
     368         [ +  - ]:          20 :                                                  /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
     369   [ +  -  +  - ]:          30 :         package_unrelated.emplace_back(MakeTransactionRef(mtx));
     370                 :          10 :     }
     371         [ +  - ]:           1 :     auto result_unrelated_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     372   [ +  -  +  - ]:           1 :                                                      package_unrelated, /*test_accept=*/false, /*client_maxfeerate=*/{});
     373                 :             :     // We don't expect m_tx_results for each transaction when basic sanity checks haven't passed.
     374   [ +  -  +  -  :           2 :     BOOST_CHECK(result_unrelated_submit.m_state.IsInvalid());
                   +  - ]
     375   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
     376   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
                   +  - ]
     377   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     378                 :             : 
     379                 :             :     // Parent and Child (and Grandchild) Package
     380                 :           1 :     Package package_parent_child;
     381                 :           1 :     Package package_3gen;
     382         [ +  - ]:           2 :     auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
     383         [ +  - ]:           1 :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     384                 :             :                                                     /*output_destination=*/parent_locking_script,
     385         [ +  - ]:           2 :                                                     /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
     386         [ +  - ]:           1 :     CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
     387         [ +  - ]:           1 :     package_parent_child.push_back(tx_parent);
     388         [ +  - ]:           1 :     package_3gen.push_back(tx_parent);
     389                 :             : 
     390                 :           1 :     CKey child_key = GenerateRandomKey();
     391   [ +  -  +  -  :           1 :     CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
                   +  - ]
     392         [ +  - ]:           2 :     auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
     393                 :             :                                                    /*input_height=*/101, /*input_signing_key=*/parent_key,
     394                 :             :                                                    /*output_destination=*/child_locking_script,
     395         [ +  - ]:           2 :                                                    /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
     396         [ +  - ]:           1 :     CTransactionRef tx_child = MakeTransactionRef(mtx_child);
     397         [ +  - ]:           1 :     package_parent_child.push_back(tx_child);
     398         [ +  - ]:           1 :     package_3gen.push_back(tx_child);
     399                 :             : 
     400                 :           1 :     CKey grandchild_key = GenerateRandomKey();
     401   [ +  -  +  -  :           1 :     CScript grandchild_locking_script = GetScriptForDestination(PKHash(grandchild_key.GetPubKey()));
                   +  - ]
     402         [ +  - ]:           2 :     auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/tx_child, /*input_vout=*/0,
     403                 :             :                                                        /*input_height=*/101, /*input_signing_key=*/child_key,
     404                 :             :                                                        /*output_destination=*/grandchild_locking_script,
     405         [ +  - ]:           3 :                                                        /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
     406         [ +  - ]:           1 :     CTransactionRef tx_grandchild = MakeTransactionRef(mtx_grandchild);
     407         [ +  - ]:           1 :     package_3gen.push_back(tx_grandchild);
     408                 :             : 
     409                 :             :     // 3 Generations is not allowed.
     410                 :           1 :     {
     411         [ +  - ]:           1 :         auto result_3gen_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     412   [ +  -  +  - ]:           1 :                                                     package_3gen, /*test_accept=*/false, /*client_maxfeerate=*/{});
     413   [ +  -  +  -  :           2 :         BOOST_CHECK(result_3gen_submit.m_state.IsInvalid());
                   +  - ]
     414   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
     415   [ +  -  -  +  :           2 :         BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
                   +  - ]
     416   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     417                 :           1 :     }
     418                 :             : 
     419                 :             :     // Parent and child package where transactions are invalid for reasons other than fee and
     420                 :             :     // missing inputs, so the package validation isn't expected to happen.
     421                 :           1 :     {
     422                 :           1 :         CScriptWitness bad_witness;
     423         [ +  - ]:           1 :         bad_witness.stack.emplace_back(1);
     424         [ +  - ]:           1 :         CMutableTransaction mtx_parent_invalid{mtx_parent};
     425         [ +  - ]:           1 :         mtx_parent_invalid.vin[0].scriptWitness = bad_witness;
     426         [ +  - ]:           1 :         CTransactionRef tx_parent_invalid = MakeTransactionRef(mtx_parent_invalid);
     427   [ +  +  +  -  :           3 :         Package package_invalid_parent{tx_parent_invalid, tx_child};
             -  -  -  - ]
     428         [ +  - ]:           1 :         auto result_quit_early = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     429   [ +  -  +  - ]:           1 :                                                    package_invalid_parent, /*test_accept=*/ false, /*client_maxfeerate=*/{});
     430   [ +  -  -  + ]:           1 :         if (auto err_parent_invalid{CheckPackageMempoolAcceptResult(package_invalid_parent, result_quit_early, /*expect_valid=*/false, m_node.mempool.get())}) {
     431   [ #  #  #  #  :           0 :             BOOST_ERROR(err_parent_invalid.value());
                   #  # ]
     432                 :             :         } else {
     433                 :           1 :             auto it_parent = result_quit_early.m_tx_results.find(tx_parent_invalid->GetWitnessHash());
     434                 :           1 :             auto it_child = result_quit_early.m_tx_results.find(tx_child->GetWitnessHash());
     435   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(it_parent->second.m_state.GetResult(), TxValidationResult::TX_WITNESS_MUTATED);
     436   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(it_parent->second.m_state.GetRejectReason(), "bad-witness-nonstandard");
                   +  - ]
     437   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(it_child->second.m_state.GetResult(), TxValidationResult::TX_MISSING_INPUTS);
     438   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(it_child->second.m_state.GetRejectReason(), "bad-txns-inputs-missingorspent");
                   +  - ]
     439                 :           0 :         }
     440   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result_quit_early.m_state.GetResult(), PackageValidationResult::PCKG_TX);
     441         [ +  - ]:           3 :     }
     442                 :             : 
     443                 :             :     // Submit package with parent + child.
     444                 :           1 :     {
     445         [ +  - ]:           1 :         const auto submit_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     446   [ +  -  +  - ]:           1 :                                                            package_parent_child, /*test_accept=*/false, /*client_maxfeerate=*/{});
     447                 :           1 :         expected_pool_size += 2;
     448   [ +  -  -  +  :           3 :         BOOST_CHECK_MESSAGE(submit_parent_child.m_state.IsValid(),
             +  -  +  - ]
     449                 :             :                             "Package validation unexpectedly failed: " << submit_parent_child.m_state.GetRejectReason());
     450   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(submit_parent_child.m_tx_results.size(), package_parent_child.size());
                   +  - ]
     451                 :           1 :         auto it_parent = submit_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
     452                 :           1 :         auto it_child = submit_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
     453   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent != submit_parent_child.m_tx_results.end());
                   +  - ]
     454   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent->second.m_state.IsValid());
                   +  - ]
     455   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent->second.m_effective_feerate == CFeeRate(1 * COIN, GetVirtualTransactionSize(*tx_parent)));
          +  -  +  -  +  
                      - ]
     456   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().size(), 1);
             -  +  +  - ]
     457   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().front(), tx_parent->GetWitnessHash());
                   +  - ]
     458   [ +  -  +  -  :           2 :         BOOST_CHECK(it_child->second.m_effective_feerate == CFeeRate(1 * COIN, GetVirtualTransactionSize(*tx_child)));
          +  -  +  -  +  
                      - ]
     459   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().size(), 1);
             -  +  +  - ]
     460   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().front(), tx_child->GetWitnessHash());
                   +  - ]
     461                 :             : 
     462   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     463                 :           1 :     }
     464                 :             : 
     465                 :             :     // Already-in-mempool transactions should be detected and de-duplicated.
     466                 :           1 :     {
     467         [ +  - ]:           1 :         const auto submit_deduped = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     468   [ +  -  +  - ]:           1 :                                                       package_parent_child, /*test_accept=*/false, /*client_maxfeerate=*/{});
     469   [ +  -  -  + ]:           1 :         if (auto err_deduped{CheckPackageMempoolAcceptResult(package_parent_child, submit_deduped, /*expect_valid=*/true, m_node.mempool.get())}) {
     470   [ #  #  #  #  :           0 :             BOOST_ERROR(err_deduped.value());
                   #  # ]
     471                 :             :         } else {
     472                 :           1 :             auto it_parent_deduped = submit_deduped.m_tx_results.find(tx_parent->GetWitnessHash());
     473                 :           1 :             auto it_child_deduped = submit_deduped.m_tx_results.find(tx_child->GetWitnessHash());
     474   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
                   +  - ]
     475   [ +  -  +  - ]:           2 :             BOOST_CHECK(it_child_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
     476                 :           0 :         }
     477                 :             : 
     478   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     479                 :           1 :     }
     480                 :             : 
     481                 :             :     // In-mempool parent and child with missing parent.
     482                 :           1 :     {
     483   [ +  -  +  -  :           4 :         auto tx_parent_1 = MakeTransactionRef(CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
             +  -  +  - ]
     484                 :             :                                                                             /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     485                 :             :                                                                             /*output_destination=*/parent_locking_script,
     486                 :           1 :                                                                             /*output_amount=*/CAmount(50 * COIN - low_fee_amt), /*submit=*/false));
     487   [ +  -  +  -  :           4 :         auto tx_parent_2 = MakeTransactionRef(CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
             +  -  +  - ]
     488                 :             :                                                                             /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     489                 :             :                                                                             /*output_destination=*/parent_locking_script,
     490                 :           1 :                                                                             /*output_amount=*/CAmount(50 * COIN - 800), /*submit=*/false));
     491                 :             : 
     492   [ +  -  +  -  :           9 :         auto tx_child_missing_parent = MakeTransactionRef(CreateValidMempoolTransaction({tx_parent_1, tx_parent_2},
          +  -  +  -  +  
          +  +  -  +  +  
          +  +  -  -  -  
             -  -  -  -  
                      - ]
     493                 :             :                                                                                         {{tx_parent_1->GetHash(), 0}, {tx_parent_2->GetHash(), 0}},
     494                 :             :                                                                                         /*input_height=*/0, {parent_key},
     495                 :           2 :                                                                                         {{49 * COIN, child_locking_script}}, /*submit=*/false));
     496                 :             : 
     497   [ +  +  +  -  :           3 :         Package package_missing_parent{tx_parent_1, tx_child_missing_parent};
             -  -  -  - ]
     498                 :             : 
     499         [ +  - ]:           1 :         const auto result_missing_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     500   [ +  -  +  - ]:           1 :                                                              package_missing_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
     501   [ +  -  -  + ]:           1 :         if (auto err_missing_parent{CheckPackageMempoolAcceptResult(package_missing_parent, result_missing_parent, /*expect_valid=*/false, m_node.mempool.get())}) {
     502   [ #  #  #  #  :           0 :             BOOST_ERROR(err_missing_parent.value());
                   #  # ]
     503                 :             :         } else {
     504                 :           1 :             auto it_parent = result_missing_parent.m_tx_results.find(tx_parent_1->GetWitnessHash());
     505                 :           1 :             auto it_child = result_missing_parent.m_tx_results.find(tx_child_missing_parent->GetWitnessHash());
     506                 :             : 
     507   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetResult(), PackageValidationResult::PCKG_TX);
     508   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetRejectReason(), "transaction failed");
                   +  - ]
     509                 :             : 
     510   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(it_parent->second.m_state.GetResult(), TxValidationResult::TX_RECONSIDERABLE);
     511   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(it_child->second.m_state.GetResult(), TxValidationResult::TX_MISSING_INPUTS);
     512   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(it_child->second.m_state.GetRejectReason(), "bad-txns-inputs-missingorspent");
                   +  - ]
     513   [ +  -  +  -  :           1 :             BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     514                 :           0 :         }
     515                 :             : 
     516                 :             :         // Submit parent2 ahead of time, should become ok.
     517   [ +  +  +  -  :           2 :         Package package_just_parent2{tx_parent_2};
             -  -  -  - ]
     518                 :           1 :         expected_pool_size += 1;
     519         [ +  - ]:           1 :         const auto result_just_parent2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     520   [ +  -  +  - ]:           1 :                                                            package_just_parent2, /*test_accept=*/false, /*client_maxfeerate=*/{});
     521   [ +  -  -  + ]:           1 :         if (auto err_parent2{CheckPackageMempoolAcceptResult(package_just_parent2, result_just_parent2, /*expect_valid=*/true, m_node.mempool.get())}) {
     522   [ #  #  #  #  :           0 :             BOOST_ERROR(err_parent2.value());
                   #  # ]
     523                 :           0 :         }
     524   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     525                 :             : 
     526         [ +  - ]:           1 :         const auto result_parent_already_in = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     527   [ +  -  +  - ]:           1 :                                                                 package_missing_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
     528                 :           1 :         expected_pool_size += 2;
     529   [ +  -  -  + ]:           1 :         if (auto err_parent_already_in{CheckPackageMempoolAcceptResult(package_missing_parent, result_parent_already_in, /*expect_valid=*/true, m_node.mempool.get())}) {
     530   [ #  #  #  #  :           0 :             BOOST_ERROR(err_parent_already_in.value());
                   #  # ]
     531                 :           0 :         }
     532   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     533   [ +  -  +  -  :           4 :     }
             +  -  +  - ]
     534   [ +  -  +  -  :          20 : }
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          -  +  +  -  +  
                -  +  - ]
     535                 :             : 
     536                 :             : // Tests for packages containing a single transaction
     537   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_single_tx)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     538                 :             : {
     539                 :             :     // Mine blocks to mature coinbases.
     540                 :           1 :     mineBlocks(3);
     541                 :           1 :     LOCK(cs_main);
     542         [ +  - ]:           1 :     auto expected_pool_size{m_node.mempool->size()};
     543                 :             : 
     544                 :           1 :     const CAmount high_fee{1000};
     545                 :             : 
     546                 :             :     // No unconfirmed parents
     547                 :           1 :     CKey single_key = GenerateRandomKey();
     548   [ +  -  +  -  :           1 :     CScript single_locking_script = GetScriptForDestination(PKHash(single_key.GetPubKey()));
                   +  - ]
     549         [ +  - ]:           2 :     auto mtx_single = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
     550         [ +  - ]:           1 :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     551                 :             :                                                     /*output_destination=*/single_locking_script,
     552         [ +  - ]:           2 :                                                     /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
     553         [ +  - ]:           1 :     CTransactionRef tx_single = MakeTransactionRef(mtx_single);
     554   [ +  +  +  -  :           2 :     Package package_tx_single{tx_single};
             -  -  -  - ]
     555         [ +  - ]:           1 :     const auto result_single_tx = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     556   [ +  -  +  - ]:           1 :                                                     package_tx_single, /*test_accept=*/false, /*client_maxfeerate=*/{});
     557                 :           1 :     expected_pool_size += 1;
     558   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(result_single_tx.m_state.IsValid(),
             +  -  +  - ]
     559                 :             :                         "Package validation unexpectedly failed: " << result_single_tx.m_state.ToString());
     560   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     561                 :             : 
     562                 :             :     // Parent and Child. Both submitted by themselves through the ProcessNewPackage interface.
     563                 :           1 :     CKey parent_key = GenerateRandomKey();
     564   [ +  -  +  -  :           1 :     CScript parent_locking_script = GetScriptForDestination(WitnessV0KeyHash(parent_key.GetPubKey()));
                   +  - ]
     565   [ +  -  +  - ]:           2 :     auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
     566                 :             :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     567                 :             :                                                     /*output_destination=*/parent_locking_script,
     568         [ +  - ]:           2 :                                                     /*output_amount=*/CAmount(50 * COIN) - high_fee, /*submit=*/false);
     569         [ +  - ]:           1 :     CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
     570   [ +  +  +  -  :           2 :     Package package_just_parent{tx_parent};
             -  -  -  - ]
     571   [ +  -  +  - ]:           1 :     const auto result_just_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_just_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
     572   [ +  -  -  + ]:           1 :     if (auto err_parent_child{CheckPackageMempoolAcceptResult(package_just_parent, result_just_parent, /*expect_valid=*/true, nullptr)}) {
     573   [ #  #  #  #  :           0 :         BOOST_ERROR(err_parent_child.value());
                   #  # ]
     574                 :             :     } else {
     575                 :           1 :         auto it_parent = result_just_parent.m_tx_results.find(tx_parent->GetWitnessHash());
     576   [ +  -  +  -  :           2 :         BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(), it_parent->second.m_state.ToString());
                   +  - ]
     577   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_parent)) == high_fee);
          +  -  +  -  +  
                -  +  - ]
     578   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().size(), 1);
             -  +  +  - ]
     579   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().front(), tx_parent->GetWitnessHash());
                   +  - ]
     580                 :           0 :     }
     581                 :           1 :     expected_pool_size += 1;
     582   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     583                 :             : 
     584                 :           1 :     CKey child_key = GenerateRandomKey();
     585   [ +  -  +  -  :           1 :     CScript child_locking_script = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
                   +  - ]
     586         [ +  - ]:           2 :     auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
     587                 :             :                                                    /*input_height=*/101, /*input_signing_key=*/parent_key,
     588                 :             :                                                    /*output_destination=*/child_locking_script,
     589         [ +  - ]:           2 :                                                    /*output_amount=*/CAmount(50 * COIN) - 2 * high_fee, /*submit=*/false);
     590         [ +  - ]:           1 :     CTransactionRef tx_child = MakeTransactionRef(mtx_child);
     591   [ +  +  +  -  :           2 :     Package package_just_child{tx_child};
             -  -  -  - ]
     592   [ +  -  +  - ]:           1 :     const auto result_just_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_just_child, /*test_accept=*/false, /*client_maxfeerate=*/{});
     593   [ +  -  -  + ]:           1 :     if (auto err_parent_child{CheckPackageMempoolAcceptResult(package_just_child, result_just_child, /*expect_valid=*/true, nullptr)}) {
     594   [ #  #  #  #  :           0 :         BOOST_ERROR(err_parent_child.value());
                   #  # ]
     595                 :             :     } else {
     596                 :           1 :         auto it_child = result_just_child.m_tx_results.find(tx_child->GetWitnessHash());
     597   [ +  -  +  -  :           2 :         BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(), it_child->second.m_state.ToString());
                   +  - ]
     598   [ +  -  +  -  :           2 :         BOOST_CHECK(it_child->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_child)) == high_fee);
          +  -  +  -  +  
                -  +  - ]
     599   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().size(), 1);
             -  +  +  - ]
     600   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().front(), tx_child->GetWitnessHash());
                   +  - ]
     601                 :           0 :     }
     602                 :           1 :     expected_pool_size += 1;
     603   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     604                 :             : 
     605                 :             :     // Too-low fee to RBF tx_single
     606   [ +  -  +  - ]:           2 :     auto mtx_single_low_fee = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
     607                 :             :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     608                 :             :                                                     /*output_destination=*/single_locking_script,
     609         [ +  - ]:           2 :                                                     /*output_amount=*/CAmount(49 * COIN - 1), /*submit=*/false);
     610         [ +  - ]:           1 :     CTransactionRef tx_single_low_fee = MakeTransactionRef(mtx_single_low_fee);
     611   [ +  +  +  -  :           2 :     Package package_tx_single_low_fee{tx_single_low_fee};
             -  -  -  - ]
     612         [ +  - ]:           1 :     const auto result_single_tx_low_fee = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     613   [ +  -  +  - ]:           1 :                                                     package_tx_single_low_fee, /*test_accept=*/false, /*client_maxfeerate=*/{});
     614                 :             : 
     615   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     616                 :             : 
     617   [ +  -  +  -  :           2 :     BOOST_CHECK(!result_single_tx_low_fee.m_state.IsValid());
                   +  - ]
     618   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(result_single_tx_low_fee.m_state.GetResult(), PackageValidationResult::PCKG_TX);
     619                 :           1 :     auto it_low_fee = result_single_tx_low_fee.m_tx_results.find(tx_single_low_fee->GetWitnessHash());
     620   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(it_low_fee->second.m_state.GetResult(), TxValidationResult::TX_RECONSIDERABLE);
     621   [ +  -  -  + ]:           1 :     if (auto err_single{CheckPackageMempoolAcceptResult(package_tx_single_low_fee, result_single_tx_low_fee, /*expect_valid=*/false, m_node.mempool.get())}) {
     622   [ #  #  #  #  :           0 :         BOOST_ERROR(err_single.value());
                   #  # ]
     623                 :           0 :     }
     624   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     625   [ +  -  -  +  :          18 : }
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
     626                 :             : 
     627                 :             : // Tests for packages containing transactions that have same-txid-different-witness equivalents in
     628                 :             : // the mempool.
     629   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_witness_swap_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     630                 :             : {
     631                 :             :     // Mine blocks to mature coinbases.
     632                 :           1 :     mineBlocks(5);
     633                 :           1 :     MockMempoolMinFee(CFeeRate(5000), *m_node.mempool);
     634                 :           1 :     LOCK(cs_main);
     635                 :             : 
     636                 :             :     // Transactions with a same-txid-different-witness transaction in the mempool should be ignored,
     637                 :             :     // and the mempool entry's wtxid returned.
     638   [ +  -  +  - ]:           1 :     CScript witnessScript = CScript() << OP_DROP << OP_TRUE;
     639   [ +  -  +  - ]:           1 :     CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     640         [ +  - ]:           2 :     auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
     641         [ +  - ]:           1 :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     642                 :             :                                                     /*output_destination=*/scriptPubKey,
     643         [ +  - ]:           2 :                                                     /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
     644         [ +  - ]:           1 :     CTransactionRef ptx_parent = MakeTransactionRef(mtx_parent);
     645                 :             : 
     646                 :             :     // Make two children with the same txid but different witnesses.
     647                 :           1 :     CScriptWitness witness1;
     648         [ +  - ]:           1 :     witness1.stack.emplace_back(1);
     649   [ -  +  +  - ]:           1 :     witness1.stack.emplace_back(witnessScript.begin(), witnessScript.end());
     650                 :             : 
     651         [ +  - ]:           1 :     CScriptWitness witness2(witness1);
     652         [ +  - ]:           1 :     witness2.stack.emplace_back(2);
     653   [ -  +  +  - ]:           1 :     witness2.stack.emplace_back(witnessScript.begin(), witnessScript.end());
     654                 :             : 
     655                 :           1 :     CKey child_key = GenerateRandomKey();
     656   [ +  -  +  -  :           1 :     CScript child_locking_script = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
                   +  - ]
     657         [ +  - ]:           1 :     CMutableTransaction mtx_child1;
     658                 :           1 :     mtx_child1.version = 1;
     659                 :           1 :     mtx_child1.vin = {
     660         [ +  - ]:           1 :         CTxIn{COutPoint{ptx_parent->GetHash(), 0}},
     661   [ -  +  +  +  :           2 :     };
                   -  - ]
     662         [ +  - ]:           1 :     mtx_child1.vin[0].scriptWitness = witness1;
     663                 :           1 :     mtx_child1.vout = {
     664                 :           1 :         CTxOut{48 * COIN, child_locking_script},
     665   [ -  +  +  +  :           2 :     };
                   -  - ]
     666                 :             : 
     667         [ +  - ]:           1 :     CMutableTransaction mtx_child2{mtx_child1};
     668         [ +  - ]:           1 :     mtx_child2.vin[0].scriptWitness = witness2;
     669                 :             : 
     670         [ +  - ]:           1 :     CTransactionRef ptx_child1 = MakeTransactionRef(mtx_child1);
     671         [ +  - ]:           1 :     CTransactionRef ptx_child2 = MakeTransactionRef(mtx_child2);
     672                 :             : 
     673                 :             :     // child1 and child2 have the same txid
     674   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ptx_child1->GetHash(), ptx_child2->GetHash());
     675                 :             :     // child1 and child2 have different wtxids
     676   [ +  -  +  -  :           3 :     BOOST_CHECK(ptx_child1->GetWitnessHash() != ptx_child2->GetWitnessHash());
             +  -  +  - ]
     677                 :             :     // Check that they have different package hashes
     678   [ +  -  +  -  :           9 :     BOOST_CHECK(GetPackageHash({ptx_parent, ptx_child1}) != GetPackageHash({ptx_parent, ptx_child2}));
          +  -  +  -  +  
          -  +  +  +  -  
          +  +  +  -  -  
          -  -  -  -  -  
                   -  - ]
     679                 :             : 
     680                 :             :     // Try submitting Package1{parent, child1} and Package2{parent, child2} where the children are
     681                 :             :     // same-txid-different-witness.
     682                 :           1 :     {
     683   [ +  +  +  -  :           3 :         Package package_parent_child1{ptx_parent, ptx_child1};
             -  -  -  - ]
     684         [ +  - ]:           1 :         const auto submit_witness1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     685   [ +  -  +  - ]:           1 :                                                        package_parent_child1, /*test_accept=*/false, /*client_maxfeerate=*/{});
     686   [ +  -  -  + ]:           1 :         if (auto err_witness1{CheckPackageMempoolAcceptResult(package_parent_child1, submit_witness1, /*expect_valid=*/true, m_node.mempool.get())}) {
     687   [ #  #  #  #  :           0 :             BOOST_ERROR(err_witness1.value());
                   #  # ]
     688                 :           0 :         }
     689                 :             : 
     690                 :             :         // Child2 would have been validated individually.
     691   [ +  +  +  -  :           3 :         Package package_parent_child2{ptx_parent, ptx_child2};
             -  -  -  - ]
     692         [ +  - ]:           1 :         const auto submit_witness2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     693   [ +  -  +  - ]:           1 :                                                        package_parent_child2, /*test_accept=*/false, /*client_maxfeerate=*/{});
     694   [ +  -  -  + ]:           1 :         if (auto err_witness2{CheckPackageMempoolAcceptResult(package_parent_child2, submit_witness2, /*expect_valid=*/true, m_node.mempool.get())}) {
     695   [ #  #  #  #  :           0 :             BOOST_ERROR(err_witness2.value());
                   #  # ]
     696                 :             :         } else {
     697                 :           1 :             auto it_parent2_deduped = submit_witness2.m_tx_results.find(ptx_parent->GetWitnessHash());
     698                 :           1 :             auto it_child2 = submit_witness2.m_tx_results.find(ptx_child2->GetWitnessHash());
     699   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent2_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
                   +  - ]
     700   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
                   +  - ]
     701   [ +  -  +  -  :           1 :             BOOST_CHECK_EQUAL(ptx_child1->GetWitnessHash(), it_child2->second.m_other_wtxid.value());
                   +  - ]
     702                 :           0 :         }
     703                 :             : 
     704                 :             :         // Deduplication should work when wtxid != txid. Submit package with the already-in-mempool
     705                 :             :         // transactions again, which should not fail.
     706         [ +  - ]:           1 :         const auto submit_segwit_dedup = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     707   [ +  -  +  - ]:           1 :                                                            package_parent_child1, /*test_accept=*/false, /*client_maxfeerate=*/{});
     708   [ +  -  -  + ]:           1 :         if (auto err_segwit_dedup{CheckPackageMempoolAcceptResult(package_parent_child1, submit_segwit_dedup, /*expect_valid=*/true, m_node.mempool.get())}) {
     709   [ #  #  #  #  :           0 :             BOOST_ERROR(err_segwit_dedup.value());
                   #  # ]
     710                 :             :         } else {
     711                 :           1 :             auto it_parent_dup = submit_segwit_dedup.m_tx_results.find(ptx_parent->GetWitnessHash());
     712                 :           1 :             auto it_child_dup = submit_segwit_dedup.m_tx_results.find(ptx_child1->GetWitnessHash());
     713   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
                   +  - ]
     714   [ +  -  +  - ]:           2 :             BOOST_CHECK(it_child_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
     715                 :           0 :         }
     716                 :           1 :     }
     717                 :             : 
     718                 :             :     // Try submitting Package1{child2, grandchild} where child2 is same-txid-different-witness as
     719                 :             :     // the in-mempool transaction, child1. Since child1 exists in the mempool and its outputs are
     720                 :             :     // available, child2 should be ignored and grandchild should be accepted.
     721                 :             :     //
     722                 :             :     // This tests a potential censorship vector in which an attacker broadcasts a competing package
     723                 :             :     // where a parent's witness is mutated. The honest package should be accepted despite the fact
     724                 :             :     // that we don't allow witness replacement.
     725                 :           1 :     CKey grandchild_key = GenerateRandomKey();
     726   [ +  -  +  -  :           1 :     CScript grandchild_locking_script = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
                   +  - ]
     727         [ +  - ]:           2 :     auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/ptx_child2, /*input_vout=*/0,
     728                 :             :                                                         /*input_height=*/0, /*input_signing_key=*/child_key,
     729                 :             :                                                         /*output_destination=*/grandchild_locking_script,
     730         [ +  - ]:           2 :                                                         /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
     731         [ +  - ]:           1 :     CTransactionRef ptx_grandchild = MakeTransactionRef(mtx_grandchild);
     732                 :             :     // Check that they have different package hashes
     733   [ +  -  +  -  :           9 :     BOOST_CHECK(GetPackageHash({ptx_child1, ptx_grandchild}) != GetPackageHash({ptx_child2, ptx_grandchild}));
          +  -  +  -  +  
          -  +  +  +  -  
          +  +  +  -  -  
          -  -  -  -  -  
                   -  - ]
     734                 :             :     // We already submitted child1 above.
     735                 :           1 :     {
     736   [ +  +  +  -  :           3 :         Package package_child2_grandchild{ptx_child2, ptx_grandchild};
             -  -  -  - ]
     737         [ +  - ]:           1 :         const auto submit_spend_ignored = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     738   [ +  -  +  - ]:           1 :                                                             package_child2_grandchild, /*test_accept=*/false, /*client_maxfeerate=*/{});
     739   [ +  -  -  + ]:           1 :         if (auto err_spend_ignored{CheckPackageMempoolAcceptResult(package_child2_grandchild, submit_spend_ignored, /*expect_valid=*/true, m_node.mempool.get())}) {
     740   [ #  #  #  #  :           0 :             BOOST_ERROR(err_spend_ignored.value());
                   #  # ]
     741                 :             :         } else {
     742                 :           1 :             auto it_child2_ignored = submit_spend_ignored.m_tx_results.find(ptx_child2->GetWitnessHash());
     743                 :           1 :             auto it_grandchild = submit_spend_ignored.m_tx_results.find(ptx_grandchild->GetWitnessHash());
     744   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child2_ignored->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
                   +  - ]
     745   [ +  -  +  - ]:           2 :             BOOST_CHECK(it_grandchild->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
     746                 :           0 :         }
     747                 :           1 :     }
     748                 :             : 
     749                 :             :     // A package Package{parent1, parent2, parent3, child} where the parents are a mixture of
     750                 :             :     // identical-tx-in-mempool, same-txid-different-witness-in-mempool, and new transactions.
     751                 :           1 :     Package package_mixed;
     752                 :             : 
     753                 :             :     // Give all the parents anyone-can-spend scripts so we don't have to deal with signing the child.
     754         [ +  - ]:           1 :     CScript acs_script = CScript() << OP_TRUE;
     755   [ +  -  +  - ]:           1 :     CScript acs_spk = GetScriptForDestination(WitnessV0ScriptHash(acs_script));
     756                 :           1 :     CScriptWitness acs_witness;
     757   [ -  +  +  - ]:           1 :     acs_witness.stack.emplace_back(acs_script.begin(), acs_script.end());
     758                 :             : 
     759                 :             :     // parent1 will already be in the mempool
     760   [ +  -  +  - ]:           2 :     auto mtx_parent1 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
     761                 :             :                                                      /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     762                 :             :                                                      /*output_destination=*/acs_spk,
     763         [ +  - ]:           2 :                                                      /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
     764         [ +  - ]:           1 :     CTransactionRef ptx_parent1 = MakeTransactionRef(mtx_parent1);
     765         [ +  - ]:           1 :     package_mixed.push_back(ptx_parent1);
     766                 :             : 
     767                 :             :     // parent2 will have a same-txid-different-witness tx already in the mempool
     768   [ +  -  +  - ]:           1 :     CScript grandparent2_script = CScript() << OP_DROP << OP_TRUE;
     769   [ +  -  +  - ]:           1 :     CScript grandparent2_spk = GetScriptForDestination(WitnessV0ScriptHash(grandparent2_script));
     770                 :           1 :     CScriptWitness parent2_witness1;
     771         [ +  - ]:           1 :     parent2_witness1.stack.emplace_back(1);
     772   [ -  +  +  - ]:           1 :     parent2_witness1.stack.emplace_back(grandparent2_script.begin(), grandparent2_script.end());
     773                 :           1 :     CScriptWitness parent2_witness2;
     774         [ +  - ]:           1 :     parent2_witness2.stack.emplace_back(2);
     775   [ -  +  +  - ]:           1 :     parent2_witness2.stack.emplace_back(grandparent2_script.begin(), grandparent2_script.end());
     776                 :             : 
     777                 :             :     // Create grandparent2 creating an output with multiple spending paths. Submit to mempool.
     778   [ +  -  +  - ]:           2 :     auto mtx_grandparent2 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
     779                 :             :                                                           /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     780                 :             :                                                           /*output_destination=*/grandparent2_spk,
     781         [ +  - ]:           2 :                                                           /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
     782         [ +  - ]:           1 :     CTransactionRef ptx_grandparent2 = MakeTransactionRef(mtx_grandparent2);
     783                 :             : 
     784         [ +  - ]:           1 :     CMutableTransaction mtx_parent2_v1;
     785                 :           1 :     mtx_parent2_v1.version = 1;
     786                 :           1 :     mtx_parent2_v1.vin = {
     787         [ +  - ]:           1 :         CTxIn{COutPoint{ptx_grandparent2->GetHash(), 0}},
     788   [ -  +  +  +  :           2 :     };
                   -  - ]
     789         [ +  - ]:           1 :     mtx_parent2_v1.vin[0].scriptWitness = parent2_witness1;
     790                 :           1 :     mtx_parent2_v1.vout = {
     791                 :           1 :         CTxOut{48 * COIN, acs_spk},
     792   [ -  +  +  +  :           2 :     };
                   -  - ]
     793                 :             : 
     794         [ +  - ]:           1 :     CMutableTransaction mtx_parent2_v2{mtx_parent2_v1};
     795         [ +  - ]:           1 :     mtx_parent2_v2.vin[0].scriptWitness = parent2_witness2;
     796                 :             : 
     797         [ +  - ]:           1 :     CTransactionRef ptx_parent2_v1 = MakeTransactionRef(mtx_parent2_v1);
     798         [ +  - ]:           1 :     CTransactionRef ptx_parent2_v2 = MakeTransactionRef(mtx_parent2_v2);
     799                 :             :     // Put parent2_v1 in the package, submit parent2_v2 to the mempool.
     800         [ +  - ]:           1 :     const MempoolAcceptResult parent2_v2_result = m_node.chainman->ProcessTransaction(ptx_parent2_v2);
     801   [ +  -  +  -  :           2 :     BOOST_CHECK(parent2_v2_result.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
     802         [ +  - ]:           1 :     package_mixed.push_back(ptx_parent2_v1);
     803                 :             : 
     804                 :             :     // parent3 will be a new transaction. Put a low feerate to make it invalid on its own.
     805   [ +  -  +  - ]:           2 :     auto mtx_parent3 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[3], /*input_vout=*/0,
     806                 :             :                                                      /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     807                 :             :                                                      /*output_destination=*/acs_spk,
     808         [ +  - ]:           2 :                                                      /*output_amount=*/CAmount(50 * COIN - low_fee_amt), /*submit=*/false);
     809         [ +  - ]:           1 :     CTransactionRef ptx_parent3 = MakeTransactionRef(mtx_parent3);
     810         [ +  - ]:           1 :     package_mixed.push_back(ptx_parent3);
     811   [ +  -  +  -  :           2 :     BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*ptx_parent3)) > low_fee_amt);
          +  -  +  -  +  
                -  +  - ]
     812   [ +  -  +  -  :           2 :     BOOST_CHECK(m_node.mempool->m_opts.min_relay_feerate.GetFee(GetVirtualTransactionSize(*ptx_parent3)) <= low_fee_amt);
             +  -  +  - ]
     813                 :             : 
     814                 :             :     // child spends parent1, parent2, and parent3
     815                 :           1 :     CKey mixed_grandchild_key = GenerateRandomKey();
     816   [ +  -  +  -  :           1 :     CScript mixed_child_spk = GetScriptForDestination(WitnessV0KeyHash(mixed_grandchild_key.GetPubKey()));
                   +  - ]
     817                 :             : 
     818         [ +  - ]:           1 :     CMutableTransaction mtx_mixed_child;
     819         [ +  - ]:           1 :     mtx_mixed_child.vin.emplace_back(COutPoint(ptx_parent1->GetHash(), 0));
     820         [ +  - ]:           1 :     mtx_mixed_child.vin.emplace_back(COutPoint(ptx_parent2_v1->GetHash(), 0));
     821         [ +  - ]:           1 :     mtx_mixed_child.vin.emplace_back(COutPoint(ptx_parent3->GetHash(), 0));
     822         [ +  - ]:           1 :     mtx_mixed_child.vin[0].scriptWitness = acs_witness;
     823         [ +  - ]:           1 :     mtx_mixed_child.vin[1].scriptWitness = acs_witness;
     824         [ +  - ]:           1 :     mtx_mixed_child.vin[2].scriptWitness = acs_witness;
     825         [ +  - ]:           1 :     mtx_mixed_child.vout.emplace_back((48 + 49 + 50 - 1) * COIN, mixed_child_spk);
     826         [ +  - ]:           1 :     CTransactionRef ptx_mixed_child = MakeTransactionRef(mtx_mixed_child);
     827         [ +  - ]:           1 :     package_mixed.push_back(ptx_mixed_child);
     828                 :             : 
     829                 :             :     // Submit package:
     830                 :             :     // parent1 should be ignored
     831                 :             :     // parent2_v1 should be ignored (and v2 wtxid returned)
     832                 :             :     // parent3 should be accepted
     833                 :             :     // child should be accepted
     834                 :           1 :     {
     835   [ +  -  +  - ]:           1 :         const auto mixed_result = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_mixed, false, /*client_maxfeerate=*/{});
     836   [ +  -  -  + ]:           1 :         if (auto err_mixed{CheckPackageMempoolAcceptResult(package_mixed, mixed_result, /*expect_valid=*/true, m_node.mempool.get())}) {
     837   [ #  #  #  #  :           0 :             BOOST_ERROR(err_mixed.value());
                   #  # ]
     838                 :             :         } else {
     839                 :           1 :             auto it_parent1 = mixed_result.m_tx_results.find(ptx_parent1->GetWitnessHash());
     840                 :           1 :             auto it_parent2 = mixed_result.m_tx_results.find(ptx_parent2_v1->GetWitnessHash());
     841                 :           1 :             auto it_parent3 = mixed_result.m_tx_results.find(ptx_parent3->GetWitnessHash());
     842                 :           1 :             auto it_child = mixed_result.m_tx_results.find(ptx_mixed_child->GetWitnessHash());
     843                 :             : 
     844   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent1->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
                   +  - ]
     845   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
                   +  - ]
     846   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent3->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
     847   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
     848   [ +  -  +  -  :           1 :             BOOST_CHECK_EQUAL(ptx_parent2_v2->GetWitnessHash(), it_parent2->second.m_other_wtxid.value());
                   +  - ]
     849                 :             : 
     850                 :             :             // package feerate should include parent3 and child. It should not include parent1 or parent2_v1.
     851   [ +  -  +  -  :           1 :             const CFeeRate expected_feerate(1 * COIN, GetVirtualTransactionSize(*ptx_parent3) + GetVirtualTransactionSize(*ptx_mixed_child));
                   +  - ]
     852   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent3->second.m_effective_feerate.value() == expected_feerate);
             +  -  +  - ]
     853   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_effective_feerate.value() == expected_feerate);
             +  -  +  - ]
     854         [ +  - ]:           1 :             std::vector<Wtxid> expected_wtxids({ptx_parent3->GetWitnessHash(), ptx_mixed_child->GetWitnessHash()});
     855   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent3->second.m_wtxids_fee_calculations.value() == expected_wtxids);
             +  -  +  - ]
     856   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_wtxids_fee_calculations.value() == expected_wtxids);
                   +  - ]
     857                 :           1 :         }
     858         [ +  - ]:           1 :     }
     859   [ +  -  +  -  :          46 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     860                 :             : 
     861   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_cpfp_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     862                 :             : {
     863                 :           1 :     mineBlocks(5);
     864                 :           1 :     MockMempoolMinFee(CFeeRate(5000), *m_node.mempool);
     865                 :           1 :     LOCK(::cs_main);
     866         [ +  - ]:           1 :     size_t expected_pool_size = m_node.mempool->size();
     867                 :           1 :     CKey child_key = GenerateRandomKey();
     868   [ +  -  +  -  :           1 :     CScript parent_spk = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
                   +  - ]
     869                 :           1 :     CKey grandchild_key = GenerateRandomKey();
     870   [ +  -  +  -  :           1 :     CScript child_spk = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
                   +  - ]
     871                 :             : 
     872                 :             :     // low-fee parent and high-fee child package
     873                 :           1 :     const CAmount coinbase_value{50 * COIN};
     874                 :           1 :     const CAmount parent_value{coinbase_value - low_fee_amt};
     875                 :           1 :     const CAmount child_value{parent_value - COIN};
     876                 :             : 
     877                 :           1 :     Package package_cpfp;
     878         [ +  - ]:           2 :     auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
     879         [ +  - ]:           1 :                                                     /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     880                 :             :                                                     /*output_destination=*/parent_spk,
     881         [ +  - ]:           2 :                                                     /*output_amount=*/parent_value, /*submit=*/false);
     882         [ +  - ]:           1 :     CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
     883         [ +  - ]:           1 :     package_cpfp.push_back(tx_parent);
     884                 :             : 
     885         [ +  - ]:           2 :     auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
     886                 :             :                                                    /*input_height=*/101, /*input_signing_key=*/child_key,
     887                 :             :                                                    /*output_destination=*/child_spk,
     888         [ +  - ]:           2 :                                                    /*output_amount=*/child_value, /*submit=*/false);
     889         [ +  - ]:           1 :     CTransactionRef tx_child = MakeTransactionRef(mtx_child);
     890         [ +  - ]:           1 :     package_cpfp.push_back(tx_child);
     891                 :             : 
     892                 :             :     // Package feerate is calculated using modified fees, and prioritisetransaction accepts negative
     893                 :             :     // fee deltas. This should be taken into account. De-prioritise the parent transaction
     894                 :             :     // to bring the package feerate to 0.
     895         [ +  - ]:           1 :     m_node.mempool->PrioritiseTransaction(tx_parent->GetHash(), child_value - coinbase_value);
     896                 :           1 :     {
     897   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     898         [ +  - ]:           1 :         const auto submit_cpfp_deprio = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     899   [ +  -  +  - ]:           1 :                                                    package_cpfp, /*test_accept=*/ false, /*client_maxfeerate=*/{});
     900   [ +  -  -  + ]:           1 :         if (auto err_cpfp_deprio{CheckPackageMempoolAcceptResult(package_cpfp, submit_cpfp_deprio, /*expect_valid=*/false, m_node.mempool.get())}) {
     901   [ #  #  #  #  :           0 :             BOOST_ERROR(err_cpfp_deprio.value());
                   #  # ]
     902                 :             :         } else {
     903   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_state.GetResult(), PackageValidationResult::PCKG_TX);
     904   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_tx_results.find(tx_parent->GetWitnessHash())->second.m_state.GetResult(),
     905                 :             :                               TxValidationResult::TX_RECONSIDERABLE);
     906   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_tx_results.find(tx_child->GetWitnessHash())->second.m_state.GetResult(),
     907                 :             :                               TxValidationResult::TX_RECONSIDERABLE);
     908   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_tx_results.find(tx_parent->GetWitnessHash())->second.m_state.GetRejectReason(), "mempool min fee not met");
                   +  - ]
     909   [ +  -  +  -  :           1 :             BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     910                 :           0 :         }
     911                 :           1 :     }
     912                 :             : 
     913                 :             :     // Clear the prioritisation of the parent transaction.
     914         [ +  - ]:           3 :     WITH_LOCK(m_node.mempool->cs, m_node.mempool->ClearPrioritisation(tx_parent->GetHash()));
     915                 :             : 
     916                 :             :     // Package CPFP: Even though the parent's feerate is below the mempool minimum feerate, the
     917                 :             :     // child pays enough for the package feerate to meet the threshold.
     918                 :           1 :     {
     919   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     920         [ +  - ]:           1 :         const auto submit_cpfp = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     921   [ +  -  +  - ]:           1 :                                                    package_cpfp, /*test_accept=*/ false, /*client_maxfeerate=*/{});
     922   [ +  -  -  + ]:           1 :         if (auto err_cpfp{CheckPackageMempoolAcceptResult(package_cpfp, submit_cpfp, /*expect_valid=*/true, m_node.mempool.get())}) {
     923   [ #  #  #  #  :           0 :             BOOST_ERROR(err_cpfp.value());
                   #  # ]
     924                 :             :         } else {
     925                 :           1 :             auto it_parent = submit_cpfp.m_tx_results.find(tx_parent->GetWitnessHash());
     926                 :           1 :             auto it_child = submit_cpfp.m_tx_results.find(tx_child->GetWitnessHash());
     927   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
     928   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_base_fees.value() == coinbase_value - parent_value);
             +  -  +  - ]
     929   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
     930   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_base_fees.value() == COIN);
             +  -  +  - ]
     931                 :             : 
     932         [ +  - ]:           1 :             const CFeeRate expected_feerate(coinbase_value - child_value,
     933   [ +  -  +  -  :           1 :                                             GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child));
                   +  - ]
     934   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_effective_feerate.value() == expected_feerate);
             +  -  +  - ]
     935   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_effective_feerate.value() == expected_feerate);
             +  -  +  - ]
     936         [ +  - ]:           1 :             std::vector<Wtxid> expected_wtxids({tx_parent->GetWitnessHash(), tx_child->GetWitnessHash()});
     937   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_wtxids_fee_calculations.value() == expected_wtxids);
             +  -  +  - ]
     938   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_wtxids_fee_calculations.value() == expected_wtxids);
             +  -  +  - ]
     939   [ +  -  +  -  :           2 :             BOOST_CHECK(expected_feerate.GetFeePerK() > 1000);
                   +  - ]
     940                 :           1 :         }
     941                 :           1 :         expected_pool_size += 2;
     942   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     943                 :           1 :     }
     944                 :             : 
     945                 :             :     // Just because we allow low-fee parents doesn't mean we allow low-feerate packages.
     946                 :             :     // The mempool minimum feerate is 5sat/vB, but this package just pays 800 satoshis total.
     947                 :             :     // The child fees would be able to pay for itself, but isn't enough for the entire package.
     948                 :           1 :     Package package_still_too_low;
     949                 :           1 :     const CAmount parent_fee{200};
     950                 :           1 :     const CAmount child_fee{600};
     951   [ +  -  +  - ]:           2 :     auto mtx_parent_cheap = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
     952                 :             :                                                           /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
     953                 :             :                                                           /*output_destination=*/parent_spk,
     954         [ +  - ]:           2 :                                                           /*output_amount=*/coinbase_value - parent_fee, /*submit=*/false);
     955         [ +  - ]:           1 :     CTransactionRef tx_parent_cheap = MakeTransactionRef(mtx_parent_cheap);
     956         [ +  - ]:           1 :     package_still_too_low.push_back(tx_parent_cheap);
     957   [ +  -  +  -  :           2 :     BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*tx_parent_cheap)) > parent_fee);
          +  -  +  -  +  
                -  +  - ]
     958   [ +  -  +  -  :           2 :     BOOST_CHECK(m_node.mempool->m_opts.min_relay_feerate.GetFee(GetVirtualTransactionSize(*tx_parent_cheap)) <= parent_fee);
             +  -  +  - ]
     959                 :             : 
     960         [ +  - ]:           2 :     auto mtx_child_cheap = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_cheap, /*input_vout=*/0,
     961                 :             :                                                          /*input_height=*/101, /*input_signing_key=*/child_key,
     962                 :             :                                                          /*output_destination=*/child_spk,
     963         [ +  - ]:           2 :                                                          /*output_amount=*/coinbase_value - parent_fee - child_fee, /*submit=*/false);
     964         [ +  - ]:           1 :     CTransactionRef tx_child_cheap = MakeTransactionRef(mtx_child_cheap);
     965         [ +  - ]:           1 :     package_still_too_low.push_back(tx_child_cheap);
     966   [ +  -  +  -  :           2 :     BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*tx_child_cheap)) <= child_fee);
          +  -  +  -  +  
                -  +  - ]
     967   [ +  -  +  -  :           2 :     BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap)) > parent_fee + child_fee);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     968   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     969                 :             : 
     970                 :             :     // Cheap package should fail for being too low fee.
     971                 :           1 :     {
     972         [ +  - ]:           1 :         const auto submit_package_too_low = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     973   [ +  -  +  - ]:           1 :                                                    package_still_too_low, /*test_accept=*/false, /*client_maxfeerate=*/{});
     974   [ +  -  -  + ]:           1 :         if (auto err_package_too_low{CheckPackageMempoolAcceptResult(package_still_too_low, submit_package_too_low, /*expect_valid=*/false, m_node.mempool.get())}) {
     975   [ #  #  #  #  :           0 :             BOOST_ERROR(err_package_too_low.value());
                   #  # ]
     976                 :             :         } else {
     977                 :             :             // Individual feerate of parent is too low.
     978   [ +  -  +  -  :           1 :             BOOST_CHECK_EQUAL(submit_package_too_low.m_tx_results.at(tx_parent_cheap->GetWitnessHash()).m_state.GetResult(),
                   +  - ]
     979                 :             :                               TxValidationResult::TX_RECONSIDERABLE);
     980   [ +  -  +  -  :           2 :             BOOST_CHECK(submit_package_too_low.m_tx_results.at(tx_parent_cheap->GetWitnessHash()).m_effective_feerate.value() ==
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     981                 :             :                         CFeeRate(parent_fee, GetVirtualTransactionSize(*tx_parent_cheap)));
     982                 :             :             // Package feerate of parent + child is too low.
     983   [ +  -  +  -  :           1 :             BOOST_CHECK_EQUAL(submit_package_too_low.m_tx_results.at(tx_child_cheap->GetWitnessHash()).m_state.GetResult(),
                   +  - ]
     984                 :             :                               TxValidationResult::TX_RECONSIDERABLE);
     985   [ +  -  +  -  :           2 :             BOOST_CHECK(submit_package_too_low.m_tx_results.at(tx_child_cheap->GetWitnessHash()).m_effective_feerate.value() ==
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     986                 :             :                         CFeeRate(parent_fee + child_fee, GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap)));
     987                 :           0 :         }
     988   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetResult(), PackageValidationResult::PCKG_TX);
     989   [ +  -  -  +  :           2 :         BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetRejectReason(), "transaction failed");
                   +  - ]
     990   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
     991                 :           1 :     }
     992                 :             : 
     993                 :             :     // Package feerate includes the modified fees of the transactions.
     994                 :             :     // This means a child with its fee delta from prioritisetransaction can pay for a parent.
     995         [ +  - ]:           1 :     m_node.mempool->PrioritiseTransaction(tx_child_cheap->GetHash(), 1 * COIN);
     996                 :             :     // Now that the child's fees have "increased" by 1 BTC, the cheap package should succeed.
     997                 :           1 :     {
     998         [ +  - ]:           1 :         const auto submit_prioritised_package = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
     999   [ +  -  +  - ]:           1 :                                                                   package_still_too_low, /*test_accept=*/false, /*client_maxfeerate=*/{});
    1000   [ +  -  -  + ]:           1 :         if (auto err_prioritised{CheckPackageMempoolAcceptResult(package_still_too_low, submit_prioritised_package, /*expect_valid=*/true, m_node.mempool.get())}) {
    1001   [ #  #  #  #  :           0 :             BOOST_ERROR(err_prioritised.value());
                   #  # ]
    1002                 :             :         } else {
    1003         [ +  - ]:           1 :             const CFeeRate expected_feerate(1 * COIN + parent_fee + child_fee,
    1004   [ +  -  +  -  :           1 :                 GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap));
                   +  - ]
    1005   [ +  -  -  +  :           1 :             BOOST_CHECK_EQUAL(submit_prioritised_package.m_tx_results.size(), package_still_too_low.size());
                   +  - ]
    1006                 :           1 :             auto it_parent = submit_prioritised_package.m_tx_results.find(tx_parent_cheap->GetWitnessHash());
    1007                 :           1 :             auto it_child = submit_prioritised_package.m_tx_results.find(tx_child_cheap->GetWitnessHash());
    1008   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
    1009   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_base_fees.value() == parent_fee);
             +  -  +  - ]
    1010   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_effective_feerate.value() == expected_feerate);
             +  -  +  - ]
    1011   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
    1012   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_base_fees.value() == child_fee);
             +  -  +  - ]
    1013   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_effective_feerate.value() == expected_feerate);
             +  -  +  - ]
    1014         [ +  - ]:           1 :             std::vector<Wtxid> expected_wtxids({tx_parent_cheap->GetWitnessHash(), tx_child_cheap->GetWitnessHash()});
    1015   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_wtxids_fee_calculations.value() == expected_wtxids);
             +  -  +  - ]
    1016   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_wtxids_fee_calculations.value() == expected_wtxids);
                   +  - ]
    1017                 :           1 :         }
    1018                 :           1 :         expected_pool_size += 2;
    1019   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1020                 :           1 :     }
    1021                 :             : 
    1022                 :             :     // Package feerate is calculated without topology in mind; it's just aggregating fees and sizes.
    1023                 :             :     // However, this should not allow parents to pay for children. Each transaction should be
    1024                 :             :     // validated individually first, eliminating sufficient-feerate parents before they are unfairly
    1025                 :             :     // included in the package feerate. It's also important that the low-fee child doesn't prevent
    1026                 :             :     // the parent from being accepted.
    1027                 :           1 :     Package package_rich_parent;
    1028                 :           1 :     const CAmount high_parent_fee{1 * COIN};
    1029   [ +  -  +  - ]:           2 :     auto mtx_parent_rich = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
    1030                 :             :                                                          /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
    1031                 :             :                                                          /*output_destination=*/parent_spk,
    1032         [ +  - ]:           2 :                                                          /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
    1033         [ +  - ]:           1 :     CTransactionRef tx_parent_rich = MakeTransactionRef(mtx_parent_rich);
    1034         [ +  - ]:           1 :     package_rich_parent.push_back(tx_parent_rich);
    1035                 :             : 
    1036         [ +  - ]:           2 :     auto mtx_child_poor = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_rich, /*input_vout=*/0,
    1037                 :             :                                                         /*input_height=*/101, /*input_signing_key=*/child_key,
    1038                 :             :                                                         /*output_destination=*/child_spk,
    1039         [ +  - ]:           2 :                                                         /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
    1040         [ +  - ]:           1 :     CTransactionRef tx_child_poor = MakeTransactionRef(mtx_child_poor);
    1041         [ +  - ]:           1 :     package_rich_parent.push_back(tx_child_poor);
    1042                 :             : 
    1043                 :             :     // Parent pays 1 BTC and child pays none. The parent should be accepted without the child.
    1044                 :           1 :     {
    1045   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1046         [ +  - ]:           1 :         const auto submit_rich_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
    1047   [ +  -  +  - ]:           1 :                                                           package_rich_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
    1048   [ +  -  -  + ]:           1 :         if (auto err_rich_parent{CheckPackageMempoolAcceptResult(package_rich_parent, submit_rich_parent, /*expect_valid=*/false, m_node.mempool.get())}) {
    1049   [ #  #  #  #  :           0 :             BOOST_ERROR(err_rich_parent.value());
                   #  # ]
    1050                 :             :         } else {
    1051                 :             :             // The child would have been validated on its own and failed.
    1052   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetResult(), PackageValidationResult::PCKG_TX);
    1053   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetRejectReason(), "transaction failed");
                   +  - ]
    1054                 :             : 
    1055                 :           1 :             auto it_parent = submit_rich_parent.m_tx_results.find(tx_parent_rich->GetWitnessHash());
    1056                 :           1 :             auto it_child = submit_rich_parent.m_tx_results.find(tx_child_poor->GetWitnessHash());
    1057   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
                   +  - ]
    1058   [ +  -  +  -  :           2 :             BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
                   +  - ]
    1059   [ +  -  -  +  :           3 :             BOOST_CHECK(it_parent->second.m_state.GetRejectReason() == "");
             +  -  +  - ]
    1060   [ +  -  +  -  :           2 :             BOOST_CHECK_MESSAGE(it_parent->second.m_base_fees.value() == high_parent_fee,
          +  -  +  -  +  
                      - ]
    1061                 :             :                     strprintf("rich parent: expected fee %s, got %s", high_parent_fee, it_parent->second.m_base_fees.value()));
    1062   [ +  -  +  -  :           2 :             BOOST_CHECK(it_parent->second.m_effective_feerate == CFeeRate(high_parent_fee, GetVirtualTransactionSize(*tx_parent_rich)));
          +  -  +  -  +  
                      - ]
    1063   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(it_child->second.m_result_type, MempoolAcceptResult::ResultType::INVALID);
    1064   [ +  -  +  - ]:           1 :             BOOST_CHECK_EQUAL(it_child->second.m_state.GetResult(), TxValidationResult::TX_RECONSIDERABLE);
    1065   [ +  -  -  +  :           2 :             BOOST_CHECK_EQUAL(it_child->second.m_state.GetRejectReason(), "mempool min fee not met");
                   +  - ]
    1066                 :           0 :         }
    1067                 :           1 :         expected_pool_size += 1;
    1068   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1069         [ +  - ]:           1 :     }
    1070   [ +  -  +  -  :          13 : }
          +  -  +  -  +  
                -  +  - ]
    1071                 :             : 
    1072   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(package_rbf_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1073                 :             : {
    1074                 :           1 :     mineBlocks(5);
    1075                 :           1 :     LOCK(::cs_main);
    1076         [ +  - ]:           1 :     size_t expected_pool_size = m_node.mempool->size();
    1077                 :           1 :     CKey child_key{GenerateRandomKey()};
    1078   [ +  -  +  -  :           1 :     CScript parent_spk = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
                   +  - ]
    1079                 :           1 :     CKey grandchild_key{GenerateRandomKey()};
    1080   [ +  -  +  -  :           1 :     CScript child_spk = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
                   +  - ]
    1081                 :             : 
    1082                 :           1 :     const CAmount coinbase_value{50 * COIN};
    1083                 :             :     // Test that de-duplication works. This is not actually package rbf.
    1084                 :           1 :     {
    1085                 :             :         // 1 parent paying 200sat, 1 child paying 300sat
    1086                 :           1 :         Package package1;
    1087                 :             :         // 1 parent paying 200sat, 1 child paying 500sat
    1088                 :           1 :         Package package2;
    1089                 :             :         // Package1 and package2 have the same parent. The children conflict.
    1090         [ +  - ]:           2 :         auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
    1091         [ +  - ]:           1 :                                                         /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
    1092                 :             :                                                         /*output_destination=*/parent_spk,
    1093         [ +  - ]:           2 :                                                         /*output_amount=*/coinbase_value - low_fee_amt, /*submit=*/false);
    1094         [ +  - ]:           1 :         CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
    1095         [ +  - ]:           1 :         package1.push_back(tx_parent);
    1096         [ +  - ]:           1 :         package2.push_back(tx_parent);
    1097                 :             : 
    1098   [ +  -  +  -  :           5 :         CTransactionRef tx_child_1 = MakeTransactionRef(CreateValidMempoolTransaction(tx_parent, 0, 101, child_key, child_spk, coinbase_value - low_fee_amt - 300, false));
                   +  - ]
    1099         [ +  - ]:           1 :         package1.push_back(tx_child_1);
    1100   [ +  -  +  -  :           5 :         CTransactionRef tx_child_2 = MakeTransactionRef(CreateValidMempoolTransaction(tx_parent, 0, 101, child_key, child_spk, coinbase_value - low_fee_amt - 500, false));
                   +  - ]
    1101         [ +  - ]:           1 :         package2.push_back(tx_child_2);
    1102                 :             : 
    1103         [ +  - ]:           1 :         LOCK(m_node.mempool->cs);
    1104   [ +  -  +  - ]:           1 :         const auto submit1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, /*test_accept=*/false, std::nullopt);
    1105   [ +  -  -  + ]:           1 :         if (auto err_1{CheckPackageMempoolAcceptResult(package1, submit1, /*expect_valid=*/true, m_node.mempool.get())}) {
    1106   [ #  #  #  #  :           0 :             BOOST_ERROR(err_1.value());
                   #  # ]
    1107                 :           0 :         }
    1108                 :             : 
    1109                 :             :         // Check precise ResultTypes and mempool size. We know it_parent_1 and it_child_1 exist from above call
    1110                 :           1 :         auto it_parent_1 = submit1.m_tx_results.find(tx_parent->GetWitnessHash());
    1111                 :           1 :         auto it_child_1 = submit1.m_tx_results.find(tx_child_1->GetWitnessHash());
    1112   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_parent_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1113   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_child_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1114                 :           1 :         expected_pool_size += 2;
    1115   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1116                 :             : 
    1117   [ +  -  +  - ]:           1 :         const auto submit2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package2, /*test_accept=*/false, std::nullopt);
    1118   [ +  -  -  + ]:           1 :         if (auto err_2{CheckPackageMempoolAcceptResult(package2, submit2, /*expect_valid=*/true, m_node.mempool.get())}) {
    1119   [ #  #  #  #  :           0 :             BOOST_ERROR(err_2.value());
                   #  # ]
    1120                 :           0 :         }
    1121                 :             : 
    1122                 :             :         // Check precise ResultTypes and mempool size. We know it_parent_2 and it_child_2 exist from above call
    1123                 :           1 :         auto it_parent_2 = submit2.m_tx_results.find(tx_parent->GetWitnessHash());
    1124                 :           1 :         auto it_child_2 = submit2.m_tx_results.find(tx_child_2->GetWitnessHash());
    1125   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_parent_2->second.m_result_type, MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
    1126   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_child_2->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1127   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1128                 :             : 
    1129                 :             :         // child1 has been replaced
    1130   [ +  -  +  -  :           2 :         BOOST_CHECK(!m_node.mempool->exists(tx_child_1->GetHash()));
                   +  - ]
    1131   [ +  -  +  -  :           6 :     }
             +  -  +  - ]
    1132                 :             : 
    1133                 :             :     // Test package rbf.
    1134                 :           1 :     {
    1135   [ +  -  +  -  :           4 :         CTransactionRef tx_parent_1 = MakeTransactionRef(CreateValidMempoolTransaction(
                   +  - ]
    1136         [ +  - ]:           1 :             m_coinbase_txns[1], /*input_vout=*/0, /*input_height=*/0,
    1137                 :           1 :             coinbaseKey, parent_spk, coinbase_value - 200, /*submit=*/false));
    1138   [ +  -  +  -  :           4 :         CTransactionRef tx_child_1 = MakeTransactionRef(CreateValidMempoolTransaction(
                   +  - ]
    1139                 :             :             tx_parent_1, /*input_vout=*/0, /*input_height=*/101,
    1140                 :           1 :             child_key, child_spk, coinbase_value - 400, /*submit=*/false));
    1141                 :             : 
    1142   [ +  -  +  -  :           4 :         CTransactionRef tx_parent_2 = MakeTransactionRef(CreateValidMempoolTransaction(
                   +  - ]
    1143         [ +  - ]:           1 :             m_coinbase_txns[1], /*input_vout=*/0, /*input_height=*/0,
    1144                 :           1 :             coinbaseKey, parent_spk, coinbase_value - 800, /*submit=*/false));
    1145   [ +  -  +  -  :           4 :         CTransactionRef tx_child_2 = MakeTransactionRef(CreateValidMempoolTransaction(
                   +  - ]
    1146                 :             :             tx_parent_2, /*input_vout=*/0, /*input_height=*/101,
    1147                 :           1 :             child_key, child_spk, coinbase_value - 800 - 200, /*submit=*/false));
    1148                 :             : 
    1149   [ +  -  +  -  :           4 :         CTransactionRef tx_parent_3 = MakeTransactionRef(CreateValidMempoolTransaction(
                   +  - ]
    1150         [ +  - ]:           1 :             m_coinbase_txns[1], /*input_vout=*/0, /*input_height=*/0,
    1151                 :           1 :             coinbaseKey, parent_spk, coinbase_value - 199, /*submit=*/false));
    1152   [ +  -  +  -  :           4 :         CTransactionRef tx_child_3 = MakeTransactionRef(CreateValidMempoolTransaction(
                   +  - ]
    1153                 :             :             tx_parent_3, /*input_vout=*/0, /*input_height=*/101,
    1154                 :           1 :             child_key, child_spk, coinbase_value - 199 - 1300, /*submit=*/false));
    1155                 :             : 
    1156                 :             :         // In all packages, the parents conflict with each other
    1157   [ +  -  +  -  :           3 :         BOOST_CHECK(tx_parent_1->GetHash() != tx_parent_2->GetHash());
             +  -  +  - ]
    1158   [ +  -  +  -  :           3 :         BOOST_CHECK(tx_parent_2->GetHash() != tx_parent_3->GetHash());
             +  -  +  - ]
    1159                 :             : 
    1160                 :             :         // 1 parent paying 200sat, 1 child paying 200sat.
    1161   [ +  +  +  -  :           3 :         Package package1{tx_parent_1, tx_child_1};
             -  -  -  - ]
    1162                 :             :         // 1 parent paying 800sat, 1 child paying 200sat.
    1163   [ +  +  +  -  :           3 :         Package package2{tx_parent_2, tx_child_2};
             -  -  -  - ]
    1164                 :             :         // 1 parent paying 199sat, 1 child paying 1300sat.
    1165   [ +  +  +  -  :           3 :         Package package3{tx_parent_3, tx_child_3};
             -  -  -  - ]
    1166                 :             : 
    1167   [ +  -  +  - ]:           1 :         const auto submit1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, false, std::nullopt);
    1168   [ +  -  -  + ]:           1 :         if (auto err_1{CheckPackageMempoolAcceptResult(package1, submit1, /*expect_valid=*/true, m_node.mempool.get())}) {
    1169   [ #  #  #  #  :           0 :             BOOST_ERROR(err_1.value());
                   #  # ]
    1170                 :           0 :         }
    1171                 :           1 :         auto it_parent_1 = submit1.m_tx_results.find(tx_parent_1->GetWitnessHash());
    1172                 :           1 :         auto it_child_1 = submit1.m_tx_results.find(tx_child_1->GetWitnessHash());
    1173   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_parent_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1174   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_child_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1175                 :           1 :         expected_pool_size += 2;
    1176   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1177                 :             : 
    1178                 :             :         // This replacement is actually not package rbf; the parent carries enough fees
    1179                 :             :         // to replace the entire package on its own.
    1180   [ +  -  +  - ]:           1 :         const auto submit2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package2, false, std::nullopt);
    1181   [ +  -  -  + ]:           1 :         if (auto err_2{CheckPackageMempoolAcceptResult(package2, submit2, /*expect_valid=*/true, m_node.mempool.get())}) {
    1182   [ #  #  #  #  :           0 :             BOOST_ERROR(err_2.value());
                   #  # ]
    1183                 :           0 :         }
    1184                 :           1 :         auto it_parent_2 = submit2.m_tx_results.find(tx_parent_2->GetWitnessHash());
    1185                 :           1 :         auto it_child_2 = submit2.m_tx_results.find(tx_child_2->GetWitnessHash());
    1186   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_parent_2->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1187   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_child_2->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1188   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1189                 :             : 
    1190                 :             :         // Package RBF, in which the replacement transaction's child sponsors the fees to meet RBF feerate rules
    1191   [ +  -  +  - ]:           1 :         const auto submit3 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package3, false, std::nullopt);
    1192   [ +  -  -  + ]:           1 :         if (auto err_3{CheckPackageMempoolAcceptResult(package3, submit3, /*expect_valid=*/true, m_node.mempool.get())}) {
    1193   [ #  #  #  #  :           0 :             BOOST_ERROR(err_3.value());
                   #  # ]
    1194                 :           0 :         }
    1195                 :           1 :         auto it_parent_3 = submit3.m_tx_results.find(tx_parent_3->GetWitnessHash());
    1196                 :           1 :         auto it_child_3 = submit3.m_tx_results.find(tx_child_3->GetWitnessHash());
    1197   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_parent_3->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1198   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_child_3->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1199                 :             : 
    1200                 :             :         // package3 was considered as a package to replace both package2 transactions
    1201   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent_3->second.m_replaced_transactions.size() == 2);
                   +  - ]
    1202   [ +  -  +  -  :           2 :         BOOST_CHECK(it_child_3->second.m_replaced_transactions.empty());
                   +  - ]
    1203                 :             : 
    1204         [ +  - ]:           1 :         std::vector<Wtxid> expected_package3_wtxids({tx_parent_3->GetWitnessHash(), tx_child_3->GetWitnessHash()});
    1205   [ +  -  +  - ]:           1 :         const auto package3_total_vsize{GetVirtualTransactionSize(*tx_parent_3) + GetVirtualTransactionSize(*tx_child_3)};
    1206   [ +  -  +  -  :           2 :         BOOST_CHECK(it_parent_3->second.m_wtxids_fee_calculations.value() == expected_package3_wtxids);
             +  -  +  - ]
    1207   [ +  -  +  -  :           2 :         BOOST_CHECK(it_child_3->second.m_wtxids_fee_calculations.value() == expected_package3_wtxids);
             +  -  +  - ]
    1208   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_parent_3->second.m_effective_feerate.value().GetFee(package3_total_vsize), 199 + 1300);
             +  -  +  - ]
    1209   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(it_child_3->second.m_effective_feerate.value().GetFee(package3_total_vsize), 199 + 1300);
             +  -  +  - ]
    1210                 :             : 
    1211   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
                   +  - ]
    1212                 :             : 
    1213                 :             :         // Finally, check that we can prioritise tx_child_1 to get package1 into the mempool.
    1214                 :             :         // It should not be possible to resubmit package1 and get it in without prioritisation.
    1215   [ +  -  +  - ]:           1 :         const auto submit4 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, false, std::nullopt);
    1216   [ +  -  -  + ]:           1 :         if (auto err_4{CheckPackageMempoolAcceptResult(package1, submit4, /*expect_valid=*/false, m_node.mempool.get())}) {
    1217   [ #  #  #  #  :           0 :             BOOST_ERROR(err_4.value());
                   #  # ]
    1218                 :           0 :         }
    1219         [ +  - ]:           1 :         m_node.mempool->PrioritiseTransaction(tx_child_1->GetHash(), 1363);
    1220   [ +  -  +  - ]:           1 :         const auto submit5 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, false, std::nullopt);
    1221   [ +  -  -  + ]:           1 :         if (auto err_5{CheckPackageMempoolAcceptResult(package1, submit5, /*expect_valid=*/true, m_node.mempool.get())}) {
    1222   [ #  #  #  #  :           0 :             BOOST_ERROR(err_5.value());
                   #  # ]
    1223                 :           0 :         }
    1224                 :           1 :         it_parent_1 = submit5.m_tx_results.find(tx_parent_1->GetWitnessHash());
    1225                 :           1 :         it_child_1 = submit5.m_tx_results.find(tx_child_1->GetWitnessHash());
    1226   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_parent_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1227   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(it_child_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
    1228         [ +  - ]:           1 :         LOCK(m_node.mempool->cs);
    1229   [ +  -  +  -  :           2 :         BOOST_CHECK(m_node.mempool->GetIter(tx_parent_1->GetHash()).has_value());
             +  -  +  - ]
    1230   [ +  -  +  -  :           2 :         BOOST_CHECK(m_node.mempool->GetIter(tx_child_1->GetHash()).has_value());
             +  -  +  - ]
    1231   [ +  -  +  -  :           7 :     }
          +  -  +  -  +  
                -  +  - ]
    1232   [ +  -  +  -  :          11 : }
          -  +  +  -  +  
          -  -  +  +  -  
          +  -  -  +  +  
                      - ]
    1233                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1