Skip to content

Commit 13161ec

Browse files
committed
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 SHIFT to the omission branch, and CUT if the last selected UTXO is not heavier than the minimum weight of the remainder.
1 parent b7672c7 commit 13161ec

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/wallet/coinselection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,13 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
454454
best_selection_weight = curr_weight;
455455
best_selection_amount = curr_amount;
456456
}
457+
} else if (!best_selection.empty() && curr_weight + int64_t{min_tail_weight[curr_tail]} * ((total_target - curr_amount + utxo_pool[curr_tail].GetSelectionAmount() - 1) / utxo_pool[curr_tail].GetSelectionAmount()) > best_selection_weight) {
458+
// Compare minimal tail weight and last selected amount with the amount missing to gauge whether a better weight is still possible.
459+
if (utxo_pool[curr_tail].m_weight <= min_tail_weight[curr_tail]) {
460+
should_cut = true;
461+
} else {
462+
should_shift = true;
463+
}
457464
}
458465

459466
if (curr_try >= TOTAL_TRIES) {

src/wallet/test/coinselector_tests.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
11111111
// 4) Test that two less valuable UTXOs with a combined lower weight are preferred over a more valuable heavier UTXO
11121112
// 5) Test finding a solution in a UTXO pool with mixed weights
11131113
// 6) Test that the lightest solution among many clones is found
1114-
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts
1114+
// 7) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead
11151115

11161116
FastRandomContext rand;
11171117
CoinSelectionParams dummy_params{ // Only used to provide the 'avoid_partial' flag.
@@ -1180,7 +1180,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
11801180
});
11811181
BOOST_CHECK(res);
11821182
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1183-
size_t expected_attempts = 184;
1183+
size_t expected_attempts = 37;
11841184
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
11851185
}
11861186

@@ -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 = 218;
1234+
size_t expected_attempts = 92;
12351235
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12361236
}
12371237

@@ -1270,14 +1270,13 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12701270
add_coin(1 * COIN, 0, expected_result);
12711271
BOOST_CHECK(EquivalentResult(expected_result, *res));
12721272
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1273-
// If this takes more attempts, the implementation has regressed
1274-
size_t expected_attempts = 42;
1273+
size_t expected_attempts = 38;
12751274
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12761275
}
12771276

12781277
{
12791278
// #################################################################################################################
1280-
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts
1279+
// 7) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead
12811280
// #################################################################################################################
12821281
CAmount target = 1.9L * COIN;
12831282
int max_weight = 40000; // WU
@@ -1293,11 +1292,11 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12931292
return available_coins;
12941293
});
12951294
SelectionResult expected_result(CAmount(0), SelectionAlgorithm::CG);
1296-
add_coin(1.8 * COIN, 1, expected_result);
1295+
add_coin(1 * COIN, 1, expected_result);
12971296
add_coin(1 * COIN, 2, expected_result);
12981297
BOOST_CHECK(EquivalentResult(expected_result, *res));
12991298
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1300-
size_t expected_attempts = 100'000;
1299+
size_t expected_attempts = 7;
13011300
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
13021301
}
13031302
}

0 commit comments

Comments
 (0)