mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
opt: Track remaining effective_value in lookahead
Introduces a dedicated data structure to track the total effective_value available in the remaining UTXOs at each index of the UTXO pool. In contrast to the approach in BnB, this allows us to immediately jump to a lower index instead of visiting every UTXO to add back their eff_value to the lookahead.
This commit is contained in:
parent
5f84f3cc04
commit
407b1e3432
2 changed files with 15 additions and 8 deletions
|
@ -313,13 +313,17 @@ util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool
|
||||||
util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, CAmount change_target, int max_weight)
|
util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, CAmount change_target, int max_weight)
|
||||||
{
|
{
|
||||||
std::sort(utxo_pool.begin(), utxo_pool.end(), descending);
|
std::sort(utxo_pool.begin(), utxo_pool.end(), descending);
|
||||||
|
// The sum of UTXO amounts after this UTXO index, e.g. lookahead[5] = Σ(UTXO[6+].amount)
|
||||||
|
std::vector<CAmount> lookahead(utxo_pool.size());
|
||||||
|
|
||||||
// Check that there are sufficient funds
|
// Calculate lookahead values, and check that there are sufficient funds
|
||||||
CAmount total_available = 0;
|
CAmount total_available = 0;
|
||||||
for (const OutputGroup& utxo : utxo_pool) {
|
for (size_t i = 0; i < utxo_pool.size(); ++i) {
|
||||||
// Assert UTXOs with non-positive effective value have been filtered
|
size_t index = utxo_pool.size() - 1 - i; // Loop over every element in reverse order
|
||||||
Assume(utxo.GetSelectionAmount() > 0);
|
lookahead[index] = total_available;
|
||||||
total_available += utxo.GetSelectionAmount();
|
// UTXOs with non-positive effective value must have been filtered
|
||||||
|
Assume(utxo_pool[index].GetSelectionAmount() > 0);
|
||||||
|
total_available += utxo_pool[index].GetSelectionAmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
const CAmount total_target = selection_target + change_target;
|
const CAmount total_target = selection_target + change_target;
|
||||||
|
@ -409,7 +413,10 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
|
||||||
++curr_try;
|
++curr_try;
|
||||||
|
|
||||||
// EVALUATE current selection: check for solutions and see whether we can CUT or SHIFT before EXPLORING further
|
// EVALUATE current selection: check for solutions and see whether we can CUT or SHIFT before EXPLORING further
|
||||||
if (curr_weight > max_weight) {
|
if (curr_amount + lookahead[curr_selection.back()] < total_target) {
|
||||||
|
// Insufficient funds with lookahead: CUT
|
||||||
|
should_cut = true;
|
||||||
|
} else if (curr_weight > max_weight) {
|
||||||
// max_weight exceeded: SHIFT
|
// max_weight exceeded: SHIFT
|
||||||
max_tx_weight_exceeded = true;
|
max_tx_weight_exceeded = true;
|
||||||
should_shift = true;
|
should_shift = true;
|
||||||
|
|
|
@ -1231,7 +1231,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
add_coin(4 * COIN, 3, expected_result);
|
add_coin(4 * COIN, 3, expected_result);
|
||||||
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
||||||
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
||||||
size_t expected_attempts = 525;
|
size_t expected_attempts = 281;
|
||||||
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1271,7 +1271,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
||||||
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
||||||
// If this takes more attempts, the implementation has regressed
|
// If this takes more attempts, the implementation has regressed
|
||||||
size_t expected_attempts = 82'815;
|
size_t expected_attempts = 82'407;
|
||||||
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue