From 89d09566431f57034d9a7df32547ceb13d79c62c Mon Sep 17 00:00:00 2001 From: Murch Date: Tue, 13 Jun 2023 15:56:18 -0400 Subject: [PATCH] opt: Tie-break UTXO sort by waste for BnB Since we are searching for the minimal waste, we sort UTXOs with equal effective value by ascending waste to be able to cut barren branches earlier. --- src/wallet/coinselection.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 7d15a9997afd3..570fac6900346 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -25,10 +25,14 @@ static util::Result ErrorMaxWeightExceeded() "Please try sending a smaller amount or manually consolidating your wallet's UTXOs")}; } -// Descending order comparator +// Sort by descending (effective) value prefer lower waste on tie struct { bool operator()(const OutputGroup& a, const OutputGroup& b) const { + if (a.GetSelectionAmount() == b.GetSelectionAmount()) { + // Lower waste is better when effective_values are tied + return (a.fee - a.long_term_fee) < (b.fee - b.long_term_fee); + } return a.GetSelectionAmount() > b.GetSelectionAmount(); } } descending;