Bitcoin Core  31.0.0
P2P Digital Currency
coinselection_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2024-present 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>
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;
17 
18 static const int P2WPKH_INPUT_VSIZE = 68;
19 static const int P2WPKH_OUTPUT_VSIZE = 31;
20 
25 {
27  /*rng_fast=*/default_rand,
28  /*change_output_size=*/P2WPKH_OUTPUT_VSIZE,
29  /*change_spend_size=*/P2WPKH_INPUT_VSIZE,
30  /*min_change_target=*/50'000,
31  /*effective_feerate=*/CFeeRate(5000),
32  /*long_term_feerate=*/CFeeRate(10'000),
33  /*discard_feerate=*/CFeeRate(3000),
34  /*tx_noinputs_size=*/11 + P2WPKH_OUTPUT_VSIZE, //static header size + output size
35  /*avoid_partial=*/false,
36  };
37  dcsp.m_change_fee = /*155 sats=*/dcsp.m_effective_feerate.GetFee(dcsp.change_output_size);
38  dcsp.min_viable_change = /*204 sats=*/dcsp.m_discard_feerate.GetFee(dcsp.change_spend_size);
39  dcsp.m_cost_of_change = /*204 + 155 sats=*/dcsp.min_viable_change + dcsp.m_change_fee;
40  dcsp.m_subtract_fee_outputs = false;
41  return dcsp;
42 }
43 
45 
47 static OutputGroup MakeCoin(const CAmount& amount, bool is_eff_value = true, CoinSelectionParams cs_params = default_cs_params, int custom_spending_vsize = P2WPKH_INPUT_VSIZE)
48 {
49  // Always assume that we only have one input
51  tx.vout.resize(1);
52  CAmount fees = cs_params.m_effective_feerate.GetFee(custom_spending_vsize);
53  tx.vout[0].nValue = amount + int(is_eff_value) * fees;
54  tx.nLockTime = next_lock_time++; // so all transactions get different hashes
55  OutputGroup group(cs_params);
56  group.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);
57  return group;
58 }
59 
61 static void AddCoins(std::vector<OutputGroup>& utxo_pool, std::vector<CAmount> coins, CoinSelectionParams cs_params = default_cs_params)
62 {
63  for (CAmount c : coins) {
64  utxo_pool.push_back(MakeCoin(c, true, cs_params));
65  }
66 }
67 
69 static void AddDuplicateCoins(std::vector<OutputGroup>& utxo_pool, int count, int amount, CoinSelectionParams cs_params = default_cs_params) {
70  for (int i = 0 ; i < count; ++i) {
71  utxo_pool.push_back(MakeCoin(amount, true, cs_params));
72  }
73 }
74 
77 static bool HaveEquivalentValues(const SelectionResult& a, const SelectionResult& b)
78 {
79  std::vector<CAmount> a_amts;
80  std::vector<CAmount> b_amts;
81  for (const auto& coin : a.GetInputSet()) {
82  a_amts.push_back(coin->txout.nValue);
83  }
84  for (const auto& coin : b.GetInputSet()) {
85  b_amts.push_back(coin->txout.nValue);
86  }
87  std::sort(a_amts.begin(), a_amts.end());
88  std::sort(b_amts.begin(), b_amts.end());
89 
90  auto ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());
91  return ret.first == a_amts.end() && ret.second == b_amts.end();
92 }
93 
94 static std::string InputAmountsToString(const SelectionResult& selection)
95 {
96  return "[" + util::Join(selection.GetInputSet(), " ", [](const auto& input){ return util::ToString(input->txout.nValue);}) + "]";
97 }
98 
99 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, const int custom_spending_vsize = P2WPKH_INPUT_VSIZE, const int max_selection_weight = MAX_STANDARD_TX_WEIGHT)
100 {
102  CAmount expected_amount = 0;
103  for (CAmount input_amount : expected_input_amounts) {
104  OutputGroup group = MakeCoin(input_amount, true, cs_params, custom_spending_vsize);
105  expected_amount += group.m_value;
106  expected_result.AddInput(group);
107  }
108 
109  const auto result = SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/default_cs_params.m_cost_of_change, max_selection_weight);
110  BOOST_CHECK_MESSAGE(result, "Falsy result in BnB-Success: " + test_title);
111  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)));
112  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()));
113 }
114 
115 static void TestBnBFail(std::string test_title, std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, int max_selection_weight = MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded = false)
116 {
117  const auto result = SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/default_cs_params.m_cost_of_change, max_selection_weight);
118  BOOST_CHECK_MESSAGE(!result, "BnB-Fail: " + test_title);
119  bool max_weight_exceeded = util::ErrorString(result).original.find("The inputs size exceeds the maximum weight") != std::string::npos;
120  BOOST_CHECK(expect_max_weight_exceeded == max_weight_exceeded);
121 }
122 
124 {
125  std::vector<int> feerates = {0, 1, 5'000, 10'000, 25'000, 59'764, 500'000, 999'000, 1'500'000};
126 
127  for (int feerate : feerates) {
128  std::vector<OutputGroup> utxo_pool;
129 
131  cs_params.m_effective_feerate = CFeeRate{feerate};
132 
133  // Fail for empty UTXO pool
134  TestBnBFail("Empty UTXO pool", utxo_pool, /*selection_target=*/1 * CENT);
135 
136  AddCoins(utxo_pool, {1 * CENT, 3 * CENT, 5 * CENT}, cs_params);
137 
138  // Simple success cases
139  TestBnBSuccess("Select smallest UTXO", utxo_pool, /*selection_target=*/1 * CENT, /*expected_input_amounts=*/{1 * CENT}, cs_params);
140  TestBnBSuccess("Select middle UTXO", utxo_pool, /*selection_target=*/3 * CENT, /*expected_input_amounts=*/{3 * CENT}, cs_params);
141  TestBnBSuccess("Select biggest UTXO", utxo_pool, /*selection_target=*/5 * CENT, /*expected_input_amounts=*/{5 * CENT}, cs_params);
142  TestBnBSuccess("Select two UTXOs", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, cs_params);
143  TestBnBSuccess("Select all UTXOs", utxo_pool, /*selection_target=*/9 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT, 5 * CENT}, cs_params);
144 
145  // BnB finds changeless solution while overshooting by up to cost_of_change
146  TestBnBSuccess("Select upper bound", utxo_pool, /*selection_target=*/4 * CENT - default_cs_params.m_cost_of_change, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, cs_params);
147 
148  // BnB fails to find changeless solution when overshooting by cost_of_change + 1 sat
149  TestBnBFail("Overshoot upper bound", utxo_pool, /*selection_target=*/4 * CENT - default_cs_params.m_cost_of_change - 1);
150 
151  TestBnBSuccess("Select max weight", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, cs_params, /*custom_spending_vsize=*/P2WPKH_INPUT_VSIZE, /*max_selection_weight=*/4 * 2 * P2WPKH_INPUT_VSIZE);
152 
153  TestBnBFail("Exceed max weight", utxo_pool, /*selection_target=*/4 * CENT, /*max_selection_weight=*/4 * 2 * P2WPKH_INPUT_VSIZE - 1, /*expect_max_weight_exceeded=*/true);
154 
155  // Simple cases without BnB solution
156  TestBnBFail("Smallest combination too big", utxo_pool, /*selection_target=*/0.5 * CENT);
157  TestBnBFail("No UTXO combination in target window", utxo_pool, /*selection_target=*/7 * CENT);
158  TestBnBFail("Select more than available", utxo_pool, /*selection_target=*/10 * CENT);
159 
160  // Test skipping of equivalent input sets
161  std::vector<OutputGroup> clone_pool;
162  AddCoins(clone_pool, {2 * CENT, 7 * CENT, 7 * CENT}, cs_params);
163  AddDuplicateCoins(clone_pool, 50'000, 5 * CENT, cs_params);
164  TestBnBSuccess("Skip equivalent input sets", clone_pool, /*selection_target=*/16 * CENT, /*expected_input_amounts=*/{2 * CENT, 7 * CENT, 7 * CENT}, cs_params);
165 
166  /* Test BnB attempt limit (`TOTAL_TRIES`)
167  *
168  * Generally, on a diverse UTXO pool BnB will quickly pass over UTXOs bigger than the target and then start
169  * combining small counts of UTXOs that in sum remain under the selection_target+cost_of_change. When there are
170  * multiple UTXOs that have matching amount and cost, combinations with equivalent input sets are skipped. The
171  * UTXO pool for this test is specifically crafted to create as much branching as possible. The selection target
172  * is 8 CENT while all UTXOs are slightly bigger than 1 CENT. The smallest eight are 100,000…100,007 sats, while
173  * the larger nine are 100,368…100,375 (i.e., 100,008…100,016 sats plus cost_of_change (359 sats)).
174  *
175  * Because BnB will only select input sets that fall between selection_target and selection_target +
176  * cost_of_change, and the search traverses the UTXO pool from large to small amounts, the search will visit
177  * every single combination of eight inputs. All except the last combination will overshoot by more than
178  * cost_of_change on the eighth input, because the larger nine inputs each exceed 1 CENT by more than
179  * cost_of_change. Only the last combination consisting of the eight smallest UTXOs falls into the target
180  * window.
181  */
182  std::vector<OutputGroup> doppelganger_pool;
183  std::vector<CAmount> doppelgangers;
184  std::vector<CAmount> expected_inputs;
185  for (int i = 0; i < 17; ++i) {
186  if (i < 8) {
187  // The eight smallest UTXOs can be combined to create expected_result
188  doppelgangers.push_back(1 * CENT + i);
189  expected_inputs.push_back(doppelgangers[i]);
190  } else {
191  // Any eight UTXOs including at least one UTXO with the added cost_of_change will exceed target window
192  doppelgangers.push_back(1 * CENT + default_cs_params.m_cost_of_change + i);
193  }
194  }
195  AddCoins(doppelganger_pool, doppelgangers, cs_params);
196  // Among up to 17 unique UTXOs of similar effective value we will find a solution composed of the eight smallest UTXOs
197  TestBnBSuccess("Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT, /*expected_input_amounts=*/expected_inputs, cs_params);
198 
199  // Starting with 18 unique UTXOs of similar effective value we will not find the solution due to exceeding the attempt limit
200  AddCoins(doppelganger_pool, {1 * CENT + default_cs_params.m_cost_of_change + 17}, cs_params);
201  TestBnBFail("Exhaust looking for smallest 8 of 18 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT);
202  }
203 }
204 
205 BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test)
206 {
207  // Create sets of UTXOs with the same effective amounts at different feerates (but different absolute amounts)
208  std::vector<OutputGroup> low_feerate_pool; // 5 sat/vB (default, and lower than long_term_feerate of 10 sat/vB)
209  AddCoins(low_feerate_pool, {2 * CENT, 3 * CENT, 5 * CENT, 10 * CENT});
210  TestBnBSuccess("Select many inputs at low feerates", low_feerate_pool, /*selection_target=*/10 * CENT, /*expected_input_amounts=*/{2 * CENT, 3 * CENT, 5 * CENT});
211 
212  CoinSelectionParams high_feerate_params = init_default_params();
213  high_feerate_params.m_effective_feerate = CFeeRate{25'000};
214  std::vector<OutputGroup> high_feerate_pool; // 25 sat/vB (greater than long_term_feerate of 10 sat/vB)
215  AddCoins(high_feerate_pool, {2 * CENT, 3 * CENT, 5 * CENT, 10 * CENT}, high_feerate_params);
216  TestBnBSuccess("Select one input at high feerates", high_feerate_pool, /*selection_target=*/10 * CENT, /*expected_input_amounts=*/{10 * CENT}, high_feerate_params);
217 
218  // Add heavy inputs {6, 7} to existing {2, 3, 5, 10}
219  low_feerate_pool.push_back(MakeCoin(6 * CENT, true, default_cs_params, /*custom_spending_vsize=*/500));
220  low_feerate_pool.push_back(MakeCoin(7 * CENT, true, default_cs_params, /*custom_spending_vsize=*/500));
221  TestBnBSuccess("Prefer two heavy inputs over two light inputs at low feerates", low_feerate_pool, /*selection_target=*/13 * CENT, /*expected_input_amounts=*/{6 * CENT, 7 * CENT}, default_cs_params, /*custom_spending_vsize=*/500);
222 
223  high_feerate_pool.push_back(MakeCoin(6 * CENT, true, high_feerate_params, /*custom_spending_vsize=*/500));
224  high_feerate_pool.push_back(MakeCoin(7 * CENT, true, high_feerate_params, /*custom_spending_vsize=*/500));
225  TestBnBSuccess("Prefer two light inputs over two heavy inputs at high feerates", high_feerate_pool, /*selection_target=*/13 * CENT, /*expected_input_amounts=*/{3 * CENT, 10 * CENT}, high_feerate_params);
226 }
227 
229 } // namespace wallet
static void TestBnBFail(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, int max_selection_weight=MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded=false)
int ret
static OutputGroup MakeCoin(const CAmount &amount, bool is_eff_value=true, CoinSelectionParams cs_params=default_cs_params, int custom_spending_vsize=P2WPKH_INPUT_VSIZE)
Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given ...
static const CoinSelectionParams default_cs_params
const OutputSet & GetInputSet() const
Get m_selected_inputs.
static bool HaveEquivalentValues(const SelectionResult &a, const SelectionResult &b)
Check if SelectionResult a is equivalent to SelectionResult b.
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
static const int P2WPKH_OUTPUT_VSIZE
static std::string InputAmountsToString(const SelectionResult &selection)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
Fast randomness source.
Definition: random.h:385
BOOST_AUTO_TEST_SUITE_END()
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
A group of UTXOs paid to the same output script.
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
static int next_lock_time
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
std::vector< CTxOut > vout
Definition: transaction.h:360
Parameters for one iteration of Coin Selection.
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we&#39;re willing to relay/mine.
Definition: policy.h:37
static void AddCoins(std::vector< OutputGroup > &utxo_pool, std::vector< CAmount > coins, CoinSelectionParams cs_params=default_cs_params)
Make multiple OutputGroups with the given values as their effective value.
BOOST_AUTO_TEST_CASE(bnb_test)
static FastRandomContext default_rand
auto result
Definition: common-types.h:74
std::string original
Definition: translation.h:25
static void AddDuplicateCoins(std::vector< OutputGroup > &utxo_pool, int count, int amount, CoinSelectionParams cs_params=default_cs_params)
Make multiple coins that share the same effective value.
static CoinSelectionParams init_default_params()
Default coin selection parameters (dcsp) allow us to only explicitly set parameters when a diverging ...
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac...
Definition: feerate.h:31
static int count
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
A mutable version of CTransaction.
Definition: transaction.h:357
static const int P2WPKH_INPUT_VSIZE
static constexpr CAmount CENT
Definition: setup_common.h:47
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:205
Testing setup that configures a complete environment.
Definition: setup_common.h:121
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:246
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, const int custom_spending_vsize=P2WPKH_INPUT_VSIZE, const int max_selection_weight=MAX_STANDARD_TX_WEIGHT)
#define BOOST_CHECK(expr)
Definition: object.cpp:16