Skip to content

Commit 0856c15

Browse files
committed
Merge #18262: bnb: exit selection when best_waste is 0
9b5950d bnb: exit selection when best_waste is 0 (Andrew Chow) Pull request description: If we find a solution which has no waste, just use that. This solution is what we would consider to be optimal, and other solutions we find would have to also have 0 waste, so they are equivalent to the first one with 0 waste. Thus we can optimize by just choosing the first one with 0 waste. Closes #18257 ACKs for top commit: instagibbs: utACK bitcoin/bitcoin@9b5950d meshcollider: utACK 9b5950d Tree-SHA512: 59565ff4a3d8281e7bc0ce87065a34c8d8bf8a95f628ba96b4fe89f1274979165aea6312e5f1f21b418c8c484aafc5166d22d9eff9d127a8192498625d58c557
2 parents 969ee85 + 9b5950d commit 0856c15

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/wallet/coinselection.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_v
106106
best_selection = curr_selection;
107107
best_selection.resize(utxo_pool.size());
108108
best_waste = curr_waste;
109+
if (best_waste == 0) {
110+
break;
111+
}
109112
}
110113
curr_waste -= (curr_value - actual_target); // Remove the excess value as we will be selecting different coins now
111114
backtrack = true;

src/wallet/test/coinselector_tests.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
176176
selection.clear();
177177

178178
// Select 5 Cent
179-
add_coin(3 * CENT, 3, actual_selection);
180-
add_coin(2 * CENT, 2, actual_selection);
179+
add_coin(4 * CENT, 4, actual_selection);
180+
add_coin(1 * CENT, 1, actual_selection);
181181
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
182182
BOOST_CHECK(equal_sets(selection, actual_selection));
183183
BOOST_CHECK_EQUAL(value_ret, 5 * CENT);
@@ -204,9 +204,8 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
204204

205205
// Select 10 Cent
206206
add_coin(5 * CENT, 5, utxo_pool);
207+
add_coin(5 * CENT, 5, actual_selection);
207208
add_coin(4 * CENT, 4, actual_selection);
208-
add_coin(3 * CENT, 3, actual_selection);
209-
add_coin(2 * CENT, 2, actual_selection);
210209
add_coin(1 * CENT, 1, actual_selection);
211210
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
212211
BOOST_CHECK(equal_sets(selection, actual_selection));

0 commit comments

Comments
 (0)