Skip to content

Commit

Permalink
opt: Skip over barren combinations of tiny UTXOs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
murchandamus committed Jan 12, 2024
1 parent 7bf1dce commit e1d5f43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& 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()] * ((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) {
Expand Down
12 changes: 6 additions & 6 deletions src/wallet/test/coinselector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +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
// 7) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead

FastRandomContext rand;
CoinSelectionParams dummy_params{ // Only used to provide the 'avoid_partial' flag.
Expand Down Expand Up @@ -1180,7 +1180,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, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
}

Expand Down Expand Up @@ -1227,7 +1227,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
});
BOOST_CHECK(res);
// If this takes more attempts, the implementation has regressed
size_t expected_attempts = 218;
size_t expected_attempts = 151;
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
}

Expand Down Expand Up @@ -1273,7 +1273,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)

{
// #################################################################################################################
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts
// 7) 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;
Expand All @@ -1290,11 +1290,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 = 100'000;
size_t expected_attempts = 7;
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
}
}
Expand Down

0 comments on commit e1d5f43

Please sign in to comment.