diff options
author | Murch <murch@murch.one> | 2024-01-08 14:47:54 -0500 |
---|---|---|
committer | Murch <murch@murch.one> | 2024-02-09 10:58:17 +0100 |
commit | 9124c73742287b06dfe6e8a94be56ace25f07b2c (patch) | |
tree | fdb260edd550d2a485cc5c3418c9bbe96c5d4b91 /src/wallet/coinselection.cpp | |
parent | 451be19dc10381122f705bbb2127b083f1d598c6 (diff) | |
download | bitcoin-9124c73742287b06dfe6e8a94be56ace25f07b2c.tar.xz |
opt: Tiebreak UTXOs by weight for CoinGrinder
Diffstat (limited to 'src/wallet/coinselection.cpp')
-rw-r--r-- | src/wallet/coinselection.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index aca81353ef..9cad76574a 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<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool * set in the node. * * @param std::vector<OutputGroup>& 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<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool */ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& 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<CAmount> lookahead(utxo_pool.size()); |