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

lightningnetwork / lnd / 12761228048

14 Jan 2025 04:36AM UTC coverage: 57.511% (-1.2%) from 58.719%
12761228048

Pull #9390

github

NishantBansal2003
docs: add release notes.

Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
Pull Request #9390: Enhance `lncli` listchannels command with the chan_id and short_chan_id (human readable format)

1 of 70 new or added lines in 2 files covered. (1.43%)

19537 existing lines in 250 files now uncovered.

102701 of 178577 relevant lines covered (57.51%)

24806.24 hits per line

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

71.47
/contractcourt/htlc_timeout_resolver.go
1
package contractcourt
2

3
import (
4
        "encoding/binary"
5
        "fmt"
6
        "io"
7
        "sync"
8

9
        "github.com/btcsuite/btcd/btcutil"
10
        "github.com/btcsuite/btcd/chaincfg/chainhash"
11
        "github.com/btcsuite/btcd/txscript"
12
        "github.com/btcsuite/btcd/wire"
13
        "github.com/davecgh/go-spew/spew"
14
        "github.com/lightningnetwork/lnd/chainntnfs"
15
        "github.com/lightningnetwork/lnd/channeldb"
16
        "github.com/lightningnetwork/lnd/fn/v2"
17
        "github.com/lightningnetwork/lnd/input"
18
        "github.com/lightningnetwork/lnd/lntypes"
19
        "github.com/lightningnetwork/lnd/lnutils"
20
        "github.com/lightningnetwork/lnd/lnwallet"
21
        "github.com/lightningnetwork/lnd/lnwire"
22
        "github.com/lightningnetwork/lnd/sweep"
23
)
24

25
// htlcTimeoutResolver is a ContractResolver that's capable of resolving an
26
// outgoing HTLC. The HTLC may be on our commitment transaction, or on the
27
// commitment transaction of the remote party. An output on our commitment
28
// transaction is considered fully resolved once the second-level transaction
29
// has been confirmed (and reached a sufficient depth). An output on the
30
// commitment transaction of the remote party is resolved once we detect a
31
// spend of the direct HTLC output using the timeout clause.
32
type htlcTimeoutResolver struct {
33
        // htlcResolution contains all the information required to properly
34
        // resolve this outgoing HTLC.
35
        htlcResolution lnwallet.OutgoingHtlcResolution
36

37
        // outputIncubating returns true if we've sent the output to the output
38
        // incubator (utxo nursery).
39
        outputIncubating bool
40

41
        // broadcastHeight is the height that the original contract was
42
        // broadcast to the main-chain at. We'll use this value to bound any
43
        // historical queries to the chain for spends/confirmations.
44
        //
45
        // TODO(roasbeef): wrap above into definite resolution embedding?
46
        broadcastHeight uint32
47

48
        // htlc contains information on the htlc that we are resolving on-chain.
49
        htlc channeldb.HTLC
50

51
        // currentReport stores the current state of the resolver for reporting
52
        // over the rpc interface. This should only be reported in case we have
53
        // a non-nil SignDetails on the htlcResolution, otherwise the nursery
54
        // will produce reports.
55
        currentReport ContractReport
56

57
        // reportLock prevents concurrent access to the resolver report.
58
        reportLock sync.Mutex
59

60
        contractResolverKit
61

62
        htlcLeaseResolver
63

64
        // incomingHTLCExpiryHeight is the absolute block height at which the
65
        // incoming HTLC will expire. This is used as the deadline height as
66
        // the outgoing HTLC must be swept before its incoming HTLC expires.
67
        incomingHTLCExpiryHeight fn.Option[int32]
68
}
69

70
// newTimeoutResolver instantiates a new timeout htlc resolver.
71
func newTimeoutResolver(res lnwallet.OutgoingHtlcResolution,
72
        broadcastHeight uint32, htlc channeldb.HTLC,
73
        resCfg ResolverConfig) *htlcTimeoutResolver {
1✔
74

1✔
75
        h := &htlcTimeoutResolver{
1✔
76
                contractResolverKit: *newContractResolverKit(resCfg),
1✔
77
                htlcResolution:      res,
1✔
78
                broadcastHeight:     broadcastHeight,
1✔
79
                htlc:                htlc,
1✔
80
        }
1✔
81

1✔
82
        h.initReport()
1✔
83
        h.initLogger(fmt.Sprintf("%T(%v)", h, h.outpoint()))
1✔
84

1✔
85
        return h
1✔
86
}
1✔
87

88
// isTaproot returns true if the htlc output is a taproot output.
89
func (h *htlcTimeoutResolver) isTaproot() bool {
51✔
90
        return txscript.IsPayToTaproot(
51✔
91
                h.htlcResolution.SweepSignDesc.Output.PkScript,
51✔
92
        )
51✔
93
}
51✔
94

95
// outpoint returns the outpoint of the HTLC output we're attempting to sweep.
96
func (h *htlcTimeoutResolver) outpoint() wire.OutPoint {
43✔
97
        // The primary key for this resolver will be the outpoint of the HTLC
43✔
98
        // on the commitment transaction itself. If this is our commitment,
43✔
99
        // then the output can be found within the signed timeout tx,
43✔
100
        // otherwise, it's just the ClaimOutpoint.
43✔
101
        if h.htlcResolution.SignedTimeoutTx != nil {
69✔
102
                return h.htlcResolution.SignedTimeoutTx.TxIn[0].PreviousOutPoint
26✔
103
        }
26✔
104

105
        return h.htlcResolution.ClaimOutpoint
17✔
106
}
107

108
// ResolverKey returns an identifier which should be globally unique for this
109
// particular resolver within the chain the original contract resides within.
110
//
111
// NOTE: Part of the ContractResolver interface.
112
func (h *htlcTimeoutResolver) ResolverKey() []byte {
19✔
113
        key := newResolverID(h.outpoint())
19✔
114
        return key[:]
19✔
115
}
19✔
116

117
const (
118
        // expectedRemoteWitnessSuccessSize is the expected size of the witness
119
        // on the remote commitment transaction for an outgoing HTLC that is
120
        // swept on-chain by them with pre-image.
121
        expectedRemoteWitnessSuccessSize = 5
122

123
        // expectedLocalWitnessSuccessSize is the expected size of the witness
124
        // on the local commitment transaction for an outgoing HTLC that is
125
        // swept on-chain by them with pre-image.
126
        expectedLocalWitnessSuccessSize = 3
127

128
        // remotePreimageIndex index within the witness on the remote
129
        // commitment transaction that will hold they pre-image if they go to
130
        // sweep it on chain.
131
        remotePreimageIndex = 3
132

133
        // localPreimageIndex is the index within the witness on the local
134
        // commitment transaction for an outgoing HTLC that will hold the
135
        // pre-image if the remote party sweeps it.
136
        localPreimageIndex = 1
137

138
        // remoteTaprootWitnessSuccessSize is the expected size of the witness
139
        // on the remote commitment for taproot channels. The spend path will
140
        // look like
141
        //   - <sender sig> <receiver sig> <preimage> <success_script>
142
        //     <control_block>
143
        remoteTaprootWitnessSuccessSize = 5
144

145
        // localTaprootWitnessSuccessSize is the expected size of the witness
146
        // on the local commitment for taproot channels. The spend path will
147
        // look like
148
        //  - <receiver sig> <preimage> <success_script> <control_block>
149
        localTaprootWitnessSuccessSize = 4
150

151
        // taprootRemotePreimageIndex is the index within the witness on the
152
        // taproot remote commitment spend that'll hold the pre-image if the
153
        // remote party sweeps it.
154
        taprootRemotePreimageIndex = 2
155
)
156

