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

lightningnetwork / lnd / 11166428937

03 Oct 2024 05:07PM UTC coverage: 58.738% (-0.08%) from 58.817%
11166428937

push

github

web-flow
Merge pull request #8960 from lightningnetwork/0-19-staging-rebased

[custom channels 5/5]: merge custom channel staging branch into master

657 of 875 new or added lines in 29 files covered. (75.09%)

260 existing lines in 20 files now uncovered.

130540 of 222243 relevant lines covered (58.74%)

28084.43 hits per line

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

97.1
/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"
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

69
// createWalletTxInput converts a wallet utxo into an object that can be added
70
// to the other inputs to sweep.
71
func createWalletTxInput(utxo *lnwallet.Utxo) (input.Input, error) {
7✔
72
        signDesc := &input.SignDescriptor{
7✔
73
                Output: &wire.TxOut{
7✔
74
                        PkScript: utxo.PkScript,
7✔
75
                        Value:    int64(utxo.Value),
7✔
76
                },
7✔
77
                HashType: txscript.SigHashAll,
7✔
78
        }
7✔
79

7✔
80
        var witnessType input.WitnessType
7✔
81
        switch utxo.AddressType {
7✔
82
        case lnwallet.WitnessPubKey:
6✔
83
                witnessType = input.WitnessKeyHash
6✔
84
        case lnwallet.NestedWitnessPubKey:
×
85
                witnessType = input.NestedWitnessKeyHash
×
86
        case lnwallet.TaprootPubkey:
3✔
87
                witnessType = input.TaprootPubKeySpend
3✔
88
                signDesc.HashType = txscript.SigHashDefault
3✔
89
        default:
1✔
90
                return nil, fmt.Errorf("unknown address type %v",
1✔
91
                        utxo.AddressType)
1✔
92
        }
93

94
        // A height hint doesn't need to be set, because we don't monitor these
95
        // inputs for spend.
96
        heightHint := uint32(0)
6✔
97

6✔
98
        return input.NewBaseInput(
6✔
99
                &utxo.OutPoint, witnessType, signDesc, heightHint,
6✔
100
        ), nil
6✔
101
}
102

103
// BudgetInputSet implements the interface `InputSet`. It takes a list of
104
// pending inputs which share the same deadline height and groups them into a
105
// set conditionally based on their economical values.
106
type BudgetInputSet struct {
107
        // inputs is the set of inputs that have been added to the set after
108
        // considering their economical contribution.
109
        inputs []*SweeperInput
110

111
        // deadlineHeight is the height which the inputs in this set must be
112
        // confirmed by.
113
        deadlineHeight int32
114

115
        // extraBudget is a value that should be allocated to sweep the given
116
        // set of inputs. This can be used to add extra funds to the sweep
117
        // transaction, for example to cover fees for additional outputs of
118
        // custom channels.
119
        extraBudget btcutil.Amount
120
}
121

122
// Compile-time constraint to ensure budgetInputSet implements InputSet.
123
var _ InputSet = (*BudgetInputSet)(nil)
124

125
// errEmptyInputs is returned when the input slice is empty.
126
var errEmptyInputs = fmt.Errorf("inputs slice is empty")
127

