Bitcoin Core  24.1.0
P2P Digital Currency
txpackage_tests.cpp
Go to the documentation of this file.
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>
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 {
23  mtx.vin.resize(num_inputs);
24  mtx.vout.resize(num_outputs);
25  auto random_script = CScript() << ToByteVector(InsecureRand256()) << ToByteVector(InsecureRand256());
26  for (size_t i{0}; i < num_inputs; ++i) {
27  mtx.vin[i].prevout.hash = InsecureRand256();
28  mtx.vin[i].prevout.n = 0;
29  mtx.vin[i].scriptSig = random_script;
30  }
31  for (size_t o{0}; o < num_outputs; ++o) {
32  mtx.vout[o].nValue = 1 * CENT;
33  mtx.vout[o].scriptPubKey = random_script;
34  }
35  return MakeTransactionRef(mtx);
36 }
37 
38 BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
39 {
40  // Packages can't have more than 25 transactions.
41  Package package_too_many;
42  package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
43  for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
44  package_too_many.emplace_back(create_placeholder_tx(1, 1));
45  }
46  PackageValidationState state_too_many;
47  BOOST_CHECK(!CheckPackage(package_too_many, state_too_many));
49  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  CTransactionRef large_ptx = create_placeholder_tx(150, 150);
53  Package package_too_large;
54  auto size_large = GetVirtualTransactionSize(*large_ptx);
55  size_t total_size{0};
56  while (total_size <= MAX_PACKAGE_SIZE * 1000) {
57  package_too_large.push_back(large_ptx);
58  total_size += size_large;
59  }
60  BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
61  PackageValidationState state_too_large;
62  BOOST_CHECK(!CheckPackage(package_too_large, state_too_large));
63  BOOST_CHECK_EQUAL(state_too_large.GetResult(), PackageValidationResult::PCKG_POLICY);
64  BOOST_CHECK_EQUAL(state_too_large.GetRejectReason(), "package-too-large");
65 }
66 
67 BOOST_FIXTURE_TEST_CASE(package_validation_tests, TestChain100Setup)
68 {
69  LOCK(cs_main);
70  unsigned int initialPoolSize = m_node.mempool->size();
71 
72  // Parent and Child Package
73  CKey parent_key;
74  parent_key.MakeNewKey(true);
75  CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
76  auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
77  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
78  /*output_destination=*/parent_locking_script,
79  /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
80  CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
81 
82  CKey child_key;
83  child_key.MakeNewKey(true);
84  CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
85  auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
86  /*input_height=*/101, /*input_signing_key=*/parent_key,
87  /*output_destination=*/child_locking_script,
88  /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
89  CTransactionRef tx_child = MakeTransactionRef(mtx_child);
90  const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {tx_parent, tx_child}, /*test_accept=*/true);
91  BOOST_CHECK_MESSAGE(result_parent_child.m_state.IsValid(),
92  "Package validation unexpectedly failed: " << result_parent_child.m_state.GetRejectReason());
93  auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
94  auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
95  BOOST_CHECK(it_parent != result_parent_child.m_tx_results.end());
96  BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(),
97  "Package validation unexpectedly failed: " << it_parent->second.m_state.GetRejectReason());
98  BOOST_CHECK(it_child != result_parent_child.m_tx_results.end());
99  BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(),
100  "Package validation unexpectedly failed: " << it_child->second.m_state.GetRejectReason());
101  BOOST_CHECK(result_parent_child.m_package_feerate.has_value());
102  BOOST_CHECK(result_parent_child.m_package_feerate.value() ==
103  CFeeRate(2 * COIN, GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child)));
104 
105  // A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
106  CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
108  auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {giant_ptx}, /*test_accept=*/true);
109  BOOST_CHECK(result_single_large.m_state.IsInvalid());
110  BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
111  BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
112  auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
113  BOOST_CHECK(it_giant_tx != result_single_large.m_tx_results.end());
114  BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
115  BOOST_CHECK(result_single_large.m_package_feerate == std::nullopt);
116 
117  // Check that mempool size hasn't changed.
118  BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
119 }
120 
121 BOOST_FIXTURE_TEST_CASE(noncontextual_package_tests, TestChain100Setup)
122 {
123  // The signatures won't be verified so we can just use a placeholder
124  CKey placeholder_key;
125  placeholder_key.MakeNewKey(true);
126  CScript spk = GetScriptForDestination(PKHash(placeholder_key.GetPubKey()));
127  CKey placeholder_key_2;
128  placeholder_key_2.MakeNewKey(true);
129  CScript spk2 = GetScriptForDestination(PKHash(placeholder_key_2.GetPubKey()));
130 
131  // Parent and Child Package
132  {
133  auto mtx_parent = CreateValidMempoolTransaction(m_coinbase_txns[0], 0, 0, coinbaseKey, spk,
134  CAmount(49 * COIN), /*submit=*/false);
135  CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
136 
137  auto mtx_child = CreateValidMempoolTransaction(tx_parent, 0, 101, placeholder_key, spk2,
138  CAmount(48 * COIN), /*submit=*/false);
139  CTransactionRef tx_child = MakeTransactionRef(mtx_child);
140 
142  BOOST_CHECK(CheckPackage({tx_parent, tx_child}, state));
143  BOOST_CHECK(!CheckPackage({tx_child, tx_parent}, state));
145  BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
146  BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
147  }
148 
149  // 24 Parents and 1 Child
150  {
151  Package package;
152  CMutableTransaction child;
153  for (int i{0}; i < 24; ++i) {
154  auto parent = MakeTransactionRef(CreateValidMempoolTransaction(m_coinbase_txns[i + 1],
155  0, 0, coinbaseKey, spk, CAmount(48 * COIN), false));
156  package.emplace_back(parent);
157  child.vin.push_back(CTxIn(COutPoint(parent->GetHash(), 0)));
158  }
159  child.vout.push_back(CTxOut(47 * COIN, spk2));
160 
161  // The child must be in the package.
162  BOOST_CHECK(!IsChildWithParents(package));
163 
164  // The parents can be in any order.
165  FastRandomContext rng;
166  Shuffle(package.begin(), package.end(), rng);
167  package.push_back(MakeTransactionRef(child));
168 
170  BOOST_CHECK(CheckPackage(package, state));
172 
173  package.erase(package.begin());
175 
176  // The package cannot have unrelated transactions.
177  package.insert(package.begin(), m_coinbase_txns[0]);
178  BOOST_CHECK(!IsChildWithParents(package));
179  }
180 
181  // 2 Parents and 1 Child where one parent depends on the other.
182  {
183  CMutableTransaction mtx_parent;
184  mtx_parent.vin.push_back(CTxIn(COutPoint(m_coinbase_txns[0]->GetHash(), 0)));
185  mtx_parent.vout.push_back(CTxOut(20 * COIN, spk));
186  mtx_parent.vout.push_back(CTxOut(20 * COIN, spk2));
187  CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
188 
189  CMutableTransaction mtx_parent_also_child;
190  mtx_parent_also_child.vin.push_back(CTxIn(COutPoint(tx_parent->GetHash(), 0)));
191  mtx_parent_also_child.vout.push_back(CTxOut(20 * COIN, spk));
192  CTransactionRef tx_parent_also_child = MakeTransactionRef(mtx_parent_also_child);
193 
194  CMutableTransaction mtx_child;
195  mtx_child.vin.push_back(CTxIn(COutPoint(tx_parent->GetHash(), 1)));
196  mtx_child.vin.push_back(CTxIn(COutPoint(tx_parent_also_child->GetHash(), 0)));
197  mtx_child.vout.push_back(CTxOut(39 * COIN, spk));
198  CTransactionRef tx_child = MakeTransactionRef(mtx_child);
199 
201  BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child}));
202  BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
203  BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child, tx_child}));
204  // IsChildWithParents does not detect unsorted parents.
205  BOOST_CHECK(IsChildWithParents({tx_parent_also_child, tx_parent, tx_child}));
206  BOOST_CHECK(CheckPackage({tx_parent, tx_parent_also_child, tx_child}, state));
207  BOOST_CHECK(!CheckPackage({tx_parent_also_child, tx_parent, tx_child}, state));
209  BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
210  }
211 }
212 
213 BOOST_FIXTURE_TEST_CASE(package_submission_tests, TestChain100Setup)
214 {
215  LOCK(cs_main);
216  unsigned int expected_pool_size = m_node.mempool->size();
217  CKey parent_key;
218  parent_key.MakeNewKey(true);
219  CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
220 
221  // Unrelated transactions are not allowed in package submission.
222  Package package_unrelated;
223  for (size_t i{0}; i < 10; ++i) {
224  auto mtx = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[i + 25], /*input_vout=*/0,
225  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
226  /*output_destination=*/parent_locking_script,
227  /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
228  package_unrelated.emplace_back(MakeTransactionRef(mtx));
229  }
230  auto result_unrelated_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
231  package_unrelated, /*test_accept=*/false);
232  BOOST_CHECK(result_unrelated_submit.m_state.IsInvalid());
233  BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
234  BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
235  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
236  BOOST_CHECK(result_unrelated_submit.m_package_feerate == std::nullopt);
237 
238  // Parent and Child (and Grandchild) Package
239  Package package_parent_child;
240  Package package_3gen;
241  auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
242  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
243  /*output_destination=*/parent_locking_script,
244  /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
245  CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
246  package_parent_child.push_back(tx_parent);
247  package_3gen.push_back(tx_parent);
248 
249  CKey child_key;
250  child_key.MakeNewKey(true);
251  CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
252  auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
253  /*input_height=*/101, /*input_signing_key=*/parent_key,
254  /*output_destination=*/child_locking_script,
255  /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
256  CTransactionRef tx_child = MakeTransactionRef(mtx_child);
257  package_parent_child.push_back(tx_child);
258  package_3gen.push_back(tx_child);
259 
260  CKey grandchild_key;
261  grandchild_key.MakeNewKey(true);
262  CScript grandchild_locking_script = GetScriptForDestination(PKHash(grandchild_key.GetPubKey()));
263  auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/tx_child, /*input_vout=*/0,
264  /*input_height=*/101, /*input_signing_key=*/child_key,
265  /*output_destination=*/grandchild_locking_script,
266  /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
267  CTransactionRef tx_grandchild = MakeTransactionRef(mtx_grandchild);
268  package_3gen.push_back(tx_grandchild);
269 
270  // 3 Generations is not allowed.
271  {
272  auto result_3gen_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
273  package_3gen, /*test_accept=*/false);
274  BOOST_CHECK(result_3gen_submit.m_state.IsInvalid());
275  BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
276  BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
277  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
278  BOOST_CHECK(result_3gen_submit.m_package_feerate == std::nullopt);
279  }
280 
281  // Child with missing parent.
282  mtx_child.vin.push_back(CTxIn(COutPoint(package_unrelated[0]->GetHash(), 0)));
283  Package package_missing_parent;
284  package_missing_parent.push_back(tx_parent);
285  package_missing_parent.push_back(MakeTransactionRef(mtx_child));
286  {
287  const auto result_missing_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
288  package_missing_parent, /*test_accept=*/false);
289  BOOST_CHECK(result_missing_parent.m_state.IsInvalid());
290  BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
291  BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetRejectReason(), "package-not-child-with-unconfirmed-parents");
292  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
293 
294  BOOST_CHECK(result_missing_parent.m_package_feerate == std::nullopt);
295  }
296 
297  // Submit package with parent + child.
298  {
299  const auto submit_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
300  package_parent_child, /*test_accept=*/false);
301  expected_pool_size += 2;
302  BOOST_CHECK_MESSAGE(submit_parent_child.m_state.IsValid(),
303  "Package validation unexpectedly failed: " << submit_parent_child.m_state.GetRejectReason());
304  auto it_parent = submit_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
305  auto it_child = submit_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
306  BOOST_CHECK(it_parent != submit_parent_child.m_tx_results.end());
307  BOOST_CHECK(it_parent->second.m_state.IsValid());
308  BOOST_CHECK(it_child != submit_parent_child.m_tx_results.end());
309  BOOST_CHECK(it_child->second.m_state.IsValid());
310 
311  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
312  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
313  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  BOOST_CHECK(submit_parent_child.m_package_feerate == std::nullopt);
318  }
319 
320  // Already-in-mempool transactions should be detected and de-duplicated.
321  {
322  const auto submit_deduped = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
323  package_parent_child, /*test_accept=*/false);
324  BOOST_CHECK_MESSAGE(submit_deduped.m_state.IsValid(),
325  "Package validation unexpectedly failed: " << submit_deduped.m_state.GetRejectReason());
326  auto it_parent_deduped = submit_deduped.m_tx_results.find(tx_parent->GetWitnessHash());
327  auto it_child_deduped = submit_deduped.m_tx_results.find(tx_child->GetWitnessHash());
328  BOOST_CHECK(it_parent_deduped != submit_deduped.m_tx_results.end());
329  BOOST_CHECK(it_parent_deduped->second.m_state.IsValid());
330  BOOST_CHECK(it_parent_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
331  BOOST_CHECK(it_child_deduped != submit_deduped.m_tx_results.end());
332  BOOST_CHECK(it_child_deduped->second.m_state.IsValid());
333  BOOST_CHECK(it_child_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
334 
335  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
336  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
337  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
338 
339  BOOST_CHECK(submit_deduped.m_package_feerate == std::nullopt);
340  }
341 }
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 {
347  // Mine blocks to mature coinbases.
348  mineBlocks(5);
349  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  CScript witnessScript = CScript() << OP_DROP << OP_TRUE;
354  CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
355  auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
356  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
357  /*output_destination=*/scriptPubKey,
358  /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
359  CTransactionRef ptx_parent = MakeTransactionRef(mtx_parent);
360 
361  // Make two children with the same txid but different witnesses.
362  CScriptWitness witness1;
363  witness1.stack.push_back(std::vector<unsigned char>(1));
364  witness1.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
365 
366  CScriptWitness witness2(witness1);
367  witness2.stack.push_back(std::vector<unsigned char>(2));
368  witness2.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
369 
370  CKey child_key;
371  child_key.MakeNewKey(true);
372  CScript child_locking_script = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
373  CMutableTransaction mtx_child1;
374  mtx_child1.nVersion = 1;
375  mtx_child1.vin.resize(1);
376  mtx_child1.vin[0].prevout.hash = ptx_parent->GetHash();
377  mtx_child1.vin[0].prevout.n = 0;
378  mtx_child1.vin[0].scriptSig = CScript();
379  mtx_child1.vin[0].scriptWitness = witness1;
380  mtx_child1.vout.resize(1);
381  mtx_child1.vout[0].nValue = CAmount(48 * COIN);
382  mtx_child1.vout[0].scriptPubKey = child_locking_script;
383 
384  CMutableTransaction mtx_child2{mtx_child1};
385  mtx_child2.vin[0].scriptWitness = witness2;
386 
387  CTransactionRef ptx_child1 = MakeTransactionRef(mtx_child1);
388  CTransactionRef ptx_child2 = MakeTransactionRef(mtx_child2);
389 
390  // child1 and child2 have the same txid
391  BOOST_CHECK_EQUAL(ptx_child1->GetHash(), ptx_child2->GetHash());
392  // child1 and child2 have different wtxids
393  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  {
398  const auto submit_witness1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
399  {ptx_parent, ptx_child1}, /*test_accept=*/false);
400  BOOST_CHECK_MESSAGE(submit_witness1.m_state.IsValid(),
401  "Package validation unexpectedly failed: " << submit_witness1.m_state.GetRejectReason());
402  auto it_parent1 = submit_witness1.m_tx_results.find(ptx_parent->GetWitnessHash());
403  auto it_child1 = submit_witness1.m_tx_results.find(ptx_child1->GetWitnessHash());
404  BOOST_CHECK(it_parent1 != submit_witness1.m_tx_results.end());
405  BOOST_CHECK_MESSAGE(it_parent1->second.m_state.IsValid(),
406  "Transaction unexpectedly failed: " << it_parent1->second.m_state.GetRejectReason());
407  BOOST_CHECK(it_child1 != submit_witness1.m_tx_results.end());
408  BOOST_CHECK_MESSAGE(it_child1->second.m_state.IsValid(),
409  "Transaction unexpectedly failed: " << it_child1->second.m_state.GetRejectReason());
410 
411  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent->GetHash())));
412  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_child1->GetHash())));
413 
414  // Child2 would have been validated individually.
415  BOOST_CHECK(submit_witness1.m_package_feerate == std::nullopt);
416 
417  const auto submit_witness2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
418  {ptx_parent, ptx_child2}, /*test_accept=*/false);
419  BOOST_CHECK(submit_witness2.m_package_feerate == std::nullopt);
420  BOOST_CHECK_MESSAGE(submit_witness2.m_state.IsValid(),
421  "Package validation unexpectedly failed: " << submit_witness2.m_state.GetRejectReason());
422  auto it_parent2_deduped = submit_witness2.m_tx_results.find(ptx_parent->GetWitnessHash());
423  auto it_child2 = submit_witness2.m_tx_results.find(ptx_child2->GetWitnessHash());
424  BOOST_CHECK(it_parent2_deduped != submit_witness2.m_tx_results.end());
425  BOOST_CHECK(it_parent2_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
426  BOOST_CHECK(it_child2 != submit_witness2.m_tx_results.end());
427  BOOST_CHECK(it_child2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
428  BOOST_CHECK_EQUAL(ptx_child1->GetWitnessHash(), it_child2->second.m_other_wtxid.value());
429 
430  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_child2->GetHash())));
431  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  const auto submit_segwit_dedup = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
436  {ptx_parent, ptx_child1}, /*test_accept=*/false);
437  BOOST_CHECK_MESSAGE(submit_segwit_dedup.m_state.IsValid(),
438  "Package validation unexpectedly failed: " << submit_segwit_dedup.m_state.GetRejectReason());
439  auto it_parent_dup = submit_segwit_dedup.m_tx_results.find(ptx_parent->GetWitnessHash());
440  auto it_child_dup = submit_segwit_dedup.m_tx_results.find(ptx_child1->GetWitnessHash());
441  BOOST_CHECK(it_parent_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
442  BOOST_CHECK(it_child_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
443  BOOST_CHECK(submit_witness2.m_package_feerate == std::nullopt);
444  }
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  CKey grandchild_key;
454  grandchild_key.MakeNewKey(true);
455  CScript grandchild_locking_script = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
456  auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/ptx_child2, /*input_vout=*/0,
457  /*input_height=*/0, /*input_signing_key=*/child_key,
458  /*output_destination=*/grandchild_locking_script,
459  /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
460  CTransactionRef ptx_grandchild = MakeTransactionRef(mtx_grandchild);
461 
462  // We already submitted child1 above.
463  {
464  const auto submit_spend_ignored = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
465  {ptx_child2, ptx_grandchild}, /*test_accept=*/false);
466  BOOST_CHECK_MESSAGE(submit_spend_ignored.m_state.IsValid(),
467  "Package validation unexpectedly failed: " << submit_spend_ignored.m_state.GetRejectReason());
468  auto it_child2_ignored = submit_spend_ignored.m_tx_results.find(ptx_child2->GetWitnessHash());
469  auto it_grandchild = submit_spend_ignored.m_tx_results.find(ptx_grandchild->GetWitnessHash());
470  BOOST_CHECK(it_child2_ignored != submit_spend_ignored.m_tx_results.end());
471  BOOST_CHECK(it_child2_ignored->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
472  BOOST_CHECK(it_grandchild != submit_spend_ignored.m_tx_results.end());
473  BOOST_CHECK(it_grandchild->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
474 
475  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_child2->GetHash())));
476  BOOST_CHECK(!m_node.mempool->exists(GenTxid::Wtxid(ptx_child2->GetWitnessHash())));
477  BOOST_CHECK(m_node.mempool->exists(GenTxid::Wtxid(ptx_grandchild->GetWitnessHash())));
478 
479  // Since child2 is ignored, grandchild would be validated individually.
480  BOOST_CHECK(submit_spend_ignored.m_package_feerate == std::nullopt);
481  }
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  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  CScript acs_script = CScript() << OP_TRUE;
489  CScript acs_spk = GetScriptForDestination(WitnessV0ScriptHash(acs_script));
490  CScriptWitness acs_witness;
491  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  auto mtx_parent1 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
495  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
496  /*output_destination=*/acs_spk,
497  /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
498  CTransactionRef ptx_parent1 = MakeTransactionRef(mtx_parent1);
499  package_mixed.push_back(ptx_parent1);
500 
501  // parent2 will have a same-txid-different-witness tx already in the mempool
502  CScript grandparent2_script = CScript() << OP_DROP << OP_TRUE;
503  CScript grandparent2_spk = GetScriptForDestination(WitnessV0ScriptHash(grandparent2_script));
504  CScriptWitness parent2_witness1;
505  parent2_witness1.stack.push_back(std::vector<unsigned char>(1));
506  parent2_witness1.stack.push_back(std::vector<unsigned char>(grandparent2_script.begin(), grandparent2_script.end()));
507  CScriptWitness parent2_witness2;
508  parent2_witness2.stack.push_back(std::vector<unsigned char>(2));
509  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  auto mtx_grandparent2 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
513  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
514  /*output_destination=*/grandparent2_spk,
515  /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
516  CTransactionRef ptx_grandparent2 = MakeTransactionRef(mtx_grandparent2);
517 
518  CMutableTransaction mtx_parent2_v1;
519  mtx_parent2_v1.nVersion = 1;
520  mtx_parent2_v1.vin.resize(1);
521  mtx_parent2_v1.vin[0].prevout.hash = ptx_grandparent2->GetHash();
522  mtx_parent2_v1.vin[0].prevout.n = 0;
523  mtx_parent2_v1.vin[0].scriptSig = CScript();
524  mtx_parent2_v1.vin[0].scriptWitness = parent2_witness1;
525  mtx_parent2_v1.vout.resize(1);
526  mtx_parent2_v1.vout[0].nValue = CAmount(48 * COIN);
527  mtx_parent2_v1.vout[0].scriptPubKey = acs_spk;
528 
529  CMutableTransaction mtx_parent2_v2{mtx_parent2_v1};
530  mtx_parent2_v2.vin[0].scriptWitness = parent2_witness2;
531 
532  CTransactionRef ptx_parent2_v1 = MakeTransactionRef(mtx_parent2_v1);
533  CTransactionRef ptx_parent2_v2 = MakeTransactionRef(mtx_parent2_v2);
534  // Put parent2_v1 in the package, submit parent2_v2 to the mempool.
535  const MempoolAcceptResult parent2_v2_result = m_node.chainman->ProcessTransaction(ptx_parent2_v2);
537  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  auto mtx_parent3 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[3], /*input_vout=*/0,
541  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
542  /*output_destination=*/acs_spk,
543  /*output_amount=*/CAmount(50 * COIN), /*submit=*/false);
544  CTransactionRef ptx_parent3 = MakeTransactionRef(mtx_parent3);
545  package_mixed.push_back(ptx_parent3);
546 
547  // child spends parent1, parent2, and parent3
548  CKey mixed_grandchild_key;
549  mixed_grandchild_key.MakeNewKey(true);
550  CScript mixed_child_spk = GetScriptForDestination(WitnessV0KeyHash(mixed_grandchild_key.GetPubKey()));
551 
552  CMutableTransaction mtx_mixed_child;
553  mtx_mixed_child.vin.push_back(CTxIn(COutPoint(ptx_parent1->GetHash(), 0)));
554  mtx_mixed_child.vin.push_back(CTxIn(COutPoint(ptx_parent2_v1->GetHash(), 0)));
555  mtx_mixed_child.vin.push_back(CTxIn(COutPoint(ptx_parent3->GetHash(), 0)));
556  mtx_mixed_child.vin[0].scriptWitness = acs_witness;
557  mtx_mixed_child.vin[1].scriptWitness = acs_witness;
558  mtx_mixed_child.vin[2].scriptWitness = acs_witness;
559  mtx_mixed_child.vout.push_back(CTxOut((48 + 49 + 50 - 1) * COIN, mixed_child_spk));
560  CTransactionRef ptx_mixed_child = MakeTransactionRef(mtx_mixed_child);
561  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  {
569  const auto mixed_result = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_mixed, false);
570  BOOST_CHECK_MESSAGE(mixed_result.m_state.IsValid(), mixed_result.m_state.GetRejectReason());
571  auto it_parent1 = mixed_result.m_tx_results.find(ptx_parent1->GetWitnessHash());
572  auto it_parent2 = mixed_result.m_tx_results.find(ptx_parent2_v1->GetWitnessHash());
573  auto it_parent3 = mixed_result.m_tx_results.find(ptx_parent3->GetWitnessHash());
574  auto it_child = mixed_result.m_tx_results.find(ptx_mixed_child->GetWitnessHash());
575  BOOST_CHECK(it_parent1 != mixed_result.m_tx_results.end());
576  BOOST_CHECK(it_parent2 != mixed_result.m_tx_results.end());
577  BOOST_CHECK(it_parent3 != mixed_result.m_tx_results.end());
578  BOOST_CHECK(it_child != mixed_result.m_tx_results.end());
579 
580  BOOST_CHECK(it_parent1->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
581  BOOST_CHECK(it_parent2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
582  BOOST_CHECK(it_parent3->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
583  BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
584  BOOST_CHECK_EQUAL(ptx_parent2_v2->GetWitnessHash(), it_parent2->second.m_other_wtxid.value());
585 
586  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent1->GetHash())));
587  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent2_v1->GetHash())));
588  BOOST_CHECK(!m_node.mempool->exists(GenTxid::Wtxid(ptx_parent2_v1->GetWitnessHash())));
589  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(ptx_parent3->GetHash())));
590  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  BOOST_CHECK(mixed_result.m_package_feerate.has_value());
594  const CFeeRate expected_feerate(1 * COIN, GetVirtualTransactionSize(*ptx_parent3) + GetVirtualTransactionSize(*ptx_mixed_child));
595  BOOST_CHECK_MESSAGE(mixed_result.m_package_feerate.value() == expected_feerate,
596  strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
597  mixed_result.m_package_feerate.value().ToString()));
598  }
599 }
600 
602 {
603  mineBlocks(5);
604  LOCK(::cs_main);
605  size_t expected_pool_size = m_node.mempool->size();
606  CKey child_key;
607  child_key.MakeNewKey(true);
608  CScript parent_spk = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
609  CKey grandchild_key;
610  grandchild_key.MakeNewKey(true);
611  CScript child_spk = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
612 
613  // zero-fee parent and high-fee child package
614  const CAmount coinbase_value{50 * COIN};
615  const CAmount parent_value{coinbase_value - 0};
616  const CAmount child_value{parent_value - COIN};
617 
618  Package package_cpfp;
619  auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
620  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
621  /*output_destination=*/parent_spk,
622  /*output_amount=*/parent_value, /*submit=*/false);
623  CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
624  package_cpfp.push_back(tx_parent);
625 
626  auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
627  /*input_height=*/101, /*input_signing_key=*/child_key,
628  /*output_destination=*/child_spk,
629  /*output_amount=*/child_value, /*submit=*/false);
630  CTransactionRef tx_child = MakeTransactionRef(mtx_child);
631  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  m_node.mempool->PrioritiseTransaction(tx_parent->GetHash(), -1 * COIN);
637  {
638  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
639  const auto submit_cpfp_deprio = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
640  package_cpfp, /*test_accept=*/ false);
641  BOOST_CHECK_MESSAGE(submit_cpfp_deprio.m_state.IsInvalid(),
642  "Package validation unexpectedly succeeded: " << submit_cpfp_deprio.m_state.GetRejectReason());
643  BOOST_CHECK(submit_cpfp_deprio.m_tx_results.empty());
644  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
645  const CFeeRate expected_feerate(0, GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child));
646  BOOST_CHECK(submit_cpfp_deprio.m_package_feerate.has_value());
647  BOOST_CHECK(submit_cpfp_deprio.m_package_feerate.value() == CFeeRate{0});
648  BOOST_CHECK_MESSAGE(submit_cpfp_deprio.m_package_feerate.value() == expected_feerate,
649  strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
650  submit_cpfp_deprio.m_package_feerate.value().ToString()));
651  }
652 
653  // Clear the prioritisation of the parent transaction.
654  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  {
659  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
660  const auto submit_cpfp = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
661  package_cpfp, /*test_accept=*/ false);
662  expected_pool_size += 2;
663  BOOST_CHECK_MESSAGE(submit_cpfp.m_state.IsValid(),
664  "Package validation unexpectedly failed: " << submit_cpfp.m_state.GetRejectReason());
665  auto it_parent = submit_cpfp.m_tx_results.find(tx_parent->GetWitnessHash());
666  auto it_child = submit_cpfp.m_tx_results.find(tx_child->GetWitnessHash());
667  BOOST_CHECK(it_parent != submit_cpfp.m_tx_results.end());
668  BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
669  BOOST_CHECK(it_parent->second.m_base_fees.value() == 0);
670  BOOST_CHECK(it_child != submit_cpfp.m_tx_results.end());
671  BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
672  BOOST_CHECK(it_child->second.m_base_fees.value() == COIN);
673 
674  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
675  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
676  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
677 
678  const CFeeRate expected_feerate(coinbase_value - child_value,
679  GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child));
680  BOOST_CHECK(expected_feerate.GetFeePerK() > 1000);
681  BOOST_CHECK(submit_cpfp.m_package_feerate.has_value());
682  BOOST_CHECK_MESSAGE(submit_cpfp.m_package_feerate.value() == expected_feerate,
683  strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
684  submit_cpfp.m_package_feerate.value().ToString()));
685  }
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  Package package_still_too_low;
691  auto mtx_parent_cheap = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
692  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
693  /*output_destination=*/parent_spk,
694  /*output_amount=*/coinbase_value, /*submit=*/false);
695  CTransactionRef tx_parent_cheap = MakeTransactionRef(mtx_parent_cheap);
696  package_still_too_low.push_back(tx_parent_cheap);
697 
698  auto mtx_child_cheap = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_cheap, /*input_vout=*/0,
699  /*input_height=*/101, /*input_signing_key=*/child_key,
700  /*output_destination=*/child_spk,
701  /*output_amount=*/coinbase_value - 200, /*submit=*/false);
702  CTransactionRef tx_child_cheap = MakeTransactionRef(mtx_child_cheap);
703  package_still_too_low.push_back(tx_child_cheap);
704 
705  // Cheap package should fail with package-fee-too-low.
706  {
707  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
708  const auto submit_package_too_low = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
709  package_still_too_low, /*test_accept=*/false);
710  BOOST_CHECK_MESSAGE(submit_package_too_low.m_state.IsInvalid(), "Package validation unexpectedly succeeded");
711  BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
712  BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetRejectReason(), "package-fee-too-low");
713  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
714  const CFeeRate child_feerate(200, GetVirtualTransactionSize(*tx_child_cheap));
715  BOOST_CHECK(child_feerate.GetFeePerK() > 1000);
716  const CFeeRate expected_feerate(200,
717  GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap));
718  BOOST_CHECK(expected_feerate.GetFeePerK() < 1000);
719  BOOST_CHECK(submit_package_too_low.m_package_feerate.has_value());
720  BOOST_CHECK_MESSAGE(submit_package_too_low.m_package_feerate.value() == expected_feerate,
721  strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
722  submit_package_too_low.m_package_feerate.value().ToString()));
723  }
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  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  {
730  const auto submit_prioritised_package = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
731  package_still_too_low, /*test_accept=*/false);
732  expected_pool_size += 2;
733  BOOST_CHECK_MESSAGE(submit_prioritised_package.m_state.IsValid(),
734  "Package validation unexpectedly failed" << submit_prioritised_package.m_state.GetRejectReason());
735  const CFeeRate expected_feerate(1 * COIN + 200,
736  GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap));
737  BOOST_CHECK(submit_prioritised_package.m_package_feerate.has_value());
738  BOOST_CHECK_MESSAGE(submit_prioritised_package.m_package_feerate.value() == expected_feerate,
739  strprintf("Expected package feerate %s, got %s", expected_feerate.ToString(),
740  submit_prioritised_package.m_package_feerate.value().ToString()));
741  }
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  Package package_rich_parent;
749  const CAmount high_parent_fee{1 * COIN};
750  auto mtx_parent_rich = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
751  /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
752  /*output_destination=*/parent_spk,
753  /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
754  CTransactionRef tx_parent_rich = MakeTransactionRef(mtx_parent_rich);
755  package_rich_parent.push_back(tx_parent_rich);
756 
757  auto mtx_child_poor = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_rich, /*input_vout=*/0,
758  /*input_height=*/101, /*input_signing_key=*/child_key,
759  /*output_destination=*/child_spk,
760  /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
761  CTransactionRef tx_child_poor = MakeTransactionRef(mtx_child_poor);
762  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  {
766  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
767  const auto submit_rich_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
768  package_rich_parent, /*test_accept=*/false);
769  expected_pool_size += 1;
770  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  BOOST_CHECK(submit_rich_parent.m_package_feerate.has_value());
775  BOOST_CHECK_MESSAGE(submit_rich_parent.m_package_feerate.value() == CFeeRate(),
776  "expected 0, got " << submit_rich_parent.m_package_feerate.value().ToString());
777  BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
778  BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetRejectReason(), "package-fee-too-low");
779 
780  auto it_parent = submit_rich_parent.m_tx_results.find(tx_parent_rich->GetWitnessHash());
781  BOOST_CHECK(it_parent != submit_rich_parent.m_tx_results.end());
782  BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
783  BOOST_CHECK(it_parent->second.m_state.GetRejectReason() == "");
784  BOOST_CHECK_MESSAGE(it_parent->second.m_base_fees.value() == high_parent_fee,
785  strprintf("rich parent: expected fee %s, got %s", high_parent_fee, it_parent->second.m_base_fees.value()));
786 
787  BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
788  BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent_rich->GetHash())));
789  BOOST_CHECK(!m_node.mempool->exists(GenTxid::Txid(tx_child_poor->GetHash())));
790  }
791 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:426
static constexpr uint32_t MAX_PACKAGE_SIZE
Default maximum total virtual size of transactions in a package in KvB.
Definition: packages.h:19
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:68
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:1164
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
std::vector< CTxIn > vin
Definition: transaction.h:374
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:44
BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
std::map< const uint256, const MempoolAcceptResult > m_tx_results
Map from wtxid to finished MempoolAcceptResults.
Definition: validation.h:211
std::vector< std::vector< unsigned char > > stack
Definition: script.h:566
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:144
bool CheckPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
Definition: packages.cpp:18
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:50
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
iterator end()
Definition: prevector.h:294
Definition: script.h:80
An input of a transaction.
Definition: transaction.h:73
#define LOCK(cs)
Definition: sync.h:261
Fast randomness source.
Definition: random.h:142
BOOST_AUTO_TEST_SUITE_END()
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:39
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
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
An output of a transaction.
Definition: transaction.h:156
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
At least one tx is invalid.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:126
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:34
std::vector< CTxOut > vout
Definition: transaction.h:375
Validation result for a single transaction mempool acceptance.
Definition: validation.h:135
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:305
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:415
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:63
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:271
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:410
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:121
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
iterator begin()
Definition: prevector.h:292
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
A mutable version of CTransaction.
Definition: transaction.h:372
static constexpr CAmount CENT
Definition: setup_common.h:78
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:26
std::string GetRejectReason() const
Definition: validation.h:125
CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
static uint256 InsecureRand256()
Definition: setup_common.h:73
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:54
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:425
#define BOOST_CHECK(expr)
Definition: object.cpp:16
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15