Skip to content

Commit

Permalink
opt: Tiebreak UTXOs by weight for CoinGrinder
Browse files Browse the repository at this point in the history
  • Loading branch information
murchandamus committed Dec 8, 2023
1 parent fc05966 commit 7fbeba5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static util::Result<SelectionResult> 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
{
Expand All @@ -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
Expand Down Expand Up @@ -236,7 +248,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
bool max_tx_weight_exceeded = false;

// Sort the utxo_pool
std::sort(utxo_pool.begin(), utxo_pool.end(), descending);
std::sort(utxo_pool.begin(), utxo_pool.end(), descending_effval_weight);
std::vector<CAmount> lookahead(utxo_pool.size());

// Calculate lookahead and check that there are sufficient funds
Expand Down

0 comments on commit 7fbeba5

Please sign in to comment.