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

lightningnetwork / lnd / 14919919715

09 May 2025 02:10AM UTC coverage: 58.581% (-0.009%) from 58.59%
14919919715

Pull #9797

github

web-flow
Merge 49fa69b5e into ee25c228e
Pull Request #9797: peer: introduce super priority send queue for pings+pongs

33 of 119 new or added lines in 1 file covered. (27.73%)

35 existing lines in 11 files now uncovered.

97442 of 166336 relevant lines covered (58.58%)

1.82 hits per line

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

77.33
/contractcourt/channel_arbitrator.go
1
package contractcourt
2

3
import (
4
        "bytes"
5
        "context"
6
        "errors"
7
        "fmt"
8
        "math"
9
        "sync"
10
        "sync/atomic"
11
        "time"
12

13
        "github.com/btcsuite/btcd/btcutil"
14
        "github.com/btcsuite/btcd/chaincfg/chainhash"
15
        "github.com/btcsuite/btcd/txscript"
16
        "github.com/btcsuite/btcd/wire"
17
        "github.com/lightningnetwork/lnd/chainio"
18
        "github.com/lightningnetwork/lnd/channeldb"
19
        "github.com/lightningnetwork/lnd/fn/v2"
20
        "github.com/lightningnetwork/lnd/graph/db/models"
21
        "github.com/lightningnetwork/lnd/htlcswitch/hop"
22
        "github.com/lightningnetwork/lnd/input"
23
        "github.com/lightningnetwork/lnd/invoices"
24
        "github.com/lightningnetwork/lnd/kvdb"
25
        "github.com/lightningnetwork/lnd/labels"
26
        "github.com/lightningnetwork/lnd/lntypes"
27
        "github.com/lightningnetwork/lnd/lnutils"
28
        "github.com/lightningnetwork/lnd/lnwallet"
29
        "github.com/lightningnetwork/lnd/lnwire"
30
        "github.com/lightningnetwork/lnd/sweep"
31
)
32

33
var (
34
        // errAlreadyForceClosed is an error returned when we attempt to force
35
        // close a channel that's already in the process of doing so.
36
        errAlreadyForceClosed = errors.New("channel is already in the " +
37
                "process of being force closed")
38
)
39

40
const (
41
        // arbitratorBlockBufferSize is the size of the buffer we give to each
42
        // channel arbitrator.
43
        arbitratorBlockBufferSize = 20
44

45
        // AnchorOutputValue is the output value for the anchor output of an
46
        // anchor channel.
47
        // See BOLT 03 for more details:
48
        // https://github.com/lightning/bolts/blob/master/03-transactions.md
49
        AnchorOutputValue = btcutil.Amount(330)
50
)
51

52
// WitnessSubscription represents an intent to be notified once new witnesses
53
// are discovered by various active contract resolvers. A contract resolver may
54
// use this to be notified of when it can satisfy an incoming contract after we
55
// discover the witness for an outgoing contract.
56
type WitnessSubscription struct {
57
        // WitnessUpdates is a channel that newly discovered witnesses will be
58
        // sent over.
59
        //
60
        // TODO(roasbeef): couple with WitnessType?
61
        WitnessUpdates <-chan lntypes.Preimage
62

63
        // CancelSubscription is a function closure that should be used by a
64
        // client to cancel the subscription once they are no longer interested
65
        // in receiving new updates.
66
        CancelSubscription func()
67
}
68

69
// WitnessBeacon is a global beacon of witnesses. Contract resolvers will use
70
// this interface to lookup witnesses (preimages typically) of contracts
71
// they're trying to resolve, add new preimages they resolve, and finally
72
// receive new updates each new time a preimage is discovered.
73
//
74
// TODO(roasbeef): need to delete the pre-images once we've used them
75
// and have been sufficiently confirmed?
76
type WitnessBeacon interface {
77
        // SubscribeUpdates returns a channel that will be sent upon *each* time
78
        // a new preimage is discovered.
79
        SubscribeUpdates(chanID lnwire.ShortChannelID, htlc *channeldb.HTLC,
80
                payload *hop.Payload,
81
                nextHopOnionBlob []byte) (*WitnessSubscription, error)
82

83
        // LookupPreimage attempts to lookup a preimage in the global cache.
84
        // True is returned for the second argument if the preimage is found.
85
        LookupPreimage(payhash lntypes.Hash) (lntypes.Preimage, bool)
86

87
        // AddPreimages adds a batch of newly discovered preimages to the global
88
        // cache, and also signals any subscribers of the newly discovered
89
        // witness.
90
        AddPreimages(preimages ...lntypes.Preimage) error
91
}
92

93
// ArbChannel is an abstraction that allows the channel arbitrator to interact
94
// with an open channel.
95
type ArbChannel interface {
96
        // ForceCloseChan should force close the contract that this attendant
97
        // is watching over. We'll use this when we decide that we need to go
98
        // to chain. It should in addition tell the switch to remove the
99
        // corresponding link, such that we won't accept any new updates. The
100
        // returned summary contains all items needed to eventually resolve all
101
        // outputs on chain.
102
        ForceCloseChan() (*wire.MsgTx, error)
103

104
        // NewAnchorResolutions returns the anchor resolutions for currently
105
        // valid commitment transactions.
106
        NewAnchorResolutions() (*lnwallet.AnchorResolutions, error)
107
}
108

109
// ChannelArbitratorConfig contains all the functionality that the
110
// ChannelArbitrator needs in order to properly arbitrate any contract dispute
111
// on chain.
112
type ChannelArbitratorConfig struct {
113
        // ChanPoint is the channel point that uniquely identifies this
114
        // channel.
115
        ChanPoint wire.OutPoint
116

117
        // Channel is the full channel data structure. For legacy channels, this
118
        // field may not always be set after a restart.
119
        Channel ArbChannel
120

121
        // ShortChanID describes the exact location of the channel within the
122
        // chain. We'll use this to address any messages that we need to send
123
        // to the switch during contract resolution.
124
        ShortChanID lnwire.ShortChannelID
125

126
        // ChainEvents is an active subscription to the chain watcher for this
127
        // channel to be notified of any on-chain activity related to this
128
        // channel.
129
        ChainEvents *ChainEventSubscription
130

131
        // MarkCommitmentBroadcasted should mark the channel as the commitment
132
        // being broadcast, and we are waiting for the commitment to confirm.
133
        MarkCommitmentBroadcasted func(*wire.MsgTx, lntypes.ChannelParty) error
134

135
        // MarkChannelClosed marks the channel closed in the database, with the
136
        // passed close summary. After this method successfully returns we can
137
        // no longer expect to receive chain events for this channel, and must
138
        // be able to recover from a failure without getting the close event
139
        // again. It takes an optional channel status which will update the
140
        // channel status in the record that we keep of historical channels.
141
        MarkChannelClosed func(*channeldb.ChannelCloseSummary,
142
                ...channeldb.ChannelStatus) error
143

144
        // IsPendingClose is a boolean indicating whether the channel is marked
145
        // as pending close in the database.
146
        IsPendingClose bool
147

148
        // ClosingHeight is the height at which the channel was closed. Note
149
        // that this value is only valid if IsPendingClose is true.
150
        ClosingHeight uint32
151

152
        // CloseType is the type of the close event in case IsPendingClose is
153
        // true. Otherwise this value is unset.
154
        CloseType channeldb.ClosureType
155

156
        // MarkChannelResolved is a function closure that serves to mark a
157
        // channel as "fully resolved". A channel itself can be considered
158
        // fully resolved once all active contracts have individually been
159
        // fully resolved.
160
        //
161
        // TODO(roasbeef): need RPC's to combine for pendingchannels RPC
162
        MarkChannelResolved func() error
163

164
        // PutResolverReport records a resolver report for the channel. If the
165
        // transaction provided is nil, the function should write the report
166
        // in a new transaction.
167
        PutResolverReport func(tx kvdb.RwTx,
168
                report *channeldb.ResolverReport) error
169

170
        // FetchHistoricalChannel retrieves the historical state of a channel.
171
        // This is mostly used to supplement the ContractResolvers with
172
        // additional information required for proper contract resolution.
173
        FetchHistoricalChannel func() (*channeldb.OpenChannel, error)
174

175
        // FindOutgoingHTLCDeadline returns the deadline in absolute block
176
        // height for the specified outgoing HTLC. For an outgoing HTLC, its
177
        // deadline is defined by the timeout height of its corresponding
178
        // incoming HTLC - this is the expiry height the that remote peer can
179
        // spend his/her outgoing HTLC via the timeout path.
180
        FindOutgoingHTLCDeadline func(htlc channeldb.HTLC) fn.Option[int32]
181

182
        ChainArbitratorConfig
183
}
184

185
// ReportOutputType describes the type of output that is being reported
186
// on.
187
type ReportOutputType uint8
188

189
const (
190
        // ReportOutputIncomingHtlc is an incoming hash time locked contract on
191
        // the commitment tx.
192
        ReportOutputIncomingHtlc ReportOutputType = iota
193

194
        // ReportOutputOutgoingHtlc is an outgoing hash time locked contract on
195
        // the commitment tx.
196
        ReportOutputOutgoingHtlc
197

198
        // ReportOutputUnencumbered is an uncontested output on the commitment
199
        // transaction paying to us directly.
200
        ReportOutputUnencumbered
201

202
        // ReportOutputAnchor is an anchor output on the commitment tx.
203
        ReportOutputAnchor
204
)
205

206
// ContractReport provides a summary of a commitment tx output.
207
type ContractReport struct {
208
        // Outpoint is the final output that will be swept back to the wallet.
209
        Outpoint wire.OutPoint
210

211
        // Type indicates the type of the reported output.
212
        Type ReportOutputType
213

214
        // Amount is the final value that will be swept in back to the wallet.
215
        Amount btcutil.Amount
216

217
        // MaturityHeight is the absolute block height that this output will
218
        // mature at.
219
        MaturityHeight uint32
220

221
        // Stage indicates whether the htlc is in the CLTV-timeout stage (1) or
222
        // the CSV-delay stage (2). A stage 1 htlc's maturity height will be set
223
        // to its expiry height, while a stage 2 htlc's maturity height will be
224
        // set to its confirmation height plus the maturity requirement.
225
        Stage uint32
226

227
        // LimboBalance is the total number of frozen coins within this
228
        // contract.
229
        LimboBalance btcutil.Amount
230

231
        // RecoveredBalance is the total value that has been successfully swept
232
        // back to the user's wallet.
233
        RecoveredBalance btcutil.Amount
234
}
235

236
// resolverReport creates a resolve report using some of the information in the
237
// contract report.
238
func (c *ContractReport) resolverReport(spendTx *chainhash.Hash,
239
        resolverType channeldb.ResolverType,
240
        outcome channeldb.ResolverOutcome) *channeldb.ResolverReport {
3✔
241

3✔
242
        return &channeldb.ResolverReport{
3✔
243
                OutPoint:        c.Outpoint,
3✔
244
                Amount:          c.Amount,
3✔
245
                ResolverType:    resolverType,
3✔
246
                ResolverOutcome: outcome,
3✔
247
                SpendTxID:       spendTx,
3✔
248
        }
3✔
249
}
3✔
250

251
// htlcSet represents the set of active HTLCs on a given commitment
252
// transaction.
253
type htlcSet struct {
254
        // incomingHTLCs is a map of all incoming HTLCs on the target
255
        // commitment transaction. We may potentially go onchain to claim the
256
        // funds sent to us within this set.
257
        incomingHTLCs map[uint64]channeldb.HTLC
258

259
        // outgoingHTLCs is a map of all outgoing HTLCs on the target
260
        // commitment transaction. We may potentially go onchain to reclaim the
261
        // funds that are currently in limbo.
262
        outgoingHTLCs map[uint64]channeldb.HTLC
263
}
264

265
// newHtlcSet constructs a new HTLC set from a slice of HTLC's.
266
func newHtlcSet(htlcs []channeldb.HTLC) htlcSet {
3✔
267
        outHTLCs := make(map[uint64]channeldb.HTLC)
3✔
268
        inHTLCs := make(map[uint64]channeldb.HTLC)
3✔
269
        for _, htlc := range htlcs {
6✔
270
                if htlc.Incoming {
6✔
271
                        inHTLCs[htlc.HtlcIndex] = htlc
3✔
272
                        continue
3✔
273
                }
274

275
                outHTLCs[htlc.HtlcIndex] = htlc
3✔
276
        }
277

278
        return htlcSet{
3✔
279
                incomingHTLCs: inHTLCs,
3✔
280
                outgoingHTLCs: outHTLCs,
3✔
281
        }
3✔
282
}
283

284
// HtlcSetKey is a two-tuple that uniquely identifies a set of HTLCs on a
285
// commitment transaction.
286
type HtlcSetKey struct {
287
        // IsRemote denotes if the HTLCs are on the remote commitment
288
        // transaction.
289
        IsRemote bool
290

291
        // IsPending denotes if the commitment transaction that HTLCS are on
292
        // are pending (the higher of two unrevoked commitments).
293
        IsPending bool
294
}
295

296
var (
297
        // LocalHtlcSet is the HtlcSetKey used for local commitments.
298
        LocalHtlcSet = HtlcSetKey{IsRemote: false, IsPending: false}
299

300
        // RemoteHtlcSet is the HtlcSetKey used for remote commitments.
301
        RemoteHtlcSet = HtlcSetKey{IsRemote: true, IsPending: false}
302

303
        // RemotePendingHtlcSet is the HtlcSetKey used for dangling remote
304
        // commitment transactions.
305
        RemotePendingHtlcSet = HtlcSetKey{IsRemote: true, IsPending: true}
306
)
307

308
// String returns a human readable string describing the target HtlcSetKey.
309
func (h HtlcSetKey) String() string {
3✔
310
        switch h {
3✔
311
        case LocalHtlcSet:
3✔
312
                return "LocalHtlcSet"
3✔
313
        case RemoteHtlcSet:
3✔
314
                return "RemoteHtlcSet"
3✔
315
        case RemotePendingHtlcSet:
3✔
316
                return "RemotePendingHtlcSet"
3✔
317
        default:
×
318
                return "unknown HtlcSetKey"
×
319
        }
320
}
321

322
// ChannelArbitrator is the on-chain arbitrator for a particular channel. The
323
// struct will keep in sync with the current set of HTLCs on the commitment
324
// transaction. The job of the attendant is to go on-chain to either settle or
325
// cancel an HTLC as necessary iff: an HTLC times out, or we known the
326
// pre-image to an HTLC, but it wasn't settled by the link off-chain. The
327
// ChannelArbitrator will factor in an expected confirmation delta when
328
// broadcasting to ensure that we avoid any possibility of race conditions, and
329
// sweep the output(s) without contest.
330
type ChannelArbitrator struct {
331
        started int32 // To be used atomically.
332
        stopped int32 // To be used atomically.
333

334
        // Embed the blockbeat consumer struct to get access to the method
335
        // `NotifyBlockProcessed` and the `BlockbeatChan`.
336
        chainio.BeatConsumer
337

338
        // startTimestamp is the time when this ChannelArbitrator was started.
339
        startTimestamp time.Time
340

341
        // log is a persistent log that the attendant will use to checkpoint
342
        // its next action, and the state of any unresolved contracts.
343
        log ArbitratorLog
344

345
        // activeHTLCs is the set of active incoming/outgoing HTLC's on all
346
        // currently valid commitment transactions.
347
        activeHTLCs map[HtlcSetKey]htlcSet
348

349
        // unmergedSet is used to update the activeHTLCs map in two callsites:
350
        // checkLocalChainActions and sweepAnchors. It contains the latest
351
        // updates from the link. It is not deleted from, its entries may be
352
        // replaced on subsequent calls to notifyContractUpdate.
353
        unmergedSet map[HtlcSetKey]htlcSet
354
        unmergedMtx sync.RWMutex
355

356
        // cfg contains all the functionality that the ChannelArbitrator requires
357
        // to do its duty.
358
        cfg ChannelArbitratorConfig
359

360
        // signalUpdates is a channel that any new live signals for the channel
361
        // we're watching over will be sent.
362
        signalUpdates chan *signalUpdateMsg
363

364
        // activeResolvers is a slice of any active resolvers. This is used to
365
        // be able to signal them for shutdown in the case that we shutdown.
366
        activeResolvers []ContractResolver
367

368
        // activeResolversLock prevents simultaneous read and write to the
369
        // resolvers slice.
370
        activeResolversLock sync.RWMutex
371

372
        // resolutionSignal is a channel that will be sent upon by contract
373
        // resolvers once their contract has been fully resolved. With each
374
        // send, we'll check to see if the contract is fully resolved.
375
        resolutionSignal chan struct{}
376

377
        // forceCloseReqs is a channel that requests to forcibly close the
378
        // contract will be sent over.
379
        forceCloseReqs chan *forceCloseReq
380

381
        // state is the current state of the arbitrator. This state is examined
382
        // upon start up to decide which actions to take.
383
        state ArbitratorState
384

385
        wg   sync.WaitGroup
386
        quit chan struct{}
387
}
388

389
// NewChannelArbitrator returns a new instance of a ChannelArbitrator backed by
390
// the passed config struct.
391
func NewChannelArbitrator(cfg ChannelArbitratorConfig,
392
        htlcSets map[HtlcSetKey]htlcSet, log ArbitratorLog) *ChannelArbitrator {
3✔
393

3✔
394
        // Create a new map for unmerged HTLC's as we will overwrite the values
3✔
395
        // and want to avoid modifying activeHTLCs directly. This soft copying
3✔
396
        // is done to ensure that activeHTLCs isn't reset as an empty map later
3✔
397
        // on.
3✔
398
        unmerged := make(map[HtlcSetKey]htlcSet)
3✔
399
        unmerged[LocalHtlcSet] = htlcSets[LocalHtlcSet]
3✔
400
        unmerged[RemoteHtlcSet] = htlcSets[RemoteHtlcSet]
3✔
401

3✔
402
        // If the pending set exists, write that as well.
3✔
403
        if _, ok := htlcSets[RemotePendingHtlcSet]; ok {
3✔
404
                unmerged[RemotePendingHtlcSet] = htlcSets[RemotePendingHtlcSet]
×
405
        }
×
406

407
        c := &ChannelArbitrator{
3✔
408
                log:              log,
3✔
409
                signalUpdates:    make(chan *signalUpdateMsg),
3✔
410
                resolutionSignal: make(chan struct{}),
3✔
411
                forceCloseReqs:   make(chan *forceCloseReq),
3✔
412
                activeHTLCs:      htlcSets,
3✔
413
                unmergedSet:      unmerged,
3✔
414
                cfg:              cfg,
3✔
415
                quit:             make(chan struct{}),
3✔
416
        }
3✔
417

3✔
418
        // Mount the block consumer.
3✔
419
        c.BeatConsumer = chainio.NewBeatConsumer(c.quit, c.Name())
3✔
420

3✔
421
        return c
3✔
422
}
423

424
// Compile-time check for the chainio.Consumer interface.
425
var _ chainio.Consumer = (*ChannelArbitrator)(nil)
426

427
// chanArbStartState contains the information from disk that we need to start
428
// up a channel arbitrator.
429
type chanArbStartState struct {
430
        currentState ArbitratorState
431
        commitSet    *CommitSet
432
}
433

434
// getStartState retrieves the information from disk that our channel arbitrator
435
// requires to start.
436
func (c *ChannelArbitrator) getStartState(tx kvdb.RTx) (*chanArbStartState,
437
        error) {
3✔
438

3✔
439
        // First, we'll read our last state from disk, so our internal state
3✔
440
        // machine can act accordingly.
3✔
441
        state, err := c.log.CurrentState(tx)
3✔
442
        if err != nil {
3✔
443
                return nil, err
×
444
        }
×
445

446
        // Next we'll fetch our confirmed commitment set. This will only exist
447
        // if the channel has been closed out on chain for modern nodes. For
448
        // older nodes, this won't be found at all, and will rely on the
449
        // existing written chain actions. Additionally, if this channel hasn't
450
        // logged any actions in the log, then this field won't be present.
451
        commitSet, err := c.log.FetchConfirmedCommitSet(tx)
3✔
452
        if err != nil && err != errNoCommitSet && err != errScopeBucketNoExist {
3✔
453
                return nil, err
×
454
        }
×
455

456
        return &chanArbStartState{
3✔
457
                currentState: state,
3✔
458
                commitSet:    commitSet,
3✔
459
        }, nil
3✔
460
}
461

