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];
43 const std::vector<CAmount>& output_values)
45 assert(parent->vout.size() >= 2);
50 tx1.
vout.resize(output_values.size());
52 tx1.
vin[0].prevout.hash = parent->GetHash();
53 tx1.
vin[0].prevout.n = 0;
56 witness.
stack.emplace_back(10);
57 tx1.
vin[0].scriptWitness = witness;
59 for (
size_t i = 0; i < output_values.size(); ++i) {
61 tx1.
vout[i].nValue = output_values[i];
66 tx2.
vin[0].prevout.n = 1;
78 auto tx_to_spend = tx;
79 for (int32_t i{0}; i < num_descendants; ++i) {
80 auto next_tx =
make_tx({tx_to_spend}, {(50 - i) *
CENT});
81 pool.addUnchecked(entry.
FromTx(next_tx));
82 tx_to_spend = next_tx;
96 pool.addUnchecked(entry.
FromTx(child_tx));
110 pool.addUnchecked(entry.
FromTx(children_tx.first));
111 pool.addUnchecked(entry.
FromTx(children_tx.second));
126 const auto tx1 =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
132 const auto tx3 =
make_tx( {m_coinbase_txns[1]}, {1099 *
CENT});
138 const auto tx5 =
make_tx( {m_coinbase_txns[2]}, {1099 *
CENT});
147 const auto tx7 =
make_tx( {m_coinbase_txns[3]}, {999 *
CENT});
149 const auto tx8 =
make_tx( {m_coinbase_txns[4]}, {999 *
CENT});
153 const auto tx9 =
make_tx( {m_coinbase_txns[5]}, {995 *
CENT});
155 const auto tx10 =
make_tx( {m_coinbase_txns[6]}, {995 *
CENT});
159 const auto tx11 =
make_tx( {m_coinbase_txns[7]}, {995 *
CENT});
161 const auto tx12 =
make_tx( {m_coinbase_txns[8]}, {995 *
CENT});
165 const auto tx13 =
make_tx( {m_coinbase_txns[9]}, {995 *
CENT, 995 *
CENT});
168 const auto entry1_normal = pool.
GetIter(tx1->GetHash()).value();
169 const auto entry2_normal = pool.
GetIter(tx2->GetHash()).value();
170 const auto entry3_low = pool.
GetIter(tx3->GetHash()).value();
171 const auto entry4_high = pool.
GetIter(tx4->GetHash()).value();
172 const auto entry5_low = pool.
GetIter(tx5->GetHash()).value();
173 const auto entry6_low_prioritised = pool.
GetIter(tx6->GetHash()).value();
174 const auto entry7_high = pool.
GetIter(tx7->GetHash()).value();
175 const auto entry8_high = pool.
GetIter(tx8->GetHash()).value();
176 const auto entry9_unchained = pool.
GetIter(tx9->GetHash()).value();
177 const auto entry10_unchained = pool.
GetIter(tx10->GetHash()).value();
178 const auto entry11_unchained = pool.
GetIter(tx11->GetHash()).value();
179 const auto entry12_unchained = pool.
GetIter(tx12->GetHash()).value();
180 const auto entry13_unchained = pool.
GetIter(tx13->GetHash()).value();
196 entry5_low, entry6_low_prioritised, entry7_high, entry8_high};
204 CFeeRate(entry1_normal->GetModifiedFee() + 1, entry1_normal->GetTxSize() + 2),
205 unused_txid).has_value());
251 const std::vector<CTransactionRef> parent_inputs({m_coinbase_txns[0], m_coinbase_txns[1], m_coinbase_txns[2],
252 m_coinbase_txns[3], m_coinbase_txns[4]});
253 const auto conflicts_with_parents =
make_tx(parent_inputs, {50 *
CENT});
258 all_conflicts) == std::nullopt);
260 auto conflicts_size = all_conflicts.size();
261 all_conflicts.clear();
265 conflicts_size += 23;
267 all_conflicts.clear();
271 conflicts_size += 23;
273 all_conflicts.clear();
277 conflicts_size += 23;
279 all_conflicts.clear();
283 conflicts_size += 23;
286 all_conflicts.clear();
293 const auto spends_unconfirmed =
make_tx({tx1}, {36 *
CENT});
294 for (
const auto& input : spends_unconfirmed->vin) {
300 all_entries) == std::nullopt);
304 const auto spends_new_unconfirmed =
make_tx({tx1, tx8}, {36 *
CENT});
308 const auto spends_conflicting_confirmed =
make_tx({m_coinbase_txns[0], m_coinbase_txns[1]}, {45 *
CENT});
328 const auto entry10_child = pool.
GetIter(child_tx->GetHash()).value();
334 const auto entry10_grand_child = pool.
GetIter(grand_child_tx->GetHash()).value();
341 const auto entry_two_parent_child = pool.
GetIter(two_parent_child_tx->GetHash()).value();
342 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()));
343 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()));
348 const auto entry_sibling_1 = pool.
GetIter(two_siblings.first->GetHash()).value();
349 const auto entry_sibling_2 = pool.
GetIter(two_siblings.second->GetHash()).value();
350 BOOST_CHECK_EQUAL(pool.
CheckConflictTopology({entry_sibling_1}).value(),
strprintf(
"%s is not the only child of parent %s", entry_sibling_1->GetSharedTx()->GetHash().ToString(), entry13_unchained->GetSharedTx()->GetHash().ToString()));
351 BOOST_CHECK_EQUAL(pool.
CheckConflictTopology({entry_sibling_2}).value(),
strprintf(
"%s is not the only child of parent %s", entry_sibling_2->GetSharedTx()->GetHash().ToString(), entry13_unchained->GetSharedTx()->GetHash().ToString()));
365 const auto tx1 =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
370 const auto entry1 = pool.
GetIter(tx1->GetHash()).value();
371 const auto tx1_fee = entry1->GetModifiedFee();
372 const auto tx1_size = entry1->GetTxSize();
373 const auto entry2 = pool.
GetIter(tx2->GetHash()).value();
374 const auto tx2_fee = entry2->GetModifiedFee();
375 const auto tx2_size = entry2->GetTxSize();
380 const auto res1 =
ImprovesFeerateDiagram(pool, {entry1}, {entry1, entry2}, tx1_fee + tx2_fee, tx1_size + tx2_size);
383 BOOST_CHECK(res1.value().second ==
"insufficient feerate: does not improve feerate diagram");
390 const auto res2 =
ImprovesFeerateDiagram(pool, {entry1}, {entry1, entry2}, tx1_fee + tx2_fee + 1, tx1_size + tx2_size);
393 BOOST_CHECK(res2.value().second ==
"insufficient feerate: does not improve feerate diagram");
402 const auto res3 =
ImprovesFeerateDiagram(pool, {entry1}, {entry1, entry2}, tx1_fee + tx2_fee + 1, tx1_size + tx2_size);
405 BOOST_CHECK(res3.value().second ==
strprintf(
"%s has 2 descendants, max 1 allowed", tx1->GetHash().GetHex()));
421 const auto low_tx =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
424 const auto entry_low = pool.
GetIter(low_tx->GetHash()).value();
425 const auto low_size = entry_low->GetTxSize();
431 std::vector<FeeFrac> expected_old_chunks{{
low_fee, low_size}};
432 BOOST_CHECK(replace_one->first == expected_old_chunks);
433 std::vector<FeeFrac> expected_new_chunks{{0, 1}};
434 BOOST_CHECK(replace_one->second == expected_new_chunks);
441 std::vector<FeeFrac> expected_old_diagram{{
low_fee, low_size}};
442 BOOST_CHECK(replace_one_fee->first == expected_old_diagram);
443 std::vector<FeeFrac> expected_new_diagram{{
high_fee, low_size}};
444 BOOST_CHECK(replace_one_fee->second == expected_new_diagram);
448 const auto high_tx =
make_tx( {low_tx}, {995 *
CENT});
450 const auto entry_high = pool.
GetIter(high_tx->GetHash()).value();
451 const auto high_size = entry_high->GetTxSize();
456 std::vector<FeeFrac> expected_old_chunks{{
low_fee +
high_fee, low_size + high_size}};
457 BOOST_CHECK(replace_single_chunk->first == expected_old_chunks);
458 std::vector<FeeFrac> expected_new_chunks{{
high_fee, low_size}};
459 BOOST_CHECK(replace_single_chunk->second == expected_new_chunks);
466 std::vector<FeeFrac> expected_old_chunks{{
low_fee +
high_fee, low_size + high_size}};
467 BOOST_CHECK(replace_cpfp_child->first == expected_old_chunks);
468 std::vector<FeeFrac> expected_new_chunks{{
high_fee, low_size}, {
low_fee, low_size}};
469 BOOST_CHECK(replace_cpfp_child->second == expected_new_chunks);
473 const auto normal_tx =
make_tx( {high_tx}, {995 *
CENT});
475 const auto entry_normal = pool.
GetIter(normal_tx->GetHash()).value();
476 const auto normal_size = entry_normal->GetTxSize();
479 const auto replace_too_large{pool.
CalculateChunksForRBF(normal_fee, normal_size, {entry_low}, {entry_low, entry_high, entry_normal})};
485 const auto high_tx_2 =
make_tx( {m_coinbase_txns[1]}, {10 *
COIN});
487 const auto entry_high_2 = pool.
GetIter(high_tx_2->GetHash()).value();
488 const auto high_size_2 = entry_high_2->GetTxSize();
490 const auto low_tx_2 =
make_tx( {high_tx_2}, {9 *
COIN});
492 const auto entry_low_2 = pool.
GetIter(low_tx_2->GetHash()).value();
493 const auto low_size_2 = entry_low_2->GetTxSize();
497 BOOST_CHECK(replace_two_chunks_single_cluster.has_value());
498 std::vector<FeeFrac> expected_old_chunks{{
high_fee, high_size_2}, {
low_fee, low_size_2}};
499 BOOST_CHECK(replace_two_chunks_single_cluster->first == expected_old_chunks);
500 std::vector<FeeFrac> expected_new_chunks{{
high_fee, low_size_2}};
501 BOOST_CHECK(replace_two_chunks_single_cluster->second == expected_new_chunks);
505 const auto conflict_1 =
make_tx( {m_coinbase_txns[2]}, {10 *
COIN});
507 const auto conflict_1_entry = pool.
GetIter(conflict_1->GetHash()).value();
509 const auto conflict_2 =
make_tx( {m_coinbase_txns[3]}, {10 *
COIN});
511 const auto conflict_2_entry = pool.
GetIter(conflict_2->GetHash()).value();
513 const auto conflict_3 =
make_tx( {m_coinbase_txns[4]}, {10 *
COIN});
515 const auto conflict_3_entry = pool.
GetIter(conflict_3->GetHash()).value();
518 const auto replace_multiple_clusters{pool.
CalculateChunksForRBF(
high_fee, low_size, {conflict_1_entry, conflict_2_entry, conflict_3_entry}, {conflict_1_entry, conflict_2_entry, conflict_3_entry})};
519 BOOST_CHECK(replace_multiple_clusters.has_value());
520 BOOST_CHECK(replace_multiple_clusters->first.size() == 3);
521 BOOST_CHECK(replace_multiple_clusters->second.size() == 1);
525 const auto conflict_1_child =
make_tx({conflict_1}, {995 *
CENT});
527 const auto conflict_1_child_entry = pool.
GetIter(conflict_1_child->GetHash()).value();
530 const auto replace_multiple_clusters_2{pool.
CalculateChunksForRBF(
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})};
532 BOOST_CHECK(replace_multiple_clusters_2.has_value());
533 BOOST_CHECK(replace_multiple_clusters_2->first.size() == 4);
534 BOOST_CHECK(replace_multiple_clusters_2->second.size() == 1);
538 const auto conflict_1_grand_child =
make_tx({conflict_1_child}, {995 *
CENT});
540 const auto conflict_1_grand_child_entry = pool.
GetIter(conflict_1_child->GetHash()).value();
543 const auto replace_cluster_size_3{pool.
CalculateChunksForRBF(
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})};
555 std::vector<FeeFrac> old_chunks{{{950, 300}, {100, 100}}};
556 std::vector<FeeFrac> new_chunks{{{1000, 300}, {50, 100}}};
562 old_chunks = {{950, 300}, {100, 100}};
563 new_chunks = {{1000, 300}, {0, 100}};
569 old_chunks = {{950, 300}, {100, 100}};
570 new_chunks = {{1100, 300}};
577 old_chunks = {{950, 300}, {100, 100}};
578 new_chunks = {{1100, 100}, {0, 100}};
584 old_chunks = {{950, 300}, {100, 100}};
585 new_chunks = {{750, 100}, {249, 250}, {151, 650}};
591 old_chunks = {{950, 300}, {100, 100}};
592 new_chunks = {{750, 100}, {250, 250}, {150, 150}};
598 old_chunks = {{950, 300}, {100, 100}};
599 new_chunks = {{950, 300}, {100, 100}};
605 old_chunks = {{950, 300}, {100, 99}};
606 new_chunks = {{950, 300}, {100, 100}};
613 old_chunks = {{950, 300}, {100, 99}};
614 new_chunks = {{950, 300}, {100, 100}, {0, 1}, {0, 1}};
620 new_chunks = {{950, 300}, {100, 100}, {0, 1}, {0, 1}, {1, 1}};
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.
util::Result< std::pair< std::vector< FeeFrac >, std::vector< FeeFrac > > > CalculateChunksForRBF(CAmount replacement_fees, int64_t replacement_vsize, const setEntries &direct_conflicts, const setEntries &all_conflicts) EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate the sorted chunks for the old and new mempool relating to the clusters that would be affect...
std::set< txiter, CompareIteratorByHash > setEntries
bool exists(const GenTxid >xid) const
std::optional< std::string > CheckConflictTopology(const setEntries &direct_conflicts)
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
static CTransactionRef make_tx(const std::vector< CTransactionRef > &inputs, const std::vector< CAmount > &output_values)
static std::pair< CTransactionRef, CTransactionRef > make_two_siblings(const CTransactionRef parent, const std::vector< CAmount > &output_values)
static CTransactionRef add_descendants(const CTransactionRef &tx, int32_t num_descendants, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(
BOOST_AUTO_TEST_CASE(feerate_chunks_utilities)
static std::pair< CTransactionRef, CTransactionRef > add_children_to_parent(const CTransactionRef parent, 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
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 CompareChunks(Span< const FeeFrac > chunks0, Span< const FeeFrac > chunks1)
Compare the feerate diagrams implied by the provided sorted chunks data.