diff --git a/src/wallet/test/coinselection_tests.cpp b/src/wallet/test/coinselection_tests.cpp index c0232e91f05004..36806d2c263a16 100644 --- a/src/wallet/test/coinselection_tests.cpp +++ b/src/wallet/test/coinselection_tests.cpp @@ -91,9 +91,9 @@ static COutput MakeCoin(const CAmount& amount, bool is_eff_value = true, int nIn return COutput{COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ custom_spending_vsize, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, /*fees=*/ fees}; } -static void AddCoins(std::vector& utxo_pool, std::vector coins) { +static void AddCoins(std::vector& utxo_pool, std::vector coins, CFeeRate feerate = default_cs_params.m_effective_feerate) { for (int c : coins) { - utxo_pool.push_back(MakeCoin(c)); + utxo_pool.push_back(MakeCoin(c, true, 0, feerate)); } } @@ -103,13 +103,13 @@ std::optional SelectCoinsBnB(std::vector& utxo_pool, c return res ? std::optional(*res) : std::nullopt; } -static void TestBnBSuccess(std::string test_title, std::vector& utxo_pool, const CAmount& selection_target, const std::vector& expected_input_amounts) +static void TestBnBSuccess(std::string test_title, std::vector& utxo_pool, const CAmount& selection_target, const std::vector& expected_input_amounts, const CFeeRate& feerate = default_cs_params.m_effective_feerate ) { SelectionResult expected_result(CAmount(0), SelectionAlgorithm::BNB); CAmount expected_amount = 0; for (int input_amount : expected_input_amounts) { OutputGroup group; - COutput coin = MakeCoin(input_amount); + COutput coin = MakeCoin(input_amount, true, 0, feerate); expected_amount += coin.txout.nValue; group.Insert(std::make_shared(coin), /*ancestors=*/ 0, /*descendants=*/ 0); expected_result.AddInput(group); @@ -138,5 +138,18 @@ BOOST_AUTO_TEST_CASE(bnb_test) TestBnBSuccess("Select upper bound", utxo_pool, /*selection_target=*/ 9 * CENT - default_cs_params.m_cost_of_change, /*expected_input_amounts=*/ {1 * CENT, 3 * CENT, 5 * CENT}); } +BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test) +{ + // Create sets of UTXOs with the same effective amounts at different feerates (but different absolute amounts) + std::vector low_feerate_pool; // 5 sat/vB + AddCoins(low_feerate_pool, {2 * CENT, 3 * CENT, 5 * CENT, 10 * CENT}); + TestBnBSuccess("Select many inputs at low feerates", low_feerate_pool, /*selection_target=*/ 10 * CENT, /*expected_input_amounts=*/ {10 * CENT}); +// TestBnBSuccess("Select many inputs at low feerates", low_feerate_pool, /*selection_target=*/ 10 * CENT, /*expected_input_amounts=*/ {2 * CENT, 3 * CENT, 5 * CENT}); + + std::vector high_feerate_pool; // 25 sat/vB + AddCoins(high_feerate_pool, {2 * CENT, 3 * CENT, 5 * CENT, 10 * CENT}, CFeeRate{25'000}); + TestBnBSuccess("Select one input at high feerates", high_feerate_pool, /*selection_target=*/ 10 * CENT, /*expected_input_amounts=*/ {10 * CENT}, CFeeRate{25'000}); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace wallet