462
// Start starts all the goroutines that the ChannelArbitrator needs to operate.
463
// If takes a start state, which will be looked up on disk if it is not
464
// provided.
465
func (c *ChannelArbitrator) Start(state *chanArbStartState,
466
        beat chainio.Blockbeat) error {
3✔
467

3✔
468
        if !atomic.CompareAndSwapInt32(&c.started, 0, 1) {
3✔
469
                return nil
×
470
        }
×
471
        c.startTimestamp = c.cfg.Clock.Now()
3✔
472

3✔
473
        // If the state passed in is nil, we look it up now.
3✔
474
        if state == nil {
6✔
475
                var err error
3✔
476
                state, err = c.getStartState(nil)
3✔
477
                if err != nil {
3✔
478
                        return err
×
479
                }
×
480
        }
481

482
        log.Tracef("Starting ChannelArbitrator(%v), htlc_set=%v, state=%v",
3✔
483
                c.cfg.ChanPoint, lnutils.SpewLogClosure(c.activeHTLCs),
3✔
484
                state.currentState)
3✔
485

3✔
486
        // Set our state from our starting state.
3✔
487
        c.state = state.currentState
3✔
488

3✔
489
        // Get the starting height.
3✔
490
        bestHeight := beat.Height()
3✔
491

3✔
492
        c.wg.Add(1)
3✔
493
        go c.channelAttendant(bestHeight, state.commitSet)
3✔
494

3✔
495
        return nil
3✔
496
}
497

498
// progressStateMachineAfterRestart attempts to progress the state machine
499
// after a restart. This makes sure that if the state transition failed, we
500
// will try to progress the state machine again. Moreover it will relaunch
501
// resolvers if the channel is still in the pending close state and has not
502
// been fully resolved yet.
503
func (c *ChannelArbitrator) progressStateMachineAfterRestart(bestHeight int32,
504
        commitSet *CommitSet) error {
3✔
505

3✔
506
        // If the channel has been marked pending close in the database, and we
3✔
507
        // haven't transitioned the state machine to StateContractClosed (or a
3✔
508
        // succeeding state), then a state transition most likely failed. We'll
3✔
509
        // try to recover from this by manually advancing the state by setting
3✔
510
        // the corresponding close trigger.
3✔
511
        trigger := chainTrigger
3✔
512
        triggerHeight := uint32(bestHeight)
3✔
513
        if c.cfg.IsPendingClose {
6✔
514
                switch c.state {
3✔
515
                case StateDefault:
×
516
                        fallthrough
×
517
                case StateBroadcastCommit:
×
518
                        fallthrough
×
519
                case StateCommitmentBroadcasted:
×
520
                        switch c.cfg.CloseType {
×
521

522
                        case channeldb.CooperativeClose:
×
523
                                trigger = coopCloseTrigger
×
524

525
                        case channeldb.BreachClose:
×
526
                                trigger = breachCloseTrigger
×
527

528
                        case channeldb.LocalForceClose:
×
529
                                trigger = localCloseTrigger
×
530

531
                        case channeldb.RemoteForceClose:
×
532
                                trigger = remoteCloseTrigger
×
533
                        }
534

535
                        log.Warnf("ChannelArbitrator(%v): detected stalled "+
×
536
                                "state=%v for closed channel",
×
537
                                c.cfg.ChanPoint, c.state)
×
538
                }
539

540
                triggerHeight = c.cfg.ClosingHeight
3✔
541
        }
542

543
        log.Infof("ChannelArbitrator(%v): starting state=%v, trigger=%v, "+
3✔
544
                "triggerHeight=%v", c.cfg.ChanPoint, c.state, trigger,
3✔
545
                triggerHeight)
3✔
546

3✔
547
        // We'll now attempt to advance our state forward based on the current
3✔
548
        // on-chain state, and our set of active contracts.
3✔
549
        startingState := c.state
3✔
550
        nextState, _, err := c.advanceState(
3✔
551
                triggerHeight, trigger, commitSet,
3✔
552
        )
3✔
553
        if err != nil {
3✔
554
                switch err {
×
555

556
                // If we detect that we tried to fetch resolutions, but failed,
557
                // this channel was marked closed in the database before
558
                // resolutions successfully written. In this case there is not
559
                // much we can do, so we don't return the error.
560
                case errScopeBucketNoExist:
×
561
                        fallthrough
×
562
                case errNoResolutions:
×
563
                        log.Warnf("ChannelArbitrator(%v): detected closed"+
×
564
                                "channel with no contract resolutions written.",
×
565
                                c.cfg.ChanPoint)
×
566

567
                default:
×
568
                        return err
×
569
                }
570
        }
571

572
        // If we start and ended at the awaiting full resolution state, then
573
        // we'll relaunch our set of unresolved contracts.
574
        if startingState == StateWaitingFullResolution &&
3✔
575
                nextState == StateWaitingFullResolution {
6✔
576

3✔
577
                // In order to relaunch the resolvers, we'll need to fetch the
3✔
578
                // set of HTLCs that were present in the commitment transaction
3✔
579
                // at the time it was confirmed. commitSet.ConfCommitKey can't
3✔
580
                // be nil at this point since we're in
3✔
581
                // StateWaitingFullResolution. We can only be in
3✔
582
                // StateWaitingFullResolution after we've transitioned from
3✔
583
                // StateContractClosed which can only be triggered by the local
3✔
584
                // or remote close trigger. This trigger is only fired when we
3✔
585
                // receive a chain event from the chain watcher that the
3✔
586
                // commitment has been confirmed on chain, and before we
3✔
587
                // advance our state step, we call InsertConfirmedCommitSet.
3✔
588
                err := c.relaunchResolvers(commitSet, triggerHeight)
3✔
589
                if err != nil {
3✔
590
                        return err
×
591
                }
×
592
        }
593

594
        return nil
3✔
595
}
596

597
// maybeAugmentTaprootResolvers will update the contract resolution information
598
// for taproot channels. This ensures that all the resolvers have the latest
599
// resolution, which may also include data such as the control block and tap
600
// tweaks.
601
func maybeAugmentTaprootResolvers(chanType channeldb.ChannelType,
602
        resolver ContractResolver,
603
        contractResolutions *ContractResolutions) {
3✔
604

3✔
605
        if !chanType.IsTaproot() {
6✔
606
                return
3✔
607
        }
3✔
608

609
        // The on disk resolutions contains all the ctrl block
610
        // information, so we'll set that now for the relevant
611
        // resolvers.
612
        switch r := resolver.(type) {
3✔
613
        case *commitSweepResolver:
2✔
614
                if contractResolutions.CommitResolution != nil {
4✔
615
                        //nolint:ll
2✔
616
                        r.commitResolution = *contractResolutions.CommitResolution
2✔
617
                }
2✔
618
        case *htlcOutgoingContestResolver:
3✔
619
                //nolint:ll
3✔
620
                htlcResolutions := contractResolutions.HtlcResolutions.OutgoingHTLCs
3✔
621
                for _, htlcRes := range htlcResolutions {
6✔
622
                        htlcRes := htlcRes
3✔
623

3✔
624
                        if r.htlcResolution.ClaimOutpoint ==
3✔
625
                                htlcRes.ClaimOutpoint {
6✔
626

3✔
627
                                r.htlcResolution = htlcRes
3✔
628
                        }
3✔
629
                }
630

631
        case *htlcTimeoutResolver:
2✔
632
                //nolint:ll
2✔
633
                htlcResolutions := contractResolutions.HtlcResolutions.OutgoingHTLCs
2✔
634
                for _, htlcRes := range htlcResolutions {
4✔
635
                        htlcRes := htlcRes
2✔
636

2✔
637
                        if r.htlcResolution.ClaimOutpoint ==
2✔
638
                                htlcRes.ClaimOutpoint {
4✔
639

2✔
640
                                r.htlcResolution = htlcRes
2✔
641
                        }
2✔
642
                }
643

644
        case *htlcIncomingContestResolver:
3✔
645
                //nolint:ll
3✔
646
                htlcResolutions := contractResolutions.HtlcResolutions.IncomingHTLCs
3✔
647
                for _, htlcRes := range htlcResolutions {
6✔
648
                        htlcRes := htlcRes
3✔
649

3✔
650
                        if r.htlcResolution.ClaimOutpoint ==
3✔
651
                                htlcRes.ClaimOutpoint {
6✔
652

3✔
653
                                r.htlcResolution = htlcRes
3✔
654
                        }
3✔
655
                }
656
        case *htlcSuccessResolver:
×
657
                //nolint:ll
×
658
                htlcResolutions := contractResolutions.HtlcResolutions.IncomingHTLCs
×
659
                for _, htlcRes := range htlcResolutions {
×
660
                        htlcRes := htlcRes
×
661

×
662
                        if r.htlcResolution.ClaimOutpoint ==
×
663
                                htlcRes.ClaimOutpoint {
×
664

×
665
                                r.htlcResolution = htlcRes
×
666
                        }
×
667
                }
668
        }
669
}
670

671
// relauchResolvers relaunches the set of resolvers for unresolved contracts in
672
// order to provide them with information that's not immediately available upon
673
// starting the ChannelArbitrator. This information should ideally be stored in
674
// the database, so this only serves as a intermediate work-around to prevent a
675
// migration.
676
func (c *ChannelArbitrator) relaunchResolvers(commitSet *CommitSet,
677
        heightHint uint32) error {
3✔
678

3✔
679
        // We'll now query our log to see if there are any active unresolved
3✔
680
        // contracts. If this is the case, then we'll relaunch all contract
3✔
681
        // resolvers.
3✔
682
        unresolvedContracts, err := c.log.FetchUnresolvedContracts()
3✔
683
        if err != nil {
3✔
684
                return err
×
685
        }
×
686

687
        // Retrieve the commitment tx hash from the log.
688
        contractResolutions, err := c.log.FetchContractResolutions()
3✔
689
        if err != nil {
3✔
690
                log.Errorf("unable to fetch contract resolutions: %v",
×
691
                        err)
×
692
                return err
×
693
        }
×
694
        commitHash := contractResolutions.CommitHash
3✔
695

3✔
696
        // In prior versions of lnd, the information needed to supplement the
3✔
697
        // resolvers (in most cases, the full amount of the HTLC) was found in
3✔
698
        // the chain action map, which is now deprecated.  As a result, if the
3✔
699
        // commitSet is nil (an older node with unresolved HTLCs at time of
3✔
700
        // upgrade), then we'll use the chain action information in place. The
3✔
701
        // chain actions may exclude some information, but we cannot recover it
3✔
702
        // for these older nodes at the moment.
3✔
703
        var confirmedHTLCs []channeldb.HTLC
3✔
704
        if commitSet != nil && commitSet.ConfCommitKey.IsSome() {
6✔
705
                confCommitKey, err := commitSet.ConfCommitKey.UnwrapOrErr(
3✔
706
                        fmt.Errorf("no commitKey available"),
3✔
707
                )
3✔
708
                if err != nil {
3✔
709
                        return err
×
710
                }
×
711
                confirmedHTLCs = commitSet.HtlcSets[confCommitKey]
3✔
712
        } else {
×
713
                chainActions, err := c.log.FetchChainActions()
×
714
                if err != nil {
×
715
                        log.Errorf("unable to fetch chain actions: %v", err)
×
716
                        return err
×
717
                }
×
718
                for _, htlcs := range chainActions {
×
719
                        confirmedHTLCs = append(confirmedHTLCs, htlcs...)
×
720
                }
×
721
        }
722

723
        // Reconstruct the htlc outpoints and data from the chain action log.
724
        // The purpose of the constructed htlc map is to supplement to
725
        // resolvers restored from database with extra data. Ideally this data
726
        // is stored as part of the resolver in the log. This is a workaround
727
        // to prevent a db migration. We use all available htlc sets here in
728
        // order to ensure we have complete coverage.
729
        htlcMap := make(map[wire.OutPoint]*channeldb.HTLC)
3✔
730
        for _, htlc := range confirmedHTLCs {
6✔
731
                htlc := htlc
3✔
732
                outpoint := wire.OutPoint{
3✔
733
                        Hash:  commitHash,
3✔
734
                        Index: uint32(htlc.OutputIndex),
3✔
735
                }
3✔
736
                htlcMap[outpoint] = &htlc
3✔
737
        }
3✔
738

739
        // We'll also fetch the historical state of this channel, as it should
740
        // have been marked as closed by now, and supplement it to each resolver
741
        // such that we can properly resolve our pending contracts.
742
        var chanState *channeldb.OpenChannel
3✔
743
        chanState, err = c.cfg.FetchHistoricalChannel()
3✔
744
        switch {
3✔
745
        // If we don't find this channel, then it may be the case that it
746
        // was closed before we started to retain the final state
747
        // information for open channels.
748
        case err == channeldb.ErrNoHistoricalBucket:
×
749
                fallthrough
×
750
        case err == channeldb.ErrChannelNotFound:
×
751
                log.Warnf("ChannelArbitrator(%v): unable to fetch historical "+
×
752
                        "state", c.cfg.ChanPoint)
×
753

754
        case err != nil:
×
755
                return err
×
756
        }
757

758
        log.Infof("ChannelArbitrator(%v): relaunching %v contract "+
3✔
759
                "resolvers", c.cfg.ChanPoint, len(unresolvedContracts))
3✔
760

3✔
761
        for i := range unresolvedContracts {
6✔
762
                resolver := unresolvedContracts[i]
3✔
763

3✔
764
                if chanState != nil {
6✔
765
                        resolver.SupplementState(chanState)
3✔
766

3✔
767
                        // For taproot channels, we'll need to also make sure
3✔
768
                        // the control block information was set properly.
3✔
769
                        maybeAugmentTaprootResolvers(
3✔
770
                                chanState.ChanType, resolver,
3✔
771
                                contractResolutions,
3✔
772
                        )
3✔
773
                }
3✔
774

775
                unresolvedContracts[i] = resolver
3✔
776

3✔
777
                htlcResolver, ok := resolver.(htlcContractResolver)
3✔
778
                if !ok {
6✔
779
                        continue
3✔
780
                }
781

782
                htlcPoint := htlcResolver.HtlcPoint()
3✔
783
                htlc, ok := htlcMap[htlcPoint]
3✔
784
                if !ok {
3✔
785
                        return fmt.Errorf(
×
786
                                "htlc resolver %T unavailable", resolver,
×
787
                        )
×
788
                }
×
789

790
                htlcResolver.Supplement(*htlc)
3✔
791

3✔
792
                // If this is an outgoing HTLC, we will also need to supplement
3✔
793
                // the resolver with the expiry block height of its
3✔
794
                // corresponding incoming HTLC.
3✔
795
                if !htlc.Incoming {
6✔
796
                        deadline := c.cfg.FindOutgoingHTLCDeadline(*htlc)
3✔
797
                        htlcResolver.SupplementDeadline(deadline)
3✔
798
                }
3✔
799
        }
800

801
        // The anchor resolver is stateless and can always be re-instantiated.
802
        if contractResolutions.AnchorResolution != nil {
6✔
803
                anchorResolver := newAnchorResolver(
3✔
804
                        contractResolutions.AnchorResolution.AnchorSignDescriptor,
3✔
805
                        contractResolutions.AnchorResolution.CommitAnchor,
3✔
806
                        heightHint, c.cfg.ChanPoint,
3✔
807
                        ResolverConfig{
3✔
808
                                ChannelArbitratorConfig: c.cfg,
3✔
809
                        },
3✔
810
                )
3✔
811

3✔
812
                anchorResolver.SupplementState(chanState)
3✔
813

3✔
814
                unresolvedContracts = append(unresolvedContracts, anchorResolver)
3✔
815

3✔
816
                // TODO(roasbeef): this isn't re-launched?
3✔
817
        }
3✔
818

819
        c.resolveContracts(unresolvedContracts)
3✔
820

3✔
821
        return nil
3✔
822
}
823

824
// Report returns htlc reports for the active resolvers.
825
func (c *ChannelArbitrator) Report() []*ContractReport {
3✔
826
        c.activeResolversLock.RLock()
3✔
827
        defer c.activeResolversLock.RUnlock()
3✔
828

3✔
829
        var reports []*ContractReport
3✔
830
        for _, resolver := range c.activeResolvers {
6✔
831
                r, ok := resolver.(reportingContractResolver)
3✔
832
                if !ok {
3✔
833
                        continue
×
834
                }
835

836
                report := r.report()
3✔
837
                if report == nil {
6✔
838
                        continue
3✔
839
                }
840

841
                reports = append(reports, report)
3✔
842
        }
843

844
        return reports
3✔
845
}
846

847
// Stop signals the ChannelArbitrator for a graceful shutdown.
848
func (c *ChannelArbitrator) Stop() error {
3✔
849
        if !atomic.CompareAndSwapInt32(&c.stopped, 0, 1) {
3✔
850
                return nil
×
851
        }
×
852

853
        log.Debugf("Stopping ChannelArbitrator(%v)", c.cfg.ChanPoint)
3✔
854

3✔
855
        if c.cfg.ChainEvents.Cancel != nil {
6✔
856
                go c.cfg.ChainEvents.Cancel()
3✔
857
        }
3✔
858

859
        c.activeResolversLock.RLock()
3✔
860
        for _, activeResolver := range c.activeResolvers {
6✔
861
                activeResolver.Stop()
3✔
862
        }
3✔
863
        c.activeResolversLock.RUnlock()
3✔
864

3✔
865
        close(c.quit)
3✔
866
        c.wg.Wait()
3✔
867

3✔
868
        return nil
3✔
869
}
870

871
// transitionTrigger is an enum that denotes exactly *why* a state transition
872
// was initiated. This is useful as depending on the initial trigger, we may
873
// skip certain states as those actions are expected to have already taken
874
// place as a result of the external trigger.
875
type transitionTrigger uint8
876

877
const (
878
        // chainTrigger is a transition trigger that has been attempted due to
879
        // changing on-chain conditions such as a block which times out HTLC's
880
        // being attached.
881
        chainTrigger transitionTrigger = iota
882

883
        // userTrigger is a transition trigger driven by user action. Examples
884
        // of such a trigger include a user requesting a force closure of the
885
        // channel.
886
        userTrigger
887

888
        // remoteCloseTrigger is a transition trigger driven by the remote
889
        // peer's commitment being confirmed.
890
        remoteCloseTrigger
891

892
        // localCloseTrigger is a transition trigger driven by our commitment
893
        // being confirmed.
894
        localCloseTrigger
895

896
        // coopCloseTrigger is a transition trigger driven by a cooperative
897
        // close transaction being confirmed.
898
        coopCloseTrigger
899

900
        // breachCloseTrigger is a transition trigger driven by a remote breach
901
        // being confirmed. In this case the channel arbitrator will wait for
902
        // the BreachArbitrator to finish and then clean up gracefully.
903
        breachCloseTrigger
904
)
905

906
// String returns a human readable string describing the passed
907
// transitionTrigger.
908
func (t transitionTrigger) String() string {
3✔
909
        switch t {
3✔
910
        case chainTrigger:
3✔
911
                return "chainTrigger"
3✔
912

913
        case remoteCloseTrigger:
3✔
914
                return "remoteCloseTrigger"
3✔
915

916
        case userTrigger:
3✔
917
                return "userTrigger"
3✔
918

919
        case localCloseTrigger:
3✔
920
                return "localCloseTrigger"
3✔
921

922
        case coopCloseTrigger:
3✔
923
                return "coopCloseTrigger"
3✔
924

925
        case breachCloseTrigger:
3✔
926
                return "breachCloseTrigger"
3✔
927

928
        default:
×
929
                return "unknown trigger"
×
930
        }
931
}
932

