Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// 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>11#include <test/util/setup_common.h>13#include <boost/test/unit_test.hpp>14#include <optional>15#include <vector>17BOOST_FIXTURE_TEST_SUITE(rbf_tests, BasicTestingSetup)19static inline CTransactionRef make_tx(const std::vector<CTransactionRef>& inputs,20const std::vector<CAmount>& output_values)21{22CMutableTransaction tx = CMutableTransaction();23tx.vin.resize(inputs.size());24tx.vout.resize(output_values.size());25for (size_t i = 0; i < inputs.size(); ++i) {26tx.vin[i].prevout.hash = inputs[i]->GetHash();27tx.vin[i].prevout.n = 0;28// Add a witness so wtxid != txid29CScriptWitness witness;30witness.stack.emplace_back(i + 10);31tx.vin[i].scriptWitness = witness;32}33for (size_t i = 0; i < output_values.size(); ++i) {34tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;35tx.vout[i].nValue = output_values[i];36}37return MakeTransactionRef(tx);38}40static CTransactionRef add_descendants(const CTransactionRef& tx, int32_t num_descendants, CTxMemPool& pool)41EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)42{43AssertLockHeld(::cs_main);44AssertLockHeld(pool.cs);45TestMemPoolEntryHelper entry;46// Assumes this isn't already spent in mempool47auto tx_to_spend = tx;48for (int32_t i{0}; i < num_descendants; ++i) {49auto next_tx = make_tx(/*inputs=*/{tx_to_spend}, /*output_values=*/{(50 - i) * CENT});50TryAddToMempool(pool, entry.FromTx(next_tx));51BOOST_CHECK(pool.GetIter(next_tx->GetHash()).has_value());52tx_to_spend = next_tx;53}54// Return last created tx55return tx_to_spend;56}58BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)59{60CTxMemPool& pool = *Assert(m_node.mempool);61LOCK2(::cs_main, pool.cs);62TestMemPoolEntryHelper entry;64const CAmount low_fee{CENT/100};65const CAmount normal_fee{CENT/10};66const CAmount high_fee{CENT};68// Create a parent tx1 and child tx2 with normal fees:69const auto tx1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {10 * COIN});70TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx1));71const auto tx2 = make_tx(/*inputs=*/ {tx1}, /*output_values=*/ {995 * CENT});72TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx2));74// Create a low-feerate parent tx3 and high-feerate child tx4 (cpfp)75const auto tx3 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ {1099 * CENT});76TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx3));77const auto tx4 = make_tx(/*inputs=*/ {tx3}, /*output_values=*/ {999 * CENT});78TryAddToMempool(pool, entry.Fee(high_fee).FromTx(tx4));80// Create a parent tx5 and child tx6 where both have very low fees81const auto tx5 = make_tx(/*inputs=*/ {m_coinbase_txns[2]}, /*output_values=*/ {1099 * CENT});82TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx5));83const auto tx6 = make_tx(/*inputs=*/ {tx5}, /*output_values=*/ {1098 * CENT});84TryAddToMempool(pool, entry.Fee(low_fee).FromTx(tx6));85// Make tx6's modified fee much higher than its base fee. This should cause it to pass86// the fee-related checks despite being low-feerate.87pool.PrioritiseTransaction(tx6->GetHash(), 1 * COIN);89// Two independent high-feerate transactions, tx7 and tx890const auto tx7 = make_tx(/*inputs=*/ {m_coinbase_txns[3]}, /*output_values=*/ {999 * CENT});91TryAddToMempool(pool, entry.Fee(high_fee).FromTx(tx7));92const auto tx8 = make_tx(/*inputs=*/ {m_coinbase_txns[4]}, /*output_values=*/ {999 * CENT});93TryAddToMempool(pool, entry.Fee(high_fee).FromTx(tx8));95// Will make these two parents of single child96const auto tx11 = make_tx(/*inputs=*/ {m_coinbase_txns[7]}, /*output_values=*/ {995 * CENT});97TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx11));98const auto tx12 = make_tx(/*inputs=*/ {m_coinbase_txns[8]}, /*output_values=*/ {995 * CENT});99TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx12));101// Will make two children of this single parent102const auto tx13 = make_tx(/*inputs=*/ {m_coinbase_txns[9]}, /*output_values=*/ {995 * CENT, 995 * CENT});103TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx13));105const auto entry1_normal = pool.GetIter(tx1->GetHash()).value();106const auto entry2_normal = pool.GetIter(tx2->GetHash()).value();107const auto entry3_low = pool.GetIter(tx3->GetHash()).value();108const auto entry4_high = pool.GetIter(tx4->GetHash()).value();109const auto entry5_low = pool.GetIter(tx5->GetHash()).value();110const auto entry6_low_prioritised = pool.GetIter(tx6->GetHash()).value();111const auto entry7_high = pool.GetIter(tx7->GetHash()).value();112const auto entry8_high = pool.GetIter(tx8->GetHash()).value();114BOOST_CHECK_EQUAL(entry1_normal->GetFee(), normal_fee);115BOOST_CHECK_EQUAL(entry2_normal->GetFee(), normal_fee);116BOOST_CHECK_EQUAL(entry3_low->GetFee(), low_fee);117BOOST_CHECK_EQUAL(entry4_high->GetFee(), high_fee);118BOOST_CHECK_EQUAL(entry5_low->GetFee(), low_fee);119BOOST_CHECK_EQUAL(entry6_low_prioritised->GetFee(), low_fee);120BOOST_CHECK_EQUAL(entry7_high->GetFee(), high_fee);121BOOST_CHECK_EQUAL(entry8_high->GetFee(), high_fee);123CTxMemPool::setEntries set_12_normal{entry1_normal, entry2_normal};124CTxMemPool::setEntries empty_set;126const auto unused_txid = Txid::FromUint256(GetRandHash());128// Tests for EntriesAndTxidsDisjoint129BOOST_CHECK(EntriesAndTxidsDisjoint(empty_set, {tx1->GetHash()}, unused_txid) == std::nullopt);130BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx3->GetHash()}, unused_txid) == std::nullopt);131BOOST_CHECK(EntriesAndTxidsDisjoint({entry2_normal}, {tx2->GetHash()}, unused_txid).has_value());132BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx1->GetHash()}, unused_txid).has_value());133BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx2->GetHash()}, unused_txid).has_value());134// EntriesAndTxidsDisjoint does not calculate descendants of iters_conflicting; it uses whatever135// the caller passed in. As such, no error is returned even though entry2_normal is a descendant of tx1.136BOOST_CHECK(EntriesAndTxidsDisjoint({entry2_normal}, {tx1->GetHash()}, unused_txid) == std::nullopt);138// Tests for PaysForRBF139const CFeeRate incremental_relay_feerate{DEFAULT_INCREMENTAL_RELAY_FEE};140const CFeeRate higher_relay_feerate{2 * DEFAULT_INCREMENTAL_RELAY_FEE};141// Must pay at least as much as the original.142BOOST_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);148BOOST_CHECK(PaysForRBF(high_fee, high_fee - 1, 1, CFeeRate(0), unused_txid).has_value());149BOOST_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 fee151BOOST_CHECK(PaysForRBF(high_fee, high_fee + 1, 11, incremental_relay_feerate, unused_txid).has_value());152BOOST_CHECK(PaysForRBF(high_fee, high_fee + 1, 10, incremental_relay_feerate, unused_txid) == std::nullopt);153BOOST_CHECK(PaysForRBF(high_fee, high_fee + 2, 11, higher_relay_feerate, unused_txid).has_value());154BOOST_CHECK(PaysForRBF(high_fee, high_fee + 4, 20, higher_relay_feerate, unused_txid) == std::nullopt);155BOOST_CHECK(PaysForRBF(low_fee, high_fee, 99999999, incremental_relay_feerate, unused_txid).has_value());156BOOST_CHECK(PaysForRBF(low_fee, high_fee + 99999999, 99999999, incremental_relay_feerate, unused_txid) == std::nullopt);157}159BOOST_FIXTURE_TEST_CASE(rbf_conflicts_calculator, TestChain100Setup)160{161CTxMemPool& pool = *Assert(m_node.mempool);162LOCK2(::cs_main, pool.cs);163TestMemPoolEntryHelper entry;165const CAmount normal_fee{CENT/10};167// Create two parent transactions with 51 outputs each168const int NUM_OUTPUTS = 51;169std::vector<CAmount> output_values;170output_values.reserve(NUM_OUTPUTS);171for (int i = 0; i < NUM_OUTPUTS; ++i) {172output_values.push_back(1 * COIN);173}175const auto parent_tx_1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ output_values);176const auto parent_tx_2 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ output_values);177TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(parent_tx_1));178TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(parent_tx_2));180std::vector<CTransactionRef> direct_children;182// Create individual spends of these outputs183for (const auto& parent_tx : {parent_tx_1, parent_tx_2}) {184for (auto i = 0; i < NUM_OUTPUTS; ++i) {185auto pretx = make_tx(/*inputs=*/ {parent_tx}, /*output_values=*/ {995 * CENT});186CMutableTransaction tx(*pretx);187tx.vin[0].prevout.n = i;188TryAddToMempool(pool, entry.Fee(normal_fee).FromTx(tx));189BOOST_CHECK(pool.GetIter(tx.GetHash()).has_value());190direct_children.push_back(MakeTransactionRef(tx));191}192}194// At this point, we should have 2 clusters in the mempool, each with 52195// transactions.197// parent_tx and all children are in one cluster, so we can have as many198// conflicts within this cluster as we want without violating the RBF conflicts199// limit.200const auto parent_entry_1 = pool.GetIter(parent_tx_1->GetHash()).value();201const auto parent_entry_2 = pool.GetIter(parent_tx_2->GetHash()).value();202const auto conflicting_transaction = make_tx({parent_tx_1, parent_tx_2}, {50 * CENT});203CTxMemPool::setEntries all_conflicts, dummy;204BOOST_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);209dummy.clear();210// Conflicting directly with all those conflicts doesn't change anything.211BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),212/*pool=*/ pool,213/*iters_conflicting=*/ all_conflicts,214/*all_conflicts=*/ dummy) == std::nullopt);215BOOST_CHECK_EQUAL(all_conflicts.size(), dummy.size());216dummy.clear();218// If we mine the parent_tx's, then the clusters split (102 clusters).219pool.removeForBlock({parent_tx_1, parent_tx_2});221// Add some descendants now to each of the direct children (we can do this now that the clusters have split).222for (const auto& child : direct_children) {223add_descendants(child, 10, pool);224}226// We can conflict with 100 different clusters, even if they have lots of transactions.227CTxMemPool::setEntries conflicts;228for (auto i = 0; i < 100; ++i) {229conflicts.insert(pool.GetIter(direct_children[i]->GetHash()).value());230}231BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),232/*pool=*/ pool,233/*iters_conflicting=*/ conflicts,234/*all_conflicts=*/ dummy) == std::nullopt);236// Conflicting with 1 more distinct cluster causes failure, however.237conflicts.insert(pool.GetIter(direct_children[100]->GetHash()).value());238BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),239/*pool=*/ pool,240/*iters_conflicting=*/ conflicts,241/*all_conflicts=*/ dummy).has_value());242}244BOOST_FIXTURE_TEST_CASE(improves_feerate, TestChain100Setup)245{246CTxMemPool& pool = *Assert(m_node.mempool);247LOCK2(::cs_main, pool.cs);248TestMemPoolEntryHelper entry;250const CAmount low_fee{CENT/100};
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.