• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15155511119

21 May 2025 06:52AM UTC coverage: 57.389% (-11.6%) from 68.996%
15155511119

Pull #9844

github

web-flow
Merge 8658c8597 into c52a6ddeb
Pull Request #9844: Refactor Payment PR 3

346 of 493 new or added lines in 4 files covered. (70.18%)

30172 existing lines in 456 files now uncovered.

95441 of 166305 relevant lines covered (57.39%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.28
/sweep/tx_input_set.go
1
package sweep
2

3
import (
4
        "fmt"
5
        "math"
6
        "sort"
7

8
        "github.com/btcsuite/btcd/btcutil"
9
        "github.com/btcsuite/btcd/txscript"
10
        "github.com/btcsuite/btcd/wire"
11
        "github.com/lightningnetwork/lnd/fn/v2"
12
        "github.com/lightningnetwork/lnd/input"
13
        "github.com/lightningnetwork/lnd/lnwallet"
14
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
15
)
16

17
var (
18
        // ErrNotEnoughInputs is returned when there are not enough wallet
19
        // inputs to construct a non-dust change output for an input set.
20
        ErrNotEnoughInputs = fmt.Errorf("not enough inputs")
21

22
        // ErrDeadlinesMismatch is returned when the deadlines of the input
23
        // sets do not match.
24
        ErrDeadlinesMismatch = fmt.Errorf("deadlines mismatch")
25

26
        // ErrDustOutput is returned when the output value is below the dust
27
        // limit.
28
        ErrDustOutput = fmt.Errorf("dust output")
29
)
30

31
// InputSet defines an interface that's responsible for filtering a set of
32
// inputs that can be swept economically.
33
type InputSet interface {
34
        // Inputs returns the set of inputs that should be used to create a tx.
35
        Inputs() []input.Input
36

37
        // AddWalletInputs adds wallet inputs to the set until a non-dust
38
        // change output can be made. Return an error if there are not enough
39
        // wallet inputs.
40
        AddWalletInputs(wallet Wallet) error
41

42
        // NeedWalletInput returns true if the input set needs more wallet
43
        // inputs.
44
        NeedWalletInput() bool
45

46
        // DeadlineHeight returns an absolute block height to express the
47
        // time-sensitivity of the input set. The outputs from a force close tx
48
        // have different time preferences:
49
        // - to_local: no time pressure as it can only be swept by us.
50
        // - first level outgoing HTLC: must be swept before its corresponding
51
        //   incoming HTLC's CLTV is reached.
52
        // - first level incoming HTLC: must be swept before its CLTV is
53
        //   reached.
54
        // - second level HTLCs: no time pressure.
55
        // - anchor: for CPFP-purpose anchor, it must be swept before any of
56
        //   the above CLTVs is reached. For non-CPFP purpose anchor, there's
57
        //   no time pressure.
58
        DeadlineHeight() int32
59

60
        // Budget givens the total amount that can be used as fees by this
61
        // input set.
62
        Budget() btcutil.Amount
63

64
        // StartingFeeRate returns the max starting fee rate found in the
65
        // inputs.
66
        StartingFeeRate() fn.Option[chainfee.SatPerKWeight]
67

68
        // Immediate returns a boolean to indicate whether the tx made from
69
        // this input set should be published immediately.
70
        //
71
        // TODO(yy): create a new method `Params` to combine the informational
72
        // methods DeadlineHeight, Budget, StartingFeeRate and Immediate.
73
        Immediate() bool
74
}
75

76
// createWalletTxInput converts a wallet utxo into an object that can be added
77
// to the other inputs to sweep.
78
func createWalletTxInput(utxo *lnwallet.Utxo) (input.Input, error) {
1✔
79
        signDesc := &input.SignDescriptor{
1✔
80
                Output: &wire.TxOut{
1✔
81
                        PkScript: utxo.PkScript,
1✔
82
                        Value:    int64(utxo.Value),
1✔
83
                },
1✔
84
                HashType: txscript.SigHashAll,
1✔
85
        }
1✔
86

1✔
87
        var witnessType input.WitnessType
1✔
88
        switch utxo.AddressType {
1✔
89
        case lnwallet.WitnessPubKey:
1✔
90
                witnessType = input.WitnessKeyHash
1✔
91
        case lnwallet.NestedWitnessPubKey:
×
92
                witnessType = input.NestedWitnessKeyHash
×
93
        case lnwallet.TaprootPubkey:
1✔
94
                witnessType = input.TaprootPubKeySpend
1✔
95
                signDesc.HashType = txscript.SigHashDefault
1✔
UNCOV
96
        default:
×
UNCOV
97
                return nil, fmt.Errorf("unknown address type %v",
×
UNCOV
98
                        utxo.AddressType)
×
99
        }
100

101
        // A height hint doesn't need to be set, because we don't monitor these
102
        // inputs for spend.
103
        heightHint := uint32(0)
1✔
104

1✔
105
        return input.NewBaseInput(
1✔
106
                &utxo.OutPoint, witnessType, signDesc, heightHint,
1✔
107
        ), nil
1✔
108
}
109

110
// BudgetInputSet implements the interface `InputSet`. It takes a list of
111
// pending inputs which share the same deadline height and groups them into a
112
// set conditionally based on their economical values.
113
type BudgetInputSet struct {
114
        // inputs is the set of inputs that have been added to the set after
115
        // considering their economical contribution.
116
        inputs []*SweeperInput
117

118
        // deadlineHeight is the height which the inputs in this set must be
119
        // confirmed by.
120
        deadlineHeight int32
121

122
        // extraBudget is a value that should be allocated to sweep the given
123
        // set of inputs. This can be used to add extra funds to the sweep
124
        // transaction, for example to cover fees for additional outputs of
125
        // custom channels.
126
        extraBudget btcutil.Amount
127
}
128

129
// Compile-time constraint to ensure budgetInputSet implements InputSet.
130
var _ InputSet = (*BudgetInputSet)(nil)
131

132
// errEmptyInputs is returned when the input slice is empty.
133
var errEmptyInputs = fmt.Errorf("inputs slice is empty")
134

135
// validateInputs is used when creating new BudgetInputSet to ensure there are
136
// no duplicate inputs and they all share the same deadline heights, if set.
137
func validateInputs(inputs []SweeperInput, deadlineHeight int32) error {
1✔
138
        // Sanity check the input slice to ensure it's non-empty.
1✔
139
        if len(inputs) == 0 {
1✔
UNCOV
140
                return errEmptyInputs
×
UNCOV
141
        }
×
142

143
        // inputDeadline tracks the input's deadline height. It will be updated
144
        // if the input has a different deadline than the specified
145
        // deadlineHeight.
146
        inputDeadline := deadlineHeight
1✔
147

1✔
148
        // dedupInputs is a set used to track unique outpoints of the inputs.
1✔
149
        dedupInputs := fn.NewSet(
1✔
150
                // Iterate all the inputs and map the function.
1✔
151
                fn.Map(inputs, func(inp SweeperInput) wire.OutPoint {
2✔
152
                        // If the input has a deadline height, we'll check if
1✔
153
                        // it's the same as the specified.
1✔
154
                        inp.params.DeadlineHeight.WhenSome(func(h int32) {
2✔
155
                                // Exit early if the deadlines matched.
1✔
156
                                if h == deadlineHeight {
2✔
157
                                        return
1✔
158
                                }
1✔
159

160
                                // Update the deadline height if it's
161
                                // different.
UNCOV
162
                                inputDeadline = h
×
163
                        })
164

165
                        return inp.OutPoint()
1✔
166
                })...,
167
        )
168

169
        // Make sure the inputs share the same deadline height when there is
170
        // one.
171
        if inputDeadline != deadlineHeight {
1✔
UNCOV
172
                return fmt.Errorf("input deadline height not matched: want "+
×
UNCOV
173
                        "%d, got %d", deadlineHeight, inputDeadline)
×
UNCOV
174
        }
×
175

176
        // Provide a defensive check to ensure that we don't have any duplicate
177
        // inputs within the set.
178
        if len(dedupInputs) != len(inputs) {
1✔
UNCOV
179
                return fmt.Errorf("duplicate inputs")
×
UNCOV
180
        }
×
181

182
        return nil
1✔
183
}
184

185
// NewBudgetInputSet creates a new BudgetInputSet.
186
func NewBudgetInputSet(inputs []SweeperInput, deadlineHeight int32,
187
        auxSweeper fn.Option[AuxSweeper]) (*BudgetInputSet, error) {
1✔
188

1✔
189
        // Validate the supplied inputs.
1✔
190
        if err := validateInputs(inputs, deadlineHeight); err != nil {
1✔
UNCOV
191
                return nil, err
×
UNCOV
192
        }
×
193

194
        bi := &BudgetInputSet{
1✔
195
                deadlineHeight: deadlineHeight,
1✔
196
                inputs:         make([]*SweeperInput, 0, len(inputs)),
1✔
197
        }
1✔
198

1✔
199
        for _, input := range inputs {
2✔
200
                bi.addInput(input)
1✔
201
        }
1✔
202

203
        log.Tracef("Created %v", bi.String())
1✔
204

1✔
205
        // Attach an optional budget. This will be a no-op if the auxSweeper
1✔
206
        // is not set.
1✔
207
        if err := bi.attachExtraBudget(auxSweeper); err != nil {
1✔
208
                return nil, err
×
209
        }
×
210

211
        return bi, nil
1✔
212
}
213

214
// attachExtraBudget attaches an extra budget to the input set, if the passed
215
// aux sweeper is set.
216
func (b *BudgetInputSet) attachExtraBudget(s fn.Option[AuxSweeper]) error {
1✔
217
        extraBudget, err := fn.MapOptionZ(
1✔
218
                s, func(aux AuxSweeper) fn.Result[btcutil.Amount] {
1✔
UNCOV
219
                        return aux.ExtraBudgetForInputs(b.Inputs())
×
UNCOV
220
                },
×
221
        ).Unpack()
222
        if err != nil {
1✔
223
                return err
×
224
        }
×
225

226
        b.extraBudget = extraBudget
1✔
227

1✔
228
        return nil
1✔
229
}
230

231
// String returns a human-readable description of the input set.
232
func (b *BudgetInputSet) String() string {
1✔
233
        inputsDesc := ""
1✔
234
        for _, input := range b.inputs {
2✔
235
                inputsDesc += fmt.Sprintf("\n%v", input)
1✔
236
        }
1✔
237

238
        return fmt.Sprintf("BudgetInputSet(budget=%v, deadline=%v, "+
1✔
239
                "inputs=[%v])", b.Budget(), b.DeadlineHeight(), inputsDesc)
1✔
240
}
241

242
// addInput adds an input to the input set.
243
func (b *BudgetInputSet) addInput(input SweeperInput) {
1✔
244
        b.inputs = append(b.inputs, &input)
1✔
245
}
1✔
246

247
// addWalletInput takes a wallet UTXO and adds it as an input to be used as
248
// budget for the input set.
249
func (b *BudgetInputSet) addWalletInput(utxo *lnwallet.Utxo) error {
1✔
250
        input, err := createWalletTxInput(utxo)
1✔
251
        if err != nil {
1✔
UNCOV
252
                return err
×
UNCOV
253
        }
×
254

255
        pi := SweeperInput{
1✔
256
                Input: input,
1✔
257
                params: Params{
1✔
258
                        DeadlineHeight: fn.Some(b.deadlineHeight),
1✔
259
                },
1✔
260
        }
1✔
261
        b.addInput(pi)
1✔
262

1✔
263
        log.Debugf("Added wallet input to input set: op=%v, amt=%v",
1✔
264
                pi.OutPoint(), utxo.Value)
1✔
265

1✔
266
        return nil
1✔
267
}
268

269
// NeedWalletInput returns true if the input set needs more wallet inputs.
270
//
271
// A set may need wallet inputs when it has a required output or its total
272
// value cannot cover its total budget.
273
func (b *BudgetInputSet) NeedWalletInput() bool {
1✔
274
        var (
1✔
275
                // budgetNeeded is the amount that needs to be covered from
1✔
276
                // other inputs. We start at the value of the extra budget,
1✔
277
                // which might be needed for custom channels that add extra
1✔
278
                // outputs.
1✔
279
                budgetNeeded = b.extraBudget
1✔
280

1✔
281
                // budgetBorrowable is the amount that can be borrowed from
1✔
282
                // other inputs.
1✔
283
                budgetBorrowable btcutil.Amount
1✔
284
        )
1✔
285

1✔
286
        for _, inp := range b.inputs {
2✔
287
                // If this input has a required output, we can assume it's a
1✔
288
                // second-level htlc txns input. Although this input must have
1✔
289
                // a value that can cover its budget, it cannot be used to pay
1✔
290
                // fees. Instead, we need to borrow budget from other inputs to
1✔
291
                // make the sweep happen. Once swept, the input value will be
1✔
292
                // credited to the wallet.
1✔
293
                if inp.RequiredTxOut() != nil {
2✔
294
                        budgetNeeded += inp.params.Budget
1✔
295
                        continue
1✔
296
                }
297

298
                // Get the amount left after covering the input's own budget.
299
                // This amount can then be lent to the above input. For a wallet
300
                // input, its `Budget` is set to zero, which means the whole
301
                // input can be borrowed to cover the budget.
302
                budget := inp.params.Budget
1✔
303
                output := btcutil.Amount(inp.SignDesc().Output.Value)
1✔
304
                budgetBorrowable += output - budget
1✔
305

1✔
306
                // If the input's budget is not even covered by itself, we need
1✔
307
                // to borrow outputs from other inputs.
1✔
308
                if budgetBorrowable < 0 {
2✔
309
                        log.Tracef("Input %v specified a budget that exceeds "+
1✔
310
                                "its output value: %v > %v", inp, budget,
1✔
311
                                output)
1✔
312
                }
1✔
313
        }
314

315
        log.Debugf("NeedWalletInput: budgetNeeded=%v, budgetBorrowable=%v",
1✔
316
                budgetNeeded, budgetBorrowable)
1✔
317

1✔
318
        // If we don't have enough extra budget to borrow, we need wallet
1✔
319
        // inputs.
1✔
320
        return budgetBorrowable < budgetNeeded
1✔
321
}
322

323
// hasNormalInput return a bool to indicate whether there exists an input that
324
// doesn't require a TxOut. When an input has no required outputs, it's either a
325
// wallet input, or an input we want to sweep.
326
func (b *BudgetInputSet) hasNormalInput() bool {
1✔
327
        for _, inp := range b.inputs {
2✔
328
                if inp.RequiredTxOut() != nil {
2✔
329
                        continue
1✔
330
                }
331

332
                return true
1✔
333
        }
334

335
        return false
1✔
336
}
337

338
// AddWalletInputs adds wallet inputs to the set until the specified budget is
339
// met. When sweeping inputs with required outputs, although there's budget
340
// specified, it cannot be directly spent from these required outputs. Instead,
341
// we need to borrow budget from other inputs to make the sweep happen.
342
// There are two sources to borrow from: 1) other inputs, 2) wallet utxos. If
343
// we are calling this method, it means other inputs cannot cover the specified
344
// budget, so we need to borrow from wallet utxos.
345
//
346
// Return an error if there are not enough wallet inputs, and the budget set is
347
// set to its initial state by removing any wallet inputs added.
348
//
349
// NOTE: must be called with the wallet lock held via `WithCoinSelectLock`.
350
func (b *BudgetInputSet) AddWalletInputs(wallet Wallet) error {
1✔
351
        // Retrieve wallet utxos. Only consider confirmed utxos to prevent
1✔
352
        // problems around RBF rules for unconfirmed inputs. This currently
1✔
353
        // ignores the configured coin selection strategy.
1✔
354
        utxos, err := wallet.ListUnspentWitnessFromDefaultAccount(
1✔
355
                1, math.MaxInt32,
1✔
356
        )
1✔
357
        if err != nil {
1✔
UNCOV
358
                return fmt.Errorf("list unspent witness: %w", err)
×
UNCOV
359
        }
×
360

361
        // Sort the UTXOs by putting smaller values at the start of the slice
362
        // to avoid locking large UTXO for sweeping.
363
        //
364
        // TODO(yy): add more choices to CoinSelectionStrategy and use the
365
        // configured value here.
366
        sort.Slice(utxos, func(i, j int) bool {
2✔
367
                return utxos[i].Value < utxos[j].Value
1✔
368
        })
1✔
369

370
        // Add wallet inputs to the set until the specified budget is covered.
371
        for _, utxo := range utxos {
2✔
372
                err := b.addWalletInput(utxo)
1✔
373
                if err != nil {
1✔
UNCOV
374
                        return err
×
UNCOV
375
                }
×
376

377
                // Return if we've reached the minimum output amount.
378
                if !b.NeedWalletInput() {
2✔
379
                        return nil
1✔
380
                }
1✔
381
        }
382

383
        // Exit if there are no inputs can contribute to the fees.
384
        if !b.hasNormalInput() {
2✔
385
                return ErrNotEnoughInputs
1✔
386
        }
1✔
387

388
        // If there's at least one input that can contribute to fees, we allow
389
        // the sweep to continue, even though the full budget can't be met.
390
        // Maybe later more wallet inputs will become available and we can add
391
        // them if needed.
392
        budget := b.Budget()
1✔
393
        total, spendable := b.inputAmts()
1✔
394
        log.Warnf("Not enough wallet UTXOs: need budget=%v, has spendable=%v, "+
1✔
395
                "total=%v, missing at least %v, sweeping anyway...", budget,
1✔
396
                spendable, total, budget-spendable)
1✔
397

1✔
398
        return nil
1✔
399
}
400

401
// Budget returns the total budget of the set.
402
//
403
// NOTE: part of the InputSet interface.
404
func (b *BudgetInputSet) Budget() btcutil.Amount {
1✔
405
        budget := btcutil.Amount(0)
1✔
406
        for _, input := range b.inputs {
2✔
407
                budget += input.params.Budget
1✔
408
        }
1✔
409

410
        // We'll also tack on the extra budget which will eventually be
411
        // accounted for by the wallet txns when we're broadcasting.
412
        return budget + b.extraBudget
1✔
413
}
414

415
// DeadlineHeight returns the deadline height of the set.
416
//
417
// NOTE: part of the InputSet interface.
418
func (b *BudgetInputSet) DeadlineHeight() int32 {
1✔
419
        return b.deadlineHeight
1✔
420
}
1✔
421

422
// Inputs returns the inputs that should be used to create a tx.
423
//
424
// NOTE: part of the InputSet interface.
425
func (b *BudgetInputSet) Inputs() []input.Input {
1✔
426
        inputs := make([]input.Input, 0, len(b.inputs))
1✔
427
        for _, inp := range b.inputs {
2✔
428
                inputs = append(inputs, inp.Input)
1✔
429
        }
1✔
430

431
        return inputs
1✔
432
}
433

434
// inputAmts returns two values for the set - the total input amount, and the
435
// spendable amount. Only the spendable amount can be used to pay the fees.
436
func (b *BudgetInputSet) inputAmts() (btcutil.Amount, btcutil.Amount) {
1✔
437
        var (
1✔
438
                totalAmt     btcutil.Amount
1✔
439
                spendableAmt btcutil.Amount
1✔
440
        )
1✔
441

1✔
442
        for _, inp := range b.inputs {
2✔
443
                output := btcutil.Amount(inp.SignDesc().Output.Value)
1✔
444
                totalAmt += output
1✔
445

1✔
446
                if inp.RequiredTxOut() != nil {
1✔
UNCOV
447
                        continue
×
448
                }
449

450
                spendableAmt += output
1✔
451
        }
452

453
        return totalAmt, spendableAmt
1✔
454
}
455

456
// StartingFeeRate returns the max starting fee rate found in the inputs.
457
//
458
// NOTE: part of the InputSet interface.
459
func (b *BudgetInputSet) StartingFeeRate() fn.Option[chainfee.SatPerKWeight] {
1✔
460
        maxFeeRate := chainfee.SatPerKWeight(0)
1✔
461
        startingFeeRate := fn.None[chainfee.SatPerKWeight]()
1✔
462

1✔
463
        for _, inp := range b.inputs {
2✔
464
                feerate := inp.params.StartingFeeRate.UnwrapOr(0)
1✔
465
                if feerate > maxFeeRate {
2✔
466
                        maxFeeRate = feerate
1✔
467
                        startingFeeRate = fn.Some(maxFeeRate)
1✔
468
                }
1✔
469
        }
470

471
        return startingFeeRate
1✔
472
}
473

474
// Immediate returns whether the inputs should be swept immediately.
475
//
476
// NOTE: part of the InputSet interface.
477
func (b *BudgetInputSet) Immediate() bool {
1✔
478
        for _, inp := range b.inputs {
2✔
479
                // As long as one of the inputs is immediate, the whole set is
1✔
480
                // immediate.
1✔
481
                if inp.params.Immediate {
2✔
482
                        return true
1✔
483
                }
1✔
484
        }
485

486
        return false
1✔
487
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc