Skip to content

Commit 2bafc46

Browse files
committed
test: Recreate simple BnB success tests
Recreates the tests in a new test suite coinselection_tests.cpp that is based on UTXOs being created per their effective values rather than nominal values and uses transactions with non-zero feerates.
1 parent c5e44a0 commit 2bafc46

File tree

3 files changed

+123
-45
lines changed

3 files changed

+123
-45
lines changed

src/wallet/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ target_sources(test_bitcoin
1010
wallet_test_fixture.cpp
1111
db_tests.cpp
1212
coinselector_tests.cpp
13+
coinselection_tests.cpp
1314
feebumper_tests.cpp
1415
group_outputs_tests.cpp
1516
init_tests.cpp
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

src/wallet/test/coinselector_tests.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -208,59 +208,14 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
208208
add_coin(3 * CENT, 3, utxo_pool);
209209
add_coin(4 * CENT, 4, utxo_pool);
210210

211-
// Select 1 Cent
212-
add_coin(1 * CENT, 1, expected_result);
213-
const auto result1 = SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT);
214-
BOOST_CHECK(result1);
215-
BOOST_CHECK(EquivalentResult(expected_result, *result1));
216-
BOOST_CHECK_EQUAL(result1->GetSelectedValue(), 1 * CENT);
217-
expected_result.Clear();
218-
219-
// Select 2 Cent
220-
add_coin(2 * CENT, 2, expected_result);
221-
const auto result2 = SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, 0.5 * CENT);
222-
BOOST_CHECK(result2);
223-
BOOST_CHECK(EquivalentResult(expected_result, *result2));
224-
BOOST_CHECK_EQUAL(result2->GetSelectedValue(), 2 * CENT);
225-
expected_result.Clear();
226-
227-
// Select 5 Cent
228-
add_coin(3 * CENT, 3, expected_result);
229-
add_coin(2 * CENT, 2, expected_result);
230-
const auto result3 = SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, 0.5 * CENT);
231-
BOOST_CHECK(result3);
232-
BOOST_CHECK(EquivalentResult(expected_result, *result3));
233-
BOOST_CHECK_EQUAL(result3->GetSelectedValue(), 5 * CENT);
234-
expected_result.Clear();
235-
236211
// Select 11 Cent, not possible
237212
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 11 * CENT, 0.5 * CENT));
238213
expected_result.Clear();
239214

240-
// Cost of change is greater than the difference between target value and utxo sum
241-
add_coin(1 * CENT, 1, expected_result);
242-
const auto result4 = SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0.5 * CENT);
243-
BOOST_CHECK(result4);
244-
BOOST_CHECK_EQUAL(result4->GetSelectedValue(), 1 * CENT);
245-
BOOST_CHECK(EquivalentResult(expected_result, *result4));
246-
expected_result.Clear();
247-
248215
// Cost of change is less than the difference between target value and utxo sum
249216
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0));
250217
expected_result.Clear();
251218

252-
// Select 10 Cent
253-
add_coin(5 * CENT, 5, utxo_pool);
254-
add_coin(4 * CENT, 4, expected_result);
255-
add_coin(3 * CENT, 3, expected_result);
256-
add_coin(2 * CENT, 2, expected_result);
257-
add_coin(1 * CENT, 1, expected_result);
258-
const auto result5 = SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 0.5 * CENT);
259-
BOOST_CHECK(result5);
260-
BOOST_CHECK(EquivalentResult(expected_result, *result5));
261-
BOOST_CHECK_EQUAL(result5->GetSelectedValue(), 10 * CENT);
262-
expected_result.Clear();
263-
264219
// Select 0.25 Cent, not possible
265220
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.25 * CENT, 0.5 * CENT));
266221
expected_result.Clear();

0 commit comments

Comments
 (0)