Bitcoin Core  26.99.0
P2P Digital Currency
rbf_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #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 static 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 
40 static void 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  pool.addUnchecked(entry.FromTx(next_tx));
51  tx_to_spend = next_tx;
52  }
53 }
54 
56 {
57  CTxMemPool& pool = *Assert(m_node.mempool);
58  LOCK2(::cs_main, pool.cs);
60 
61  const CAmount low_fee{CENT/100};
62  const CAmount normal_fee{CENT/10};
63  const CAmount high_fee{CENT};
64 
65  // Create a parent tx1 and child tx2 with normal fees:
66  const auto tx1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {10 * COIN});
67  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx1));
68  const auto tx2 = make_tx(/*inputs=*/ {tx1}, /*output_values=*/ {995 * CENT});
69  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx2));
70 
71  // Create a low-feerate parent tx3 and high-feerate child tx4 (cpfp)
72  const auto tx3 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ {1099 * CENT});
73  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx3));
74  const auto tx4 = make_tx(/*inputs=*/ {tx3}, /*output_values=*/ {999 * CENT});
75  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx4));
76 
77  // Create a parent tx5 and child tx6 where both have very low fees
78  const auto tx5 = make_tx(/*inputs=*/ {m_coinbase_txns[2]}, /*output_values=*/ {1099 * CENT});
79  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5));
80  const auto tx6 = make_tx(/*inputs=*/ {tx5}, /*output_values=*/ {1098 * CENT});
81  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx6));
82  // Make tx6's modified fee much higher than its base fee. This should cause it to pass
83  // the fee-related checks despite being low-feerate.
84  pool.PrioritiseTransaction(tx6->GetHash(), 1 * COIN);
85 
86  // Two independent high-feerate transactions, tx7 and tx8
87  const auto tx7 = make_tx(/*inputs=*/ {m_coinbase_txns[3]}, /*output_values=*/ {999 * CENT});
88  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx7));
89  const auto tx8 = make_tx(/*inputs=*/ {m_coinbase_txns[4]}, /*output_values=*/ {999 * CENT});
90  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx8));
91 
92  const auto entry1 = pool.GetIter(tx1->GetHash()).value();
93  const auto entry2 = pool.GetIter(tx2->GetHash()).value();
94  const auto entry3 = pool.GetIter(tx3->GetHash()).value();
95  const auto entry4 = pool.GetIter(tx4->GetHash()).value();
96  const auto entry5 = pool.GetIter(tx5->GetHash()).value();
97  const auto entry6 = pool.GetIter(tx6->GetHash()).value();
98  const auto entry7 = pool.GetIter(tx7->GetHash()).value();
99  const auto entry8 = pool.GetIter(tx8->GetHash()).value();
100 
101  BOOST_CHECK_EQUAL(entry1->GetFee(), normal_fee);
102  BOOST_CHECK_EQUAL(entry2->GetFee(), normal_fee);
103  BOOST_CHECK_EQUAL(entry3->GetFee(), low_fee);
104  BOOST_CHECK_EQUAL(entry4->GetFee(), high_fee);
105  BOOST_CHECK_EQUAL(entry5->GetFee(), low_fee);
106  BOOST_CHECK_EQUAL(entry6->GetFee(), low_fee);
107  BOOST_CHECK_EQUAL(entry7->GetFee(), high_fee);
108  BOOST_CHECK_EQUAL(entry8->GetFee(), high_fee);
109 
110  CTxMemPool::setEntries set_12_normal{entry1, entry2};
111  CTxMemPool::setEntries set_34_cpfp{entry3, entry4};
112  CTxMemPool::setEntries set_56_low{entry5, entry6};
113  CTxMemPool::setEntries all_entries{entry1, entry2, entry3, entry4, entry5, entry6, entry7, entry8};
114  CTxMemPool::setEntries empty_set;
115 
116  const auto unused_txid{GetRandHash()};
117 
118  // Tests for PaysMoreThanConflicts
119  // These tests use feerate, not absolute fee.
120  BOOST_CHECK(PaysMoreThanConflicts(/*iters_conflicting=*/set_12_normal,
121  /*replacement_feerate=*/CFeeRate(entry1->GetModifiedFee() + 1, entry1->GetTxSize() + 2),
122  /*txid=*/unused_txid).has_value());
123  // Replacement must be strictly greater than the originals.
124  BOOST_CHECK(PaysMoreThanConflicts(set_12_normal, CFeeRate(entry1->GetModifiedFee(), entry1->GetTxSize()), unused_txid).has_value());
125  BOOST_CHECK(PaysMoreThanConflicts(set_12_normal, CFeeRate(entry1->GetModifiedFee() + 1, entry1->GetTxSize()), unused_txid) == std::nullopt);
126  // These tests use modified fees (including prioritisation), not base fees.
127  BOOST_CHECK(PaysMoreThanConflicts({entry5}, CFeeRate(entry5->GetModifiedFee() + 1, entry5->GetTxSize()), unused_txid) == std::nullopt);
128  BOOST_CHECK(PaysMoreThanConflicts({entry6}, CFeeRate(entry6->GetFee() + 1, entry6->GetTxSize()), unused_txid).has_value());
129  BOOST_CHECK(PaysMoreThanConflicts({entry6}, CFeeRate(entry6->GetModifiedFee() + 1, entry6->GetTxSize()), unused_txid) == std::nullopt);
130  // PaysMoreThanConflicts checks individual feerate, not ancestor feerate. This test compares
131  // replacement_feerate and entry4's feerate, which are the same. The replacement_feerate is
132  // considered too low even though entry4 has a low ancestor feerate.
133  BOOST_CHECK(PaysMoreThanConflicts(set_34_cpfp, CFeeRate(entry4->GetModifiedFee(), entry4->GetTxSize()), unused_txid).has_value());
134 
135  // Tests for EntriesAndTxidsDisjoint
136  BOOST_CHECK(EntriesAndTxidsDisjoint(empty_set, {tx1->GetHash()}, unused_txid) == std::nullopt);
137  BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx3->GetHash()}, unused_txid) == std::nullopt);
138  BOOST_CHECK(EntriesAndTxidsDisjoint({entry2}, {tx2->GetHash()}, unused_txid).has_value());
139  BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx1->GetHash()}, unused_txid).has_value());
140  BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx2->GetHash()}, unused_txid).has_value());
141  // EntriesAndTxidsDisjoint does not calculate descendants of iters_conflicting; it uses whatever
142  // the caller passed in. As such, no error is returned even though entry2 is a descendant of tx1.
143  BOOST_CHECK(EntriesAndTxidsDisjoint({entry2}, {tx1->GetHash()}, unused_txid) == std::nullopt);
144 
145  // Tests for PaysForRBF
146  const CFeeRate incremental_relay_feerate{DEFAULT_INCREMENTAL_RELAY_FEE};
147  const CFeeRate higher_relay_feerate{2 * DEFAULT_INCREMENTAL_RELAY_FEE};
148  // Must pay at least as much as the original.
149  BOOST_CHECK(PaysForRBF(/*original_fees=*/high_fee,
150  /*replacement_fees=*/high_fee,
151  /*replacement_vsize=*/1,
152  /*relay_fee=*/CFeeRate(0),
153  /*txid=*/unused_txid)
154  == std::nullopt);
155  BOOST_CHECK(PaysForRBF(high_fee, high_fee - 1, 1, CFeeRate(0), unused_txid).has_value());
156  BOOST_CHECK(PaysForRBF(high_fee + 1, high_fee, 1, CFeeRate(0), unused_txid).has_value());
157  // Additional fees must cover the replacement's vsize at incremental relay fee
158  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 1, 2, incremental_relay_feerate, unused_txid).has_value());
159  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 2, 2, incremental_relay_feerate, unused_txid) == std::nullopt);
160  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 2, 2, higher_relay_feerate, unused_txid).has_value());
161  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 4, 2, higher_relay_feerate, unused_txid) == std::nullopt);
162  BOOST_CHECK(PaysForRBF(low_fee, high_fee, 99999999, incremental_relay_feerate, unused_txid).has_value());
163  BOOST_CHECK(PaysForRBF(low_fee, high_fee + 99999999, 99999999, incremental_relay_feerate, unused_txid) == std::nullopt);
164 
165  // Tests for GetEntriesForConflicts
166  CTxMemPool::setEntries all_parents{entry1, entry3, entry5, entry7, entry8};
167  CTxMemPool::setEntries all_children{entry2, entry4, entry6};
168  const std::vector<CTransactionRef> parent_inputs({m_coinbase_txns[0], m_coinbase_txns[1], m_coinbase_txns[2],
169  m_coinbase_txns[3], m_coinbase_txns[4]});
170  const auto conflicts_with_parents = make_tx(parent_inputs, {50 * CENT});
171  CTxMemPool::setEntries all_conflicts;
172  BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicts_with_parents.get(),
173  /*pool=*/ pool,
174  /*iters_conflicting=*/ all_parents,
175  /*all_conflicts=*/ all_conflicts) == std::nullopt);
176  BOOST_CHECK(all_conflicts == all_entries);
177  auto conflicts_size = all_conflicts.size();
178  all_conflicts.clear();
179 
180  add_descendants(tx2, 23, pool);
181  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
182  conflicts_size += 23;
183  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
184  all_conflicts.clear();
185 
186  add_descendants(tx4, 23, pool);
187  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
188  conflicts_size += 23;
189  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
190  all_conflicts.clear();
191 
192  add_descendants(tx6, 23, pool);
193  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
194  conflicts_size += 23;
195  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
196  all_conflicts.clear();
197 
198  add_descendants(tx7, 23, pool);
199  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
200  conflicts_size += 23;
201  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
202  BOOST_CHECK_EQUAL(all_conflicts.size(), 100);
203  all_conflicts.clear();
204 
205  // Exceeds maximum number of conflicts.
206  add_descendants(tx8, 1, pool);
207  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts).has_value());
208 
209  // Tests for HasNoNewUnconfirmed
210  const auto spends_unconfirmed = make_tx({tx1}, {36 * CENT});
211  for (const auto& input : spends_unconfirmed->vin) {
212  // Spends unconfirmed inputs.
213  BOOST_CHECK(pool.exists(GenTxid::Txid(input.prevout.hash)));
214  }
215  BOOST_CHECK(HasNoNewUnconfirmed(/*tx=*/ *spends_unconfirmed.get(),
216  /*pool=*/ pool,
217  /*iters_conflicting=*/ all_entries) == std::nullopt);
218  BOOST_CHECK(HasNoNewUnconfirmed(*spends_unconfirmed.get(), pool, {entry2}) == std::nullopt);
219  BOOST_CHECK(HasNoNewUnconfirmed(*spends_unconfirmed.get(), pool, empty_set).has_value());
220 
221  const auto spends_new_unconfirmed = make_tx({tx1, tx8}, {36 * CENT});
222  BOOST_CHECK(HasNoNewUnconfirmed(*spends_new_unconfirmed.get(), pool, {entry2}).has_value());
223  BOOST_CHECK(HasNoNewUnconfirmed(*spends_new_unconfirmed.get(), pool, all_entries).has_value());
224 
225  const auto spends_conflicting_confirmed = make_tx({m_coinbase_txns[0], m_coinbase_txns[1]}, {45 * CENT});
226  BOOST_CHECK(HasNoNewUnconfirmed(*spends_conflicting_confirmed.get(), pool, {entry1, entry3}) == std::nullopt);
227 }
228 
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
#define Assert(val)
Identity function.
Definition: check.h:77
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:300
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:872
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:388
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:946
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:394
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:674
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.h:472
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:434
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_AUTO_TEST_SUITE_END()
const CAmount high_fee
const CAmount low_fee
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::optional< std::string > PaysMoreThanConflicts(const CTxMemPool::setEntries &iters_conflicting, CFeeRate replacement_feerate, const uint256 &txid)
Check that the feerate of the replacement transaction(s) is higher than the feerate of each of the tr...
Definition: rbf.cpp:132
std::optional< std::string > HasNoNewUnconfirmed(const CTransaction &tx, const CTxMemPool &pool, const CTxMemPool::setEntries &iters_conflicting)
The replacement transaction may only include an unconfirmed input if that input was included in one o...
Definition: rbf.cpp:85
std::optional< std::string > PaysForRBF(CAmount original_fees, CAmount replacement_fees, size_t replacement_vsize, CFeeRate relay_fee, const uint256 &txid)
The replacement transaction must pay more fees than the original transactions.
Definition: rbf.cpp:158
std::optional< std::string > EntriesAndTxidsDisjoint(const CTxMemPool::setEntries &ancestors, const std::set< Txid > &direct_conflicts, const uint256 &txid)
Check the intersection between two sets of transactions (a set of mempool entries and a set of txids)...
Definition: rbf.cpp:117
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:57
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:35
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
uint256 GetRandHash() noexcept
Definition: random.cpp:650
static CTransactionRef make_tx(const std::vector< CTransactionRef > &inputs, const std::vector< CAmount > &output_values)
Definition: rbf_tests.cpp:19
static void add_descendants(const CTransactionRef &tx, int32_t num_descendants, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(
Definition: rbf_tests.cpp:40
BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
Definition: rbf_tests.cpp:55
@ OP_EQUAL
Definition: script.h:145
@ OP_11
Definition: script.h:93
static constexpr CAmount CENT
Definition: setup_common.h:44
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
std::vector< std::vector< unsigned char > > stack
Definition: script.h:569
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:99
Definition: txmempool.h:19
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Definition: txmempool.cpp:31
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: txmempool.h:33
Testing setup that configures a complete environment.
Definition: setup_common.h:78
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:57
#define LOCK2(cs1, cs2)
Definition: sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
AssertLockHeld(pool.cs)