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) 2024-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.5#include <consensus/amount.h>6#include <policy/policy.h>7#include <wallet/coinselection.h>8#include <wallet/test/wallet_test_fixture.h>10#include <boost/test/unit_test.hpp>12namespace wallet {13BOOST_FIXTURE_TEST_SUITE(coinselection_tests, TestingSetup)15static int next_lock_time = 0;16static FastRandomContext default_rand;18static const int P2WPKH_INPUT_VSIZE = 68;19static const int P2WPKH_OUTPUT_VSIZE = 31;21/**22* This set of feerates is used in the tests to test edge cases around the23* default minimum feerate and other potential special cases:24* - zero: 0 s/kvB25* - minimum non-zero s/kvB: 1 s/kvB26* - just below the new default minimum feerate: 99 s/kvB27* - new default minimum feerate: 100 s/kvB28* - old default minimum feerate: 1000 s/kvB29* - a few non-round realistic feerates around default minimum feerate,30* dust feerate, and default LTFRE: 315 s/kvB, 2345 s/kvB, and31* 10'292 s/kvB32* - a high feerate that has been exceeded occasionally: 59'764 s/kvB33* - a huge feerate that is extremely uncommon: 1'500'000 s/kvB */34static const std::vector<int> FEERATES = {0, 1, 99, 100, 315, 1'000, 2'345, 10'292, 59'764, 1'500'000};36/** Default coin selection parameters allow us to only explicitly set37* parameters when a diverging value is relevant in the context of a test,38* without reiterating the defaults in every test. We use P2WPKH input and39* output weights for the change weights. */40static CoinSelectionParams init_cs_params(int eff_feerate = 5000)41{42CoinSelectionParams csp{43/*rng_fast=*/default_rand,44/*change_output_size=*/P2WPKH_OUTPUT_VSIZE,45/*change_spend_size=*/P2WPKH_INPUT_VSIZE,46/*min_change_target=*/50'000,47/*effective_feerate=*/CFeeRate(eff_feerate),48/*long_term_feerate=*/CFeeRate(10'000),49/*discard_feerate=*/CFeeRate(3000),50/*tx_noinputs_size=*/11 + P2WPKH_OUTPUT_VSIZE, //static header size + output size51/*avoid_partial=*/false,52};53csp.m_change_fee = csp.m_effective_feerate.GetFee(csp.change_output_size); // 155 sats for default feerate of 5000 s/kvB54csp.min_viable_change = /*204 sats=*/csp.m_discard_feerate.GetFee(csp.change_spend_size);55csp.m_cost_of_change = csp.min_viable_change + csp.m_change_fee; // 204 + 155 sats for default feerate of 5000 s/kvB56csp.m_subtract_fee_outputs = false;57return csp;58}60static const CoinSelectionParams default_cs_params = init_cs_params();62/** Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given amount (`is_eff_value = false`). */63static OutputGroup MakeCoin(const CAmount& amount, bool is_eff_value = true, CoinSelectionParams cs_params = default_cs_params, int custom_spending_vsize = P2WPKH_INPUT_VSIZE)64{65// Always assume that we only have one input66CMutableTransaction tx;67tx.vout.resize(1);68CAmount fees = cs_params.m_effective_feerate.GetFee(custom_spending_vsize);69tx.vout[0].nValue = amount + int(is_eff_value) * fees;70tx.nLockTime = next_lock_time++; // so all transactions get different hashes71OutputGroup group(cs_params);72group.Insert(std::make_shared<COutput>(COutPoint(tx.GetHash(), 0), tx.vout.at(0), /*depth=*/1, /*input_bytes=*/custom_spending_vsize, /*solvable=*/true, /*safe=*/true, /*time=*/0, /*from_me=*/false, /*fees=*/fees), /*ancestors=*/0, /*cluster_count=*/0);73return group;74}76/** Make multiple OutputGroups with the given values as their effective value */77static void AddCoins(std::vector<OutputGroup>& utxo_pool, std::vector<CAmount> coins, CoinSelectionParams cs_params = default_cs_params)78{79for (CAmount c : coins) {80utxo_pool.push_back(MakeCoin(c, true, cs_params));81}82}84/** Make multiple coins that share the same effective value */85static void AddDuplicateCoins(std::vector<OutputGroup>& utxo_pool, int count, int amount, CoinSelectionParams cs_params = default_cs_params) {86for (int i = 0 ; i < count; ++i) {87utxo_pool.push_back(MakeCoin(amount, true, cs_params));88}89}91/** Check if SelectionResult a is equivalent to SelectionResult b.92* Two results are equivalent if they are composed of the same input values, even if they have different inputs (i.e., same value, different prevout) */93static bool HaveEquivalentValues(const SelectionResult& a, const SelectionResult& b)94{95std::vector<CAmount> a_amts;96std::vector<CAmount> b_amts;97for (const auto& coin : a.GetInputSet()) {98a_amts.push_back(coin->txout.nValue);99}100for (const auto& coin : b.GetInputSet()) {101b_amts.push_back(coin->txout.nValue);102}103std::sort(a_amts.begin(), a_amts.end());104std::sort(b_amts.begin(), b_amts.end());106auto ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());107return ret.first == a_amts.end() && ret.second == b_amts.end();108}110static std::string InputAmountsToString(const SelectionResult& selection)111{112return "[" + util::Join(selection.GetInputSet(), " ", [](const auto& input){ return util::ToString(input->txout.nValue);}) + "]";113}115static void TestBnBSuccess(std::string test_title, std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const std::vector<CAmount>& expected_input_amounts, size_t expected_attempts, const CoinSelectionParams& cs_params = default_cs_params, const int custom_spending_vsize = P2WPKH_INPUT_VSIZE, const int max_selection_weight = MAX_STANDARD_TX_WEIGHT)116{117SelectionResult expected_result(CAmount(0), SelectionAlgorithm::BNB);118CAmount expected_amount = 0;119for (CAmount input_amount : expected_input_amounts) {120OutputGroup group = MakeCoin(input_amount, true, cs_params, custom_spending_vsize);121expected_amount += group.m_value;122expected_result.AddInput(group);123}125const auto result = SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/cs_params.m_cost_of_change, max_selection_weight);126BOOST_CHECK_MESSAGE(result, "Falsy result in BnB-Success: " + test_title);127BOOST_CHECK_MESSAGE(HaveEquivalentValues(expected_result, *result), strprintf("Result mismatch in BnB-Success: %s. Expected %s, but got %s", test_title, InputAmountsToString(expected_result), InputAmountsToString(*result)));128BOOST_CHECK_MESSAGE(result->GetSelectedValue() == expected_amount, strprintf("Selected amount mismatch in BnB-Success: %s. Expected %d, but got %d", test_title, expected_amount, result->GetSelectedValue()));129BOOST_CHECK_MESSAGE(result->GetWeight() <= max_selection_weight, strprintf("Selected weight is higher than permitted in BnB-Success: %s. Expected %d, but got %d", test_title, max_selection_weight, result->GetWeight()));130BOOST_CHECK_MESSAGE(result->GetSelectionsEvaluated() == expected_attempts, strprintf("Unexpected number of attempts in BnB-Success: %s. Expected %i attempts, but got %i", test_title, expected_attempts, result->GetSelectionsEvaluated()));131}133static void TestBnBFail(std::string test_title, std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CoinSelectionParams& cs_params = default_cs_params, int max_selection_weight = MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded = false)134{135const auto result = SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/cs_params.m_cost_of_change, max_selection_weight);136BOOST_CHECK_MESSAGE(!result, "BnB-Fail: " + test_title);137bool max_weight_exceeded = util::ErrorString(result).original.find("The inputs size exceeds the maximum weight") != std::string::npos;138BOOST_CHECK(expect_max_weight_exceeded == max_weight_exceeded);139}141BOOST_AUTO_TEST_CASE(bnb_test)142{143for (int feerate : FEERATES) {144std::vector<OutputGroup> utxo_pool;146const CoinSelectionParams cs_params = init_cs_params(feerate);148TestBnBFail("Empty UTXO pool", utxo_pool, /*selection_target=*/1 * CENT, cs_params);150AddCoins(utxo_pool, {1 * CENT, 3 * CENT, 5 * CENT}, cs_params);152// Simple success cases153TestBnBSuccess("Select smallest UTXO", utxo_pool, /*selection_target=*/1 * CENT, /*expected_input_amounts=*/{1 * CENT}, /*expected_attempts=*/3, cs_params);154TestBnBSuccess("Select middle UTXO", utxo_pool, /*selection_target=*/3 * CENT, /*expected_input_amounts=*/{3 * CENT}, /*expected_attempts=*/3, cs_params);155TestBnBSuccess("Select biggest UTXO", utxo_pool, /*selection_target=*/5 * CENT, /*expected_input_amounts=*/{5 * CENT}, /*expected_attempts=*/2, cs_params);156TestBnBSuccess("Select two UTXOs", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, /*expected_attempts=*/4, cs_params);157TestBnBSuccess("Select all UTXOs", utxo_pool, /*selection_target=*/9 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT, 5 * CENT}, /*expected_attempts=*/5, cs_params);159// BnB finds changeless solution while overshooting by up to cost_of_change160TestBnBSuccess("Select upper bound", utxo_pool, /*selection_target=*/4 * CENT - cs_params.m_cost_of_change, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, /*expected_attempts=*/4, cs_params);162// BnB fails to find changeless solution when overshooting by cost_of_change + 1 sat163TestBnBFail("Overshoot upper bound", utxo_pool, /*selection_target=*/4 * CENT - cs_params.m_cost_of_change - 1, cs_params);165TestBnBSuccess("Select max weight", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, /*expected_attempts=*/4, cs_params, /*custom_spending_vsize=*/P2WPKH_INPUT_VSIZE, /*max_selection_weight=*/4 * 2 * P2WPKH_INPUT_VSIZE);167TestBnBFail("Exceed max weight", utxo_pool, /*selection_target=*/4 * CENT, cs_params, /*max_selection_weight=*/4 * 2 * P2WPKH_INPUT_VSIZE - 1, /*expect_max_weight_exceeded=*/true);169// Simple cases without BnB solution170TestBnBFail("Smallest combination too big", utxo_pool, /*selection_target=*/0.5 * CENT, cs_params);171TestBnBFail("No UTXO combination in target window", utxo_pool, /*selection_target=*/7 * CENT, cs_params);172TestBnBFail("Select more than available", utxo_pool, /*selection_target=*/10 * CENT, cs_params);174// Test skipping of equivalent input sets175std::vector<OutputGroup> clone_pool;176AddCoins(clone_pool, {2 * CENT, 7 * CENT, 7 * CENT}, cs_params);177AddDuplicateCoins(clone_pool, /*count=*/50'000, /*amount=*/5 * CENT, cs_params);178TestBnBSuccess("Skip equivalent input sets", clone_pool, /*selection_target=*/16 * CENT, /*expected_input_amounts=*/{2 * CENT, 7 * CENT, 7 * CENT}, /*expected_attempts=*/16, cs_params);180/* Test BnB attempt limit (`TOTAL_TRIES`)181*182* Generally, on a diverse UTXO pool BnB will quickly pass over UTXOs bigger than the target and then start183* combining small counts of UTXOs that in sum remain under the selection_target+cost_of_change. When there are184* multiple UTXOs that have matching amount and cost, combinations with equivalent input sets are skipped. The185* UTXO pool for this test is specifically crafted to create as much branching as possible. The selection target186* is 8 CENT while all UTXOs are slightly bigger than 1 CENT. The smallest eight are 100,000…100,007 sats, while187* the larger nine are 100,368…100,375 (i.e., 100,008…100,016 sats plus cost_of_change (359 sats)).188*189* Because BnB will only select input sets that fall between selection_target and selection_target +190* cost_of_change, and the search traverses the UTXO pool from large to small amounts, the search will visit191* every single combination of eight inputs. All except the last combination will overshoot by more than192* cost_of_change on the eighth input, because the larger nine inputs each exceed 1 CENT by more than193* cost_of_change. Only the last combination consisting of the eight smallest UTXOs falls into the target194* window.195*/196std::vector<OutputGroup> doppelganger_pool;197std::vector<CAmount> doppelgangers;198std::vector<CAmount> expected_inputs;199for (int i = 0; i < 17; ++i) {200if (i < 8) {201// The eight smallest UTXOs can be combined to create expected_result202doppelgangers.push_back(1 * CENT + i);203expected_inputs.push_back(doppelgangers[i]);204} else {205// Any eight UTXOs including at least one UTXO with the added cost_of_change will exceed target window206doppelgangers.push_back(1 * CENT + cs_params.m_cost_of_change + i);207}208}209AddCoins(doppelganger_pool, doppelgangers, cs_params);210// Among 17 unique UTXOs of similar effective value we will find a solution composed of the eight smallest UTXOs211TestBnBSuccess("Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT, /*expected_input_amounts=*/expected_inputs, /*expected_attempts=*/51'765, cs_params);213// Among up to 18 unique UTXOs of similar effective value we will find a solution composed of the eight smallest UTXOs214AddCoins(doppelganger_pool, {1 * CENT + cs_params.m_cost_of_change + 17}, cs_params);215TestBnBSuccess("Combine smallest 8 of 18 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT, /*expected_input_amounts=*/expected_inputs, /*expected_attempts=*/87'957, cs_params);217// Starting with 19 unique UTXOs of similar effective value we will not find the solution due to exceeding the attempt limit218AddCoins(doppelganger_pool, {1 * CENT + cs_params.m_cost_of_change + 18}, cs_params);219TestBnBFail("Exhaust looking for smallest 8 of 19 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT, cs_params);220}221}223BOOST_AUTO_TEST_CASE(bnb_exhaustion_with_solution_test)224{225std::vector<OutputGroup> utxo_pool;226utxo_pool.reserve(19);228CAmount selection_target{800'000};229// A hard case with no exact-match solution: BnB must still report that the algorithm did not complete once the230// search is pushed into the attempt limit, even though it finds a solution within cost_of_change of the target.231for (size_t i = 0; i < 19; ++i) {232utxo_pool.push_back(MakeCoin(100'000 + i, /*is_eff_value=*/true, default_cs_params));233}235const auto result{SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/default_cs_params.m_cost_of_change, MAX_STANDARD_TX_WEIGHT)};236BOOST_CHECK_MESSAGE(result, "Falsy result in BnB-Success: Exhaust with early solution");237BOOST_CHECK(result->GetSelectedEffectiveValue() > selection_target + 28);238BOOST_CHECK_EQUAL(result->GetInputSet().size(), 8U);239BOOST_CHECK_EQUAL(result->GetSelectionsEvaluated(), 100'000U);240BOOST_CHECK(!result->GetAlgoCompleted());241}243BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test)244{245// Create sets of UTXOs with the same effective amounts at different feerates (but different absolute amounts)246std::vector<OutputGroup> low_feerate_pool; // 5 sat/vB (default, and lower than long_term_feerate of 10 sat/vB)247AddCoins(low_feerate_pool, {2 * CENT, 3 * CENT, 5 * CENT, 10 * CENT});248TestBnBSuccess("Select many inputs at low feerates", low_feerate_pool, /*selection_target=*/10 * CENT, /*expected_input_amounts=*/{2 * CENT, 3 * CENT, 5 * CENT}, /*expected_attempts=*/6);250const CoinSelectionParams high_feerate_params = init_cs_params(/*eff_feerate=*/25'000);
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.