13 #include <boost/test/unit_test.hpp>
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;
30 witness.
stack.emplace_back(i + 10);
31 tx.
vin[i].scriptWitness = witness;
33 for (
size_t i = 0; i < output_values.size(); ++i) {
35 tx.
vout[i].nValue = output_values[i];
47 auto tx_to_spend = tx;
48 for (int32_t i{0}; i < num_descendants; ++i) {
49 auto next_tx =
make_tx({tx_to_spend}, {(50 - i) *
CENT});
50 pool.addUnchecked(entry.
FromTx(next_tx));
51 tx_to_spend = next_tx;
65 pool.addUnchecked(entry.
FromTx(child_tx));
81 const auto tx1 =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
87 const auto tx3 =
make_tx( {m_coinbase_txns[1]}, {1099 *
CENT});
93 const auto tx5 =
make_tx( {m_coinbase_txns[2]}, {1099 *
CENT});
102 const auto tx7 =
make_tx( {m_coinbase_txns[3]}, {999 *
CENT});
104 const auto tx8 =
make_tx( {m_coinbase_txns[4]}, {999 *
CENT});
108 const auto tx9 =
make_tx( {m_coinbase_txns[5]}, {995 *
CENT});
110 const auto tx10 =
make_tx( {m_coinbase_txns[6]}, {995 *
CENT});
114 const auto tx11 =
make_tx( {m_coinbase_txns[7]}, {995 *
CENT});
116 const auto tx12 =
make_tx( {m_coinbase_txns[8]}, {995 *
CENT});
119 const auto entry1_normal = pool.
GetIter(tx1->GetHash()).value();
120 const auto entry2_normal = pool.
GetIter(tx2->GetHash()).value();
121 const auto entry3_low = pool.
GetIter(tx3->GetHash()).value();
122 const auto entry4_high = pool.
GetIter(tx4->GetHash()).value();
123 const auto entry5_low = pool.
GetIter(tx5->GetHash()).value();
124 const auto entry6_low_prioritised = pool.
GetIter(tx6->GetHash()).value();
125 const auto entry7_high = pool.
GetIter(tx7->GetHash()).value();
126 const auto entry8_high = pool.
GetIter(tx8->GetHash()).value();
127 const auto entry9_unchained = pool.
GetIter(tx9->GetHash()).value();
128 const auto entry10_unchained = pool.
GetIter(tx10->GetHash()).value();
129 const auto entry11_unchained = pool.
GetIter(tx11->GetHash()).value();
130 const auto entry12_unchained = pool.
GetIter(tx12->GetHash()).value();
146 entry5_low, entry6_low_prioritised, entry7_high, entry8_high};
154 CFeeRate(entry1_normal->GetModifiedFee() + 1, entry1_normal->GetTxSize() + 2),
155 unused_txid).has_value());
201 const std::vector<CTransactionRef> parent_inputs({m_coinbase_txns[0], m_coinbase_txns[1], m_coinbase_txns[2],
202 m_coinbase_txns[3], m_coinbase_txns[4]});
203 const auto conflicts_with_parents =
make_tx(parent_inputs, {50 *
CENT});
208 all_conflicts) == std::nullopt);
210 auto conflicts_size = all_conflicts.size();
211 all_conflicts.clear();
215 conflicts_size += 23;
217 all_conflicts.clear();
221 conflicts_size += 23;
223 all_conflicts.clear();
227 conflicts_size += 23;
229 all_conflicts.clear();
233 conflicts_size += 23;
236 all_conflicts.clear();
243 const auto spends_unconfirmed =
make_tx({tx1}, {36 *
CENT});
244 for (
const auto& input : spends_unconfirmed->vin) {
250 all_entries) == std::nullopt);
254 const auto spends_new_unconfirmed =
make_tx({tx1, tx8}, {36 *
CENT});
258 const auto spends_conflicting_confirmed =
make_tx({m_coinbase_txns[0], m_coinbase_txns[1]}, {45 *
CENT});
278 const auto entry10_child = pool.
GetIter(child_tx->GetHash()).value();
284 const auto entry10_grand_child = pool.
GetIter(grand_child_tx->GetHash()).value();
291 const auto entry_two_parent_child = pool.
GetIter(two_parent_child_tx->GetHash()).value();
292 BOOST_CHECK_EQUAL(pool.
CheckConflictTopology({entry11_unchained}).value(),
strprintf(
"%s is not the only parent of child %s", entry11_unchained->GetSharedTx()->GetHash().ToString(), entry_two_parent_child->GetSharedTx()->GetHash().ToString()));
293 BOOST_CHECK_EQUAL(pool.
CheckConflictTopology({entry12_unchained}).value(),
strprintf(
"%s is not the only parent of child %s", entry12_unchained->GetSharedTx()->GetHash().ToString(), entry_two_parent_child->GetSharedTx()->GetHash().ToString()));
307 const auto tx1 =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
312 const auto entry1 = pool.
GetIter(tx1->GetHash()).value();
313 const auto tx1_fee = entry1->GetModifiedFee();
314 const auto tx1_size = entry1->GetTxSize();
315 const auto entry2 = pool.
GetIter(tx2->GetHash()).value();
316 const auto tx2_fee = entry2->GetModifiedFee();
317 const auto tx2_size = entry2->GetTxSize();
322 const auto res1 =
ImprovesFeerateDiagram(pool, {entry1}, {entry1, entry2}, tx1_fee + tx2_fee, tx1_size + tx2_size);
325 BOOST_CHECK(res1.value().second ==
"insufficient feerate: does not improve feerate diagram");
333 const auto res3 =
ImprovesFeerateDiagram(pool, {entry1}, {entry1, entry2}, tx1_fee + tx2_fee + 1, tx1_size + tx2_size);
336 BOOST_CHECK(res3.value().second ==
strprintf(
"%s has 2 descendants, max 1 allowed", tx1->GetHash().GetHex()));
351 const auto low_tx =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
354 const auto entry_low = pool.
GetIter(low_tx->GetHash()).value();
355 const auto low_size = entry_low->GetTxSize();
357 std::vector<FeeFrac> old_diagram, new_diagram;
362 old_diagram = replace_one->first;
363 new_diagram = replace_one->second;
374 old_diagram = replace_one_fee->first;
375 new_diagram = replace_one_fee->second;
384 const auto high_tx =
make_tx( {low_tx}, {995 *
CENT});
386 const auto entry_high = pool.
GetIter(high_tx->GetHash()).value();
387 const auto high_size = entry_high->GetTxSize();
391 old_diagram = replace_single_chunk->first;
392 new_diagram = replace_single_chunk->second;
403 old_diagram = replace_cpfp_child->first;
404 new_diagram = replace_cpfp_child->second;
414 const auto normal_tx =
make_tx( {high_tx}, {995 *
CENT});
416 const auto entry_normal = pool.
GetIter(normal_tx->GetHash()).value();
417 const auto normal_size = entry_normal->GetTxSize();
426 const auto high_tx_2 =
make_tx( {m_coinbase_txns[1]}, {10 *
COIN});
428 const auto entry_high_2 = pool.
GetIter(high_tx_2->GetHash()).value();
429 const auto high_size_2 = entry_high_2->GetTxSize();
431 const auto low_tx_2 =
make_tx( {high_tx_2}, {9 *
COIN});
433 const auto entry_low_2 = pool.
GetIter(low_tx_2->GetHash()).value();
434 const auto low_size_2 = entry_low_2->GetTxSize();
437 BOOST_CHECK(replace_two_chunks_single_cluster.has_value());
438 old_diagram = replace_two_chunks_single_cluster->first;
439 new_diagram = replace_two_chunks_single_cluster->second;
449 const auto conflict_1 =
make_tx( {m_coinbase_txns[2]}, {10 *
COIN});
451 const auto conflict_1_entry = pool.
GetIter(conflict_1->GetHash()).value();
453 const auto conflict_2 =
make_tx( {m_coinbase_txns[3]}, {10 *
COIN});
455 const auto conflict_2_entry = pool.
GetIter(conflict_2->GetHash()).value();
457 const auto conflict_3 =
make_tx( {m_coinbase_txns[4]}, {10 *
COIN});
459 const auto conflict_3_entry = pool.
GetIter(conflict_3->GetHash()).value();
461 const auto replace_multiple_clusters{pool.
CalculateFeerateDiagramsForRBF(
high_fee, low_size, {conflict_1_entry, conflict_2_entry, conflict_3_entry}, {conflict_1_entry, conflict_2_entry, conflict_3_entry})};
463 BOOST_CHECK(replace_multiple_clusters.has_value());
464 old_diagram = replace_multiple_clusters->first;
465 new_diagram = replace_multiple_clusters->second;
470 const auto conflict_1_child =
make_tx({conflict_1}, {995 *
CENT});
472 const auto conflict_1_child_entry = pool.
GetIter(conflict_1_child->GetHash()).value();
474 const auto replace_multiple_clusters_2{pool.
CalculateFeerateDiagramsForRBF(
high_fee, low_size, {conflict_1_entry, conflict_2_entry, conflict_3_entry}, {conflict_1_entry, conflict_2_entry, conflict_3_entry, conflict_1_child_entry})};
476 BOOST_CHECK(replace_multiple_clusters_2.has_value());
477 old_diagram = replace_multiple_clusters_2->first;
478 new_diagram = replace_multiple_clusters_2->second;
485 const auto conflict_1_grand_child =
make_tx({conflict_1_child}, {995 *
CENT});
487 const auto conflict_1_grand_child_entry = pool.
GetIter(conflict_1_child->GetHash()).value();
489 const auto replace_cluster_size_3{pool.
CalculateFeerateDiagramsForRBF(
high_fee, low_size, {conflict_1_entry, conflict_2_entry, conflict_3_entry}, {conflict_1_entry, conflict_2_entry, conflict_3_entry, conflict_1_child_entry, conflict_1_grand_child_entry})};
562 new_diagram = {
FeeFrac{0, 0},
FeeFrac{950, 300},
FeeFrac{1050, 400},
FeeFrac{1050, 401},
FeeFrac{1050, 402},
FeeFrac{1051, 403}};
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
#define Assert(val)
Identity function.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Serialized script, used inside transaction inputs and outputs.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
std::set< txiter, CompareIteratorByHash > setEntries
bool exists(const GenTxid >xid) const
std::optional< std::string > CheckConflictTopology(const setEntries &direct_conflicts)
util::Result< std::pair< std::vector< FeeFrac >, std::vector< FeeFrac > > > CalculateFeerateDiagramsForRBF(CAmount replacement_fees, int64_t replacement_vsize, const setEntries &direct_conflicts, const setEntries &all_conflicts) EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate the old and new mempool feerate diagrams relating to the clusters that would be affected by...
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...
static GenTxid Txid(const uint256 &hash)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE_END()
bilingual_str ErrorString(const Result< T > &result)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
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...
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...
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.
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)...
std::optional< std::pair< DiagramCheckError, std::string > > ImprovesFeerateDiagram(CTxMemPool &pool, const CTxMemPool::setEntries &direct_conflicts, const CTxMemPool::setEntries &all_conflicts, CAmount replacement_fees, int64_t replacement_vsize)
The replacement transaction must improve the feerate diagram of the mempool.
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.
@ FAILURE
New diagram wasn't strictly superior
@ UNCALCULABLE
Unable to calculate due to topology or other reason.
static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE
Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or rep...
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
BOOST_AUTO_TEST_CASE(feerate_diagram_utilities)
static CTransactionRef make_tx(const std::vector< CTransactionRef > &inputs, const std::vector< CAmount > &output_values)
static CTransactionRef add_descendants(const CTransactionRef &tx, int32_t num_descendants, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(
static CTransactionRef add_descendant_to_parents(const std::vector< CTransactionRef > &parents, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(
BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
static constexpr CAmount CENT
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
Data structure storing a fee and size, ordered by increasing fee/size.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & Fee(CAmount _fee)
Testing setup that configures a complete environment.
std::unique_ptr< CTxMemPool > mempool
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::partial_ordering CompareFeerateDiagram(Span< const FeeFrac > dia0, Span< const FeeFrac > dia1)
Compares two feerate diagrams.