157
// claimCleanUp is a helper method that's called once the HTLC output is spent
158
// by the remote party. It'll extract the preimage, add it to the global cache,
159
// and finally send the appropriate clean up message.
160
func (h *htlcTimeoutResolver) claimCleanUp(
161
        commitSpend *chainntnfs.SpendDetail) error {
6✔
162

6✔
163
        // Depending on if this is our commitment or not, then we'll be looking
6✔
164
        // for a different witness pattern.
6✔
165
        spenderIndex := commitSpend.SpenderInputIndex
6✔
166
        spendingInput := commitSpend.SpendingTx.TxIn[spenderIndex]
6✔
167

6✔
168
        log.Infof("%T(%v): extracting preimage! remote party spent "+
6✔
169
                "HTLC with tx=%v", h, h.htlcResolution.ClaimOutpoint,
6✔
170
                spew.Sdump(commitSpend.SpendingTx))
6✔
171

6✔
172
        // If this is the remote party's commitment, then we'll be looking for
6✔
173
        // them to spend using the second-level success transaction.
6✔
174
        var preimageBytes []byte
6✔
175
        switch {
6✔
176
        // For taproot channels, if the remote party has swept the HTLC, then
177
        // the witness stack will look like:
178
        //
179
        //   - <sender sig> <receiver sig> <preimage> <success_script>
180
        //     <control_block>
UNCOV
181
        case h.isTaproot() && h.htlcResolution.SignedTimeoutTx == nil:
×
UNCOV
182
                //nolint:ll
×
UNCOV
183
                preimageBytes = spendingInput.Witness[taprootRemotePreimageIndex]
×
184

185
        // The witness stack when the remote party sweeps the output on a
186
        // regular channel to them looks like:
187
        //
188
        //  - <0> <sender sig> <recvr sig> <preimage> <witness script>
189
        case !h.isTaproot() && h.htlcResolution.SignedTimeoutTx == nil:
3✔
190
                preimageBytes = spendingInput.Witness[remotePreimageIndex]
3✔
191

192
        // If this is a taproot channel, and there's only a single witness
193
        // element, then we're actually on the losing side of a breach
194
        // attempt...
UNCOV
195
        case h.isTaproot() && len(spendingInput.Witness) == 1:
×
UNCOV
196
                return fmt.Errorf("breach attempt failed")
×
197

198
        // Otherwise, they'll be spending directly from our commitment output.
199
        // In which case the witness stack looks like:
200
        //
201
        //  - <sig> <preimage> <witness script>
202
        //
203
        // For taproot channels, this looks like:
204
        //  - <receiver sig> <preimage> <success_script> <control_block>
205
        //
206
        // So we can target the same index.
207
        default:
3✔
208
                preimageBytes = spendingInput.Witness[localPreimageIndex]
3✔
209
        }
210

211
        preimage, err := lntypes.MakePreimage(preimageBytes)
6✔
212
        if err != nil {
6✔
213
                return fmt.Errorf("unable to create pre-image from witness: %w",
×
214
                        err)
×
215
        }
×
216

217
        log.Infof("%T(%v): extracting preimage=%v from on-chain "+
6✔
218
                "spend!", h, h.htlcResolution.ClaimOutpoint, preimage)
6✔
219

6✔
220
        // With the preimage obtained, we can now add it to the global cache.
6✔
221
        if err := h.PreimageDB.AddPreimages(preimage); err != nil {
6✔
222
                log.Errorf("%T(%v): unable to add witness to cache",
×
223
                        h, h.htlcResolution.ClaimOutpoint)
×
224
        }
×
225

226
        var pre [32]byte
6✔
227
        copy(pre[:], preimage[:])
6✔
228

6✔
229
        // Finally, we'll send the clean up message, mark ourselves as
6✔
230
        // resolved, then exit.
6✔
231
        if err := h.DeliverResolutionMsg(ResolutionMsg{
6✔
232
                SourceChan: h.ShortChanID,
6✔
233
                HtlcIndex:  h.htlc.HtlcIndex,
6✔
234
                PreImage:   &pre,
6✔
235
        }); err != nil {
6✔
236
                return err
×
237
        }
×
238
        h.markResolved()
6✔
239

6✔
240
        // Checkpoint our resolver with a report which reflects the preimage
6✔
241
        // claim by the remote party.
6✔
242
        amt := btcutil.Amount(h.htlcResolution.SweepSignDesc.Output.Value)
6✔
243
        report := &channeldb.ResolverReport{
6✔
244
                OutPoint:        h.htlcResolution.ClaimOutpoint,
6✔
245
                Amount:          amt,
6✔
246
                ResolverType:    channeldb.ResolverTypeOutgoingHtlc,
6✔
247
                ResolverOutcome: channeldb.ResolverOutcomeClaimed,
6✔
248
                SpendTxID:       commitSpend.SpenderTxHash,
6✔
249
        }
6✔
250

6✔
251
        return h.Checkpoint(h, report)
6✔
252
}
253

254
// chainDetailsToWatch returns the output and script which we use to watch for
255
// spends from the direct HTLC output on the commitment transaction.
256
func (h *htlcTimeoutResolver) chainDetailsToWatch() (*wire.OutPoint, []byte, error) {
19✔
257
        // If there's no timeout transaction, it means we are spending from a
19✔
258
        // remote commit, then the claim output is the output directly on the
19✔
259
        // commitment transaction, so we'll just use that.
19✔
260
        if h.htlcResolution.SignedTimeoutTx == nil {
25✔
261
                outPointToWatch := h.htlcResolution.ClaimOutpoint
6✔
262
                scriptToWatch := h.htlcResolution.SweepSignDesc.Output.PkScript
6✔
263

6✔
264
                return &outPointToWatch, scriptToWatch, nil
6✔
265
        }
6✔
266

267
        // If SignedTimeoutTx is not nil, this is the local party's commitment,
268
        // and we'll need to grab watch the output that our timeout transaction
269
        // points to. We can directly grab the outpoint, then also extract the
270
        // witness script (the last element of the witness stack) to
271
        // re-construct the pkScript we need to watch.
272
        //
273
        //nolint:ll
274
        outPointToWatch := h.htlcResolution.SignedTimeoutTx.TxIn[0].PreviousOutPoint
13✔
275
        witness := h.htlcResolution.SignedTimeoutTx.TxIn[0].Witness
13✔
276

13✔
277
        var (
13✔
278
                scriptToWatch []byte
13✔
279
                err           error
13✔
280
        )
13✔
281
        switch {
13✔
282
        // For taproot channels, then final witness element is the control
283
        // block, and the one before it the witness script. We can use both of
284
        // these together to reconstruct the taproot output key, then map that
285
        // into a v1 witness program.
UNCOV
286
        case h.isTaproot():
×
UNCOV
287
                // First, we'll parse the control block into something we can
×
UNCOV
288
                // use.
×
UNCOV
289
                ctrlBlockBytes := witness[len(witness)-1]
×
UNCOV
290
                ctrlBlock, err := txscript.ParseControlBlock(ctrlBlockBytes)
×
UNCOV
291
                if err != nil {
×
292
                        return nil, nil, err
×
293
                }
×
294

295
                // With the control block, we'll grab the witness script, then
296
                // use that to derive the tapscript root.
UNCOV
297
                witnessScript := witness[len(witness)-2]
×
UNCOV
298
                tapscriptRoot := ctrlBlock.RootHash(witnessScript)
×
UNCOV
299

×
UNCOV
300
                // Once we have the root, then we can derive the output key
×
UNCOV
301
                // from the internal key, then turn that into a witness
×
UNCOV
302
                // program.
×
UNCOV
303
                outputKey := txscript.ComputeTaprootOutputKey(
×
UNCOV
304
                        ctrlBlock.InternalKey, tapscriptRoot,
×
UNCOV
305
                )
×
UNCOV
306
                scriptToWatch, err = txscript.PayToTaprootScript(outputKey)
×
UNCOV
307
                if err != nil {
×
308
                        return nil, nil, err
×
309
                }
×
310

311
        // For regular channels, the witness script is the last element on the
312
        // stack. We can then use this to re-derive the output that we're
313
        // watching on chain.
314
        default:
13✔
315
                scriptToWatch, err = input.WitnessScriptHash(
13✔
316
                        witness[len(witness)-1],
13✔
317
                )
13✔
318
        }
319
        if err != nil {
13✔
320
                return nil, nil, err
×
321
        }
×
322

323
        return &outPointToWatch, scriptToWatch, nil
13✔
324
}
325

326
// isPreimageSpend returns true if the passed spend on the specified commitment
327
// is a success spend that reveals the pre-image or not.
328
func isPreimageSpend(isTaproot bool, spend *chainntnfs.SpendDetail,
329
        localCommit bool) bool {
19✔
330

19✔
331
        // Based on the spending input index and transaction, obtain the
19✔
332
        // witness that tells us what type of spend this is.
19✔
333
        spenderIndex := spend.SpenderInputIndex
19✔
334
        spendingInput := spend.SpendingTx.TxIn[spenderIndex]
19✔
335
        spendingWitness := spendingInput.Witness
19✔
336

19✔
337
        switch {
19✔
338
        // If this is a taproot remote commitment, then we can detect the type
339
        // of spend via the leaf revealed in the control block and the witness
340
        // itself.
341
        //
342
        // The keyspend (revocation path) is just a single signature, while the
343
        // timeout and success paths are most distinct.
344
        //
345
        // The success path will look like:
346
        //
347
        //   - <sender sig> <receiver sig> <preimage> <success_script>
348
        //     <control_block>
349
        case isTaproot && !localCommit:
1✔
350
                return checkSizeAndIndex(
1✔
351
                        spendingWitness, remoteTaprootWitnessSuccessSize,
1✔
352
                        taprootRemotePreimageIndex,
1✔
353
                )
1✔
354

355
        // Otherwise, then if this is our local commitment transaction, then if
356
        // they're sweeping the transaction, it'll be directly from the output,
357
        // skipping the second level.
358
        //
359
        // In this case, then there're two main tapscript paths, with the
360
        // success case look like:
361
        //
362
        //  - <receiver sig> <preimage> <success_script> <control_block>
363
        case isTaproot && localCommit:
1✔
364
                return checkSizeAndIndex(
1✔
365
                        spendingWitness, localTaprootWitnessSuccessSize,
1✔
366
                        localPreimageIndex,
1✔
367
                )
1✔
368

369
        // If this is the non-taproot, remote commitment then the only possible
370
        // spends for outgoing HTLCs are:
371
        //
372
        //  RECVR: <0> <sender sig> <recvr sig> <preimage> (2nd level success spend)
373
        //  REVOK: <sig> <key>
374
        //  SENDR: <sig> 0
375
        //
376
        // In this case, if 5 witness elements are present (factoring the
377
        // witness script), and the 3rd element is the size of the pre-image,
378
        // then this is a remote spend. If not, then we swept it ourselves, or
379
        // revoked their output.
380
        case !isTaproot && !localCommit:
5✔
381
                return checkSizeAndIndex(
5✔
382
                        spendingWitness, expectedRemoteWitnessSuccessSize,
5✔
383
                        remotePreimageIndex,
5✔
384
                )
5✔
385

386
        // Otherwise, for our non-taproot commitment, the only possible spends
387
        // for an outgoing HTLC are:
388
        //
389
        //  SENDR: <0> <sendr sig>  <recvr sig> <0> (2nd level timeout)
390
        //  RECVR: <recvr sig>  <preimage>
391
        //  REVOK: <revoke sig> <revoke key>
392
        //
393
        // So the only success case has the pre-image as the 2nd (index 1)
394
        // element in the witness.
395
        case !isTaproot:
12✔
396
                fallthrough
12✔
397

398
        default:
12✔
399
                return checkSizeAndIndex(
12✔
400
                        spendingWitness, expectedLocalWitnessSuccessSize,
12✔
401
                        localPreimageIndex,
12✔
402
                )
12✔
403
        }
404
}
405

406
// checkSizeAndIndex checks that the witness is of the expected size and that
407
// the witness element at the specified index is of the expected size.
408
func checkSizeAndIndex(witness wire.TxWitness, size, index int) bool {
22✔
409
        if len(witness) != size {
33✔
410
                return false
11✔
411
        }
11✔
412

413
        return len(witness[index]) == lntypes.HashSize
11✔
414
}
415

416
// Resolve kicks off full resolution of an outgoing HTLC output. If it's our
417
// commitment, it isn't resolved until we see the second level HTLC txn
418
// confirmed. If it's the remote party's commitment, we don't resolve until we
419
// see a direct sweep via the timeout clause.
420
//
421
// NOTE: Part of the ContractResolver interface.
422
func (h *htlcTimeoutResolver) Resolve() (ContractResolver, error) {
19✔
423
        // If we're already resolved, then we can exit early.
19✔
424
        if h.IsResolved() {
25✔
425
                h.log.Errorf("already resolved")
6✔
426
                return nil, nil
6✔
427
        }
6✔
428

429
        // If this is an output on the remote party's commitment transaction,
430
        // use the direct-spend path to sweep the htlc.
431
        if h.isRemoteCommitOutput() {
17✔
432
                return nil, h.resolveRemoteCommitOutput()
4✔
433
        }
4✔
434

435
        // If this is a zero-fee HTLC, we now handle the spend from our
436
        // commitment transaction.
437
        if h.isZeroFeeOutput() {
12✔
438
                return nil, h.resolveTimeoutTx()
3✔
439
        }
3✔
440

441
        // If this is an output on our own commitment using pre-anchor channel
442
        // type, we will let the utxo nursery handle it.
443
        return nil, h.resolveSecondLevelTxLegacy()
6✔
444
}
445

446
// sweepTimeoutTx sends a second level timeout transaction to the sweeper.
447
// This transaction uses the SINLGE|ANYONECANPAY flag.
448
func (h *htlcTimeoutResolver) sweepTimeoutTx() error {
2✔
449
        var inp input.Input
2✔
450
        if h.isTaproot() {
2✔
UNCOV
451
                inp = lnutils.Ptr(input.MakeHtlcSecondLevelTimeoutTaprootInput(
×
UNCOV
452
                        h.htlcResolution.SignedTimeoutTx,
×
UNCOV
453
                        h.htlcResolution.SignDetails,
×
UNCOV
454
                        h.broadcastHeight,
×
UNCOV
455
                        input.WithResolutionBlob(
×
UNCOV
456
                                h.htlcResolution.ResolutionBlob,
×
UNCOV
457
                        ),
×
UNCOV
458
                ))
×
459
        } else {
2✔
460
                inp = lnutils.Ptr(input.MakeHtlcSecondLevelTimeoutAnchorInput(
2✔
461
                        h.htlcResolution.SignedTimeoutTx,
2✔
462
                        h.htlcResolution.SignDetails,
2✔
463
                        h.broadcastHeight,
2✔
464
                ))
2✔
465
        }
2✔
466

467
        // Calculate the budget.
468
        //
2✔
469
        // TODO(yy): the budget is twice the output's value, which is needed as
2✔
470
        // we don't force sweep the output now. To prevent cascading force
2✔
471
        // closes, we use all its output value plus a wallet input as the
2✔
472
        // budget. This is a temporary solution until we can optionally cancel
2✔
473
        // the incoming HTLC, more details in,
2✔
474
        // - https://github.com/lightningnetwork/lnd/issues/7969
2✔
475
        budget := calculateBudget(
2✔
476
                btcutil.Amount(inp.SignDesc().Output.Value), 2, 0,
2✔
477
        )
2✔
478

2✔
479
        h.log.Infof("offering 2nd-level HTLC timeout tx to sweeper "+
2✔
480
                "with deadline=%v, budget=%v", h.incomingHTLCExpiryHeight,
2✔
481
                budget)
2✔
482

2✔
483
        // For an outgoing HTLC, it must be swept before the RefundTimeout of
2✔
484
        // its incoming HTLC is reached.
2✔
485
        _, err := h.Sweeper.SweepInput(
2✔
486
                inp,
2✔
487
                sweep.Params{
×
488
                        Budget:         budget,
×
489
                        DeadlineHeight: h.incomingHTLCExpiryHeight,
490
                },
2✔
491
        )
492
        if err != nil {
493
                return err
494
        }
495

6✔
496
        return err
6✔
497
}
6✔
498

6✔
499
// resolveSecondLevelTxLegacy sends a second level timeout transaction to the
6✔
500
// utxo nursery. This transaction uses the legacy SIGHASH_ALL flag.
6✔
501
func (h *htlcTimeoutResolver) resolveSecondLevelTxLegacy() error {
6✔
502
        h.log.Debug("incubating htlc output")
6✔
503

6✔
504
        // The utxo nursery will take care of broadcasting the second-level
6✔
505
        // timeout tx and sweeping its output once it confirms.
6✔
506
        err := h.IncubateOutputs(
×
507
                h.ChanPoint, fn.Some(h.htlcResolution),
×
508
                fn.None[lnwallet.IncomingHtlcResolution](),
509
                h.broadcastHeight, h.incomingHTLCExpiryHeight,
6✔
510
        )
511
        if err != nil {
512
                return err
513
        }
514

515
        return h.resolveTimeoutTx()
516
}
4✔
517

4✔
518
// sweepDirectHtlcOutput sends the direct spend of the HTLC output to the
4✔
UNCOV
519
// sweeper. This is used when the remote party goes on chain, and we're able to
×
520
// sweep an HTLC we offered after a timeout. Only the CLTV encumbered outputs
4✔
521
// are resolved via this path.
4✔
522
func (h *htlcTimeoutResolver) sweepDirectHtlcOutput() error {
4✔
523
        var htlcWitnessType input.StandardWitnessType
524
        if h.isTaproot() {
4✔
525
                htlcWitnessType = input.TaprootHtlcOfferedRemoteTimeout
4✔
526
        } else {
4✔
527
                htlcWitnessType = input.HtlcOfferedRemoteTimeout
4✔
528
        }
4✔
529

4✔
530
        sweepInput := input.NewCsvInputWithCltv(
4✔
531
                &h.htlcResolution.ClaimOutpoint, htlcWitnessType,
4✔
532
                &h.htlcResolution.SweepSignDesc, h.broadcastHeight,
4✔
533
                h.htlcResolution.CsvDelay, h.htlcResolution.Expiry,
4✔
534
                input.WithResolutionBlob(h.htlcResolution.ResolutionBlob),
4✔
535
        )
4✔
536

4✔
537
        // Calculate the budget.
4✔
538
        //
4✔
539
        // TODO(yy): the budget is twice the output's value, which is needed as
4✔
540
        // we don't force sweep the output now. To prevent cascading force
4✔
541
        // closes, we use all its output value plus a wallet input as the
4✔
542
        // budget. This is a temporary solution until we can optionally cancel
4✔
543
        // the incoming HTLC, more details in,
4✔
544
        // - https://github.com/lightningnetwork/lnd/issues/7969
4✔
545
        budget := calculateBudget(
4✔
546
                btcutil.Amount(sweepInput.SignDesc().Output.Value), 2, 0,
4✔
547
        )
4✔
548

4✔
549
        log.Infof("%T(%x): offering offered remote timeout HTLC output to "+
4✔
550
                "sweeper with deadline %v and budget=%v at height=%v",
4✔
551
                h, h.htlc.RHash[:], h.incomingHTLCExpiryHeight, budget,
4✔
552
                h.broadcastHeight)
4✔
553

×
554
        _, err := h.Sweeper.SweepInput(
×
555
                sweepInput,
556
                sweep.Params{
4✔
557
                        Budget: budget,
558

559
                        // This is an outgoing HTLC, so we want to make sure
560
                        // that we sweep it before the incoming HTLC expires.
561
                        DeadlineHeight: h.incomingHTLCExpiryHeight,
562
                },
563
        )
15✔
564
        if err != nil {
15✔
565
                return err
15✔
566
        }
15✔
567

15✔
568
        return nil
15✔
569
}
×
570

×
571
// watchHtlcSpend watches for a spend of the HTLC output. For neutrino backend,
572
// it will check blocks for the confirmed spend. For btcd and bitcoind, it will
573
// check both the mempool and the blocks.
574
func (h *htlcTimeoutResolver) watchHtlcSpend() (*chainntnfs.SpendDetail,
30✔
575
        error) {
15✔
576

15✔
577
        // TODO(yy): outpointToWatch is always h.HtlcOutpoint(), can refactor
578
        // to remove the redundancy.
UNCOV
579
        outpointToWatch, scriptToWatch, err := h.chainDetailsToWatch()
×
580
        if err != nil {
581
                return nil, err
582
        }
583

584
        // If there's no mempool configured, which is the case for SPV node
585
        // such as neutrino, then we will watch for confirmed spend only.
15✔
586
        if h.Mempool == nil {
15✔
587
                return h.waitForConfirmedSpend(outpointToWatch, scriptToWatch)
15✔
588
        }
15✔
589

15✔
590
        // Watch for a spend of the HTLC output in both the mempool and blocks.
15✔
591
        return h.waitForMempoolOrBlockSpend(*outpointToWatch, scriptToWatch)
15✔
592
}
15✔
UNCOV
593

×
UNCOV
594
// waitForConfirmedSpend waits for the HTLC output to be spent and confirmed in
×
595
// a block, returns the spend details.
596
func (h *htlcTimeoutResolver) waitForConfirmedSpend(op *wire.OutPoint,
15✔
597
        pkScript []byte) (*chainntnfs.SpendDetail, error) {
598

599
        // We'll block here until either we exit, or the HTLC output on the
600
        // commitment transaction has been spent.
601
        spend, err := waitForSpend(
602
                op, pkScript, h.broadcastHeight, h.Notifier, h.quit,
603
        )
1✔
604
        if err != nil {
1✔
605
                return nil, err
1✔
606
        }
1✔
607

1✔
608
        return spend, err
1✔
609
}
610

UNCOV
611
// Stop signals the resolver to cancel any current resolution processes, and
×
UNCOV
612
// suspend.
×
UNCOV
613
//
×
UNCOV
614
// NOTE: Part of the ContractResolver interface.
×
UNCOV
615
func (h *htlcTimeoutResolver) Stop() {
×
UNCOV
616
        h.log.Debugf("stopping...")
×
617
        defer h.log.Debugf("stopped")
×
618

×
619
        close(h.quit)
UNCOV
620
}
×
UNCOV
621

×
UNCOV
622
// report returns a report on the resolution state of the contract.
×
UNCOV
623
func (h *htlcTimeoutResolver) report() *ContractReport {
×
624
        // If we have a SignedTimeoutTx but no SignDetails, this is a local
625
        // commitment for a non-anchor channel, which was handled by the utxo
626
        // nursery.
16✔
627
        if h.htlcResolution.SignDetails == nil && h.
16✔
628
                htlcResolution.SignedTimeoutTx != nil {
16✔
629
                return nil
16✔
630
        }
26✔
631

10✔
632
        h.reportLock.Lock()
10✔
633
        defer h.reportLock.Unlock()
10✔
634
        cpy := h.currentReport
10✔
635
        return &cpy
636
}
637

638
func (h *htlcTimeoutResolver) initReport() {
16✔
639
        // We create the initial report. This will only be reported for
22✔
640
        // resolvers not handled by the nursery.
6✔
641
        finalAmt := h.htlc.Amt.ToSatoshis()
6✔
642
        if h.htlcResolution.SignedTimeoutTx != nil {
643
                finalAmt = btcutil.Amount(
16✔
644
                        h.htlcResolution.SignedTimeoutTx.TxOut[0].Value,
16✔
645
                )
16✔
646
        }
16✔
647

16✔
648
        // If there's no timeout transaction, then we're already effectively in
16✔
649
        // level two.
16✔
650
        stage := uint32(1)
16✔
651
        if h.htlcResolution.SignedTimeoutTx == nil {
652
                stage = 2
653
        }
654

655
        h.currentReport = ContractReport{
656
                Outpoint:       h.htlcResolution.ClaimOutpoint,
657
                Type:           ReportOutputOutgoingHtlc,
19✔
658
                Amount:         finalAmt,
19✔
659
                MaturityHeight: h.htlcResolution.Expiry,
19✔
660
                LimboBalance:   finalAmt,
19✔
661
                Stage:          stage,
×
662
        }
×
663
}
664