933
// stateStep is a help method that examines our internal state, and attempts
934
// the appropriate state transition if necessary. The next state we transition
935
// to is returned, Additionally, if the next transition results in a commitment
936
// broadcast, the commitment transaction itself is returned.
937
func (c *ChannelArbitrator) stateStep(
938
        triggerHeight uint32, trigger transitionTrigger,
939
        confCommitSet *CommitSet) (ArbitratorState, *wire.MsgTx, error) {
3✔
940

3✔
941
        var (
3✔
942
                nextState ArbitratorState
3✔
943
                closeTx   *wire.MsgTx
3✔
944
        )
3✔
945
        switch c.state {
3✔
946

947
        // If we're in the default state, then we'll check our set of actions
948
        // to see if while we were down, conditions have changed.
949
        case StateDefault:
3✔
950
                log.Debugf("ChannelArbitrator(%v): examining active HTLCs in "+
3✔
951
                        "block %v, confCommitSet: %v", c.cfg.ChanPoint,
3✔
952
                        triggerHeight, lnutils.LogClosure(confCommitSet.String))
3✔
953

3✔
954
                // As a new block has been connected to the end of the main
3✔
955
                // chain, we'll check to see if we need to make any on-chain
3✔
956
                // claims on behalf of the channel contract that we're
3✔
957
                // arbitrating for. If a commitment has confirmed, then we'll
3✔
958
                // use the set snapshot from the chain, otherwise we'll use our
3✔
959
                // current set.
3✔
960
                var (
3✔
961
                        chainActions ChainActionMap
3✔
962
                        err          error
3✔
963
                )
3✔
964

3✔
965
                // Normally if we force close the channel locally we will have
3✔
966
                // no confCommitSet. However when the remote commitment confirms
3✔
967
                // without us ever broadcasting our local commitment we need to
3✔
968
                // make sure we cancel all upstream HTLCs for outgoing dust
3✔
969
                // HTLCs as well hence we need to fetch the chain actions here
3✔
970
                // as well.
3✔
971
                if confCommitSet == nil {
6✔
972
                        // Update the set of activeHTLCs so
3✔
973
                        // checkLocalChainActions has an up-to-date view of the
3✔
974
                        // commitments.
3✔
975
                        c.updateActiveHTLCs()
3✔
976
                        htlcs := c.activeHTLCs
3✔
977
                        chainActions, err = c.checkLocalChainActions(
3✔
978
                                triggerHeight, trigger, htlcs, false,
3✔
979
                        )
3✔
980
                        if err != nil {
3✔
981
                                return StateDefault, nil, err
×
982
                        }
×
983
                } else {
3✔
984
                        chainActions, err = c.constructChainActions(
3✔
985
                                confCommitSet, triggerHeight, trigger,
3✔
986
                        )
3✔
987
                        if err != nil {
3✔
988
                                return StateDefault, nil, err
×
989
                        }
×
990
                }
991

992
                // If there are no actions to be made, then we'll remain in the
993
                // default state. If this isn't a self initiated event (we're
994
                // checking due to a chain update), then we'll exit now.
995
                if len(chainActions) == 0 && trigger == chainTrigger {
6✔
996
                        log.Debugf("ChannelArbitrator(%v): no actions for "+
3✔
997
                                "chain trigger, terminating", c.cfg.ChanPoint)
3✔
998

3✔
999
                        return StateDefault, closeTx, nil
3✔
1000
                }
3✔
1001

1002
                // Otherwise, we'll log that we checked the HTLC actions as the
1003
                // commitment transaction has already been broadcast.
1004
                log.Tracef("ChannelArbitrator(%v): logging chain_actions=%v",
3✔
1005
                        c.cfg.ChanPoint, lnutils.SpewLogClosure(chainActions))
3✔
1006

3✔
1007
                // Cancel upstream HTLCs for all outgoing dust HTLCs available
3✔
1008
                // either on the local or the remote/remote pending commitment
3✔
1009
                // transaction.
3✔
1010
                dustHTLCs := chainActions[HtlcFailDustAction]
3✔
1011
                if len(dustHTLCs) > 0 {
6✔
1012
                        log.Debugf("ChannelArbitrator(%v): canceling %v dust "+
3✔
1013
                                "HTLCs backwards", c.cfg.ChanPoint,
3✔
1014
                                len(dustHTLCs))
3✔
1015

3✔
1016
                        getIdx := func(htlc channeldb.HTLC) uint64 {
6✔
1017
                                return htlc.HtlcIndex
3✔
1018
                        }
3✔
1019
                        dustHTLCSet := fn.NewSet(fn.Map(dustHTLCs, getIdx)...)
3✔
1020
                        err = c.abandonForwards(dustHTLCSet)
3✔
1021
                        if err != nil {
3✔
1022
                                return StateError, closeTx, err
×
1023
                        }
×
1024
                }
1025

1026
                // Depending on the type of trigger, we'll either "tunnel"
1027
                // through to a farther state, or just proceed linearly to the
1028
                // next state.
1029
                switch trigger {
3✔
1030

1031
                // If this is a chain trigger, then we'll go straight to the
1032
                // next state, as we still need to broadcast the commitment
1033
                // transaction.
1034
                case chainTrigger:
3✔
1035
                        fallthrough
3✔
1036
                case userTrigger:
3✔
1037
                        nextState = StateBroadcastCommit
3✔
1038

1039
                // If the trigger is a cooperative close being confirmed, then
1040
                // we can go straight to StateFullyResolved, as there won't be
1041
                // any contracts to resolve.
1042
                case coopCloseTrigger:
3✔
1043
                        nextState = StateFullyResolved
3✔
1044

1045
                // Otherwise, if this state advance was triggered by a
1046
                // commitment being confirmed on chain, then we'll jump
1047
                // straight to the state where the contract has already been
1048
                // closed, and we will inspect the set of unresolved contracts.
1049
                case localCloseTrigger:
3✔
1050
                        log.Errorf("ChannelArbitrator(%v): unexpected local "+
3✔
1051
                                "commitment confirmed while in StateDefault",
3✔
1052
                                c.cfg.ChanPoint)
3✔
1053
                        fallthrough
3✔
1054
                case remoteCloseTrigger:
3✔
1055
                        nextState = StateContractClosed
3✔
1056

1057
                case breachCloseTrigger:
3✔
1058
                        nextContractState, err := c.checkLegacyBreach()
3✔
1059
                        if nextContractState == StateError {
3✔
1060
                                return nextContractState, nil, err
×
1061
                        }
×
1062

1063
                        nextState = nextContractState
3✔
1064
                }
1065

1066
        // If we're in this state, then we've decided to broadcast the
1067
        // commitment transaction. We enter this state either due to an outside
1068
        // sub-system, or because an on-chain action has been triggered.
1069
        case StateBroadcastCommit:
3✔
1070
                // Under normal operation, we can only enter
3✔
1071
                // StateBroadcastCommit via a user or chain trigger. On restart,
3✔
1072
                // this state may be reexecuted after closing the channel, but
3✔
1073
                // failing to commit to StateContractClosed or
3✔
1074
                // StateFullyResolved. In that case, one of the four close
3✔
1075
                // triggers will be presented, signifying that we should skip
3✔
1076
                // rebroadcasting, and go straight to resolving the on-chain
3✔
1077
                // contract or marking the channel resolved.
3✔
1078
                switch trigger {
3✔
1079
                case localCloseTrigger, remoteCloseTrigger:
×
1080
                        log.Infof("ChannelArbitrator(%v): detected %s "+
×
1081
                                "close after closing channel, fast-forwarding "+
×
1082
                                "to %s to resolve contract",
×
1083
                                c.cfg.ChanPoint, trigger, StateContractClosed)
×
1084
                        return StateContractClosed, closeTx, nil
×
1085

1086
                case breachCloseTrigger:
3✔
1087
                        nextContractState, err := c.checkLegacyBreach()
3✔
1088
                        if nextContractState == StateError {
3✔
1089
                                log.Infof("ChannelArbitrator(%v): unable to "+
×
1090
                                        "advance breach close resolution: %v",
×
1091
                                        c.cfg.ChanPoint, nextContractState)
×
1092
                                return StateError, closeTx, err
×
1093
                        }
×
1094

1095
                        log.Infof("ChannelArbitrator(%v): detected %s close "+
3✔
1096
                                "after closing channel, fast-forwarding to %s"+
3✔
1097
                                " to resolve contract", c.cfg.ChanPoint,
3✔
1098
                                trigger, nextContractState)
3✔
1099

3✔
1100
                        return nextContractState, closeTx, nil
3✔
1101

1102
                case coopCloseTrigger:
×
1103
                        log.Infof("ChannelArbitrator(%v): detected %s "+
×
1104
                                "close after closing channel, fast-forwarding "+
×
1105
                                "to %s to resolve contract",
×
1106
                                c.cfg.ChanPoint, trigger, StateFullyResolved)
×
1107
                        return StateFullyResolved, closeTx, nil
×
1108
                }
1109

1110
                log.Infof("ChannelArbitrator(%v): force closing "+
3✔
1111
                        "chan", c.cfg.ChanPoint)
3✔
1112

3✔
1113
                // Now that we have all the actions decided for the set of
3✔
1114
                // HTLC's, we'll broadcast the commitment transaction, and
3✔
1115
                // signal the link to exit.
3✔
1116

3✔
1117
                // We'll tell the switch that it should remove the link for
3✔
1118
                // this channel, in addition to fetching the force close
3✔
1119
                // summary needed to close this channel on chain.
3✔
1120
                forceCloseTx, err := c.cfg.Channel.ForceCloseChan()
3✔
1121
                if err != nil {
3✔
1122
                        log.Errorf("ChannelArbitrator(%v): unable to "+
×
1123
                                "force close: %v", c.cfg.ChanPoint, err)
×
1124

×
1125
                        // We tried to force close (HTLC may be expiring from
×
1126
                        // our PoV, etc), but we think we've lost data. In this
×
1127
                        // case, we'll not force close, but terminate the state
×
1128
                        // machine here to wait to see what confirms on chain.
×
1129
                        if errors.Is(err, lnwallet.ErrForceCloseLocalDataLoss) {
×
1130
                                log.Error("ChannelArbitrator(%v): broadcast "+
×
1131
                                        "failed due to local data loss, "+
×
1132
                                        "waiting for on chain confimation...",
×
1133
                                        c.cfg.ChanPoint)
×
1134

×
1135
                                return StateBroadcastCommit, nil, nil
×
1136
                        }
×
1137

1138
                        return StateError, closeTx, err
×
1139
                }
1140
                closeTx = forceCloseTx
3✔
1141

3✔
1142
                // Before publishing the transaction, we store it to the
3✔
1143
                // database, such that we can re-publish later in case it
3✔
1144
                // didn't propagate. We initiated the force close, so we
3✔
1145
                // mark broadcast with local initiator set to true.
3✔
1146
                err = c.cfg.MarkCommitmentBroadcasted(closeTx, lntypes.Local)
3✔
1147
                if err != nil {
3✔
1148
                        log.Errorf("ChannelArbitrator(%v): unable to "+
×
1149
                                "mark commitment broadcasted: %v",
×
1150
                                c.cfg.ChanPoint, err)
×
1151
                        return StateError, closeTx, err
×
1152
                }
×
1153

1154
                // With the close transaction in hand, broadcast the
1155
                // transaction to the network, thereby entering the post
1156
                // channel resolution state.
1157
                log.Infof("Broadcasting force close transaction %v, "+
3✔
1158
                        "ChannelPoint(%v): %v", closeTx.TxHash(),
3✔
1159
                        c.cfg.ChanPoint, lnutils.SpewLogClosure(closeTx))
3✔
1160

3✔
1161
                // At this point, we'll now broadcast the commitment
3✔
1162
                // transaction itself.
3✔
1163
                label := labels.MakeLabel(
3✔
1164
                        labels.LabelTypeChannelClose, &c.cfg.ShortChanID,
3✔
1165
                )
3✔
1166
                if err := c.cfg.PublishTx(closeTx, label); err != nil {
6✔
1167
                        log.Errorf("ChannelArbitrator(%v): unable to broadcast "+
3✔
1168
                                "close tx: %v", c.cfg.ChanPoint, err)
3✔
1169

3✔
1170
                        // This makes sure we don't fail at startup if the
3✔
1171
                        // commitment transaction has too low fees to make it
3✔
1172
                        // into mempool. The rebroadcaster makes sure this
3✔
1173
                        // transaction is republished regularly until confirmed
3✔
1174
                        // or replaced.
3✔
1175
                        if !errors.Is(err, lnwallet.ErrDoubleSpend) &&
3✔
1176
                                !errors.Is(err, lnwallet.ErrMempoolFee) {
6✔
1177

3✔
1178
                                return StateError, closeTx, err
3✔
1179
                        }
3✔
1180
                }
1181

1182
                // We go to the StateCommitmentBroadcasted state, where we'll
1183
                // be waiting for the commitment to be confirmed.
1184
                nextState = StateCommitmentBroadcasted
3✔
1185

1186
        // In this state we have broadcasted our own commitment, and will need
1187
        // to wait for a commitment (not necessarily the one we broadcasted!)
1188
        // to be confirmed.
1189
        case StateCommitmentBroadcasted:
3✔
1190
                switch trigger {
3✔
1191

1192
                // We are waiting for a commitment to be confirmed.
1193
                case chainTrigger, userTrigger:
3✔
1194
                        // The commitment transaction has been broadcast, but it
3✔
1195
                        // doesn't necessarily need to be the commitment
3✔
1196
                        // transaction version that is going to be confirmed. To
3✔
1197
                        // be sure that any of those versions can be anchored
3✔
1198
                        // down, we now submit all anchor resolutions to the
3✔
1199
                        // sweeper. The sweeper will keep trying to sweep all of
3✔
1200
                        // them.
3✔
1201
                        //
3✔
1202
                        // Note that the sweeper is idempotent. If we ever
3✔
1203
                        // happen to end up at this point in the code again, no
3✔
1204
                        // harm is done by re-offering the anchors to the
3✔
1205
                        // sweeper.
3✔
1206
                        anchors, err := c.cfg.Channel.NewAnchorResolutions()
3✔
1207
                        if err != nil {
3✔
1208
                                return StateError, closeTx, err
×
1209
                        }
×
1210

1211
                        err = c.sweepAnchors(anchors, triggerHeight)
3✔
1212
                        if err != nil {
3✔
1213
                                return StateError, closeTx, err
×
1214
                        }
×
1215

1216
                        nextState = StateCommitmentBroadcasted
3✔
1217

1218
                // If this state advance was triggered by any of the
1219
                // commitments being confirmed, then we'll jump to the state
1220
                // where the contract has been closed.
1221
                case localCloseTrigger, remoteCloseTrigger:
3✔
1222
                        nextState = StateContractClosed
3✔
1223

1224
                // If a coop close was confirmed, jump straight to the fully
1225
                // resolved state.
1226
                case coopCloseTrigger:
×
1227
                        nextState = StateFullyResolved
×
1228

1229
                case breachCloseTrigger:
×
1230
                        nextContractState, err := c.checkLegacyBreach()
×
1231
                        if nextContractState == StateError {
×
1232
                                return nextContractState, closeTx, err
×
1233
                        }
×
1234

1235
                        nextState = nextContractState
×
1236
                }
1237

1238
                log.Infof("ChannelArbitrator(%v): trigger %v moving from "+
3✔
1239
                        "state %v to %v", c.cfg.ChanPoint, trigger, c.state,
3✔
1240
                        nextState)
3✔
1241

1242
        // If we're in this state, then the contract has been fully closed to
1243
        // outside sub-systems, so we'll process the prior set of on-chain
1244
        // contract actions and launch a set of resolvers.
1245
        case StateContractClosed:
3✔
1246
                // First, we'll fetch our chain actions, and both sets of
3✔
1247
                // resolutions so we can process them.
3✔
1248
                contractResolutions, err := c.log.FetchContractResolutions()
3✔
1249
                if err != nil {
3✔
1250
                        log.Errorf("unable to fetch contract resolutions: %v",
×
1251
                                err)
×
1252
                        return StateError, closeTx, err
×
1253
                }
×
1254

1255
                // If the resolution is empty, and we have no HTLCs at all to
1256
                // send to, then we're done here. We don't need to launch any
1257
                // resolvers, and can go straight to our final state.
1258
                if contractResolutions.IsEmpty() && confCommitSet.IsEmpty() {
6✔
1259
                        log.Infof("ChannelArbitrator(%v): contract "+
3✔
1260
                                "resolutions empty, marking channel as fully resolved!",
3✔
1261
                                c.cfg.ChanPoint)
3✔
1262
                        nextState = StateFullyResolved
3✔
1263
                        break
3✔
1264
                }
1265

1266
                // First, we'll reconstruct a fresh set of chain actions as the
1267
                // set of actions we need to act on may differ based on if it
1268
                // was our commitment, or they're commitment that hit the chain.
1269
                htlcActions, err := c.constructChainActions(
3✔
1270
                        confCommitSet, triggerHeight, trigger,
3✔
1271
                )
3✔
1272
                if err != nil {
3✔
1273
                        return StateError, closeTx, err
×
1274
                }
×
1275

1276
                // In case its a breach transaction we fail back all upstream
1277
                // HTLCs for their corresponding outgoing HTLCs on the remote
1278
                // commitment set (remote and remote pending set).
1279
                if contractResolutions.BreachResolution != nil {
6✔
1280
                        // cancelBreachedHTLCs is a set which holds HTLCs whose
3✔
1281
                        // corresponding incoming HTLCs will be failed back
3✔
1282
                        // because the peer broadcasted an old state.
3✔
1283
                        cancelBreachedHTLCs := fn.NewSet[uint64]()
3✔
1284

3✔
1285
                        // We'll use the CommitSet, we'll fail back all
3✔
1286
                        // upstream HTLCs for their corresponding outgoing
3✔
1287
                        // HTLC that exist on either of the remote commitments.
3✔
1288
                        // The map is used to deduplicate any shared HTLC's.
3✔
1289
                        for htlcSetKey, htlcs := range confCommitSet.HtlcSets {
6✔
1290
                                if !htlcSetKey.IsRemote {
6✔
1291
                                        continue
3✔
1292
                                }
1293

1294
                                for _, htlc := range htlcs {
6✔
1295
                                        // Only outgoing HTLCs have a
3✔
1296
                                        // corresponding incoming HTLC.
3✔
1297
                                        if htlc.Incoming {
6✔
1298
                                                continue
3✔
1299
                                        }
1300

1301
                                        cancelBreachedHTLCs.Add(htlc.HtlcIndex)
3✔
1302
                                }
1303
                        }
1304

1305
                        err := c.abandonForwards(cancelBreachedHTLCs)
3✔
1306
                        if err != nil {
3✔
1307
                                return StateError, closeTx, err
×
1308
                        }
×
1309
                } else {
3✔
1310
                        // If it's not a breach, we resolve all incoming dust
3✔
1311
                        // HTLCs immediately after the commitment is confirmed.
3✔
1312
                        err = c.failIncomingDust(
3✔
1313
                                htlcActions[HtlcIncomingDustFinalAction],
3✔
1314
                        )
3✔
1315
                        if err != nil {
3✔
1316
                                return StateError, closeTx, err
×
1317
                        }
×
1318

1319
                        // We fail the upstream HTLCs for all remote pending
1320
                        // outgoing HTLCs as soon as the commitment is
1321
                        // confirmed. The upstream HTLCs for outgoing dust
1322
                        // HTLCs have already been resolved before we reach
1323
                        // this point.
1324
                        getIdx := func(htlc channeldb.HTLC) uint64 {
6✔
1325
                                return htlc.HtlcIndex
3✔
1326
                        }
3✔
1327
                        remoteDangling := fn.NewSet(fn.Map(
3✔
1328
                                htlcActions[HtlcFailDanglingAction], getIdx,
3✔
1329
                        )...)
3✔
1330
                        err := c.abandonForwards(remoteDangling)
3✔
1331
                        if err != nil {
3✔
1332
                                return StateError, closeTx, err
×
1333
                        }
×
1334
                }
1335

1336
                // Now that we know we'll need to act, we'll process all the
1337
                // resolvers, then create the structures we need to resolve all
1338
                // outstanding contracts.
1339
                resolvers, err := c.prepContractResolutions(
3✔
1340
                        contractResolutions, triggerHeight, htlcActions,
3✔
1341
                )
3✔
1342
                if err != nil {
3✔
1343
                        log.Errorf("ChannelArbitrator(%v): unable to "+
×
1344
                                "resolve contracts: %v", c.cfg.ChanPoint, err)
×
1345
                        return StateError, closeTx, err
×
1346
                }
×
1347

1348
                log.Debugf("ChannelArbitrator(%v): inserting %v contract "+
3✔
1349
                        "resolvers", c.cfg.ChanPoint, len(resolvers))
3✔
1350

3✔
1351
                err = c.log.InsertUnresolvedContracts(nil, resolvers...)
3✔
1352
                if err != nil {
3✔
1353
                        return StateError, closeTx, err
×
1354
                }
×
1355

1356
                // Finally, we'll launch all the required contract resolvers.
1357
                // Once they're all resolved, we're no longer needed.
1358
                c.resolveContracts(resolvers)
3✔
1359

3✔
1360
                nextState = StateWaitingFullResolution
3✔
1361

1362
        // This is our terminal state. We'll keep returning this state until
1363
        // all contracts are fully resolved.
1364
        case StateWaitingFullResolution:
3✔
1365
                log.Infof("ChannelArbitrator(%v): still awaiting contract "+
3✔
1366
                        "resolution", c.cfg.ChanPoint)
3✔
1367

3✔
1368
                unresolved, err := c.log.FetchUnresolvedContracts()
3✔
1369
                if err != nil {
3✔
1370
                        return StateError, closeTx, err
×
1371
                }
×
1372

1373
                // If we have no unresolved contracts, then we can move to the
1374
                // final state.
1375
                if len(unresolved) == 0 {
6✔
1376
                        nextState = StateFullyResolved
3✔
1377
                        break
3✔
1378
                }
1379

1380
                // Otherwise we still have unresolved contracts, then we'll
1381
                // stay alive to oversee their resolution.
1382
                nextState = StateWaitingFullResolution
3✔
1383

3✔
1384
                // Add debug logs.
3✔
1385
                for _, r := range unresolved {
6✔
1386
                        log.Debugf("ChannelArbitrator(%v): still have "+
3✔
1387
                                "unresolved contract: %T", c.cfg.ChanPoint, r)
3✔
1388
                }
3✔
1389

1390
        // If we start as fully resolved, then we'll end as fully resolved.
1391
        case StateFullyResolved:
3✔
1392
                // To ensure that the state of the contract in persistent
3✔
1393
                // storage is properly reflected, we'll mark the contract as
3✔
1394
                // fully resolved now.
3✔
1395
                nextState = StateFullyResolved
3✔
1396

3✔
1397
                log.Infof("ChannelPoint(%v) has been fully resolved "+
3✔
1398
                        "on-chain at height=%v", c.cfg.ChanPoint, triggerHeight)
3✔
1399

3✔
1400
                if err := c.cfg.MarkChannelResolved(); err != nil {
3✔
1401
                        log.Errorf("unable to mark channel resolved: %v", err)
×
1402
                        return StateError, closeTx, err
×
1403
                }
×
1404
        }
1405

1406
        log.Tracef("ChannelArbitrator(%v): next_state=%v", c.cfg.ChanPoint,
3✔
1407
                nextState)
3✔
1408

3✔
1409
        return nextState, closeTx, nil
3✔
1410
}
1411

1412
// sweepAnchors offers all given anchor resolutions to the sweeper. It requests
1413
// sweeping at the minimum fee rate. This fee rate can be upped manually by the
1414
// user via the BumpFee rpc.
1415
func (c *ChannelArbitrator) sweepAnchors(anchors *lnwallet.AnchorResolutions,
1416
        heightHint uint32) error {
3✔
1417

3✔
1418
        // Update the set of activeHTLCs so that the sweeping routine has an
3✔
1419
        // up-to-date view of the set of commitments.
3✔
1420
        c.updateActiveHTLCs()
3✔
1421

3✔
1422
        // Prepare the sweeping requests for all possible versions of
3✔
1423
        // commitments.
3✔
1424
        sweepReqs, err := c.prepareAnchorSweeps(heightHint, anchors)
3✔
1425
        if err != nil {
3✔
1426
                return err
×
1427
        }
×
1428

1429
        // Send out the sweeping requests to the sweeper.
1430
        for _, req := range sweepReqs {
6✔
1431
                _, err = c.cfg.Sweeper.SweepInput(req.input, req.params)
3✔
1432
                if err != nil {
3✔
1433
                        return err
×
1434
                }
×
1435
        }
1436

1437
        return nil
3✔
1438
}
1439

1440
// findCommitmentDeadlineAndValue finds the deadline (relative block height)
1441
// for a commitment transaction by extracting the minimum CLTV from its HTLCs.
1442
// From our PoV, the deadline delta is defined to be the smaller of,
1443
//   - half of the least CLTV from outgoing HTLCs' corresponding incoming
1444
//     HTLCs,  or,
1445
//   - half of the least CLTV from incoming HTLCs if the preimage is available.
1446
//
1447
// We use half of the CTLV value to ensure that we have enough time to sweep
1448
// the second-level HTLCs.
1449
//
1450
// It also finds the total value that are time-sensitive, which is the sum of
1451
// all the outgoing HTLCs plus incoming HTLCs whose preimages are known. It
1452
// then returns the value left after subtracting the budget used for sweeping
1453
// the time-sensitive HTLCs.
1454
//
1455
// NOTE: when the deadline turns out to be 0 blocks, we will replace it with 1
1456
// block because our fee estimator doesn't allow a 0 conf target. This also
1457
// means we've left behind and should increase our fee to make the transaction
1458
// confirmed asap.
1459
func (c *ChannelArbitrator) findCommitmentDeadlineAndValue(heightHint uint32,
1460
        htlcs htlcSet) (fn.Option[int32], btcutil.Amount, error) {
3✔
1461

3✔
1462
        deadlineMinHeight := uint32(math.MaxUint32)
3✔
1463
        totalValue := btcutil.Amount(0)
3✔
1464

3✔
1465
        // First, iterate through the outgoingHTLCs to find the lowest CLTV
3✔
1466
        // value.
3✔
1467
        for _, htlc := range htlcs.outgoingHTLCs {
6✔
1468
                // Skip if the HTLC is dust.
3✔
1469
                if htlc.OutputIndex < 0 {
6✔
1470
                        log.Debugf("ChannelArbitrator(%v): skipped deadline "+
3✔
1471
                                "for dust htlc=%x",
3✔
1472
                                c.cfg.ChanPoint, htlc.RHash[:])
3✔
1473

3✔
1474
                        continue
3✔
1475
                }
1476

1477
                value := htlc.Amt.ToSatoshis()
3✔
1478

3✔
1479
                // Find the expiry height for this outgoing HTLC's incoming
3✔
1480
                // HTLC.
3✔
1481
                deadlineOpt := c.cfg.FindOutgoingHTLCDeadline(htlc)
3✔
1482

3✔
1483
                // The deadline is default to the current deadlineMinHeight,
3✔
1484
                // and it's overwritten when it's not none.
3✔
1485
                deadline := deadlineMinHeight
3✔
1486
                deadlineOpt.WhenSome(func(d int32) {
6✔
1487
                        deadline = uint32(d)
3✔
1488

3✔
1489
                        // We only consider the value is under protection when
3✔
1490
                        // it's time-sensitive.
3✔
1491
                        totalValue += value
3✔
1492
                })
3✔
1493

1494
                if deadline < deadlineMinHeight {
6✔
1495
                        deadlineMinHeight = deadline
3✔
1496

3✔
1497
                        log.Tracef("ChannelArbitrator(%v): outgoing HTLC has "+
3✔
1498
                                "deadline=%v, value=%v", c.cfg.ChanPoint,
3✔
1499
                                deadlineMinHeight, value)
3✔
1500
                }
3✔
1501
        }
1502

1503
        // Then going through the incomingHTLCs, and update the minHeight when
1504
        // conditions met.
1505
        for _, htlc := range htlcs.incomingHTLCs {
6✔
1506
                // Skip if the HTLC is dust.
3✔
1507
                if htlc.OutputIndex < 0 {
3✔
1508
                        log.Debugf("ChannelArbitrator(%v): skipped deadline "+
×
1509
                                "for dust htlc=%x",
×
1510
                                c.cfg.ChanPoint, htlc.RHash[:])
×
1511

×
1512
                        continue
×
1513
                }
1514

1515
                // Since it's an HTLC sent to us, check if we have preimage for
1516
                // this HTLC.
1517
                preimageAvailable, err := c.isPreimageAvailable(htlc.RHash)
3✔
1518
                if err != nil {
3✔
1519
                        return fn.None[int32](), 0, err
×
1520
                }
×
1521

1522
                if !preimageAvailable {
6✔
1523
                        continue
3✔
1524
                }
1525

1526
                value := htlc.Amt.ToSatoshis()
3✔
1527
                totalValue += value
3✔
1528

3✔
1529
                if htlc.RefundTimeout < deadlineMinHeight {
6✔
1530
                        deadlineMinHeight = htlc.RefundTimeout
3✔
1531

3✔
1532
                        log.Tracef("ChannelArbitrator(%v): incoming HTLC has "+
3✔
1533
                                "deadline=%v, amt=%v", c.cfg.ChanPoint,
3✔
1534
                                deadlineMinHeight, value)
3✔
1535
                }
3✔
1536
        }
1537

1538
        // Calculate the deadline. There are two cases to be handled here,
1539
        //   - when the deadlineMinHeight never gets updated, which could
1540
        //     happen when we have no outgoing HTLCs, and, for incoming HTLCs,
1541
        //       * either we have none, or,
1542
        //       * none of the HTLCs are preimageAvailable.
1543
        //   - when our deadlineMinHeight is no greater than the heightHint,
1544
        //     which means we are behind our schedule.
1545
        var deadline uint32
3✔
1546
        switch {
3✔
1547
        // When we couldn't find a deadline height from our HTLCs, we will fall
1548
        // back to the default value as there's no time pressure here.
1549
        case deadlineMinHeight == math.MaxUint32:
3✔
1550
                return fn.None[int32](), 0, nil
3✔
1551

1552
        // When the deadline is passed, we will fall back to the smallest conf
1553
        // target (1 block).
1554
        case deadlineMinHeight <= heightHint:
3✔
1555
                log.Warnf("ChannelArbitrator(%v): deadline is passed with "+
3✔
1556
                        "deadlineMinHeight=%d, heightHint=%d",
3✔
1557
                        c.cfg.ChanPoint, deadlineMinHeight, heightHint)
3✔
1558
                deadline = 1
3✔
1559

1560
        // Use half of the deadline delta, and leave the other half to be used
1561
        // to sweep the HTLCs.
1562
        default:
3✔
1563
                deadline = (deadlineMinHeight - heightHint) / 2
3✔
1564
        }
1565

1566
        // Calculate the value left after subtracting the budget used for
1567
        // sweeping the time-sensitive HTLCs.
1568
        valueLeft := totalValue - calculateBudget(
3✔
1569
                totalValue, c.cfg.Budget.DeadlineHTLCRatio,
3✔
1570
                c.cfg.Budget.DeadlineHTLC,
3✔
1571
        )
3✔
1572

3✔
1573
        log.Debugf("ChannelArbitrator(%v): calculated valueLeft=%v, "+
3✔
1574
                "deadline=%d, using deadlineMinHeight=%d, heightHint=%d",
3✔
1575
                c.cfg.ChanPoint, valueLeft, deadline, deadlineMinHeight,
3✔
1576
                heightHint)
3✔
1577

3✔
1578
        return fn.Some(int32(deadline)), valueLeft, nil
3✔
1579
}
1580

1581
// resolveContracts updates the activeResolvers list and starts to resolve each
1582
// contract concurrently, and launches them.
1583
func (c *ChannelArbitrator) resolveContracts(resolvers []ContractResolver) {
3✔
1584
        c.activeResolversLock.Lock()
3✔
1585
        c.activeResolvers = resolvers
3✔
1586
        c.activeResolversLock.Unlock()
3✔
1587

3✔
1588
        // Launch all resolvers.
3✔
1589
        c.launchResolvers()
3✔
1590

3✔
1591
        for _, contract := range resolvers {
6✔
1592
                c.wg.Add(1)
3✔
1593
                go c.resolveContract(contract)
3✔
1594
        }
3✔
1595
}
1596

1597
// launchResolvers launches all the active resolvers concurrently.
1598
func (c *ChannelArbitrator) launchResolvers() {
3✔
1599
        c.activeResolversLock.Lock()
3✔
1600
        resolvers := c.activeResolvers
3✔
1601
        c.activeResolversLock.Unlock()
3✔
1602

3✔
1603
        // errChans is a map of channels that will be used to receive errors
3✔
1604
        // returned from launching the resolvers.
3✔
1605
        errChans := make(map[ContractResolver]chan error, len(resolvers))
3✔
1606

3✔
1607
        // Launch each resolver in goroutines.
3✔
1608
        for _, r := range resolvers {
6✔
1609
                // If the contract is already resolved, there's no need to
3✔
1610
                // launch it again.
3✔
1611
                if r.IsResolved() {
6✔
1612
                        log.Debugf("ChannelArbitrator(%v): skipping resolver "+
3✔
1613
                                "%T as it's already resolved", c.cfg.ChanPoint,
3✔
1614
                                r)
3✔
1615

3✔
1616
                        continue
3✔
1617
                }
1618

1619
                // Create a signal chan.
1620
                errChan := make(chan error, 1)
3✔
1621
                errChans[r] = errChan
3✔
1622

3✔
1623
                go func() {
6✔
1624
                        err := r.Launch()
3✔
1625
                        errChan <- err
3✔
1626
                }()
3✔
1627
        }
1628

1629
        // Wait for all resolvers to finish launching.
1630
        for r, errChan := range errChans {
6✔
1631
                select {
3✔
1632
                case err := <-errChan:
3✔
1633
                        if err == nil {
6✔
1634
                                continue
3✔
1635
                        }
1636

1637
                        log.Errorf("ChannelArbitrator(%v): unable to launch "+
×
1638
                                "contract resolver(%T): %v", c.cfg.ChanPoint, r,
×
1639
                                err)
×
1640

1641
                case <-c.quit:
×
1642
                        log.Debugf("ChannelArbitrator quit signal received, " +
×
1643
                                "exit launchResolvers")
×
1644

×
1645
                        return
×
1646
                }
1647
        }
1648
}
1649

1650
// advanceState is the main driver of our state machine. This method is an
1651
// iterative function which repeatedly attempts to advance the internal state
1652
// of the channel arbitrator. The state will be advanced until we reach a
1653
// redundant transition, meaning that the state transition is a noop. The final
1654
// param is a callback that allows the caller to execute an arbitrary action
1655
// after each state transition.
1656
func (c *ChannelArbitrator) advanceState(
1657
        triggerHeight uint32, trigger transitionTrigger,
1658
        confCommitSet *CommitSet) (ArbitratorState, *wire.MsgTx, error) {
3✔
1659

3✔
1660
        var (
3✔
1661
                priorState   ArbitratorState
3✔
1662
                forceCloseTx *wire.MsgTx
3✔
1663
        )
3✔
1664

3✔
1665
        // We'll continue to advance our state forward until the state we
3✔
1666
        // transition to is that same state that we started at.
3✔
1667
        for {
6✔
1668
                priorState = c.state
3✔
1669
                log.Debugf("ChannelArbitrator(%v): attempting state step with "+
3✔
1670
                        "trigger=%v from state=%v at height=%v",
3✔
1671
                        c.cfg.ChanPoint, trigger, priorState, triggerHeight)
3✔
1672

3✔
1673
                nextState, closeTx, err := c.stateStep(
3✔
1674
                        triggerHeight, trigger, confCommitSet,
3✔
1675
                )
3✔
1676
                if err != nil {
6✔
1677
                        log.Errorf("ChannelArbitrator(%v): unable to advance "+
3✔
1678
                                "state: %v", c.cfg.ChanPoint, err)
3✔
1679
                        return priorState, nil, err
3✔
1680
                }
3✔
1681

1682
                if forceCloseTx == nil && closeTx != nil {
6✔
1683
                        forceCloseTx = closeTx
3✔
1684
                }
3✔
1685

1686
                // Our termination transition is a noop transition. If we get
1687
                // our prior state back as the next state, then we'll
1688
                // terminate.
1689
                if nextState == priorState {
6✔
1690
                        log.Debugf("ChannelArbitrator(%v): terminating at "+
3✔
1691
                                "state=%v", c.cfg.ChanPoint, nextState)
3✔
1692
                        return nextState, forceCloseTx, nil
3✔
1693
                }
3✔
1694

1695
                // As the prior state was successfully executed, we can now
1696
                // commit the next state. This ensures that we will re-execute
1697
                // the prior state if anything fails.
1698
                if err := c.log.CommitState(nextState); err != nil {
3✔
1699
                        log.Errorf("ChannelArbitrator(%v): unable to commit "+
×
1700
                                "next state(%v): %v", c.cfg.ChanPoint,
×
1701
                                nextState, err)
×
1702
                        return priorState, nil, err
×
1703
                }
×
1704
                c.state = nextState
3✔
1705
        }
1706
}
1707

1708
// ChainAction is an enum that encompasses all possible on-chain actions
1709
// we'll take for a set of HTLC's.
1710
type ChainAction uint8
1711

1712
const (
1713
        // NoAction is the min chainAction type, indicating that no action
1714
        // needs to be taken for a given HTLC.
1715
        NoAction ChainAction = 0
1716

1717
        // HtlcTimeoutAction indicates that the HTLC will timeout soon. As a
1718
        // result, we should get ready to sweep it on chain after the timeout.
1719
        HtlcTimeoutAction = 1
1720

1721
        // HtlcClaimAction indicates that we should claim the HTLC on chain
1722
        // before its timeout period.
1723
        HtlcClaimAction = 2
1724

1725
        // HtlcFailDustAction indicates that we should fail the upstream HTLC
1726
        // for an outgoing dust HTLC immediately (even before the commitment
1727
        // transaction is confirmed) because it has no output on the commitment
1728
        // transaction. This also includes remote pending outgoing dust HTLCs.
1729
        HtlcFailDustAction = 3
1730

1731
        // HtlcOutgoingWatchAction indicates that we can't yet timeout this
1732
        // HTLC, but we had to go to chain on order to resolve an existing
1733
        // HTLC.  In this case, we'll either: time it out once it expires, or
1734
        // will learn the pre-image if the remote party claims the output. In
1735
        // this case, well add the pre-image to our global store.
1736
        HtlcOutgoingWatchAction = 4
1737

1738
        // HtlcIncomingWatchAction indicates that we don't yet have the
1739
        // pre-image to claim incoming HTLC, but we had to go to chain in order
1740
        // to resolve and existing HTLC. In this case, we'll either: let the
1741
        // other party time it out, or eventually learn of the pre-image, in
1742
        // which case we'll claim on chain.
1743
        HtlcIncomingWatchAction = 5
1744

1745
        // HtlcIncomingDustFinalAction indicates that we should mark an incoming
1746
        // dust htlc as final because it can't be claimed on-chain.
1747
        HtlcIncomingDustFinalAction = 6
1748

1749
        // HtlcFailDanglingAction indicates that we should fail the upstream
1750
        // HTLC for an outgoing HTLC immediately after the commitment
1751
        // transaction has confirmed because it has no corresponding output on
1752
        // the commitment transaction. This category does NOT include any dust
1753
        // HTLCs which are mapped in the "HtlcFailDustAction" category.
1754
        HtlcFailDanglingAction = 7
1755
)
1756

1757
// String returns a human readable string describing a chain action.
1758
func (c ChainAction) String() string {
×
1759
        switch c {
×
1760
        case NoAction:
×
1761
                return "NoAction"
×
1762

1763
        case HtlcTimeoutAction:
×
1764
                return "HtlcTimeoutAction"
×
1765

1766
        case HtlcClaimAction:
×
1767
                return "HtlcClaimAction"
×
1768

1769
        case HtlcFailDustAction:
×
1770
                return "HtlcFailDustAction"
×
1771

1772
        case HtlcOutgoingWatchAction:
×
1773
                return "HtlcOutgoingWatchAction"
×
1774

1775
        case HtlcIncomingWatchAction:
×
1776
                return "HtlcIncomingWatchAction"
×
1777

1778
        case HtlcIncomingDustFinalAction:
×
1779
                return "HtlcIncomingDustFinalAction"
×
1780

1781
        case HtlcFailDanglingAction:
×
1782
                return "HtlcFailDanglingAction"
×
1783

1784
        default:
×
1785
                return "<unknown action>"
×
1786
        }
1787
}
1788

1789
// ChainActionMap is a map of a chain action, to the set of HTLC's that need to
1790
// be acted upon for a given action type. The channel
1791
type ChainActionMap map[ChainAction][]channeldb.HTLC
1792

1793
// Merge merges the passed chain actions with the target chain action map.
1794
func (c ChainActionMap) Merge(actions ChainActionMap) {
3✔
1795
        for chainAction, htlcs := range actions {
6✔
1796
                c[chainAction] = append(c[chainAction], htlcs...)
3✔
1797
        }
3✔
1798
}
1799

1800
// shouldGoOnChain takes into account the absolute timeout of the HTLC, if the
1801
// confirmation delta that we need is close, and returns a bool indicating if
1802
// we should go on chain to claim.  We do this rather than waiting up until the
1803
// last minute as we want to ensure that when we *need* (HTLC is timed out) to
1804
// sweep, the commitment is already confirmed.
1805
func (c *ChannelArbitrator) shouldGoOnChain(htlc channeldb.HTLC,
1806
        broadcastDelta, currentHeight uint32) bool {
3✔
1807

3✔
1808
        // We'll calculate the broadcast cut off for this HTLC. This is the
3✔
1809
        // height that (based on our current fee estimation) we should
3✔
1810
        // broadcast in order to ensure the commitment transaction is confirmed
3✔
1811
        // before the HTLC fully expires.
3✔
1812
        broadcastCutOff := htlc.RefundTimeout - broadcastDelta
3✔
1813

3✔
1814
        log.Tracef("ChannelArbitrator(%v): examining outgoing contract: "+
3✔
1815
                "expiry=%v, cutoff=%v, height=%v", c.cfg.ChanPoint, htlc.RefundTimeout,
3✔
1816
                broadcastCutOff, currentHeight)
3✔
1817

3✔
1818
        // TODO(roasbeef): take into account default HTLC delta, don't need to
3✔
1819
        // broadcast immediately
3✔
1820
        //  * can then batch with SINGLE | ANYONECANPAY
3✔
1821

3✔
1822
        // We should on-chain for this HTLC, iff we're within out broadcast
3✔
1823
        // cutoff window.
3✔
1824
        if currentHeight < broadcastCutOff {
6✔
1825
                return false
3✔
1826
        }
3✔
1827

1828
        // In case of incoming htlc we should go to chain.
1829
        if htlc.Incoming {
6✔
1830
                return true
3✔
1831
        }
3✔
1832

1833
        // For htlcs that are result of our initiated payments we give some grace
1834
        // period before force closing the channel. During this time we expect
1835
        // both nodes to connect and give a chance to the other node to send its
1836
        // updates and cancel the htlc.
1837
        // This shouldn't add any security risk as there is no incoming htlc to
1838
        // fulfill at this case and the expectation is that when the channel is
1839
        // active the other node will send update_fail_htlc to remove the htlc
1840
        // without closing the channel. It is up to the user to force close the
1841
        // channel if the peer misbehaves and doesn't send the update_fail_htlc.
1842
        // It is useful when this node is most of the time not online and is
1843
        // likely to miss the time slot where the htlc may be cancelled.
1844
        isForwarded := c.cfg.IsForwardedHTLC(c.cfg.ShortChanID, htlc.HtlcIndex)
3✔
1845
        upTime := c.cfg.Clock.Now().Sub(c.startTimestamp)
3✔
1846
        return isForwarded || upTime > c.cfg.PaymentsExpirationGracePeriod
3✔
1847
}
1848

