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 : : #include <common/system.h>
5 : : #include <policy/rbf.h>
6 : : #include <random.h>
7 : : #include <test/util/txmempool.h>
8 : : #include <txmempool.h>
9 : : #include <util/time.h>
10 : :
11 : : #include <test/util/setup_common.h>
12 : :
13 : : #include <boost/test/unit_test.hpp>
14 : : #include <optional>
15 : : #include <vector>
16 : :
17 : : BOOST_FIXTURE_TEST_SUITE(rbf_tests, TestingSetup)
18 : :
19 : 1150 : static inline CTransactionRef make_tx(const std::vector<CTransactionRef>& inputs,
20 : : const std::vector<CAmount>& output_values)
21 : : {
22 : 1150 : CMutableTransaction tx = CMutableTransaction();
23 [ - + + - ]: 1150 : tx.vin.resize(inputs.size());
24 [ - + + - ]: 1150 : tx.vout.resize(output_values.size());
25 [ - + + + ]: 2303 : for (size_t i = 0; i < inputs.size(); ++i) {
26 [ + - ]: 1153 : tx.vin[i].prevout.hash = inputs[i]->GetHash();
27 : 1153 : tx.vin[i].prevout.n = 0;
28 : : // Add a witness so wtxid != txid
29 : 1153 : CScriptWitness witness;
30 [ + - ]: 1153 : witness.stack.emplace_back(i + 10);
31 [ + - ]: 1153 : tx.vin[i].scriptWitness = witness;
32 : 1153 : }
33 [ - + + + ]: 2401 : for (size_t i = 0; i < output_values.size(); ++i) {
34 [ + - + - ]: 1251 : tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
35 : 1251 : tx.vout[i].nValue = output_values[i];
36 : : }
37 [ + - ]: 2300 : return MakeTransactionRef(tx);
38 : 1150 : }
39 : :
40 : 102 : static CTransactionRef add_descendants(const CTransactionRef& tx, int32_t num_descendants, CTxMemPool& pool)
41 : : EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
42 : : {
43 : 102 : AssertLockHeld(::cs_main);
44 : 102 : AssertLockHeld(pool.cs);
45 : 102 : TestMemPoolEntryHelper entry;
46 : : // Assumes this isn't already spent in mempool
47 [ + - ]: 102 : auto tx_to_spend = tx;
48 [ + + ]: 1122 : for (int32_t i{0}; i < num_descendants; ++i) {
49 [ + - + - : 4080 : auto next_tx = make_tx(/*inputs=*/{tx_to_spend}, /*output_values=*/{(50 - i) * CENT});
+ + + - +
- - - -
- ]
50 [ + - + - ]: 1020 : TryAddToMempool(pool, entry.FromTx(next_tx));
51 [ + - + - : 2040 : BOOST_CHECK(pool.GetIter(next_tx->GetHash()).has_value());
+ - ]
52 [ + - ]: 1020 : tx_to_spend = next_tx;
53 : 1020 : }
54 : : // Return last created tx
55 : 102 : return tx_to_spend;
56 [ + - + - ]: 2040 : }
57 : :
58 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
59 : : {
60 [ - + ]: 1 : CTxMemPool& pool = *Assert(m_node.mempool);
61 [ + - ]: 1 : LOCK2(::cs_main, pool.cs);
62 : 1 : TestMemPoolEntryHelper entry;
63 : :
64 : 1 : const CAmount low_fee{CENT/100};
65 : 1 : const CAmount normal_fee{CENT/10};
66 : 1 : const CAmount high_fee{CENT};
67 : :
68 : : // Create a parent tx1 and child tx2 with normal fees:
69 [ + - + - : 4 : const auto tx1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
70 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx1));
71 [ + - + - : 4 : const auto tx2 = make_tx(/*inputs=*/ {tx1}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
72 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx2));
73 : :
74 : : // Create a low-feerate parent tx3 and high-feerate child tx4 (cpfp)
75 [ + - + - : 4 : const auto tx3 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ {1099 * CENT});
+ - + + +
- + - - -
- - ]
76 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx3));
77 [ + - + - : 4 : const auto tx4 = make_tx(/*inputs=*/ {tx3}, /*output_values=*/ {999 * CENT});
+ + + - +
- - - -
- ]
78 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(high_fee).FromTx(tx4));
79 : :
80 : : // Create a parent tx5 and child tx6 where both have very low fees
81 [ + - + - : 4 : const auto tx5 = make_tx(/*inputs=*/ {m_coinbase_txns[2]}, /*output_values=*/ {1099 * CENT});
+ - + + +
- + - - -
- - ]
82 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx5));
83 [ + - + - : 4 : const auto tx6 = make_tx(/*inputs=*/ {tx5}, /*output_values=*/ {1098 * CENT});
+ + + - +
- - - -
- ]
84 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx6));
85 : : // Make tx6's modified fee much higher than its base fee. This should cause it to pass
86 : : // the fee-related checks despite being low-feerate.
87 [ + - ]: 1 : pool.PrioritiseTransaction(tx6->GetHash(), 1 * COIN);
88 : :
89 : : // Two independent high-feerate transactions, tx7 and tx8
90 [ + - + - : 4 : const auto tx7 = make_tx(/*inputs=*/ {m_coinbase_txns[3]}, /*output_values=*/ {999 * CENT});
+ - + + +
- + - - -
- - ]
91 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(high_fee).FromTx(tx7));
92 [ + - + - : 4 : const auto tx8 = make_tx(/*inputs=*/ {m_coinbase_txns[4]}, /*output_values=*/ {999 * CENT});
+ - + + +
- + - - -
- - ]
93 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(high_fee).FromTx(tx8));
94 : :
95 : : // Will make these two parents of single child
96 [ + - + - : 4 : const auto tx11 = make_tx(/*inputs=*/ {m_coinbase_txns[7]}, /*output_values=*/ {995 * CENT});
+ - + + +
- + - - -
- - ]
97 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx11));
98 [ + - + - : 4 : const auto tx12 = make_tx(/*inputs=*/ {m_coinbase_txns[8]}, /*output_values=*/ {995 * CENT});
+ - + + +
- + - - -
- - ]
99 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx12));
100 : :
101 : : // Will make two children of this single parent
102 [ + - + - : 4 : const auto tx13 = make_tx(/*inputs=*/ {m_coinbase_txns[9]}, /*output_values=*/ {995 * CENT, 995 * CENT});
+ - + + +
- + - - -
- - ]
103 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx13));
104 : :
105 [ + - ]: 1 : const auto entry1_normal = pool.GetIter(tx1->GetHash()).value();
106 [ + - ]: 1 : const auto entry2_normal = pool.GetIter(tx2->GetHash()).value();
107 [ + - ]: 1 : const auto entry3_low = pool.GetIter(tx3->GetHash()).value();
108 [ + - ]: 1 : const auto entry4_high = pool.GetIter(tx4->GetHash()).value();
109 [ + - ]: 1 : const auto entry5_low = pool.GetIter(tx5->GetHash()).value();
110 [ + - ]: 1 : const auto entry6_low_prioritised = pool.GetIter(tx6->GetHash()).value();
111 [ + - ]: 1 : const auto entry7_high = pool.GetIter(tx7->GetHash()).value();
112 [ + - ]: 1 : const auto entry8_high = pool.GetIter(tx8->GetHash()).value();
113 : :
114 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry1_normal->GetFee(), normal_fee);
115 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry2_normal->GetFee(), normal_fee);
116 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry3_low->GetFee(), low_fee);
117 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry4_high->GetFee(), high_fee);
118 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry5_low->GetFee(), low_fee);
119 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry6_low_prioritised->GetFee(), low_fee);
120 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry7_high->GetFee(), high_fee);
121 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(entry8_high->GetFee(), high_fee);
122 : :
123 [ + - ]: 1 : CTxMemPool::setEntries set_12_normal{entry1_normal, entry2_normal};
124 [ + - ]: 1 : CTxMemPool::setEntries set_34_cpfp{entry3_low, entry4_high};
125 [ + - ]: 1 : CTxMemPool::setEntries set_56_low{entry5_low, entry6_low_prioritised};
126 [ + - ]: 1 : CTxMemPool::setEntries set_78_high{entry7_high, entry8_high};
127 : 1 : CTxMemPool::setEntries all_entries{entry1_normal, entry2_normal, entry3_low, entry4_high,
128 [ + - ]: 1 : entry5_low, entry6_low_prioritised, entry7_high, entry8_high};
129 : 1 : CTxMemPool::setEntries empty_set;
130 : :
131 [ + - ]: 1 : const auto unused_txid = Txid::FromUint256(GetRandHash());
132 : :
133 : : // Tests for EntriesAndTxidsDisjoint
134 [ + - + - : 2 : BOOST_CHECK(EntriesAndTxidsDisjoint(empty_set, {tx1->GetHash()}, unused_txid) == std::nullopt);
+ - + - +
- ]
135 [ + - + - : 2 : BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx3->GetHash()}, unused_txid) == std::nullopt);
+ - + - +
- ]
136 [ + - + - : 2 : BOOST_CHECK(EntriesAndTxidsDisjoint({entry2_normal}, {tx2->GetHash()}, unused_txid).has_value());
+ - + - +
- + - ]
137 [ + - + - : 2 : BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx1->GetHash()}, unused_txid).has_value());
+ - + - +
- ]
138 [ + - + - : 2 : BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx2->GetHash()}, unused_txid).has_value());
+ - + - +
- ]
139 : : // EntriesAndTxidsDisjoint does not calculate descendants of iters_conflicting; it uses whatever
140 : : // the caller passed in. As such, no error is returned even though entry2_normal is a descendant of tx1.
141 [ + - + - : 2 : BOOST_CHECK(EntriesAndTxidsDisjoint({entry2_normal}, {tx1->GetHash()}, unused_txid) == std::nullopt);
+ - + - +
- + - ]
142 : :
143 : : // Tests for PaysForRBF
144 [ + - ]: 1 : const CFeeRate incremental_relay_feerate{DEFAULT_INCREMENTAL_RELAY_FEE};
145 : 1 : const CFeeRate higher_relay_feerate{2 * DEFAULT_INCREMENTAL_RELAY_FEE};
146 : : // Must pay at least as much as the original.
147 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(/*original_fees=*/high_fee,
+ - + - ]
148 : : /*replacement_fees=*/high_fee,
149 : : /*replacement_vsize=*/1,
150 : : /*relay_fee=*/CFeeRate(0),
151 : : /*txid=*/unused_txid)
152 : : == std::nullopt);
153 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(high_fee, high_fee - 1, 1, CFeeRate(0), unused_txid).has_value());
+ - + - ]
154 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(high_fee + 1, high_fee, 1, CFeeRate(0), unused_txid).has_value());
+ - + - ]
155 : : // Additional fees must cover the replacement's vsize at incremental relay fee
156 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(high_fee, high_fee + 1, 11, incremental_relay_feerate, unused_txid).has_value());
+ - + - ]
157 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(high_fee, high_fee + 1, 10, incremental_relay_feerate, unused_txid) == std::nullopt);
+ - + - ]
158 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(high_fee, high_fee + 2, 11, higher_relay_feerate, unused_txid).has_value());
+ - + - ]
159 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(high_fee, high_fee + 4, 20, higher_relay_feerate, unused_txid) == std::nullopt);
+ - + - ]
160 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(low_fee, high_fee, 99999999, incremental_relay_feerate, unused_txid).has_value());
+ - + - ]
161 [ + - + - : 2 : BOOST_CHECK(PaysForRBF(low_fee, high_fee + 99999999, 99999999, incremental_relay_feerate, unused_txid) == std::nullopt);
+ - ]
162 [ + - + - : 28 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
163 : :
164 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(rbf_conflicts_calculator, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
165 : : {
166 [ - + ]: 1 : CTxMemPool& pool = *Assert(m_node.mempool);
167 [ + - ]: 1 : LOCK2(::cs_main, pool.cs);
168 : 1 : TestMemPoolEntryHelper entry;
169 : :
170 : 1 : const CAmount normal_fee{CENT/10};
171 : :
172 : : // Create two parent transactions with 51 outputs each
173 : 1 : const int NUM_OUTPUTS = 51;
174 : 1 : std::vector<CAmount> output_values;
175 [ + - ]: 1 : output_values.reserve(NUM_OUTPUTS);
176 [ + + ]: 52 : for (int i = 0; i < NUM_OUTPUTS; ++i) {
177 [ + - ]: 51 : output_values.push_back(1 * COIN);
178 : : }
179 : :
180 [ + - + - : 3 : const auto parent_tx_1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ output_values);
+ + + - -
- - - ]
181 [ + - + - : 3 : const auto parent_tx_2 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ output_values);
+ + + - -
- - - ]
182 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(parent_tx_1));
183 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(parent_tx_2));
184 : :
185 : 1 : std::vector<CTransactionRef> direct_children;
186 : :
187 : : // Create individual spends of these outputs
188 [ + - + - : 9 : for (const auto& parent_tx : {parent_tx_1, parent_tx_2}) {
+ + + - -
- ]
189 [ + + ]: 104 : for (auto i = 0; i < NUM_OUTPUTS; ++i) {
190 [ + - + - : 408 : auto pretx = make_tx(/*inputs=*/ {parent_tx}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
191 [ + - ]: 102 : CMutableTransaction tx(*pretx);
192 [ + - ]: 102 : tx.vin[0].prevout.n = i;
193 [ + - + - ]: 102 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx));
194 [ + - + - : 204 : BOOST_CHECK(pool.GetIter(tx.GetHash()).has_value());
+ - + - +
- ]
195 [ + - + - : 204 : direct_children.push_back(MakeTransactionRef(tx));
- + ]
196 [ + - ]: 204 : }
197 [ + + - - ]: 3 : }
198 : :
199 : : // At this point, we should have 2 clusters in the mempool, each with 52
200 : : // transactions.
201 : :
202 : : // parent_tx and all children are in one cluster, so we can have as many
203 : : // conflicts within this cluster as we want without violating the RBF conflicts
204 : : // limit.
205 [ + - ]: 1 : const auto parent_entry_1 = pool.GetIter(parent_tx_1->GetHash()).value();
206 [ + - ]: 1 : const auto parent_entry_2 = pool.GetIter(parent_tx_2->GetHash()).value();
207 [ + - + - : 5 : const auto conflicting_transaction = make_tx({parent_tx_1, parent_tx_2}, {50 * CENT});
+ + + - +
- - - -
- ]
208 [ + - ]: 1 : CTxMemPool::setEntries all_conflicts, dummy;
209 [ + - + - : 2 : BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
+ - ]
210 : : /*pool=*/ pool,
211 : : /*iters_conflicting=*/ {parent_entry_1, parent_entry_2},
212 : : /*all_conflicts=*/ all_conflicts) == std::nullopt);
213 : :
214 : 1 : dummy.clear();
215 : : // Conflicting directly with all those conflicts doesn't change anything.
216 [ + - + - : 2 : BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
+ - + - ]
217 : : /*pool=*/ pool,
218 : : /*iters_conflicting=*/ all_conflicts,
219 : : /*all_conflicts=*/ dummy) == std::nullopt);
220 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(all_conflicts.size(), dummy.size());
221 : 1 : dummy.clear();
222 : :
223 : : // If we mine the parent_tx's, then the clusters split (102 clusters).
224 [ + - + + : 3 : pool.removeForBlock({parent_tx_1, parent_tx_2}, /* dummy */ 1);
+ - - - -
- ]
225 : :
226 : : // Add some descendants now to each of the direct children (we can do this now that the clusters have split).
227 [ + + ]: 103 : for (const auto& child : direct_children) {
228 [ + - ]: 204 : add_descendants(child, 10, pool);
229 : : }
230 : :
231 : : // We can conflict with 100 different clusters, even if they have lots of transactions.
232 : 1 : CTxMemPool::setEntries conflicts;
233 [ + + ]: 101 : for (auto i = 0; i < 100; ++i) {
234 [ + - + - ]: 200 : conflicts.insert(pool.GetIter(direct_children[i]->GetHash()).value());
235 : : }
236 [ + - + - : 2 : BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
+ - + - ]
237 : : /*pool=*/ pool,
238 : : /*iters_conflicting=*/ conflicts,
239 : : /*all_conflicts=*/ dummy) == std::nullopt);
240 : :
241 : : // Conflicting with 1 more distinct cluster causes failure, however.
242 [ + - + - ]: 2 : conflicts.insert(pool.GetIter(direct_children[100]->GetHash()).value());
243 [ + - + - : 2 : BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
+ - ]
244 : : /*pool=*/ pool,
245 : : /*iters_conflicting=*/ conflicts,
246 : : /*all_conflicts=*/ dummy).has_value());
247 [ + - + - : 218 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
248 : :
249 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(improves_feerate, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
250 : : {
251 [ - + ]: 1 : CTxMemPool& pool = *Assert(m_node.mempool);
252 [ + - ]: 1 : LOCK2(::cs_main, pool.cs);
253 : 1 : TestMemPoolEntryHelper entry;
254 : :
255 : 1 : const CAmount low_fee{CENT/100};
256 : 1 : const CAmount normal_fee{CENT/10};
257 : :
258 : : // low feerate parent with normal feerate child
259 [ + - + - : 5 : const auto tx1 = make_tx(/*inputs=*/ {m_coinbase_txns[0], m_coinbase_txns[1]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
260 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx1));
261 [ + - + - : 4 : const auto tx2 = make_tx(/*inputs=*/ {tx1}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
262 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx2));
263 : :
264 [ + - ]: 1 : const auto entry1 = pool.GetIter(tx1->GetHash()).value();
265 [ + - ]: 1 : const auto tx1_fee = entry1->GetModifiedFee();
266 [ + - ]: 1 : const auto entry2 = pool.GetIter(tx2->GetHash()).value();
267 [ + - ]: 1 : const auto tx2_fee = entry2->GetModifiedFee();
268 : :
269 : : // conflicting transactions
270 [ + - + - : 5 : const auto tx1_conflict = make_tx(/*inputs=*/ {m_coinbase_txns[0], m_coinbase_txns[2]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
271 [ + - + - : 4 : const auto tx3 = make_tx(/*inputs=*/ {tx1_conflict}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
272 [ + - ]: 1 : auto entry3 = entry.FromTx(tx3);
273 : :
274 : : // Now test ImprovesFeerateDiagram with various levels of "package rbf" feerates
275 : :
276 : : // It doesn't improve itself
277 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
278 [ + - ]: 1 : changeset->StageRemoval(entry1);
279 [ + - ]: 1 : changeset->StageRemoval(entry2);
280 [ + - ]: 1 : changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0, false, 4, LockPoints());
281 [ + - ]: 1 : changeset->StageAddition(tx3, tx2_fee, 0, 1, 0, false, 4, LockPoints());
282 [ + - ]: 1 : const auto res1 = ImprovesFeerateDiagram(*changeset);
283 [ + - + - : 2 : BOOST_CHECK(res1.has_value());
+ - ]
284 [ + - + - : 2 : BOOST_CHECK(res1.value().first == DiagramCheckError::FAILURE);
+ - + - ]
285 [ + - + - : 2 : BOOST_CHECK(res1.value().second == "insufficient feerate: does not improve feerate diagram");
+ - + - ]
286 : :
287 : : // With one more satoshi it does
288 [ + - ]: 1 : changeset.reset();
289 [ + - ]: 2 : changeset = pool.GetChangeSet();
290 [ + - ]: 1 : changeset->StageRemoval(entry1);
291 [ + - ]: 1 : changeset->StageRemoval(entry2);
292 [ + - ]: 1 : changeset->StageAddition(tx1_conflict, tx1_fee+1, 0, 1, 0, false, 4, LockPoints());
293 [ + - ]: 1 : changeset->StageAddition(tx3, tx2_fee, 0, 1, 0, false, 4, LockPoints());
294 [ + - + - : 2 : BOOST_CHECK(ImprovesFeerateDiagram(*changeset) == std::nullopt);
+ - + - ]
295 : :
296 [ + - ]: 1 : changeset.reset();
297 : : // With prioritisation of in-mempool conflicts, it affects the results of the comparison using the same args as just above
298 [ + - + - ]: 2 : pool.PrioritiseTransaction(entry1->GetSharedTx()->GetHash(), /*nFeeDelta=*/1);
299 [ + - ]: 2 : changeset = pool.GetChangeSet();
300 [ + - ]: 1 : changeset->StageRemoval(entry1);
301 [ + - ]: 1 : changeset->StageRemoval(entry2);
302 [ + - ]: 1 : changeset->StageAddition(tx1_conflict, tx1_fee+1, 0, 1, 0, false, 4, LockPoints());
303 [ + - ]: 1 : changeset->StageAddition(tx3, tx2_fee, 0, 1, 0, false, 4, LockPoints());
304 [ + - ]: 1 : const auto res2 = ImprovesFeerateDiagram(*changeset);
305 [ + - + - : 2 : BOOST_CHECK(res2.has_value());
+ - ]
306 [ + - + - : 2 : BOOST_CHECK(res2.value().first == DiagramCheckError::FAILURE);
+ - + - ]
307 [ + - + - : 2 : BOOST_CHECK(res2.value().second == "insufficient feerate: does not improve feerate diagram");
+ - + - ]
308 [ + - ]: 1 : changeset.reset();
309 : :
310 [ + - + - ]: 2 : pool.PrioritiseTransaction(entry1->GetSharedTx()->GetHash(), /*nFeeDelta=*/-1);
311 : :
312 : : // With fewer vbytes it does
313 [ + - ]: 1 : CMutableTransaction tx4{entry3.GetTx()};
314 : 1 : tx4.vin[0].scriptWitness = CScriptWitness(); // Clear out the witness, to reduce size
315 [ + - + - ]: 2 : auto entry4 = entry.FromTx(MakeTransactionRef(tx4));
316 [ + - ]: 2 : changeset = pool.GetChangeSet();
317 [ + - ]: 1 : changeset->StageRemoval(entry1);
318 [ + - ]: 1 : changeset->StageRemoval(entry2);
319 [ + - ]: 1 : changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0, false, 4, LockPoints());
320 [ + - + - ]: 2 : changeset->StageAddition(entry4.GetSharedTx(), tx2_fee, 0, 1, 0, false, 4, LockPoints());
321 [ + - + - : 2 : BOOST_CHECK(ImprovesFeerateDiagram(*changeset) == std::nullopt);
+ - + - ]
322 [ + - ]: 1 : changeset.reset();
323 : :
324 : : // Adding a grandchild makes the cluster size 3, which is also calculable
325 [ + - + - : 4 : const auto tx5 = make_tx(/*inputs=*/ {tx2}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
326 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx5));
327 [ + - ]: 1 : const auto entry5 = pool.GetIter(tx5->GetHash()).value();
328 : :
329 [ + - ]: 2 : changeset = pool.GetChangeSet();
330 [ + - ]: 1 : changeset->StageRemoval(entry1);
331 [ + - ]: 1 : changeset->StageRemoval(entry2);
332 [ + - ]: 1 : changeset->StageRemoval(entry5);
333 [ + - ]: 1 : changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0, false, 4, LockPoints());
334 [ + - + - ]: 2 : changeset->StageAddition(entry4.GetSharedTx(), tx2_fee + entry5->GetModifiedFee() + 1, 0, 1, 0, false, 4, LockPoints());
335 [ + - ]: 1 : const auto res3 = ImprovesFeerateDiagram(*changeset);
336 [ + - + - ]: 2 : BOOST_CHECK(res3 == std::nullopt);
337 [ + - + - : 19 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
338 : :
339 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
340 : : {
341 [ - + ]: 1 : CTxMemPool& pool = *Assert(m_node.mempool);
342 [ + - ]: 1 : LOCK2(::cs_main, pool.cs);
343 : 1 : TestMemPoolEntryHelper entry;
344 : :
345 : 1 : const CAmount low_fee{CENT/100};
346 : 1 : const CAmount high_fee{CENT};
347 : :
348 : : // low -> high -> medium fee transactions that would result in two chunks together since they
349 : : // are all same size
350 [ + - + - : 4 : const auto low_tx = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
351 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(low_tx));
352 : :
353 [ + - ]: 1 : const auto entry_low = pool.GetIter(low_tx->GetHash()).value();
354 [ + - ]: 1 : const auto low_size = entry_low->GetAdjustedWeight();
355 : :
356 [ + - + - : 4 : const auto replacement_tx = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {9 * COIN});
+ - + + +
- + - - -
- - ]
357 [ + - ]: 1 : auto entry_replacement = entry.FromTx(replacement_tx);
358 : :
359 : : // Replacement of size 1
360 : 1 : {
361 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
362 [ + - ]: 1 : changeset->StageRemoval(entry_low);
363 [ + - ]: 1 : changeset->StageAddition(replacement_tx, 0, 0, 1, 0, false, 4, LockPoints());
364 [ + - ]: 1 : const auto replace_one{changeset->CalculateChunksForRBF()};
365 [ + - + - : 2 : BOOST_CHECK(replace_one.has_value());
+ - ]
366 [ + - ]: 1 : std::vector<FeeFrac> expected_old_chunks{{low_fee, low_size}};
367 [ + - + - : 2 : BOOST_CHECK(replace_one->first == expected_old_chunks);
+ - ]
368 [ + - + - ]: 1 : std::vector<FeeFrac> expected_new_chunks{{0, entry_replacement.GetAdjustedWeight()}};
369 [ + - + - ]: 2 : BOOST_CHECK(replace_one->second == expected_new_chunks);
370 : 1 : }
371 : :
372 : : // Non-zero replacement fee/size
373 : 1 : {
374 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
375 [ + - ]: 1 : changeset->StageRemoval(entry_low);
376 [ + - ]: 1 : changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
377 [ + - ]: 1 : const auto replace_one_fee{changeset->CalculateChunksForRBF()};
378 [ + - + - : 2 : BOOST_CHECK(replace_one_fee.has_value());
+ - ]
379 [ + - ]: 1 : std::vector<FeeFrac> expected_old_diagram{{low_fee, low_size}};
380 [ + - + - : 2 : BOOST_CHECK(replace_one_fee->first == expected_old_diagram);
+ - ]
381 [ + - + - ]: 1 : std::vector<FeeFrac> expected_new_diagram{{high_fee, entry_replacement.GetAdjustedWeight()}};
382 [ + - + - ]: 2 : BOOST_CHECK(replace_one_fee->second == expected_new_diagram);
383 : 1 : }
384 : :
385 : : // Add a second transaction to the cluster that will make a single chunk, to be evicted in the RBF
386 [ + - + - : 4 : const auto high_tx = make_tx(/*inputs=*/ {low_tx}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
387 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(high_fee).FromTx(high_tx));
388 [ + - ]: 1 : const auto entry_high = pool.GetIter(high_tx->GetHash()).value();
389 [ + - ]: 1 : const auto high_size = entry_high->GetAdjustedWeight();
390 : :
391 : 1 : {
392 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
393 [ + - ]: 1 : changeset->StageRemoval(entry_low);
394 [ + - ]: 1 : changeset->StageRemoval(entry_high);
395 [ + - ]: 1 : changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
396 [ + - ]: 1 : const auto replace_single_chunk{changeset->CalculateChunksForRBF()};
397 [ + - + - : 2 : BOOST_CHECK(replace_single_chunk.has_value());
+ - ]
398 [ + - ]: 1 : std::vector<FeeFrac> expected_old_chunks{{low_fee + high_fee, low_size + high_size}};
399 [ + - + - : 2 : BOOST_CHECK(replace_single_chunk->first == expected_old_chunks);
+ - ]
400 [ + - + - ]: 1 : std::vector<FeeFrac> expected_new_chunks{{high_fee, entry_replacement.GetAdjustedWeight()}};
401 [ + - + - ]: 2 : BOOST_CHECK(replace_single_chunk->second == expected_new_chunks);
402 : 1 : }
403 : :
404 : : // Conflict with the 2nd tx, resulting in new diagram with three entries
405 : 1 : {
406 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
407 [ + - ]: 1 : changeset->StageRemoval(entry_high);
408 [ + - ]: 1 : changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
409 [ + - ]: 1 : const auto replace_cpfp_child{changeset->CalculateChunksForRBF()};
410 [ + - + - : 2 : BOOST_CHECK(replace_cpfp_child.has_value());
+ - ]
411 [ + - ]: 1 : std::vector<FeeFrac> expected_old_chunks{{low_fee + high_fee, low_size + high_size}};
412 [ + - + - : 2 : BOOST_CHECK(replace_cpfp_child->first == expected_old_chunks);
+ - ]
413 [ + - + - ]: 1 : std::vector<FeeFrac> expected_new_chunks{{high_fee, entry_replacement.GetAdjustedWeight()}, {low_fee, low_size}};
414 [ + - + - ]: 2 : BOOST_CHECK(replace_cpfp_child->second == expected_new_chunks);
415 : 1 : }
416 : :
417 : : // Make a size 2 cluster that is itself two chunks; evict both txns
418 [ + - + - : 4 : const auto high_tx_2 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
419 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(high_fee).FromTx(high_tx_2));
420 [ + - ]: 1 : const auto entry_high_2 = pool.GetIter(high_tx_2->GetHash()).value();
421 [ + - ]: 1 : const auto high_size_2 = entry_high_2->GetAdjustedWeight();
422 : :
423 [ + - + - : 4 : const auto low_tx_2 = make_tx(/*inputs=*/ {high_tx_2}, /*output_values=*/ {9 * COIN});
+ + + - +
- - - -
- ]
424 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(low_tx_2));
425 [ + - ]: 1 : const auto entry_low_2 = pool.GetIter(low_tx_2->GetHash()).value();
426 [ + - ]: 1 : const auto low_size_2 = entry_low_2->GetAdjustedWeight();
427 : :
428 : 1 : {
429 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
430 [ + - ]: 1 : changeset->StageRemoval(entry_high_2);
431 [ + - ]: 1 : changeset->StageRemoval(entry_low_2);
432 [ + - ]: 1 : changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
433 [ + - ]: 1 : const auto replace_two_chunks_single_cluster{changeset->CalculateChunksForRBF()};
434 [ + - + - : 2 : BOOST_CHECK(replace_two_chunks_single_cluster.has_value());
+ - ]
435 [ + - ]: 1 : std::vector<FeeFrac> expected_old_chunks{{high_fee, high_size_2}, {low_fee, low_size_2}};
436 [ + - + - : 2 : BOOST_CHECK(replace_two_chunks_single_cluster->first == expected_old_chunks);
+ - ]
437 [ + - ]: 1 : std::vector<FeeFrac> expected_new_chunks{{high_fee, low_size_2}};
438 [ + - + - ]: 2 : BOOST_CHECK(replace_two_chunks_single_cluster->second == expected_new_chunks);
439 : 1 : }
440 : :
441 : : // You can have more than two direct conflicts
442 [ + - + - : 4 : const auto conflict_1 = make_tx(/*inputs=*/ {m_coinbase_txns[2]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
443 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(conflict_1));
444 [ + - ]: 1 : const auto conflict_1_entry = pool.GetIter(conflict_1->GetHash()).value();
445 : :
446 [ + - + - : 4 : const auto conflict_2 = make_tx(/*inputs=*/ {m_coinbase_txns[3]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
447 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(conflict_2));
448 [ + - ]: 1 : const auto conflict_2_entry = pool.GetIter(conflict_2->GetHash()).value();
449 : :
450 [ + - + - : 4 : const auto conflict_3 = make_tx(/*inputs=*/ {m_coinbase_txns[4]}, /*output_values=*/ {10 * COIN});
+ - + + +
- + - - -
- - ]
451 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(conflict_3));
452 [ + - ]: 1 : const auto conflict_3_entry = pool.GetIter(conflict_3->GetHash()).value();
453 : :
454 : 1 : {
455 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
456 [ + - ]: 1 : changeset->StageRemoval(conflict_1_entry);
457 [ + - ]: 1 : changeset->StageRemoval(conflict_2_entry);
458 [ + - ]: 1 : changeset->StageRemoval(conflict_3_entry);
459 [ + - ]: 1 : changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
460 [ + - ]: 1 : const auto replace_multiple_clusters{changeset->CalculateChunksForRBF()};
461 [ + - + - : 2 : BOOST_CHECK(replace_multiple_clusters.has_value());
+ - ]
462 [ + - - + : 2 : BOOST_CHECK(replace_multiple_clusters->first.size() == 3);
+ - + - ]
463 [ + - - + : 2 : BOOST_CHECK(replace_multiple_clusters->second.size() == 1);
+ - ]
464 : 1 : }
465 : :
466 : : // Add a child transaction to conflict_1 and make it cluster size 2, two chunks due to same feerate
467 [ + - + - : 4 : const auto conflict_1_child = make_tx(/*inputs=*/{conflict_1}, /*output_values=*/ {995 * CENT});
+ + + - +
- - - -
- ]
468 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(low_fee).FromTx(conflict_1_child));
469 [ + - ]: 1 : const auto conflict_1_child_entry = pool.GetIter(conflict_1_child->GetHash()).value();
470 : :
471 : 1 : {
472 [ + - ]: 1 : auto changeset = pool.GetChangeSet();
473 [ + - ]: 1 : changeset->StageRemoval(conflict_1_entry);
474 [ + - ]: 1 : changeset->StageRemoval(conflict_2_entry);
475 [ + - ]: 1 : changeset->StageRemoval(conflict_3_entry);
476 [ + - ]: 1 : changeset->StageRemoval(conflict_1_child_entry);
477 [ + - ]: 1 : changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
478 [ + - ]: 1 : const auto replace_multiple_clusters_2{changeset->CalculateChunksForRBF()};
479 : :
480 [ + - + - : 2 : BOOST_CHECK(replace_multiple_clusters_2.has_value());
+ - ]
481 [ + - - + : 2 : BOOST_CHECK(replace_multiple_clusters_2->first.size() == 4);
+ - + - ]
482 [ + - - + : 2 : BOOST_CHECK(replace_multiple_clusters_2->second.size() == 1);
+ - ]
483 [ + - ]: 1 : }
484 [ + - + - : 23 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
485 : :
486 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(feerate_chunks_utilities)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
487 : : {
488 : : // Sanity check the correctness of the feerate chunks comparison.
489 : :
490 : : // A strictly better case.
491 : 1 : std::vector<FeeFrac> old_chunks{{{950, 300}, {100, 100}}};
492 [ + - ]: 1 : std::vector<FeeFrac> new_chunks{{{1000, 300}, {50, 100}}};
493 : :
494 [ + - - + : 2 : BOOST_CHECK(std::is_lt(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
495 [ + - - + : 2 : BOOST_CHECK(std::is_gt(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
496 : :
497 : : // Incomparable diagrams
498 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 100}};
499 [ + - ]: 1 : new_chunks = {{1000, 300}, {0, 100}};
500 : :
501 [ + - - + : 3 : BOOST_CHECK(CompareChunks(old_chunks, new_chunks) == std::partial_ordering::unordered);
- + + - +
- + - ]
502 [ + - - + : 3 : BOOST_CHECK(CompareChunks(new_chunks, old_chunks) == std::partial_ordering::unordered);
- + + - +
- + - ]
503 : :
504 : : // Strictly better but smaller size.
505 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 100}};
506 [ + - ]: 1 : new_chunks = {{1100, 300}};
507 : :
508 [ + - - + : 2 : BOOST_CHECK(std::is_lt(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
509 [ + - - + : 2 : BOOST_CHECK(std::is_gt(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
510 : :
511 : : // New diagram is strictly better due to the first chunk, even though
512 : : // second chunk contributes no fees
513 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 100}};
514 [ + - ]: 1 : new_chunks = {{1100, 100}, {0, 100}};
515 : :
516 [ + - - + : 2 : BOOST_CHECK(std::is_lt(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
517 [ + - - + : 2 : BOOST_CHECK(std::is_gt(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
518 : :
519 : : // Feerate of first new chunk is better with, but second chunk is worse
520 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 100}};
521 [ + - ]: 1 : new_chunks = {{750, 100}, {249, 250}, {151, 650}};
522 : :
523 [ + - - + : 3 : BOOST_CHECK(CompareChunks(old_chunks, new_chunks) == std::partial_ordering::unordered);
- + + - +
- + - ]
524 [ + - - + : 3 : BOOST_CHECK(CompareChunks(new_chunks, old_chunks) == std::partial_ordering::unordered);
- + + - +
- + - ]
525 : :
526 : : // If we make the second chunk slightly better, the new diagram now wins.
527 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 100}};
528 [ + - ]: 1 : new_chunks = {{750, 100}, {250, 250}, {150, 150}};
529 : :
530 [ + - - + : 2 : BOOST_CHECK(std::is_lt(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
531 [ + - - + : 2 : BOOST_CHECK(std::is_gt(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
532 : :
533 : : // Identical diagrams, cannot be strictly better
534 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 100}};
535 [ + - ]: 1 : new_chunks = {{950, 300}, {100, 100}};
536 : :
537 [ + - - + : 2 : BOOST_CHECK(std::is_eq(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
538 [ + - - + : 2 : BOOST_CHECK(std::is_eq(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
539 : :
540 : : // Same aggregate fee, but different total size (trigger single tail fee check step)
541 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 99}};
542 [ + - ]: 1 : new_chunks = {{950, 300}, {100, 100}};
543 : :
544 : : // No change in evaluation when tail check needed.
545 [ + - - + : 2 : BOOST_CHECK(std::is_gt(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
546 [ + - - + : 2 : BOOST_CHECK(std::is_lt(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
547 : :
548 : : // Trigger multiple tail fee check steps
549 [ + - ]: 1 : old_chunks = {{950, 300}, {100, 99}};
550 [ + - ]: 1 : new_chunks = {{950, 300}, {100, 100}, {0, 1}, {0, 1}};
551 : :
552 [ + - - + : 2 : BOOST_CHECK(std::is_gt(CompareChunks(old_chunks, new_chunks)));
- + + - +
- + - ]
553 [ + - - + : 2 : BOOST_CHECK(std::is_lt(CompareChunks(new_chunks, old_chunks)));
- + + - +
- + - ]
554 : :
555 : : // Multiple tail fee check steps, unordered result
556 [ + - ]: 1 : new_chunks = {{950, 300}, {100, 100}, {0, 1}, {0, 1}, {1, 1}};
557 [ + - - + : 3 : BOOST_CHECK(CompareChunks(old_chunks, new_chunks) == std::partial_ordering::unordered);
- + + - +
- + - ]
558 [ + - - + : 3 : BOOST_CHECK(CompareChunks(new_chunks, old_chunks) == std::partial_ordering::unordered);
- + + - +
- ]
559 : 1 : }
560 : :
561 : : BOOST_AUTO_TEST_SUITE_END()
|