Skip to content

Commit 5248e2a

Browse files
committed
opt: Skip heavier UTXOs with same effective value
When two successive UTXOs differ in weight but match in effective value, we can skip the second if the first is not selected, because all input sets we can generate by swapping out a lighter UTXOs with a heavier UTXO of matching effective value would be strictly worse.
1 parent 9124c73 commit 5248e2a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/wallet/coinselection.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,12 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
479479
deselect_last();
480480
should_shift = false;
481481

482-
// After SHIFTing to an omission branch, the `next_utxo` might have the same value and same weight as the
483-
// UTXO we just omitted (i.e. it is a "clone"). If so, selecting `next_utxo` would produce an equivalent
484-
// selection as one we previously evaluated. In that case, increment `next_utxo` until we find a UTXO with a
485-
// differing amount or weight.
486-
while (utxo_pool[next_utxo - 1].GetSelectionAmount() == utxo_pool[next_utxo].GetSelectionAmount()
487-
&& utxo_pool[next_utxo - 1].m_weight == utxo_pool[next_utxo].m_weight) {
482+
// After SHIFTing to an omission branch, the `next_utxo` might have the same effective value as the UTXO we
483+
// just omitted. Since lower weight is our tiebreaker on UTXOs with equal effective value for sorting, if it
484+
// ties on the effective value, it _must_ have the same weight (i.e. be a "clone" of the prior UTXO) or a
485+
// higher weight. If so, selecting `next_utxo` would produce an equivalent or worse selection as one we
486+
// previously evaluated. In that case, increment `next_utxo` until we find a UTXO with a differing amount.
487+
while (utxo_pool[next_utxo - 1].GetSelectionAmount() == utxo_pool[next_utxo].GetSelectionAmount()) {
488488
if (next_utxo >= utxo_pool.size() - 1) {
489489
// Reached end of UTXO pool skipping clones: SHIFT instead
490490
should_shift = true;

0 commit comments

Comments
 (0)