665
// Encode writes an encoded version of the ContractResolver into the passed
666
// Writer.
19✔
667
//
×
668
// NOTE: Part of the ContractResolver interface.
×
669
func (h *htlcTimeoutResolver) Encode(w io.Writer) error {
19✔
670
        // First, we'll write out the relevant fields of the
×
671
        // OutgoingHtlcResolution to the writer.
×
672
        if err := encodeOutgoingResolution(w, &h.htlcResolution); err != nil {
19✔
673
                return err
×
674
        }
×
675

676
        // With that portion written, we can now write out the fields specific
19✔
677
        // to the resolver itself.
×
678
        if err := binary.Write(w, endian, h.outputIncubating); err != nil {
×
679
                return err
680
        }
681
        if err := binary.Write(w, endian, h.IsResolved()); err != nil {
19✔
682
                return err
19✔
683
        }
×
684
        if err := binary.Write(w, endian, h.broadcastHeight); err != nil {
×
685
                return err
686
        }
19✔
687

688
        if err := binary.Write(w, endian, h.htlc.HtlcIndex); err != nil {
689
                return err
690
        }
691

692
        // We encode the sign details last for backwards compatibility.
693
        err := encodeSignDetails(w, h.htlcResolution.SignDetails)
15✔
694
        if err != nil {
15✔
695
                return err
15✔
696
        }
15✔
697

15✔
698
        return nil
15✔
699
}
15✔
700

15✔
701
// newTimeoutResolverFromReader attempts to decode an encoded ContractResolver
15✔
702
// from the passed Reader instance, returning an active ContractResolver
×
703
// instance.
×
704
func newTimeoutResolverFromReader(r io.Reader, resCfg ResolverConfig) (
705
        *htlcTimeoutResolver, error) {
706

707
        h := &htlcTimeoutResolver{
15✔
708
                contractResolverKit: *newContractResolverKit(resCfg),
×
709
        }
×
710

711
        // First, we'll read out all the mandatory fields of the
15✔
712
        // OutgoingHtlcResolution that we store.
15✔
713
        if err := decodeOutgoingResolution(r, &h.htlcResolution); err != nil {
×
714
                return nil, err
×
715
        }
24✔
716

9✔
717
        // With those fields read, we can now read back the fields that are
9✔
718
        // specific to the resolver itself.
719
        if err := binary.Read(r, endian, &h.outputIncubating); err != nil {
15✔
720
                return nil, err
×
721
        }
×
722

723
        var resolved bool
15✔
724
        if err := binary.Read(r, endian, &resolved); err != nil {
×
725
                return nil, err
×
726
        }
727
        if resolved {
728
                h.markResolved()
729
        }
730

15✔
731
        if err := binary.Read(r, endian, &h.broadcastHeight); err != nil {
30✔
732
                return nil, err
15✔
733
        }
15✔
734

×
735
        if err := binary.Read(r, endian, &h.htlc.HtlcIndex); err != nil {
×
736
                return nil, err
737
        }
15✔
738

15✔
739
        // Sign details is a new field that was added to the htlc resolution,
15✔
740
        // so it is serialized last for backwards compatibility. We try to read
15✔
741
        // it, but don't error out if there are not bytes left.
742
        signDetails, err := decodeSignDetails(r)
743
        if err == nil {
744
                h.htlcResolution.SignDetails = signDetails
745
        } else if err != io.EOF && err != io.ErrUnexpectedEOF {
746
                return nil, err
747
        }
9✔
748

9✔
749
        h.initReport()
9✔
750
        h.initLogger(fmt.Sprintf("%T(%v)", h, h.outpoint()))
751

752
        return h, nil
753
}
754

1✔
755
// Supplement adds additional information to the resolver that is required
1✔
756
// before Resolve() is called.
1✔
757
//
758
// NOTE: Part of the htlcContractResolver interface.
759
func (h *htlcTimeoutResolver) Supplement(htlc channeldb.HTLC) {
760
        h.htlc = htlc
761
}
UNCOV
762

×
UNCOV
763
// HtlcPoint returns the htlc's outpoint on the commitment tx.
×
UNCOV
764
//
×
765
// NOTE: Part of the htlcContractResolver interface.
766
func (h *htlcTimeoutResolver) HtlcPoint() wire.OutPoint {
767
        return h.htlcResolution.HtlcPoint()
768
}
769

770
// SupplementDeadline sets the incomingHTLCExpiryHeight for this outgoing htlc
771
// resolver.
772
//
773
// NOTE: Part of the htlcContractResolver interface.
774
func (h *htlcTimeoutResolver) SupplementDeadline(d fn.Option[int32]) {
775
        h.incomingHTLCExpiryHeight = d
776
}
777

778
// A compile time assertion to ensure htlcTimeoutResolver meets the
779
// ContractResolver interface.
780
var _ htlcContractResolver = (*htlcTimeoutResolver)(nil)
781

782
// spendResult is used to hold the result of a spend event from either a
UNCOV
783
// mempool spend or a block spend.
×
UNCOV
784
type spendResult struct {
×
UNCOV
785
        // spend contains the details of the spend.
×
UNCOV
786
        spend *chainntnfs.SpendDetail
×
UNCOV
787

×
UNCOV
788
        // err is the error that occurred during the spend notification.
×
UNCOV
789
        err error
×
UNCOV
790
}
×
UNCOV
791

×
UNCOV
792
// waitForMempoolOrBlockSpend waits for the htlc output to be spent by a
×
793
// transaction that's either be found in the mempool or in a block.
×
794
func (h *htlcTimeoutResolver) waitForMempoolOrBlockSpend(op wire.OutPoint,
×
795
        pkScript []byte) (*chainntnfs.SpendDetail, error) {
796

UNCOV
797
        log.Infof("%T(%v): waiting for spent of HTLC output %v to be found "+
×
UNCOV
798
                "in mempool or block", h, h.htlcResolution.ClaimOutpoint, op)
×
799

×
800
        // Subscribe for block spent(confirmed).
×
801
        blockSpent, err := h.Notifier.RegisterSpendNtfn(
802
                &op, pkScript, h.broadcastHeight,
803
        )
UNCOV
804
        if err != nil {
×
UNCOV
805
                return nil, fmt.Errorf("register spend: %w", err)
×
UNCOV
806
        }
×
UNCOV
807

×
UNCOV
808
        // Subscribe for mempool spent(unconfirmed).
×
UNCOV
809
        mempoolSpent, err := h.Mempool.SubscribeMempoolSpent(op)
×
UNCOV
810
        if err != nil {
×
UNCOV
811
                return nil, fmt.Errorf("register mempool spend: %w", err)
×
UNCOV
812
        }
×
UNCOV
813

×
UNCOV
814
        // Create a result chan that will be used to receive the spending
×
UNCOV
815
        // events.
×
UNCOV
816
        result := make(chan *spendResult, 2)
×
UNCOV
817

×
UNCOV
818
        // Create a goroutine that will wait for either a mempool spend or a
×
UNCOV
819
        // block spend.
×
820
        //
UNCOV
821
        // NOTE: no need to use waitgroup here as when the resolver exits, the
×
UNCOV
822
        // goroutine will return on the quit channel.
×
823
        go h.consumeSpendEvents(result, blockSpent.Spend, mempoolSpent.Spend)
824

825
        // Wait for the spend event to be received.
826
        select {
827
        case event := <-result:
828
                // Cancel the mempool subscription as we don't need it anymore.
829
                h.Mempool.CancelMempoolSpendEvent(mempoolSpent)
830

831
                return event.spend, event.err
832

833
        case <-h.quit:
834
                return nil, errResolverShuttingDown
835
        }
836
}
837

