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