128
// validateInputs is used when creating new BudgetInputSet to ensure there are
129
// no duplicate inputs and they all share the same deadline heights, if set.
130
func validateInputs(inputs []SweeperInput, deadlineHeight int32) error {
27✔
131
        // Sanity check the input slice to ensure it's non-empty.
27✔
132
        if len(inputs) == 0 {
29✔
133
                return errEmptyInputs
2✔
134
        }
2✔
135

136
        // inputDeadline tracks the input's deadline height. It will be updated
137
        // if the input has a different deadline than the specified
138
        // deadlineHeight.
139
        inputDeadline := deadlineHeight
25✔
140

25✔
141
        // dedupInputs is a set used to track unique outpoints of the inputs.
25✔
142
        dedupInputs := fn.NewSet(
25✔
143
                // Iterate all the inputs and map the function.
25✔
144
                fn.Map(func(inp SweeperInput) wire.OutPoint {
62✔
145
                        // If the input has a deadline height, we'll check if
37✔
146
                        // it's the same as the specified.
37✔
147
                        inp.params.DeadlineHeight.WhenSome(func(h int32) {
56✔
148
                                // Exit early if the deadlines matched.
19✔
149
                                if h == deadlineHeight {
34✔
150
                                        return
15✔
151
                                }
15✔
152

153
                                // Update the deadline height if it's
154
                                // different.
155
                                inputDeadline = h
4✔
156
                        })
157

158
                        return inp.OutPoint()
37✔
159
                }, inputs)...,
160
        )
161

162
        // Make sure the inputs share the same deadline height when there is
163
        // one.
164
        if inputDeadline != deadlineHeight {
28✔
165
                return fmt.Errorf("input deadline height not matched: want "+
3✔
166
                        "%d, got %d", deadlineHeight, inputDeadline)
3✔
167
        }
3✔
168

169
        // Provide a defensive check to ensure that we don't have any duplicate
170
        // inputs within the set.
171
        if len(dedupInputs) != len(inputs) {
23✔
172
                return fmt.Errorf("duplicate inputs")
1✔
173
        }
1✔
174

175
        return nil
21✔
176
}
177

178
// NewBudgetInputSet creates a new BudgetInputSet.
179
func NewBudgetInputSet(inputs []SweeperInput, deadlineHeight int32,
180
        auxSweeper fn.Option[AuxSweeper]) (*BudgetInputSet, error) {
27✔
181

27✔
182
        // Validate the supplied inputs.
27✔
183
        if err := validateInputs(inputs, deadlineHeight); err != nil {
33✔
184
                return nil, err
6✔
185
        }
6✔
186

187
        bi := &BudgetInputSet{
21✔
188
                deadlineHeight: deadlineHeight,
21✔
189
                inputs:         make([]*SweeperInput, 0, len(inputs)),
21✔
190
        }
21✔
191

21✔
192
        for _, input := range inputs {
50✔
193
                bi.addInput(input)
29✔
194
        }
29✔
195

196
        log.Tracef("Created %v", bi.String())
21✔
197

21✔
198
        // Attach an optional budget. This will be a no-op if the auxSweeper
21✔
199
        // is not set.
21✔
200
        if err := bi.attachExtraBudget(auxSweeper); err != nil {
21✔
NEW
201
                return nil, err
×
NEW
202
        }
×
203

204
        return bi, nil
21✔
205
}
206

207
// attachExtraBudget attaches an extra budget to the input set, if the passed
208
// aux sweeper is set.
209
func (b *BudgetInputSet) attachExtraBudget(s fn.Option[AuxSweeper]) error {
21✔
210
        extraBudget, err := fn.MapOptionZ(
21✔
211
                s, func(aux AuxSweeper) fn.Result[btcutil.Amount] {
27✔
212
                        return aux.ExtraBudgetForInputs(b.Inputs())
6✔
213
                },
6✔
214
        ).Unpack()
215
        if err != nil {
21✔
NEW
216
                return err
×
NEW
217
        }
×
218

219
        b.extraBudget = extraBudget
21✔
220

21✔
221
        return nil
21✔
222
}
223

224
// String returns a human-readable description of the input set.
225
func (b *BudgetInputSet) String() string {
21✔
226
        inputsDesc := ""
21✔
227
        for _, input := range b.inputs {
50✔
228
                inputsDesc += fmt.Sprintf("\n%v", input)
29✔
229
        }
29✔
230

231
        return fmt.Sprintf("BudgetInputSet(budget=%v, deadline=%v, "+
21✔
232
                "inputs=[%v])", b.Budget(), b.DeadlineHeight(), inputsDesc)
21✔
233
}
234

235
// addInput adds an input to the input set.
236
func (b *BudgetInputSet) addInput(input SweeperInput) {
33✔
237
        b.inputs = append(b.inputs, &input)
33✔
238
}
33✔
239