838
// consumeSpendEvents consumes the spend events from the block and mempool
UNCOV
839
// subscriptions. It exits when a spend event is received from the block, or
×
UNCOV
840
// the resolver itself quits. When a spend event is received from the mempool,
×
UNCOV
841
// however, it won't exit but continuing to wait for a spend event from the
×
UNCOV
842
// block subscription.
×
UNCOV
843
//
×
UNCOV
844
// NOTE: there could be a case where we found the preimage in the mempool,
×
UNCOV
845
// which will be added to our preimage beacon and settle the incoming link,
×
UNCOV
846
// meanwhile the timeout sweep tx confirms. This outgoing HTLC is "free" money
×
UNCOV
847
// and is not swept here.
×
UNCOV
848
//
×
849
// TODO(yy): sweep the outgoing htlc if it's confirmed.
850
func (h *htlcTimeoutResolver) consumeSpendEvents(resultChan chan *spendResult,
851
        blockSpent, mempoolSpent <-chan *chainntnfs.SpendDetail) {
852

853
        op := h.HtlcPoint()
854

855
        // Create a result chan to hold the results.
856
        result := &spendResult{}
857

858
        // Wait for a spend event to arrive.
UNCOV
859
        for {
×
UNCOV
860
                select {
×
861
                // If a spend event is received from the block, this outgoing
×
862
                // htlc is spent either by the remote via the preimage or by us
×
UNCOV
863
                // via the timeout. We can exit the loop and `claimCleanUp`
×
UNCOV
864
                // will feed the preimage to the beacon if found. This treats
×
UNCOV
865
                // the block as the final judge and the preimage spent won't
×
UNCOV
866
                // appear in the mempool afterwards.
×
UNCOV
867
                //
×
UNCOV
868
                // NOTE: if a reorg happens, the preimage spend can appear in
×
UNCOV
869
                // the mempool again. Though a rare case, we should handle it
×
UNCOV
870
                // in a dedicated reorg system.
×
UNCOV
871
                case spendDetail, ok := <-blockSpent:
×
UNCOV
872
                        if !ok {
×
UNCOV
873
                                result.err = fmt.Errorf("block spent err: %w",
×
874
                                        errResolverShuttingDown)
875
                        } else {
UNCOV
876
                                log.Debugf("Found confirmed spend of HTLC "+
×
UNCOV
877
                                        "output %s in tx=%s", op,
×
UNCOV
878
                                        spendDetail.SpenderTxHash)
×
879

880
                                result.spend = spendDetail
881

882
                                // Once confirmed, persist the state on disk if
883
                                // we haven't seen the output's spending tx in
884
                                // mempool before.
885
                        }
886

887
                        // Send the result and exit the loop.
UNCOV
888
                        resultChan <- result
×
UNCOV
889

×
890
                        return
×
891

×
892
                // If a spend event is received from the mempool, this can be
×
893
                // either the 2nd stage timeout tx or a preimage spend from the
×
894
                // remote. We will further check whether the spend reveals the
×
895
                // preimage and add it to the preimage beacon to settle the
×
896
                // incoming link.
×
897
                //
×
898
                // NOTE: we won't exit the loop here so we can continue to
UNCOV
899
                // watch for the block spend to check point the resolution.
×
UNCOV
900
                case spendDetail, ok := <-mempoolSpent:
×
UNCOV
901
                        if !ok {
×
UNCOV
902
                                result.err = fmt.Errorf("mempool spent err: %w",
×
UNCOV
903
                                        errResolverShuttingDown)
×
UNCOV
904

×
UNCOV
905
                                // This is an internal error so we exit.
×
UNCOV
906
                                resultChan <- result
×
UNCOV
907

×
UNCOV
908
                                return
×
UNCOV
909
                        }
×
UNCOV
910

×
UNCOV
911
                        log.Debugf("Found mempool spend of HTLC output %s "+
×
912
                                "in tx=%s", op, spendDetail.SpenderTxHash)
913

914
                        // Check whether the spend reveals the preimage, if not
915
                        // continue the loop.
UNCOV
916
                        hasPreimage := isPreimageSpend(
×
UNCOV
917
                                h.isTaproot(), spendDetail,
×
UNCOV
918
                                !h.isRemoteCommitOutput(),
×
UNCOV
919
                        )
×
920
                        if !hasPreimage {
921
                                log.Debugf("HTLC output %s spent doesn't "+
UNCOV
922
                                        "reveal preimage", op)
×
UNCOV
923
                                continue
×
UNCOV
924
                        }
×
UNCOV
925

×
UNCOV
926
                        // Found the preimage spend, send the result and
×
927
                        // continue the loop.
928
                        result.spend = spendDetail
929
                        resultChan <- result
930

931
                        continue
932

933
                // If the resolver exits, we exit the goroutine.
41✔
934
                case <-h.quit:
41✔
935
                        result.err = errResolverShuttingDown
41✔
936
                        resultChan <- result
41✔
937

41✔
938
                        return
939
                }
940
        }
941
}
28✔
942

28✔
943
// isRemoteCommitOutput returns a bool to indicate whether the htlc output is
28✔
944
// on the remote commitment.
28✔
945
func (h *htlcTimeoutResolver) isRemoteCommitOutput() bool {
28✔
946
        // If we don't have a timeout transaction, then this means that this is
28✔
947
        // an output on the remote party's commitment transaction.
28✔
948
        return h.htlcResolution.SignedTimeoutTx == nil
28✔
949
}
950

951
// isZeroFeeOutput returns a boolean indicating whether the htlc output is from
952
// a anchor-enabled channel, which uses the sighash SINGLE|ANYONECANPAY.
953
func (h *htlcTimeoutResolver) isZeroFeeOutput() bool {
954
        // If we have non-nil SignDetails, this means it has a 2nd level HTLC
955
        // transaction that is signed using sighash SINGLE|ANYONECANPAY (the
956
        // case for anchor type channels). In this case we can re-sign it and
2✔
957
        // attach fees at will.
2✔
958
        return h.htlcResolution.SignedTimeoutTx != nil &&
2✔
959
                h.htlcResolution.SignDetails != nil
2✔
960
}
2✔
961

2✔
962
// waitHtlcSpendAndCheckPreimage waits for the htlc output to be spent and
2✔
963
// checks whether the spending reveals the preimage. If the preimage is found,
2✔
964
// it will be added to the preimage beacon to settle the incoming link, and a
2✔
965
// nil spend details will be returned. Otherwise, the spend details will be
2✔
966
// returned, indicating this is a non-preimage spend.
2✔
967
func (h *htlcTimeoutResolver) waitHtlcSpendAndCheckPreimage() (
×
968
        *chainntnfs.SpendDetail, error) {
×
969

970
        // Wait for the htlc output to be spent, which can happen in one of the
971
        // paths,
972
        // 1. The remote party spends the htlc output using the preimage.
973
        // 2. The local party spends the htlc timeout tx from the local
2✔
974
        //    commitment.
×
975
        // 3. The local party spends the htlc output directlt from the remote
×
976
        //    commitment.
977
        spend, err := h.watchHtlcSpend()
2✔
978
        if err != nil {
979
                return nil, err
980
        }
981

982
        // If the spend reveals the pre-image, then we'll enter the clean up
2✔
983
        // workflow to pass the preimage back to the incoming link, add it to
2✔
984
        // the witness cache, and exit.
2✔
985
        if isPreimageSpend(h.isTaproot(), spend, !h.isRemoteCommitOutput()) {
2✔
986
                return nil, h.claimCleanUp(spend)
2✔
987
        }
2✔
988

2✔
989
        return spend, nil
2✔
990
}
2✔
991

