From 47f3b559796ba7693b51590de482bbfd4afe8e34 Mon Sep 17 00:00:00 2001 From: Murch Date: Mon, 8 Jan 2024 13:04:29 -0500 Subject: [PATCH] opt: Skip over barren combinations of tiny UTXOs Given a lot of small amount UTXOs it is possible that the lookahead indicates sufficient funds, but any combination of them would push us beyond the current best_weight. We can estimate a lower bound for the minimal necessary weight to reach target from the maximal amount and minimal weight in the tail of the UTXO pool: if adding a number of hypothetical UTXOs of this maximum amount and minimum weight would not be able to beat `best_weight`, we can cut the entire subtree. --- src/wallet/coinselection.cpp | 4 ++++ src/wallet/test/coinselector_tests.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 6df8304a220976..9421a07d845615 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -336,6 +337,9 @@ util::Result CoinGrinder(std::vector& utxo_pool, c best_selection_weight = curr_weight; best_selection_amount = curr_amount; } + } else if (!best_selection.empty() && curr_weight + min_tail_weight[curr_selection.back()] * std::ceil((selection_target + change_target - curr_amount) / utxo_pool[curr_selection.back()].GetSelectionAmount()) > best_selection_weight) { + // Compare minimal tail weight and last selected amount with the amount missing to gauge whether a better weight is still possible. + should_cut = true; } if (curr_try >= TOTAL_TRIES) { diff --git a/src/wallet/test/coinselector_tests.cpp b/src/wallet/test/coinselector_tests.cpp index 5774b952e071c4..670ac661edb53d 100644 --- a/src/wallet/test/coinselector_tests.cpp +++ b/src/wallet/test/coinselector_tests.cpp @@ -1178,7 +1178,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests) }); BOOST_CHECK(res); // If this takes more attempts, the implementation has regressed - size_t expected_attempts = 184; + size_t expected_attempts = 37; BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, "Expected " + std::to_string(expected_attempts) + " attempts, but got " + std::to_string(res->GetSelectionsEvaluated())); } @@ -1247,7 +1247,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests) { // ################################################################################################################# - // 6) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts + // 6) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead // ################################################################################################################# SelectionResult expected_result(CAmount(0), SelectionAlgorithm::CG); CAmount target = 1.9L * COIN; @@ -1264,11 +1264,11 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests) return available_coins; }); expected_result.Clear(); - add_coin(1.8 * COIN, 1, expected_result); + add_coin(1 * COIN, 1, expected_result); add_coin(1 * COIN, 2, expected_result); BOOST_CHECK(EquivalentResult(expected_result, *res)); // If this takes more attempts, the implementation has regressed - size_t expected_attempts = 100000; + size_t expected_attempts = 7; BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, "Expected " + std::to_string(expected_attempts) + " attempts, but got " + std::to_string(res->GetSelectionsEvaluated())); }