1849
// checkCommitChainActions is called for each new block connected to the end of
1850
// the main chain. Given the new block height, this new method will examine all
1851
// active HTLC's, and determine if we need to go on-chain to claim any of them.
1852
// A map of action -> []htlc is returned, detailing what action (if any) should
1853
// be performed for each HTLC. For timed out HTLC's, once the commitment has
1854
// been sufficiently confirmed, the HTLC's should be canceled backwards. For
1855
// redeemed HTLC's, we should send the pre-image back to the incoming link.
1856
func (c *ChannelArbitrator) checkCommitChainActions(height uint32,
1857
        trigger transitionTrigger, htlcs htlcSet) (ChainActionMap, error) {
3✔
1858

3✔
1859
        // TODO(roasbeef): would need to lock channel? channel totem?
3✔
1860
        //  * race condition if adding and we broadcast, etc
3✔
1861
        //  * or would make each instance sync?
3✔
1862

3✔
1863
        log.Debugf("ChannelArbitrator(%v): checking commit chain actions at "+
3✔
1864
                "height=%v, in_htlc_count=%v, out_htlc_count=%v",
3✔
1865
                c.cfg.ChanPoint, height,
3✔
1866
                len(htlcs.incomingHTLCs), len(htlcs.outgoingHTLCs))
3✔
1867

3✔
1868
        actionMap := make(ChainActionMap)
3✔
1869

3✔
1870
        // First, we'll make an initial pass over the set of incoming and
3✔
1871
        // outgoing HTLC's to decide if we need to go on chain at all.
3✔
1872
        haveChainActions := false
3✔
1873
        for _, htlc := range htlcs.outgoingHTLCs {
6✔
1874
                // We'll need to go on-chain for an outgoing HTLC if it was
3✔
1875
                // never resolved downstream, and it's "close" to timing out.
3✔
1876
                //
3✔
1877
                // TODO(yy): If there's no corresponding incoming HTLC, it
3✔
1878
                // means we are the first hop, hence the payer. This is a
3✔
1879
                // tricky case - unlike a forwarding hop, we don't have an
3✔
1880
                // incoming HTLC that will time out, which means as long as we
3✔
1881
                // can learn the preimage, we can settle the invoice (before it
3✔
1882
                // expires?).
3✔
1883
                toChain := c.shouldGoOnChain(
3✔
1884
                        htlc, c.cfg.OutgoingBroadcastDelta, height,
3✔
1885
                )
3✔
1886

3✔
1887
                if toChain {
6✔
1888
                        // Convert to int64 in case of overflow.
3✔
1889
                        remainingBlocks := int64(htlc.RefundTimeout) -
3✔
1890
                                int64(height)
3✔
1891

3✔
1892
                        log.Infof("ChannelArbitrator(%v): go to chain for "+
3✔
1893
                                "outgoing htlc %x: timeout=%v, amount=%v, "+
3✔
1894
                                "blocks_until_expiry=%v, broadcast_delta=%v",
3✔
1895
                                c.cfg.ChanPoint, htlc.RHash[:],
3✔
1896
                                htlc.RefundTimeout, htlc.Amt, remainingBlocks,
3✔
1897
                                c.cfg.OutgoingBroadcastDelta,
3✔
1898
                        )
3✔
1899
                }
3✔
1900

1901
                haveChainActions = haveChainActions || toChain
3✔
1902
        }
1903

1904
        for _, htlc := range htlcs.incomingHTLCs {
6✔
1905
                // We'll need to go on-chain to pull an incoming HTLC iff we
3✔
1906
                // know the pre-image and it's close to timing out. We need to
3✔
1907
                // ensure that we claim the funds that are rightfully ours
3✔
1908
                // on-chain.
3✔
1909
                preimageAvailable, err := c.isPreimageAvailable(htlc.RHash)
3✔
1910
                if err != nil {
3✔
1911
                        return nil, err
×
1912
                }
×
1913

1914
                if !preimageAvailable {
6✔
1915
                        continue
3✔
1916
                }
1917

1918
                toChain := c.shouldGoOnChain(
3✔
1919
                        htlc, c.cfg.IncomingBroadcastDelta, height,
3✔
1920
                )
3✔
1921

3✔
1922
                if toChain {
6✔
1923
                        // Convert to int64 in case of overflow.
3✔
1924
                        remainingBlocks := int64(htlc.RefundTimeout) -
3✔
1925
                                int64(height)
3✔
1926

3✔
1927
                        log.Infof("ChannelArbitrator(%v): go to chain for "+
3✔
1928
                                "incoming htlc %x: timeout=%v, amount=%v, "+
3✔
1929
                                "blocks_until_expiry=%v, broadcast_delta=%v",
3✔
1930
                                c.cfg.ChanPoint, htlc.RHash[:],
3✔
1931
                                htlc.RefundTimeout, htlc.Amt, remainingBlocks,
3✔
1932
                                c.cfg.IncomingBroadcastDelta,
3✔
1933
                        )
3✔
1934
                }
3✔
1935

1936
                haveChainActions = haveChainActions || toChain
3✔
1937
        }
1938

1939
        // If we don't have any actions to make, then we'll return an empty
1940
        // action map. We only do this if this was a chain trigger though, as
1941
        // if we're going to broadcast the commitment (or the remote party did)
1942
        // we're *forced* to act on each HTLC.
1943
        if !haveChainActions && trigger == chainTrigger {
6✔
1944
                log.Tracef("ChannelArbitrator(%v): no actions to take at "+
3✔
1945
                        "height=%v", c.cfg.ChanPoint, height)
3✔
1946
                return actionMap, nil
3✔
1947
        }
3✔
1948

1949
        // Now that we know we'll need to go on-chain, we'll examine all of our
1950
        // active outgoing HTLC's to see if we either need to: sweep them after
1951
        // a timeout (then cancel backwards), cancel them backwards
1952
        // immediately, or watch them as they're still active contracts.
1953
        for _, htlc := range htlcs.outgoingHTLCs {
6✔
1954
                switch {
3✔
1955
                // If the HTLC is dust, then we can cancel it backwards
1956
                // immediately as there's no matching contract to arbitrate
1957
                // on-chain. We know the HTLC is dust, if the OutputIndex
1958
                // negative.
1959
                case htlc.OutputIndex < 0:
3✔
1960
                        log.Tracef("ChannelArbitrator(%v): immediately "+
3✔
1961
                                "failing dust htlc=%x", c.cfg.ChanPoint,
3✔
1962
                                htlc.RHash[:])
3✔
1963

3✔
1964
                        actionMap[HtlcFailDustAction] = append(
3✔
1965
                                actionMap[HtlcFailDustAction], htlc,
3✔
1966
                        )
3✔
1967

1968
                // If we don't need to immediately act on this HTLC, then we'll
1969
                // mark it still "live". After we broadcast, we'll monitor it
1970
                // until the HTLC times out to see if we can also redeem it
1971
                // on-chain.
1972
                case !c.shouldGoOnChain(htlc, c.cfg.OutgoingBroadcastDelta,
1973
                        height,
1974
                ):
3✔
1975
                        // TODO(roasbeef): also need to be able to query
3✔
1976
                        // circuit map to see if HTLC hasn't been fully
3✔
1977
                        // resolved
3✔
1978
                        //
3✔
1979
                        //  * can't fail incoming until if outgoing not yet
3✔
1980
                        //  failed
3✔
1981

3✔
1982
                        log.Tracef("ChannelArbitrator(%v): watching chain to "+
3✔
1983
                                "decide action for outgoing htlc=%x",
3✔
1984
                                c.cfg.ChanPoint, htlc.RHash[:])
3✔
1985

3✔
1986
                        actionMap[HtlcOutgoingWatchAction] = append(
3✔
1987
                                actionMap[HtlcOutgoingWatchAction], htlc,
3✔
1988
                        )
3✔
1989

1990
                // Otherwise, we'll update our actionMap to mark that we need
1991
                // to sweep this HTLC on-chain
1992
                default:
3✔
1993
                        log.Tracef("ChannelArbitrator(%v): going on-chain to "+
3✔
1994
                                "timeout htlc=%x", c.cfg.ChanPoint, htlc.RHash[:])
3✔
1995

3✔
1996
                        actionMap[HtlcTimeoutAction] = append(
3✔
1997
                                actionMap[HtlcTimeoutAction], htlc,
3✔
1998
                        )
3✔
1999
                }
2000
        }
2001

2002
        // Similarly, for each incoming HTLC, now that we need to go on-chain,
2003
        // we'll either: sweep it immediately if we know the pre-image, or
2004
        // observe the output on-chain if we don't In this last, case we'll
2005
        // either learn of it eventually from the outgoing HTLC, or the sender
2006
        // will timeout the HTLC.
2007
        for _, htlc := range htlcs.incomingHTLCs {
6✔
2008
                // If the HTLC is dust, there is no action to be taken.
3✔
2009
                if htlc.OutputIndex < 0 {
6✔
2010
                        log.Debugf("ChannelArbitrator(%v): no resolution "+
3✔
2011
                                "needed for incoming dust htlc=%x",
3✔
2012
                                c.cfg.ChanPoint, htlc.RHash[:])
3✔
2013

3✔
2014
                        actionMap[HtlcIncomingDustFinalAction] = append(
3✔
2015
                                actionMap[HtlcIncomingDustFinalAction], htlc,
3✔
2016
                        )
3✔
2017

3✔
2018
                        continue
3✔
2019
                }
2020

2021
                log.Tracef("ChannelArbitrator(%v): watching chain to decide "+
3✔
2022
                        "action for incoming htlc=%x", c.cfg.ChanPoint,
3✔
2023
                        htlc.RHash[:])
3✔
2024

3✔
2025
                actionMap[HtlcIncomingWatchAction] = append(
3✔
2026
                        actionMap[HtlcIncomingWatchAction], htlc,
3✔
2027
                )
3✔
2028
        }
2029

2030
        return actionMap, nil
3✔
2031
}
2032

2033
// isPreimageAvailable returns whether the hash preimage is available in either
2034
// the preimage cache or the invoice database.
2035
func (c *ChannelArbitrator) isPreimageAvailable(hash lntypes.Hash) (bool,
2036
        error) {
3✔
2037

3✔
2038
        // Start by checking the preimage cache for preimages of
3✔
2039
        // forwarded HTLCs.
3✔
2040
        _, preimageAvailable := c.cfg.PreimageDB.LookupPreimage(
3✔
2041
                hash,
3✔
2042
        )
3✔
2043
        if preimageAvailable {
6✔
2044
                return true, nil
3✔
2045
        }
3✔
2046

2047
        // Then check if we have an invoice that can be settled by this HTLC.
2048
        //
2049
        // TODO(joostjager): Check that there are still more blocks remaining
2050
        // than the invoice cltv delta. We don't want to go to chain only to
2051
        // have the incoming contest resolver decide that we don't want to
2052
        // settle this invoice.
2053
        invoice, err := c.cfg.Registry.LookupInvoice(context.Background(), hash)
3✔
2054
        switch {
3✔
2055
        case err == nil:
3✔
2056
        case errors.Is(err, invoices.ErrInvoiceNotFound) ||
2057
                errors.Is(err, invoices.ErrNoInvoicesCreated):
3✔
2058

3✔
2059
                return false, nil
3✔
2060
        default:
×
2061
                return false, err
×
2062
        }
2063

2064
        preimageAvailable = invoice.Terms.PaymentPreimage != nil
3✔
2065

3✔
2066
        return preimageAvailable, nil
3✔
2067
}
2068

2069
// checkLocalChainActions is similar to checkCommitChainActions, but it also
2070
// examines the set of HTLCs on the remote party's commitment. This allows us
2071
// to ensure we're able to satisfy the HTLC timeout constraints for incoming vs
2072
// outgoing HTLCs.
2073
func (c *ChannelArbitrator) checkLocalChainActions(
2074
        height uint32, trigger transitionTrigger,
2075
        activeHTLCs map[HtlcSetKey]htlcSet,
2076
        commitsConfirmed bool) (ChainActionMap, error) {
3✔
2077

3✔
2078
        // First, we'll check our local chain actions as normal. This will only
3✔
2079
        // examine HTLCs on our local commitment (timeout or settle).
3✔
2080
        localCommitActions, err := c.checkCommitChainActions(
3✔
2081
                height, trigger, activeHTLCs[LocalHtlcSet],
3✔
2082
        )
3✔
2083
        if err != nil {
3✔
2084
                return nil, err
×
2085
        }
×
2086

2087
        // Next, we'll examine the remote commitment (and maybe a dangling one)
2088
        // to see if the set difference of our HTLCs is non-empty. If so, then
2089
        // we may need to cancel back some HTLCs if we decide go to chain.
2090
        remoteDanglingActions := c.checkRemoteDanglingActions(
3✔
2091
                height, activeHTLCs, commitsConfirmed,
3✔
2092
        )
3✔
2093

3✔
2094
        // Finally, we'll merge the two set of chain actions.
3✔
2095
        localCommitActions.Merge(remoteDanglingActions)
3✔
2096

3✔
2097
        return localCommitActions, nil
3✔
2098
}
2099

2100
// checkRemoteDanglingActions examines the set of remote commitments for any
2101
// HTLCs that are close to timing out. If we find any, then we'll return a set
2102
// of chain actions for HTLCs that are on our commitment, but not theirs to
2103
// cancel immediately.
2104
func (c *ChannelArbitrator) checkRemoteDanglingActions(
2105
        height uint32, activeHTLCs map[HtlcSetKey]htlcSet,
2106
        commitsConfirmed bool) ChainActionMap {
3✔
2107

3✔
2108
        var (
3✔
2109
                pendingRemoteHTLCs []channeldb.HTLC
3✔
2110
                localHTLCs         = make(map[uint64]struct{})
3✔
2111
                remoteHTLCs        = make(map[uint64]channeldb.HTLC)
3✔
2112
                actionMap          = make(ChainActionMap)
3✔
2113
        )
3✔
2114

3✔
2115
        // First, we'll construct two sets of the outgoing HTLCs: those on our
3✔
2116
        // local commitment, and those that are on the remote commitment(s).
3✔
2117
        for htlcSetKey, htlcs := range activeHTLCs {
6✔
2118
                if htlcSetKey.IsRemote {
6✔
2119
                        for _, htlc := range htlcs.outgoingHTLCs {
6✔
2120
                                remoteHTLCs[htlc.HtlcIndex] = htlc
3✔
2121
                        }
3✔
2122
                } else {
3✔
2123
                        for _, htlc := range htlcs.outgoingHTLCs {
6✔
2124
                                localHTLCs[htlc.HtlcIndex] = struct{}{}
3✔
2125
                        }
3✔
2126
                }
2127
        }
2128

2129
        // With both sets constructed, we'll now compute the set difference of
2130
        // our two sets of HTLCs. This'll give us the HTLCs that exist on the
2131
        // remote commitment transaction, but not on ours.
2132
        for htlcIndex, htlc := range remoteHTLCs {
6✔
2133
                if _, ok := localHTLCs[htlcIndex]; ok {
6✔
2134
                        continue
3✔
2135
                }
2136

2137
                pendingRemoteHTLCs = append(pendingRemoteHTLCs, htlc)
3✔
2138
        }
2139

2140
        // Finally, we'll examine all the pending remote HTLCs for those that
2141
        // have expired. If we find any, then we'll recommend that they be
2142
        // failed now so we can free up the incoming HTLC.
2143
        for _, htlc := range pendingRemoteHTLCs {
6✔
2144
                // We'll now check if we need to go to chain in order to cancel
3✔
2145
                // the incoming HTLC.
3✔
2146
                goToChain := c.shouldGoOnChain(htlc, c.cfg.OutgoingBroadcastDelta,
3✔
2147
                        height,
3✔
2148
                )
3✔
2149

3✔
2150
                // If we don't need to go to chain, and no commitments have
3✔
2151
                // been confirmed, then we can move on. Otherwise, if
3✔
2152
                // commitments have been confirmed, then we need to cancel back
3✔
2153
                // *all* of the pending remote HTLCS.
3✔
2154
                if !goToChain && !commitsConfirmed {
6✔
2155
                        continue
3✔
2156
                }
2157

2158
                preimageAvailable, err := c.isPreimageAvailable(htlc.RHash)
3✔
2159
                if err != nil {
3✔
2160
                        log.Errorf("ChannelArbitrator(%v): failed to query "+
×
2161
                                "preimage for dangling htlc=%x from remote "+
×
2162
                                "commitments diff", c.cfg.ChanPoint,
×
2163
                                htlc.RHash[:])
×
2164

×
2165
                        continue
×
2166
                }
2167

2168
                if preimageAvailable {
3✔
2169
                        continue
×
2170
                }
2171

2172
                // Dust htlcs can be canceled back even before the commitment
2173
                // transaction confirms. Dust htlcs are not enforceable onchain.
2174
                // If another version of the commit tx would confirm we either
2175
                // gain or lose those dust amounts but there is no other way
2176
                // than cancelling the incoming back because we will never learn
2177
                // the preimage.
2178
                if htlc.OutputIndex < 0 {
3✔
2179
                        log.Infof("ChannelArbitrator(%v): fail dangling dust "+
×
2180
                                "htlc=%x from local/remote commitments diff",
×
2181
                                c.cfg.ChanPoint, htlc.RHash[:])
×
2182

×
2183
                        actionMap[HtlcFailDustAction] = append(
×
2184
                                actionMap[HtlcFailDustAction], htlc,
×
2185
                        )
×
2186

×
2187
                        continue
×
2188
                }
2189

2190
                log.Infof("ChannelArbitrator(%v): fail dangling htlc=%x from "+
3✔
2191
                        "local/remote commitments diff",
3✔
2192
                        c.cfg.ChanPoint, htlc.RHash[:])
3✔
2193

3✔
2194
                actionMap[HtlcFailDanglingAction] = append(
3✔
2195
                        actionMap[HtlcFailDanglingAction], htlc,
3✔
2196
                )
3✔
2197
        }
2198

2199
        return actionMap
3✔
2200
}
2201

2202
// checkRemoteChainActions examines the two possible remote commitment chains
2203
// and returns the set of chain actions we need to carry out if the remote
2204
// commitment (non pending) confirms. The pendingConf indicates if the pending
2205
// remote commitment confirmed. This is similar to checkCommitChainActions, but
2206
// we'll immediately fail any HTLCs on the pending remote commit, but not the
2207
// remote commit (or the other way around).
2208
func (c *ChannelArbitrator) checkRemoteChainActions(
2209
        height uint32, trigger transitionTrigger,
2210
        activeHTLCs map[HtlcSetKey]htlcSet,
2211
        pendingConf bool) (ChainActionMap, error) {
3✔
2212

3✔
2213
        // First, we'll examine all the normal chain actions on the remote
3✔
2214
        // commitment that confirmed.
3✔
2215
        confHTLCs := activeHTLCs[RemoteHtlcSet]
3✔
2216
        if pendingConf {
3✔
2217
                confHTLCs = activeHTLCs[RemotePendingHtlcSet]
×
2218
        }
×
2219
        remoteCommitActions, err := c.checkCommitChainActions(
3✔
2220
                height, trigger, confHTLCs,
3✔
2221
        )
3✔
2222
        if err != nil {
3✔
2223
                return nil, err
×
2224
        }
×
2225

2226
        // With these actions computed, we'll now check the diff of the HTLCs on
2227
        // the commitments, and cancel back any that are on the pending but not
2228
        // the non-pending.
2229
        remoteDiffActions := c.checkRemoteDiffActions(
3✔
2230
                activeHTLCs, pendingConf,
3✔
2231
        )
3✔
2232

3✔
2233
        // Finally, we'll merge all the chain actions and the final set of
3✔
2234
        // chain actions.
3✔
2235
        remoteCommitActions.Merge(remoteDiffActions)
3✔
2236
        return remoteCommitActions, nil
3✔
2237
}
2238

