Skip to content

Commit 407b1e3

Browse files
committed
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.
1 parent 5f84f3c commit 407b1e3

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/wallet/coinselection.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,17 @@ util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool
313313
util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, CAmount change_target, int max_weight)
314314
{
315315
std::sort(utxo_pool.begin(), utxo_pool.end(), descending);
316+
// The sum of UTXO amounts after this UTXO index, e.g. lookahead[5] = Σ(UTXO[6+].amount)
317+
std::vector<CAmount> lookahead(utxo_pool.size());
316318

317-
// Check that there are sufficient funds
319+
// Calculate lookahead values, and check that there are sufficient funds
318320
CAmount total_available = 0;
319-
for (const OutputGroup& utxo : utxo_pool) {
320-
// Assert UTXOs with non-positive effective value have been filtered
321-
Assume(utxo.GetSelectionAmount() > 0);
322-
total_available += utxo.GetSelectionAmount();
321+
for (size_t i = 0; i < utxo_pool.size(); ++i) {
322+
size_t index = utxo_pool.size() - 1 - i; // Loop over every element in reverse order
323+
lookahead[index] = total_available;
324+
// UTXOs with non-positive effective value must have been filtered
325+
Assume(utxo_pool[index].GetSelectionAmount() > 0);
326+
total_available += utxo_pool[index].GetSelectionAmount();
323327
}
324328

325329
const CAmount total_target = selection_target + change_target;
@@ -409,7 +413,10 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
409413
++curr_try;
410414

411415
// EVALUATE current selection: check for solutions and see whether we can CUT or SHIFT before EXPLORING further
412-
if (curr_weight > max_weight) {
416+
if (curr_amount + lookahead[curr_selection.back()] < total_target) {
417+
// Insufficient funds with lookahead: CUT
418+
should_cut = true;
419+
} else if (curr_weight > max_weight) {
413420
// max_weight exceeded: SHIFT
414421
max_tx_weight_exceeded = true;
415422
should_shift = true;

src/wallet/test/coinselector_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12311231
add_coin(4 * COIN, 3, expected_result);
12321232
BOOST_CHECK(EquivalentResult(expected_result, *res));
12331233
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1234-
size_t expected_attempts = 525;
1234+
size_t expected_attempts = 281;
12351235
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12361236
}
12371237

@@ -1271,7 +1271,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12711271
BOOST_CHECK(EquivalentResult(expected_result, *res));
12721272
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
12731273
// If this takes more attempts, the implementation has regressed
1274-
size_t expected_attempts = 82'815;
1274+
size_t expected_attempts = 82'407;
12751275
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12761276
}
12771277

0 commit comments

Comments
 (0)