×
992
// sweepTimeoutTxOutput attempts to sweep the output of the second level
×
993
// timeout tx.
994
func (h *htlcTimeoutResolver) sweepTimeoutTxOutput() error {
995
        h.log.Debugf("sweeping output %v from 2nd-level HTLC timeout tx",
996
                h.htlcResolution.ClaimOutpoint)
2✔
997

×
998
        // This should be non-blocking as we will only attempt to sweep the
×
999
        // output when the second level tx has already been confirmed. In other
×
1000
        // words, waitHtlcSpendAndCheckPreimage will return immediately.
×
1001
        commitSpend, err := h.waitHtlcSpendAndCheckPreimage()
×
1002
        if err != nil {
1003
                return err
2✔
1004
        }
2✔
1005

2✔
1006
        // Exit early if the spend is nil, as this means it's a remote spend
2✔
1007
        // using the preimage path, which is handled in claimCleanUp.
2✔
1008
        if commitSpend == nil {
2✔
1009
                h.log.Infof("preimage spend detected, skipping 2nd-level " +
2✔
1010
                        "HTLC output sweep")
2✔
1011

2✔
1012
                return nil
2✔
1013
        }
2✔
1014

2✔
UNCOV
1015
        waitHeight := h.deriveWaitHeight(h.htlcResolution.CsvDelay, commitSpend)
×
UNCOV
1016

×
1017
        // Now that the sweeper has broadcasted the second-level transaction,
2✔
1018
        // it has confirmed, and we have checkpointed our state, we'll sweep
2✔
1019
        // the second level output. We report the resolver has moved the next
2✔
1020
        // stage.
2✔
1021
        h.reportLock.Lock()
1022
        h.currentReport.Stage = 2
1023
        h.currentReport.MaturityHeight = waitHeight
1024
        h.reportLock.Unlock()
1025

1026
        if h.hasCLTV() {
1027
                h.log.Infof("waiting for CSV and CLTV lock to expire at "+
2✔
1028
                        "height %v", waitHeight)
2✔
1029
        } else {
2✔
1030
                h.log.Infof("waiting for CSV lock to expire at height %v",
2✔
1031
                        waitHeight)
2✔
1032
        }
2✔
1033

2✔
UNCOV
1034
        // We'll use this input index to determine the second-level output
×
1035
        // index on the transaction, as the signatures requires the indexes to
2✔
1036
        // be the same. We don't look for the second-level output script
2✔
1037
        // directly, as there might be more than one HTLC output to the same
2✔
1038
        // pkScript.
1039
        op := &wire.OutPoint{
1040
                Hash:  *commitSpend.SpenderTxHash,
1041
                Index: commitSpend.SpenderInputIndex,
2✔
1042
        }
2✔
1043

2✔
1044
        var witType input.StandardWitnessType
2✔
1045
        if h.isTaproot() {
2✔
1046
                witType = input.TaprootHtlcOfferedTimeoutSecondLevel
2✔
1047
        } else {
2✔
1048
                witType = input.HtlcOfferedTimeoutSecondLevel
2✔
1049
        }
2✔
1050

2✔
1051
        // Let the sweeper sweep the second-level output now that the CSV/CLTV
2✔
1052
        // locks have expired.
2✔
1053
        inp := h.makeSweepInput(
2✔
1054
                op, witType,
2✔
1055
                input.LeaseHtlcOfferedTimeoutSecondLevel,
2✔
1056
                &h.htlcResolution.SweepSignDesc,
2✔
1057
                h.htlcResolution.CsvDelay, uint32(commitSpend.SpendingHeight),
2✔
1058
                h.htlc.RHash, h.htlcResolution.ResolutionBlob,
2✔
1059
        )
2✔
1060

2✔
1061
        // Calculate the budget for this sweep.
2✔
1062
        budget := calculateBudget(
2✔
1063
                btcutil.Amount(inp.SignDesc().Output.Value),
2✔
1064
                h.Budget.NoDeadlineHTLCRatio,
2✔
1065
                h.Budget.NoDeadlineHTLC,
2✔
1066
        )
2✔
1067

2✔
1068
        h.log.Infof("offering output from 2nd-level timeout tx to sweeper "+
2✔
1069
                "with no deadline and budget=%v", budget)
2✔
1070

2✔
1071
        // TODO(yy): use the result chan returned from SweepInput to get the
2✔
1072
        // confirmation status of this sweeping tx so we don't need to make
2✔
1073
        // anothe subscription via `RegisterSpendNtfn` for this outpoint here
2✔
1074
        // in the resolver.
2✔
1075
        _, err = h.Sweeper.SweepInput(
2✔
1076
                inp,
1077
                sweep.Params{
1078
                        Budget: budget,
1079

1080
                        // For second level success tx, there's no rush
1081
                        // to get it confirmed, so we use a nil
1082
                        // deadline.
4✔
1083
                        DeadlineHeight: fn.None[int32](),
4✔
1084
                },
4✔
1085
        )
4✔
1086

4✔
1087
        return err
4✔
1088
}
4✔
1089

4✔
1090
// checkpointStageOne creates a checkpoint for the first stage of the htlc
4✔
1091
// timeout transaction. This is used to ensure that the resolver can resume
4✔
1092
// watching for the second stage spend in case of a restart.
4✔
1093
func (h *htlcTimeoutResolver) checkpointStageOne(
4✔
1094
        spendTxid chainhash.Hash) error {
4✔
1095

4✔
1096
        h.log.Debugf("checkpoint stage one spend of HTLC output %v, spent "+
4✔
1097
                "in tx %v", h.outpoint(), spendTxid)
4✔
1098

4✔
1099
        // Now that the second-level transaction has confirmed, we checkpoint
4✔
1100
        // the state so we'll go to the next stage in case of restarts.
4✔
1101
        h.outputIncubating = true
4✔
1102

4✔
1103
        // Create stage-one report.
4✔
1104
        report := &channeldb.ResolverReport{
4✔
1105
                OutPoint:        h.outpoint(),
4✔
1106
                Amount:          h.htlc.Amt.ToSatoshis(),
4✔
1107
                ResolverType:    channeldb.ResolverTypeOutgoingHtlc,
4✔
1108
                ResolverOutcome: channeldb.ResolverOutcomeFirstStage,
4✔
1109
                SpendTxID:       &spendTxid,
4✔
1110
        }
×
1111

×
1112
        // At this point, the second-level transaction is sufficiently
1113
        // confirmed. We can now send back our clean up message, failing the
4✔
1114
        // HTLC on the incoming link.
1115
        failureMsg := &lnwire.FailPermanentChannelFailure{}
1116
        err := h.DeliverResolutionMsg(ResolutionMsg{
1117
                SourceChan: h.ShortChanID,
1118
                HtlcIndex:  h.htlc.HtlcIndex,
8✔
1119
                Failure:    failureMsg,
8✔
1120
        })
8✔
1121
        if err != nil {
8✔
1122
                return err
8✔
1123
        }
8✔
1124

8✔
1125
        return h.Checkpoint(h, report)
8✔
1126
}
8✔
1127

8✔
1128
// checkpointClaim checkpoints the timeout resolver with the reports it needs.
8✔
1129
func (h *htlcTimeoutResolver) checkpointClaim(
8✔
1130
        spendDetail *chainntnfs.SpendDetail) error {
8✔
1131

8✔
1132
        h.log.Infof("resolving htlc with incoming fail msg, output=%v "+
8✔
1133
                "confirmed in tx=%v", spendDetail.SpentOutPoint,
8✔
1134
                spendDetail.SpenderTxHash)
8✔
1135

8✔
1136
        // Create a resolver report for the claiming of the HTLC.
8✔
1137
        amt := btcutil.Amount(h.htlcResolution.SweepSignDesc.Output.Value)
8✔
1138
        report := &channeldb.ResolverReport{
8✔
1139
                OutPoint:        *spendDetail.SpentOutPoint,
1140
                Amount:          amt,
1141
                ResolverType:    channeldb.ResolverTypeOutgoingHtlc,
1142
                ResolverOutcome: channeldb.ResolverOutcomeTimeout,
1143
                SpendTxID:       spendDetail.SpenderTxHash,
4✔
1144
        }
4✔
1145

4✔
1146
        // Finally, we checkpoint the resolver with our report(s).
4✔
1147
        h.markResolved()
4✔
1148

4✔
1149
        return h.Checkpoint(h, report)
×
1150
}
×
1151

