From 9124c73742287b06dfe6e8a94be56ace25f07b2c Mon Sep 17 00:00:00 2001 From: Murch Date: Mon, 8 Jan 2024 14:47:54 -0500 Subject: [PATCH] opt: Tiebreak UTXOs by weight for CoinGrinder --- src/wallet/coinselection.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index aca81353ef5..9cad76574a2 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -37,6 +37,18 @@ struct { } } descending; +// Sort by descending (effective) value prefer lower weight on tie +struct { + bool operator()(const OutputGroup& a, const OutputGroup& b) const + { + if (a.GetSelectionAmount() == b.GetSelectionAmount()) { + // Sort lower weight to front on tied effective_value + return a.m_weight < b.m_weight; + } + return a.GetSelectionAmount() > b.GetSelectionAmount(); + } +} descending_effval_weight; + /* * This is the Branch and Bound Coin Selection algorithm designed by Murch. It searches for an input * 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 SelectCoinsBnB(std::vector& utxo_pool * set in the node. * * @param std::vector& utxo_pool The UTXOs that we are choosing from. These UTXOs will be sorted in - * descending order by effective value, with lower waste preferred as a tie-breaker. (We can think of an output + * descending order by effective value, with lower weight preferred as a tie-breaker. (We can think of an output * group with multiple as a heavier UTXO with the combined amount here.) * @param const CAmount& selection_target This is the minimum amount that we need for the transaction without considering change. * @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 SelectCoinsBnB(std::vector& utxo_pool */ util::Result CoinGrinder(std::vector& 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_effval_weight); // The sum of UTXO amounts after this UTXO index, e.g. lookahead[5] = Σ(UTXO[6+].amount) std::vector lookahead(utxo_pool.size());