From c2148ad757543b94fe5098f393f172dbba8fa71b Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 7 Sep 2023 00:52:57 +0800 Subject: [PATCH] sweep: prioritize smaller inputs when adding wallet UTXOs This commit sorts wallet UTXOs by their values when using them for sweeping inputs. This way we'd avoid locking large UTXOs when sweeping inputs and also provide an opportunity to aggregate wallet UTXOs. --- sweep/tx_input_set.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sweep/tx_input_set.go b/sweep/tx_input_set.go index 29c61bd81..bcaa42113 100644 --- a/sweep/tx_input_set.go +++ b/sweep/tx_input_set.go @@ -3,6 +3,7 @@ package sweep import ( "fmt" "math" + "sort" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/txscript" @@ -375,6 +376,15 @@ func (t *txInputSet) tryAddWalletInputsIfNeeded() error { return err } + // Sort the UTXOs by putting smaller values at the start of the slice + // to avoid locking large UTXO for sweeping. + // + // TODO(yy): add more choices to CoinSelectionStrategy and use the + // configured value here. + sort.Slice(utxos, func(i, j int) bool { + return utxos[i].Value < utxos[j].Value + }) + for _, utxo := range utxos { input, err := createWalletTxInput(utxo) if err != nil {