Skip to content

Commit 9124c73

Browse files
committed
opt: Tiebreak UTXOs by weight for CoinGrinder
1 parent 451be19 commit 9124c73

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/wallet/coinselection.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ struct {
3737
}
3838
} descending;
3939

40+
// Sort by descending (effective) value prefer lower weight on tie
41+
struct {
42+
bool operator()(const OutputGroup& a, const OutputGroup& b) const
43+
{
44+
if (a.GetSelectionAmount() == b.GetSelectionAmount()) {
45+
// Sort lower weight to front on tied effective_value
46+
return a.m_weight < b.m_weight;
47+
}
48+
return a.GetSelectionAmount() > b.GetSelectionAmount();
49+
}
50+
} descending_effval_weight;
51+
4052
/*
4153
* This is the Branch and Bound Coin Selection algorithm designed by Murch. It searches for an input
4254
* set that can pay for the spending target and does not exceed the spending target by more than the
@@ -303,7 +315,7 @@ util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool
303315
* set in the node.
304316
*
305317
* @param std::vector<OutputGroup>& utxo_pool The UTXOs that we are choosing from. These UTXOs will be sorted in
306-
* descending order by effective value, with lower waste preferred as a tie-breaker. (We can think of an output
318+
* descending order by effective value, with lower weight preferred as a tie-breaker. (We can think of an output
307319
* group with multiple as a heavier UTXO with the combined amount here.)
308320
* @param const CAmount& selection_target This is the minimum amount that we need for the transaction without considering change.
309321
* @param const CAmount& change_target The minimum budget for creating a change output, by which we increase the selection_target.
@@ -312,7 +324,7 @@ util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool
312324
*/
313325
util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, CAmount change_target, int max_weight)
314326
{
315-
std::sort(utxo_pool.begin(), utxo_pool.end(), descending);
327+
std::sort(utxo_pool.begin(), utxo_pool.end(), descending_effval_weight);
316328
// The sum of UTXO amounts after this UTXO index, e.g. lookahead[5] = Σ(UTXO[6+].amount)
317329
std::vector<CAmount> lookahead(utxo_pool.size());
318330

0 commit comments

Comments
 (0)