240
// NeedWalletInput returns true if the input set needs more wallet inputs.
241
//
242
// A set may need wallet inputs when it has a required output or its total
243
// value cannot cover its total budget.
244
func (b *BudgetInputSet) NeedWalletInput() bool {
12✔
245
        var (
12✔
246
                // budgetNeeded is the amount that needs to be covered from
12✔
247
                // other inputs. We start at the value of the extra budget,
12✔
248
                // which might be needed for custom channels that add extra
12✔
249
                // outputs.
12✔
250
                budgetNeeded = b.extraBudget
12✔
251

12✔
252
                // budgetBorrowable is the amount that can be borrowed from
12✔
253
                // other inputs.
12✔
254
                budgetBorrowable btcutil.Amount
12✔
255
        )
12✔
256

12✔
257
        for _, inp := range b.inputs {
30✔
258
                // If this input has a required output, we can assume it's a
18✔
259
                // second-level htlc txns input. Although this input must have
18✔
260
                // a value that can cover its budget, it cannot be used to pay
18✔
261
                // fees. Instead, we need to borrow budget from other inputs to
18✔
262
                // make the sweep happen. Once swept, the input value will be
18✔
263
                // credited to the wallet.
18✔
264
                if inp.RequiredTxOut() != nil {
27✔
265
                        budgetNeeded += inp.params.Budget
9✔
266
                        continue
9✔
267
                }
268

269
                // Get the amount left after covering the input's own budget.
270
                // This amount can then be lent to the above input.
271
                budget := inp.params.Budget
12✔
272
                output := btcutil.Amount(inp.SignDesc().Output.Value)
12✔
273
                budgetBorrowable += output - budget
12✔
274

12✔
275
                // If the input's budget is not even covered by itself, we need
12✔
276
                // to borrow outputs from other inputs.
12✔
277
                if budgetBorrowable < 0 {
16✔
278
                        log.Tracef("Input %v specified a budget that exceeds "+
4✔
279
                                "its output value: %v > %v", inp, budget,
4✔
280
                                output)
4✔
281
                }
4✔
282
        }
283

284
        log.Debugf("NeedWalletInput: budgetNeeded=%v, budgetBorrowable=%v",
12✔
285
                budgetNeeded, budgetBorrowable)
12✔
286

12✔
287
        // If we don't have enough extra budget to borrow, we need wallet
12✔
288
        // inputs.
12✔
289
        return budgetBorrowable < budgetNeeded
12✔
290
}
291

292
// copyInputs returns a copy of the slice of the inputs in the set.
293
func (b *BudgetInputSet) copyInputs() []*SweeperInput {
7✔
294
        inputs := make([]*SweeperInput, len(b.inputs))
7✔
295
        copy(inputs, b.inputs)
7✔
296
        return inputs
7✔
297
}
7✔
298