2239
// checkRemoteDiffActions checks the set difference of the HTLCs on the remote
2240
// confirmed commit and remote pending commit for HTLCS that we need to cancel
2241
// back. If we find any HTLCs on the remote pending but not the remote, then
2242
// we'll mark them to be failed immediately.
2243
func (c *ChannelArbitrator) checkRemoteDiffActions(
2244
        activeHTLCs map[HtlcSetKey]htlcSet,
2245
        pendingConf bool) ChainActionMap {
3✔
2246

3✔
2247
        // First, we'll partition the HTLCs into those that are present on the
3✔
2248
        // confirmed commitment, and those on the dangling commitment.
3✔
2249
        confHTLCs := activeHTLCs[RemoteHtlcSet]
3✔
2250
        danglingHTLCs := activeHTLCs[RemotePendingHtlcSet]
3✔
2251
        if pendingConf {
3✔
2252
                confHTLCs = activeHTLCs[RemotePendingHtlcSet]
×
2253
                danglingHTLCs = activeHTLCs[RemoteHtlcSet]
×
2254
        }
×
2255

2256
        // Next, we'll create a set of all the HTLCs confirmed commitment.
2257
        remoteHtlcs := make(map[uint64]struct{})
3✔
2258
        for _, htlc := range confHTLCs.outgoingHTLCs {
6✔
2259
                remoteHtlcs[htlc.HtlcIndex] = struct{}{}
3✔
2260
        }
3✔
2261

2262
        // With the remote HTLCs assembled, we'll mark any HTLCs only on the
2263
        // remote pending commitment to be failed asap.
2264
        actionMap := make(ChainActionMap)
3✔
2265
        for _, htlc := range danglingHTLCs.outgoingHTLCs {
4✔
2266
                if _, ok := remoteHtlcs[htlc.HtlcIndex]; ok {
2✔
2267
                        continue
1✔
2268
                }
2269

2270
                preimageAvailable, err := c.isPreimageAvailable(htlc.RHash)
×
2271
                if err != nil {
×
2272
                        log.Errorf("ChannelArbitrator(%v): failed to query "+
×
2273
                                "preimage for dangling htlc=%x from remote "+
×
2274
                                "commitments diff", c.cfg.ChanPoint,
×
2275
                                htlc.RHash[:])
×
2276

×
2277
                        continue
×
2278
                }
2279

2280
                if preimageAvailable {
×
2281
                        continue
×
2282
                }
2283

2284
                // Dust HTLCs on the remote commitment can be failed back.
2285
                if htlc.OutputIndex < 0 {
×
2286
                        log.Infof("ChannelArbitrator(%v): fail dangling dust "+
×
2287
                                "htlc=%x from remote commitments diff",
×
2288
                                c.cfg.ChanPoint, htlc.RHash[:])
×
2289

×
2290
                        actionMap[HtlcFailDustAction] = append(
×
2291
                                actionMap[HtlcFailDustAction], htlc,
×
2292
                        )
×
2293

×
2294
                        continue
×
2295
                }
2296

2297
                actionMap[HtlcFailDanglingAction] = append(
×
2298
                        actionMap[HtlcFailDanglingAction], htlc,
×
2299
                )
×
2300

×
2301
                log.Infof("ChannelArbitrator(%v): fail dangling htlc=%x from "+
×
2302
                        "remote commitments diff",
×
2303
                        c.cfg.ChanPoint, htlc.RHash[:])
×
2304
        }
2305

2306
        return actionMap
3✔
2307
}
2308

2309
// constructChainActions returns the set of actions that should be taken for
2310
// confirmed HTLCs at the specified height. Our actions will depend on the set
2311
// of HTLCs that were active across all channels at the time of channel
2312
// closure.
2313
func (c *ChannelArbitrator) constructChainActions(confCommitSet *CommitSet,
2314
        height uint32, trigger transitionTrigger) (ChainActionMap, error) {
3✔
2315

3✔
2316
        // If we've reached this point and have not confirmed commitment set,
3✔
2317
        // then this is an older node that had a pending close channel before
3✔
2318
        // the CommitSet was introduced. In this case, we'll just return the
3✔
2319
        // existing ChainActionMap they had on disk.
3✔
2320
        if confCommitSet == nil || confCommitSet.ConfCommitKey.IsNone() {
3✔
2321
                return c.log.FetchChainActions()
×
2322
        }
×
2323

2324
        // Otherwise, we have the full commitment set written to disk, and can
2325
        // proceed as normal.
2326
        htlcSets := confCommitSet.toActiveHTLCSets()
3✔
2327
        confCommitKey, err := confCommitSet.ConfCommitKey.UnwrapOrErr(
3✔
2328
                fmt.Errorf("no commitKey available"),
3✔
2329
        )
3✔
2330
        if err != nil {
3✔
2331
                return nil, err
×
2332
        }
×
2333

2334
        switch confCommitKey {
3✔
2335
        // If the local commitment transaction confirmed, then we'll examine
2336
        // that as well as their commitments to the set of chain actions.
2337
        case LocalHtlcSet:
3✔
2338
                return c.checkLocalChainActions(
3✔
2339
                        height, trigger, htlcSets, true,
3✔
2340
                )
3✔
2341

2342
        // If the remote commitment confirmed, then we'll grab all the chain
2343
        // actions for the remote commit, and check the pending commit for any
2344
        // HTLCS we need to handle immediately (dust).
2345
        case RemoteHtlcSet:
3✔
2346
                return c.checkRemoteChainActions(
3✔
2347
                        height, trigger, htlcSets, false,
3✔
2348
                )
3✔
2349

2350
        // Otherwise, the remote pending commitment confirmed, so we'll examine
2351
        // the HTLCs on that unrevoked dangling commitment.
2352
        case RemotePendingHtlcSet:
×
2353
                return c.checkRemoteChainActions(
×
2354
                        height, trigger, htlcSets, true,
×
2355
                )
×
2356
        }
2357

2358
        return nil, fmt.Errorf("unable to locate chain actions")
×
2359
}
2360

2361
// prepContractResolutions is called either in the case that we decide we need
2362
// to go to chain, or the remote party goes to chain. Given a set of actions we
2363
// need to take for each HTLC, this method will return a set of contract
2364
// resolvers that will resolve the contracts on-chain if needed, and also a set
2365
// of packets to send to the htlcswitch in order to ensure all incoming HTLC's
2366
// are properly resolved.
2367
func (c *ChannelArbitrator) prepContractResolutions(
2368
        contractResolutions *ContractResolutions, height uint32,
2369
        htlcActions ChainActionMap) ([]ContractResolver, error) {
3✔
2370

3✔
2371
        // We'll also fetch the historical state of this channel, as it should
3✔
2372
        // have been marked as closed by now, and supplement it to each resolver
3✔
2373
        // such that we can properly resolve our pending contracts.
3✔
2374
        var chanState *channeldb.OpenChannel
3✔
2375
        chanState, err := c.cfg.FetchHistoricalChannel()
3✔
2376
        switch {
3✔
2377
        // If we don't find this channel, then it may be the case that it
2378
        // was closed before we started to retain the final state
2379
        // information for open channels.
2380
        case err == channeldb.ErrNoHistoricalBucket:
×
2381
                fallthrough
×
2382
        case err == channeldb.ErrChannelNotFound:
×
2383
                log.Warnf("ChannelArbitrator(%v): unable to fetch historical "+
×
2384
                        "state", c.cfg.ChanPoint)
×
2385

2386
        case err != nil:
×
2387
                return nil, err
×
2388
        }
2389

2390
        incomingResolutions := contractResolutions.HtlcResolutions.IncomingHTLCs
3✔
2391
        outgoingResolutions := contractResolutions.HtlcResolutions.OutgoingHTLCs
3✔
2392

3✔
2393
        // We'll use these two maps to quickly look up an active HTLC with its
3✔
2394
        // matching HTLC resolution.
3✔
2395
        outResolutionMap := make(map[wire.OutPoint]lnwallet.OutgoingHtlcResolution)
3✔
2396
        inResolutionMap := make(map[wire.OutPoint]lnwallet.IncomingHtlcResolution)
3✔
2397
        for i := 0; i < len(incomingResolutions); i++ {
6✔
2398
                inRes := incomingResolutions[i]
3✔
2399
                inResolutionMap[inRes.HtlcPoint()] = inRes
3✔
2400
        }
3✔
2401
        for i := 0; i < len(outgoingResolutions); i++ {
6✔
2402
                outRes := outgoingResolutions[i]
3✔
2403
                outResolutionMap[outRes.HtlcPoint()] = outRes
3✔
2404
        }
3✔
2405

2406
        // We'll create the resolver kit that we'll be cloning for each
2407
        // resolver so they each can do their duty.
2408
        resolverCfg := ResolverConfig{
3✔
2409
                ChannelArbitratorConfig: c.cfg,
3✔
2410
                Checkpoint: func(res ContractResolver,
3✔
2411
                        reports ...*channeldb.ResolverReport) error {
6✔
2412

3✔
2413
                        return c.log.InsertUnresolvedContracts(reports, res)
3✔
2414
                },
3✔
2415
        }
2416

2417
        commitHash := contractResolutions.CommitHash
3✔
2418

3✔
2419
        var htlcResolvers []ContractResolver
3✔
2420

3✔
2421
        // We instantiate an anchor resolver if the commitment tx has an
3✔
2422
        // anchor.
3✔
2423
        if contractResolutions.AnchorResolution != nil {
6✔
2424
                anchorResolver := newAnchorResolver(
3✔
2425
                        contractResolutions.AnchorResolution.AnchorSignDescriptor,
3✔
2426
                        contractResolutions.AnchorResolution.CommitAnchor,
3✔
2427
                        height, c.cfg.ChanPoint, resolverCfg,
3✔
2428
                )
3✔
2429
                anchorResolver.SupplementState(chanState)
3✔
2430

3✔
2431
                htlcResolvers = append(htlcResolvers, anchorResolver)
3✔
2432
        }
3✔
2433

2434
        // If this is a breach close, we'll create a breach resolver, determine
2435
        // the htlc's to fail back, and exit. This is done because the other
2436
        // steps taken for non-breach-closes do not matter for breach-closes.
2437
        if contractResolutions.BreachResolution != nil {
6✔
2438
                breachResolver := newBreachResolver(resolverCfg)
3✔
2439
                htlcResolvers = append(htlcResolvers, breachResolver)
3✔
2440

3✔
2441
                return htlcResolvers, nil
3✔
2442
        }
3✔
2443

2444
        // For each HTLC, we'll either act immediately, meaning we'll instantly
2445
        // fail the HTLC, or we'll act only once the transaction has been
2446
        // confirmed, in which case we'll need an HTLC resolver.
2447
        for htlcAction, htlcs := range htlcActions {
6✔
2448
                switch htlcAction {
3✔
2449
                // If we can claim this HTLC, we'll create an HTLC resolver to
2450
                // claim the HTLC (second-level or directly), then add the pre
2451
                case HtlcClaimAction:
×
2452
                        for _, htlc := range htlcs {
×
2453
                                htlc := htlc
×
2454

×
2455
                                htlcOp := wire.OutPoint{
×
2456
                                        Hash:  commitHash,
×
2457
                                        Index: uint32(htlc.OutputIndex),
×
2458
                                }
×
2459

×
2460
                                resolution, ok := inResolutionMap[htlcOp]
×
2461
                                if !ok {
×
2462
                                        // TODO(roasbeef): panic?
×
2463
                                        log.Errorf("ChannelArbitrator(%v) unable to find "+
×
2464
                                                "incoming resolution: %v",
×
2465
                                                c.cfg.ChanPoint, htlcOp)
×
2466
                                        continue
×
2467
                                }
2468

2469
                                resolver := newSuccessResolver(
×
2470
                                        resolution, height, htlc, resolverCfg,
×
2471
                                )
×
2472
                                if chanState != nil {
×
2473
                                        resolver.SupplementState(chanState)
×
2474
                                }
×
2475
                                htlcResolvers = append(htlcResolvers, resolver)
×
2476
                        }
2477

2478
                // If we can timeout the HTLC directly, then we'll create the
2479
                // proper resolver to do so, who will then cancel the packet
2480
                // backwards.
2481
                case HtlcTimeoutAction:
3✔
2482
                        for _, htlc := range htlcs {
6✔
2483
                                htlc := htlc
3✔
2484

3✔
2485
                                htlcOp := wire.OutPoint{
3✔
2486
                                        Hash:  commitHash,
3✔
2487
                                        Index: uint32(htlc.OutputIndex),
3✔
2488
                                }
3✔
2489

3✔
2490
                                resolution, ok := outResolutionMap[htlcOp]
3✔
2491
                                if !ok {
3✔
2492
                                        log.Errorf("ChannelArbitrator(%v) unable to find "+
×
2493
                                                "outgoing resolution: %v", c.cfg.ChanPoint, htlcOp)
×
2494
                                        continue
×
2495
                                }
2496

2497
                                resolver := newTimeoutResolver(
3✔
2498
                                        resolution, height, htlc, resolverCfg,
3✔
2499
                                )
3✔
2500
                                if chanState != nil {
6✔
2501
                                        resolver.SupplementState(chanState)
3✔
2502
                                }
3✔
2503

2504
                                // For outgoing HTLCs, we will also need to
2505
                                // supplement the resolver with the expiry
2506
                                // block height of its corresponding incoming
2507
                                // HTLC.
2508
                                deadline := c.cfg.FindOutgoingHTLCDeadline(htlc)
3✔
2509
                                resolver.SupplementDeadline(deadline)
3✔
2510

3✔
2511
                                htlcResolvers = append(htlcResolvers, resolver)
3✔
2512
                        }
2513

2514
                // If this is an incoming HTLC, but we can't act yet, then
2515
                // we'll create an incoming resolver to redeem the HTLC if we
2516
                // learn of the pre-image, or let the remote party time out.
2517
                case HtlcIncomingWatchAction:
3✔
2518
                        for _, htlc := range htlcs {
6✔
2519
                                htlc := htlc
3✔
2520

3✔
2521
                                htlcOp := wire.OutPoint{
3✔
2522
                                        Hash:  commitHash,
3✔
2523
                                        Index: uint32(htlc.OutputIndex),
3✔
2524
                                }
3✔
2525

3✔
2526
                                // TODO(roasbeef): need to handle incoming dust...
3✔
2527

3✔
2528
                                // TODO(roasbeef): can't be negative!!!
3✔
2529
                                resolution, ok := inResolutionMap[htlcOp]
3✔
2530
                                if !ok {
3✔
2531
                                        log.Errorf("ChannelArbitrator(%v) unable to find "+
×
2532
                                                "incoming resolution: %v",
×
2533
                                                c.cfg.ChanPoint, htlcOp)
×
2534
                                        continue
×
2535
                                }
2536

2537
                                resolver := newIncomingContestResolver(
3✔
2538
                                        resolution, height, htlc,
3✔
2539
                                        resolverCfg,
3✔
2540
                                )
3✔
2541
                                if chanState != nil {
6✔
2542
                                        resolver.SupplementState(chanState)
3✔
2543
                                }
3✔
2544
                                htlcResolvers = append(htlcResolvers, resolver)
3✔
2545
                        }
2546

2547
                // Finally, if this is an outgoing HTLC we've sent, then we'll
2548
                // launch a resolver to watch for the pre-image (and settle
2549
                // backwards), or just timeout.
2550
                case HtlcOutgoingWatchAction:
3✔
2551
                        for _, htlc := range htlcs {
6✔
2552
                                htlc := htlc
3✔
2553

3✔
2554
                                htlcOp := wire.OutPoint{
3✔
2555
                                        Hash:  commitHash,
3✔
2556
                                        Index: uint32(htlc.OutputIndex),
3✔
2557
                                }
3✔
2558

3✔
2559
                                resolution, ok := outResolutionMap[htlcOp]
3✔
2560
                                if !ok {
3✔
2561
                                        log.Errorf("ChannelArbitrator(%v) "+
×
2562
                                                "unable to find outgoing "+
×
2563
                                                "resolution: %v",
×
2564
                                                c.cfg.ChanPoint, htlcOp)
×
2565

×
2566
                                        continue
×
2567
                                }
2568

2569
                                resolver := newOutgoingContestResolver(
3✔
2570
                                        resolution, height, htlc, resolverCfg,
3✔
2571
                                )
3✔
2572
                                if chanState != nil {
6✔
2573
                                        resolver.SupplementState(chanState)
3✔
2574
                                }
3✔
2575

2576
                                // For outgoing HTLCs, we will also need to
2577
                                // supplement the resolver with the expiry
2578
                                // block height of its corresponding incoming
2579
                                // HTLC.
2580
                                deadline := c.cfg.FindOutgoingHTLCDeadline(htlc)
3✔
2581
                                resolver.SupplementDeadline(deadline)
3✔
2582

3✔
2583
                                htlcResolvers = append(htlcResolvers, resolver)
3✔
2584
                        }
2585
                }
2586
        }
2587

2588
        // If this is was an unilateral closure, then we'll also create a
2589
        // resolver to sweep our commitment output (but only if it wasn't
2590
        // trimmed).
2591
        if contractResolutions.CommitResolution != nil {
6✔
2592
                resolver := newCommitSweepResolver(
3✔
2593
                        *contractResolutions.CommitResolution, height,
3✔
2594
                        c.cfg.ChanPoint, resolverCfg,
3✔
2595
                )
3✔
2596
                if chanState != nil {
6✔
2597
                        resolver.SupplementState(chanState)
3✔
2598
                }
3✔
2599
                htlcResolvers = append(htlcResolvers, resolver)
3✔
2600
        }
2601

2602
        return htlcResolvers, nil
3✔
2603
}
2604

2605
// replaceResolver replaces a in the list of active resolvers. If the resolver
2606
// to be replaced is not found, it returns an error.
2607
func (c *ChannelArbitrator) replaceResolver(oldResolver,
2608
        newResolver ContractResolver) error {
3✔
2609

3✔
2610
        c.activeResolversLock.Lock()
3✔
2611
        defer c.activeResolversLock.Unlock()
3✔
2612

3✔
2613
        oldKey := oldResolver.ResolverKey()
3✔
2614
        for i, r := range c.activeResolvers {
6✔
2615
                if bytes.Equal(r.ResolverKey(), oldKey) {
6✔
2616
                        c.activeResolvers[i] = newResolver
3✔
2617
                        return nil
3✔
2618
                }
3✔
2619
        }
2620

2621
        return errors.New("resolver to be replaced not found")
×
2622
}
2623

2624
// resolveContract is a goroutine tasked with fully resolving an unresolved
2625
// contract. Either the initial contract will be resolved after a single step,
2626
// or the contract will itself create another contract to be resolved. In
2627
// either case, one the contract has been fully resolved, we'll signal back to
2628
// the main goroutine so it can properly keep track of the set of unresolved
2629
// contracts.
2630
//
2631
// NOTE: This MUST be run as a goroutine.
2632
func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver) {
3✔
2633
        defer c.wg.Done()
3✔
2634

3✔
2635
        log.Tracef("ChannelArbitrator(%v): attempting to resolve %T",
3✔
2636
                c.cfg.ChanPoint, currentContract)
3✔
2637

3✔
2638
        // Until the contract is fully resolved, we'll continue to iteratively
3✔
2639
        // resolve the contract one step at a time.
3✔
2640
        for !currentContract.IsResolved() {
6✔
2641
                log.Tracef("ChannelArbitrator(%v): contract %T not yet "+
3✔
2642
                        "resolved", c.cfg.ChanPoint, currentContract)
3✔
2643

3✔
2644
                select {
3✔
2645

2646
                // If we've been signalled to quit, then we'll exit early.
2647
                case <-c.quit:
×
2648
                        return
×
2649

2650
                default:
3✔
2651
                        // Otherwise, we'll attempt to resolve the current
3✔
2652
                        // contract.
3✔
2653
                        nextContract, err := currentContract.Resolve()
3✔
2654
                        if err != nil {
6✔
2655
                                if err == errResolverShuttingDown {
6✔
2656
                                        return
3✔
2657
                                }
3✔
2658

2659
                                log.Errorf("ChannelArbitrator(%v): unable to "+
3✔
2660
                                        "progress %T: %v",
3✔
2661
                                        c.cfg.ChanPoint, currentContract, err)
3✔
2662
                                return
3✔
2663
                        }
2664

2665
                        switch {
3✔
2666
                        // If this contract produced another, then this means
2667
                        // the current contract was only able to be partially
2668
                        // resolved in this step. So we'll do a contract swap
2669
                        // within our logs: the new contract will take the
2670
                        // place of the old one.
2671
                        case nextContract != nil:
3✔
2672
                                log.Debugf("ChannelArbitrator(%v): swapping "+
3✔
2673
                                        "out contract %T for %T ",
3✔
2674
                                        c.cfg.ChanPoint, currentContract,
3✔
2675
                                        nextContract)
3✔
2676

3✔
2677
                                // Swap contract in log.
3✔
2678
                                err := c.log.SwapContract(
3✔
2679
                                        currentContract, nextContract,
3✔
2680
                                )
3✔
2681
                                if err != nil {
3✔
2682
                                        log.Errorf("unable to add recurse "+
×
2683
                                                "contract: %v", err)
×
2684
                                }
×
2685

2686
                                // Swap contract in resolvers list. This is to
2687
                                // make sure that reports are queried from the
2688
                                // new resolver.
2689
                                err = c.replaceResolver(
3✔
2690
                                        currentContract, nextContract,
3✔
2691
                                )
3✔
2692
                                if err != nil {
3✔
2693
                                        log.Errorf("unable to replace "+
×
2694
                                                "contract: %v", err)
×
2695
                                }
×
2696

2697
                                // As this contract produced another, we'll
2698
                                // re-assign, so we can continue our resolution
2699
                                // loop.
2700
                                currentContract = nextContract
3✔
2701

3✔
2702
                                // Launch the new contract.
3✔
2703
                                err = currentContract.Launch()
3✔
2704
                                if err != nil {
3✔
2705
                                        log.Errorf("Failed to launch %T: %v",
×
2706
                                                currentContract, err)
×
2707
                                }
×
2708

2709
                        // If this contract is actually fully resolved, then
2710
                        // we'll mark it as such within the database.
2711
                        case currentContract.IsResolved():
3✔
2712
                                log.Debugf("ChannelArbitrator(%v): marking "+
3✔
2713
                                        "contract %T fully resolved",
3✔
2714
                                        c.cfg.ChanPoint, currentContract)
3✔
2715

3✔
2716
                                err := c.log.ResolveContract(currentContract)
3✔
2717
                                if err != nil {
3✔
2718
                                        log.Errorf("unable to resolve contract: %v",
×
2719
                                                err)
×
2720
                                }
×
2721

2722
                                // Now that the contract has been resolved,
2723
                                // well signal to the main goroutine.
2724
                                select {
3✔
2725
                                case c.resolutionSignal <- struct{}{}:
3✔
2726
                                case <-c.quit:
3✔
2727
                                        return
3✔
2728
                                }
2729
                        }
2730

2731
                }
2732
        }
2733
}
2734

