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