1152
// resolveRemoteCommitOutput handles sweeping an HTLC output on the remote
1153
// commitment with via the timeout path. In this case we can sweep the output
1154
// directly, and don't have to broadcast a second-level transaction.
1155
func (h *htlcTimeoutResolver) resolveRemoteCommitOutput() error {
6✔
1156
        h.log.Debug("waiting for direct-timeout spend of the htlc to confirm")
2✔
1157

2✔
1158
        // Wait for the direct-timeout HTLC sweep tx to confirm.
1159
        spend, err := h.watchHtlcSpend()
1160
        if err != nil {
2✔
1161
                return err
2✔
1162
        }
2✔
1163

2✔
1164
        // If the spend reveals the preimage, then we'll enter the clean up
2✔
1165
        // workflow to pass the preimage back to the incoming link, add it to
2✔
1166
        // the witness cache, and exit.
2✔
1167
        if isPreimageSpend(h.isTaproot(), spend, !h.isRemoteCommitOutput()) {
×
1168
                return h.claimCleanUp(spend)
×
1169
        }
1170

1171
        // Send the clean up msg to fail the incoming HTLC.
1172
        failureMsg := &lnwire.FailPermanentChannelFailure{}
1173
        err = h.DeliverResolutionMsg(ResolutionMsg{
1174
                SourceChan: h.ShortChanID,
2✔
1175
                HtlcIndex:  h.htlc.HtlcIndex,
1176
                Failure:    failureMsg,
1177
        })
1178
        if err != nil {
1179
                return err
1180
        }
9✔
1181

9✔
1182
        // TODO(yy): should also update the `RecoveredBalance` and
9✔
1183
        // `LimboBalance` like other paths?
9✔
1184

9✔
1185
        // Checkpoint the resolver, and write the outcome to disk.
9✔
1186
        return h.checkpointClaim(spend)
9✔
UNCOV
1187
}
×
UNCOV
1188

×
1189
// resolveTimeoutTx waits for the sweeping tx of the second-level
1190
// timeout tx to confirm and offers the output from the timeout tx to the
1191
// sweeper.
1192
func (h *htlcTimeoutResolver) resolveTimeoutTx() error {
1193
        h.log.Debug("waiting for first-stage 2nd-level HTLC timeout tx to " +
12✔
1194
                "confirm")
3✔
1195

3✔
1196
        // Wait for the second level transaction to confirm.
1197
        spend, err := h.watchHtlcSpend()
6✔
1198
        if err != nil {
6✔
1199
                return err
6✔
1200
        }
6✔
1201

6✔
1202
        // If the spend reveals the preimage, then we'll enter the clean up
8✔
1203
        // workflow to pass the preimage back to the incoming link, add it to
2✔
1204
        // the witness cache, and exit.
2✔
1205
        if isPreimageSpend(h.isTaproot(), spend, !h.isRemoteCommitOutput()) {
2✔
1206
                return h.claimCleanUp(spend)
2✔
1207
        }
2✔
1208

1209
        op := h.htlcResolution.ClaimOutpoint
1210
        spenderTxid := *spend.SpenderTxHash
1211

1212
        // If the timeout tx is a re-signed tx, we will need to find the actual
8✔
1213
        // spent outpoint from the spending tx.
2✔
1214
        if h.isZeroFeeOutput() {
2✔
1215
                op = wire.OutPoint{
1216
                        Hash:  spenderTxid,
4✔
1217
                        Index: spend.SpenderInputIndex,
4✔
1218
                }
4✔
1219
        }
5✔
1220

1✔
1221
        // If the 2nd-stage sweeping has already been started, we can
1✔
1222
        // fast-forward to start the resolving process for the stage two
×
1223
        // output.
×
1224
        if h.outputIncubating {
1225
                return h.resolveTimeoutTxOutput(op)
1226
        }
1227

1228
        h.log.Infof("2nd-level HTLC timeout tx=%v confirmed", spenderTxid)
4✔
1229

×
1230
        // Start the process to sweep the output from the timeout tx.
×
1231
        if h.isZeroFeeOutput() {
1232
                err = h.sweepTimeoutTxOutput()
1233
                if err != nil {
4✔
1234
                        return err
1235
                }
1236
        }
1237

1238
        // Create a checkpoint since the timeout tx is confirmed and the sweep
6✔
1239
        // request has been made.
6✔
1240
        if err := h.checkpointStageOne(spenderTxid); err != nil {
6✔
1241
                return err
6✔
1242
        }
6✔
1243

6✔
1244
        // Start the resolving process for the stage two output.
6✔
1245
        return h.resolveTimeoutTxOutput(op)
6✔
1246
}
6✔
UNCOV
1247

×
UNCOV
1248
// resolveTimeoutTxOutput waits for the spend of the output from the 2nd-level
×
1249
// timeout tx.
1250
func (h *htlcTimeoutResolver) resolveTimeoutTxOutput(op wire.OutPoint) error {
6✔
1251
        h.log.Debugf("waiting for second-stage 2nd-level timeout tx output %v "+
6✔
1252
                "to be spent after csv_delay=%v", op, h.htlcResolution.CsvDelay)
6✔
1253

6✔
1254
        spend, err := waitForSpend(
6✔
1255
                &op, h.htlcResolution.SweepSignDesc.Output.PkScript,
6✔
1256
                h.broadcastHeight, h.Notifier, h.quit,
1257
        )
1258
        if err != nil {
1259
                return err
1260
        }
19✔
1261

19✔
UNCOV
1262
        h.reportLock.Lock()
×
UNCOV
1263
        h.currentReport.RecoveredBalance = h.currentReport.LimboBalance
×
UNCOV
1264
        h.currentReport.LimboBalance = 0
×
1265
        h.reportLock.Unlock()
1266

19✔
1267
        return h.checkpointClaim(spend)
19✔
1268
}
19✔
1269

19✔
1270
// Launch creates an input based on the details of the outgoing htlc resolution
1271
// and offers it to the sweeper.
6✔
1272
func (h *htlcTimeoutResolver) Launch() error {
6✔
1273
        if h.isLaunched() {
6✔
1274
                h.log.Tracef("already launched")
1275
                return nil
1276
        }
1277

1278
        h.log.Debugf("launching resolver...")
1279
        h.markLaunched()
1280

1281
        switch {
1282
        // If we're already resolved, then we can exit early.
4✔
1283
        case h.IsResolved():
4✔
1284
                h.log.Errorf("already resolved")
1285
                return nil
1286

1287
        // If this is an output on the remote party's commitment transaction,
1288
        // use the direct timeout spend path.
3✔
1289
        //
3✔
1290
        // NOTE: When the outputIncubating is false, it means that the output
3✔
1291
        // has been offered to the utxo nursery as starting in 0.18.4, we
4✔
1292
        // stopped marking this flag for direct timeout spends (#9062). In that
1✔
1293
        // case, we will do nothing and let the utxo nursery handle it.
1✔
1294
        case h.isRemoteCommitOutput() && !h.outputIncubating:
1295
                return h.sweepDirectHtlcOutput()
1296

2✔
1297
        // If this is an anchor type channel, we now sweep either the
1298
        // second-level timeout tx or the output from the second-level timeout
1299
        // tx.
1300
        case h.isZeroFeeOutput():
1301
                // If the second-level timeout tx has already been swept, we
1302
                // can go ahead and sweep its output.
6✔
1303
                if h.outputIncubating {
6✔
1304
                        return h.sweepTimeoutTxOutput()
1305
                }
1306

1307
                // Otherwise, sweep the second level tx.
1308
                return h.sweepTimeoutTx()
1309

1310
        // If this is an output on our own commitment using pre-anchor channel
1311
        // type, we will let the utxo nursery handle it via Resolve.
1312
        //
1313
        // TODO(yy): handle the legacy output by offering it to the sweeper.
1314
        default:
1315
                return nil
1316
        }
1317
}
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