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

lightningnetwork / lnd / 12115442155

02 Dec 2024 08:28AM UTC coverage: 48.662% (-10.3%) from 58.948%
12115442155

Pull #9175

github

ellemouton
netann: update ChanAnn2 validation to work for P2WSH channels

This commit expands the ChannelAnnouncement2 validation for the case
where it is announcing a P2WSH channel.
Pull Request #9175: lnwire+netann: update structure of g175 messages to be pure TLV

6 of 314 new or added lines in 9 files covered. (1.91%)

27532 existing lines in 434 files now uncovered.

97890 of 201164 relevant lines covered (48.66%)

0.52 hits per line

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

87.92
/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) {
1✔
72
        signDesc := &input.SignDescriptor{
1✔
73
                Output: &wire.TxOut{
1✔
74
                        PkScript: utxo.PkScript,
1✔
75
                        Value:    int64(utxo.Value),
1✔
76
                },
1✔
77
                HashType: txscript.SigHashAll,
1✔
78
        }
1✔
79

1✔
80
        var witnessType input.WitnessType
1✔
81
        switch utxo.AddressType {
1✔
82
        case lnwallet.WitnessPubKey:
1✔
83
                witnessType = input.WitnessKeyHash
1✔
84
        case lnwallet.NestedWitnessPubKey:
×
85
                witnessType = input.NestedWitnessKeyHash
×
86
        case lnwallet.TaprootPubkey:
1✔
87
                witnessType = input.TaprootPubKeySpend
1✔
88
                signDesc.HashType = txscript.SigHashDefault
1✔
UNCOV
89
        default:
×
UNCOV
90
                return nil, fmt.Errorf("unknown address type %v",
×
UNCOV
91
                        utxo.AddressType)
×
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)
1✔
97

1✔
98
        return input.NewBaseInput(
1✔
99
                &utxo.OutPoint, witnessType, signDesc, heightHint,
1✔
100
        ), nil
1✔
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 {
1✔
131
        // Sanity check the input slice to ensure it's non-empty.
1✔
132
        if len(inputs) == 0 {
1✔
UNCOV
133
                return errEmptyInputs
×
UNCOV
134
        }
×
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
1✔
140

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

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

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

162
        // Make sure the inputs share the same deadline height when there is
163
        // one.
164
        if inputDeadline != deadlineHeight {
1✔
UNCOV
165
                return fmt.Errorf("input deadline height not matched: want "+
×
UNCOV
166
                        "%d, got %d", deadlineHeight, inputDeadline)
×
UNCOV
167
        }
×
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) {
1✔
UNCOV
172
                return fmt.Errorf("duplicate inputs")
×
UNCOV
173
        }
×
174

175
        return nil
1✔
176
}
177

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

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

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

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

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

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

204
        return bi, nil
1✔
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 {
1✔
210
        extraBudget, err := fn.MapOptionZ(
1✔
211
                s, func(aux AuxSweeper) fn.Result[btcutil.Amount] {
1✔
UNCOV
212
                        return aux.ExtraBudgetForInputs(b.Inputs())
×
UNCOV
213
                },
×
214
        ).Unpack()
215
        if err != nil {
1✔
216
                return err
×
217
        }
×
218

219
        b.extraBudget = extraBudget
1✔
220

1✔
221
        return nil
1✔
222
}
223

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

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

235
// addInput adds an input to the input set.
236
func (b *BudgetInputSet) addInput(input SweeperInput) {
1✔
237
        b.inputs = append(b.inputs, &input)
1✔
238
}
1✔
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 {
1✔
245
        var (
1✔
246
                // budgetNeeded is the amount that needs to be covered from
1✔
247
                // other inputs. We start at the value of the extra budget,
1✔
248
                // which might be needed for custom channels that add extra
1✔
249
                // outputs.
1✔
250
                budgetNeeded = b.extraBudget
1✔
251

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

1✔
257
        for _, inp := range b.inputs {
2✔
258
                // If this input has a required output, we can assume it's a
1✔
259
                // second-level htlc txns input. Although this input must have
1✔
260
                // a value that can cover its budget, it cannot be used to pay
1✔
261
                // fees. Instead, we need to borrow budget from other inputs to
1✔
262
                // make the sweep happen. Once swept, the input value will be
1✔
263
                // credited to the wallet.
1✔
264
                if inp.RequiredTxOut() != nil {
2✔
265
                        budgetNeeded += inp.params.Budget
1✔
266
                        continue
1✔
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
1✔
272
                output := btcutil.Amount(inp.SignDesc().Output.Value)
1✔
273
                budgetBorrowable += output - budget
1✔
274

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

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

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

292
// copyInputs returns a copy of the slice of the inputs in the set.
293
func (b *BudgetInputSet) copyInputs() []*SweeperInput {
1✔
294
        inputs := make([]*SweeperInput, len(b.inputs))
1✔
295
        copy(inputs, b.inputs)
1✔
296
        return inputs
1✔
297
}
1✔
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 {
1✔
312
        // Retrieve wallet utxos. Only consider confirmed utxos to prevent
1✔
313
        // problems around RBF rules for unconfirmed inputs. This currently
1✔
314
        // ignores the configured coin selection strategy.
1✔
315
        utxos, err := wallet.ListUnspentWitnessFromDefaultAccount(
1✔
316
                1, math.MaxInt32,
1✔
317
        )
1✔
318
        if err != nil {
1✔
UNCOV
319
                return fmt.Errorf("list unspent witness: %w", err)
×
UNCOV
320
        }
×
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 {
2✔
328
                return utxos[i].Value < utxos[j].Value
1✔
329
        })
1✔
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()
1✔
335

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

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

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

1✔
354
                // Return if we've reached the minimum output amount.
1✔
355
                if !b.NeedWalletInput() {
2✔
356
                        return nil
1✔
357
                }
1✔
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
1✔
363

1✔
364
        return ErrNotEnoughInputs
1✔
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 {
1✔
371
        budget := btcutil.Amount(0)
1✔
372
        for _, input := range b.inputs {
2✔
373
                budget += input.params.Budget
1✔
374
        }
1✔
375

376
        // We'll also tack on the extra budget which will eventually be
377
        // accounted for by the wallet txns when we're broadcasting.
378
        return budget + b.extraBudget
1✔
379
}
380

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

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

397
        return inputs
1✔
398
}
399

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

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

415
        return startingFeeRate
1✔
416
}
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