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 <policy/rbf.h>
10 : : #include <primitives/transaction.h>
11 : : #include <script/script.h>
12 : : #include <serialize.h>
13 : : #include <streams.h>
14 : : #include <test/util/random.h>
15 : : #include <test/util/script.h>
16 : : #include <test/util/setup_common.h>
17 : : #include <util/strencodings.h>
18 : : #include <test/util/txmempool.h>
19 : : #include <validation.h>
20 : :
21 : : #include <boost/test/unit_test.hpp>
22 : :
23 : : using namespace util::hex_literals;
24 : :
25 : : // A fee amount that is above 1sat/vB but below 5sat/vB for most transactions created within these
26 : : // unit tests.
27 : : static const CAmount low_fee_amt{200};
28 : :
29 : 18 : struct TxPackageTest : TestChain100Setup {
30 : : // Create placeholder transactions that have no meaning.
31 : 29 : inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
32 : : {
33 : 29 : CMutableTransaction mtx = CMutableTransaction();
34 [ + - ]: 29 : mtx.vin.resize(num_inputs);
35 [ + - ]: 29 : mtx.vout.resize(num_outputs);
36 [ + - + - ]: 87 : auto random_script = CScript() << ToByteVector(m_rng.rand256()) << ToByteVector(m_rng.rand256());
37 [ + + ]: 1205 : for (size_t i{0}; i < num_inputs; ++i) {
38 : 1176 : mtx.vin[i].prevout.hash = Txid::FromUint256(m_rng.rand256());
39 : 1176 : mtx.vin[i].prevout.n = 0;
40 : 1176 : mtx.vin[i].scriptSig = random_script;
41 : : }
42 [ + + ]: 1205 : for (size_t o{0}; o < num_outputs; ++o) {
43 : 1176 : mtx.vout[o].nValue = 1 * CENT;
44 : 1176 : mtx.vout[o].scriptPubKey = random_script;
45 : : }
46 [ + - ]: 58 : return MakeTransactionRef(mtx);
47 : 58 : }
48 : : }; // struct TxPackageTest
49 : :
50 : : BOOST_FIXTURE_TEST_SUITE(txpackage_tests, TxPackageTest)
51 : :
52 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_hash_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
53 : : {
54 : : // Random real segwit transaction
55 : 1 : DataStream stream_1{
56 : 1 : "02000000000101964b8aa63509579ca6086e6012eeaa4c2f4dd1e283da29b67c8eea38b3c6fd220000000000fdffffff0294c618000000000017a9145afbbb42f4e83312666d0697f9e66259912ecde38768fa2c0000000000160014897388a0889390fd0e153a22bb2cf9d8f019faf50247304402200547406380719f84d68cf4e96cc3e4a1688309ef475b150be2b471c70ea562aa02206d255f5acc40fd95981874d77201d2eb07883657ce1c796513f32b6079545cdf0121023ae77335cefcb5ab4c1dc1fb0d2acfece184e593727d7d5906c78e564c7c11d125cf0c00"_hex,
57 : 1 : };
58 [ + - ]: 1 : CTransaction tx_1(deserialize, TX_WITH_WITNESS, stream_1);
59 [ + - ]: 1 : CTransactionRef ptx_1{MakeTransactionRef(tx_1)};
60 : :
61 : : // Random real nonsegwit transaction
62 : 1 : DataStream stream_2{
63 [ + - ]: 1 : "01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"_hex,
64 [ + - ]: 1 : };
65 [ + - ]: 1 : CTransaction tx_2(deserialize, TX_WITH_WITNESS, stream_2);
66 [ + - ]: 1 : CTransactionRef ptx_2{MakeTransactionRef(tx_2)};
67 : :
68 : : // Random real segwit transaction
69 : 1 : DataStream stream_3{
70 [ + - ]: 1 : "0200000000010177862801f77c2c068a70372b4c435ef8dd621291c36a64eb4dd491f02218f5324600000000fdffffff014a0100000000000022512035ea312034cfac01e956a269f3bf147f569c2fbb00180677421262da042290d803402be713325ff285e66b0380f53f2fae0d0fb4e16f378a440fed51ce835061437566729d4883bc917632f3cff474d6384bc8b989961a1d730d4a87ed38ad28bd337b20f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7fac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a224342414c222c22616d74223a2236393639227d6821c1f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7f00000000"_hex,
71 [ + - ]: 1 : };
72 [ + - ]: 1 : CTransaction tx_3(deserialize, TX_WITH_WITNESS, stream_3);
73 [ + - ]: 1 : CTransactionRef ptx_3{MakeTransactionRef(tx_3)};
74 : :
75 : : // It's easy to see that wtxids are sorted in lexicographical order:
76 [ + - ]: 1 : Wtxid wtxid_1{Wtxid::FromHex("85cd1a31eb38f74ed5742ec9cb546712ab5aaf747de28a9168b53e846cbda17f").value()};
77 [ + - ]: 1 : Wtxid wtxid_2{Wtxid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
78 [ + - ]: 1 : Wtxid wtxid_3{Wtxid::FromHex("e065bac15f62bb4e761d761db928ddee65a47296b2b776785abb912cdec474e3").value()};
79 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tx_1.GetWitnessHash(), wtxid_1);
80 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tx_2.GetWitnessHash(), wtxid_2);
81 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tx_3.GetWitnessHash(), wtxid_3);
82 : :
83 [ + - + - : 2 : BOOST_CHECK(wtxid_1.GetHex() < wtxid_2.GetHex());
+ - + - +
- + - ]
84 [ + - + - : 2 : 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 [ + - ]: 1 : Txid txid_1{Txid::FromHex("bd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69").value()};
88 [ + - ]: 1 : Txid txid_2{Txid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
89 [ + - ]: 1 : Txid txid_3{Txid::FromHex("ee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93").value()};
90 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tx_1.GetHash(), txid_1);
91 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tx_2.GetHash(), txid_2);
92 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tx_3.GetHash(), txid_3);
93 : :
94 [ + - + - : 2 : BOOST_CHECK(txid_2.GetHex() < txid_1.GetHex());
+ - + - +
- ]
95 : :
96 [ + - + - : 2 : BOOST_CHECK(txid_1.ToUint256() != wtxid_1.ToUint256());
+ - ]
97 [ + - + - : 2 : BOOST_CHECK(txid_2.ToUint256() == wtxid_2.ToUint256());
+ - ]
98 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(wtxid_2 < wtxid_1);
+ - ]
103 : : // (in this pair of wtxids, hex string order == uint256 order)
104 [ + - + - : 2 : 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 [ + + + - : 4 : std::vector<CTransactionRef> package_123{ptx_1, ptx_2, ptx_3};
- - - - ]
108 [ + + + - : 4 : std::vector<CTransactionRef> package_132{ptx_1, ptx_3, ptx_2};
- - - - ]
109 [ + + + - : 4 : std::vector<CTransactionRef> package_231{ptx_2, ptx_3, ptx_1};
- - - - ]
110 [ + + + - : 4 : std::vector<CTransactionRef> package_213{ptx_2, ptx_1, ptx_3};
- - - - ]
111 [ + + + - : 4 : std::vector<CTransactionRef> package_312{ptx_3, ptx_1, ptx_2};
- - - - ]
112 [ + + + - : 4 : std::vector<CTransactionRef> package_321{ptx_3, ptx_2, ptx_1};
- - - - ]
113 : :
114 [ + - + - : 1 : uint256 calculated_hash_123 = (HashWriter() << wtxid_1 << wtxid_2 << wtxid_3).GetSHA256();
+ - + - +
- ]
115 : :
116 [ + - + - : 1 : uint256 hash_if_by_txid = (HashWriter() << wtxid_2 << wtxid_1 << wtxid_3).GetSHA256();
+ - + - +
- ]
117 [ + - + - : 2 : BOOST_CHECK(hash_if_by_txid != calculated_hash_123);
+ - ]
118 : :
119 [ + - + - : 1 : uint256 hash_if_use_txid = (HashWriter() << txid_2 << txid_1 << txid_3).GetSHA256();
+ - + - +
- ]
120 [ + - + - : 2 : BOOST_CHECK(hash_if_use_txid != calculated_hash_123);
+ - ]
121 : :
122 [ + - + - : 1 : uint256 hash_if_use_int_order = (HashWriter() << wtxid_2 << wtxid_1 << wtxid_3).GetSHA256();
+ - + - +
- ]
123 [ + - + - : 2 : BOOST_CHECK(hash_if_use_int_order != calculated_hash_123);
+ - ]
124 : :
125 [ + - + - : 1 : BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_123));
+ - ]
126 [ + - + - : 1 : BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_132));
+ - ]
127 [ + - + - : 1 : BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_231));
+ - ]
128 [ + - + - : 1 : BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_213));
+ - ]
129 [ + - + - : 1 : BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_312));
+ - ]
130 [ + - + - : 1 : BOOST_CHECK_EQUAL(calculated_hash_123, GetPackageHash(package_321));
+ - ]
131 [ + - + - : 31 : }
+ - - + +
- + - + -
- + + - +
- + - - +
+ - + - +
- - + + -
+ - + - -
+ + - + -
+ - - + +
- + - +
- ]
132 : :
133 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_sanitization_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
134 : : {
135 : : // Packages can't have more than 25 transactions.
136 : 1 : Package package_too_many;
137 [ + - ]: 1 : package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
138 [ + + ]: 27 : for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
139 [ + - + - ]: 52 : package_too_many.emplace_back(create_placeholder_tx(1, 1));
140 : : }
141 [ + - ]: 1 : PackageValidationState state_too_many;
142 [ + - + - : 2 : BOOST_CHECK(!IsWellFormedPackage(package_too_many, state_too_many, /*require_sorted=*/true));
+ - + - ]
143 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(state_too_many.GetResult(), PackageValidationResult::PCKG_POLICY);
144 [ + - - + : 2 : 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 [ + - ]: 1 : CTransactionRef large_ptx = create_placeholder_tx(150, 150);
148 : 1 : Package package_too_large;
149 : 1 : auto size_large = GetTransactionWeight(*large_ptx);
150 : 1 : size_t total_weight{0};
151 [ + + ]: 6 : while (total_weight <= MAX_PACKAGE_WEIGHT) {
152 [ + - ]: 4 : package_too_large.push_back(large_ptx);
153 : 4 : total_weight += size_large;
154 : : }
155 [ + - - + : 2 : BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
+ - + - ]
156 [ + - ]: 1 : PackageValidationState state_too_large;
157 [ + - + - : 2 : BOOST_CHECK(!IsWellFormedPackage(package_too_large, state_too_large, /*require_sorted=*/true));
+ - + - ]
158 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(state_too_large.GetResult(), PackageValidationResult::PCKG_POLICY);
159 [ + - - + : 2 : BOOST_CHECK_EQUAL(state_too_large.GetRejectReason(), "package-too-large");
+ - ]
160 : :
161 : : // Packages can't contain transactions with the same txid.
162 : 1 : Package package_duplicate_txids_empty;
163 [ + + ]: 4 : for (auto i{0}; i < 3; ++i) {
164 [ + - ]: 3 : CMutableTransaction empty_tx;
165 [ + - + - ]: 9 : package_duplicate_txids_empty.emplace_back(MakeTransactionRef(empty_tx));
166 : 3 : }
167 [ + - ]: 1 : PackageValidationState state_duplicates;
168 [ + - + - : 2 : BOOST_CHECK(!IsWellFormedPackage(package_duplicate_txids_empty, state_duplicates, /*require_sorted=*/true));
+ - + - ]
169 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(state_duplicates.GetResult(), PackageValidationResult::PCKG_POLICY);
170 [ + - - + : 2 : BOOST_CHECK_EQUAL(state_duplicates.GetRejectReason(), "package-contains-duplicates");
+ - ]
171 [ + - + - : 2 : BOOST_CHECK(!IsConsistentPackage(package_duplicate_txids_empty));
+ - + - ]
172 : :
173 : : // Packages can't have transactions spending the same prevout
174 [ + - ]: 1 : CMutableTransaction tx_zero_1;
175 [ + - ]: 1 : CMutableTransaction tx_zero_2;
176 [ + - ]: 1 : COutPoint same_prevout{Txid::FromUint256(m_rng.rand256()), 0};
177 [ + - ]: 1 : tx_zero_1.vin.emplace_back(same_prevout);
178 [ + - ]: 1 : tx_zero_2.vin.emplace_back(same_prevout);
179 : : // Different vouts (not the same tx)
180 [ + - ]: 1 : tx_zero_1.vout.emplace_back(CENT, P2WSH_OP_TRUE);
181 [ + - ]: 1 : tx_zero_2.vout.emplace_back(2 * CENT, P2WSH_OP_TRUE);
182 [ + + + - : 3 : Package package_conflicts{MakeTransactionRef(tx_zero_1), MakeTransactionRef(tx_zero_2)};
- - - - ]
183 [ + - + - : 2 : BOOST_CHECK(!IsConsistentPackage(package_conflicts));
+ - + - ]
184 : : // Transactions are considered sorted when they have no dependencies.
185 [ + - + - : 2 : BOOST_CHECK(IsTopoSortedPackage(package_conflicts));
+ - + - ]
186 [ + - ]: 1 : PackageValidationState state_conflicts;
187 [ + - + - : 2 : BOOST_CHECK(!IsWellFormedPackage(package_conflicts, state_conflicts, /*require_sorted=*/true));
+ - + - ]
188 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(state_conflicts.GetResult(), PackageValidationResult::PCKG_POLICY);
189 [ + - - + : 2 : 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 [ + - ]: 1 : CMutableTransaction dup_tx;
194 [ + - ]: 1 : const COutPoint rand_prevout{Txid::FromUint256(m_rng.rand256()), 0};
195 [ + - ]: 1 : dup_tx.vin.emplace_back(rand_prevout);
196 [ + - ]: 1 : dup_tx.vin.emplace_back(rand_prevout);
197 [ + + + - : 2 : Package package_with_dup_tx{MakeTransactionRef(dup_tx)};
- - - - ]
198 [ + - + - : 2 : BOOST_CHECK(IsConsistentPackage(package_with_dup_tx));
+ - + - ]
199 [ + - + - ]: 1 : package_with_dup_tx.emplace_back(create_placeholder_tx(1, 1));
200 [ + - + - : 2 : BOOST_CHECK(IsConsistentPackage(package_with_dup_tx));
+ - ]
201 [ + - + - : 14 : }
- + + - -
+ + - - -
- - ]
202 : :
203 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_validation_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
204 : : {
205 : 1 : LOCK(cs_main);
206 [ + - ]: 1 : unsigned int initialPoolSize = m_node.mempool->size();
207 : :
208 : : // Parent and Child Package
209 : 1 : CKey parent_key = GenerateRandomKey();
210 [ + - + - : 1 : CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
+ - ]
211 [ + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
212 [ + - ]: 1 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
213 : : /*output_destination=*/parent_locking_script,
214 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
215 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
216 : :
217 : 1 : CKey child_key = GenerateRandomKey();
218 [ + - + - : 1 : CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
+ - ]
219 [ + - ]: 2 : 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 [ + - ]: 2 : /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
223 [ + - ]: 1 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
224 [ + + + - : 3 : Package package_parent_child{tx_parent, tx_child};
- - - - ]
225 [ + - + - ]: 1 : const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_parent_child, /*test_accept=*/true, /*client_maxfeerate=*/{});
226 [ + - - + ]: 1 : if (auto err_parent_child{CheckPackageMempoolAcceptResult(package_parent_child, result_parent_child, /*expect_valid=*/true, nullptr)}) {
227 [ # # # # : 0 : BOOST_ERROR(err_parent_child.value());
# # ]
228 : : } else {
229 : 1 : auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
230 : 1 : auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
231 : :
232 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_parent)) == COIN);
+ - + - +
- + - ]
233 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().size(), 1);
- + + - ]
234 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().front(), tx_parent->GetWitnessHash());
+ - ]
235 : :
236 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_child)) == COIN);
+ - + - +
- + - ]
237 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().size(), 1);
- + + - ]
238 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().front(), tx_child->GetWitnessHash());
+ - ]
239 : 0 : }
240 : : // A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
241 [ + - ]: 1 : CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
242 [ + - + - : 2 : BOOST_CHECK(GetVirtualTransactionSize(*giant_ptx) > DEFAULT_ANCESTOR_SIZE_LIMIT_KVB * 1000);
+ - + - ]
243 [ + + + - : 2 : Package package_single_giant{giant_ptx};
- - - - ]
244 [ + - + - ]: 1 : auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_single_giant, /*test_accept=*/true, /*client_maxfeerate=*/{});
245 [ + - - + ]: 1 : if (auto err_single_large{CheckPackageMempoolAcceptResult(package_single_giant, result_single_large, /*expect_valid=*/false, nullptr)}) {
246 [ # # # # : 0 : BOOST_ERROR(err_single_large.value());
# # ]
247 : : } else {
248 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
249 [ + - - + : 2 : BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
+ - ]
250 : 1 : auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
251 [ + - - + : 2 : BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
+ - ]
252 : 0 : }
253 : :
254 : : // Check that mempool size hasn't changed.
255 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
+ - ]
256 [ + - + - : 12 : }
- + + - -
+ + - + -
+ - + - ]
257 : :
258 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(noncontextual_package_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
259 : : {
260 : : // The signatures won't be verified so we can just use a placeholder
261 : 1 : CKey placeholder_key = GenerateRandomKey();
262 [ + - + - : 1 : CScript spk = GetScriptForDestination(PKHash(placeholder_key.GetPubKey()));
+ - ]
263 : 1 : CKey placeholder_key_2 = GenerateRandomKey();
264 [ + - + - : 1 : CScript spk2 = GetScriptForDestination(PKHash(placeholder_key_2.GetPubKey()));
+ - ]
265 : :
266 : : // Parent and Child Package
267 : 1 : {
268 [ + - + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(m_coinbase_txns[0], 0, 0, coinbaseKey, spk,
269 [ + - ]: 2 : CAmount(49 * COIN), /*submit=*/false);
270 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
271 : :
272 [ + - ]: 2 : auto mtx_child = CreateValidMempoolTransaction(tx_parent, 0, 101, placeholder_key, spk2,
273 [ + - ]: 2 : CAmount(48 * COIN), /*submit=*/false);
274 [ + - ]: 1 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
275 : :
276 [ + - ]: 1 : PackageValidationState state;
277 [ + - + - : 5 : BOOST_CHECK(IsWellFormedPackage({tx_parent, tx_child}, state, /*require_sorted=*/true));
+ - + - +
+ + - - -
- - ]
278 [ + - + - : 5 : BOOST_CHECK(!IsWellFormedPackage({tx_child, tx_parent}, state, /*require_sorted=*/true));
+ - + - +
+ + - - -
- - ]
279 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(state.GetResult(), PackageValidationResult::PCKG_POLICY);
280 [ + - - + : 2 : BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
+ - ]
281 [ + - + - : 5 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
+ - + - +
+ + - - -
- - ]
282 [ + - + - : 5 : BOOST_CHECK(IsChildWithParentsTree({tx_parent, tx_child}));
+ - + - +
+ + - - -
- - ]
283 [ + - + - : 6 : BOOST_CHECK(GetPackageHash({tx_parent}) != GetPackageHash({tx_child}));
+ - + - +
- + + + -
+ + + - -
- - - - -
- - ]
284 [ + - + - : 7 : BOOST_CHECK(GetPackageHash({tx_child, tx_child}) != GetPackageHash({tx_child}));
+ - + - +
- + + + -
+ + + - -
- - - - -
- - ]
285 [ + - + - : 8 : BOOST_CHECK(GetPackageHash({tx_child, tx_parent}) != GetPackageHash({tx_child, tx_child}));
+ - + - +
- + + + -
+ + + - -
- - - - -
- - ]
286 [ + - + - : 2 : BOOST_CHECK(!IsChildWithParents({}));
+ - + - ]
287 [ + - + - : 2 : BOOST_CHECK(!IsChildWithParentsTree({}));
+ - ]
288 [ + - + - ]: 4 : }
289 : :
290 : : // 24 Parents and 1 Child
291 : 1 : {
292 : 1 : Package package;
293 [ + - ]: 1 : CMutableTransaction child;
294 [ + + ]: 25 : for (int i{0}; i < 24; ++i) {
295 [ + - + - : 96 : auto parent = MakeTransactionRef(CreateValidMempoolTransaction(m_coinbase_txns[i + 1],
+ - + - ]
296 : 24 : 0, 0, coinbaseKey, spk, CAmount(48 * COIN), false));
297 [ + - ]: 24 : package.emplace_back(parent);
298 [ + - ]: 24 : child.vin.emplace_back(COutPoint(parent->GetHash(), 0));
299 : 24 : }
300 [ + - ]: 1 : child.vout.emplace_back(47 * COIN, spk2);
301 : :
302 : : // The child must be in the package.
303 [ + - + - : 2 : BOOST_CHECK(!IsChildWithParents(package));
+ - ]
304 : :
305 : : // The parents can be in any order.
306 : 1 : FastRandomContext rng;
307 : 1 : std::shuffle(package.begin(), package.end(), rng);
308 [ + - + - : 2 : package.push_back(MakeTransactionRef(child));
- + ]
309 : :
310 [ + - ]: 1 : PackageValidationState state;
311 [ + - + - : 2 : BOOST_CHECK(IsWellFormedPackage(package, state, /*require_sorted=*/true));
+ - + - ]
312 [ + - + - : 2 : BOOST_CHECK(IsChildWithParents(package));
+ - + - ]
313 [ + - + - : 2 : BOOST_CHECK(IsChildWithParentsTree(package));
+ - ]
314 : :
315 : 1 : package.erase(package.begin());
316 [ + - + - : 2 : BOOST_CHECK(IsChildWithParents(package));
+ - + - ]
317 : :
318 : : // The package cannot have unrelated transactions.
319 [ + - ]: 1 : package.insert(package.begin(), m_coinbase_txns[0]);
320 [ + - + - : 2 : BOOST_CHECK(!IsChildWithParents(package));
+ - ]
321 : 2 : }
322 : :
323 : : // 2 Parents and 1 Child where one parent depends on the other.
324 : 1 : {
325 [ + - ]: 1 : CMutableTransaction mtx_parent;
326 [ + - ]: 1 : mtx_parent.vin.emplace_back(COutPoint(m_coinbase_txns[0]->GetHash(), 0));
327 [ + - ]: 1 : mtx_parent.vout.emplace_back(20 * COIN, spk);
328 [ + - ]: 1 : mtx_parent.vout.emplace_back(20 * COIN, spk2);
329 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
330 : :
331 [ + - ]: 1 : CMutableTransaction mtx_parent_also_child;
332 [ + - ]: 1 : mtx_parent_also_child.vin.emplace_back(COutPoint(tx_parent->GetHash(), 0));
333 [ + - ]: 1 : mtx_parent_also_child.vout.emplace_back(20 * COIN, spk);
334 [ + - ]: 1 : CTransactionRef tx_parent_also_child = MakeTransactionRef(mtx_parent_also_child);
335 : :
336 [ + - ]: 1 : CMutableTransaction mtx_child;
337 [ + - ]: 1 : mtx_child.vin.emplace_back(COutPoint(tx_parent->GetHash(), 1));
338 [ + - ]: 1 : mtx_child.vin.emplace_back(COutPoint(tx_parent_also_child->GetHash(), 0));
339 [ + - ]: 1 : mtx_child.vout.emplace_back(39 * COIN, spk);
340 [ + - ]: 1 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
341 : :
342 [ + - ]: 1 : PackageValidationState state;
343 [ + - + - : 5 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child}));
+ - + - +
+ + - - -
- - ]
344 [ + - + - : 5 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_child}));
+ - + - +
+ + - - -
- - ]
345 [ + - + - : 6 : BOOST_CHECK(IsChildWithParents({tx_parent, tx_parent_also_child, tx_child}));
+ - + - +
+ + - - -
- - ]
346 [ + - + - : 6 : BOOST_CHECK(!IsChildWithParentsTree({tx_parent, tx_parent_also_child, tx_child}));
+ - + - +
+ + - - -
- - ]
347 : : // IsChildWithParents does not detect unsorted parents.
348 [ + - + - : 6 : BOOST_CHECK(IsChildWithParents({tx_parent_also_child, tx_parent, tx_child}));
+ - + - +
+ + - - -
- - ]
349 [ + - + - : 6 : BOOST_CHECK(IsWellFormedPackage({tx_parent, tx_parent_also_child, tx_child}, state, /*require_sorted=*/true));
+ - + - +
+ + - - -
- - ]
350 [ + - + - : 6 : BOOST_CHECK(!IsWellFormedPackage({tx_parent_also_child, tx_parent, tx_child}, state, /*require_sorted=*/true));
+ - + - +
+ + - - -
- - ]
351 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(state.GetResult(), PackageValidationResult::PCKG_POLICY);
352 [ + - - + : 2 : BOOST_CHECK_EQUAL(state.GetRejectReason(), "package-not-sorted");
+ - ]
353 [ + - + - : 7 : }
+ - ]
354 [ + - + - : 54 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
355 : :
356 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_submission_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
357 : : {
358 : : // Mine blocks to mature coinbases.
359 : 1 : mineBlocks(3);
360 : 1 : MockMempoolMinFee(CFeeRate(5000));
361 : 1 : LOCK(cs_main);
362 [ + - ]: 1 : unsigned int expected_pool_size = m_node.mempool->size();
363 : 1 : CKey parent_key = GenerateRandomKey();
364 [ + - + - : 1 : CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
+ - ]
365 : :
366 : : // Unrelated transactions are not allowed in package submission.
367 : 1 : Package package_unrelated;
368 [ + + ]: 11 : for (size_t i{0}; i < 10; ++i) {
369 [ + - ]: 20 : auto mtx = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[i + 25], /*input_vout=*/0,
370 [ + - ]: 10 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
371 : : /*output_destination=*/parent_locking_script,
372 [ + - ]: 20 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
373 [ + - + - ]: 30 : package_unrelated.emplace_back(MakeTransactionRef(mtx));
374 : 10 : }
375 [ + - ]: 1 : auto result_unrelated_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
376 [ + - + - ]: 1 : package_unrelated, /*test_accept=*/false, /*client_maxfeerate=*/{});
377 : : // We don't expect m_tx_results for each transaction when basic sanity checks haven't passed.
378 [ + - + - : 2 : BOOST_CHECK(result_unrelated_submit.m_state.IsInvalid());
+ - ]
379 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
380 [ + - - + : 2 : BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
+ - ]
381 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
382 : :
383 : : // Parent and Child (and Grandchild) Package
384 : 1 : Package package_parent_child;
385 : 1 : Package package_3gen;
386 [ + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
387 [ + - ]: 1 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
388 : : /*output_destination=*/parent_locking_script,
389 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
390 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
391 [ + - ]: 1 : package_parent_child.push_back(tx_parent);
392 [ + - ]: 1 : package_3gen.push_back(tx_parent);
393 : :
394 : 1 : CKey child_key = GenerateRandomKey();
395 [ + - + - : 1 : CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
+ - ]
396 [ + - ]: 2 : auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
397 : : /*input_height=*/101, /*input_signing_key=*/parent_key,
398 : : /*output_destination=*/child_locking_script,
399 [ + - ]: 2 : /*output_amount=*/CAmount(48 * COIN), /*submit=*/false);
400 [ + - ]: 1 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
401 [ + - ]: 1 : package_parent_child.push_back(tx_child);
402 [ + - ]: 1 : package_3gen.push_back(tx_child);
403 : :
404 : 1 : CKey grandchild_key = GenerateRandomKey();
405 [ + - + - : 1 : CScript grandchild_locking_script = GetScriptForDestination(PKHash(grandchild_key.GetPubKey()));
+ - ]
406 [ + - ]: 2 : auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/tx_child, /*input_vout=*/0,
407 : : /*input_height=*/101, /*input_signing_key=*/child_key,
408 : : /*output_destination=*/grandchild_locking_script,
409 [ + - ]: 2 : /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
410 [ + - ]: 1 : CTransactionRef tx_grandchild = MakeTransactionRef(mtx_grandchild);
411 [ + - ]: 1 : package_3gen.push_back(tx_grandchild);
412 : :
413 : : // 3 Generations is not allowed.
414 : 1 : {
415 [ + - ]: 1 : auto result_3gen_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
416 [ + - + - ]: 1 : package_3gen, /*test_accept=*/false, /*client_maxfeerate=*/{});
417 [ + - + - : 2 : BOOST_CHECK(result_3gen_submit.m_state.IsInvalid());
+ - ]
418 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
419 [ + - - + : 2 : BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
+ - ]
420 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
421 : 1 : }
422 : :
423 : : // Parent and child package where transactions are invalid for reasons other than fee and
424 : : // missing inputs, so the package validation isn't expected to happen.
425 : 1 : {
426 : 1 : CScriptWitness bad_witness;
427 [ + - ]: 1 : bad_witness.stack.emplace_back(1);
428 [ + - ]: 1 : CMutableTransaction mtx_parent_invalid{mtx_parent};
429 [ + - ]: 1 : mtx_parent_invalid.vin[0].scriptWitness = bad_witness;
430 [ + - ]: 1 : CTransactionRef tx_parent_invalid = MakeTransactionRef(mtx_parent_invalid);
431 [ + + + - : 3 : Package package_invalid_parent{tx_parent_invalid, tx_child};
- - - - ]
432 [ + - ]: 1 : auto result_quit_early = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
433 [ + - + - ]: 1 : package_invalid_parent, /*test_accept=*/ false, /*client_maxfeerate=*/{});
434 [ + - - + ]: 1 : if (auto err_parent_invalid{CheckPackageMempoolAcceptResult(package_invalid_parent, result_quit_early, /*expect_valid=*/false, m_node.mempool.get())}) {
435 [ # # # # : 0 : BOOST_ERROR(err_parent_invalid.value());
# # ]
436 : : } else {
437 : 1 : auto it_parent = result_quit_early.m_tx_results.find(tx_parent_invalid->GetWitnessHash());
438 : 1 : auto it_child = result_quit_early.m_tx_results.find(tx_child->GetWitnessHash());
439 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent->second.m_state.GetResult(), TxValidationResult::TX_WITNESS_MUTATED);
440 [ + - - + : 2 : BOOST_CHECK_EQUAL(it_parent->second.m_state.GetRejectReason(), "bad-witness-nonstandard");
+ - ]
441 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child->second.m_state.GetResult(), TxValidationResult::TX_MISSING_INPUTS);
442 [ + - - + : 2 : BOOST_CHECK_EQUAL(it_child->second.m_state.GetRejectReason(), "bad-txns-inputs-missingorspent");
+ - ]
443 : 0 : }
444 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_quit_early.m_state.GetResult(), PackageValidationResult::PCKG_TX);
445 [ + - ]: 3 : }
446 : :
447 : : // Submit package with parent + child.
448 : 1 : {
449 [ + - ]: 1 : const auto submit_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
450 [ + - + - ]: 1 : package_parent_child, /*test_accept=*/false, /*client_maxfeerate=*/{});
451 : 1 : expected_pool_size += 2;
452 [ + - - + : 3 : BOOST_CHECK_MESSAGE(submit_parent_child.m_state.IsValid(),
+ - + - ]
453 : : "Package validation unexpectedly failed: " << submit_parent_child.m_state.GetRejectReason());
454 [ + - - + : 1 : BOOST_CHECK_EQUAL(submit_parent_child.m_tx_results.size(), package_parent_child.size());
+ - ]
455 : 1 : auto it_parent = submit_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
456 : 1 : auto it_child = submit_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
457 [ + - + - : 2 : BOOST_CHECK(it_parent != submit_parent_child.m_tx_results.end());
+ - ]
458 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_state.IsValid());
+ - ]
459 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_effective_feerate == CFeeRate(1 * COIN, GetVirtualTransactionSize(*tx_parent)));
+ - + - +
- ]
460 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().size(), 1);
- + + - ]
461 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().front(), tx_parent->GetWitnessHash());
+ - ]
462 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_effective_feerate == CFeeRate(1 * COIN, GetVirtualTransactionSize(*tx_child)));
+ - + - +
- ]
463 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().size(), 1);
- + + - ]
464 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().front(), tx_child->GetWitnessHash());
+ - ]
465 : :
466 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
467 : 1 : }
468 : :
469 : : // Already-in-mempool transactions should be detected and de-duplicated.
470 : 1 : {
471 [ + - ]: 1 : const auto submit_deduped = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
472 [ + - + - ]: 1 : package_parent_child, /*test_accept=*/false, /*client_maxfeerate=*/{});
473 [ + - - + ]: 1 : if (auto err_deduped{CheckPackageMempoolAcceptResult(package_parent_child, submit_deduped, /*expect_valid=*/true, m_node.mempool.get())}) {
474 [ # # # # : 0 : BOOST_ERROR(err_deduped.value());
# # ]
475 : : } else {
476 : 1 : auto it_parent_deduped = submit_deduped.m_tx_results.find(tx_parent->GetWitnessHash());
477 : 1 : auto it_child_deduped = submit_deduped.m_tx_results.find(tx_child->GetWitnessHash());
478 [ + - + - : 2 : BOOST_CHECK(it_parent_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
+ - ]
479 [ + - + - ]: 2 : BOOST_CHECK(it_child_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
480 : 0 : }
481 : :
482 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
483 : 1 : }
484 : :
485 : : // In-mempool parent and child with missing parent.
486 : 1 : {
487 [ + - + - : 4 : auto tx_parent_1 = MakeTransactionRef(CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
+ - + - ]
488 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
489 : : /*output_destination=*/parent_locking_script,
490 : 1 : /*output_amount=*/CAmount(50 * COIN - low_fee_amt), /*submit=*/false));
491 [ + - + - : 4 : auto tx_parent_2 = MakeTransactionRef(CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
+ - + - ]
492 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
493 : : /*output_destination=*/parent_locking_script,
494 : 1 : /*output_amount=*/CAmount(50 * COIN - 800), /*submit=*/false));
495 : :
496 [ + - + - : 9 : auto tx_child_missing_parent = MakeTransactionRef(CreateValidMempoolTransaction({tx_parent_1, tx_parent_2},
+ - + - +
+ + - + +
+ + - - -
- - - -
- ]
497 : : {{tx_parent_1->GetHash(), 0}, {tx_parent_2->GetHash(), 0}},
498 : : /*input_height=*/0, {parent_key},
499 : 2 : {{49 * COIN, child_locking_script}}, /*submit=*/false));
500 : :
501 [ + + + - : 3 : Package package_missing_parent{tx_parent_1, tx_child_missing_parent};
- - - - ]
502 : :
503 [ + - ]: 1 : const auto result_missing_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
504 [ + - + - ]: 1 : package_missing_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
505 [ + - - + ]: 1 : if (auto err_missing_parent{CheckPackageMempoolAcceptResult(package_missing_parent, result_missing_parent, /*expect_valid=*/false, m_node.mempool.get())}) {
506 [ # # # # : 0 : BOOST_ERROR(err_missing_parent.value());
# # ]
507 : : } else {
508 : 1 : auto it_parent = result_missing_parent.m_tx_results.find(tx_parent_1->GetWitnessHash());
509 : 1 : auto it_child = result_missing_parent.m_tx_results.find(tx_child_missing_parent->GetWitnessHash());
510 : :
511 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetResult(), PackageValidationResult::PCKG_TX);
512 [ + - - + : 2 : BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetRejectReason(), "transaction failed");
+ - ]
513 : :
514 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent->second.m_state.GetResult(), TxValidationResult::TX_RECONSIDERABLE);
515 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child->second.m_state.GetResult(), TxValidationResult::TX_MISSING_INPUTS);
516 [ + - - + : 2 : BOOST_CHECK_EQUAL(it_child->second.m_state.GetRejectReason(), "bad-txns-inputs-missingorspent");
+ - ]
517 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
518 : 0 : }
519 : :
520 : : // Submit parent2 ahead of time, should become ok.
521 [ + + + - : 2 : Package package_just_parent2{tx_parent_2};
- - - - ]
522 : 1 : expected_pool_size += 1;
523 [ + - ]: 1 : const auto result_just_parent2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
524 [ + - + - ]: 1 : package_just_parent2, /*test_accept=*/false, /*client_maxfeerate=*/{});
525 [ + - - + ]: 1 : if (auto err_parent2{CheckPackageMempoolAcceptResult(package_just_parent2, result_just_parent2, /*expect_valid=*/true, m_node.mempool.get())}) {
526 [ # # # # : 0 : BOOST_ERROR(err_parent2.value());
# # ]
527 : 0 : }
528 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
529 : :
530 [ + - ]: 1 : const auto result_parent_already_in = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
531 [ + - + - ]: 1 : package_missing_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
532 : 1 : expected_pool_size += 2;
533 [ + - - + ]: 1 : if (auto err_parent_already_in{CheckPackageMempoolAcceptResult(package_missing_parent, result_parent_already_in, /*expect_valid=*/true, m_node.mempool.get())}) {
534 [ # # # # : 0 : BOOST_ERROR(err_parent_already_in.value());
# # ]
535 : 0 : }
536 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
537 [ + - + - : 4 : }
+ - + - ]
538 [ + - + - : 20 : }
- + + - +
- + - + -
+ - + - +
- - + + -
- + + - +
- + - ]
539 : :
540 : : // Tests for packages containing a single transaction
541 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_single_tx)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
542 : : {
543 : : // Mine blocks to mature coinbases.
544 : 1 : mineBlocks(3);
545 : 1 : LOCK(cs_main);
546 [ + - ]: 1 : auto expected_pool_size{m_node.mempool->size()};
547 : :
548 : 1 : const CAmount high_fee{1000};
549 : :
550 : : // No unconfirmed parents
551 : 1 : CKey single_key = GenerateRandomKey();
552 [ + - + - : 1 : CScript single_locking_script = GetScriptForDestination(PKHash(single_key.GetPubKey()));
+ - ]
553 [ + - ]: 2 : auto mtx_single = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
554 [ + - ]: 1 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
555 : : /*output_destination=*/single_locking_script,
556 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
557 [ + - ]: 1 : CTransactionRef tx_single = MakeTransactionRef(mtx_single);
558 [ + + + - : 2 : Package package_tx_single{tx_single};
- - - - ]
559 [ + - ]: 1 : const auto result_single_tx = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
560 [ + - + - ]: 1 : package_tx_single, /*test_accept=*/false, /*client_maxfeerate=*/{});
561 : 1 : expected_pool_size += 1;
562 [ + - + - : 2 : BOOST_CHECK_MESSAGE(result_single_tx.m_state.IsValid(),
+ - + - ]
563 : : "Package validation unexpectedly failed: " << result_single_tx.m_state.ToString());
564 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
565 : :
566 : : // Parent and Child. Both submitted by themselves through the ProcessNewPackage interface.
567 : 1 : CKey parent_key = GenerateRandomKey();
568 [ + - + - : 1 : CScript parent_locking_script = GetScriptForDestination(WitnessV0KeyHash(parent_key.GetPubKey()));
+ - ]
569 [ + - + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
570 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
571 : : /*output_destination=*/parent_locking_script,
572 [ + - ]: 2 : /*output_amount=*/CAmount(50 * COIN) - high_fee, /*submit=*/false);
573 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
574 [ + + + - : 2 : Package package_just_parent{tx_parent};
- - - - ]
575 [ + - + - ]: 1 : const auto result_just_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_just_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
576 [ + - - + ]: 1 : if (auto err_parent_child{CheckPackageMempoolAcceptResult(package_just_parent, result_just_parent, /*expect_valid=*/true, nullptr)}) {
577 [ # # # # : 0 : BOOST_ERROR(err_parent_child.value());
# # ]
578 : : } else {
579 : 1 : auto it_parent = result_just_parent.m_tx_results.find(tx_parent->GetWitnessHash());
580 [ + - + - : 2 : BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(), it_parent->second.m_state.ToString());
+ - ]
581 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_parent)) == high_fee);
+ - + - +
- + - ]
582 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().size(), 1);
- + + - ]
583 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent->second.m_wtxids_fee_calculations.value().front(), tx_parent->GetWitnessHash());
+ - ]
584 : 0 : }
585 : 1 : expected_pool_size += 1;
586 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
587 : :
588 : 1 : CKey child_key = GenerateRandomKey();
589 [ + - + - : 1 : CScript child_locking_script = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
+ - ]
590 [ + - ]: 2 : auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
591 : : /*input_height=*/101, /*input_signing_key=*/parent_key,
592 : : /*output_destination=*/child_locking_script,
593 [ + - ]: 2 : /*output_amount=*/CAmount(50 * COIN) - 2 * high_fee, /*submit=*/false);
594 [ + - ]: 1 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
595 [ + + + - : 2 : Package package_just_child{tx_child};
- - - - ]
596 [ + - + - ]: 1 : const auto result_just_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_just_child, /*test_accept=*/false, /*client_maxfeerate=*/{});
597 [ + - - + ]: 1 : if (auto err_parent_child{CheckPackageMempoolAcceptResult(package_just_child, result_just_child, /*expect_valid=*/true, nullptr)}) {
598 [ # # # # : 0 : BOOST_ERROR(err_parent_child.value());
# # ]
599 : : } else {
600 : 1 : auto it_child = result_just_child.m_tx_results.find(tx_child->GetWitnessHash());
601 [ + - + - : 2 : BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(), it_child->second.m_state.ToString());
+ - ]
602 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_effective_feerate.value().GetFee(GetVirtualTransactionSize(*tx_child)) == high_fee);
+ - + - +
- + - ]
603 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().size(), 1);
- + + - ]
604 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child->second.m_wtxids_fee_calculations.value().front(), tx_child->GetWitnessHash());
+ - ]
605 : 0 : }
606 : 1 : expected_pool_size += 1;
607 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
608 : :
609 : : // Too-low fee to RBF tx_single
610 [ + - + - ]: 2 : auto mtx_single_low_fee = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
611 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
612 : : /*output_destination=*/single_locking_script,
613 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN - 1), /*submit=*/false);
614 [ + - ]: 1 : CTransactionRef tx_single_low_fee = MakeTransactionRef(mtx_single_low_fee);
615 [ + + + - : 2 : Package package_tx_single_low_fee{tx_single_low_fee};
- - - - ]
616 [ + - ]: 1 : const auto result_single_tx_low_fee = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
617 [ + - + - ]: 1 : package_tx_single_low_fee, /*test_accept=*/false, /*client_maxfeerate=*/{});
618 : :
619 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
620 : :
621 [ + - + - : 2 : BOOST_CHECK(!result_single_tx_low_fee.m_state.IsValid());
+ - ]
622 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_single_tx_low_fee.m_state.GetResult(), PackageValidationResult::PCKG_TX);
623 : 1 : auto it_low_fee = result_single_tx_low_fee.m_tx_results.find(tx_single_low_fee->GetWitnessHash());
624 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_low_fee->second.m_state.GetResult(), TxValidationResult::TX_RECONSIDERABLE);
625 [ + - - + ]: 1 : if (auto err_single{CheckPackageMempoolAcceptResult(package_tx_single_low_fee, result_single_tx_low_fee, /*expect_valid=*/false, m_node.mempool.get())}) {
626 [ # # # # : 0 : BOOST_ERROR(err_single.value());
# # ]
627 : 0 : }
628 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
629 [ + - - + : 18 : }
+ - - + +
- - + + -
- + + - +
- + - + -
+ - ]
630 : :
631 : : // Tests for packages containing transactions that have same-txid-different-witness equivalents in
632 : : // the mempool.
633 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_witness_swap_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
634 : : {
635 : : // Mine blocks to mature coinbases.
636 : 1 : mineBlocks(5);
637 : 1 : MockMempoolMinFee(CFeeRate(5000));
638 : 1 : LOCK(cs_main);
639 : :
640 : : // Transactions with a same-txid-different-witness transaction in the mempool should be ignored,
641 : : // and the mempool entry's wtxid returned.
642 [ + - + - ]: 1 : CScript witnessScript = CScript() << OP_DROP << OP_TRUE;
643 [ + - + - ]: 1 : CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
644 [ + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
645 [ + - ]: 1 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
646 : : /*output_destination=*/scriptPubKey,
647 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/false);
648 [ + - ]: 1 : CTransactionRef ptx_parent = MakeTransactionRef(mtx_parent);
649 : :
650 : : // Make two children with the same txid but different witnesses.
651 : 1 : CScriptWitness witness1;
652 [ + - ]: 1 : witness1.stack.emplace_back(1);
653 [ - + + - ]: 1 : witness1.stack.emplace_back(witnessScript.begin(), witnessScript.end());
654 : :
655 [ + - ]: 1 : CScriptWitness witness2(witness1);
656 [ + - ]: 1 : witness2.stack.emplace_back(2);
657 [ - + + - ]: 1 : witness2.stack.emplace_back(witnessScript.begin(), witnessScript.end());
658 : :
659 : 1 : CKey child_key = GenerateRandomKey();
660 [ + - + - : 1 : CScript child_locking_script = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
+ - ]
661 [ + - ]: 1 : CMutableTransaction mtx_child1;
662 : 1 : mtx_child1.version = 1;
663 [ + - ]: 1 : mtx_child1.vin.resize(1);
664 : 1 : mtx_child1.vin[0].prevout.hash = ptx_parent->GetHash();
665 : 1 : mtx_child1.vin[0].prevout.n = 0;
666 : 1 : mtx_child1.vin[0].scriptSig = CScript();
667 [ + - ]: 1 : mtx_child1.vin[0].scriptWitness = witness1;
668 [ + - ]: 1 : mtx_child1.vout.resize(1);
669 : 1 : mtx_child1.vout[0].nValue = CAmount(48 * COIN);
670 : 1 : mtx_child1.vout[0].scriptPubKey = child_locking_script;
671 : :
672 [ + - ]: 1 : CMutableTransaction mtx_child2{mtx_child1};
673 [ + - ]: 1 : mtx_child2.vin[0].scriptWitness = witness2;
674 : :
675 [ + - ]: 1 : CTransactionRef ptx_child1 = MakeTransactionRef(mtx_child1);
676 [ + - ]: 1 : CTransactionRef ptx_child2 = MakeTransactionRef(mtx_child2);
677 : :
678 : : // child1 and child2 have the same txid
679 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ptx_child1->GetHash(), ptx_child2->GetHash());
680 : : // child1 and child2 have different wtxids
681 [ + - + - : 2 : BOOST_CHECK(ptx_child1->GetWitnessHash() != ptx_child2->GetWitnessHash());
+ - ]
682 : : // Check that they have different package hashes
683 [ + - + - : 8 : BOOST_CHECK(GetPackageHash({ptx_parent, ptx_child1}) != GetPackageHash({ptx_parent, ptx_child2}));
+ - + - +
- + + + -
+ + + - -
- - - - -
- - ]
684 : :
685 : : // Try submitting Package1{parent, child1} and Package2{parent, child2} where the children are
686 : : // same-txid-different-witness.
687 : 1 : {
688 [ + + + - : 3 : Package package_parent_child1{ptx_parent, ptx_child1};
- - - - ]
689 [ + - ]: 1 : const auto submit_witness1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
690 [ + - + - ]: 1 : package_parent_child1, /*test_accept=*/false, /*client_maxfeerate=*/{});
691 [ + - - + ]: 1 : if (auto err_witness1{CheckPackageMempoolAcceptResult(package_parent_child1, submit_witness1, /*expect_valid=*/true, m_node.mempool.get())}) {
692 [ # # # # : 0 : BOOST_ERROR(err_witness1.value());
# # ]
693 : 0 : }
694 : :
695 : : // Child2 would have been validated individually.
696 [ + + + - : 3 : Package package_parent_child2{ptx_parent, ptx_child2};
- - - - ]
697 [ + - ]: 1 : const auto submit_witness2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
698 [ + - + - ]: 1 : package_parent_child2, /*test_accept=*/false, /*client_maxfeerate=*/{});
699 [ + - - + ]: 1 : if (auto err_witness2{CheckPackageMempoolAcceptResult(package_parent_child2, submit_witness2, /*expect_valid=*/true, m_node.mempool.get())}) {
700 [ # # # # : 0 : BOOST_ERROR(err_witness2.value());
# # ]
701 : : } else {
702 : 1 : auto it_parent2_deduped = submit_witness2.m_tx_results.find(ptx_parent->GetWitnessHash());
703 : 1 : auto it_child2 = submit_witness2.m_tx_results.find(ptx_child2->GetWitnessHash());
704 [ + - + - : 2 : BOOST_CHECK(it_parent2_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
+ - ]
705 [ + - + - : 2 : BOOST_CHECK(it_child2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
+ - ]
706 [ + - + - : 1 : BOOST_CHECK_EQUAL(ptx_child1->GetWitnessHash(), it_child2->second.m_other_wtxid.value());
+ - ]
707 : 0 : }
708 : :
709 : : // Deduplication should work when wtxid != txid. Submit package with the already-in-mempool
710 : : // transactions again, which should not fail.
711 [ + - ]: 1 : const auto submit_segwit_dedup = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
712 [ + - + - ]: 1 : package_parent_child1, /*test_accept=*/false, /*client_maxfeerate=*/{});
713 [ + - - + ]: 1 : if (auto err_segwit_dedup{CheckPackageMempoolAcceptResult(package_parent_child1, submit_segwit_dedup, /*expect_valid=*/true, m_node.mempool.get())}) {
714 [ # # # # : 0 : BOOST_ERROR(err_segwit_dedup.value());
# # ]
715 : : } else {
716 : 1 : auto it_parent_dup = submit_segwit_dedup.m_tx_results.find(ptx_parent->GetWitnessHash());
717 : 1 : auto it_child_dup = submit_segwit_dedup.m_tx_results.find(ptx_child1->GetWitnessHash());
718 [ + - + - : 2 : BOOST_CHECK(it_parent_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
+ - ]
719 [ + - + - ]: 2 : BOOST_CHECK(it_child_dup->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
720 : 0 : }
721 : 1 : }
722 : :
723 : : // Try submitting Package1{child2, grandchild} where child2 is same-txid-different-witness as
724 : : // the in-mempool transaction, child1. Since child1 exists in the mempool and its outputs are
725 : : // available, child2 should be ignored and grandchild should be accepted.
726 : : //
727 : : // This tests a potential censorship vector in which an attacker broadcasts a competing package
728 : : // where a parent's witness is mutated. The honest package should be accepted despite the fact
729 : : // that we don't allow witness replacement.
730 : 1 : CKey grandchild_key = GenerateRandomKey();
731 [ + - + - : 1 : CScript grandchild_locking_script = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
+ - ]
732 [ + - ]: 2 : auto mtx_grandchild = CreateValidMempoolTransaction(/*input_transaction=*/ptx_child2, /*input_vout=*/0,
733 : : /*input_height=*/0, /*input_signing_key=*/child_key,
734 : : /*output_destination=*/grandchild_locking_script,
735 [ + - ]: 2 : /*output_amount=*/CAmount(47 * COIN), /*submit=*/false);
736 [ + - ]: 1 : CTransactionRef ptx_grandchild = MakeTransactionRef(mtx_grandchild);
737 : : // Check that they have different package hashes
738 [ + - + - : 8 : BOOST_CHECK(GetPackageHash({ptx_child1, ptx_grandchild}) != GetPackageHash({ptx_child2, ptx_grandchild}));
+ - + - +
- + + + -
+ + + - -
- - - - -
- - ]
739 : : // We already submitted child1 above.
740 : 1 : {
741 [ + + + - : 3 : Package package_child2_grandchild{ptx_child2, ptx_grandchild};
- - - - ]
742 [ + - ]: 1 : const auto submit_spend_ignored = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
743 [ + - + - ]: 1 : package_child2_grandchild, /*test_accept=*/false, /*client_maxfeerate=*/{});
744 [ + - - + ]: 1 : if (auto err_spend_ignored{CheckPackageMempoolAcceptResult(package_child2_grandchild, submit_spend_ignored, /*expect_valid=*/true, m_node.mempool.get())}) {
745 [ # # # # : 0 : BOOST_ERROR(err_spend_ignored.value());
# # ]
746 : : } else {
747 : 1 : auto it_child2_ignored = submit_spend_ignored.m_tx_results.find(ptx_child2->GetWitnessHash());
748 : 1 : auto it_grandchild = submit_spend_ignored.m_tx_results.find(ptx_grandchild->GetWitnessHash());
749 [ + - + - : 2 : BOOST_CHECK(it_child2_ignored->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
+ - ]
750 [ + - + - ]: 2 : BOOST_CHECK(it_grandchild->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
751 : 0 : }
752 : 1 : }
753 : :
754 : : // A package Package{parent1, parent2, parent3, child} where the parents are a mixture of
755 : : // identical-tx-in-mempool, same-txid-different-witness-in-mempool, and new transactions.
756 : 1 : Package package_mixed;
757 : :
758 : : // Give all the parents anyone-can-spend scripts so we don't have to deal with signing the child.
759 [ + - ]: 1 : CScript acs_script = CScript() << OP_TRUE;
760 [ + - + - ]: 1 : CScript acs_spk = GetScriptForDestination(WitnessV0ScriptHash(acs_script));
761 : 1 : CScriptWitness acs_witness;
762 [ - + + - ]: 1 : acs_witness.stack.emplace_back(acs_script.begin(), acs_script.end());
763 : :
764 : : // parent1 will already be in the mempool
765 [ + - + - ]: 2 : auto mtx_parent1 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
766 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
767 : : /*output_destination=*/acs_spk,
768 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
769 [ + - ]: 1 : CTransactionRef ptx_parent1 = MakeTransactionRef(mtx_parent1);
770 [ + - ]: 1 : package_mixed.push_back(ptx_parent1);
771 : :
772 : : // parent2 will have a same-txid-different-witness tx already in the mempool
773 [ + - + - ]: 1 : CScript grandparent2_script = CScript() << OP_DROP << OP_TRUE;
774 [ + - + - ]: 1 : CScript grandparent2_spk = GetScriptForDestination(WitnessV0ScriptHash(grandparent2_script));
775 : 1 : CScriptWitness parent2_witness1;
776 [ + - ]: 1 : parent2_witness1.stack.emplace_back(1);
777 [ - + + - ]: 1 : parent2_witness1.stack.emplace_back(grandparent2_script.begin(), grandparent2_script.end());
778 : 1 : CScriptWitness parent2_witness2;
779 [ + - ]: 1 : parent2_witness2.stack.emplace_back(2);
780 [ - + + - ]: 1 : parent2_witness2.stack.emplace_back(grandparent2_script.begin(), grandparent2_script.end());
781 : :
782 : : // Create grandparent2 creating an output with multiple spending paths. Submit to mempool.
783 [ + - + - ]: 2 : auto mtx_grandparent2 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
784 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
785 : : /*output_destination=*/grandparent2_spk,
786 [ + - ]: 2 : /*output_amount=*/CAmount(49 * COIN), /*submit=*/true);
787 [ + - ]: 1 : CTransactionRef ptx_grandparent2 = MakeTransactionRef(mtx_grandparent2);
788 : :
789 [ + - ]: 1 : CMutableTransaction mtx_parent2_v1;
790 : 1 : mtx_parent2_v1.version = 1;
791 [ + - ]: 1 : mtx_parent2_v1.vin.resize(1);
792 : 1 : mtx_parent2_v1.vin[0].prevout.hash = ptx_grandparent2->GetHash();
793 : 1 : mtx_parent2_v1.vin[0].prevout.n = 0;
794 : 1 : mtx_parent2_v1.vin[0].scriptSig = CScript();
795 [ + - ]: 1 : mtx_parent2_v1.vin[0].scriptWitness = parent2_witness1;
796 [ + - ]: 1 : mtx_parent2_v1.vout.resize(1);
797 : 1 : mtx_parent2_v1.vout[0].nValue = CAmount(48 * COIN);
798 : 1 : mtx_parent2_v1.vout[0].scriptPubKey = acs_spk;
799 : :
800 [ + - ]: 1 : CMutableTransaction mtx_parent2_v2{mtx_parent2_v1};
801 [ + - ]: 1 : mtx_parent2_v2.vin[0].scriptWitness = parent2_witness2;
802 : :
803 [ + - ]: 1 : CTransactionRef ptx_parent2_v1 = MakeTransactionRef(mtx_parent2_v1);
804 [ + - ]: 1 : CTransactionRef ptx_parent2_v2 = MakeTransactionRef(mtx_parent2_v2);
805 : : // Put parent2_v1 in the package, submit parent2_v2 to the mempool.
806 [ + - ]: 1 : const MempoolAcceptResult parent2_v2_result = m_node.chainman->ProcessTransaction(ptx_parent2_v2);
807 [ + - + - : 2 : BOOST_CHECK(parent2_v2_result.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
808 [ + - ]: 1 : package_mixed.push_back(ptx_parent2_v1);
809 : :
810 : : // parent3 will be a new transaction. Put a low feerate to make it invalid on its own.
811 [ + - + - ]: 2 : auto mtx_parent3 = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[3], /*input_vout=*/0,
812 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
813 : : /*output_destination=*/acs_spk,
814 [ + - ]: 2 : /*output_amount=*/CAmount(50 * COIN - low_fee_amt), /*submit=*/false);
815 [ + - ]: 1 : CTransactionRef ptx_parent3 = MakeTransactionRef(mtx_parent3);
816 [ + - ]: 1 : package_mixed.push_back(ptx_parent3);
817 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*ptx_parent3)) > low_fee_amt);
+ - + - +
- + - ]
818 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->m_opts.min_relay_feerate.GetFee(GetVirtualTransactionSize(*ptx_parent3)) <= low_fee_amt);
+ - + - ]
819 : :
820 : : // child spends parent1, parent2, and parent3
821 : 1 : CKey mixed_grandchild_key = GenerateRandomKey();
822 [ + - + - : 1 : CScript mixed_child_spk = GetScriptForDestination(WitnessV0KeyHash(mixed_grandchild_key.GetPubKey()));
+ - ]
823 : :
824 [ + - ]: 1 : CMutableTransaction mtx_mixed_child;
825 [ + - ]: 1 : mtx_mixed_child.vin.emplace_back(COutPoint(ptx_parent1->GetHash(), 0));
826 [ + - ]: 1 : mtx_mixed_child.vin.emplace_back(COutPoint(ptx_parent2_v1->GetHash(), 0));
827 [ + - ]: 1 : mtx_mixed_child.vin.emplace_back(COutPoint(ptx_parent3->GetHash(), 0));
828 [ + - ]: 1 : mtx_mixed_child.vin[0].scriptWitness = acs_witness;
829 [ + - ]: 1 : mtx_mixed_child.vin[1].scriptWitness = acs_witness;
830 [ + - ]: 1 : mtx_mixed_child.vin[2].scriptWitness = acs_witness;
831 [ + - ]: 1 : mtx_mixed_child.vout.emplace_back((48 + 49 + 50 - 1) * COIN, mixed_child_spk);
832 [ + - ]: 1 : CTransactionRef ptx_mixed_child = MakeTransactionRef(mtx_mixed_child);
833 [ + - ]: 1 : package_mixed.push_back(ptx_mixed_child);
834 : :
835 : : // Submit package:
836 : : // parent1 should be ignored
837 : : // parent2_v1 should be ignored (and v2 wtxid returned)
838 : : // parent3 should be accepted
839 : : // child should be accepted
840 : 1 : {
841 [ + - + - ]: 1 : const auto mixed_result = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_mixed, false, /*client_maxfeerate=*/{});
842 [ + - - + ]: 1 : if (auto err_mixed{CheckPackageMempoolAcceptResult(package_mixed, mixed_result, /*expect_valid=*/true, m_node.mempool.get())}) {
843 [ # # # # : 0 : BOOST_ERROR(err_mixed.value());
# # ]
844 : : } else {
845 : 1 : auto it_parent1 = mixed_result.m_tx_results.find(ptx_parent1->GetWitnessHash());
846 : 1 : auto it_parent2 = mixed_result.m_tx_results.find(ptx_parent2_v1->GetWitnessHash());
847 : 1 : auto it_parent3 = mixed_result.m_tx_results.find(ptx_parent3->GetWitnessHash());
848 : 1 : auto it_child = mixed_result.m_tx_results.find(ptx_mixed_child->GetWitnessHash());
849 : :
850 [ + - + - : 2 : BOOST_CHECK(it_parent1->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
+ - ]
851 [ + - + - : 2 : BOOST_CHECK(it_parent2->second.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS);
+ - ]
852 [ + - + - : 2 : BOOST_CHECK(it_parent3->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
853 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
854 [ + - + - : 1 : BOOST_CHECK_EQUAL(ptx_parent2_v2->GetWitnessHash(), it_parent2->second.m_other_wtxid.value());
+ - ]
855 : :
856 : : // package feerate should include parent3 and child. It should not include parent1 or parent2_v1.
857 [ + - + - : 1 : const CFeeRate expected_feerate(1 * COIN, GetVirtualTransactionSize(*ptx_parent3) + GetVirtualTransactionSize(*ptx_mixed_child));
+ - ]
858 [ + - + - : 2 : BOOST_CHECK(it_parent3->second.m_effective_feerate.value() == expected_feerate);
+ - + - ]
859 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_effective_feerate.value() == expected_feerate);
+ - + - ]
860 [ + - ]: 1 : std::vector<Wtxid> expected_wtxids({ptx_parent3->GetWitnessHash(), ptx_mixed_child->GetWitnessHash()});
861 [ + - + - : 2 : BOOST_CHECK(it_parent3->second.m_wtxids_fee_calculations.value() == expected_wtxids);
+ - + - ]
862 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_wtxids_fee_calculations.value() == expected_wtxids);
+ - ]
863 : 1 : }
864 [ + - ]: 1 : }
865 [ + - + - : 42 : }
+ - + - +
- + - + -
+ - - + +
- + - - +
+ - + - +
- + - + -
+ - + - +
- - + + -
+ - + - +
- + - + -
+ - + - +
- + - ]
866 : :
867 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_cpfp_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
868 : : {
869 : 1 : mineBlocks(5);
870 : 1 : MockMempoolMinFee(CFeeRate(5000));
871 : 1 : LOCK(::cs_main);
872 [ + - ]: 1 : size_t expected_pool_size = m_node.mempool->size();
873 : 1 : CKey child_key = GenerateRandomKey();
874 [ + - + - : 1 : CScript parent_spk = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
+ - ]
875 : 1 : CKey grandchild_key = GenerateRandomKey();
876 [ + - + - : 1 : CScript child_spk = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
+ - ]
877 : :
878 : : // low-fee parent and high-fee child package
879 : 1 : const CAmount coinbase_value{50 * COIN};
880 : 1 : const CAmount parent_value{coinbase_value - low_fee_amt};
881 : 1 : const CAmount child_value{parent_value - COIN};
882 : :
883 : 1 : Package package_cpfp;
884 [ + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
885 [ + - ]: 1 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
886 : : /*output_destination=*/parent_spk,
887 [ + - ]: 2 : /*output_amount=*/parent_value, /*submit=*/false);
888 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
889 [ + - ]: 1 : package_cpfp.push_back(tx_parent);
890 : :
891 [ + - ]: 2 : auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent, /*input_vout=*/0,
892 : : /*input_height=*/101, /*input_signing_key=*/child_key,
893 : : /*output_destination=*/child_spk,
894 [ + - ]: 2 : /*output_amount=*/child_value, /*submit=*/false);
895 [ + - ]: 1 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
896 [ + - ]: 1 : package_cpfp.push_back(tx_child);
897 : :
898 : : // Package feerate is calculated using modified fees, and prioritisetransaction accepts negative
899 : : // fee deltas. This should be taken into account. De-prioritise the parent transaction
900 : : // to bring the package feerate to 0.
901 [ + - ]: 1 : m_node.mempool->PrioritiseTransaction(tx_parent->GetHash(), child_value - coinbase_value);
902 : 1 : {
903 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
904 [ + - ]: 1 : const auto submit_cpfp_deprio = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
905 [ + - + - ]: 1 : package_cpfp, /*test_accept=*/ false, /*client_maxfeerate=*/{});
906 [ + - - + ]: 1 : if (auto err_cpfp_deprio{CheckPackageMempoolAcceptResult(package_cpfp, submit_cpfp_deprio, /*expect_valid=*/false, m_node.mempool.get())}) {
907 [ # # # # : 0 : BOOST_ERROR(err_cpfp_deprio.value());
# # ]
908 : : } else {
909 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_state.GetResult(), PackageValidationResult::PCKG_TX);
910 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_tx_results.find(tx_parent->GetWitnessHash())->second.m_state.GetResult(),
911 : : TxValidationResult::TX_MEMPOOL_POLICY);
912 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(submit_cpfp_deprio.m_tx_results.find(tx_child->GetWitnessHash())->second.m_state.GetResult(),
913 : : TxValidationResult::TX_MISSING_INPUTS);
914 [ + - - + : 3 : BOOST_CHECK(submit_cpfp_deprio.m_tx_results.find(tx_parent->GetWitnessHash())->second.m_state.GetRejectReason() == "min relay fee not met");
+ - + - ]
915 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
916 : 0 : }
917 : 1 : }
918 : :
919 : : // Clear the prioritisation of the parent transaction.
920 [ + - ]: 3 : WITH_LOCK(m_node.mempool->cs, m_node.mempool->ClearPrioritisation(tx_parent->GetHash()));
921 : :
922 : : // Package CPFP: Even though the parent's feerate is below the mempool minimum feerate, the
923 : : // child pays enough for the package feerate to meet the threshold.
924 : 1 : {
925 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
926 [ + - ]: 1 : const auto submit_cpfp = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
927 [ + - + - ]: 1 : package_cpfp, /*test_accept=*/ false, /*client_maxfeerate=*/{});
928 [ + - - + ]: 1 : if (auto err_cpfp{CheckPackageMempoolAcceptResult(package_cpfp, submit_cpfp, /*expect_valid=*/true, m_node.mempool.get())}) {
929 [ # # # # : 0 : BOOST_ERROR(err_cpfp.value());
# # ]
930 : : } else {
931 : 1 : auto it_parent = submit_cpfp.m_tx_results.find(tx_parent->GetWitnessHash());
932 : 1 : auto it_child = submit_cpfp.m_tx_results.find(tx_child->GetWitnessHash());
933 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
934 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_base_fees.value() == coinbase_value - parent_value);
+ - + - ]
935 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
936 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_base_fees.value() == COIN);
+ - + - ]
937 : :
938 [ + - ]: 1 : const CFeeRate expected_feerate(coinbase_value - child_value,
939 [ + - + - : 1 : GetVirtualTransactionSize(*tx_parent) + GetVirtualTransactionSize(*tx_child));
+ - ]
940 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_effective_feerate.value() == expected_feerate);
+ - + - ]
941 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_effective_feerate.value() == expected_feerate);
+ - + - ]
942 [ + - ]: 1 : std::vector<Wtxid> expected_wtxids({tx_parent->GetWitnessHash(), tx_child->GetWitnessHash()});
943 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_wtxids_fee_calculations.value() == expected_wtxids);
+ - + - ]
944 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_wtxids_fee_calculations.value() == expected_wtxids);
+ - + - ]
945 [ + - + - ]: 2 : BOOST_CHECK(expected_feerate.GetFeePerK() > 1000);
946 : 1 : }
947 : 1 : expected_pool_size += 2;
948 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
949 : 1 : }
950 : :
951 : : // Just because we allow low-fee parents doesn't mean we allow low-feerate packages.
952 : : // The mempool minimum feerate is 5sat/vB, but this package just pays 800 satoshis total.
953 : : // The child fees would be able to pay for itself, but isn't enough for the entire package.
954 : 1 : Package package_still_too_low;
955 : 1 : const CAmount parent_fee{200};
956 : 1 : const CAmount child_fee{600};
957 [ + - + - ]: 2 : auto mtx_parent_cheap = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[1], /*input_vout=*/0,
958 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
959 : : /*output_destination=*/parent_spk,
960 [ + - ]: 2 : /*output_amount=*/coinbase_value - parent_fee, /*submit=*/false);
961 [ + - ]: 1 : CTransactionRef tx_parent_cheap = MakeTransactionRef(mtx_parent_cheap);
962 [ + - ]: 1 : package_still_too_low.push_back(tx_parent_cheap);
963 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*tx_parent_cheap)) > parent_fee);
+ - + - +
- + - ]
964 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->m_opts.min_relay_feerate.GetFee(GetVirtualTransactionSize(*tx_parent_cheap)) <= parent_fee);
+ - + - ]
965 : :
966 [ + - ]: 2 : auto mtx_child_cheap = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_cheap, /*input_vout=*/0,
967 : : /*input_height=*/101, /*input_signing_key=*/child_key,
968 : : /*output_destination=*/child_spk,
969 [ + - ]: 2 : /*output_amount=*/coinbase_value - parent_fee - child_fee, /*submit=*/false);
970 [ + - ]: 1 : CTransactionRef tx_child_cheap = MakeTransactionRef(mtx_child_cheap);
971 [ + - ]: 1 : package_still_too_low.push_back(tx_child_cheap);
972 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*tx_child_cheap)) <= child_fee);
+ - + - +
- + - ]
973 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->GetMinFee().GetFee(GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap)) > parent_fee + child_fee);
+ - + - +
- + - +
- ]
974 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
975 : :
976 : : // Cheap package should fail for being too low fee.
977 : 1 : {
978 [ + - ]: 1 : const auto submit_package_too_low = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
979 [ + - + - ]: 1 : package_still_too_low, /*test_accept=*/false, /*client_maxfeerate=*/{});
980 [ + - - + ]: 1 : if (auto err_package_too_low{CheckPackageMempoolAcceptResult(package_still_too_low, submit_package_too_low, /*expect_valid=*/false, m_node.mempool.get())}) {
981 [ # # # # : 0 : BOOST_ERROR(err_package_too_low.value());
# # ]
982 : : } else {
983 : : // Individual feerate of parent is too low.
984 [ + - + - : 1 : BOOST_CHECK_EQUAL(submit_package_too_low.m_tx_results.at(tx_parent_cheap->GetWitnessHash()).m_state.GetResult(),
+ - ]
985 : : TxValidationResult::TX_RECONSIDERABLE);
986 [ + - + - : 2 : BOOST_CHECK(submit_package_too_low.m_tx_results.at(tx_parent_cheap->GetWitnessHash()).m_effective_feerate.value() ==
+ - + - +
- + - +
- ]
987 : : CFeeRate(parent_fee, GetVirtualTransactionSize(*tx_parent_cheap)));
988 : : // Package feerate of parent + child is too low.
989 [ + - + - : 1 : BOOST_CHECK_EQUAL(submit_package_too_low.m_tx_results.at(tx_child_cheap->GetWitnessHash()).m_state.GetResult(),
+ - ]
990 : : TxValidationResult::TX_RECONSIDERABLE);
991 [ + - + - : 2 : BOOST_CHECK(submit_package_too_low.m_tx_results.at(tx_child_cheap->GetWitnessHash()).m_effective_feerate.value() ==
+ - + - +
- + - +
- ]
992 : : CFeeRate(parent_fee + child_fee, GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap)));
993 : 0 : }
994 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetResult(), PackageValidationResult::PCKG_TX);
995 [ + - - + : 2 : BOOST_CHECK_EQUAL(submit_package_too_low.m_state.GetRejectReason(), "transaction failed");
+ - ]
996 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
997 : 1 : }
998 : :
999 : : // Package feerate includes the modified fees of the transactions.
1000 : : // This means a child with its fee delta from prioritisetransaction can pay for a parent.
1001 [ + - ]: 1 : m_node.mempool->PrioritiseTransaction(tx_child_cheap->GetHash(), 1 * COIN);
1002 : : // Now that the child's fees have "increased" by 1 BTC, the cheap package should succeed.
1003 : 1 : {
1004 [ + - ]: 1 : const auto submit_prioritised_package = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
1005 [ + - + - ]: 1 : package_still_too_low, /*test_accept=*/false, /*client_maxfeerate=*/{});
1006 [ + - - + ]: 1 : if (auto err_prioritised{CheckPackageMempoolAcceptResult(package_still_too_low, submit_prioritised_package, /*expect_valid=*/true, m_node.mempool.get())}) {
1007 [ # # # # : 0 : BOOST_ERROR(err_prioritised.value());
# # ]
1008 : : } else {
1009 [ + - ]: 1 : const CFeeRate expected_feerate(1 * COIN + parent_fee + child_fee,
1010 [ + - + - : 1 : GetVirtualTransactionSize(*tx_parent_cheap) + GetVirtualTransactionSize(*tx_child_cheap));
+ - ]
1011 [ + - - + : 1 : BOOST_CHECK_EQUAL(submit_prioritised_package.m_tx_results.size(), package_still_too_low.size());
+ - ]
1012 : 1 : auto it_parent = submit_prioritised_package.m_tx_results.find(tx_parent_cheap->GetWitnessHash());
1013 : 1 : auto it_child = submit_prioritised_package.m_tx_results.find(tx_child_cheap->GetWitnessHash());
1014 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
1015 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_base_fees.value() == parent_fee);
+ - + - ]
1016 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_effective_feerate.value() == expected_feerate);
+ - + - ]
1017 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
1018 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_base_fees.value() == child_fee);
+ - + - ]
1019 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_effective_feerate.value() == expected_feerate);
+ - + - ]
1020 [ + - ]: 1 : std::vector<Wtxid> expected_wtxids({tx_parent_cheap->GetWitnessHash(), tx_child_cheap->GetWitnessHash()});
1021 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_wtxids_fee_calculations.value() == expected_wtxids);
+ - + - ]
1022 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_wtxids_fee_calculations.value() == expected_wtxids);
+ - ]
1023 : 1 : }
1024 : 1 : expected_pool_size += 2;
1025 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1026 : 1 : }
1027 : :
1028 : : // Package feerate is calculated without topology in mind; it's just aggregating fees and sizes.
1029 : : // However, this should not allow parents to pay for children. Each transaction should be
1030 : : // validated individually first, eliminating sufficient-feerate parents before they are unfairly
1031 : : // included in the package feerate. It's also important that the low-fee child doesn't prevent
1032 : : // the parent from being accepted.
1033 : 1 : Package package_rich_parent;
1034 : 1 : const CAmount high_parent_fee{1 * COIN};
1035 [ + - + - ]: 2 : auto mtx_parent_rich = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[2], /*input_vout=*/0,
1036 : : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
1037 : : /*output_destination=*/parent_spk,
1038 [ + - ]: 2 : /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
1039 [ + - ]: 1 : CTransactionRef tx_parent_rich = MakeTransactionRef(mtx_parent_rich);
1040 [ + - ]: 1 : package_rich_parent.push_back(tx_parent_rich);
1041 : :
1042 [ + - ]: 2 : auto mtx_child_poor = CreateValidMempoolTransaction(/*input_transaction=*/tx_parent_rich, /*input_vout=*/0,
1043 : : /*input_height=*/101, /*input_signing_key=*/child_key,
1044 : : /*output_destination=*/child_spk,
1045 [ + - ]: 2 : /*output_amount=*/coinbase_value - high_parent_fee, /*submit=*/false);
1046 [ + - ]: 1 : CTransactionRef tx_child_poor = MakeTransactionRef(mtx_child_poor);
1047 [ + - ]: 1 : package_rich_parent.push_back(tx_child_poor);
1048 : :
1049 : : // Parent pays 1 BTC and child pays none. The parent should be accepted without the child.
1050 : 1 : {
1051 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1052 [ + - ]: 1 : const auto submit_rich_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
1053 [ + - + - ]: 1 : package_rich_parent, /*test_accept=*/false, /*client_maxfeerate=*/{});
1054 [ + - - + ]: 1 : if (auto err_rich_parent{CheckPackageMempoolAcceptResult(package_rich_parent, submit_rich_parent, /*expect_valid=*/false, m_node.mempool.get())}) {
1055 [ # # # # : 0 : BOOST_ERROR(err_rich_parent.value());
# # ]
1056 : : } else {
1057 : : // The child would have been validated on its own and failed.
1058 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetResult(), PackageValidationResult::PCKG_TX);
1059 [ + - - + : 2 : BOOST_CHECK_EQUAL(submit_rich_parent.m_state.GetRejectReason(), "transaction failed");
+ - ]
1060 : :
1061 : 1 : auto it_parent = submit_rich_parent.m_tx_results.find(tx_parent_rich->GetWitnessHash());
1062 : 1 : auto it_child = submit_rich_parent.m_tx_results.find(tx_child_poor->GetWitnessHash());
1063 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_result_type == MempoolAcceptResult::ResultType::VALID);
+ - ]
1064 [ + - + - : 2 : BOOST_CHECK(it_child->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
+ - ]
1065 [ + - - + : 3 : BOOST_CHECK(it_parent->second.m_state.GetRejectReason() == "");
+ - + - ]
1066 [ + - + - : 2 : BOOST_CHECK_MESSAGE(it_parent->second.m_base_fees.value() == high_parent_fee,
+ - + - +
- ]
1067 : : strprintf("rich parent: expected fee %s, got %s", high_parent_fee, it_parent->second.m_base_fees.value()));
1068 [ + - + - : 2 : BOOST_CHECK(it_parent->second.m_effective_feerate == CFeeRate(high_parent_fee, GetVirtualTransactionSize(*tx_parent_rich)));
+ - + - +
- ]
1069 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child->second.m_result_type, MempoolAcceptResult::ResultType::INVALID);
1070 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child->second.m_state.GetResult(), TxValidationResult::TX_MEMPOOL_POLICY);
1071 [ + - - + : 3 : BOOST_CHECK(it_child->second.m_state.GetRejectReason() == "min relay fee not met");
+ - ]
1072 : 0 : }
1073 : 1 : expected_pool_size += 1;
1074 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1075 [ + - ]: 1 : }
1076 [ + - + - : 13 : }
+ - + - +
- + - ]
1077 : :
1078 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(package_rbf_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
1079 : : {
1080 : 1 : mineBlocks(5);
1081 : 1 : LOCK(::cs_main);
1082 [ + - ]: 1 : size_t expected_pool_size = m_node.mempool->size();
1083 : 1 : CKey child_key{GenerateRandomKey()};
1084 [ + - + - : 1 : CScript parent_spk = GetScriptForDestination(WitnessV0KeyHash(child_key.GetPubKey()));
+ - ]
1085 : 1 : CKey grandchild_key{GenerateRandomKey()};
1086 [ + - + - : 1 : CScript child_spk = GetScriptForDestination(WitnessV0KeyHash(grandchild_key.GetPubKey()));
+ - ]
1087 : :
1088 : 1 : const CAmount coinbase_value{50 * COIN};
1089 : : // Test that de-duplication works. This is not actually package rbf.
1090 : 1 : {
1091 : : // 1 parent paying 200sat, 1 child paying 300sat
1092 : 1 : Package package1;
1093 : : // 1 parent paying 200sat, 1 child paying 500sat
1094 : 1 : Package package2;
1095 : : // Package1 and package2 have the same parent. The children conflict.
1096 [ + - ]: 2 : auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/m_coinbase_txns[0], /*input_vout=*/0,
1097 [ + - ]: 1 : /*input_height=*/0, /*input_signing_key=*/coinbaseKey,
1098 : : /*output_destination=*/parent_spk,
1099 [ + - ]: 2 : /*output_amount=*/coinbase_value - low_fee_amt, /*submit=*/false);
1100 [ + - ]: 1 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
1101 [ + - ]: 1 : package1.push_back(tx_parent);
1102 [ + - ]: 1 : package2.push_back(tx_parent);
1103 : :
1104 [ + - + - : 5 : CTransactionRef tx_child_1 = MakeTransactionRef(CreateValidMempoolTransaction(tx_parent, 0, 101, child_key, child_spk, coinbase_value - low_fee_amt - 300, false));
+ - ]
1105 [ + - ]: 1 : package1.push_back(tx_child_1);
1106 [ + - + - : 5 : CTransactionRef tx_child_2 = MakeTransactionRef(CreateValidMempoolTransaction(tx_parent, 0, 101, child_key, child_spk, coinbase_value - low_fee_amt - 500, false));
+ - ]
1107 [ + - ]: 1 : package2.push_back(tx_child_2);
1108 : :
1109 [ + - ]: 1 : LOCK(m_node.mempool->cs);
1110 [ + - + - ]: 1 : const auto submit1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, /*test_accept=*/false, std::nullopt);
1111 [ + - - + ]: 1 : if (auto err_1{CheckPackageMempoolAcceptResult(package1, submit1, /*expect_valid=*/true, m_node.mempool.get())}) {
1112 [ # # # # : 0 : BOOST_ERROR(err_1.value());
# # ]
1113 : 0 : }
1114 : :
1115 : : // Check precise ResultTypes and mempool size. We know it_parent_1 and it_child_1 exist from above call
1116 : 1 : auto it_parent_1 = submit1.m_tx_results.find(tx_parent->GetWitnessHash());
1117 : 1 : auto it_child_1 = submit1.m_tx_results.find(tx_child_1->GetWitnessHash());
1118 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1119 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1120 : 1 : expected_pool_size += 2;
1121 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1122 : :
1123 [ + - + - ]: 1 : const auto submit2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package2, /*test_accept=*/false, std::nullopt);
1124 [ + - - + ]: 1 : if (auto err_2{CheckPackageMempoolAcceptResult(package2, submit2, /*expect_valid=*/true, m_node.mempool.get())}) {
1125 [ # # # # : 0 : BOOST_ERROR(err_2.value());
# # ]
1126 : 0 : }
1127 : :
1128 : : // Check precise ResultTypes and mempool size. We know it_parent_2 and it_child_2 exist from above call
1129 : 1 : auto it_parent_2 = submit2.m_tx_results.find(tx_parent->GetWitnessHash());
1130 : 1 : auto it_child_2 = submit2.m_tx_results.find(tx_child_2->GetWitnessHash());
1131 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent_2->second.m_result_type, MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
1132 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child_2->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1133 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1134 : :
1135 : : // child1 has been replaced
1136 [ + - + - : 2 : BOOST_CHECK(!m_node.mempool->exists(tx_child_1->GetHash()));
+ - ]
1137 [ + - + - : 6 : }
+ - + - ]
1138 : :
1139 : : // Test package rbf.
1140 : 1 : {
1141 [ + - + - : 4 : CTransactionRef tx_parent_1 = MakeTransactionRef(CreateValidMempoolTransaction(
+ - ]
1142 [ + - ]: 1 : m_coinbase_txns[1], /*input_vout=*/0, /*input_height=*/0,
1143 : 1 : coinbaseKey, parent_spk, coinbase_value - 200, /*submit=*/false));
1144 [ + - + - : 4 : CTransactionRef tx_child_1 = MakeTransactionRef(CreateValidMempoolTransaction(
+ - ]
1145 : : tx_parent_1, /*input_vout=*/0, /*input_height=*/101,
1146 : 1 : child_key, child_spk, coinbase_value - 400, /*submit=*/false));
1147 : :
1148 [ + - + - : 4 : CTransactionRef tx_parent_2 = MakeTransactionRef(CreateValidMempoolTransaction(
+ - ]
1149 [ + - ]: 1 : m_coinbase_txns[1], /*input_vout=*/0, /*input_height=*/0,
1150 : 1 : coinbaseKey, parent_spk, coinbase_value - 800, /*submit=*/false));
1151 [ + - + - : 4 : CTransactionRef tx_child_2 = MakeTransactionRef(CreateValidMempoolTransaction(
+ - ]
1152 : : tx_parent_2, /*input_vout=*/0, /*input_height=*/101,
1153 : 1 : child_key, child_spk, coinbase_value - 800 - 200, /*submit=*/false));
1154 : :
1155 [ + - + - : 4 : CTransactionRef tx_parent_3 = MakeTransactionRef(CreateValidMempoolTransaction(
+ - ]
1156 [ + - ]: 1 : m_coinbase_txns[1], /*input_vout=*/0, /*input_height=*/0,
1157 : 1 : coinbaseKey, parent_spk, coinbase_value - 199, /*submit=*/false));
1158 [ + - + - : 4 : CTransactionRef tx_child_3 = MakeTransactionRef(CreateValidMempoolTransaction(
+ - ]
1159 : : tx_parent_3, /*input_vout=*/0, /*input_height=*/101,
1160 : 1 : child_key, child_spk, coinbase_value - 199 - 1300, /*submit=*/false));
1161 : :
1162 : : // In all packages, the parents conflict with each other
1163 [ + - + - : 2 : BOOST_CHECK(tx_parent_1->GetHash() != tx_parent_2->GetHash() && tx_parent_2->GetHash() != tx_parent_3->GetHash());
- + + - +
- ]
1164 : :
1165 : : // 1 parent paying 200sat, 1 child paying 200sat.
1166 [ + + + - : 3 : Package package1{tx_parent_1, tx_child_1};
- - - - ]
1167 : : // 1 parent paying 800sat, 1 child paying 200sat.
1168 [ + + + - : 3 : Package package2{tx_parent_2, tx_child_2};
- - - - ]
1169 : : // 1 parent paying 199sat, 1 child paying 1300sat.
1170 [ + + + - : 3 : Package package3{tx_parent_3, tx_child_3};
- - - - ]
1171 : :
1172 [ + - + - ]: 1 : const auto submit1 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, false, std::nullopt);
1173 [ + - - + ]: 1 : if (auto err_1{CheckPackageMempoolAcceptResult(package1, submit1, /*expect_valid=*/true, m_node.mempool.get())}) {
1174 [ # # # # : 0 : BOOST_ERROR(err_1.value());
# # ]
1175 : 0 : }
1176 : 1 : auto it_parent_1 = submit1.m_tx_results.find(tx_parent_1->GetWitnessHash());
1177 : 1 : auto it_child_1 = submit1.m_tx_results.find(tx_child_1->GetWitnessHash());
1178 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1179 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1180 : 1 : expected_pool_size += 2;
1181 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1182 : :
1183 : : // This replacement is actually not package rbf; the parent carries enough fees
1184 : : // to replace the entire package on its own.
1185 [ + - + - ]: 1 : const auto submit2 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package2, false, std::nullopt);
1186 [ + - - + ]: 1 : if (auto err_2{CheckPackageMempoolAcceptResult(package2, submit2, /*expect_valid=*/true, m_node.mempool.get())}) {
1187 [ # # # # : 0 : BOOST_ERROR(err_2.value());
# # ]
1188 : 0 : }
1189 : 1 : auto it_parent_2 = submit2.m_tx_results.find(tx_parent_2->GetWitnessHash());
1190 : 1 : auto it_child_2 = submit2.m_tx_results.find(tx_child_2->GetWitnessHash());
1191 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent_2->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1192 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child_2->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1193 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1194 : :
1195 : : // Package RBF, in which the replacement transaction's child sponsors the fees to meet RBF feerate rules
1196 [ + - + - ]: 1 : const auto submit3 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package3, false, std::nullopt);
1197 [ + - - + ]: 1 : if (auto err_3{CheckPackageMempoolAcceptResult(package3, submit3, /*expect_valid=*/true, m_node.mempool.get())}) {
1198 [ # # # # : 0 : BOOST_ERROR(err_3.value());
# # ]
1199 : 0 : }
1200 : 1 : auto it_parent_3 = submit3.m_tx_results.find(tx_parent_3->GetWitnessHash());
1201 : 1 : auto it_child_3 = submit3.m_tx_results.find(tx_child_3->GetWitnessHash());
1202 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent_3->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1203 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child_3->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1204 : :
1205 : : // package3 was considered as a package to replace both package2 transactions
1206 [ + - + - : 2 : BOOST_CHECK(it_parent_3->second.m_replaced_transactions.size() == 2);
+ - ]
1207 [ + - + - : 2 : BOOST_CHECK(it_child_3->second.m_replaced_transactions.empty());
+ - ]
1208 : :
1209 [ + - ]: 1 : std::vector<Wtxid> expected_package3_wtxids({tx_parent_3->GetWitnessHash(), tx_child_3->GetWitnessHash()});
1210 [ + - + - ]: 1 : const auto package3_total_vsize{GetVirtualTransactionSize(*tx_parent_3) + GetVirtualTransactionSize(*tx_child_3)};
1211 [ + - + - : 2 : BOOST_CHECK(it_parent_3->second.m_wtxids_fee_calculations.value() == expected_package3_wtxids);
+ - + - ]
1212 [ + - + - : 2 : BOOST_CHECK(it_child_3->second.m_wtxids_fee_calculations.value() == expected_package3_wtxids);
+ - + - ]
1213 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_parent_3->second.m_effective_feerate.value().GetFee(package3_total_vsize), 199 + 1300);
+ - + - ]
1214 [ + - + - : 1 : BOOST_CHECK_EQUAL(it_child_3->second.m_effective_feerate.value().GetFee(package3_total_vsize), 199 + 1300);
+ - + - ]
1215 : :
1216 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
+ - ]
1217 : :
1218 : : // Finally, check that we can prioritise tx_child_1 to get package1 into the mempool.
1219 : : // It should not be possible to resubmit package1 and get it in without prioritisation.
1220 [ + - + - ]: 1 : const auto submit4 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, false, std::nullopt);
1221 [ + - - + ]: 1 : if (auto err_4{CheckPackageMempoolAcceptResult(package1, submit4, /*expect_valid=*/false, m_node.mempool.get())}) {
1222 [ # # # # : 0 : BOOST_ERROR(err_4.value());
# # ]
1223 : 0 : }
1224 [ + - ]: 1 : m_node.mempool->PrioritiseTransaction(tx_child_1->GetHash(), 1363);
1225 [ + - + - ]: 1 : const auto submit5 = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package1, false, std::nullopt);
1226 [ + - - + ]: 1 : if (auto err_5{CheckPackageMempoolAcceptResult(package1, submit5, /*expect_valid=*/true, m_node.mempool.get())}) {
1227 [ # # # # : 0 : BOOST_ERROR(err_5.value());
# # ]
1228 : 0 : }
1229 : 1 : it_parent_1 = submit5.m_tx_results.find(tx_parent_1->GetWitnessHash());
1230 : 1 : it_child_1 = submit5.m_tx_results.find(tx_child_1->GetWitnessHash());
1231 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_parent_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1232 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(it_child_1->second.m_result_type, MempoolAcceptResult::ResultType::VALID);
1233 [ + - ]: 1 : LOCK(m_node.mempool->cs);
1234 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->GetIter(tx_parent_1->GetHash()).has_value());
+ - + - ]
1235 [ + - + - : 2 : BOOST_CHECK(m_node.mempool->GetIter(tx_child_1->GetHash()).has_value());
+ - + - ]
1236 [ + - + - : 7 : }
+ - + - +
- + - ]
1237 [ + - + - : 11 : }
- + + - +
- - + + -
+ - - + +
- ]
1238 : : BOOST_AUTO_TEST_SUITE_END()
|