2735
// signalUpdateMsg is a struct that carries fresh signals to the
2736
// ChannelArbitrator. We need to receive a message like this each time the
2737
// channel becomes active, as it's internal state may change.
2738
type signalUpdateMsg struct {
2739
        // newSignals is the set of new active signals to be sent to the
2740
        // arbitrator.
2741
        newSignals *ContractSignals
2742

2743
        // doneChan is a channel that will be closed on the arbitrator has
2744
        // attached the new signals.
2745
        doneChan chan struct{}
2746
}
2747

2748
// UpdateContractSignals updates the set of signals the ChannelArbitrator needs
2749
// to receive from a channel in real-time in order to keep in sync with the
2750
// latest state of the contract.
2751
func (c *ChannelArbitrator) UpdateContractSignals(newSignals *ContractSignals) {
3✔
2752
        done := make(chan struct{})
3✔
2753

3✔
2754
        select {
3✔
2755
        case c.signalUpdates <- &signalUpdateMsg{
2756
                newSignals: newSignals,
2757
                doneChan:   done,
2758
        }:
3✔
2759
        case <-c.quit:
×
2760
        }
2761

2762
        select {
3✔
2763
        case <-done:
3✔
2764
        case <-c.quit:
×
2765
        }
2766
}
2767

2768
// notifyContractUpdate updates the ChannelArbitrator's unmerged mappings such
2769
// that it can later be merged with activeHTLCs when calling
2770
// checkLocalChainActions or sweepAnchors. These are the only two places that
2771
// activeHTLCs is used.
2772
func (c *ChannelArbitrator) notifyContractUpdate(upd *ContractUpdate) {
3✔
2773
        c.unmergedMtx.Lock()
3✔
2774
        defer c.unmergedMtx.Unlock()
3✔
2775

3✔
2776
        // Update the mapping.
3✔
2777
        c.unmergedSet[upd.HtlcKey] = newHtlcSet(upd.Htlcs)
3✔
2778

3✔
2779
        log.Tracef("ChannelArbitrator(%v): fresh set of htlcs=%v",
3✔
2780
                c.cfg.ChanPoint, lnutils.SpewLogClosure(upd))
3✔
2781
}
3✔
2782

2783
// updateActiveHTLCs merges the unmerged set of HTLCs from the link with
2784
// activeHTLCs.
2785
func (c *ChannelArbitrator) updateActiveHTLCs() {
3✔
2786
        c.unmergedMtx.RLock()
3✔
2787
        defer c.unmergedMtx.RUnlock()
3✔
2788

3✔
2789
        // Update the mapping.
3✔
2790
        c.activeHTLCs[LocalHtlcSet] = c.unmergedSet[LocalHtlcSet]
3✔
2791
        c.activeHTLCs[RemoteHtlcSet] = c.unmergedSet[RemoteHtlcSet]
3✔
2792

3✔
2793
        // If the pending set exists, update that as well.
3✔
2794
        if _, ok := c.unmergedSet[RemotePendingHtlcSet]; ok {
6✔
2795
                pendingSet := c.unmergedSet[RemotePendingHtlcSet]
3✔
2796
                c.activeHTLCs[RemotePendingHtlcSet] = pendingSet
3✔
2797
        }
3✔
2798
}
2799

2800
// channelAttendant is the primary goroutine that acts at the judicial
2801
// arbitrator between our channel state, the remote channel peer, and the
2802
// blockchain (Our judge). This goroutine will ensure that we faithfully execute
2803
// all clauses of our contract in the case that we need to go on-chain for a
2804
// dispute. Currently, two such conditions warrant our intervention: when an
2805
// outgoing HTLC is about to timeout, and when we know the pre-image for an
2806
// incoming HTLC, but it hasn't yet been settled off-chain. In these cases,
2807
// we'll: broadcast our commitment, cancel/settle any HTLC's backwards after
2808
// sufficient confirmation, and finally send our set of outputs to the UTXO
2809
// Nursery for incubation, and ultimate sweeping.
2810
//
2811
// NOTE: This MUST be run as a goroutine.
2812
func (c *ChannelArbitrator) channelAttendant(bestHeight int32,
2813
        commitSet *CommitSet) {
3✔
2814

3✔
2815
        // TODO(roasbeef): tell top chain arb we're done
3✔
2816
        defer func() {
6✔
2817
                c.wg.Done()
3✔
2818
        }()
3✔
2819

2820
        err := c.progressStateMachineAfterRestart(bestHeight, commitSet)
3✔
2821
        if err != nil {
3✔
2822
                // In case of an error, we return early but we do not shutdown
×
2823
                // LND, because there might be other channels that still can be
×
2824
                // resolved and we don't want to interfere with that.
×
2825
                // We continue to run the channel attendant in case the channel
×
2826
                // closes via other means for example the remote pary force
×
2827
                // closes the channel. So we log the error and continue.
×
2828
                log.Errorf("Unable to progress state machine after "+
×
2829
                        "restart: %v", err)
×
2830
        }
×
2831

2832
        for {
6✔
2833
                select {
3✔
2834

2835
                // A new block has arrived, we'll examine all the active HTLC's
2836
                // to see if any of them have expired, and also update our
2837
                // track of the best current height.
2838
                case beat := <-c.BlockbeatChan:
3✔
2839
                        bestHeight = beat.Height()
3✔
2840

3✔
2841
                        log.Debugf("ChannelArbitrator(%v): received new "+
3✔
2842
                                "block: height=%v, processing...",
3✔
2843
                                c.cfg.ChanPoint, bestHeight)
3✔
2844

3✔
2845
                        err := c.handleBlockbeat(beat)
3✔
2846
                        if err != nil {
3✔
2847
                                log.Errorf("Handle block=%v got err: %v",
×
2848
                                        bestHeight, err)
×
2849
                        }
×
2850

2851
                        // If as a result of this trigger, the contract is
2852
                        // fully resolved, then well exit.
2853
                        if c.state == StateFullyResolved {
3✔
2854
                                return
×
2855
                        }
×
2856

2857
                // A new signal update was just sent. This indicates that the
2858
                // channel under watch is now live, and may modify its internal
2859
                // state, so we'll get the most up to date signals to we can
2860
                // properly do our job.
2861
                case signalUpdate := <-c.signalUpdates:
3✔
2862
                        log.Tracef("ChannelArbitrator(%v): got new signal "+
3✔
2863
                                "update!", c.cfg.ChanPoint)
3✔
2864

3✔
2865
                        // We'll update the ShortChannelID.
3✔
2866
                        c.cfg.ShortChanID = signalUpdate.newSignals.ShortChanID
3✔
2867

3✔
2868
                        // Now that the signal has been updated, we'll now
3✔
2869
                        // close the done channel to signal to the caller we've
3✔
2870
                        // registered the new ShortChannelID.
3✔
2871
                        close(signalUpdate.doneChan)
3✔
2872

2873
                // We've cooperatively closed the channel, so we're no longer
2874
                // needed. We'll mark the channel as resolved and exit.
2875
                case closeInfo := <-c.cfg.ChainEvents.CooperativeClosure:
3✔
2876
                        err := c.handleCoopCloseEvent(closeInfo)
3✔
2877
                        if err != nil {
3✔
2878
                                log.Errorf("Failed to handle coop close: %v",
×
2879
                                        err)
×
2880

×
2881
                                return
×
2882
                        }
×
2883

2884
                // We have broadcasted our commitment, and it is now confirmed
2885
                // on-chain.
2886
                case closeInfo := <-c.cfg.ChainEvents.LocalUnilateralClosure:
3✔
2887
                        if c.state != StateCommitmentBroadcasted {
6✔
2888
                                log.Errorf("ChannelArbitrator(%v): unexpected "+
3✔
2889
                                        "local on-chain channel close",
3✔
2890
                                        c.cfg.ChanPoint)
3✔
2891
                        }
3✔
2892

2893
                        err := c.handleLocalForceCloseEvent(closeInfo)
3✔
2894
                        if err != nil {
3✔
2895
                                log.Errorf("Failed to handle local force "+
×
2896
                                        "close: %v", err)
×
2897

×
2898
                                return
×
2899
                        }
×
2900

2901
                // The remote party has broadcast the commitment on-chain.
2902
                // We'll examine our state to determine if we need to act at
2903
                // all.
2904
                case uniClosure := <-c.cfg.ChainEvents.RemoteUnilateralClosure:
3✔
2905
                        err := c.handleRemoteForceCloseEvent(uniClosure)
3✔
2906
                        if err != nil {
3✔
2907
                                log.Errorf("Failed to handle remote force "+
×
2908
                                        "close: %v", err)
×
2909

×
2910
                                return
×
2911
                        }
×
2912

2913
                // The remote has breached the channel. As this is handled by
2914
                // the ChainWatcher and BreachArbitrator, we don't have to do
2915
                // anything in particular, so just advance our state and
2916
                // gracefully exit.
2917
                case breachInfo := <-c.cfg.ChainEvents.ContractBreach:
3✔
2918
                        err := c.handleContractBreach(breachInfo)
3✔
2919
                        if err != nil {
3✔
2920
                                log.Errorf("Failed to handle contract breach: "+
×
2921
                                        "%v", err)
×
2922

×
2923
                                return
×
2924
                        }
×
2925

2926
                // A new contract has just been resolved, we'll now check our
2927
                // log to see if all contracts have been resolved. If so, then
2928
                // we can exit as the contract is fully resolved.
2929
                case <-c.resolutionSignal:
3✔
2930
                        log.Infof("ChannelArbitrator(%v): a contract has been "+
3✔
2931
                                "fully resolved!", c.cfg.ChanPoint)
3✔
2932

3✔
2933
                        nextState, _, err := c.advanceState(
3✔
2934
                                uint32(bestHeight), chainTrigger, nil,
3✔
2935
                        )
3✔
2936
                        if err != nil {
3✔
2937
                                log.Errorf("Unable to advance state: %v", err)
×
2938
                        }
×
2939

2940
                        // If we don't have anything further to do after
2941
                        // advancing our state, then we'll exit.
2942
                        if nextState == StateFullyResolved {
3✔
2943
                                log.Infof("ChannelArbitrator(%v): all "+
×
2944
                                        "contracts fully resolved, exiting",
×
2945
                                        c.cfg.ChanPoint)
×
2946

×
2947
                                return
×
2948
                        }
×
2949

2950
                // We've just received a request to forcibly close out the
2951
                // channel. We'll
2952
                case closeReq := <-c.forceCloseReqs:
3✔
2953
                        log.Infof("ChannelArbitrator(%v): received force "+
3✔
2954
                                "close request", c.cfg.ChanPoint)
3✔
2955

3✔
2956
                        if c.state != StateDefault {
4✔
2957
                                select {
1✔
2958
                                case closeReq.closeTx <- nil:
1✔
2959
                                case <-c.quit:
×
2960
                                }
2961

2962
                                select {
1✔
2963
                                case closeReq.errResp <- errAlreadyForceClosed:
1✔
2964
                                case <-c.quit:
×
2965
                                }
2966

2967
                                continue
1✔
2968
                        }
2969

2970
                        nextState, closeTx, err := c.advanceState(
3✔
2971
                                uint32(bestHeight), userTrigger, nil,
3✔
2972
                        )
3✔
2973
                        if err != nil {
6✔
2974
                                log.Errorf("Unable to advance state: %v", err)
3✔
2975
                        }
3✔
2976

2977
                        select {
3✔
2978
                        case closeReq.closeTx <- closeTx:
3✔
2979
                        case <-c.quit:
×
2980
                                return
×
2981
                        }
2982

2983
                        select {
3✔
2984
                        case closeReq.errResp <- err:
3✔
2985
                        case <-c.quit:
×
2986
                                return
×
2987
                        }
2988

2989
                        // If we don't have anything further to do after
2990
                        // advancing our state, then we'll exit.
2991
                        if nextState == StateFullyResolved {
3✔
2992
                                log.Infof("ChannelArbitrator(%v): all "+
×
2993
                                        "contracts resolved, exiting",
×
2994
                                        c.cfg.ChanPoint)
×
2995
                                return
×
2996
                        }
×
2997

2998
                case <-c.quit:
3✔
2999
                        return
3✔
3000
                }
3001
        }
3002
}
3003

3004
// handleBlockbeat processes a newly received blockbeat by advancing the
3005
// arbitrator's internal state using the received block height.
3006
func (c *ChannelArbitrator) handleBlockbeat(beat chainio.Blockbeat) error {
3✔
3007
        // Notify we've processed the block.
3✔
3008
        defer c.NotifyBlockProcessed(beat, nil)
3✔
3009

3✔
3010
        // If the state is StateContractClosed, StateWaitingFullResolution, or
3✔
3011
        // StateFullyResolved, there's no need to read the close event channel
3✔
3012
        // since the arbitrator can only get to this state after processing a
3✔
3013
        // previous close event and launched all its resolvers.
3✔
3014
        if c.state.IsContractClosed() {
6✔
3015
                log.Infof("ChannelArbitrator(%v): skipping reading close "+
3✔
3016
                        "events in state=%v", c.cfg.ChanPoint, c.state)
3✔
3017

3✔
3018
                // Launch all active resolvers when a new blockbeat is
3✔
3019
                // received, even when the contract is closed, we still need
3✔
3020
                // this as the resolvers may transform into new ones. For
3✔
3021
                // already launched resolvers this will be NOOP as they track
3✔
3022
                // their own `launched` states.
3✔
3023
                c.launchResolvers()
3✔
3024

3✔
3025
                return nil
3✔
3026
        }
3✔
3027

3028
        // Perform a non-blocking read on the close events in case the channel
3029
        // is closed in this blockbeat.
3030
        c.receiveAndProcessCloseEvent()
3✔
3031

3✔
3032
        // Try to advance the state if we are in StateDefault.
3✔
3033
        if c.state == StateDefault {
6✔
3034
                // Now that a new block has arrived, we'll attempt to advance
3✔
3035
                // our state forward.
3✔
3036
                _, _, err := c.advanceState(
3✔
3037
                        uint32(beat.Height()), chainTrigger, nil,
3✔
3038
                )
3✔
3039
                if err != nil {
3✔
3040
                        return fmt.Errorf("unable to advance state: %w", err)
×
3041
                }
×
3042
        }
3043

3044
        // Launch all active resolvers when a new blockbeat is received.
3045
        c.launchResolvers()
3✔
3046

3✔
3047
        return nil
3✔
3048
}
3049

3050
// receiveAndProcessCloseEvent does a non-blocking read on all the channel
3051
// close event channels. If an event is received, it will be further processed.
3052
func (c *ChannelArbitrator) receiveAndProcessCloseEvent() {
3✔
3053
        select {
3✔
3054
        // Received a coop close event, we now mark the channel as resolved and
3055
        // exit.
3056
        case closeInfo := <-c.cfg.ChainEvents.CooperativeClosure:
×
3057
                err := c.handleCoopCloseEvent(closeInfo)
×
3058
                if err != nil {
×
3059
                        log.Errorf("Failed to handle coop close: %v", err)
×
3060
                        return
×
3061
                }
×
3062

3063
        // We have broadcast our commitment, and it is now confirmed onchain.
UNCOV
3064
        case closeInfo := <-c.cfg.ChainEvents.LocalUnilateralClosure:
×
UNCOV
3065
                if c.state != StateCommitmentBroadcasted {
×
3066
                        log.Errorf("ChannelArbitrator(%v): unexpected "+
×
3067
                                "local on-chain channel close", c.cfg.ChanPoint)
×
3068
                }
×
3069

UNCOV
3070
                err := c.handleLocalForceCloseEvent(closeInfo)
×
UNCOV
3071
                if err != nil {
×
3072
                        log.Errorf("Failed to handle local force close: %v",
×
3073
                                err)
×
3074

×
3075
                        return
×
3076
                }
×
3077

3078
        // The remote party has broadcast the commitment. We'll examine our
3079
        // state to determine if we need to act at all.
3080
        case uniClosure := <-c.cfg.ChainEvents.RemoteUnilateralClosure:
1✔
3081
                err := c.handleRemoteForceCloseEvent(uniClosure)
1✔
3082
                if err != nil {
1✔
3083
                        log.Errorf("Failed to handle remote force close: %v",
×
3084
                                err)
×
3085

×
3086
                        return
×
3087
                }
×
3088

3089
        // The remote has breached the channel! We now launch the breach
3090
        // contract resolvers.
3091
        case breachInfo := <-c.cfg.ChainEvents.ContractBreach:
×
3092
                err := c.handleContractBreach(breachInfo)
×
3093
                if err != nil {
×
3094
                        log.Errorf("Failed to handle contract breach: %v", err)
×
3095
                        return
×
3096
                }
×
3097

3098
        default:
3✔
3099
                log.Infof("ChannelArbitrator(%v) no close event",
3✔
3100
                        c.cfg.ChanPoint)
3✔
3101
        }
3102
}
3103

3104
// Name returns a human-readable string for this subsystem.
3105
//
3106
// NOTE: Part of chainio.Consumer interface.
3107
func (c *ChannelArbitrator) Name() string {
3✔
3108
        return fmt.Sprintf("ChannelArbitrator(%v)", c.cfg.ChanPoint)
3✔
3109
}
3✔
3110

3111
// checkLegacyBreach returns StateFullyResolved if the channel was closed with
3112
// a breach transaction before the channel arbitrator launched its own breach
3113
// resolver. StateContractClosed is returned if this is a modern breach close
3114
// with a breach resolver. StateError is returned if the log lookup failed.
3115
func (c *ChannelArbitrator) checkLegacyBreach() (ArbitratorState, error) {
3✔
3116
        // A previous version of the channel arbitrator would make the breach
3✔
3117
        // close skip to StateFullyResolved. If there are no contract
3✔
3118
        // resolutions in the bolt arbitrator log, then this is an older breach
3✔
3119
        // close. Otherwise, if there are resolutions, the state should advance
3✔
3120
        // to StateContractClosed.
3✔
3121
        _, err := c.log.FetchContractResolutions()
3✔
3122
        if err == errNoResolutions {
3✔
3123
                // This is an older breach close still in the database.
×
3124
                return StateFullyResolved, nil
×
3125
        } else if err != nil {
3✔
3126
                return StateError, err
×
3127
        }
×
3128

3129
        // This is a modern breach close with resolvers.
3130
        return StateContractClosed, nil
3✔
3131
}
3132

3133
// sweepRequest wraps the arguments used when calling `SweepInput`.
3134
type sweepRequest struct {
3135
        // input is the input to be swept.
3136
        input input.Input
3137

3138
        // params holds the sweeping parameters.
3139
        params sweep.Params
3140
}
3141

