lightningnetwork/lnd

[bug]: sweep: AddWalletInputs modifies BudgetInputSet when error occurs

morehouse opened this issue · 0 comments

BudgetInputSet.AddWalletInputs currently restores the original inputs if there aren't enough wallet inputs to meet the budget.

lnd/sweep/tx_input_set.go

Lines 297 to 327 in 9d358bc

// Make a copy of the current inputs. If the wallet doesn't have enough
// utxos to cover the budget, we will revert the current set to its
// original state by removing the added wallet inputs.
originalInputs := b.copyInputs()
// Add wallet inputs to the set until the specified budget is covered.
for _, utxo := range utxos {
input, err := createWalletTxInput(utxo)
if err != nil {
return err
}
pi := SweeperInput{
Input: input,
params: Params{
DeadlineHeight: fn.Some(b.deadlineHeight),
},
}
b.addInput(pi)
// Return if we've reached the minimum output amount.
if !b.NeedWalletInput() {
return nil
}
}
// The wallet doesn't have enough utxos to cover the budget. Revert the
// input set to its original state.
b.inputs = originalInputs
return ErrNotEnoughInputs

But if createWalletTxInput returns an error, the original inputs are not restored.

Solution

Today createWalletTxInput shouldn't ever return an error, though it may in the future if a new witness type is added. We should unify all error paths in AddWalletInputs to ensure inputs are restored.