299
// AddWalletInputs adds wallet inputs to the set until the specified budget is
300
// met. When sweeping inputs with required outputs, although there's budget
301
// specified, it cannot be directly spent from these required outputs. Instead,
302
// we need to borrow budget from other inputs to make the sweep happen.
303
// There are two sources to borrow from: 1) other inputs, 2) wallet utxos. If
304
// we are calling this method, it means other inputs cannot cover the specified
305
// budget, so we need to borrow from wallet utxos.
306
//
307
// Return an error if there are not enough wallet inputs, and the budget set is
308
// set to its initial state by removing any wallet inputs added.
309
//
310
// NOTE: must be called with the wallet lock held via `WithCoinSelectLock`.
311
func (b *BudgetInputSet) AddWalletInputs(wallet Wallet) error {
8✔
312
        // Retrieve wallet utxos. Only consider confirmed utxos to prevent
8✔
313
        // problems around RBF rules for unconfirmed inputs. This currently
8✔
314
        // ignores the configured coin selection strategy.
8✔
315
        utxos, err := wallet.ListUnspentWitnessFromDefaultAccount(
8✔
316
                1, math.MaxInt32,
8✔
317
        )
8✔
318
        if err != nil {
9✔
319
                return fmt.Errorf("list unspent witness: %w", err)
1✔
320
        }
1✔
321

322
        // Sort the UTXOs by putting smaller values at the start of the slice
323
        // to avoid locking large UTXO for sweeping.
324
        //
325
        // TODO(yy): add more choices to CoinSelectionStrategy and use the
326
        // configured value here.
327
        sort.Slice(utxos, func(i, j int) bool {
11✔
328
                return utxos[i].Value < utxos[j].Value
4✔
329
        })
4✔
330

331
        // Make a copy of the current inputs. If the wallet doesn't have enough
332
        // utxos to cover the budget, we will revert the current set to its
333
        // original state by removing the added wallet inputs.
334
        originalInputs := b.copyInputs()
7✔
335

7✔
336
        // Add wallet inputs to the set until the specified budget is covered.
7✔
337
        for _, utxo := range utxos {
14✔
338
                input, err := createWalletTxInput(utxo)
7✔
339
                if err != nil {
8✔
340
                        return err
1✔
341
                }
1✔
342

343
                pi := SweeperInput{
6✔
344
                        Input: input,
6✔
345
                        params: Params{
6✔
346
                                DeadlineHeight: fn.Some(b.deadlineHeight),
6✔
347
                        },
6✔
348
                }
6✔
349
                b.addInput(pi)
6✔
350

6✔
351
                log.Debugf("Added wallet input to input set: op=%v, amt=%v",
6✔
352
                        pi.OutPoint(), utxo.Value)
6✔
353

6✔
354
                // Return if we've reached the minimum output amount.
6✔
355
                if !b.NeedWalletInput() {
10✔
356
                        return nil
4✔
357
                }
4✔
358
        }
359

360
        // The wallet doesn't have enough utxos to cover the budget. Revert the
361
        // input set to its original state.
362
        b.inputs = originalInputs
5✔
363

5✔
364
        return ErrNotEnoughInputs
5✔
365
}
366

367
// Budget returns the total budget of the set.
368
//
369
// NOTE: part of the InputSet interface.
370
func (b *BudgetInputSet) Budget() btcutil.Amount {
26✔
371
        budget := btcutil.Amount(0)
26✔
372
        for _, input := range b.inputs {
66✔
373
                budget += input.params.Budget
40✔
374
        }
40✔
375

376
        return budget
26✔
377
}
378

379
// DeadlineHeight returns the deadline height of the set.
380
//
381
// NOTE: part of the InputSet interface.
382
func (b *BudgetInputSet) DeadlineHeight() int32 {
25✔
383
        return b.deadlineHeight
25✔
384
}
25✔
385

386
// Inputs returns the inputs that should be used to create a tx.
387
//
388
// NOTE: part of the InputSet interface.
389
func (b *BudgetInputSet) Inputs() []input.Input {
13✔
390
        inputs := make([]input.Input, 0, len(b.inputs))
13✔
391
        for _, inp := range b.inputs {
31✔
392
                inputs = append(inputs, inp.Input)
18✔
393
        }
18✔
394

395
        return inputs
13✔
396
}
397

398
// StartingFeeRate returns the max starting fee rate found in the inputs.
399
//
400
// NOTE: part of the InputSet interface.
401
func (b *BudgetInputSet) StartingFeeRate() fn.Option[chainfee.SatPerKWeight] {
3✔
402
        maxFeeRate := chainfee.SatPerKWeight(0)
3✔
403
        startingFeeRate := fn.None[chainfee.SatPerKWeight]()
3✔
404

3✔
405
        for _, inp := range b.inputs {
6✔
406
                feerate := inp.params.StartingFeeRate.UnwrapOr(0)
3✔
407
                if feerate > maxFeeRate {
5✔
408
                        maxFeeRate = feerate
2✔
409
                        startingFeeRate = fn.Some(maxFeeRate)
2✔
410
                }
2✔
411
        }
412

413
        return startingFeeRate
3✔
414
}
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