Bitcoin Core  24.99.0
P2P Digital Currency
rbf_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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 <policy/rbf.h>
5 #include <random.h>
6 #include <txmempool.h>
7 #include <util/system.h>
8 #include <util/time.h>
9 
10 #include <test/util/setup_common.h>
11 
12 #include <boost/test/unit_test.hpp>
13 #include <optional>
14 #include <vector>
15 
16 BOOST_FIXTURE_TEST_SUITE(rbf_tests, TestingSetup)
17 
18 static inline CTransactionRef make_tx(const std::vector<CTransactionRef>& inputs,
19  const std::vector<CAmount>& output_values)
20 {
22  tx.vin.resize(inputs.size());
23  tx.vout.resize(output_values.size());
24  for (size_t i = 0; i < inputs.size(); ++i) {
25  tx.vin[i].prevout.hash = inputs[i]->GetHash();
26  tx.vin[i].prevout.n = 0;
27  // Add a witness so wtxid != txid
28  CScriptWitness witness;
29  witness.stack.push_back(std::vector<unsigned char>(i + 10));
30  tx.vin[i].scriptWitness = witness;
31  }
32  for (size_t i = 0; i < output_values.size(); ++i) {
33  tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
34  tx.vout[i].nValue = output_values[i];
35  }
36  return MakeTransactionRef(tx);
37 }
38 
39 static void add_descendants(const CTransactionRef& tx, int32_t num_descendants, CTxMemPool& pool)
41 {
43  AssertLockHeld(pool.cs);
45  // Assumes this isn't already spent in mempool
46  auto tx_to_spend = tx;
47  for (int32_t i{0}; i < num_descendants; ++i) {
48  auto next_tx = make_tx(/*inputs=*/{tx_to_spend}, /*output_values=*/{(50 - i) * CENT});
49  pool.addUnchecked(entry.FromTx(next_tx));
50  tx_to_spend = next_tx;
51  }
52 }
53 
55 {
56  CTxMemPool& pool = *Assert(m_node.mempool);
57  LOCK2(::cs_main, pool.cs);
59 
60  const CAmount low_fee{CENT/100};
61  const CAmount normal_fee{CENT/10};
62  const CAmount high_fee{CENT};
63 
64  // Create a parent tx1 and child tx2 with normal fees:
65  const auto tx1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {10 * COIN});
66  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx1));
67  const auto tx2 = make_tx(/*inputs=*/ {tx1}, /*output_values=*/ {995 * CENT});
68  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx2));
69 
70  // Create a low-feerate parent tx3 and high-feerate child tx4 (cpfp)
71  const auto tx3 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ {1099 * CENT});
72  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx3));
73  const auto tx4 = make_tx(/*inputs=*/ {tx3}, /*output_values=*/ {999 * CENT});
74  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx4));
75 
76  // Create a parent tx5 and child tx6 where both have very low fees
77  const auto tx5 = make_tx(/*inputs=*/ {m_coinbase_txns[2]}, /*output_values=*/ {1099 * CENT});
78  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5));
79  const auto tx6 = make_tx(/*inputs=*/ {tx5}, /*output_values=*/ {1098 * CENT});
80  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx6));
81  // Make tx6's modified fee much higher than its base fee. This should cause it to pass
82  // the fee-related checks despite being low-feerate.
83  pool.PrioritiseTransaction(tx6->GetHash(), 1 * COIN);
84 
85  // Two independent high-feerate transactions, tx7 and tx8
86  const auto tx7 = make_tx(/*inputs=*/ {m_coinbase_txns[3]}, /*output_values=*/ {999 * CENT});
87  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx7));
88  const auto tx8 = make_tx(/*inputs=*/ {m_coinbase_txns[4]}, /*output_values=*/ {999 * CENT});
89  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx8));
90 
91  const auto entry1 = pool.GetIter(tx1->GetHash()).value();
92  const auto entry2 = pool.GetIter(tx2->GetHash()).value();
93  const auto entry3 = pool.GetIter(tx3->GetHash()).value();
94  const auto entry4 = pool.GetIter(tx4->GetHash()).value();
95  const auto entry5 = pool.GetIter(tx5->GetHash()).value();
96  const auto entry6 = pool.GetIter(tx6->GetHash()).value();
97  const auto entry7 = pool.GetIter(tx7->GetHash()).value();
98  const auto entry8 = pool.GetIter(tx8->GetHash()).value();
99 
100  BOOST_CHECK_EQUAL(entry1->GetFee(), normal_fee);
101  BOOST_CHECK_EQUAL(entry2->GetFee(), normal_fee);
102  BOOST_CHECK_EQUAL(entry3->GetFee(), low_fee);
103  BOOST_CHECK_EQUAL(entry4->GetFee(), high_fee);
104  BOOST_CHECK_EQUAL(entry5->GetFee(), low_fee);
105  BOOST_CHECK_EQUAL(entry6->GetFee(), low_fee);
106  BOOST_CHECK_EQUAL(entry7->GetFee(), high_fee);
107  BOOST_CHECK_EQUAL(entry8->GetFee(), high_fee);
108 
109  CTxMemPool::setEntries set_12_normal{entry1, entry2};
110  CTxMemPool::setEntries set_34_cpfp{entry3, entry4};
111  CTxMemPool::setEntries set_56_low{entry5, entry6};
112  CTxMemPool::setEntries all_entries{entry1, entry2, entry3, entry4, entry5, entry6, entry7, entry8};
113  CTxMemPool::setEntries empty_set;
114 
115  const auto unused_txid{GetRandHash()};
116 
117  // Tests for PaysMoreThanConflicts
118  // These tests use feerate, not absolute fee.
119  BOOST_CHECK(PaysMoreThanConflicts(/*iters_conflicting=*/set_12_normal,
120  /*replacement_feerate=*/CFeeRate(entry1->GetModifiedFee() + 1, entry1->GetTxSize() + 2),
121  /*txid=*/unused_txid).has_value());
122  // Replacement must be strictly greater than the originals.
123  BOOST_CHECK(PaysMoreThanConflicts(set_12_normal, CFeeRate(entry1->GetModifiedFee(), entry1->GetTxSize()), unused_txid).has_value());
124  BOOST_CHECK(PaysMoreThanConflicts(set_12_normal, CFeeRate(entry1->GetModifiedFee() + 1, entry1->GetTxSize()), unused_txid) == std::nullopt);
125  // These tests use modified fees (including prioritisation), not base fees.
126  BOOST_CHECK(PaysMoreThanConflicts({entry5}, CFeeRate(entry5->GetModifiedFee() + 1, entry5->GetTxSize()), unused_txid) == std::nullopt);
127  BOOST_CHECK(PaysMoreThanConflicts({entry6}, CFeeRate(entry6->GetFee() + 1, entry6->GetTxSize()), unused_txid).has_value());
128  BOOST_CHECK(PaysMoreThanConflicts({entry6}, CFeeRate(entry6->GetModifiedFee() + 1, entry6->GetTxSize()), unused_txid) == std::nullopt);
129  // PaysMoreThanConflicts checks individual feerate, not ancestor feerate. This test compares
130  // replacement_feerate and entry4's feerate, which are the same. The replacement_feerate is
131  // considered too low even though entry4 has a low ancestor feerate.
132  BOOST_CHECK(PaysMoreThanConflicts(set_34_cpfp, CFeeRate(entry4->GetModifiedFee(), entry4->GetTxSize()), unused_txid).has_value());
133 
134  // Tests for EntriesAndTxidsDisjoint
135  BOOST_CHECK(EntriesAndTxidsDisjoint(empty_set, {tx1->GetHash()}, unused_txid) == std::nullopt);
136  BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx3->GetHash()}, unused_txid) == std::nullopt);
137  // EntriesAndTxidsDisjoint uses txids, not wtxids.
138  BOOST_CHECK(EntriesAndTxidsDisjoint({entry2}, {tx2->GetWitnessHash()}, unused_txid) == std::nullopt);
139  BOOST_CHECK(EntriesAndTxidsDisjoint({entry2}, {tx2->GetHash()}, unused_txid).has_value());
140  BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx1->GetHash()}, unused_txid).has_value());
141  BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx2->GetHash()}, unused_txid).has_value());
142  // EntriesAndTxidsDisjoint does not calculate descendants of iters_conflicting; it uses whatever
143  // the caller passed in. As such, no error is returned even though entry2 is a descendant of tx1.
144  BOOST_CHECK(EntriesAndTxidsDisjoint({entry2}, {tx1->GetHash()}, unused_txid) == std::nullopt);
145 
146  // Tests for PaysForRBF
147  const CFeeRate incremental_relay_feerate{DEFAULT_INCREMENTAL_RELAY_FEE};
148  const CFeeRate higher_relay_feerate{2 * DEFAULT_INCREMENTAL_RELAY_FEE};
149  // Must pay at least as much as the original.
150  BOOST_CHECK(PaysForRBF(/*original_fees=*/high_fee,
151  /*replacement_fees=*/high_fee,
152  /*replacement_vsize=*/1,
153  /*relay_fee=*/CFeeRate(0),
154  /*txid=*/unused_txid)
155  == std::nullopt);
156  BOOST_CHECK(PaysForRBF(high_fee, high_fee - 1, 1, CFeeRate(0), unused_txid).has_value());
157  BOOST_CHECK(PaysForRBF(high_fee + 1, high_fee, 1, CFeeRate(0), unused_txid).has_value());
158  // Additional fees must cover the replacement's vsize at incremental relay fee
159  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 1, 2, incremental_relay_feerate, unused_txid).has_value());
160  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 2, 2, incremental_relay_feerate, unused_txid) == std::nullopt);
161  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 2, 2, higher_relay_feerate, unused_txid).has_value());
162  BOOST_CHECK(PaysForRBF(high_fee, high_fee + 4, 2, higher_relay_feerate, unused_txid) == std::nullopt);
163  BOOST_CHECK(PaysForRBF(low_fee, high_fee, 99999999, incremental_relay_feerate, unused_txid).has_value());
164  BOOST_CHECK(PaysForRBF(low_fee, high_fee + 99999999, 99999999, incremental_relay_feerate, unused_txid) == std::nullopt);
165 
166  // Tests for GetEntriesForConflicts
167  CTxMemPool::setEntries all_parents{entry1, entry3, entry5, entry7, entry8};
168  CTxMemPool::setEntries all_children{entry2, entry4, entry6};
169  const std::vector<CTransactionRef> parent_inputs({m_coinbase_txns[0], m_coinbase_txns[1], m_coinbase_txns[2],
170  m_coinbase_txns[3], m_coinbase_txns[4]});
171  const auto conflicts_with_parents = make_tx(parent_inputs, {50 * CENT});
172  CTxMemPool::setEntries all_conflicts;
173  BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicts_with_parents.get(),
174  /*pool=*/ pool,
175  /*iters_conflicting=*/ all_parents,
176  /*all_conflicts=*/ all_conflicts) == std::nullopt);
177  BOOST_CHECK(all_conflicts == all_entries);
178  auto conflicts_size = all_conflicts.size();
179  all_conflicts.clear();
180 
181  add_descendants(tx2, 23, pool);
182  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
183  conflicts_size += 23;
184  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
185  all_conflicts.clear();
186 
187  add_descendants(tx4, 23, pool);
188  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
189  conflicts_size += 23;
190  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
191  all_conflicts.clear();
192 
193  add_descendants(tx6, 23, pool);
194  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
195  conflicts_size += 23;
196  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
197  all_conflicts.clear();
198 
199  add_descendants(tx7, 23, pool);
200  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
201  conflicts_size += 23;
202  BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
203  BOOST_CHECK_EQUAL(all_conflicts.size(), 100);
204  all_conflicts.clear();
205 
206  // Exceeds maximum number of conflicts.
207  add_descendants(tx8, 1, pool);
208  BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts).has_value());
209 
210  // Tests for HasNoNewUnconfirmed
211  const auto spends_unconfirmed = make_tx({tx1}, {36 * CENT});
212  for (const auto& input : spends_unconfirmed->vin) {
213  // Spends unconfirmed inputs.
214  BOOST_CHECK(pool.exists(GenTxid::Txid(input.prevout.hash)));
215  }
216  BOOST_CHECK(HasNoNewUnconfirmed(/*tx=*/ *spends_unconfirmed.get(),
217  /*pool=*/ pool,
218  /*iters_conflicting=*/ all_entries) == std::nullopt);
219  BOOST_CHECK(HasNoNewUnconfirmed(*spends_unconfirmed.get(), pool, {entry2}) == std::nullopt);
220  BOOST_CHECK(HasNoNewUnconfirmed(*spends_unconfirmed.get(), pool, empty_set).has_value());
221 
222  const auto spends_new_unconfirmed = make_tx({tx1, tx8}, {36 * CENT});
223  BOOST_CHECK(HasNoNewUnconfirmed(*spends_new_unconfirmed.get(), pool, {entry2}).has_value());
224  BOOST_CHECK(HasNoNewUnconfirmed(*spends_new_unconfirmed.get(), pool, all_entries).has_value());
225 
226  const auto spends_conflicting_confirmed = make_tx({m_coinbase_txns[0], m_coinbase_txns[1]}, {45 * CENT});
227  BOOST_CHECK(HasNoNewUnconfirmed(*spends_conflicting_confirmed.get(), pool, {entry1, entry3}) == std::nullopt);
228 }
229 
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:121
#define Assert(val)
Identity function.
Definition: check.h:74
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:432
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:900
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:521
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) 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:605
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:952
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:527
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:767
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:425
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
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:133
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:86
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:159
std::optional< std::string > EntriesAndTxidsDisjoint(const CTxMemPool::setEntries &ancestors, const std::set< uint256 > &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:118
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
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:415
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
uint256 GetRandHash() noexcept
Definition: random.cpp:592
static CTransactionRef make_tx(const std::vector< CTransactionRef > &inputs, const std::vector< CAmount > &output_values)
Definition: rbf_tests.cpp:18
static void add_descendants(const CTransactionRef &tx, int32_t num_descendants, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(
Definition: rbf_tests.cpp:39
BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
Definition: rbf_tests.cpp:54
@ OP_EQUAL
Definition: script.h:142
@ OP_11
Definition: script.h:90
static constexpr CAmount CENT
Definition: setup_common.h:78
A mutable version of CTransaction.
Definition: transaction.h:373
std::vector< CTxOut > vout
Definition: transaction.h:375
std::vector< CTxIn > vin
Definition: transaction.h:374
std::vector< std::vector< unsigned char > > stack
Definition: script.h:566
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:126
Definition: setup_common.h:207
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: setup_common.h:224
Testing setup that configures a complete environment.
Definition: setup_common.h:109
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:50
#define LOCK2(cs1, cs2)
Definition: sync.h:262
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
AssertLockHeld(pool.cs)