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