Branch data Line data Source code
# 1 : : // Copyright (c) 2021 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 <primitives/transaction.h>
# 10 : : #include <script/script.h>
# 11 : : #include <script/standard.h>
# 12 : : #include <test/util/setup_common.h>
# 13 : : #include <validation.h>
# 14 : :
# 15 : : #include <boost/test/unit_test.hpp>
# 16 : :
# 17 : : BOOST_AUTO_TEST_SUITE(txpackage_tests)
# 18 : :
# 19 : : // Create placeholder transactions that have no meaning.
# 20 : : inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
# 21 : 56 : {
# 22 : 56 : CMutableTransaction mtx = CMutableTransaction();
# 23 : 56 : mtx.vin.resize(num_inputs);
# 24 : 56 : mtx.vout.resize(num_outputs);
# 25 : 56 : auto random_script = CScript() << ToByteVector(InsecureRand256()) << ToByteVector(InsecureRand256());
# 26 [ + + ]: 2406 : for (size_t i{0}; i < num_inputs; ++i) {
# 27 : 2350 : mtx.vin[i].prevout.hash = InsecureRand256();
# 28 : 2350 : mtx.vin[i].prevout.n = 0;
# 29 : 2350 : mtx.vin[i].scriptSig = random_script;
# 30 : 2350 : }
# 31 [ + + ]: 2406 : for (size_t o{0}; o < num_outputs; ++o) {
# 32 : 2350 : mtx.vout[o].nValue = 1 * CENT;
# 33 : 2350 : mtx.vout[o].scriptPubKey = random_script;
# 34 : 2350 : }
# 35 : 56 : return MakeTransactionRef(mtx);
# 36 : 56 : }
# 37 : :
# 38 : : BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
# 39 : 2 : {
# 40 : : // Packages can't have more than 25 transactions.
# 41 : 2 : Package package_too_many;
# 42 : 2 : package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
# 43 [ + + ]: 54 : for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
# 44 : 52 : package_too_many.emplace_back(create_placeholder_tx(1, 1));
# 45 : 52 : }
# 46 : 2 : PackageValidationState state_too_many;
# 47 : 2 : BOOST_CHECK(!CheckPackage(package_too_many, state_too_many));
# 48 : 2 : BOOST_CHECK_EQUAL(state_too_many.GetResult(), PackageValidationResult::PCKG_POLICY);
# 49 : 2 : BOOST_CHECK_EQUAL(state_too_many.GetRejectReason(), "package-too-many-transactions");
# 50 : :
# 51 : : // Packages can't have a total size of more than 101KvB.
# 52 : 2 : CTransactionRef large_ptx = create_placeholder_tx(150, 150);
# 53 : 2 : Package package_too_large;
# 54 : 2 : auto size_large = GetVirtualTransactionSize(*large_ptx);
# 55 : 2 : size_t total_size{0};
# 56 [ + + ]: 10 : while (total_size <= MAX_PACKAGE_SIZE * 1000) {
# 57 : 8 : package_too_large.push_back(large_ptx);
# 58 : 8 : total_size += size_large;
# 59 : 8 : }
# 60 : 2 : BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
# 61 : 2 : PackageValidationState state_too_large;
# 62 : 2 : BOOST_CHECK(!CheckPackage(package_too_large, state_too_large));
# 63 : 2 : BOOST_CHECK_EQUAL(state_too_large.GetResult(), PackageValidationResult::PCKG_POLICY);
# 64 : 2 : BOOST_CHECK_EQUAL(state_too_large.GetRejectReason(), "package-too-large");
# 65 : 2 : }
# 66 : :
# 67 : : BOOST_FIXTURE_TEST_CASE(package_validation_tests, TestChain100Setup)
# 68 : 2 : {
# 69 : 2 : LOCK(cs_main);
# 70 : 2 : unsigned int initialPoolSize = m_node.mempool->size();
# 71 : :
# 72 : : // Parent and Child Package
# 73 : 2 : CKey parent_key;
# 74 : 2 : parent_key.MakeNewKey(true);
# 75 : 2 : CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
# 76 : 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
# 77 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 78 : 2 : /*output_destination=*/parent_locking_script,
# 79 : 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
# 80 : 2 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
# 81 : :
# 82 : 2 : CKey child_key;
# 83 : 2 : child_key.MakeNewKey(true);
# 84 : 2 : CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
# 85 : 2 : auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
# 86 : 2 : /*input_height=*/101, /*input_signing_key=*/parent_key,
# 87 : 2 : /*output_destination=*/child_locking_script,
# 88 : 2 : /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
# 89 : 2 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
# 90 : 2 : const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {tx_parent, tx_child}, /*test_accept=*/true);
# 91 : 2 : BOOST_CHECK_MESSAGE(result_parent_child.m_state.IsValid(),
# 92 : 2 : "Package validation unexpectedly failed: " << result_parent_child.m_state.GetRejectReason());
# 93 : 2 : auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
# 94 : 2 : auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
# 95 : 2 : BOOST_CHECK(it_parent != result_parent_child.m_tx_results.end());
# 96 : 2 : BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(),
# 97 : 2 : "Package validation unexpectedly failed: " << it_parent->second.m_state.GetRejectReason());
# 98 : 2 : BOOST_CHECK(it_child != result_parent_child.m_tx_results.end());
# 99 : 2 : BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(),
# 100 : 2 : "Package validation unexpectedly failed: " << it_child->second.m_state.GetRejectReason());
# 101 : 2 : BOOST_CHECK(result_parent_child.m_package_feerate.has_value());
# 102 : 2 : BOOST_CHECK(result_parent_child.m_package_feerate.value() ==
# 103 : 2 : CFeeRate(2 * COIN, GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child)));
# 104 : :
# 105 : : // A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
# 106 : 2 : CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
# 107 : 2 : BOOST_CHECK(GetVirtualTransactionSize(*giant_ptx) > MAX_PACKAGE_SIZE * 1000);
# 108 : 2 : auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {giant_ptx}, /*test_accept=*/true);
# 109 : 2 : BOOST_CHECK(result_single_large.m_state.IsInvalid());
# 110 : 2 : BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
# 111 : 2 : BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
# 112 : 2 : auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
# 113 : 2 : BOOST_CHECK(it_giant_tx != result_single_large.m_tx_results.end());
# 114 : 2 : BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
# 115 : 2 : BOOST_CHECK(result_single_large.m_package_feerate == std::nullopt);
# 116 : :
# 117 : : // Check that mempool size hasn't changed.
# 118 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
# 119 : 2 : }
# 120 : :
# 121 : : BOOST_FIXTURE_TEST_CASE(noncontextual_package_tests, TestChain100Setup)
# 122 : 2 : {
# 123 : : // The signatures won't be verified so we can just use a placeholder
# 124 : 2 : CKey placeholder_key;
# 125 : 2 : placeholder_key.MakeNewKey(true);
# 126 : 2 : CScript spk = GetScriptForDestination(PKHash(placeholder_key.GetPubKey()));
# 127 : 2 : CKey placeholder_key_2;
# 128 : 2 : placeholder_key_2.MakeNewKey(true);
# 129 : 2 : CScript spk2 = GetScriptForDestination(PKHash(placeholder_key_2.GetPubKey()));
# 130 : :
# 131 : : // Parent and Child Package
# 132 : 2 : {
# 133 : 2 : auto mtx_parent = CreateValidMempoolTransaction(m_coinbase_txns[0], 0, 0, coinbaseKey, spk,
# 134 : 2 : CAmount(49 * COIN), /*submit=*/false);
# 135 : 2 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
# 136 : :
# 137 : 2 : auto mtx_child = CreateValidMempoolTransaction(tx_parent, 0, 101, placeholder_key, spk2,
# 138 : 2 : CAmount(48 * COIN), /*submit=*/false);
# 139 : 2 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
# 140 : :
# 141 : 2 : PackageValidationState state;
# 142 : 2 : BOOST_CHECK(CheckPackage({tx_parent, tx_child}, state));
# 143 : 2 : BOOST_CHECK(!CheckPackage({tx_child, tx_parent}, state));
# 144 : 2 : BOOST_CHECK_EQUAL(state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 145 : 2 : BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
# 146 : 2 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
# 147 : 2 : }
# 148 : :
# 149 : : // 24 Parents and 1 Child
# 150 : 2 : {
# 151 : 2 : Package package;
# 152 : 2 : CMutableTransaction child;
# 153 [ + + ]: 50 : for (int i{0}; i < 24; ++i) {
# 154 : 48 : auto parent = MakeTransactionRef(CreateValidMempoolTransaction(m_coinbase_txns[i + 1],
# 155 : 48 : 0, 0, coinbaseKey, spk, CAmount(48 * COIN), false));
# 156 : 48 : package.emplace_back(parent);
# 157 : 48 : child.vin.push_back(CTxIn(COutPoint(parent->GetHash(), 0)));
# 158 : 48 : }
# 159 : 2 : child.vout.push_back(CTxOut(47 * COIN, spk2));
# 160 : :
# 161 : : // The child must be in the package.
# 162 : 2 : BOOST_CHECK(!IsChildWithParents(package));
# 163 : :
# 164 : : // The parents can be in any order.
# 165 : 2 : FastRandomContext rng;
# 166 : 2 : Shuffle(package.begin(), package.end(), rng);
# 167 : 2 : package.push_back(MakeTransactionRef(child));
# 168 : :
# 169 : 2 : PackageValidationState state;
# 170 : 2 : BOOST_CHECK(CheckPackage(package, state));
# 171 : 2 : BOOST_CHECK(IsChildWithParents(package));
# 172 : :
# 173 : 2 : package.erase(package.begin());
# 174 : 2 : BOOST_CHECK(IsChildWithParents(package));
# 175 : :
# 176 : : // The package cannot have unrelated transactions.
# 177 : 2 : package.insert(package.begin(), m_coinbase_txns[0]);
# 178 : 2 : BOOST_CHECK(!IsChildWithParents(package));
# 179 : 2 : }
# 180 : :
# 181 : : // 2 Parents and 1 Child where one parent depends on the other.
# 182 : 2 : {
# 183 : 2 : CMutableTransaction mtx_parent;
# 184 : 2 : mtx_parent.vin.push_back(CTxIn(COutPoint(m_coinbase_txns[0]->GetHash(), 0)));
# 185 : 2 : mtx_parent.vout.push_back(CTxOut(20 * COIN, spk));
# 186 : 2 : mtx_parent.vout.push_back(CTxOut(20 * COIN, spk2));
# 187 : 2 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
# 188 : :
# 189 : 2 : CMutableTransaction mtx_parent_also_child;
# 190 : 2 : mtx_parent_also_child.vin.push_back(CTxIn(COutPoint(tx_parent->GetHash(), 0)));
# 191 : 2 : mtx_parent_also_child.vout.push_back(CTxOut(20 * COIN, spk));
# 192 : 2 : CTransactionRef tx_parent_also_child = MakeTransactionRef(mtx_parent_also_child);
# 193 : :
# 194 : 2 : CMutableTransaction mtx_child;
# 195 : 2 : mtx_child.vin.push_back(CTxIn(COutPoint(tx_parent->GetHash(), 1)));
# 196 : 2 : mtx_child.vin.push_back(CTxIn(COutPoint(tx_parent_also_child->GetHash(), 0)));
# 197 : 2 : mtx_child.vout.push_back(CTxOut(39 * COIN, spk));
# 198 : 2 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
# 199 : :
# 200 : 2 : PackageValidationState state;
# 201 : 2 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child}));
# 202 : 2 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
# 203 : 2 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child, tx_child}));
# 204 : : // IsChildWithParents does not detect unsorted parents.
# 205 : 2 : BOOST_CHECK(IsChildWithParents({tx_parent_also_child, tx_parent, tx_child}));
# 206 : 2 : BOOST_CHECK(CheckPackage({tx_parent, tx_parent_also_child, tx_child}, state));
# 207 : 2 : BOOST_CHECK(!CheckPackage({tx_parent_also_child, tx_parent, tx_child}, state));
# 208 : 2 : BOOST_CHECK_EQUAL(state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 209 : 2 : BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
# 210 : 2 : }
# 211 : 2 : }
# 212 : :
# 213 : : BOOST_FIXTURE_TEST_CASE(package_submission_tests, TestChain100Setup)
# 214 : 2 : {
# 215 : 2 : LOCK(cs_main);
# 216 : 2 : unsigned int expected_pool_size = m_node.mempool->size();
# 217 : 2 : CKey parent_key;
# 218 : 2 : parent_key.MakeNewKey(true);
# 219 : 2 : CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
# 220 : :
# 221 : : // Unrelated transactions are not allowed in package submission.
# 222 : 2 : Package package_unrelated;
# 223 [ + + ]: 22 : for (size_t i{0}; i < 10; ++i) {
# 224 : 20 : auto mtx = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[i + 25], /*input_vout=*/0,
# 225 : 20 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 226 : 20 : /*output_destination=*/parent_locking_script,
# 227 : 20 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
# 228 : 20 : package_unrelated.emplace_back(MakeTransactionRef(mtx));
# 229 : 20 : }
# 230 : 2 : auto result_unrelated_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 231 : 2 : package_unrelated, /*test_accept=*/false);
# 232 : 2 : BOOST_CHECK(result_unrelated_submit.m_state.IsInvalid());
# 233 : 2 : BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 234 : 2 : BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
# 235 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 236 : 2 : BOOST_CHECK(result_unrelated_submit.m_package_feerate == std::nullopt);
# 237 : :
# 238 : : // Parent and Child (and Grandchild) Package
# 239 : 2 : Package package_parent_child;
# 240 : 2 : Package package_3gen;
# 241 : 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
# 242 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 243 : 2 : /*output_destination=*/parent_locking_script,
# 244 : 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
# 245 : 2 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
# 246 : 2 : package_parent_child.push_back(tx_parent);
# 247 : 2 : package_3gen.push_back(tx_parent);
# 248 : :
# 249 : 2 : CKey child_key;
# 250 : 2 : child_key.MakeNewKey(true);
# 251 : 2 : CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
# 252 : 2 : auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
# 253 : 2 : /*input_height=*/101, /*input_signing_key=*/parent_key,
# 254 : 2 : /*output_destination=*/child_locking_script,
# 255 : 2 : /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
# 256 : 2 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
# 257 : 2 : package_parent_child.push_back(tx_child);
# 258 : 2 : package_3gen.push_back(tx_child);
# 259 : :
# 260 : 2 : CKey grandchild_key;
# 261 : 2 : grandchild_key.MakeNewKey(true);
# 262 : 2 : CScript grandchild_locking_script = GetScriptForDestination(PKHash(grandchild_key.GetPubKey()));
# 263 : 2 : auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/tx_child, /*input_vout=*/0,
# 264 : 2 : /*input_height=*/101, /*input_signing_key=*/child_key,
# 265 : 2 : /*output_destination=*/grandchild_locking_script,
# 266 : 2 : /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
# 267 : 2 : CTransactionRef tx_grandchild = MakeTransactionRef(mtx_grandchild);
# 268 : 2 : package_3gen.push_back(tx_grandchild);
# 269 : :
# 270 : : // 3 Generations is not allowed.
# 271 : 2 : {
# 272 : 2 : auto result_3gen_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 273 : 2 : package_3gen, /*test_accept=*/false);
# 274 : 2 : BOOST_CHECK(result_3gen_submit.m_state.IsInvalid());
# 275 : 2 : BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 276 : 2 : BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
# 277 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 278 : 2 : BOOST_CHECK(result_3gen_submit.m_package_feerate == std::nullopt);
# 279 : 2 : }
# 280 : :
# 281 : : // Child with missing parent.
# 282 : 2 : mtx_child.vin.push_back(CTxIn(COutPoint(package_unrelated[0]->GetHash(), 0)));
# 283 : 2 : Package package_missing_parent;
# 284 : 2 : package_missing_parent.push_back(tx_parent);
# 285 : 2 : package_missing_parent.push_back(MakeTransactionRef(mtx_child));
# 286 : 2 : {
# 287 : 2 : const auto result_missing_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 288 : 2 : package_missing_parent, /*test_accept=*/false);
# 289 : 2 : BOOST_CHECK(result_missing_parent.m_state.IsInvalid());
# 290 : 2 : BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 291 : 2 : BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetRejectReason(), "package-not-child-with-unconfirmed-parents");
# 292 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 293 : :
# 294 : 2 : BOOST_CHECK(result_missing_parent.m_package_feerate == std::nullopt);
# 295 : 2 : }
# 296 : :
# 297 : : // Submit package with parent + child.
# 298 : 2 : {
# 299 : 2 : const auto submit_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 300 : 2 : package_parent_child, /*test_accept=*/false);
# 301 : 2 : expected_pool_size += 2;
# 302 : 2 : BOOST_CHECK_MESSAGE(submit_parent_child.m_state.IsValid(),
# 303 : 2 : "Package validation unexpectedly failed: " << submit_parent_child.m_state.GetRejectReason());
# 304 : 2 : auto it_parent = submit_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
# 305 : 2 : auto it_child = submit_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
# 306 : 2 : BOOST_CHECK(it_parent != submit_parent_child.m_tx_results.end());
# 307 : 2 : BOOST_CHECK(it_parent->second.m_state.IsValid());
# 308 : 2 : BOOST_CHECK(it_child != submit_parent_child.m_tx_results.end());
# 309 : 2 : BOOST_CHECK(it_child->second.m_state.IsValid());
# 310 : :
# 311 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 312 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
# 313 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
# 314 : :
# 315 : : // Since both transactions have high feerates, they each passed validation individually.
# 316 : : // Package validation was unnecessary, so there is no package feerate.
# 317 : 2 : BOOST_CHECK(submit_parent_child.m_package_feerate == std::nullopt);
# 318 : 2 : }
# 319 : :
# 320 : : // Already-in-mempool transactions should be detected and de-duplicated.
# 321 : 2 : {
# 322 : 2 : const auto submit_deduped = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 323 : 2 : package_parent_child, /*test_accept=*/false);
# 324 : 2 : BOOST_CHECK_MESSAGE(submit_deduped.m_state.IsValid(),
# 325 : 2 : "Package validation unexpectedly failed: " << submit_deduped.m_state.GetRejectReason());
# 326 : 2 : auto it_parent_deduped = submit_deduped.m_tx_results.find(tx_parent->GetWitnessHash());
# 327 : 2 : auto it_child_deduped = submit_deduped.m_tx_results.find(tx_child->GetWitnessHash());
# 328 : 2 : BOOST_CHECK(it_parent_deduped != submit_deduped.m_tx_results.end());
# 329 : 2 : BOOST_CHECK(it_parent_deduped->second.m_state.IsValid());
# 330 : 2 : BOOST_CHECK(it_parent_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
# 331 : 2 : BOOST_CHECK(it_child_deduped != submit_deduped.m_tx_results.end());
# 332 : 2 : BOOST_CHECK(it_child_deduped->second.m_state.IsValid());
# 333 : 2 : BOOST_CHECK(it_child_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
# 334 : :
# 335 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 336 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
# 337 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
# 338 : :
# 339 : 2 : BOOST_CHECK(submit_deduped.m_package_feerate == std::nullopt);
# 340 : 2 : }
# 341 : 2 : }
# 342 : :
# 343 : : // Tests for packages containing transactions that have same-txid-different-witness equivalents in
# 344 : : // the mempool.
# 345 : : BOOST_FIXTURE_TEST_CASE(package_witness_swap_tests, TestChain100Setup)
# 346 : 2 : {
# 347 : : // Mine blocks to mature coinbases.
# 348 : 2 : mineBlocks(5);
# 349 : 2 : LOCK(cs_main);
# 350 : :
# 351 : : // Transactions with a same-txid-different-witness transaction in the mempool should be ignored,
# 352 : : // and the mempool entry's wtxid returned.
# 353 : 2 : CScript witnessScript = CScript() << OP_DROP << OP_TRUE;
# 354 : 2 : CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
# 355 : 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
# 356 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 357 : 2 : /*output_destination=*/scriptPubKey,
# 358 : 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
# 359 : 2 : CTransactionRef ptx_parent = MakeTransactionRef(mtx_parent);
# 360 : :
# 361 : : // Make two children with the same txid but different witnesses.
# 362 : 2 : CScriptWitness witness1;
# 363 : 2 : witness1.stack.push_back(std::vector<unsigned char>(1));
# 364 : 2 : witness1.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
# 365 : :
# 366 : 2 : CScriptWitness witness2(witness1);
# 367 : 2 : witness2.stack.push_back(std::vector<unsigned char>(2));
# 368 : 2 : witness2.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
# 369 : :
# 370 : 2 : CKey child_key;
# 371 : 2 : child_key.MakeNewKey(true);
# 372 : 2 : CScript child_locking_script = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
# 373 : 2 : CMutableTransaction mtx_child1;
# 374 : 2 : mtx_child1.nVersion = 1;
# 375 : 2 : mtx_child1.vin.resize(1);
# 376 : 2 : mtx_child1.vin[0].prevout.hash = ptx_parent->GetHash();
# 377 : 2 : mtx_child1.vin[0].prevout.n = 0;
# 378 : 2 : mtx_child1.vin[0].scriptSig = CScript();
# 379 : 2 : mtx_child1.vin[0].scriptWitness = witness1;
# 380 : 2 : mtx_child1.vout.resize(1);
# 381 : 2 : mtx_child1.vout[0].nValue = CAmount(48 * COIN);
# 382 : 2 : mtx_child1.vout[0].scriptPubKey = child_locking_script;
# 383 : :
# 384 : 2 : CMutableTransaction mtx_child2{mtx_child1};
# 385 : 2 : mtx_child2.vin[0].scriptWitness = witness2;
# 386 : :
# 387 : 2 : CTransactionRef ptx_child1 = MakeTransactionRef(mtx_child1);
# 388 : 2 : CTransactionRef ptx_child2 = MakeTransactionRef(mtx_child2);
# 389 : :
# 390 : : // child1 and child2 have the same txid
# 391 : 2 : BOOST_CHECK_EQUAL(ptx_child1->GetHash(), ptx_child2->GetHash());
# 392 : : // child1 and child2 have different wtxids
# 393 : 2 : BOOST_CHECK(ptx_child1->GetWitnessHash() != ptx_child2->GetWitnessHash());
# 394 : :
# 395 : : // Try submitting Package1{parent, child1} and Package2{parent, child2} where the children are
# 396 : : // same-txid-different-witness.
# 397 : 2 : {
# 398 : 2 : const auto submit_witness1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 399 : 2 : {ptx_parent, ptx_child1}, /*test_accept=*/false);
# 400 : 2 : BOOST_CHECK_MESSAGE(submit_witness1.m_state.IsValid(),
# 401 : 2 : "Package validation unexpectedly failed: " << submit_witness1.m_state.GetRejectReason());
# 402 : 2 : auto it_parent1 = submit_witness1.m_tx_results.find(ptx_parent->GetWitnessHash());
# 403 : 2 : auto it_child1 = submit_witness1.m_tx_results.find(ptx_child1->GetWitnessHash());
# 404 : 2 : BOOST_CHECK(it_parent1 != submit_witness1.m_tx_results.end());
# 405 : 2 : BOOST_CHECK_MESSAGE(it_parent1->second.m_state.IsValid(),
# 406 : 2 : "Transaction unexpectedly failed: " << it_parent1->second.m_state.GetRejectReason());
# 407 : 2 : BOOST_CHECK(it_child1 != submit_witness1.m_tx_results.end());
# 408 : 2 : BOOST_CHECK_MESSAGE(it_child1->second.m_state.IsValid(),
# 409 : 2 : "Transaction unexpectedly failed: " << it_child1->second.m_state.GetRejectReason());
# 410 : :
# 411 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent->GetHash())));
# 412 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_child1->GetHash())));
# 413 : :
# 414 : : // Child2 would have been validated individually.
# 415 : 2 : BOOST_CHECK(submit_witness1.m_package_feerate == std::nullopt);
# 416 : :
# 417 : 2 : const auto submit_witness2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 418 : 2 : {ptx_parent, ptx_child2}, /*test_accept=*/false);
# 419 : 2 : BOOST_CHECK(submit_witness2.m_package_feerate == std::nullopt);
# 420 : 2 : BOOST_CHECK_MESSAGE(submit_witness2.m_state.IsValid(),
# 421 : 2 : "Package validation unexpectedly failed: " << submit_witness2.m_state.GetRejectReason());
# 422 : 2 : auto it_parent2_deduped = submit_witness2.m_tx_results.find(ptx_parent->GetWitnessHash());
# 423 : 2 : auto it_child2 = submit_witness2.m_tx_results.find(ptx_child2->GetWitnessHash());
# 424 : 2 : BOOST_CHECK(it_parent2_deduped != submit_witness2.m_tx_results.end());
# 425 : 2 : BOOST_CHECK(it_parent2_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
# 426 : 2 : BOOST_CHECK(it_child2 != submit_witness2.m_tx_results.end());
# 427 : 2 : BOOST_CHECK(it_child2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
# 428 : 2 : BOOST_CHECK_EQUAL(ptx_child1->GetWitnessHash(), it_child2->second.m_other_wtxid.value());
# 429 : :
# 430 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_child2->GetHash())));
# 431 : 2 : BOOST_CHECK(!m_node.mempool->exists(GenTxid::Wtxid(ptx_child2->GetWitnessHash())));
# 432 : :
# 433 : : // Deduplication should work when wtxid != txid. Submit package with the already-in-mempool
# 434 : : // transactions again, which should not fail.
# 435 : 2 : const auto submit_segwit_dedup = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 436 : 2 : {ptx_parent, ptx_child1}, /*test_accept=*/false);
# 437 : 2 : BOOST_CHECK_MESSAGE(submit_segwit_dedup.m_state.IsValid(),
# 438 : 2 : "Package validation unexpectedly failed: " << submit_segwit_dedup.m_state.GetRejectReason());
# 439 : 2 : auto it_parent_dup = submit_segwit_dedup.m_tx_results.find(ptx_parent->GetWitnessHash());
# 440 : 2 : auto it_child_dup = submit_segwit_dedup.m_tx_results.find(ptx_child1->GetWitnessHash());
# 441 : 2 : BOOST_CHECK(it_parent_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
# 442 : 2 : BOOST_CHECK(it_child_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
# 443 : 2 : BOOST_CHECK(submit_witness2.m_package_feerate == std::nullopt);
# 444 : 2 : }
# 445 : :
# 446 : : // Try submitting Package1{child2, grandchild} where child2 is same-txid-different-witness as
# 447 : : // the in-mempool transaction, child1. Since child1 exists in the mempool and its outputs are
# 448 : : // available, child2 should be ignored and grandchild should be accepted.
# 449 : : //
# 450 : : // This tests a potential censorship vector in which an attacker broadcasts a competing package
# 451 : : // where a parent's witness is mutated. The honest package should be accepted despite the fact
# 452 : : // that we don't allow witness replacement.
# 453 : 2 : CKey grandchild_key;
# 454 : 2 : grandchild_key.MakeNewKey(true);
# 455 : 2 : CScript grandchild_locking_script = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
# 456 : 2 : auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/ptx_child2, /*input_vout=*/0,
# 457 : 2 : /*input_height=*/0, /*input_signing_key=*/child_key,
# 458 : 2 : /*output_destination=*/grandchild_locking_script,
# 459 : 2 : /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
# 460 : 2 : CTransactionRef ptx_grandchild = MakeTransactionRef(mtx_grandchild);
# 461 : :
# 462 : : // We already submitted child1 above.
# 463 : 2 : {
# 464 : 2 : const auto submit_spend_ignored = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 465 : 2 : {ptx_child2, ptx_grandchild}, /*test_accept=*/false);
# 466 : 2 : BOOST_CHECK_MESSAGE(submit_spend_ignored.m_state.IsValid(),
# 467 : 2 : "Package validation unexpectedly failed: " << submit_spend_ignored.m_state.GetRejectReason());
# 468 : 2 : auto it_child2_ignored = submit_spend_ignored.m_tx_results.find(ptx_child2->GetWitnessHash());
# 469 : 2 : auto it_grandchild = submit_spend_ignored.m_tx_results.find(ptx_grandchild->GetWitnessHash());
# 470 : 2 : BOOST_CHECK(it_child2_ignored != submit_spend_ignored.m_tx_results.end());
# 471 : 2 : BOOST_CHECK(it_child2_ignored->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
# 472 : 2 : BOOST_CHECK(it_grandchild != submit_spend_ignored.m_tx_results.end());
# 473 : 2 : BOOST_CHECK(it_grandchild->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 474 : :
# 475 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_child2->GetHash())));
# 476 : 2 : BOOST_CHECK(!m_node.mempool->exists(GenTxid::Wtxid(ptx_child2->GetWitnessHash())));
# 477 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Wtxid(ptx_grandchild->GetWitnessHash())));
# 478 : :
# 479 : : // Since child2 is ignored, grandchild would be validated individually.
# 480 : 2 : BOOST_CHECK(submit_spend_ignored.m_package_feerate == std::nullopt);
# 481 : 2 : }
# 482 : :
# 483 : : // A package Package{parent1, parent2, parent3, child} where the parents are a mixture of
# 484 : : // identical-tx-in-mempool, same-txid-different-witness-in-mempool, and new transactions.
# 485 : 2 : Package package_mixed;
# 486 : :
# 487 : : // Give all the parents anyone-can-spend scripts so we don't have to deal with signing the child.
# 488 : 2 : CScript acs_script = CScript() << OP_TRUE;
# 489 : 2 : CScript acs_spk = GetScriptForDestination(WitnessV0ScriptHash(acs_script));
# 490 : 2 : CScriptWitness acs_witness;
# 491 : 2 : acs_witness.stack.push_back(std::vector<unsigned char>(acs_script.begin(), acs_script.end()));
# 492 : :
# 493 : : // parent1 will already be in the mempool
# 494 : 2 : auto mtx_parent1 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
# 495 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 496 : 2 : /*output_destination=*/acs_spk,
# 497 : 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
# 498 : 2 : CTransactionRef ptx_parent1 = MakeTransactionRef(mtx_parent1);
# 499 : 2 : package_mixed.push_back(ptx_parent1);
# 500 : :
# 501 : : // parent2 will have a same-txid-different-witness tx already in the mempool
# 502 : 2 : CScript grandparent2_script = CScript() << OP_DROP << OP_TRUE;
# 503 : 2 : CScript grandparent2_spk = GetScriptForDestination(WitnessV0ScriptHash(grandparent2_script));
# 504 : 2 : CScriptWitness parent2_witness1;
# 505 : 2 : parent2_witness1.stack.push_back(std::vector<unsigned char>(1));
# 506 : 2 : parent2_witness1.stack.push_back(std::vector<unsigned char>(grandparent2_script.begin(), grandparent2_script.end()));
# 507 : 2 : CScriptWitness parent2_witness2;
# 508 : 2 : parent2_witness2.stack.push_back(std::vector<unsigned char>(2));
# 509 : 2 : parent2_witness2.stack.push_back(std::vector<unsigned char>(grandparent2_script.begin(), grandparent2_script.end()));
# 510 : :
# 511 : : // Create grandparent2 creating an output with multiple spending paths. Submit to mempool.
# 512 : 2 : auto mtx_grandparent2 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
# 513 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 514 : 2 : /*output_destination=*/grandparent2_spk,
# 515 : 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
# 516 : 2 : CTransactionRef ptx_grandparent2 = MakeTransactionRef(mtx_grandparent2);
# 517 : :
# 518 : 2 : CMutableTransaction mtx_parent2_v1;
# 519 : 2 : mtx_parent2_v1.nVersion = 1;
# 520 : 2 : mtx_parent2_v1.vin.resize(1);
# 521 : 2 : mtx_parent2_v1.vin[0].prevout.hash = ptx_grandparent2->GetHash();
# 522 : 2 : mtx_parent2_v1.vin[0].prevout.n = 0;
# 523 : 2 : mtx_parent2_v1.vin[0].scriptSig = CScript();
# 524 : 2 : mtx_parent2_v1.vin[0].scriptWitness = parent2_witness1;
# 525 : 2 : mtx_parent2_v1.vout.resize(1);
# 526 : 2 : mtx_parent2_v1.vout[0].nValue = CAmount(48 * COIN);
# 527 : 2 : mtx_parent2_v1.vout[0].scriptPubKey = acs_spk;
# 528 : :
# 529 : 2 : CMutableTransaction mtx_parent2_v2{mtx_parent2_v1};
# 530 : 2 : mtx_parent2_v2.vin[0].scriptWitness = parent2_witness2;
# 531 : :
# 532 : 2 : CTransactionRef ptx_parent2_v1 = MakeTransactionRef(mtx_parent2_v1);
# 533 : 2 : CTransactionRef ptx_parent2_v2 = MakeTransactionRef(mtx_parent2_v2);
# 534 : : // Put parent2_v1 in the package, submit parent2_v2 to the mempool.
# 535 : 2 : const MempoolAcceptResult parent2_v2_result = m_node.chainman->ProcessTransaction(ptx_parent2_v2);
# 536 : 2 : BOOST_CHECK(parent2_v2_result.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 537 : 2 : package_mixed.push_back(ptx_parent2_v1);
# 538 : :
# 539 : : // parent3 will be a new transaction. Put 0 fees on it to make it invalid on its own.
# 540 : 2 : auto mtx_parent3 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[3], /*input_vout=*/0,
# 541 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 542 : 2 : /*output_destination=*/acs_spk,
# 543 : 2 : /*output_amount=*/CAmount(50 * COIN), /*submit=*/false);
# 544 : 2 : CTransactionRef ptx_parent3 = MakeTransactionRef(mtx_parent3);
# 545 : 2 : package_mixed.push_back(ptx_parent3);
# 546 : :
# 547 : : // child spends parent1, parent2, and parent3
# 548 : 2 : CKey mixed_grandchild_key;
# 549 : 2 : mixed_grandchild_key.MakeNewKey(true);
# 550 : 2 : CScript mixed_child_spk = GetScriptForDestination(WitnessV0KeyHash(mixed_grandchild_key.GetPubKey()));
# 551 : :
# 552 : 2 : CMutableTransaction mtx_mixed_child;
# 553 : 2 : mtx_mixed_child.vin.push_back(CTxIn(COutPoint(ptx_parent1->GetHash(), 0)));
# 554 : 2 : mtx_mixed_child.vin.push_back(CTxIn(COutPoint(ptx_parent2_v1->GetHash(), 0)));
# 555 : 2 : mtx_mixed_child.vin.push_back(CTxIn(COutPoint(ptx_parent3->GetHash(), 0)));
# 556 : 2 : mtx_mixed_child.vin[0].scriptWitness = acs_witness;
# 557 : 2 : mtx_mixed_child.vin[1].scriptWitness = acs_witness;
# 558 : 2 : mtx_mixed_child.vin[2].scriptWitness = acs_witness;
# 559 : 2 : mtx_mixed_child.vout.push_back(CTxOut((48 + 49 + 50 - 1) * COIN, mixed_child_spk));
# 560 : 2 : CTransactionRef ptx_mixed_child = MakeTransactionRef(mtx_mixed_child);
# 561 : 2 : package_mixed.push_back(ptx_mixed_child);
# 562 : :
# 563 : : // Submit package:
# 564 : : // parent1 should be ignored
# 565 : : // parent2_v1 should be ignored (and v2 wtxid returned)
# 566 : : // parent3 should be accepted
# 567 : : // child should be accepted
# 568 : 2 : {
# 569 : 2 : const auto mixed_result = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_mixed, false);
# 570 : 2 : BOOST_CHECK_MESSAGE(mixed_result.m_state.IsValid(), mixed_result.m_state.GetRejectReason());
# 571 : 2 : auto it_parent1 = mixed_result.m_tx_results.find(ptx_parent1->GetWitnessHash());
# 572 : 2 : auto it_parent2 = mixed_result.m_tx_results.find(ptx_parent2_v1->GetWitnessHash());
# 573 : 2 : auto it_parent3 = mixed_result.m_tx_results.find(ptx_parent3->GetWitnessHash());
# 574 : 2 : auto it_child = mixed_result.m_tx_results.find(ptx_mixed_child->GetWitnessHash());
# 575 : 2 : BOOST_CHECK(it_parent1 != mixed_result.m_tx_results.end());
# 576 : 2 : BOOST_CHECK(it_parent2 != mixed_result.m_tx_results.end());
# 577 : 2 : BOOST_CHECK(it_parent3 != mixed_result.m_tx_results.end());
# 578 : 2 : BOOST_CHECK(it_child != mixed_result.m_tx_results.end());
# 579 : :
# 580 : 2 : BOOST_CHECK(it_parent1->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
# 581 : 2 : BOOST_CHECK(it_parent2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
# 582 : 2 : BOOST_CHECK(it_parent3->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 583 : 2 : BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 584 : 2 : BOOST_CHECK_EQUAL(ptx_parent2_v2->GetWitnessHash(), it_parent2->second.m_other_wtxid.value());
# 585 : :
# 586 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent1->GetHash())));
# 587 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent2_v1->GetHash())));
# 588 : 2 : BOOST_CHECK(!m_node.mempool->exists(GenTxid::Wtxid(ptx_parent2_v1->GetWitnessHash())));
# 589 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent3->GetHash())));
# 590 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_mixed_child->GetHash())));
# 591 : :
# 592 : : // package feerate should include parent3 and child. It should not include parent1 or parent2_v1.
# 593 : 2 : BOOST_CHECK(mixed_result.m_package_feerate.has_value());
# 594 : 2 : const CFeeRate expected_feerate(1 * COIN, GetVirtualTransactionSize(*ptx_parent3) + GetVirtualTransactionSize(*ptx_mixed_child));
# 595 : 2 : BOOST_CHECK_MESSAGE(mixed_result.m_package_feerate.value() == expected_feerate,
# 596 : 2 : strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
# 597 : 2 : mixed_result.m_package_feerate.value().ToString()));
# 598 : 2 : }
# 599 : 2 : }
# 600 : :
# 601 : : BOOST_FIXTURE_TEST_CASE(package_cpfp_tests, TestChain100Setup)
# 602 : 2 : {
# 603 : 2 : mineBlocks(5);
# 604 : 2 : LOCK(::cs_main);
# 605 : 2 : size_t expected_pool_size = m_node.mempool->size();
# 606 : 2 : CKey child_key;
# 607 : 2 : child_key.MakeNewKey(true);
# 608 : 2 : CScript parent_spk = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
# 609 : 2 : CKey grandchild_key;
# 610 : 2 : grandchild_key.MakeNewKey(true);
# 611 : 2 : CScript child_spk = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
# 612 : :
# 613 : : // zero-fee parent and high-fee child package
# 614 : 2 : const CAmount coinbase_value{50 * COIN};
# 615 : 2 : const CAmount parent_value{coinbase_value - 0};
# 616 : 2 : const CAmount child_value{parent_value - COIN};
# 617 : :
# 618 : 2 : Package package_cpfp;
# 619 : 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
# 620 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 621 : 2 : /*output_destination=*/parent_spk,
# 622 : 2 : /*output_amount=*/parent_value, /*submit=*/false);
# 623 : 2 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
# 624 : 2 : package_cpfp.push_back(tx_parent);
# 625 : :
# 626 : 2 : auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
# 627 : 2 : /*input_height=*/101, /*input_signing_key=*/child_key,
# 628 : 2 : /*output_destination=*/child_spk,
# 629 : 2 : /*output_amount=*/child_value, /*submit=*/false);
# 630 : 2 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
# 631 : 2 : package_cpfp.push_back(tx_child);
# 632 : :
# 633 : : // Package feerate is calculated using modified fees, and prioritisetransaction accepts negative
# 634 : : // fee deltas. This should be taken into account. De-prioritise the parent transaction by -1BTC,
# 635 : : // bringing the package feerate to 0.
# 636 : 2 : m_node.mempool->PrioritiseTransaction(tx_parent->GetHash(), -1 * COIN);
# 637 : 2 : {
# 638 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 639 : 2 : const auto submit_cpfp_deprio = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 640 : 2 : package_cpfp, /*test_accept=*/ false);
# 641 : 2 : BOOST_CHECK_MESSAGE(submit_cpfp_deprio.m_state.IsInvalid(),
# 642 : 2 : "Package validation unexpectedly succeeded: " << submit_cpfp_deprio.m_state.GetRejectReason());
# 643 : 2 : BOOST_CHECK(submit_cpfp_deprio.m_tx_results.empty());
# 644 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 645 : 2 : const CFeeRate expected_feerate(0, GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child));
# 646 : 2 : BOOST_CHECK(submit_cpfp_deprio.m_package_feerate.has_value());
# 647 : 2 : BOOST_CHECK(submit_cpfp_deprio.m_package_feerate.value() == CFeeRate{0});
# 648 : 2 : BOOST_CHECK_MESSAGE(submit_cpfp_deprio.m_package_feerate.value() == expected_feerate,
# 649 : 2 : strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
# 650 : 2 : submit_cpfp_deprio.m_package_feerate.value().ToString()));
# 651 : 2 : }
# 652 : :
# 653 : : // Clear the prioritisation of the parent transaction.
# 654 : 2 : WITH_LOCK(m_node.mempool->cs, m_node.mempool->ClearPrioritisation(tx_parent->GetHash()));
# 655 : :
# 656 : : // Package CPFP: Even though the parent pays 0 absolute fees, the child pays 1 BTC which is
# 657 : : // enough for the package feerate to meet the threshold.
# 658 : 2 : {
# 659 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 660 : 2 : const auto submit_cpfp = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 661 : 2 : package_cpfp, /*test_accept=*/ false);
# 662 : 2 : expected_pool_size += 2;
# 663 : 2 : BOOST_CHECK_MESSAGE(submit_cpfp.m_state.IsValid(),
# 664 : 2 : "Package validation unexpectedly failed: " << submit_cpfp.m_state.GetRejectReason());
# 665 : 2 : auto it_parent = submit_cpfp.m_tx_results.find(tx_parent->GetWitnessHash());
# 666 : 2 : auto it_child = submit_cpfp.m_tx_results.find(tx_child->GetWitnessHash());
# 667 : 2 : BOOST_CHECK(it_parent != submit_cpfp.m_tx_results.end());
# 668 : 2 : BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 669 : 2 : BOOST_CHECK(it_parent->second.m_base_fees.value() == 0);
# 670 : 2 : BOOST_CHECK(it_child != submit_cpfp.m_tx_results.end());
# 671 : 2 : BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 672 : 2 : BOOST_CHECK(it_child->second.m_base_fees.value() == COIN);
# 673 : :
# 674 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 675 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
# 676 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
# 677 : :
# 678 : 2 : const CFeeRate expected_feerate(coinbase_value - child_value,
# 679 : 2 : GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child));
# 680 : 2 : BOOST_CHECK(expected_feerate.GetFeePerK() > 1000);
# 681 : 2 : BOOST_CHECK(submit_cpfp.m_package_feerate.has_value());
# 682 : 2 : BOOST_CHECK_MESSAGE(submit_cpfp.m_package_feerate.value() == expected_feerate,
# 683 : 2 : strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
# 684 : 2 : submit_cpfp.m_package_feerate.value().ToString()));
# 685 : 2 : }
# 686 : :
# 687 : : // Just because we allow low-fee parents doesn't mean we allow low-feerate packages.
# 688 : : // This package just pays 200 satoshis total. This would be enough to pay for the child alone,
# 689 : : // but isn't enough for the entire package to meet the 1sat/vbyte minimum.
# 690 : 2 : Package package_still_too_low;
# 691 : 2 : auto mtx_parent_cheap = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
# 692 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 693 : 2 : /*output_destination=*/parent_spk,
# 694 : 2 : /*output_amount=*/coinbase_value, /*submit=*/false);
# 695 : 2 : CTransactionRef tx_parent_cheap = MakeTransactionRef(mtx_parent_cheap);
# 696 : 2 : package_still_too_low.push_back(tx_parent_cheap);
# 697 : :
# 698 : 2 : auto mtx_child_cheap = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_cheap, /*input_vout=*/0,
# 699 : 2 : /*input_height=*/101, /*input_signing_key=*/child_key,
# 700 : 2 : /*output_destination=*/child_spk,
# 701 : 2 : /*output_amount=*/coinbase_value - 200, /*submit=*/false);
# 702 : 2 : CTransactionRef tx_child_cheap = MakeTransactionRef(mtx_child_cheap);
# 703 : 2 : package_still_too_low.push_back(tx_child_cheap);
# 704 : :
# 705 : : // Cheap package should fail with package-fee-too-low.
# 706 : 2 : {
# 707 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 708 : 2 : const auto submit_package_too_low = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 709 : 2 : package_still_too_low, /*test_accept=*/false);
# 710 : 2 : BOOST_CHECK_MESSAGE(submit_package_too_low.m_state.IsInvalid(), "Package validation unexpectedly succeeded");
# 711 : 2 : BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 712 : 2 : BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetRejectReason(), "package-fee-too-low");
# 713 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 714 : 2 : const CFeeRate child_feerate(200, GetVirtualTransactionSize(*tx_child_cheap));
# 715 : 2 : BOOST_CHECK(child_feerate.GetFeePerK() > 1000);
# 716 : 2 : const CFeeRate expected_feerate(200,
# 717 : 2 : GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap));
# 718 : 2 : BOOST_CHECK(expected_feerate.GetFeePerK() < 1000);
# 719 : 2 : BOOST_CHECK(submit_package_too_low.m_package_feerate.has_value());
# 720 : 2 : BOOST_CHECK_MESSAGE(submit_package_too_low.m_package_feerate.value() == expected_feerate,
# 721 : 2 : strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
# 722 : 2 : submit_package_too_low.m_package_feerate.value().ToString()));
# 723 : 2 : }
# 724 : :
# 725 : : // Package feerate includes the modified fees of the transactions.
# 726 : : // This means a child with its fee delta from prioritisetransaction can pay for a parent.
# 727 : 2 : m_node.mempool->PrioritiseTransaction(tx_child_cheap->GetHash(), 1 * COIN);
# 728 : : // Now that the child's fees have "increased" by 1 BTC, the cheap package should succeed.
# 729 : 2 : {
# 730 : 2 : const auto submit_prioritised_package = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 731 : 2 : package_still_too_low, /*test_accept=*/false);
# 732 : 2 : expected_pool_size += 2;
# 733 : 2 : BOOST_CHECK_MESSAGE(submit_prioritised_package.m_state.IsValid(),
# 734 : 2 : "Package validation unexpectedly failed" << submit_prioritised_package.m_state.GetRejectReason());
# 735 : 2 : const CFeeRate expected_feerate(1 * COIN + 200,
# 736 : 2 : GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap));
# 737 : 2 : BOOST_CHECK(submit_prioritised_package.m_package_feerate.has_value());
# 738 : 2 : BOOST_CHECK_MESSAGE(submit_prioritised_package.m_package_feerate.value() == expected_feerate,
# 739 : 2 : strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
# 740 : 2 : submit_prioritised_package.m_package_feerate.value().ToString()));
# 741 : 2 : }
# 742 : :
# 743 : : // Package feerate is calculated without topology in mind; it's just aggregating fees and sizes.
# 744 : : // However, this should not allow parents to pay for children. Each transaction should be
# 745 : : // validated individually first, eliminating sufficient-feerate parents before they are unfairly
# 746 : : // included in the package feerate. It's also important that the low-fee child doesn't prevent
# 747 : : // the parent from being accepted.
# 748 : 2 : Package package_rich_parent;
# 749 : 2 : const CAmount high_parent_fee{1 * COIN};
# 750 : 2 : auto mtx_parent_rich = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
# 751 : 2 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
# 752 : 2 : /*output_destination=*/parent_spk,
# 753 : 2 : /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
# 754 : 2 : CTransactionRef tx_parent_rich = MakeTransactionRef(mtx_parent_rich);
# 755 : 2 : package_rich_parent.push_back(tx_parent_rich);
# 756 : :
# 757 : 2 : auto mtx_child_poor = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_rich, /*input_vout=*/0,
# 758 : 2 : /*input_height=*/101, /*input_signing_key=*/child_key,
# 759 : 2 : /*output_destination=*/child_spk,
# 760 : 2 : /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
# 761 : 2 : CTransactionRef tx_child_poor = MakeTransactionRef(mtx_child_poor);
# 762 : 2 : package_rich_parent.push_back(tx_child_poor);
# 763 : :
# 764 : : // Parent pays 1 BTC and child pays none. The parent should be accepted without the child.
# 765 : 2 : {
# 766 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 767 : 2 : const auto submit_rich_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
# 768 : 2 : package_rich_parent, /*test_accept=*/false);
# 769 : 2 : expected_pool_size += 1;
# 770 : 2 : BOOST_CHECK_MESSAGE(submit_rich_parent.m_state.IsInvalid(), "Package validation unexpectedly succeeded");
# 771 : :
# 772 : : // The child would have been validated on its own and failed, then submitted as a "package" of 1.
# 773 : : // The package feerate is just the child's feerate, which is 0sat/vb.
# 774 : 2 : BOOST_CHECK(submit_rich_parent.m_package_feerate.has_value());
# 775 : 2 : BOOST_CHECK_MESSAGE(submit_rich_parent.m_package_feerate.value() == CFeeRate(),
# 776 : 2 : "expected 0, got " << submit_rich_parent.m_package_feerate.value().ToString());
# 777 : 2 : BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 778 : 2 : BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetRejectReason(), "package-fee-too-low");
# 779 : :
# 780 : 2 : auto it_parent = submit_rich_parent.m_tx_results.find(tx_parent_rich->GetWitnessHash());
# 781 : 2 : BOOST_CHECK(it_parent != submit_rich_parent.m_tx_results.end());
# 782 : 2 : BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
# 783 : 2 : BOOST_CHECK(it_parent->second.m_state.GetRejectReason() == "");
# 784 : 2 : BOOST_CHECK_MESSAGE(it_parent->second.m_base_fees.value() == high_parent_fee,
# 785 : 2 : strprintf("rich parent: expected fee %s, got %s", high_parent_fee, it_parent->second.m_base_fees.value()));
# 786 : :
# 787 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
# 788 : 2 : BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent_rich->GetHash())));
# 789 : 2 : BOOST_CHECK(!m_node.mempool->exists(GenTxid::Txid(tx_child_poor->GetHash())));
# 790 : 2 : }
# 791 : 2 : }
# 792 : : BOOST_AUTO_TEST_SUITE_END()
|