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