3142
// createSweepRequest creates an anchor sweeping request for a particular
3143
// version (local/remote/remote pending) of the commitment.
3144
func (c *ChannelArbitrator) createSweepRequest(
3145
        anchor *lnwallet.AnchorResolution, htlcs htlcSet, anchorPath string,
3146
        heightHint uint32) (sweepRequest, error) {
3✔
3147

3✔
3148
        // Use the chan id as the exclusive group. This prevents any of the
3✔
3149
        // anchors from being batched together.
3✔
3150
        exclusiveGroup := c.cfg.ShortChanID.ToUint64()
3✔
3151

3✔
3152
        // Find the deadline for this specific anchor.
3✔
3153
        deadline, value, err := c.findCommitmentDeadlineAndValue(
3✔
3154
                heightHint, htlcs,
3✔
3155
        )
3✔
3156
        if err != nil {
3✔
3157
                return sweepRequest{}, err
×
3158
        }
×
3159

3160
        // If we cannot find a deadline, it means there's no HTLCs at stake,
3161
        // which means we can relax our anchor sweeping conditions as we don't
3162
        // have any time sensitive outputs to sweep. However we need to
3163
        // register the anchor output with the sweeper so we are later able to
3164
        // bump the close fee.
3165
        if deadline.IsNone() {
6✔
3166
                log.Infof("ChannelArbitrator(%v): no HTLCs at stake, "+
3✔
3167
                        "sweeping anchor with default deadline",
3✔
3168
                        c.cfg.ChanPoint)
3✔
3169
        }
3✔
3170

3171
        witnessType := input.CommitmentAnchor
3✔
3172

3✔
3173
        // For taproot channels, we need to use the proper witness type.
3✔
3174
        if txscript.IsPayToTaproot(
3✔
3175
                anchor.AnchorSignDescriptor.Output.PkScript,
3✔
3176
        ) {
6✔
3177

3✔
3178
                witnessType = input.TaprootAnchorSweepSpend
3✔
3179
        }
3✔
3180

3181
        // Prepare anchor output for sweeping.
3182
        anchorInput := input.MakeBaseInput(
3✔
3183
                &anchor.CommitAnchor,
3✔
3184
                witnessType,
3✔
3185
                &anchor.AnchorSignDescriptor,
3✔
3186
                heightHint,
3✔
3187
                &input.TxInfo{
3✔
3188
                        Fee:    anchor.CommitFee,
3✔
3189
                        Weight: anchor.CommitWeight,
3✔
3190
                },
3✔
3191
        )
3✔
3192

3✔
3193
        // If we have a deadline, we'll use it to calculate the deadline
3✔
3194
        // height, otherwise default to none.
3✔
3195
        deadlineDesc := "None"
3✔
3196
        deadlineHeight := fn.MapOption(func(d int32) int32 {
6✔
3197
                deadlineDesc = fmt.Sprintf("%d", d)
3✔
3198

3✔
3199
                return d + int32(heightHint)
3✔
3200
        })(deadline)
3✔
3201

3202
        // Calculate the budget based on the value under protection, which is
3203
        // the sum of all HTLCs on this commitment subtracted by their budgets.
3204
        // The anchor output in itself has a small output value of 330 sats so
3205
        // we also include it in the budget to pay for the cpfp transaction.
3206
        budget := calculateBudget(
3✔
3207
                value, c.cfg.Budget.AnchorCPFPRatio, c.cfg.Budget.AnchorCPFP,
3✔
3208
        ) + AnchorOutputValue
3✔
3209

3✔
3210
        log.Infof("ChannelArbitrator(%v): offering anchor from %s commitment "+
3✔
3211
                "%v to sweeper with deadline=%v, budget=%v", c.cfg.ChanPoint,
3✔
3212
                anchorPath, anchor.CommitAnchor, deadlineDesc, budget)
3✔
3213

3✔
3214
        // Sweep anchor output with a confirmation target fee preference.
3✔
3215
        // Because this is a cpfp-operation, the anchor will only be attempted
3✔
3216
        // to sweep when the current fee estimate for the confirmation target
3✔
3217
        // exceeds the commit fee rate.
3✔
3218
        return sweepRequest{
3✔
3219
                input: &anchorInput,
3✔
3220
                params: sweep.Params{
3✔
3221
                        ExclusiveGroup: &exclusiveGroup,
3✔
3222
                        Budget:         budget,
3✔
3223
                        DeadlineHeight: deadlineHeight,
3✔
3224
                },
3✔
3225
        }, nil
3✔
3226
}
3227

3228
// prepareAnchorSweeps creates a list of requests to be used by the sweeper for
3229
// all possible commitment versions.
3230
func (c *ChannelArbitrator) prepareAnchorSweeps(heightHint uint32,
3231
        anchors *lnwallet.AnchorResolutions) ([]sweepRequest, error) {
3✔
3232

3✔
3233
        // requests holds all the possible anchor sweep requests. We can have
3✔
3234
        // up to 3 different versions of commitments (local/remote/remote
3✔
3235
        // dangling) to be CPFPed by the anchors.
3✔
3236
        requests := make([]sweepRequest, 0, 3)
3✔
3237

3✔
3238
        // remotePendingReq holds the request for sweeping the anchor output on
3✔
3239
        // the remote pending commitment. It's only set when there's an actual
3✔
3240
        // pending remote commitment and it's used to decide whether we need to
3✔
3241
        // update the fee budget when sweeping the anchor output on the local
3✔
3242
        // commitment.
3✔
3243
        remotePendingReq := fn.None[sweepRequest]()
3✔
3244

3✔
3245
        // First we check on the remote pending commitment and optionally
3✔
3246
        // create an anchor sweeping request.
3✔
3247
        htlcs, ok := c.activeHTLCs[RemotePendingHtlcSet]
3✔
3248
        if ok && anchors.RemotePending != nil {
3✔
3249
                req, err := c.createSweepRequest(
×
3250
                        anchors.RemotePending, htlcs, "remote pending",
×
3251
                        heightHint,
×
3252
                )
×
3253
                if err != nil {
×
3254
                        return nil, err
×
3255
                }
×
3256

3257
                // Save the request.
3258
                requests = append(requests, req)
×
3259

×
3260
                // Set the optional variable.
×
3261
                remotePendingReq = fn.Some(req)
×
3262
        }
3263

3264
        // Check the local commitment and optionally create an anchor sweeping
3265
        // request. The params used in this request will be influenced by the
3266
        // anchor sweeping request made from the pending remote commitment.
3267
        htlcs, ok = c.activeHTLCs[LocalHtlcSet]
3✔
3268
        if ok && anchors.Local != nil {
6✔
3269
                req, err := c.createSweepRequest(
3✔
3270
                        anchors.Local, htlcs, "local", heightHint,
3✔
3271
                )
3✔
3272
                if err != nil {
3✔
3273
                        return nil, err
×
3274
                }
×
3275

3276
                // If there's an anchor sweeping request from the pending
3277
                // remote commitment, we will compare its budget against the
3278
                // budget used here and choose the params that has a larger
3279
                // budget. The deadline when choosing the remote pending budget
3280
                // instead of the local one will always be earlier or equal to
3281
                // the local deadline because outgoing HTLCs are resolved on
3282
                // the local commitment first before they are removed from the
3283
                // remote one.
3284
                remotePendingReq.WhenSome(func(s sweepRequest) {
3✔
3285
                        if s.params.Budget <= req.params.Budget {
×
3286
                                return
×
3287
                        }
×
3288

3289
                        log.Infof("ChannelArbitrator(%v): replaced local "+
×
3290
                                "anchor(%v) sweep params with pending remote "+
×
3291
                                "anchor sweep params, \nold:[%v], \nnew:[%v]",
×
3292
                                c.cfg.ChanPoint, anchors.Local.CommitAnchor,
×
3293
                                req.params, s.params)
×
3294

×
3295
                        req.params = s.params
×
3296
                })
3297

3298
                // Save the request.
3299
                requests = append(requests, req)
3✔
3300
        }
3301

3302
        // Check the remote commitment and create an anchor sweeping request if
3303
        // needed.
3304
        htlcs, ok = c.activeHTLCs[RemoteHtlcSet]
3✔
3305
        if ok && anchors.Remote != nil {
6✔
3306
                req, err := c.createSweepRequest(
3✔
3307
                        anchors.Remote, htlcs, "remote", heightHint,
3✔
3308
                )
3✔
3309
                if err != nil {
3✔
3310
                        return nil, err
×
3311
                }
×
3312

3313
                requests = append(requests, req)
3✔
3314
        }
3315

3316
        return requests, nil
3✔
3317
}
3318

3319
// failIncomingDust resolves the incoming dust HTLCs because they do not have
3320
// an output on the commitment transaction and cannot be resolved onchain. We
3321
// mark them as failed here.
3322
func (c *ChannelArbitrator) failIncomingDust(
3323
        incomingDustHTLCs []channeldb.HTLC) error {
3✔
3324

3✔
3325
        for _, htlc := range incomingDustHTLCs {
6✔
3326
                if !htlc.Incoming || htlc.OutputIndex >= 0 {
3✔
3327
                        return fmt.Errorf("htlc with index %v is not incoming "+
×
3328
                                "dust", htlc.OutputIndex)
×
3329
                }
×
3330

3331
                key := models.CircuitKey{
3✔
3332
                        ChanID: c.cfg.ShortChanID,
3✔
3333
                        HtlcID: htlc.HtlcIndex,
3✔
3334
                }
3✔
3335

3✔
3336
                // Mark this dust htlc as final failed.
3✔
3337
                chainArbCfg := c.cfg.ChainArbitratorConfig
3✔
3338
                err := chainArbCfg.PutFinalHtlcOutcome(
3✔
3339
                        key.ChanID, key.HtlcID, false,
3✔
3340
                )
3✔
3341
                if err != nil {
3✔
3342
                        return err
×
3343
                }
×
3344

3345
                // Send notification.
3346
                chainArbCfg.HtlcNotifier.NotifyFinalHtlcEvent(
3✔
3347
                        key,
3✔
3348
                        channeldb.FinalHtlcInfo{
3✔
3349
                                Settled:  false,
3✔
3350
                                Offchain: false,
3✔
3351
                        },
3✔
3352
                )
3✔
3353
        }
3354

3355
        return nil
3✔
3356
}
3357

3358
// abandonForwards cancels back the incoming HTLCs for their corresponding
3359
// outgoing HTLCs. We use a set here to avoid sending duplicate failure messages
3360
// for the same HTLC. This also needs to be done for locally initiated outgoing
3361
// HTLCs they are special cased in the switch.
3362
func (c *ChannelArbitrator) abandonForwards(htlcs fn.Set[uint64]) error {
3✔
3363
        log.Debugf("ChannelArbitrator(%v): cancelling back %v incoming "+
3✔
3364
                "HTLC(s)", c.cfg.ChanPoint,
3✔
3365
                len(htlcs))
3✔
3366

3✔
3367
        msgsToSend := make([]ResolutionMsg, 0, len(htlcs))
3✔
3368
        failureMsg := &lnwire.FailPermanentChannelFailure{}
3✔
3369

3✔
3370
        for idx := range htlcs {
6✔
3371
                failMsg := ResolutionMsg{
3✔
3372
                        SourceChan: c.cfg.ShortChanID,
3✔
3373
                        HtlcIndex:  idx,
3✔
3374
                        Failure:    failureMsg,
3✔
3375
                }
3✔
3376

3✔
3377
                msgsToSend = append(msgsToSend, failMsg)
3✔
3378
        }
3✔
3379

3380
        // Send the msges to the switch, if there are any.
3381
        if len(msgsToSend) == 0 {
6✔
3382
                return nil
3✔
3383
        }
3✔
3384

3385
        log.Debugf("ChannelArbitrator(%v): sending resolution message=%v",
3✔
3386
                c.cfg.ChanPoint, lnutils.SpewLogClosure(msgsToSend))
3✔
3387

3✔
3388
        err := c.cfg.DeliverResolutionMsg(msgsToSend...)
3✔
3389
        if err != nil {
3✔
3390
                log.Errorf("Unable to send resolution msges to switch: %v", err)
×
3391
                return err
×
3392
        }
×
3393

3394
        return nil
3✔
3395
}
3396

3397
// handleCoopCloseEvent takes a coop close event from ChainEvents, marks the
3398
// channel as closed and advances the state.
3399
func (c *ChannelArbitrator) handleCoopCloseEvent(
3400
        closeInfo *CooperativeCloseInfo) error {
3✔
3401

3✔
3402
        log.Infof("ChannelArbitrator(%v) marking channel cooperatively closed "+
3✔
3403
                "at height %v", c.cfg.ChanPoint, closeInfo.CloseHeight)
3✔
3404

3✔
3405
        err := c.cfg.MarkChannelClosed(
3✔
3406
                closeInfo.ChannelCloseSummary,
3✔
3407
                channeldb.ChanStatusCoopBroadcasted,
3✔
3408
        )
3✔
3409
        if err != nil {
3✔
3410
                return fmt.Errorf("unable to mark channel closed: %w", err)
×
3411
        }
×
3412

3413
        // We'll now advance our state machine until it reaches a terminal
3414
        // state, and the channel is marked resolved.
3415
        _, _, err = c.advanceState(closeInfo.CloseHeight, coopCloseTrigger, nil)
3✔
3416
        if err != nil {
3✔
3417
                log.Errorf("Unable to advance state: %v", err)
×
3418
        }
×
3419

3420
        return nil
×
3421
}
3422

3423
// handleLocalForceCloseEvent takes a local force close event from ChainEvents,
3424
// saves the contract resolutions to disk, mark the channel as closed and
3425
// advance the state.
3426
func (c *ChannelArbitrator) handleLocalForceCloseEvent(
3427
        closeInfo *LocalUnilateralCloseInfo) error {
3✔
3428

3✔
3429
        closeTx := closeInfo.CloseTx
3✔
3430

3✔
3431
        resolutions, err := closeInfo.ContractResolutions.
3✔
3432
                UnwrapOrErr(
3✔
3433
                        fmt.Errorf("resolutions not found"),
3✔
3434
                )
3✔
3435
        if err != nil {
3✔
3436
                return fmt.Errorf("unable to get resolutions: %w", err)
×
3437
        }
×
3438

3439
        // We make sure that the htlc resolutions are present
3440
        // otherwise we would panic dereferencing the pointer.
3441
        //
3442
        // TODO(ziggie): Refactor ContractResolutions to use
3443
        // options.
3444
        if resolutions.HtlcResolutions == nil {
3✔
3445
                return fmt.Errorf("htlc resolutions is nil")
×
3446
        }
×
3447

3448
        log.Infof("ChannelArbitrator(%v): local force close tx=%v confirmed",
3✔
3449
                c.cfg.ChanPoint, closeTx.TxHash())
3✔
3450

3✔
3451
        contractRes := &ContractResolutions{
3✔
3452
                CommitHash:       closeTx.TxHash(),
3✔
3453
                CommitResolution: resolutions.CommitResolution,
3✔
3454
                HtlcResolutions:  *resolutions.HtlcResolutions,
3✔
3455
                AnchorResolution: resolutions.AnchorResolution,
3✔
3456
        }
3✔
3457

3✔
3458
        // When processing a unilateral close event, we'll transition to the
3✔
3459
        // ContractClosed state. We'll log out the set of resolutions such that
3✔
3460
        // they are available to fetch in that state, we'll also write the
3✔
3461
        // commit set so we can reconstruct our chain actions on restart.
3✔
3462
        err = c.log.LogContractResolutions(contractRes)
3✔
3463
        if err != nil {
3✔
3464
                return fmt.Errorf("unable to write resolutions: %w", err)
×
3465
        }
×
3466

3467
        err = c.log.InsertConfirmedCommitSet(&closeInfo.CommitSet)
3✔
3468
        if err != nil {
3✔
3469
                return fmt.Errorf("unable to write commit set: %w", err)
×
3470
        }
×
3471

3472
        // After the set of resolutions are successfully logged, we can safely
3473
        // close the channel. After this succeeds we won't be getting chain
3474
        // events anymore, so we must make sure we can recover on restart after
3475
        // it is marked closed. If the next state transition fails, we'll start
3476
        // up in the prior state again, and we won't be longer getting chain
3477
        // events. In this case we must manually re-trigger the state
3478
        // transition into StateContractClosed based on the close status of the
3479
        // channel.
3480
        err = c.cfg.MarkChannelClosed(
3✔
3481
                closeInfo.ChannelCloseSummary,
3✔
3482
                channeldb.ChanStatusLocalCloseInitiator,
3✔
3483
        )
3✔
3484
        if err != nil {
3✔
3485
                return fmt.Errorf("unable to mark channel closed: %w", err)
×
3486
        }
×
3487

3488
        // We'll now advance our state machine until it reaches a terminal
3489
        // state.
3490
        _, _, err = c.advanceState(
3✔
3491
                uint32(closeInfo.SpendingHeight),
3✔
3492
                localCloseTrigger, &closeInfo.CommitSet,
3✔
3493
        )
3✔
3494
        if err != nil {
3✔
3495
                log.Errorf("Unable to advance state: %v", err)
×
3496
        }
×
3497

3498
        return nil
3✔
3499
}
3500

3501
// handleRemoteForceCloseEvent takes a remote force close event from
3502
// ChainEvents, saves the contract resolutions to disk, mark the channel as
3503
// closed and advance the state.
3504
func (c *ChannelArbitrator) handleRemoteForceCloseEvent(
3505
        closeInfo *RemoteUnilateralCloseInfo) error {
3✔
3506

3✔
3507
        log.Infof("ChannelArbitrator(%v): remote party has force closed "+
3✔
3508
                "channel at height %v", c.cfg.ChanPoint,
3✔
3509
                closeInfo.SpendingHeight)
3✔
3510

3✔
3511
        // If we don't have a self output, and there are no active HTLC's, then
3✔
3512
        // we can immediately mark the contract as fully resolved and exit.
3✔
3513
        contractRes := &ContractResolutions{
3✔
3514
                CommitHash:       *closeInfo.SpenderTxHash,
3✔
3515
                CommitResolution: closeInfo.CommitResolution,
3✔
3516
                HtlcResolutions:  *closeInfo.HtlcResolutions,
3✔
3517
                AnchorResolution: closeInfo.AnchorResolution,
3✔
3518
        }
3✔
3519

3✔
3520
        // When processing a unilateral close event, we'll transition to the
3✔
3521
        // ContractClosed state. We'll log out the set of resolutions such that
3✔
3522
        // they are available to fetch in that state, we'll also write the
3✔
3523
        // commit set so we can reconstruct our chain actions on restart.
3✔
3524
        err := c.log.LogContractResolutions(contractRes)
3✔
3525
        if err != nil {
3✔
3526
                return fmt.Errorf("unable to write resolutions: %w", err)
×
3527
        }
×
3528

3529
        err = c.log.InsertConfirmedCommitSet(&closeInfo.CommitSet)
3✔
3530
        if err != nil {
3✔
3531
                return fmt.Errorf("unable to write commit set: %w", err)
×
3532
        }
×
3533

3534
        // After the set of resolutions are successfully logged, we can safely
3535
        // close the channel. After this succeeds we won't be getting chain
3536
        // events anymore, so we must make sure we can recover on restart after
3537
        // it is marked closed. If the next state transition fails, we'll start
3538
        // up in the prior state again, and we won't be longer getting chain
3539
        // events. In this case we must manually re-trigger the state
3540
        // transition into StateContractClosed based on the close status of the
3541
        // channel.
3542
        closeSummary := &closeInfo.ChannelCloseSummary
3✔
3543
        err = c.cfg.MarkChannelClosed(
3✔
3544
                closeSummary,
3✔
3545
                channeldb.ChanStatusRemoteCloseInitiator,
3✔
3546
        )
3✔
3547
        if err != nil {
3✔
3548
                return fmt.Errorf("unable to mark channel closed: %w", err)
×
3549
        }
×
3550

3551
        // We'll now advance our state machine until it reaches a terminal
3552
        // state.
3553
        _, _, err = c.advanceState(
3✔
3554
                uint32(closeInfo.SpendingHeight),
3✔
3555
                remoteCloseTrigger, &closeInfo.CommitSet,
3✔
3556
        )
3✔
3557
        if err != nil {
3✔
3558
                log.Errorf("Unable to advance state: %v", err)
×
3559
        }
×
3560

3561
        return nil
3✔
3562
}
3563

3564
// handleContractBreach takes a breach close event from ChainEvents, saves the
3565
// contract resolutions to disk, mark the channel as closed and advance the
3566
// state.
3567
func (c *ChannelArbitrator) handleContractBreach(
3568
        breachInfo *BreachCloseInfo) error {
3✔
3569

3✔
3570
        closeSummary := &breachInfo.CloseSummary
3✔
3571

3✔
3572
        log.Infof("ChannelArbitrator(%v): remote party has breached channel "+
3✔
3573
                "at height %v!", c.cfg.ChanPoint, closeSummary.CloseHeight)
3✔
3574

3✔
3575
        // In the breach case, we'll only have anchor and breach resolutions.
3✔
3576
        contractRes := &ContractResolutions{
3✔
3577
                CommitHash:       breachInfo.CommitHash,
3✔
3578
                BreachResolution: breachInfo.BreachResolution,
3✔
3579
                AnchorResolution: breachInfo.AnchorResolution,
3✔
3580
        }
3✔
3581

3✔
3582
        // We'll transition to the ContractClosed state and log the set of
3✔
3583
        // resolutions such that they can be turned into resolvers later on.
3✔
3584
        // We'll also insert the CommitSet of the latest set of commitments.
3✔
3585
        err := c.log.LogContractResolutions(contractRes)
3✔
3586
        if err != nil {
3✔
3587
                return fmt.Errorf("unable to write resolutions: %w", err)
×
3588
        }
×
3589

3590
        err = c.log.InsertConfirmedCommitSet(&breachInfo.CommitSet)
3✔
3591
        if err != nil {
3✔
3592
                return fmt.Errorf("unable to write commit set: %w", err)
×
3593
        }
×
3594

3595
        // The channel is finally marked pending closed here as the
3596
        // BreachArbitrator and channel arbitrator have persisted the relevant
3597
        // states.
3598
        err = c.cfg.MarkChannelClosed(
3✔
3599
                closeSummary, channeldb.ChanStatusRemoteCloseInitiator,
3✔
3600
        )
3✔
3601
        if err != nil {
3✔
3602
                return fmt.Errorf("unable to mark channel closed: %w", err)
×
3603
        }
×
3604

3605
        log.Infof("Breached channel=%v marked pending-closed",
3✔
3606
                breachInfo.BreachResolution.FundingOutPoint)
3✔
3607

3✔
3608
        // We'll advance our state machine until it reaches a terminal state.
3✔
3609
        _, _, err = c.advanceState(
3✔
3610
                closeSummary.CloseHeight, breachCloseTrigger,
3✔
3611
                &breachInfo.CommitSet,
3✔
3612
        )
3✔
3613
        if err != nil {
3✔
3614
                log.Errorf("Unable to advance state: %v", err)
×
3615
        }
×
3616

3617
        return nil
3✔
3618
}
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