Skip to content

Commit

Permalink
test: Exhaust search attempts with tiny UTXOs
Browse files Browse the repository at this point in the history
  • Loading branch information
murchandamus committed Feb 2, 2024
1 parent db3832a commit f9a0ddd
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/wallet/test/coinselector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
// 4) Test that two less valuable UTXOs with a combined lower weight are preferred over a more valuable heavier UTXO
// 5) Test finding a solution in a UTXO pool with mixed weights
// 6) Test that the lightest solution among many clones is found
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts

FastRandomContext rand;
CoinSelectionParams dummy_params{ // Only used to provide the 'avoid_partial' flag.
Expand Down Expand Up @@ -1267,6 +1268,32 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
size_t expected_attempts = 42;
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
}

{
// #################################################################################################################
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts
// #################################################################################################################
CAmount target = 1.9L * COIN;
int max_weight = 40000; // WU
const auto& res = CoinGrinder(target, dummy_params, m_node, max_weight, [&](CWallet& wallet) {
CoinsResult available_coins;
add_coin(available_coins, wallet, CAmount(1.8 * COIN), CFeeRate(5000), 144, false, 0, true, 2500);
add_coin(available_coins, wallet, CAmount(1 * COIN), CFeeRate(5000), 144, false, 0, true, 1000);
add_coin(available_coins, wallet, CAmount(1 * COIN), CFeeRate(5000), 144, false, 0, true, 1000);
for (int j = 0; j < 100; ++j) {
// make a 100 unique coins only differing by one sat
add_coin(available_coins, wallet, CAmount(0.01 * COIN + j), CFeeRate(5000), 144, false, 0, true, 110);
}
return available_coins;
});
SelectionResult expected_result(CAmount(0), SelectionAlgorithm::CG);
add_coin(1.8 * 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 = 100'000;
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
}
}

static util::Result<SelectionResult> SelectCoinsSRD(const CAmount& target,
Expand Down

0 comments on commit f9a0ddd

Please sign in to comment.