|
| 1 | +// Copyright (c) 2024 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 | + |
| 5 | +#include <consensus/amount.h> |
| 6 | +#include <policy/policy.h> |
| 7 | +#include <wallet/coinselection.h> |
| 8 | +#include <wallet/test/wallet_test_fixture.h> |
| 9 | + |
| 10 | +#include <boost/test/unit_test.hpp> |
| 11 | + |
| 12 | +namespace wallet { |
| 13 | +BOOST_FIXTURE_TEST_SUITE(coinselection_tests, TestingSetup) |
| 14 | + |
| 15 | +static int next_lock_time = 0; |
| 16 | +static FastRandomContext default_rand; |
| 17 | + |
| 18 | +/** Default coin selection parameters (dcsp) allow us to only explicitly set |
| 19 | + * parameters when a diverging value is relevant in the context of a test. |
| 20 | + * We use P2WPKH input and output weights for the change weights. */ |
| 21 | +static CoinSelectionParams init_default_params() |
| 22 | +{ |
| 23 | + CoinSelectionParams dcsp{ |
| 24 | + /*rng_fast*/default_rand, |
| 25 | + /*change_output_size=*/31, |
| 26 | + /*change_spend_size=*/68, |
| 27 | + /*min_change_target=*/50'000, |
| 28 | + /*effective_feerate=*/CFeeRate(5000), |
| 29 | + /*long_term_feerate=*/CFeeRate(10'000), |
| 30 | + /*discard_feerate=*/CFeeRate(3000), |
| 31 | + /*tx_noinputs_size=*/11 + 31, //static header size + output size |
| 32 | + /*avoid_partial=*/false, |
| 33 | + }; |
| 34 | + dcsp.m_change_fee = /*155 sats=*/dcsp.m_effective_feerate.GetFee(dcsp.change_output_size); |
| 35 | + dcsp.min_viable_change = /*204 sats=*/dcsp.m_discard_feerate.GetFee(dcsp.change_spend_size); |
| 36 | + dcsp.m_cost_of_change = /*204 + 155 sats=*/dcsp.min_viable_change + dcsp.m_change_fee; |
| 37 | + dcsp.m_subtract_fee_outputs = false; |
| 38 | + return dcsp; |
| 39 | +} |
| 40 | + |
| 41 | +static const CoinSelectionParams default_cs_params = init_default_params(); |
| 42 | + |
| 43 | +/** Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given amount (`is_eff_value = false`). */ |
| 44 | +static OutputGroup MakeCoin(const CAmount& amount, bool is_eff_value = true, CoinSelectionParams cs_params = default_cs_params, int custom_spending_vsize = 68) |
| 45 | +{ |
| 46 | + // Always assume that we only have one input |
| 47 | + CMutableTransaction tx; |
| 48 | + tx.vout.resize(1); |
| 49 | + CAmount fees = cs_params.m_effective_feerate.GetFee(custom_spending_vsize); |
| 50 | + tx.vout[0].nValue = amount + int(is_eff_value) * fees; |
| 51 | + tx.nLockTime = next_lock_time++; // so all transactions get different hashes |
| 52 | + OutputGroup group(cs_params); |
| 53 | + group.Insert(std::make_shared<COutput>(COutPoint(tx.GetHash(), 0), tx.vout.at(0), /*depth=*/1, /*input_bytes=*/custom_spending_vsize, /*spendable=*/true, /*solvable=*/true, /*safe=*/true, /*time=*/0, /*from_me=*/false, /*fees=*/fees), /*ancestors=*/0, /*descendants=*/0); |
| 54 | + return group; |
| 55 | +} |
| 56 | + |
| 57 | +/** Make multiple OutputGroups with the given values as their effective value */ |
| 58 | +static void AddCoins(std::vector<OutputGroup>& utxo_pool, std::vector<CAmount> coins, CoinSelectionParams cs_params = default_cs_params) |
| 59 | +{ |
| 60 | + for (CAmount c : coins) { |
| 61 | + utxo_pool.push_back(MakeCoin(c, true, cs_params)); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** Check if SelectionResult a is equivalent to SelectionResult b. |
| 66 | + * 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) */ |
| 67 | +static bool HaveEquivalentValues(const SelectionResult& a, const SelectionResult& b) |
| 68 | +{ |
| 69 | + std::vector<CAmount> a_amts; |
| 70 | + std::vector<CAmount> b_amts; |
| 71 | + for (const auto& coin : a.GetInputSet()) { |
| 72 | + a_amts.push_back(coin->txout.nValue); |
| 73 | + } |
| 74 | + for (const auto& coin : b.GetInputSet()) { |
| 75 | + b_amts.push_back(coin->txout.nValue); |
| 76 | + } |
| 77 | + std::sort(a_amts.begin(), a_amts.end()); |
| 78 | + std::sort(b_amts.begin(), b_amts.end()); |
| 79 | + |
| 80 | + auto ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin()); |
| 81 | + return ret.first == a_amts.end() && ret.second == b_amts.end(); |
| 82 | +} |
| 83 | + |
| 84 | +static std::string InputAmountsToString(const SelectionResult& selection) |
| 85 | +{ |
| 86 | + return "[" + util::Join(selection.GetInputSet(), " ", [](const auto& input){ return util::ToString(input->txout.nValue);}) + "]"; |
| 87 | +} |
| 88 | + |
| 89 | +static void TestBnBSuccess(std::string test_title, std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const std::vector<CAmount>& expected_input_amounts, const CoinSelectionParams& cs_params = default_cs_params) |
| 90 | +{ |
| 91 | + SelectionResult expected_result(CAmount(0), SelectionAlgorithm::BNB); |
| 92 | + CAmount expected_amount = 0; |
| 93 | + for (CAmount input_amount : expected_input_amounts) { |
| 94 | + OutputGroup group = MakeCoin(input_amount, true, cs_params); |
| 95 | + expected_amount += group.m_value; |
| 96 | + expected_result.AddInput(group); |
| 97 | + } |
| 98 | + |
| 99 | + const auto result = SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/default_cs_params.m_cost_of_change, /*max_selection_weight=*/MAX_STANDARD_TX_WEIGHT); |
| 100 | + BOOST_CHECK_MESSAGE(result, "Falsy result in BnB-Success: " + test_title); |
| 101 | + BOOST_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))); |
| 102 | + BOOST_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())); |
| 103 | +} |
| 104 | + |
| 105 | +BOOST_AUTO_TEST_CASE(bnb_test) |
| 106 | +{ |
| 107 | + std::vector<OutputGroup> utxo_pool; |
| 108 | + AddCoins(utxo_pool, {1 * CENT, 3 * CENT, 5 * CENT}); |
| 109 | + |
| 110 | + // Simple success cases |
| 111 | + TestBnBSuccess("Select smallest UTXO", utxo_pool, /*selection_target=*/1 * CENT, /*expected_input_amounts=*/{1 * CENT}); |
| 112 | + TestBnBSuccess("Select middle UTXO", utxo_pool, /*selection_target=*/3 * CENT, /*expected_input_amounts=*/{3 * CENT}); |
| 113 | + TestBnBSuccess("Select biggest UTXO", utxo_pool, /*selection_target=*/5 * CENT, /*expected_input_amounts=*/{5 * CENT}); |
| 114 | + TestBnBSuccess("Select two UTXOs", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}); |
| 115 | + TestBnBSuccess("Select all UTXOs", utxo_pool, /*selection_target=*/9 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT, 5 * CENT}); |
| 116 | + |
| 117 | + // BnB finds changeless solution while overshooting by up to cost_of_change |
| 118 | + TestBnBSuccess("Select upper bound", utxo_pool, /*selection_target=*/4 * CENT - default_cs_params.m_cost_of_change, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}); |
| 119 | +} |
| 120 | + |
| 121 | +BOOST_AUTO_TEST_SUITE_END() |
| 122 | +} // namespace wallet |
0 commit comments