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

lightningnetwork / lnd / 16048076081

03 Jul 2025 10:31AM UTC coverage: 67.53% (-0.06%) from 67.589%
16048076081

Pull #10018

github

web-flow
Merge b10bc9e36 into 8a0341419
Pull Request #10018: Refactor link's long methods

530 of 808 new or added lines in 1 file covered. (65.59%)

140 existing lines in 29 files now uncovered.

135096 of 200052 relevant lines covered (67.53%)

21807.17 hits per line

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

76.6
/htlcswitch/link.go
1
package htlcswitch
2

3
import (
4
        "bytes"
5
        "context"
6
        crand "crypto/rand"
7
        "crypto/sha256"
8
        "errors"
9
        "fmt"
10
        prand "math/rand"
11
        "sync"
12
        "sync/atomic"
13
        "time"
14

15
        "github.com/btcsuite/btcd/btcutil"
16
        "github.com/btcsuite/btcd/wire"
17
        "github.com/btcsuite/btclog/v2"
18
        "github.com/lightningnetwork/lnd/channeldb"
19
        "github.com/lightningnetwork/lnd/contractcourt"
20
        "github.com/lightningnetwork/lnd/fn/v2"
21
        "github.com/lightningnetwork/lnd/graph/db/models"
22
        "github.com/lightningnetwork/lnd/htlcswitch/hodl"
23
        "github.com/lightningnetwork/lnd/htlcswitch/hop"
24
        "github.com/lightningnetwork/lnd/input"
25
        "github.com/lightningnetwork/lnd/invoices"
26
        "github.com/lightningnetwork/lnd/lnpeer"
27
        "github.com/lightningnetwork/lnd/lntypes"
28
        "github.com/lightningnetwork/lnd/lnutils"
29
        "github.com/lightningnetwork/lnd/lnwallet"
30
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
31
        "github.com/lightningnetwork/lnd/lnwire"
32
        "github.com/lightningnetwork/lnd/queue"
33
        "github.com/lightningnetwork/lnd/record"
34
        "github.com/lightningnetwork/lnd/routing/route"
35
        "github.com/lightningnetwork/lnd/ticker"
36
        "github.com/lightningnetwork/lnd/tlv"
37
)
38

39
const (
40
        // DefaultMaxOutgoingCltvExpiry is the maximum outgoing time lock that
41
        // the node accepts for forwarded payments. The value is relative to the
42
        // current block height. The reason to have a maximum is to prevent
43
        // funds getting locked up unreasonably long. Otherwise, an attacker
44
        // willing to lock its own funds too, could force the funds of this node
45
        // to be locked up for an indefinite (max int32) number of blocks.
46
        //
47
        // The value 2016 corresponds to on average two weeks worth of blocks
48
        // and is based on the maximum number of hops (20), the default CLTV
49
        // delta (40), and some extra margin to account for the other lightning
50
        // implementations and past lnd versions which used to have a default
51
        // CLTV delta of 144.
52
        DefaultMaxOutgoingCltvExpiry = 2016
53

54
        // DefaultMinLinkFeeUpdateTimeout represents the minimum interval in
55
        // which a link should propose to update its commitment fee rate.
56
        DefaultMinLinkFeeUpdateTimeout = 10 * time.Minute
57

58
        // DefaultMaxLinkFeeUpdateTimeout represents the maximum interval in
59
        // which a link should propose to update its commitment fee rate.
60
        DefaultMaxLinkFeeUpdateTimeout = 60 * time.Minute
61

62
        // DefaultMaxLinkFeeAllocation is the highest allocation we'll allow
63
        // a channel's commitment fee to be of its balance. This only applies to
64
        // the initiator of the channel.
65
        DefaultMaxLinkFeeAllocation float64 = 0.5
66
)
67

68
// ExpectedFee computes the expected fee for a given htlc amount. The value
69
// returned from this function is to be used as a sanity check when forwarding
70
// HTLC's to ensure that an incoming HTLC properly adheres to our propagated
71
// forwarding policy.
72
//
73
// TODO(roasbeef): also add in current available channel bandwidth, inverse
74
// func
75
func ExpectedFee(f models.ForwardingPolicy,
76
        htlcAmt lnwire.MilliSatoshi) lnwire.MilliSatoshi {
81✔
77

81✔
78
        return f.BaseFee + (htlcAmt*f.FeeRate)/1000000
81✔
79
}
81✔
80

81
// ChannelLinkConfig defines the configuration for the channel link. ALL
82
// elements within the configuration MUST be non-nil for channel link to carry
83
// out its duties.
84
type ChannelLinkConfig struct {
85
        // FwrdingPolicy is the initial forwarding policy to be used when
86
        // deciding whether to forwarding incoming HTLC's or not. This value
87
        // can be updated with subsequent calls to UpdateForwardingPolicy
88
        // targeted at a given ChannelLink concrete interface implementation.
89
        FwrdingPolicy models.ForwardingPolicy
90

91
        // Circuits provides restricted access to the switch's circuit map,
92
        // allowing the link to open and close circuits.
93
        Circuits CircuitModifier
94

95
        // BestHeight returns the best known height.
96
        BestHeight func() uint32
97

98
        // ForwardPackets attempts to forward the batch of htlcs through the
99
        // switch. The function returns and error in case it fails to send one or
100
        // more packets. The link's quit signal should be provided to allow
101
        // cancellation of forwarding during link shutdown.
102
        ForwardPackets func(<-chan struct{}, bool, ...*htlcPacket) error
103

104
        // DecodeHopIterators facilitates batched decoding of HTLC Sphinx onion
105
        // blobs, which are then used to inform how to forward an HTLC.
106
        //
107
        // NOTE: This function assumes the same set of readers and preimages
108
        // are always presented for the same identifier. The last boolean is
109
        // used to decide whether this is a reforwarding or not - when it's
110
        // reforwarding, we skip the replay check enforced in our decay log.
111
        DecodeHopIterators func([]byte, []hop.DecodeHopIteratorRequest, bool) (
112
                []hop.DecodeHopIteratorResponse, error)
113

114
        // ExtractErrorEncrypter function is responsible for decoding HTLC
115
        // Sphinx onion blob, and creating onion failure obfuscator.
116
        ExtractErrorEncrypter hop.ErrorEncrypterExtracter
117

118
        // FetchLastChannelUpdate retrieves the latest routing policy for a
119
        // target channel. This channel will typically be the outgoing channel
120
        // specified when we receive an incoming HTLC.  This will be used to
121
        // provide payment senders our latest policy when sending encrypted
122
        // error messages.
123
        FetchLastChannelUpdate func(lnwire.ShortChannelID) (
124
                *lnwire.ChannelUpdate1, error)
125

126
        // Peer is a lightning network node with which we have the channel link
127
        // opened.
128
        Peer lnpeer.Peer
129

130
        // Registry is a sub-system which responsible for managing the invoices
131
        // in thread-safe manner.
132
        Registry InvoiceDatabase
133

134
        // PreimageCache is a global witness beacon that houses any new
135
        // preimages discovered by other links. We'll use this to add new
136
        // witnesses that we discover which will notify any sub-systems
137
        // subscribed to new events.
138
        PreimageCache contractcourt.WitnessBeacon
139

140
        // OnChannelFailure is a function closure that we'll call if the
141
        // channel failed for some reason. Depending on the severity of the
142
        // error, the closure potentially must force close this channel and
143
        // disconnect the peer.
144
        //
145
        // NOTE: The method must return in order for the ChannelLink to be able
146
        // to shut down properly.
147
        OnChannelFailure func(lnwire.ChannelID, lnwire.ShortChannelID,
148
                LinkFailureError)
149

150
        // UpdateContractSignals is a function closure that we'll use to update
151
        // outside sub-systems with this channel's latest ShortChannelID.
152
        UpdateContractSignals func(*contractcourt.ContractSignals) error
153

154
        // NotifyContractUpdate is a function closure that we'll use to update
155
        // the contractcourt and more specifically the ChannelArbitrator of the
156
        // latest channel state.
157
        NotifyContractUpdate func(*contractcourt.ContractUpdate) error
158

159
        // ChainEvents is an active subscription to the chain watcher for this
160
        // channel to be notified of any on-chain activity related to this
161
        // channel.
162
        ChainEvents *contractcourt.ChainEventSubscription
163

164
        // FeeEstimator is an instance of a live fee estimator which will be
165
        // used to dynamically regulate the current fee of the commitment
166
        // transaction to ensure timely confirmation.
167
        FeeEstimator chainfee.Estimator
168

169
        // hodl.Mask is a bitvector composed of hodl.Flags, specifying breakpoints
170
        // for HTLC forwarding internal to the switch.
171
        //
172
        // NOTE: This should only be used for testing.
173
        HodlMask hodl.Mask
174

175
        // SyncStates is used to indicate that we need send the channel
176
        // reestablishment message to the remote peer. It should be done if our
177
        // clients have been restarted, or remote peer have been reconnected.
178
        SyncStates bool
179

180
        // BatchTicker is the ticker that determines the interval that we'll
181
        // use to check the batch to see if there're any updates we should
182
        // flush out. By batching updates into a single commit, we attempt to
183
        // increase throughput by maximizing the number of updates coalesced
184
        // into a single commit.
185
        BatchTicker ticker.Ticker
186

187
        // FwdPkgGCTicker is the ticker determining the frequency at which
188
        // garbage collection of forwarding packages occurs. We use a
189
        // time-based approach, as opposed to block epochs, as to not hinder
190
        // syncing.
191
        FwdPkgGCTicker ticker.Ticker
192

193
        // PendingCommitTicker is a ticker that allows the link to determine if
194
        // a locally initiated commitment dance gets stuck waiting for the
195
        // remote party to revoke.
196
        PendingCommitTicker ticker.Ticker
197

198
        // BatchSize is the max size of a batch of updates done to the link
199
        // before we do a state update.
200
        BatchSize uint32
201

202
        // UnsafeReplay will cause a link to replay the adds in its latest
203
        // commitment txn after the link is restarted. This should only be used
204
        // in testing, it is here to ensure the sphinx replay detection on the
205
        // receiving node is persistent.
206
        UnsafeReplay bool
207

208
        // MinUpdateTimeout represents the minimum interval in which a link
209
        // will propose to update its commitment fee rate. A random timeout will
210
        // be selected between this and MaxUpdateTimeout.
211
        MinUpdateTimeout time.Duration
212

213
        // MaxUpdateTimeout represents the maximum interval in which a link
214
        // will propose to update its commitment fee rate. A random timeout will
215
        // be selected between this and MinUpdateTimeout.
216
        MaxUpdateTimeout time.Duration
217

218
        // OutgoingCltvRejectDelta defines the number of blocks before expiry of
219
        // an htlc where we don't offer an htlc anymore. This should be at least
220
        // the outgoing broadcast delta, because in any case we don't want to
221
        // risk offering an htlc that triggers channel closure.
222
        OutgoingCltvRejectDelta uint32
223

224
        // TowerClient is an optional engine that manages the signing,
225
        // encrypting, and uploading of justice transactions to the daemon's
226
        // configured set of watchtowers for legacy channels.
227
        TowerClient TowerClient
228

229
        // MaxOutgoingCltvExpiry is the maximum outgoing timelock that the link
230
        // should accept for a forwarded HTLC. The value is relative to the
231
        // current block height.
232
        MaxOutgoingCltvExpiry uint32
233

234
        // MaxFeeAllocation is the highest allocation we'll allow a channel's
235
        // commitment fee to be of its balance. This only applies to the
236
        // initiator of the channel.
237
        MaxFeeAllocation float64
238

239
        // MaxAnchorsCommitFeeRate is the max commitment fee rate we'll use as
240
        // the initiator for channels of the anchor type.
241
        MaxAnchorsCommitFeeRate chainfee.SatPerKWeight
242

243
        // NotifyActiveLink allows the link to tell the ChannelNotifier when a
244
        // link is first started.
245
        NotifyActiveLink func(wire.OutPoint)
246

247
        // NotifyActiveChannel allows the link to tell the ChannelNotifier when
248
        // channels becomes active.
249
        NotifyActiveChannel func(wire.OutPoint)
250

251
        // NotifyInactiveChannel allows the switch to tell the ChannelNotifier
252
        // when channels become inactive.
253
        NotifyInactiveChannel func(wire.OutPoint)
254

255
        // NotifyInactiveLinkEvent allows the switch to tell the
256
        // ChannelNotifier when a channel link become inactive.
257
        NotifyInactiveLinkEvent func(wire.OutPoint)
258

259
        // HtlcNotifier is an instance of a htlcNotifier which we will pipe htlc
260
        // events through.
261
        HtlcNotifier htlcNotifier
262

263
        // FailAliasUpdate is a function used to fail an HTLC for an
264
        // option_scid_alias channel.
265
        FailAliasUpdate func(sid lnwire.ShortChannelID,
266
                incoming bool) *lnwire.ChannelUpdate1
267

268
        // GetAliases is used by the link and switch to fetch the set of
269
        // aliases for a given link.
270
        GetAliases func(base lnwire.ShortChannelID) []lnwire.ShortChannelID
271

272
        // PreviouslySentShutdown is an optional value that is set if, at the
273
        // time of the link being started, persisted shutdown info was found for
274
        // the channel. This value being set means that we previously sent a
275
        // Shutdown message to our peer, and so we should do so again on
276
        // re-establish and should not allow anymore HTLC adds on the outgoing
277
        // direction of the link.
278
        PreviouslySentShutdown fn.Option[lnwire.Shutdown]
279

280
        // Adds the option to disable forwarding payments in blinded routes
281
        // by failing back any blinding-related payloads as if they were
282
        // invalid.
283
        DisallowRouteBlinding bool
284

285
        // DisallowQuiescence is a flag that can be used to disable the
286
        // quiescence protocol.
287
        DisallowQuiescence bool
288

289
        // MaxFeeExposure is the threshold in milli-satoshis after which we'll
290
        // restrict the flow of HTLCs and fee updates.
291
        MaxFeeExposure lnwire.MilliSatoshi
292

293
        // ShouldFwdExpEndorsement is a closure that indicates whether the link
294
        // should forward experimental endorsement signals.
295
        ShouldFwdExpEndorsement func() bool
296

297
        // AuxTrafficShaper is an optional auxiliary traffic shaper that can be
298
        // used to manage the bandwidth of the link.
299
        AuxTrafficShaper fn.Option[AuxTrafficShaper]
300
}
301

302
// channelLink is the service which drives a channel's commitment update
303
// state-machine. In the event that an HTLC needs to be propagated to another
304
// link, the forward handler from config is used which sends HTLC to the
305
// switch. Additionally, the link encapsulate logic of commitment protocol
306
// message ordering and updates.
307
type channelLink struct {
308
        // The following fields are only meant to be used *atomically*
309
        started       int32
310
        reestablished int32
311
        shutdown      int32
312

313
        // failed should be set to true in case a link error happens, making
314
        // sure we don't process any more updates.
315
        failed bool
316

317
        // keystoneBatch represents a volatile list of keystones that must be
318
        // written before attempting to sign the next commitment txn. These
319
        // represent all the HTLC's forwarded to the link from the switch. Once
320
        // we lock them into our outgoing commitment, then the circuit has a
321
        // keystone, and is fully opened.
322
        keystoneBatch []Keystone
323

324
        // openedCircuits is the set of all payment circuits that will be open
325
        // once we make our next commitment. After making the commitment we'll
326
        // ACK all these from our mailbox to ensure that they don't get
327
        // re-delivered if we reconnect.
328
        openedCircuits []CircuitKey
329

330
        // closedCircuits is the set of all payment circuits that will be
331
        // closed once we make our next commitment. After taking the commitment
332
        // we'll ACK all these to ensure that they don't get re-delivered if we
333
        // reconnect.
334
        closedCircuits []CircuitKey
335

336
        // channel is a lightning network channel to which we apply htlc
337
        // updates.
338
        channel *lnwallet.LightningChannel
339

340
        // cfg is a structure which carries all dependable fields/handlers
341
        // which may affect behaviour of the service.
342
        cfg ChannelLinkConfig
343

344
        // mailBox is the main interface between the outside world and the
345
        // link. All incoming messages will be sent over this mailBox. Messages
346
        // include new updates from our connected peer, and new packets to be
347
        // forwarded sent by the switch.
348
        mailBox MailBox
349

350
        // upstream is a channel that new messages sent from the remote peer to
351
        // the local peer will be sent across.
352
        upstream chan lnwire.Message
353

354
        // downstream is a channel in which new multi-hop HTLC's to be
355
        // forwarded will be sent across. Messages from this channel are sent
356
        // by the HTLC switch.
357
        downstream chan *htlcPacket
358

359
        // updateFeeTimer is the timer responsible for updating the link's
360
        // commitment fee every time it fires.
361
        updateFeeTimer *time.Timer
362

363
        // uncommittedPreimages stores a list of all preimages that have been
364
        // learned since receiving the last CommitSig from the remote peer. The
365
        // batch will be flushed just before accepting the subsequent CommitSig
366
        // or on shutdown to avoid doing a write for each preimage received.
367
        uncommittedPreimages []lntypes.Preimage
368

369
        sync.RWMutex
370

371
        // hodlQueue is used to receive exit hop htlc resolutions from invoice
372
        // registry.
373
        hodlQueue *queue.ConcurrentQueue
374

375
        // hodlMap stores related htlc data for a circuit key. It allows
376
        // resolving those htlcs when we receive a message on hodlQueue.
377
        hodlMap map[models.CircuitKey]hodlHtlc
378

379
        // log is a link-specific logging instance.
380
        log btclog.Logger
381

382
        // isOutgoingAddBlocked tracks whether the channelLink can send an
383
        // UpdateAddHTLC.
384
        isOutgoingAddBlocked atomic.Bool
385

386
        // isIncomingAddBlocked tracks whether the channelLink can receive an
387
        // UpdateAddHTLC.
388
        isIncomingAddBlocked atomic.Bool
389

390
        // flushHooks is a hookMap that is triggered when we reach a channel
391
        // state with no live HTLCs.
392
        flushHooks hookMap
393

394
        // outgoingCommitHooks is a hookMap that is triggered after we send our
395
        // next CommitSig.
396
        outgoingCommitHooks hookMap
397

398
        // incomingCommitHooks is a hookMap that is triggered after we receive
399
        // our next CommitSig.
400
        incomingCommitHooks hookMap
401

402
        // quiescer is the state machine that tracks where this channel is with
403
        // respect to the quiescence protocol.
404
        quiescer Quiescer
405

406
        // quiescenceReqs is a queue of requests to quiesce this link. The
407
        // members of the queue are send-only channels we should call back with
408
        // the result.
409
        quiescenceReqs chan StfuReq
410

411
        // cg is a helper that encapsulates a wait group and quit channel and
412
        // allows contexts that either block or cancel on those depending on
413
        // the use case.
414
        cg *fn.ContextGuard
415
}
416

417
// hookMap is a data structure that is used to track the hooks that need to be
418
// called in various parts of the channelLink's lifecycle.
419
//
420
// WARNING: NOT thread-safe.
421
type hookMap struct {
422
        // allocIdx keeps track of the next id we haven't yet allocated.
423
        allocIdx atomic.Uint64
424

425
        // transient is a map of hooks that are only called the next time invoke
426
        // is called. These hooks are deleted during invoke.
427
        transient map[uint64]func()
428

429
        // newTransients is a channel that we use to accept new hooks into the
430
        // hookMap.
431
        newTransients chan func()
432
}
433

434
// newHookMap initializes a new empty hookMap.
435
func newHookMap() hookMap {
648✔
436
        return hookMap{
648✔
437
                allocIdx:      atomic.Uint64{},
648✔
438
                transient:     make(map[uint64]func()),
648✔
439
                newTransients: make(chan func()),
648✔
440
        }
648✔
441
}
648✔
442

443
// alloc allocates space in the hook map for the supplied hook, the second
444
// argument determines whether it goes into the transient or persistent part
445
// of the hookMap.
446
func (m *hookMap) alloc(hook func()) uint64 {
5✔
447
        // We assume we never overflow a uint64. Seems OK.
5✔
448
        hookID := m.allocIdx.Add(1)
5✔
449
        if hookID == 0 {
5✔
450
                panic("hookMap allocIdx overflow")
×
451
        }
452
        m.transient[hookID] = hook
5✔
453

5✔
454
        return hookID
5✔
455
}
456

457
// invoke is used on a hook map to call all the registered hooks and then clear
458
// out the transient hooks so they are not called again.
459
func (m *hookMap) invoke() {
2,726✔
460
        for _, hook := range m.transient {
2,731✔
461
                hook()
5✔
462
        }
5✔
463

464
        m.transient = make(map[uint64]func())
2,726✔
465
}
466

467
// hodlHtlc contains htlc data that is required for resolution.
468
type hodlHtlc struct {
469
        add        lnwire.UpdateAddHTLC
470
        sourceRef  channeldb.AddRef
471
        obfuscator hop.ErrorEncrypter
472
}
473

474
// NewChannelLink creates a new instance of a ChannelLink given a configuration
475
// and active channel that will be used to verify/apply updates to.
476
func NewChannelLink(cfg ChannelLinkConfig,
477
        channel *lnwallet.LightningChannel) ChannelLink {
218✔
478

218✔
479
        logPrefix := fmt.Sprintf("ChannelLink(%v):", channel.ChannelPoint())
218✔
480

218✔
481
        // If the max fee exposure isn't set, use the default.
218✔
482
        if cfg.MaxFeeExposure == 0 {
433✔
483
                cfg.MaxFeeExposure = DefaultMaxFeeExposure
215✔
484
        }
215✔
485

486
        var qsm Quiescer
218✔
487
        if !cfg.DisallowQuiescence {
436✔
488
                qsm = NewQuiescer(QuiescerCfg{
218✔
489
                        chanID: lnwire.NewChanIDFromOutPoint(
218✔
490
                                channel.ChannelPoint(),
218✔
491
                        ),
218✔
492
                        channelInitiator: channel.Initiator(),
218✔
493
                        sendMsg: func(s lnwire.Stfu) error {
223✔
494
                                return cfg.Peer.SendMessage(false, &s)
5✔
495
                        },
5✔
496
                        timeoutDuration: defaultQuiescenceTimeout,
497
                        onTimeout: func() {
2✔
498
                                cfg.Peer.Disconnect(ErrQuiescenceTimeout)
2✔
499
                        },
2✔
500
                })
501
        } else {
×
502
                qsm = &quiescerNoop{}
×
503
        }
×
504

505
        quiescenceReqs := make(
218✔
506
                chan fn.Req[fn.Unit, fn.Result[lntypes.ChannelParty]], 1,
218✔
507
        )
218✔
508

218✔
509
        return &channelLink{
218✔
510
                cfg:                 cfg,
218✔
511
                channel:             channel,
218✔
512
                hodlMap:             make(map[models.CircuitKey]hodlHtlc),
218✔
513
                hodlQueue:           queue.NewConcurrentQueue(10),
218✔
514
                log:                 log.WithPrefix(logPrefix),
218✔
515
                flushHooks:          newHookMap(),
218✔
516
                outgoingCommitHooks: newHookMap(),
218✔
517
                incomingCommitHooks: newHookMap(),
218✔
518
                quiescer:            qsm,
218✔
519
                quiescenceReqs:      quiescenceReqs,
218✔
520
                cg:                  fn.NewContextGuard(),
218✔
521
        }
218✔
522
}
523

524
// A compile time check to ensure channelLink implements the ChannelLink
525
// interface.
526
var _ ChannelLink = (*channelLink)(nil)
527

528
// Start starts all helper goroutines required for the operation of the channel
529
// link.
530
//
531
// NOTE: Part of the ChannelLink interface.
532
func (l *channelLink) Start() error {
216✔
533
        if !atomic.CompareAndSwapInt32(&l.started, 0, 1) {
216✔
534
                err := fmt.Errorf("channel link(%v): already started", l)
×
535
                l.log.Warn("already started")
×
536
                return err
×
537
        }
×
538

539
        l.log.Info("starting")
216✔
540

216✔
541
        // If the config supplied watchtower client, ensure the channel is
216✔
542
        // registered before trying to use it during operation.
216✔
543
        if l.cfg.TowerClient != nil {
219✔
544
                err := l.cfg.TowerClient.RegisterChannel(
3✔
545
                        l.ChanID(), l.channel.State().ChanType,
3✔
546
                )
3✔
547
                if err != nil {
3✔
548
                        return err
×
549
                }
×
550
        }
551

552
        l.mailBox.ResetMessages()
216✔
553
        l.hodlQueue.Start()
216✔
554

216✔
555
        // Before launching the htlcManager messages, revert any circuits that
216✔
556
        // were marked open in the switch's circuit map, but did not make it
216✔
557
        // into a commitment txn. We use the next local htlc index as the cut
216✔
558
        // off point, since all indexes below that are committed. This action
216✔
559
        // is only performed if the link's final short channel ID has been
216✔
560
        // assigned, otherwise we would try to trim the htlcs belonging to the
216✔
561
        // all-zero, hop.Source ID.
216✔
562
        if l.ShortChanID() != hop.Source {
432✔
563
                localHtlcIndex, err := l.channel.NextLocalHtlcIndex()
216✔
564
                if err != nil {
216✔
565
                        return fmt.Errorf("unable to retrieve next local "+
×
566
                                "htlc index: %v", err)
×
567
                }
×
568

569
                // NOTE: This is automatically done by the switch when it
570
                // starts up, but is necessary to prevent inconsistencies in
571
                // the case that the link flaps. This is a result of a link's
572
                // life-cycle being shorter than that of the switch.
573
                chanID := l.ShortChanID()
216✔
574
                err = l.cfg.Circuits.TrimOpenCircuits(chanID, localHtlcIndex)
216✔
575
                if err != nil {
216✔
576
                        return fmt.Errorf("unable to trim circuits above "+
×
577
                                "local htlc index %d: %v", localHtlcIndex, err)
×
578
                }
×
579

580
                // Since the link is live, before we start the link we'll update
581
                // the ChainArbitrator with the set of new channel signals for
582
                // this channel.
583
                //
584
                // TODO(roasbeef): split goroutines within channel arb to avoid
585
                go func() {
432✔
586
                        signals := &contractcourt.ContractSignals{
216✔
587
                                ShortChanID: l.channel.ShortChanID(),
216✔
588
                        }
216✔
589

216✔
590
                        err := l.cfg.UpdateContractSignals(signals)
216✔
591
                        if err != nil {
216✔
592
                                l.log.Errorf("unable to update signals")
×
593
                        }
×
594
                }()
595
        }
596

597
        l.updateFeeTimer = time.NewTimer(l.randomFeeUpdateTimeout())
216✔
598

216✔
599
        l.cg.WgAdd(1)
216✔
600
        go l.htlcManager(context.TODO())
216✔
601

216✔
602
        return nil
216✔
603
}
604

605
// Stop gracefully stops all active helper goroutines, then waits until they've
606
// exited.
607
//
608
// NOTE: Part of the ChannelLink interface.
609
func (l *channelLink) Stop() {
217✔
610
        if !atomic.CompareAndSwapInt32(&l.shutdown, 0, 1) {
229✔
611
                l.log.Warn("already stopped")
12✔
612
                return
12✔
613
        }
12✔
614

615
        l.log.Info("stopping")
205✔
616

205✔
617
        // As the link is stopping, we are no longer interested in htlc
205✔
618
        // resolutions coming from the invoice registry.
205✔
619
        l.cfg.Registry.HodlUnsubscribeAll(l.hodlQueue.ChanIn())
205✔
620

205✔
621
        if l.cfg.ChainEvents.Cancel != nil {
208✔
622
                l.cfg.ChainEvents.Cancel()
3✔
623
        }
3✔
624

625
        // Ensure the channel for the timer is drained.
626
        if l.updateFeeTimer != nil {
410✔
627
                if !l.updateFeeTimer.Stop() {
205✔
628
                        select {
×
629
                        case <-l.updateFeeTimer.C:
×
630
                        default:
×
631
                        }
632
                }
633
        }
634

635
        if l.hodlQueue != nil {
410✔
636
                l.hodlQueue.Stop()
205✔
637
        }
205✔
638

639
        l.cg.Quit()
205✔
640
        l.cg.WgWait()
205✔
641

205✔
642
        // Now that the htlcManager has completely exited, reset the packet
205✔
643
        // courier. This allows the mailbox to revaluate any lingering Adds that
205✔
644
        // were delivered but didn't make it on a commitment to be failed back
205✔
645
        // if the link is offline for an extended period of time. The error is
205✔
646
        // ignored since it can only fail when the daemon is exiting.
205✔
647
        _ = l.mailBox.ResetPackets()
205✔
648

205✔
649
        // As a final precaution, we will attempt to flush any uncommitted
205✔
650
        // preimages to the preimage cache. The preimages should be re-delivered
205✔
651
        // after channel reestablishment, however this adds an extra layer of
205✔
652
        // protection in case the peer never returns. Without this, we will be
205✔
653
        // unable to settle any contracts depending on the preimages even though
205✔
654
        // we had learned them at some point.
205✔
655
        err := l.cfg.PreimageCache.AddPreimages(l.uncommittedPreimages...)
205✔
656
        if err != nil {
205✔
657
                l.log.Errorf("unable to add preimages=%v to cache: %v",
×
658
                        l.uncommittedPreimages, err)
×
659
        }
×
660
}
661

662
// WaitForShutdown blocks until the link finishes shutting down, which includes
663
// termination of all dependent goroutines.
664
func (l *channelLink) WaitForShutdown() {
×
665
        l.cg.WgWait()
×
666
}
×
667

668
// EligibleToForward returns a bool indicating if the channel is able to
669
// actively accept requests to forward HTLC's. We're able to forward HTLC's if
670
// we are eligible to update AND the channel isn't currently flushing the
671
// outgoing half of the channel.
672
//
673
// NOTE: MUST NOT be called from the main event loop.
674
func (l *channelLink) EligibleToForward() bool {
616✔
675
        l.RLock()
616✔
676
        defer l.RUnlock()
616✔
677

616✔
678
        return l.eligibleToForward()
616✔
679
}
616✔
680

681
// eligibleToForward returns a bool indicating if the channel is able to
682
// actively accept requests to forward HTLC's. We're able to forward HTLC's if
683
// we are eligible to update AND the channel isn't currently flushing the
684
// outgoing half of the channel.
685
//
686
// NOTE: MUST be called from the main event loop.
687
func (l *channelLink) eligibleToForward() bool {
616✔
688
        return l.eligibleToUpdate() && !l.IsFlushing(Outgoing)
616✔
689
}
616✔
690

691
// eligibleToUpdate returns a bool indicating if the channel is able to update
692
// channel state. We're able to update channel state if we know the remote
693
// party's next revocation point. Otherwise, we can't initiate new channel
694
// state. We also require that the short channel ID not be the all-zero source
695
// ID, meaning that the channel has had its ID finalized.
696
//
697
// NOTE: MUST be called from the main event loop.
698
func (l *channelLink) eligibleToUpdate() bool {
619✔
699
        return l.channel.RemoteNextRevocation() != nil &&
619✔
700
                l.channel.ShortChanID() != hop.Source &&
619✔
701
                l.isReestablished() &&
619✔
702
                l.quiescer.CanSendUpdates()
619✔
703
}
619✔
704

705
// EnableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in
706
// the specified direction. It returns true if the state was changed and false
707
// if the desired state was already set before the method was called.
708
func (l *channelLink) EnableAdds(linkDirection LinkDirection) bool {
14✔
709
        if linkDirection == Outgoing {
24✔
710
                return l.isOutgoingAddBlocked.Swap(false)
10✔
711
        }
10✔
712

713
        return l.isIncomingAddBlocked.Swap(false)
4✔
714
}
715

716
// DisableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in
717
// the specified direction. It returns true if the state was changed and false
718
// if the desired state was already set before the method was called.
719
func (l *channelLink) DisableAdds(linkDirection LinkDirection) bool {
18✔
720
        if linkDirection == Outgoing {
26✔
721
                return !l.isOutgoingAddBlocked.Swap(true)
8✔
722
        }
8✔
723

724
        return !l.isIncomingAddBlocked.Swap(true)
13✔
725
}
726

727
// IsFlushing returns true when UpdateAddHtlc's are disabled in the direction of
728
// the argument.
729
func (l *channelLink) IsFlushing(linkDirection LinkDirection) bool {
1,594✔
730
        if linkDirection == Outgoing {
2,714✔
731
                return l.isOutgoingAddBlocked.Load()
1,120✔
732
        }
1,120✔
733

734
        return l.isIncomingAddBlocked.Load()
477✔
735
}
736

737
// OnFlushedOnce adds a hook that will be called the next time the channel
738
// state reaches zero htlcs. This hook will only ever be called once. If the
739
// channel state already has zero htlcs, then this will be called immediately.
740
func (l *channelLink) OnFlushedOnce(hook func()) {
4✔
741
        select {
4✔
742
        case l.flushHooks.newTransients <- hook:
4✔
743
        case <-l.cg.Done():
×
744
        }
745
}
746

747
// OnCommitOnce adds a hook that will be called the next time a CommitSig
748
// message is sent in the argument's LinkDirection. This hook will only ever be
749
// called once. If no CommitSig is owed in the argument's LinkDirection, then
750
// we will call this hook be run immediately.
751
func (l *channelLink) OnCommitOnce(direction LinkDirection, hook func()) {
4✔
752
        var queue chan func()
4✔
753

4✔
754
        if direction == Outgoing {
8✔
755
                queue = l.outgoingCommitHooks.newTransients
4✔
756
        } else {
4✔
757
                queue = l.incomingCommitHooks.newTransients
×
758
        }
×
759

760
        select {
4✔
761
        case queue <- hook:
4✔
762
        case <-l.cg.Done():
×
763
        }
764
}
765

766
// InitStfu allows us to initiate quiescence on this link. It returns a receive
767
// only channel that will block until quiescence has been achieved, or
768
// definitively fails.
769
//
770
// This operation has been added to allow channels to be quiesced via RPC. It
771
// may be removed or reworked in the future as RPC initiated quiescence is a
772
// holdover until we have downstream protocols that use it.
773
func (l *channelLink) InitStfu() <-chan fn.Result[lntypes.ChannelParty] {
4✔
774
        req, out := fn.NewReq[fn.Unit, fn.Result[lntypes.ChannelParty]](
4✔
775
                fn.Unit{},
4✔
776
        )
4✔
777

4✔
778
        select {
4✔
779
        case l.quiescenceReqs <- req:
4✔
780
        case <-l.cg.Done():
×
781
                req.Resolve(fn.Err[lntypes.ChannelParty](ErrLinkShuttingDown))
×
782
        }
783

784
        return out
4✔
785
}
786

787
// isReestablished returns true if the link has successfully completed the
788
// channel reestablishment dance.
789
func (l *channelLink) isReestablished() bool {
619✔
790
        return atomic.LoadInt32(&l.reestablished) == 1
619✔
791
}
619✔
792

793
// markReestablished signals that the remote peer has successfully exchanged
794
// channel reestablish messages and that the channel is ready to process
795
// subsequent messages.
796
func (l *channelLink) markReestablished() {
216✔
797
        atomic.StoreInt32(&l.reestablished, 1)
216✔
798
}
216✔
799

800
// IsUnadvertised returns true if the underlying channel is unadvertised.
801
func (l *channelLink) IsUnadvertised() bool {
5✔
802
        state := l.channel.State()
5✔
803
        return state.ChannelFlags&lnwire.FFAnnounceChannel == 0
5✔
804
}
5✔
805

806
// sampleNetworkFee samples the current fee rate on the network to get into the
807
// chain in a timely manner. The returned value is expressed in fee-per-kw, as
808
// this is the native rate used when computing the fee for commitment
809
// transactions, and the second-level HTLC transactions.
810
func (l *channelLink) sampleNetworkFee() (chainfee.SatPerKWeight, error) {
4✔
811
        // We'll first query for the sat/kw recommended to be confirmed within 3
4✔
812
        // blocks.
4✔
813
        feePerKw, err := l.cfg.FeeEstimator.EstimateFeePerKW(3)
4✔
814
        if err != nil {
4✔
815
                return 0, err
×
816
        }
×
817

818
        l.log.Debugf("sampled fee rate for 3 block conf: %v sat/kw",
4✔
819
                int64(feePerKw))
4✔
820

4✔
821
        return feePerKw, nil
4✔
822
}
823

824
// shouldAdjustCommitFee returns true if we should update our commitment fee to
825
// match that of the network fee. We'll only update our commitment fee if the
826
// network fee is +/- 10% to our commitment fee or if our current commitment
827
// fee is below the minimum relay fee.
828
func shouldAdjustCommitFee(netFee, chanFee,
829
        minRelayFee chainfee.SatPerKWeight) bool {
14✔
830

14✔
831
        switch {
14✔
832
        // If the network fee is greater than our current commitment fee and
833
        // our current commitment fee is below the minimum relay fee then
834
        // we should switch to it no matter if it is less than a 10% increase.
835
        case netFee > chanFee && chanFee < minRelayFee:
1✔
836
                return true
1✔
837

838
        // If the network fee is greater than the commitment fee, then we'll
839
        // switch to it if it's at least 10% greater than the commit fee.
840
        case netFee > chanFee && netFee >= (chanFee+(chanFee*10)/100):
4✔
841
                return true
4✔
842

843
        // If the network fee is less than our commitment fee, then we'll
844
        // switch to it if it's at least 10% less than the commitment fee.
845
        case netFee < chanFee && netFee <= (chanFee-(chanFee*10)/100):
2✔
846
                return true
2✔
847

848
        // Otherwise, we won't modify our fee.
849
        default:
7✔
850
                return false
7✔
851
        }
852
}
853

854
// failCb is used to cut down on the argument verbosity.
855
type failCb func(update *lnwire.ChannelUpdate1) lnwire.FailureMessage
856

857
// createFailureWithUpdate creates a ChannelUpdate when failing an incoming or
858
// outgoing HTLC. It may return a FailureMessage that references a channel's
859
// alias. If the channel does not have an alias, then the regular channel
860
// update from disk will be returned.
861
func (l *channelLink) createFailureWithUpdate(incoming bool,
862
        outgoingScid lnwire.ShortChannelID, cb failCb) lnwire.FailureMessage {
25✔
863

25✔
864
        // Determine which SCID to use in case we need to use aliases in the
25✔
865
        // ChannelUpdate.
25✔
866
        scid := outgoingScid
25✔
867
        if incoming {
25✔
868
                scid = l.ShortChanID()
×
869
        }
×
870

871
        // Try using the FailAliasUpdate function. If it returns nil, fallback
872
        // to the non-alias behavior.
873
        update := l.cfg.FailAliasUpdate(scid, incoming)
25✔
874
        if update == nil {
44✔
875
                // Fallback to the non-alias behavior.
19✔
876
                var err error
19✔
877
                update, err = l.cfg.FetchLastChannelUpdate(l.ShortChanID())
19✔
878
                if err != nil {
19✔
879
                        return &lnwire.FailTemporaryNodeFailure{}
×
880
                }
×
881
        }
882

883
        return cb(update)
25✔
884
}
885

886
// syncChanState attempts to synchronize channel states with the remote party.
887
// This method is to be called upon reconnection after the initial funding
888
// flow. We'll compare out commitment chains with the remote party, and re-send
889
// either a danging commit signature, a revocation, or both.
890
func (l *channelLink) syncChanStates(ctx context.Context) error {
173✔
891
        chanState := l.channel.State()
173✔
892

173✔
893
        l.log.Infof("Attempting to re-synchronize channel: %v", chanState)
173✔
894

173✔
895
        // First, we'll generate our ChanSync message to send to the other
173✔
896
        // side. Based on this message, the remote party will decide if they
173✔
897
        // need to retransmit any data or not.
173✔
898
        localChanSyncMsg, err := chanState.ChanSyncMsg()
173✔
899
        if err != nil {
173✔
900
                return fmt.Errorf("unable to generate chan sync message for "+
×
901
                        "ChannelPoint(%v)", l.channel.ChannelPoint())
×
902
        }
×
903
        if err := l.cfg.Peer.SendMessage(true, localChanSyncMsg); err != nil {
173✔
904
                return fmt.Errorf("unable to send chan sync message for "+
×
905
                        "ChannelPoint(%v): %v", l.channel.ChannelPoint(), err)
×
906
        }
×
907

908
        var msgsToReSend []lnwire.Message
173✔
909

173✔
910
        // Next, we'll wait indefinitely to receive the ChanSync message. The
173✔
911
        // first message sent MUST be the ChanSync message.
173✔
912
        select {
173✔
913
        case msg := <-l.upstream:
173✔
914
                l.log.Tracef("Received msg=%v from peer(%x)", msg.MsgType(),
173✔
915
                        l.cfg.Peer.PubKey())
173✔
916

173✔
917
                remoteChanSyncMsg, ok := msg.(*lnwire.ChannelReestablish)
173✔
918
                if !ok {
173✔
919
                        return fmt.Errorf("first message sent to sync "+
×
920
                                "should be ChannelReestablish, instead "+
×
921
                                "received: %T", msg)
×
922
                }
×
923

924
                // If the remote party indicates that they think we haven't
925
                // done any state updates yet, then we'll retransmit the
926
                // channel_ready message first. We do this, as at this point
927
                // we can't be sure if they've really received the
928
                // ChannelReady message.
929
                if remoteChanSyncMsg.NextLocalCommitHeight == 1 &&
173✔
930
                        localChanSyncMsg.NextLocalCommitHeight == 1 &&
173✔
931
                        !l.channel.IsPending() {
340✔
932

167✔
933
                        l.log.Infof("resending ChannelReady message to peer")
167✔
934

167✔
935
                        nextRevocation, err := l.channel.NextRevocationKey()
167✔
936
                        if err != nil {
167✔
937
                                return fmt.Errorf("unable to create next "+
×
938
                                        "revocation: %v", err)
×
939
                        }
×
940

941
                        channelReadyMsg := lnwire.NewChannelReady(
167✔
942
                                l.ChanID(), nextRevocation,
167✔
943
                        )
167✔
944

167✔
945
                        // If this is a taproot channel, then we'll send the
167✔
946
                        // very same nonce that we sent above, as they should
167✔
947
                        // take the latest verification nonce we send.
167✔
948
                        if chanState.ChanType.IsTaproot() {
170✔
949
                                //nolint:ll
3✔
950
                                channelReadyMsg.NextLocalNonce = localChanSyncMsg.LocalNonce
3✔
951
                        }
3✔
952

953
                        // For channels that negotiated the option-scid-alias
954
                        // feature bit, ensure that we send over the alias in
955
                        // the channel_ready message. We'll send the first
956
                        // alias we find for the channel since it does not
957
                        // matter which alias we send. We'll error out if no
958
                        // aliases are found.
959
                        if l.negotiatedAliasFeature() {
170✔
960
                                aliases := l.getAliases()
3✔
961
                                if len(aliases) == 0 {
3✔
962
                                        // This shouldn't happen since we
×
963
                                        // always add at least one alias before
×
964
                                        // the channel reaches the link.
×
965
                                        return fmt.Errorf("no aliases found")
×
966
                                }
×
967

968
                                // getAliases returns a copy of the alias slice
969
                                // so it is ok to use a pointer to the first
970
                                // entry.
971
                                channelReadyMsg.AliasScid = &aliases[0]
3✔
972
                        }
973

974
                        err = l.cfg.Peer.SendMessage(false, channelReadyMsg)
167✔
975
                        if err != nil {
167✔
976
                                return fmt.Errorf("unable to re-send "+
×
977
                                        "ChannelReady: %v", err)
×
978
                        }
×
979
                }
980

981
                // In any case, we'll then process their ChanSync message.
982
                l.log.Info("received re-establishment message from remote side")
173✔
983

173✔
984
                var (
173✔
985
                        openedCircuits []CircuitKey
173✔
986
                        closedCircuits []CircuitKey
173✔
987
                )
173✔
988

173✔
989
                // We've just received a ChanSync message from the remote
173✔
990
                // party, so we'll process the message  in order to determine
173✔
991
                // if we need to re-transmit any messages to the remote party.
173✔
992
                ctx, cancel := l.cg.Create(ctx)
173✔
993
                defer cancel()
173✔
994
                msgsToReSend, openedCircuits, closedCircuits, err =
173✔
995
                        l.channel.ProcessChanSyncMsg(ctx, remoteChanSyncMsg)
173✔
996
                if err != nil {
176✔
997
                        return err
3✔
998
                }
3✔
999

1000
                // Repopulate any identifiers for circuits that may have been
1001
                // opened or unclosed. This may happen if we needed to
1002
                // retransmit a commitment signature message.
1003
                l.openedCircuits = openedCircuits
173✔
1004
                l.closedCircuits = closedCircuits
173✔
1005

173✔
1006
                // Ensure that all packets have been have been removed from the
173✔
1007
                // link's mailbox.
173✔
1008
                if err := l.ackDownStreamPackets(); err != nil {
173✔
1009
                        return err
×
1010
                }
×
1011

1012
                if len(msgsToReSend) > 0 {
178✔
1013
                        l.log.Infof("sending %v updates to synchronize the "+
5✔
1014
                                "state", len(msgsToReSend))
5✔
1015
                }
5✔
1016

1017
                // If we have any messages to retransmit, we'll do so
1018
                // immediately so we return to a synchronized state as soon as
1019
                // possible.
1020
                for _, msg := range msgsToReSend {
184✔
1021
                        err := l.cfg.Peer.SendMessage(false, msg)
11✔
1022
                        if err != nil {
11✔
NEW
1023
                                l.log.Errorf("failed to send %v: %v",
×
NEW
1024
                                        msg.MsgType(), err)
×
NEW
1025
                        }
×
1026
                }
1027

1028
        case <-l.cg.Done():
3✔
1029
                return ErrLinkShuttingDown
3✔
1030
        }
1031

1032
        return nil
173✔
1033
}
1034

1035
// resolveFwdPkgs loads any forwarding packages for this link from disk, and
1036
// reprocesses them in order. The primary goal is to make sure that any HTLCs
1037
// we previously received are reinstated in memory, and forwarded to the switch
1038
// if necessary. After a restart, this will also delete any previously
1039
// completed packages.
1040
func (l *channelLink) resolveFwdPkgs(ctx context.Context) error {
216✔
1041
        fwdPkgs, err := l.channel.LoadFwdPkgs()
216✔
1042
        if err != nil {
217✔
1043
                return err
1✔
1044
        }
1✔
1045

1046
        l.log.Debugf("loaded %d fwd pks", len(fwdPkgs))
215✔
1047

215✔
1048
        for _, fwdPkg := range fwdPkgs {
224✔
1049
                if err := l.resolveFwdPkg(fwdPkg); err != nil {
9✔
1050
                        return err
×
1051
                }
×
1052
        }
1053

1054
        // If any of our reprocessing steps require an update to the commitment
1055
        // txn, we initiate a state transition to capture all relevant changes.
1056
        if l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote) > 0 {
218✔
1057
                return l.updateCommitTx(ctx)
3✔
1058
        }
3✔
1059

1060
        return nil
215✔
1061
}
1062

1063
// resolveFwdPkg interprets the FwdState of the provided package, either
1064
// reprocesses any outstanding htlcs in the package, or performs garbage
1065
// collection on the package.
1066
func (l *channelLink) resolveFwdPkg(fwdPkg *channeldb.FwdPkg) error {
9✔
1067
        // Remove any completed packages to clear up space.
9✔
1068
        if fwdPkg.State == channeldb.FwdStateCompleted {
12✔
1069
                l.log.Debugf("removing completed fwd pkg for height=%d",
3✔
1070
                        fwdPkg.Height)
3✔
1071

3✔
1072
                err := l.channel.RemoveFwdPkgs(fwdPkg.Height)
3✔
1073
                if err != nil {
3✔
1074
                        l.log.Errorf("unable to remove fwd pkg for height=%d: "+
×
1075
                                "%v", fwdPkg.Height, err)
×
1076
                        return err
×
1077
                }
×
1078
        }
1079

1080
        // Otherwise this is either a new package or one has gone through
1081
        // processing, but contains htlcs that need to be restored in memory.
1082
        // We replay this forwarding package to make sure our local mem state
1083
        // is resurrected, we mimic any original responses back to the remote
1084
        // party, and re-forward the relevant HTLCs to the switch.
1085

1086
        // If the package is fully acked but not completed, it must still have
1087
        // settles and fails to propagate.
1088
        if !fwdPkg.SettleFailFilter.IsFull() {
12✔
1089
                l.processRemoteSettleFails(fwdPkg)
3✔
1090
        }
3✔
1091

1092
        // Finally, replay *ALL ADDS* in this forwarding package. The
1093
        // downstream logic is able to filter out any duplicates, but we must
1094
        // shove the entire, original set of adds down the pipeline so that the
1095
        // batch of adds presented to the sphinx router does not ever change.
1096
        if !fwdPkg.AckFilter.IsFull() {
15✔
1097
                l.processRemoteAdds(fwdPkg)
6✔
1098

6✔
1099
                // If the link failed during processing the adds, we must
6✔
1100
                // return to ensure we won't attempted to update the state
6✔
1101
                // further.
6✔
1102
                if l.failed {
6✔
1103
                        return fmt.Errorf("link failed while " +
×
1104
                                "processing remote adds")
×
1105
                }
×
1106
        }
1107

1108
        return nil
9✔
1109
}
1110

1111
// fwdPkgGarbager periodically reads all forwarding packages from disk and
1112
// removes those that can be discarded. It is safe to do this entirely in the
1113
// background, since all state is coordinated on disk. This also ensures the
1114
// link can continue to process messages and interleave database accesses.
1115
//
1116
// NOTE: This MUST be run as a goroutine.
1117
func (l *channelLink) fwdPkgGarbager() {
1✔
1118
        defer l.cg.WgDone()
1✔
1119

1✔
1120
        l.cfg.FwdPkgGCTicker.Resume()
1✔
1121
        defer l.cfg.FwdPkgGCTicker.Stop()
1✔
1122

1✔
1123
        if err := l.loadAndRemove(); err != nil {
2✔
1124
                l.log.Warnf("unable to run initial fwd pkgs gc: %v", err)
1✔
1125
        }
1✔
1126

1127
        for {
2✔
1128
                select {
1✔
UNCOV
1129
                case <-l.cfg.FwdPkgGCTicker.Ticks():
×
UNCOV
1130
                        if err := l.loadAndRemove(); err != nil {
×
UNCOV
1131
                                l.log.Warnf("unable to remove fwd pkgs: %v",
×
UNCOV
1132
                                        err)
×
UNCOV
1133
                                continue
×
1134
                        }
UNCOV
1135
                case <-l.cg.Done():
×
UNCOV
1136
                        return
×
1137
                }
1138
        }
1139
}
1140

1141
// loadAndRemove loads all the channels forwarding packages and determines if
1142
// they can be removed. It is called once before the FwdPkgGCTicker ticks so that
1143
// a longer tick interval can be used.
1144
func (l *channelLink) loadAndRemove() error {
1✔
1145
        fwdPkgs, err := l.channel.LoadFwdPkgs()
1✔
1146
        if err != nil {
2✔
1147
                return err
1✔
1148
        }
1✔
1149

UNCOV
1150
        var removeHeights []uint64
×
UNCOV
1151
        for _, fwdPkg := range fwdPkgs {
×
UNCOV
1152
                if fwdPkg.State != channeldb.FwdStateCompleted {
×
UNCOV
1153
                        continue
×
1154
                }
1155

UNCOV
1156
                removeHeights = append(removeHeights, fwdPkg.Height)
×
1157
        }
1158

1159
        // If removeHeights is empty, return early so we don't use a db
1160
        // transaction.
UNCOV
1161
        if len(removeHeights) == 0 {
×
UNCOV
1162
                return nil
×
UNCOV
1163
        }
×
1164

UNCOV
1165
        return l.channel.RemoveFwdPkgs(removeHeights...)
×
1166
}
1167

1168
// handleChanSyncErr performs the error handling logic in the case where we
1169
// could not successfully syncChanStates with our channel peer.
1170
func (l *channelLink) handleChanSyncErr(err error) {
3✔
1171
        l.log.Warnf("error when syncing channel states: %v", err)
3✔
1172

3✔
1173
        var errDataLoss *lnwallet.ErrCommitSyncLocalDataLoss
3✔
1174

3✔
1175
        switch {
3✔
1176
        case errors.Is(err, ErrLinkShuttingDown):
3✔
1177
                l.log.Debugf("unable to sync channel states, link is " +
3✔
1178
                        "shutting down")
3✔
1179
                return
3✔
1180

1181
        // We failed syncing the commit chains, probably because the remote has
1182
        // lost state. We should force close the channel.
1183
        case errors.Is(err, lnwallet.ErrCommitSyncRemoteDataLoss):
3✔
1184
                fallthrough
3✔
1185

1186
        // The remote sent us an invalid last commit secret, we should force
1187
        // close the channel.
1188
        // TODO(halseth): and permanently ban the peer?
1189
        case errors.Is(err, lnwallet.ErrInvalidLastCommitSecret):
3✔
1190
                fallthrough
3✔
1191

1192
        // The remote sent us a commit point different from what they sent us
1193
        // before.
1194
        // TODO(halseth): ban peer?
1195
        case errors.Is(err, lnwallet.ErrInvalidLocalUnrevokedCommitPoint):
3✔
1196
                // We'll fail the link and tell the peer to force close the
3✔
1197
                // channel. Note that the database state is not updated here,
3✔
1198
                // but will be updated when the close transaction is ready to
3✔
1199
                // avoid that we go down before storing the transaction in the
3✔
1200
                // db.
3✔
1201
                l.failf(
3✔
1202
                        LinkFailureError{
3✔
1203
                                code:          ErrSyncError,
3✔
1204
                                FailureAction: LinkFailureForceClose,
3✔
1205
                        },
3✔
1206
                        "unable to synchronize channel states: %v", err,
3✔
1207
                )
3✔
1208

1209
        // We have lost state and cannot safely force close the channel. Fail
1210
        // the channel and wait for the remote to hopefully force close it. The
1211
        // remote has sent us its latest unrevoked commitment point, and we'll
1212
        // store it in the database, such that we can attempt to recover the
1213
        // funds if the remote force closes the channel.
1214
        case errors.As(err, &errDataLoss):
3✔
1215
                err := l.channel.MarkDataLoss(
3✔
1216
                        errDataLoss.CommitPoint,
3✔
1217
                )
3✔
1218
                if err != nil {
3✔
1219
                        l.log.Errorf("unable to mark channel data loss: %v",
×
1220
                                err)
×
1221
                }
×
1222

1223
        // We determined the commit chains were not possible to sync. We
1224
        // cautiously fail the channel, but don't force close.
1225
        // TODO(halseth): can we safely force close in any cases where this
1226
        // error is returned?
1227
        case errors.Is(err, lnwallet.ErrCannotSyncCommitChains):
×
1228
                if err := l.channel.MarkBorked(); err != nil {
×
1229
                        l.log.Errorf("unable to mark channel borked: %v", err)
×
1230
                }
×
1231

1232
        // Other, unspecified error.
1233
        default:
×
1234
        }
1235

1236
        l.failf(
3✔
1237
                LinkFailureError{
3✔
1238
                        code:          ErrRecoveryError,
3✔
1239
                        FailureAction: LinkFailureForceNone,
3✔
1240
                },
3✔
1241
                "unable to synchronize channel states: %v", err,
3✔
1242
        )
3✔
1243
}
1244

1245
// htlcManager is the primary goroutine which drives a channel's commitment
1246
// update state-machine in response to messages received via several channels.
1247
// This goroutine reads messages from the upstream (remote) peer, and also from
1248
// downstream channel managed by the channel link. In the event that an htlc
1249
// needs to be forwarded, then send-only forward handler is used which sends
1250
// htlc packets to the switch. Additionally, this goroutine handles acting upon
1251
// all timeouts for any active HTLCs, manages the channel's revocation window,
1252
// and also the htlc trickle queue+timer for this active channels.
1253
//
1254
// NOTE: This MUST be run as a goroutine.
1255
func (l *channelLink) htlcManager(ctx context.Context) {
216✔
1256
        defer func() {
423✔
1257
                l.cfg.BatchTicker.Stop()
207✔
1258
                l.cg.WgDone()
207✔
1259
                l.log.Infof("exited")
207✔
1260
        }()
207✔
1261

1262
        l.log.Infof("HTLC manager started, bandwidth=%v", l.Bandwidth())
216✔
1263

216✔
1264
        // Notify any clients that the link is now in the switch via an
216✔
1265
        // ActiveLinkEvent. We'll also defer an inactive link notification for
216✔
1266
        // when the link exits to ensure that every active notification is
216✔
1267
        // matched by an inactive one.
216✔
1268
        l.cfg.NotifyActiveLink(l.ChannelPoint())
216✔
1269
        defer l.cfg.NotifyInactiveLinkEvent(l.ChannelPoint())
216✔
1270

216✔
1271
        // If the link is not started for the first time, we need to take extra
216✔
1272
        // steps to resume its state.
216✔
1273
        err := l.resumeLink(ctx)
216✔
1274
        if err != nil {
220✔
1275
                l.log.Errorf("resuming link failed: %v", err)
4✔
1276
                return
4✔
1277
        }
4✔
1278

1279
        // Now that we've received both channel_ready and channel reestablish,
1280
        // we can go ahead and send the active channel notification. We'll also
1281
        // defer the inactive notification for when the link exits to ensure
1282
        // that every active notification is matched by an inactive one.
1283
        l.cfg.NotifyActiveChannel(l.ChannelPoint())
215✔
1284
        defer l.cfg.NotifyInactiveChannel(l.ChannelPoint())
215✔
1285

215✔
1286
        for {
4,378✔
1287
                // We must always check if we failed at some point processing
4,163✔
1288
                // the last update before processing the next.
4,163✔
1289
                if l.failed {
4,177✔
1290
                        l.log.Errorf("link failed, exiting htlcManager")
14✔
1291
                        return
14✔
1292
                }
14✔
1293

1294
                // Pause or resume the batch ticker.
1295
                l.toggleBatchTicker()
4,152✔
1296

4,152✔
1297
                select {
4,152✔
1298
                // We have a new hook that needs to be run when we reach a clean
1299
                // channel state.
1300
                case hook := <-l.flushHooks.newTransients:
4✔
1301
                        if l.channel.IsChannelClean() {
7✔
1302
                                hook()
3✔
1303
                        } else {
7✔
1304
                                l.flushHooks.alloc(hook)
4✔
1305
                        }
4✔
1306

1307
                // We have a new hook that needs to be run when we have
1308
                // committed all of our updates.
1309
                case hook := <-l.outgoingCommitHooks.newTransients:
4✔
1310
                        if !l.channel.OweCommitment() {
7✔
1311
                                hook()
3✔
1312
                        } else {
4✔
1313
                                l.outgoingCommitHooks.alloc(hook)
1✔
1314
                        }
1✔
1315

1316
                // We have a new hook that needs to be run when our peer has
1317
                // committed all of their updates.
1318
                case hook := <-l.incomingCommitHooks.newTransients:
×
1319
                        if !l.channel.NeedCommitment() {
×
1320
                                hook()
×
1321
                        } else {
×
1322
                                l.incomingCommitHooks.alloc(hook)
×
1323
                        }
×
1324

1325
                // Our update fee timer has fired, so we'll check the network
1326
                // fee to see if we should adjust our commitment fee.
1327
                case <-l.updateFeeTimer.C:
4✔
1328
                        l.updateFeeTimer.Reset(l.randomFeeUpdateTimeout())
4✔
1329
                        err := l.handleUpdateFee(ctx)
4✔
1330
                        if err != nil {
4✔
NEW
1331
                                l.log.Errorf("failed to handle update fee: "+
×
NEW
1332
                                        "%v", err)
×
UNCOV
1333
                        }
×
1334

1335
                // The underlying channel has notified us of a unilateral close
1336
                // carried out by the remote peer. In the case of such an
1337
                // event, we'll wipe the channel state from the peer, and mark
1338
                // the contract as fully settled. Afterwards we can exit.
1339
                //
1340
                // TODO(roasbeef): add force closure? also breach?
1341
                case <-l.cfg.ChainEvents.RemoteUnilateralClosure:
3✔
1342
                        l.log.Warnf("remote peer has closed on-chain")
3✔
1343

3✔
1344
                        // TODO(roasbeef): remove all together
3✔
1345
                        go func() {
6✔
1346
                                chanPoint := l.channel.ChannelPoint()
3✔
1347
                                l.cfg.Peer.WipeChannel(&chanPoint)
3✔
1348
                        }()
3✔
1349

1350
                        return
3✔
1351

1352
                case <-l.cfg.BatchTicker.Ticks():
206✔
1353
                        // Attempt to extend the remote commitment chain
206✔
1354
                        // including all the currently pending entries. If the
206✔
1355
                        // send was unsuccessful, then abandon the update,
206✔
1356
                        // waiting for the revocation window to open up.
206✔
1357
                        if !l.updateCommitTxOrFail(ctx) {
206✔
1358
                                return
×
1359
                        }
×
1360

1361
                case <-l.cfg.PendingCommitTicker.Ticks():
1✔
1362
                        l.failf(
1✔
1363
                                LinkFailureError{
1✔
1364
                                        code:          ErrRemoteUnresponsive,
1✔
1365
                                        FailureAction: LinkFailureDisconnect,
1✔
1366
                                },
1✔
1367
                                "unable to complete dance",
1✔
1368
                        )
1✔
1369
                        return
1✔
1370

1371
                // A message from the switch was just received. This indicates
1372
                // that the link is an intermediate hop in a multi-hop HTLC
1373
                // circuit.
1374
                case pkt := <-l.downstream:
524✔
1375
                        l.handleDownstreamPkt(ctx, pkt)
524✔
1376

1377
                // A message from the connected peer was just received. This
1378
                // indicates that we have a new incoming HTLC, either directly
1379
                // for us, or part of a multi-hop HTLC circuit.
1380
                case msg := <-l.upstream:
3,165✔
1381
                        l.handleUpstreamMsg(ctx, msg)
3,165✔
1382

1383
                // A htlc resolution is received. This means that we now have a
1384
                // resolution for a previously accepted htlc.
1385
                case hodlItem := <-l.hodlQueue.ChanOut():
58✔
1386
                        err := l.handleHtlcResolution(ctx, hodlItem)
58✔
1387
                        if err != nil {
58✔
NEW
1388
                                l.log.Errorf("failed to handle htlc "+
×
NEW
1389
                                        "resolution: %v", err)
×
UNCOV
1390
                        }
×
1391

1392
                // A user-initiated quiescence request is received. We now
1393
                // forward it to the quiescer.
1394
                case qReq := <-l.quiescenceReqs:
4✔
1395
                        err := l.handleQuiescenceReq(qReq)
4✔
1396
                        if err != nil {
4✔
NEW
1397
                                l.log.Errorf("failed handle quiescence "+
×
NEW
1398
                                        "req: %v", err)
×
UNCOV
1399
                        }
×
1400

1401
                case <-l.cg.Done():
194✔
1402
                        return
194✔
1403
                }
1404
        }
1405
}
1406

1407
// processHodlQueue processes a received htlc resolution and continues reading
1408
// from the hodl queue until no more resolutions remain. When this function
1409
// returns without an error, the commit tx should be updated.
1410
func (l *channelLink) processHodlQueue(ctx context.Context,
1411
        firstResolution invoices.HtlcResolution) error {
58✔
1412

58✔
1413
        // Try to read all waiting resolution messages, so that they can all be
58✔
1414
        // processed in a single commitment tx update.
58✔
1415
        htlcResolution := firstResolution
58✔
1416
loop:
58✔
1417
        for {
116✔
1418
                // Lookup all hodl htlcs that can be failed or settled with this event.
58✔
1419
                // The hodl htlc must be present in the map.
58✔
1420
                circuitKey := htlcResolution.CircuitKey()
58✔
1421
                hodlHtlc, ok := l.hodlMap[circuitKey]
58✔
1422
                if !ok {
58✔
1423
                        return fmt.Errorf("hodl htlc not found: %v", circuitKey)
×
1424
                }
×
1425

1426
                if err := l.processHtlcResolution(htlcResolution, hodlHtlc); err != nil {
58✔
1427
                        return err
×
1428
                }
×
1429

1430
                // Clean up hodl map.
1431
                delete(l.hodlMap, circuitKey)
58✔
1432

58✔
1433
                select {
58✔
1434
                case item := <-l.hodlQueue.ChanOut():
3✔
1435
                        htlcResolution = item.(invoices.HtlcResolution)
3✔
1436

1437
                // No need to process it if the link is broken.
1438
                case <-l.cg.Done():
×
1439
                        return ErrLinkShuttingDown
×
1440

1441
                default:
58✔
1442
                        break loop
58✔
1443
                }
1444
        }
1445

1446
        // Update the commitment tx.
1447
        if err := l.updateCommitTx(ctx); err != nil {
58✔
UNCOV
1448
                return err
×
UNCOV
1449
        }
×
1450

1451
        return nil
58✔
1452
}
1453

1454
// processHtlcResolution applies a received htlc resolution to the provided
1455
// htlc. When this function returns without an error, the commit tx should be
1456
// updated.
1457
func (l *channelLink) processHtlcResolution(resolution invoices.HtlcResolution,
1458
        htlc hodlHtlc) error {
204✔
1459

204✔
1460
        circuitKey := resolution.CircuitKey()
204✔
1461

204✔
1462
        // Determine required action for the resolution based on the type of
204✔
1463
        // resolution we have received.
204✔
1464
        switch res := resolution.(type) {
204✔
1465
        // Settle htlcs that returned a settle resolution using the preimage
1466
        // in the resolution.
1467
        case *invoices.HtlcSettleResolution:
200✔
1468
                l.log.Debugf("received settle resolution for %v "+
200✔
1469
                        "with outcome: %v", circuitKey, res.Outcome)
200✔
1470

200✔
1471
                return l.settleHTLC(
200✔
1472
                        res.Preimage, htlc.add.ID, htlc.sourceRef,
200✔
1473
                )
200✔
1474

1475
        // For htlc failures, we get the relevant failure message based
1476
        // on the failure resolution and then fail the htlc.
1477
        case *invoices.HtlcFailResolution:
7✔
1478
                l.log.Debugf("received cancel resolution for "+
7✔
1479
                        "%v with outcome: %v", circuitKey, res.Outcome)
7✔
1480

7✔
1481
                // Get the lnwire failure message based on the resolution
7✔
1482
                // result.
7✔
1483
                failure := getResolutionFailure(res, htlc.add.Amount)
7✔
1484

7✔
1485
                l.sendHTLCError(
7✔
1486
                        htlc.add, htlc.sourceRef, failure, htlc.obfuscator,
7✔
1487
                        true,
7✔
1488
                )
7✔
1489
                return nil
7✔
1490

1491
        // Fail if we do not get a settle of fail resolution, since we
1492
        // are only expecting to handle settles and fails.
1493
        default:
×
1494
                return fmt.Errorf("unknown htlc resolution type: %T",
×
1495
                        resolution)
×
1496
        }
1497
}
1498

1499
// getResolutionFailure returns the wire message that a htlc resolution should
1500
// be failed with.
1501
func getResolutionFailure(resolution *invoices.HtlcFailResolution,
1502
        amount lnwire.MilliSatoshi) *LinkError {
7✔
1503

7✔
1504
        // If the resolution has been resolved as part of a MPP timeout,
7✔
1505
        // we need to fail the htlc with lnwire.FailMppTimeout.
7✔
1506
        if resolution.Outcome == invoices.ResultMppTimeout {
7✔
1507
                return NewDetailedLinkError(
×
1508
                        &lnwire.FailMPPTimeout{}, resolution.Outcome,
×
1509
                )
×
1510
        }
×
1511

1512
        // If the htlc is not a MPP timeout, we fail it with
1513
        // FailIncorrectDetails. This error is sent for invoice payment
1514
        // failures such as underpayment/ expiry too soon and hodl invoices
1515
        // (which return FailIncorrectDetails to avoid leaking information).
1516
        incorrectDetails := lnwire.NewFailIncorrectDetails(
7✔
1517
                amount, uint32(resolution.AcceptHeight),
7✔
1518
        )
7✔
1519

7✔
1520
        return NewDetailedLinkError(incorrectDetails, resolution.Outcome)
7✔
1521
}
1522

1523
// randomFeeUpdateTimeout returns a random timeout between the bounds defined
1524
// within the link's configuration that will be used to determine when the link
1525
// should propose an update to its commitment fee rate.
1526
func (l *channelLink) randomFeeUpdateTimeout() time.Duration {
220✔
1527
        lower := int64(l.cfg.MinUpdateTimeout)
220✔
1528
        upper := int64(l.cfg.MaxUpdateTimeout)
220✔
1529
        return time.Duration(prand.Int63n(upper-lower) + lower)
220✔
1530
}
220✔
1531

1532
// handleDownstreamUpdateAdd processes an UpdateAddHTLC packet sent from the
1533
// downstream HTLC Switch.
1534
func (l *channelLink) handleDownstreamUpdateAdd(ctx context.Context,
1535
        pkt *htlcPacket) error {
483✔
1536

483✔
1537
        htlc, ok := pkt.htlc.(*lnwire.UpdateAddHTLC)
483✔
1538
        if !ok {
483✔
1539
                return errors.New("not an UpdateAddHTLC packet")
×
1540
        }
×
1541

1542
        // If we are flushing the link in the outgoing direction or we have
1543
        // already sent Stfu, then we can't add new htlcs to the link and we
1544
        // need to bounce it.
1545
        if l.IsFlushing(Outgoing) || !l.quiescer.CanSendUpdates() {
483✔
1546
                l.mailBox.FailAdd(pkt)
×
1547

×
1548
                return NewDetailedLinkError(
×
1549
                        &lnwire.FailTemporaryChannelFailure{},
×
1550
                        OutgoingFailureLinkNotEligible,
×
1551
                )
×
1552
        }
×
1553

1554
        // If hodl.AddOutgoing mode is active, we exit early to simulate
1555
        // arbitrary delays between the switch adding an ADD to the
1556
        // mailbox, and the HTLC being added to the commitment state.
1557
        if l.cfg.HodlMask.Active(hodl.AddOutgoing) {
483✔
1558
                l.log.Warnf(hodl.AddOutgoing.Warning())
×
1559
                l.mailBox.AckPacket(pkt.inKey())
×
1560
                return nil
×
1561
        }
×
1562

1563
        // Check if we can add the HTLC here without exceededing the max fee
1564
        // exposure threshold.
1565
        if l.isOverexposedWithHtlc(htlc, false) {
487✔
1566
                l.log.Debugf("Unable to handle downstream HTLC - max fee " +
4✔
1567
                        "exposure exceeded")
4✔
1568

4✔
1569
                l.mailBox.FailAdd(pkt)
4✔
1570

4✔
1571
                return NewDetailedLinkError(
4✔
1572
                        lnwire.NewTemporaryChannelFailure(nil),
4✔
1573
                        OutgoingFailureDownstreamHtlcAdd,
4✔
1574
                )
4✔
1575
        }
4✔
1576

1577
        // A new payment has been initiated via the downstream channel,
1578
        // so we add the new HTLC to our local log, then update the
1579
        // commitment chains.
1580
        htlc.ChanID = l.ChanID()
479✔
1581
        openCircuitRef := pkt.inKey()
479✔
1582

479✔
1583
        // We enforce the fee buffer for the commitment transaction because
479✔
1584
        // we are in control of adding this htlc. Nothing has locked-in yet so
479✔
1585
        // we can securely enforce the fee buffer which is only relevant if we
479✔
1586
        // are the initiator of the channel.
479✔
1587
        index, err := l.channel.AddHTLC(htlc, &openCircuitRef)
479✔
1588
        if err != nil {
483✔
1589
                // The HTLC was unable to be added to the state machine,
4✔
1590
                // as a result, we'll signal the switch to cancel the
4✔
1591
                // pending payment.
4✔
1592
                l.log.Warnf("Unable to handle downstream add HTLC: %v",
4✔
1593
                        err)
4✔
1594

4✔
1595
                // Remove this packet from the link's mailbox, this
4✔
1596
                // prevents it from being reprocessed if the link
4✔
1597
                // restarts and resets it mailbox. If this response
4✔
1598
                // doesn't make it back to the originating link, it will
4✔
1599
                // be rejected upon attempting to reforward the Add to
4✔
1600
                // the switch, since the circuit was never fully opened,
4✔
1601
                // and the forwarding package shows it as
4✔
1602
                // unacknowledged.
4✔
1603
                l.mailBox.FailAdd(pkt)
4✔
1604

4✔
1605
                return NewDetailedLinkError(
4✔
1606
                        lnwire.NewTemporaryChannelFailure(nil),
4✔
1607
                        OutgoingFailureDownstreamHtlcAdd,
4✔
1608
                )
4✔
1609
        }
4✔
1610

1611
        l.log.Tracef("received downstream htlc: payment_hash=%x, "+
478✔
1612
                "local_log_index=%v, pend_updates=%v",
478✔
1613
                htlc.PaymentHash[:], index,
478✔
1614
                l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote))
478✔
1615

478✔
1616
        pkt.outgoingChanID = l.ShortChanID()
478✔
1617
        pkt.outgoingHTLCID = index
478✔
1618
        htlc.ID = index
478✔
1619

478✔
1620
        l.log.Debugf("queueing keystone of ADD open circuit: %s->%s",
478✔
1621
                pkt.inKey(), pkt.outKey())
478✔
1622

478✔
1623
        l.openedCircuits = append(l.openedCircuits, pkt.inKey())
478✔
1624
        l.keystoneBatch = append(l.keystoneBatch, pkt.keystone())
478✔
1625

478✔
1626
        err = l.cfg.Peer.SendMessage(false, htlc)
478✔
1627
        if err != nil {
478✔
NEW
1628
                l.log.Errorf("failed to send UpdateAddHTLC: %v", err)
×
NEW
1629
        }
×
1630

1631
        // Send a forward event notification to htlcNotifier.
1632
        l.cfg.HtlcNotifier.NotifyForwardingEvent(
478✔
1633
                newHtlcKey(pkt),
478✔
1634
                HtlcInfo{
478✔
1635
                        IncomingTimeLock: pkt.incomingTimeout,
478✔
1636
                        IncomingAmt:      pkt.incomingAmount,
478✔
1637
                        OutgoingTimeLock: htlc.Expiry,
478✔
1638
                        OutgoingAmt:      htlc.Amount,
478✔
1639
                },
478✔
1640
                getEventType(pkt),
478✔
1641
        )
478✔
1642

478✔
1643
        l.tryBatchUpdateCommitTx(ctx)
478✔
1644

478✔
1645
        return nil
478✔
1646
}
1647

1648
// handleDownstreamPkt processes an HTLC packet sent from the downstream HTLC
1649
// Switch. Possible messages sent by the switch include requests to forward new
1650
// HTLCs, timeout previously cleared HTLCs, and finally to settle currently
1651
// cleared HTLCs with the upstream peer.
1652
//
1653
// TODO(roasbeef): add sync ntfn to ensure switch always has consistent view?
1654
func (l *channelLink) handleDownstreamPkt(ctx context.Context,
1655
        pkt *htlcPacket) {
524✔
1656

524✔
1657
        if pkt.htlc.MsgType().IsChannelUpdate() &&
524✔
1658
                !l.quiescer.CanSendUpdates() {
524✔
1659

×
1660
                l.log.Warnf("unable to process channel update. "+
×
1661
                        "ChannelID=%v is quiescent.", l.ChanID)
×
1662

×
1663
                return
×
1664
        }
×
1665

1666
        switch htlc := pkt.htlc.(type) {
524✔
1667
        case *lnwire.UpdateAddHTLC:
483✔
1668
                // Handle add message. The returned error can be ignored,
483✔
1669
                // because it is also sent through the mailbox.
483✔
1670
                _ = l.handleDownstreamUpdateAdd(ctx, pkt)
483✔
1671

1672
        case *lnwire.UpdateFulfillHTLC:
26✔
1673
                l.processLocalUpdateFulfillHTLC(ctx, pkt, htlc)
26✔
1674

1675
        case *lnwire.UpdateFailHTLC:
21✔
1676
                l.processLocalUpdateFailHTLC(ctx, pkt, htlc)
21✔
1677
        }
1678
}
1679

1680
// tryBatchUpdateCommitTx updates the commitment transaction if the batch is
1681
// full.
1682
func (l *channelLink) tryBatchUpdateCommitTx(ctx context.Context) {
478✔
1683
        pending := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)
478✔
1684
        if pending < uint64(l.cfg.BatchSize) {
921✔
1685
                return
443✔
1686
        }
443✔
1687

1688
        l.updateCommitTxOrFail(ctx)
38✔
1689
}
1690

1691
// cleanupSpuriousResponse attempts to ack any AddRef or SettleFailRef
1692
// associated with this packet. If successful in doing so, it will also purge
1693
// the open circuit from the circuit map and remove the packet from the link's
1694
// mailbox.
1695
func (l *channelLink) cleanupSpuriousResponse(pkt *htlcPacket) {
2✔
1696
        inKey := pkt.inKey()
2✔
1697

2✔
1698
        l.log.Debugf("cleaning up spurious response for incoming "+
2✔
1699
                "circuit-key=%v", inKey)
2✔
1700

2✔
1701
        // If the htlc packet doesn't have a source reference, it is unsafe to
2✔
1702
        // proceed, as skipping this ack may cause the htlc to be reforwarded.
2✔
1703
        if pkt.sourceRef == nil {
3✔
1704
                l.log.Errorf("unable to cleanup response for incoming "+
1✔
1705
                        "circuit-key=%v, does not contain source reference",
1✔
1706
                        inKey)
1✔
1707
                return
1✔
1708
        }
1✔
1709

1710
        // If the source reference is present,  we will try to prevent this link
1711
        // from resending the packet to the switch. To do so, we ack the AddRef
1712
        // of the incoming HTLC belonging to this link.
1713
        err := l.channel.AckAddHtlcs(*pkt.sourceRef)
1✔
1714
        if err != nil {
1✔
1715
                l.log.Errorf("unable to ack AddRef for incoming "+
×
1716
                        "circuit-key=%v: %v", inKey, err)
×
1717

×
1718
                // If this operation failed, it is unsafe to attempt removal of
×
1719
                // the destination reference or circuit, so we exit early. The
×
1720
                // cleanup may proceed with a different packet in the future
×
1721
                // that succeeds on this step.
×
1722
                return
×
1723
        }
×
1724

1725
        // Now that we know this link will stop retransmitting Adds to the
1726
        // switch, we can begin to teardown the response reference and circuit
1727
        // map.
1728
        //
1729
        // If the packet includes a destination reference, then a response for
1730
        // this HTLC was locked into the outgoing channel. Attempt to remove
1731
        // this reference, so we stop retransmitting the response internally.
1732
        // Even if this fails, we will proceed in trying to delete the circuit.
1733
        // When retransmitting responses, the destination references will be
1734
        // cleaned up if an open circuit is not found in the circuit map.
1735
        if pkt.destRef != nil {
1✔
1736
                err := l.channel.AckSettleFails(*pkt.destRef)
×
1737
                if err != nil {
×
1738
                        l.log.Errorf("unable to ack SettleFailRef "+
×
1739
                                "for incoming circuit-key=%v: %v",
×
1740
                                inKey, err)
×
1741
                }
×
1742
        }
1743

1744
        l.log.Debugf("deleting circuit for incoming circuit-key=%x", inKey)
1✔
1745

1✔
1746
        // With all known references acked, we can now safely delete the circuit
1✔
1747
        // from the switch's circuit map, as the state is no longer needed.
1✔
1748
        err = l.cfg.Circuits.DeleteCircuits(inKey)
1✔
1749
        if err != nil {
1✔
1750
                l.log.Errorf("unable to delete circuit for "+
×
1751
                        "circuit-key=%v: %v", inKey, err)
×
1752
        }
×
1753
}
1754

1755
// handleUpstreamMsg processes wire messages related to commitment state
1756
// updates from the upstream peer. The upstream peer is the peer whom we have a
1757
// direct channel with, updating our respective commitment chains.
1758
func (l *channelLink) handleUpstreamMsg(ctx context.Context,
1759
        msg lnwire.Message) {
3,165✔
1760

3,165✔
1761
        l.log.Tracef("receive upstream msg %v, handling now... ", msg.MsgType())
3,165✔
1762
        defer l.log.Tracef("handled upstream msg %v", msg.MsgType())
3,165✔
1763

3,165✔
1764
        // First check if the message is an update and we are capable of
3,165✔
1765
        // receiving updates right now.
3,165✔
1766
        if msg.MsgType().IsChannelUpdate() && !l.quiescer.CanRecvUpdates() {
3,165✔
1767
                l.stfuFailf("update received after stfu: %T", msg)
×
1768
                return
×
1769
        }
×
1770

1771
        var err error
3,165✔
1772

3,165✔
1773
        switch msg := msg.(type) {
3,165✔
1774
        case *lnwire.UpdateAddHTLC:
453✔
1775
                err = l.processRemoteUpdateAddHTLC(msg)
453✔
1776

1777
        case *lnwire.UpdateFulfillHTLC:
230✔
1778
                err = l.processRemoteUpdateFulfillHTLC(msg)
230✔
1779

1780
        case *lnwire.UpdateFailMalformedHTLC:
6✔
1781
                err = l.processRemoteUpdateFailMalformedHTLC(msg)
6✔
1782

1783
        case *lnwire.UpdateFailHTLC:
123✔
1784
                err = l.processRemoteUpdateFailHTLC(msg)
123✔
1785

1786
        case *lnwire.CommitSig:
1,187✔
1787
                err = l.processRemoteCommitSig(ctx, msg)
1,187✔
1788

1789
        case *lnwire.RevokeAndAck:
1,175✔
1790
                err = l.processRemoteRevokeAndAck(ctx, msg)
1,175✔
1791

1792
        case *lnwire.UpdateFee:
3✔
1793
                err = l.processRemoteUpdateFee(msg)
3✔
1794

1795
        case *lnwire.Stfu:
5✔
1796
                err = l.handleStfu(msg)
5✔
1797
                if err != nil {
5✔
NEW
1798
                        l.stfuFailf("handleStfu: %v", err.Error())
×
1799
                }
×
1800

1801
        // In the case where we receive a warning message from our peer, just
1802
        // log it and move on. We choose not to disconnect from our peer,
1803
        // although we "MAY" do so according to the specification.
1804
        case *lnwire.Warning:
1✔
1805
                l.log.Warnf("received warning message from peer: %v",
1✔
1806
                        msg.Warning())
1✔
1807

1808
        case *lnwire.Error:
2✔
1809
                l.processRemoteError(msg)
2✔
1810

NEW
1811
        default:
×
NEW
1812
                l.log.Warnf("received unknown message of type %T", msg)
×
1813
        }
1814

1815
        if err != nil {
3,170✔
1816
                l.log.Errorf("failed to process remote %v: %v", msg.MsgType(),
5✔
1817
                        err)
5✔
1818
        }
5✔
1819
}
1820

1821
// handleStfu implements the top-level logic for handling the Stfu message from
1822
// our peer.
1823
func (l *channelLink) handleStfu(stfu *lnwire.Stfu) error {
5✔
1824
        if !l.noDanglingUpdates(lntypes.Remote) {
5✔
NEW
1825
                return ErrPendingRemoteUpdates
×
NEW
1826
        }
×
1827
        err := l.quiescer.RecvStfu(*stfu)
5✔
1828
        if err != nil {
5✔
NEW
1829
                return err
×
NEW
1830
        }
×
1831

1832
        // If we can immediately send an Stfu response back, we will.
1833
        if l.noDanglingUpdates(lntypes.Local) {
9✔
1834
                return l.quiescer.SendOwedStfu()
4✔
1835
        }
4✔
1836

1837
        return nil
1✔
1838
}
1839

1840
// stfuFailf fails the link in the case where the requirements of the quiescence
1841
// protocol are violated. In all cases we opt to drop the connection as only
1842
// link state (as opposed to channel state) is affected.
NEW
1843
func (l *channelLink) stfuFailf(format string, args ...interface{}) {
×
NEW
1844
        l.failf(LinkFailureError{
×
NEW
1845
                code:             ErrStfuViolation,
×
NEW
1846
                FailureAction:    LinkFailureDisconnect,
×
NEW
1847
                PermanentFailure: false,
×
NEW
1848
                Warning:          true,
×
NEW
1849
        }, format, args...)
×
NEW
1850
}
×
1851

1852
// noDanglingUpdates returns true when there are 0 updates that were originally
1853
// issued by whose on either the Local or Remote commitment transaction.
1854
func (l *channelLink) noDanglingUpdates(whose lntypes.ChannelParty) bool {
1,191✔
1855
        pendingOnLocal := l.channel.NumPendingUpdates(
1,191✔
1856
                whose, lntypes.Local,
1,191✔
1857
        )
1,191✔
1858
        pendingOnRemote := l.channel.NumPendingUpdates(
1,191✔
1859
                whose, lntypes.Remote,
1,191✔
1860
        )
1,191✔
1861

1,191✔
1862
        return pendingOnLocal == 0 && pendingOnRemote == 0
1,191✔
1863
}
1,191✔
1864

1865
// ackDownStreamPackets is responsible for removing htlcs from a link's mailbox
1866
// for packets delivered from server, and cleaning up any circuits closed by
1867
// signing a previous commitment txn. This method ensures that the circuits are
1868
// removed from the circuit map before removing them from the link's mailbox,
1869
// otherwise it could be possible for some circuit to be missed if this link
1870
// flaps.
1871
func (l *channelLink) ackDownStreamPackets() error {
1,365✔
1872
        // First, remove the downstream Add packets that were included in the
1,365✔
1873
        // previous commitment signature. This will prevent the Adds from being
1,365✔
1874
        // replayed if this link disconnects.
1,365✔
1875
        for _, inKey := range l.openedCircuits {
1,832✔
1876
                // In order to test the sphinx replay logic of the remote
467✔
1877
                // party, unsafe replay does not acknowledge the packets from
467✔
1878
                // the mailbox. We can then force a replay of any Add packets
467✔
1879
                // held in memory by disconnecting and reconnecting the link.
467✔
1880
                if l.cfg.UnsafeReplay {
470✔
1881
                        continue
3✔
1882
                }
1883

1884
                l.log.Debugf("removing Add packet %s from mailbox", inKey)
467✔
1885
                l.mailBox.AckPacket(inKey)
467✔
1886
        }
1887

1888
        // Now, we will delete all circuits closed by the previous commitment
1889
        // signature, which is the result of downstream Settle/Fail packets. We
1890
        // batch them here to ensure circuits are closed atomically and for
1891
        // performance.
1892
        err := l.cfg.Circuits.DeleteCircuits(l.closedCircuits...)
1,365✔
1893
        switch err {
1,365✔
1894
        case nil:
1,365✔
1895
                // Successful deletion.
1896

1897
        default:
×
1898
                l.log.Errorf("unable to delete %d circuits: %v",
×
1899
                        len(l.closedCircuits), err)
×
1900
                return err
×
1901
        }
1902

1903
        // With the circuits removed from memory and disk, we now ack any
1904
        // Settle/Fails in the mailbox to ensure they do not get redelivered
1905
        // after startup. If forgive is enabled and we've reached this point,
1906
        // the circuits must have been removed at some point, so it is now safe
1907
        // to un-queue the corresponding Settle/Fails.
1908
        for _, inKey := range l.closedCircuits {
1,407✔
1909
                l.log.Debugf("removing Fail/Settle packet %s from mailbox",
42✔
1910
                        inKey)
42✔
1911
                l.mailBox.AckPacket(inKey)
42✔
1912
        }
42✔
1913

1914
        // Lastly, reset our buffers to be empty while keeping any acquired
1915
        // growth in the backing array.
1916
        l.openedCircuits = l.openedCircuits[:0]
1,365✔
1917
        l.closedCircuits = l.closedCircuits[:0]
1,365✔
1918

1,365✔
1919
        return nil
1,365✔
1920
}
1921

1922
// updateCommitTxOrFail updates the commitment tx and if that fails, it fails
1923
// the link.
1924
func (l *channelLink) updateCommitTxOrFail(ctx context.Context) bool {
1,226✔
1925
        err := l.updateCommitTx(ctx)
1,226✔
1926
        switch {
1,226✔
1927
        // No error encountered, success.
1928
        case err == nil:
1,217✔
1929

1930
        // A duplicate keystone error should be resolved and is not fatal, so
1931
        // we won't send an Error message to the peer.
NEW
1932
        case errors.Is(err, ErrDuplicateKeystone):
×
1933
                l.failf(LinkFailureError{code: ErrCircuitError},
×
1934
                        "temporary circuit error: %v", err)
×
1935
                return false
×
1936

1937
        // Any other error is treated results in an Error message being sent to
1938
        // the peer.
1939
        default:
9✔
1940
                l.failf(LinkFailureError{code: ErrInternalError},
9✔
1941
                        "unable to update commitment: %v", err)
9✔
1942
                return false
9✔
1943
        }
1944

1945
        return true
1,217✔
1946
}
1947

1948
// updateCommitTx signs, then sends an update to the remote peer adding a new
1949
// commitment to their commitment chain which includes all the latest updates
1950
// we've received+processed up to this point.
1951
func (l *channelLink) updateCommitTx(ctx context.Context) error {
1,284✔
1952
        // Preemptively write all pending keystones to disk, just in case the
1,284✔
1953
        // HTLCs we have in memory are included in the subsequent attempt to
1,284✔
1954
        // sign a commitment state.
1,284✔
1955
        err := l.cfg.Circuits.OpenCircuits(l.keystoneBatch...)
1,284✔
1956
        if err != nil {
1,284✔
1957
                // If ErrDuplicateKeystone is returned, the caller will catch
×
1958
                // it.
×
1959
                return err
×
1960
        }
×
1961

1962
        // Reset the batch, but keep the backing buffer to avoid reallocating.
1963
        l.keystoneBatch = l.keystoneBatch[:0]
1,284✔
1964

1,284✔
1965
        // If hodl.Commit mode is active, we will refrain from attempting to
1,284✔
1966
        // commit any in-memory modifications to the channel state. Exiting here
1,284✔
1967
        // permits testing of either the switch or link's ability to trim
1,284✔
1968
        // circuits that have been opened, but unsuccessfully committed.
1,284✔
1969
        if l.cfg.HodlMask.Active(hodl.Commit) {
1,291✔
1970
                l.log.Warnf(hodl.Commit.Warning())
7✔
1971
                return nil
7✔
1972
        }
7✔
1973

1974
        ctx, done := l.cg.Create(ctx)
1,280✔
1975
        defer done()
1,280✔
1976

1,280✔
1977
        newCommit, err := l.channel.SignNextCommitment(ctx)
1,280✔
1978
        if err == lnwallet.ErrNoWindow {
1,368✔
1979
                l.cfg.PendingCommitTicker.Resume()
88✔
1980
                l.log.Trace("PendingCommitTicker resumed")
88✔
1981

88✔
1982
                n := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)
88✔
1983
                l.log.Tracef("revocation window exhausted, unable to send: "+
88✔
1984
                        "%v, pend_updates=%v, dangling_closes%v", n,
88✔
1985
                        lnutils.SpewLogClosure(l.openedCircuits),
88✔
1986
                        lnutils.SpewLogClosure(l.closedCircuits))
88✔
1987

88✔
1988
                return nil
88✔
1989
        } else if err != nil {
1,283✔
1990
                return err
×
1991
        }
×
1992

1993
        if err := l.ackDownStreamPackets(); err != nil {
1,195✔
1994
                return err
×
1995
        }
×
1996

1997
        l.cfg.PendingCommitTicker.Pause()
1,195✔
1998
        l.log.Trace("PendingCommitTicker paused after ackDownStreamPackets")
1,195✔
1999

1,195✔
2000
        // The remote party now has a new pending commitment, so we'll update
1,195✔
2001
        // the contract court to be aware of this new set (the prior old remote
1,195✔
2002
        // pending).
1,195✔
2003
        newUpdate := &contractcourt.ContractUpdate{
1,195✔
2004
                HtlcKey: contractcourt.RemotePendingHtlcSet,
1,195✔
2005
                Htlcs:   newCommit.PendingHTLCs,
1,195✔
2006
        }
1,195✔
2007
        err = l.cfg.NotifyContractUpdate(newUpdate)
1,195✔
2008
        if err != nil {
1,195✔
2009
                l.log.Errorf("unable to notify contract update: %v", err)
×
2010
                return err
×
2011
        }
×
2012

2013
        select {
1,195✔
2014
        case <-l.cg.Done():
9✔
2015
                return ErrLinkShuttingDown
9✔
2016
        default:
1,186✔
2017
        }
2018

2019
        auxBlobRecords, err := lnwire.ParseCustomRecords(newCommit.AuxSigBlob)
1,186✔
2020
        if err != nil {
1,186✔
2021
                return fmt.Errorf("error parsing aux sigs: %w", err)
×
2022
        }
×
2023

2024
        commitSig := &lnwire.CommitSig{
1,186✔
2025
                ChanID:        l.ChanID(),
1,186✔
2026
                CommitSig:     newCommit.CommitSig,
1,186✔
2027
                HtlcSigs:      newCommit.HtlcSigs,
1,186✔
2028
                PartialSig:    newCommit.PartialSig,
1,186✔
2029
                CustomRecords: auxBlobRecords,
1,186✔
2030
        }
1,186✔
2031
        err = l.cfg.Peer.SendMessage(false, commitSig)
1,186✔
2032
        if err != nil {
1,187✔
2033
                l.log.Errorf("failed to send CommitSig: %v", err)
1✔
2034
        }
1✔
2035

2036
        // Now that we have sent out a new CommitSig, we invoke the outgoing set
2037
        // of commit hooks.
2038
        l.RWMutex.Lock()
1,186✔
2039
        l.outgoingCommitHooks.invoke()
1,186✔
2040
        l.RWMutex.Unlock()
1,186✔
2041

1,186✔
2042
        return nil
1,186✔
2043
}
2044

2045
// Peer returns the representation of remote peer with which we have the
2046
// channel link opened.
2047
//
2048
// NOTE: Part of the ChannelLink interface.
2049
func (l *channelLink) PeerPubKey() [33]byte {
444✔
2050
        return l.cfg.Peer.PubKey()
444✔
2051
}
444✔
2052

2053
// ChannelPoint returns the channel outpoint for the channel link.
2054
// NOTE: Part of the ChannelLink interface.
2055
func (l *channelLink) ChannelPoint() wire.OutPoint {
853✔
2056
        return l.channel.ChannelPoint()
853✔
2057
}
853✔
2058

2059
// ShortChanID returns the short channel ID for the channel link. The short
2060
// channel ID encodes the exact location in the main chain that the original
2061
// funding output can be found.
2062
//
2063
// NOTE: Part of the ChannelLink interface.
2064
func (l *channelLink) ShortChanID() lnwire.ShortChannelID {
4,250✔
2065
        l.RLock()
4,250✔
2066
        defer l.RUnlock()
4,250✔
2067

4,250✔
2068
        return l.channel.ShortChanID()
4,250✔
2069
}
4,250✔
2070

2071
// UpdateShortChanID updates the short channel ID for a link. This may be
2072
// required in the event that a link is created before the short chan ID for it
2073
// is known, or a re-org occurs, and the funding transaction changes location
2074
// within the chain.
2075
//
2076
// NOTE: Part of the ChannelLink interface.
2077
func (l *channelLink) UpdateShortChanID() (lnwire.ShortChannelID, error) {
3✔
2078
        chanID := l.ChanID()
3✔
2079

3✔
2080
        // Refresh the channel state's short channel ID by loading it from disk.
3✔
2081
        // This ensures that the channel state accurately reflects the updated
3✔
2082
        // short channel ID.
3✔
2083
        err := l.channel.State().Refresh()
3✔
2084
        if err != nil {
3✔
2085
                l.log.Errorf("unable to refresh short_chan_id for chan_id=%v: "+
×
2086
                        "%v", chanID, err)
×
2087
                return hop.Source, err
×
2088
        }
×
2089

2090
        return hop.Source, nil
3✔
2091
}
2092

2093
// ChanID returns the channel ID for the channel link. The channel ID is a more
2094
// compact representation of a channel's full outpoint.
2095
//
2096
// NOTE: Part of the ChannelLink interface.
2097
func (l *channelLink) ChanID() lnwire.ChannelID {
3,924✔
2098
        return lnwire.NewChanIDFromOutPoint(l.channel.ChannelPoint())
3,924✔
2099
}
3,924✔
2100

2101
// Bandwidth returns the total amount that can flow through the channel link at
2102
// this given instance. The value returned is expressed in millisatoshi and can
2103
// be used by callers when making forwarding decisions to determine if a link
2104
// can accept an HTLC.
2105
//
2106
// NOTE: Part of the ChannelLink interface.
2107
func (l *channelLink) Bandwidth() lnwire.MilliSatoshi {
814✔
2108
        // Get the balance available on the channel for new HTLCs. This takes
814✔
2109
        // the channel reserve into account so HTLCs up to this value won't
814✔
2110
        // violate it.
814✔
2111
        return l.channel.AvailableBalance()
814✔
2112
}
814✔
2113

2114
// MayAddOutgoingHtlc indicates whether we can add an outgoing htlc with the
2115
// amount provided to the link. This check does not reserve a space, since
2116
// forwards or other payments may use the available slot, so it should be
2117
// considered best-effort.
2118
func (l *channelLink) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
3✔
2119
        return l.channel.MayAddOutgoingHtlc(amt)
3✔
2120
}
3✔
2121

2122
// getDustSum is a wrapper method that calls the underlying channel's dust sum
2123
// method.
2124
//
2125
// NOTE: Part of the dustHandler interface.
2126
func (l *channelLink) getDustSum(whoseCommit lntypes.ChannelParty,
2127
        dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {
2,526✔
2128

2,526✔
2129
        return l.channel.GetDustSum(whoseCommit, dryRunFee)
2,526✔
2130
}
2,526✔
2131

2132
// getFeeRate is a wrapper method that retrieves the underlying channel's
2133
// feerate.
2134
//
2135
// NOTE: Part of the dustHandler interface.
2136
func (l *channelLink) getFeeRate() chainfee.SatPerKWeight {
672✔
2137
        return l.channel.CommitFeeRate()
672✔
2138
}
672✔
2139

2140
// getDustClosure returns a closure that can be used by the switch or mailbox
2141
// to evaluate whether a given HTLC is dust.
2142
//
2143
// NOTE: Part of the dustHandler interface.
2144
func (l *channelLink) getDustClosure() dustClosure {
1,602✔
2145
        localDustLimit := l.channel.State().LocalChanCfg.DustLimit
1,602✔
2146
        remoteDustLimit := l.channel.State().RemoteChanCfg.DustLimit
1,602✔
2147
        chanType := l.channel.State().ChanType
1,602✔
2148

1,602✔
2149
        return dustHelper(chanType, localDustLimit, remoteDustLimit)
1,602✔
2150
}
1,602✔
2151

2152
// getCommitFee returns either the local or remote CommitFee in satoshis. This
2153
// is used so that the Switch can have access to the commitment fee without
2154
// needing to have a *LightningChannel. This doesn't include dust.
2155
//
2156
// NOTE: Part of the dustHandler interface.
2157
func (l *channelLink) getCommitFee(remote bool) btcutil.Amount {
1,895✔
2158
        if remote {
2,860✔
2159
                return l.channel.State().RemoteCommitment.CommitFee
965✔
2160
        }
965✔
2161

2162
        return l.channel.State().LocalCommitment.CommitFee
933✔
2163
}
2164

2165
// exceedsFeeExposureLimit returns whether or not the new proposed fee-rate
2166
// increases the total dust and fees within the channel past the configured
2167
// fee threshold. It first calculates the dust sum over every update in the
2168
// update log with the proposed fee-rate and taking into account both the local
2169
// and remote dust limits. It uses every update in the update log instead of
2170
// what is actually on the local and remote commitments because it is assumed
2171
// that in a worst-case scenario, every update in the update log could
2172
// theoretically be on either commitment transaction and this needs to be
2173
// accounted for with this fee-rate. It then calculates the local and remote
2174
// commitment fees given the proposed fee-rate. Finally, it tallies the results
2175
// and determines if the fee threshold has been exceeded.
2176
func (l *channelLink) exceedsFeeExposureLimit(
2177
        feePerKw chainfee.SatPerKWeight) (bool, error) {
6✔
2178

6✔
2179
        dryRunFee := fn.Some[chainfee.SatPerKWeight](feePerKw)
6✔
2180

6✔
2181
        // Get the sum of dust for both the local and remote commitments using
6✔
2182
        // this "dry-run" fee.
6✔
2183
        localDustSum := l.getDustSum(lntypes.Local, dryRunFee)
6✔
2184
        remoteDustSum := l.getDustSum(lntypes.Remote, dryRunFee)
6✔
2185

6✔
2186
        // Calculate the local and remote commitment fees using this dry-run
6✔
2187
        // fee.
6✔
2188
        localFee, remoteFee, err := l.channel.CommitFeeTotalAt(feePerKw)
6✔
2189
        if err != nil {
6✔
2190
                return false, err
×
2191
        }
×
2192

2193
        // Finally, check whether the max fee exposure was exceeded on either
2194
        // future commitment transaction with the fee-rate.
2195
        totalLocalDust := localDustSum + lnwire.NewMSatFromSatoshis(localFee)
6✔
2196
        if totalLocalDust > l.cfg.MaxFeeExposure {
6✔
2197
                l.log.Debugf("ChannelLink(%v): exceeds fee exposure limit: "+
×
2198
                        "local dust: %v, local fee: %v", l.ShortChanID(),
×
2199
                        totalLocalDust, localFee)
×
2200

×
2201
                return true, nil
×
2202
        }
×
2203

2204
        totalRemoteDust := remoteDustSum + lnwire.NewMSatFromSatoshis(
6✔
2205
                remoteFee,
6✔
2206
        )
6✔
2207

6✔
2208
        if totalRemoteDust > l.cfg.MaxFeeExposure {
6✔
2209
                l.log.Debugf("ChannelLink(%v): exceeds fee exposure limit: "+
×
2210
                        "remote dust: %v, remote fee: %v", l.ShortChanID(),
×
2211
                        totalRemoteDust, remoteFee)
×
2212

×
2213
                return true, nil
×
2214
        }
×
2215

2216
        return false, nil
6✔
2217
}
2218

2219
// isOverexposedWithHtlc calculates whether the proposed HTLC will make the
2220
// channel exceed the fee threshold. It first fetches the largest fee-rate that
2221
// may be on any unrevoked commitment transaction. Then, using this fee-rate,
2222
// determines if the to-be-added HTLC is dust. If the HTLC is dust, it adds to
2223
// the overall dust sum. If it is not dust, it contributes to weight, which
2224
// also adds to the overall dust sum by an increase in fees. If the dust sum on
2225
// either commitment exceeds the configured fee threshold, this function
2226
// returns true.
2227
func (l *channelLink) isOverexposedWithHtlc(htlc *lnwire.UpdateAddHTLC,
2228
        incoming bool) bool {
933✔
2229

933✔
2230
        dustClosure := l.getDustClosure()
933✔
2231

933✔
2232
        feeRate := l.channel.WorstCaseFeeRate()
933✔
2233

933✔
2234
        amount := htlc.Amount.ToSatoshis()
933✔
2235

933✔
2236
        // See if this HTLC is dust on both the local and remote commitments.
933✔
2237
        isLocalDust := dustClosure(feeRate, incoming, lntypes.Local, amount)
933✔
2238
        isRemoteDust := dustClosure(feeRate, incoming, lntypes.Remote, amount)
933✔
2239

933✔
2240
        // Calculate the dust sum for the local and remote commitments.
933✔
2241
        localDustSum := l.getDustSum(
933✔
2242
                lntypes.Local, fn.None[chainfee.SatPerKWeight](),
933✔
2243
        )
933✔
2244
        remoteDustSum := l.getDustSum(
933✔
2245
                lntypes.Remote, fn.None[chainfee.SatPerKWeight](),
933✔
2246
        )
933✔
2247

933✔
2248
        // Grab the larger of the local and remote commitment fees w/o dust.
933✔
2249
        commitFee := l.getCommitFee(false)
933✔
2250

933✔
2251
        if l.getCommitFee(true) > commitFee {
965✔
2252
                commitFee = l.getCommitFee(true)
32✔
2253
        }
32✔
2254

2255
        commitFeeMSat := lnwire.NewMSatFromSatoshis(commitFee)
933✔
2256

933✔
2257
        localDustSum += commitFeeMSat
933✔
2258
        remoteDustSum += commitFeeMSat
933✔
2259

933✔
2260
        // Calculate the additional fee increase if this is a non-dust HTLC.
933✔
2261
        weight := lntypes.WeightUnit(input.HTLCWeight)
933✔
2262
        additional := lnwire.NewMSatFromSatoshis(
933✔
2263
                feeRate.FeeForWeight(weight),
933✔
2264
        )
933✔
2265

933✔
2266
        if isLocalDust {
1,569✔
2267
                // If this is dust, it doesn't contribute to weight but does
636✔
2268
                // contribute to the overall dust sum.
636✔
2269
                localDustSum += lnwire.NewMSatFromSatoshis(amount)
636✔
2270
        } else {
936✔
2271
                // Account for the fee increase that comes with an increase in
300✔
2272
                // weight.
300✔
2273
                localDustSum += additional
300✔
2274
        }
300✔
2275

2276
        if localDustSum > l.cfg.MaxFeeExposure {
937✔
2277
                // The max fee exposure was exceeded.
4✔
2278
                l.log.Debugf("ChannelLink(%v): HTLC %v makes the channel "+
4✔
2279
                        "overexposed, total local dust: %v (current commit "+
4✔
2280
                        "fee: %v)", l.ShortChanID(), htlc, localDustSum)
4✔
2281

4✔
2282
                return true
4✔
2283
        }
4✔
2284

2285
        if isRemoteDust {
1,562✔
2286
                // If this is dust, it doesn't contribute to weight but does
633✔
2287
                // contribute to the overall dust sum.
633✔
2288
                remoteDustSum += lnwire.NewMSatFromSatoshis(amount)
633✔
2289
        } else {
932✔
2290
                // Account for the fee increase that comes with an increase in
299✔
2291
                // weight.
299✔
2292
                remoteDustSum += additional
299✔
2293
        }
299✔
2294

2295
        if remoteDustSum > l.cfg.MaxFeeExposure {
929✔
2296
                // The max fee exposure was exceeded.
×
2297
                l.log.Debugf("ChannelLink(%v): HTLC %v makes the channel "+
×
2298
                        "overexposed, total remote dust: %v (current commit "+
×
2299
                        "fee: %v)", l.ShortChanID(), htlc, remoteDustSum)
×
2300

×
2301
                return true
×
2302
        }
×
2303

2304
        return false
929✔
2305
}
2306

2307
// dustClosure is a function that evaluates whether an HTLC is dust. It returns
2308
// true if the HTLC is dust. It takes in a feerate, a boolean denoting whether
2309
// the HTLC is incoming (i.e. one that the remote sent), a boolean denoting
2310
// whether to evaluate on the local or remote commit, and finally an HTLC
2311
// amount to test.
2312
type dustClosure func(feerate chainfee.SatPerKWeight, incoming bool,
2313
        whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool
2314

2315
// dustHelper is used to construct the dustClosure.
2316
func dustHelper(chantype channeldb.ChannelType, localDustLimit,
2317
        remoteDustLimit btcutil.Amount) dustClosure {
1,802✔
2318

1,802✔
2319
        isDust := func(feerate chainfee.SatPerKWeight, incoming bool,
1,802✔
2320
                whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool {
12,289✔
2321

10,487✔
2322
                var dustLimit btcutil.Amount
10,487✔
2323
                if whoseCommit.IsLocal() {
15,732✔
2324
                        dustLimit = localDustLimit
5,245✔
2325
                } else {
10,490✔
2326
                        dustLimit = remoteDustLimit
5,245✔
2327
                }
5,245✔
2328

2329
                return lnwallet.HtlcIsDust(
10,487✔
2330
                        chantype, incoming, whoseCommit, feerate, amt,
10,487✔
2331
                        dustLimit,
10,487✔
2332
                )
10,487✔
2333
        }
2334

2335
        return isDust
1,802✔
2336
}
2337

2338
// zeroConfConfirmed returns whether or not the zero-conf channel has
2339
// confirmed on-chain.
2340
//
2341
// Part of the scidAliasHandler interface.
2342
func (l *channelLink) zeroConfConfirmed() bool {
6✔
2343
        return l.channel.State().ZeroConfConfirmed()
6✔
2344
}
6✔
2345

2346
// confirmedScid returns the confirmed SCID for a zero-conf channel. This
2347
// should not be called for non-zero-conf channels.
2348
//
2349
// Part of the scidAliasHandler interface.
2350
func (l *channelLink) confirmedScid() lnwire.ShortChannelID {
6✔
2351
        return l.channel.State().ZeroConfRealScid()
6✔
2352
}
6✔
2353

2354
// isZeroConf returns whether or not the underlying channel is a zero-conf
2355
// channel.
2356
//
2357
// Part of the scidAliasHandler interface.
2358
func (l *channelLink) isZeroConf() bool {
216✔
2359
        return l.channel.State().IsZeroConf()
216✔
2360
}
216✔
2361

2362
// negotiatedAliasFeature returns whether or not the underlying channel has
2363
// negotiated the option-scid-alias feature bit. This will be true for both
2364
// option-scid-alias and zero-conf channel-types. It will also be true for
2365
// channels with the feature bit but without the above channel-types.
2366
//
2367
// Part of the scidAliasFeature interface.
2368
func (l *channelLink) negotiatedAliasFeature() bool {
377✔
2369
        return l.channel.State().NegotiatedAliasFeature()
377✔
2370
}
377✔
2371

2372
// getAliases returns the set of aliases for the underlying channel.
2373
//
2374
// Part of the scidAliasHandler interface.
2375
func (l *channelLink) getAliases() []lnwire.ShortChannelID {
222✔
2376
        return l.cfg.GetAliases(l.ShortChanID())
222✔
2377
}
222✔
2378

2379
// attachFailAliasUpdate sets the link's FailAliasUpdate function.
2380
//
2381
// Part of the scidAliasHandler interface.
2382
func (l *channelLink) attachFailAliasUpdate(closure func(
2383
        sid lnwire.ShortChannelID, incoming bool) *lnwire.ChannelUpdate1) {
217✔
2384

217✔
2385
        l.Lock()
217✔
2386
        l.cfg.FailAliasUpdate = closure
217✔
2387
        l.Unlock()
217✔
2388
}
217✔
2389

2390
// AttachMailBox updates the current mailbox used by this link, and hooks up
2391
// the mailbox's message and packet outboxes to the link's upstream and
2392
// downstream chans, respectively.
2393
func (l *channelLink) AttachMailBox(mailbox MailBox) {
216✔
2394
        l.Lock()
216✔
2395
        l.mailBox = mailbox
216✔
2396
        l.upstream = mailbox.MessageOutBox()
216✔
2397
        l.downstream = mailbox.PacketOutBox()
216✔
2398
        l.Unlock()
216✔
2399

216✔
2400
        // Set the mailbox's fee rate. This may be refreshing a feerate that was
216✔
2401
        // never committed.
216✔
2402
        l.mailBox.SetFeeRate(l.getFeeRate())
216✔
2403

216✔
2404
        // Also set the mailbox's dust closure so that it can query whether HTLC's
216✔
2405
        // are dust given the current feerate.
216✔
2406
        l.mailBox.SetDustClosure(l.getDustClosure())
216✔
2407
}
216✔
2408

2409
// UpdateForwardingPolicy updates the forwarding policy for the target
2410
// ChannelLink. Once updated, the link will use the new forwarding policy to
2411
// govern if it an incoming HTLC should be forwarded or not. We assume that
2412
// fields that are zero are intentionally set to zero, so we'll use newPolicy to
2413
// update all of the link's FwrdingPolicy's values.
2414
//
2415
// NOTE: Part of the ChannelLink interface.
2416
func (l *channelLink) UpdateForwardingPolicy(
2417
        newPolicy models.ForwardingPolicy) {
15✔
2418

15✔
2419
        l.Lock()
15✔
2420
        defer l.Unlock()
15✔
2421

15✔
2422
        l.cfg.FwrdingPolicy = newPolicy
15✔
2423
}
15✔
2424

2425
// CheckHtlcForward should return a nil error if the passed HTLC details
2426
// satisfy the current forwarding policy fo the target link. Otherwise,
2427
// a LinkError with a valid protocol failure message should be returned
2428
// in order to signal to the source of the HTLC, the policy consistency
2429
// issue.
2430
//
2431
// NOTE: Part of the ChannelLink interface.
2432
func (l *channelLink) CheckHtlcForward(payHash [32]byte, incomingHtlcAmt,
2433
        amtToForward lnwire.MilliSatoshi, incomingTimeout,
2434
        outgoingTimeout uint32, inboundFee models.InboundFee,
2435
        heightNow uint32, originalScid lnwire.ShortChannelID,
2436
        customRecords lnwire.CustomRecords) *LinkError {
52✔
2437

52✔
2438
        l.RLock()
52✔
2439
        policy := l.cfg.FwrdingPolicy
52✔
2440
        l.RUnlock()
52✔
2441

52✔
2442
        // Using the outgoing HTLC amount, we'll calculate the outgoing
52✔
2443
        // fee this incoming HTLC must carry in order to satisfy the constraints
52✔
2444
        // of the outgoing link.
52✔
2445
        outFee := ExpectedFee(policy, amtToForward)
52✔
2446

52✔
2447
        // Then calculate the inbound fee that we charge based on the sum of
52✔
2448
        // outgoing HTLC amount and outgoing fee.
52✔
2449
        inFee := inboundFee.CalcFee(amtToForward + outFee)
52✔
2450

52✔
2451
        // Add up both fee components. It is important to calculate both fees
52✔
2452
        // separately. An alternative way of calculating is to first determine
52✔
2453
        // an aggregate fee and apply that to the outgoing HTLC amount. However,
52✔
2454
        // rounding may cause the result to be slightly higher than in the case
52✔
2455
        // of separately rounded fee components. This potentially causes failed
52✔
2456
        // forwards for senders and is something to be avoided.
52✔
2457
        expectedFee := inFee + int64(outFee)
52✔
2458

52✔
2459
        // If the actual fee is less than our expected fee, then we'll reject
52✔
2460
        // this HTLC as it didn't provide a sufficient amount of fees, or the
52✔
2461
        // values have been tampered with, or the send used incorrect/dated
52✔
2462
        // information to construct the forwarding information for this hop. In
52✔
2463
        // any case, we'll cancel this HTLC.
52✔
2464
        actualFee := int64(incomingHtlcAmt) - int64(amtToForward)
52✔
2465
        if incomingHtlcAmt < amtToForward || actualFee < expectedFee {
61✔
2466
                l.log.Warnf("outgoing htlc(%x) has insufficient fee: "+
9✔
2467
                        "expected %v, got %v: incoming=%v, outgoing=%v, "+
9✔
2468
                        "inboundFee=%v",
9✔
2469
                        payHash[:], expectedFee, actualFee,
9✔
2470
                        incomingHtlcAmt, amtToForward, inboundFee,
9✔
2471
                )
9✔
2472

9✔
2473
                // As part of the returned error, we'll send our latest routing
9✔
2474
                // policy so the sending node obtains the most up to date data.
9✔
2475
                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage {
18✔
2476
                        return lnwire.NewFeeInsufficient(amtToForward, *upd)
9✔
2477
                }
9✔
2478
                failure := l.createFailureWithUpdate(false, originalScid, cb)
9✔
2479
                return NewLinkError(failure)
9✔
2480
        }
2481

2482
        // Check whether the outgoing htlc satisfies the channel policy.
2483
        err := l.canSendHtlc(
46✔
2484
                policy, payHash, amtToForward, outgoingTimeout, heightNow,
46✔
2485
                originalScid, customRecords,
46✔
2486
        )
46✔
2487
        if err != nil {
62✔
2488
                return err
16✔
2489
        }
16✔
2490

2491
        // Finally, we'll ensure that the time-lock on the outgoing HTLC meets
2492
        // the following constraint: the incoming time-lock minus our time-lock
2493
        // delta should equal the outgoing time lock. Otherwise, whether the
2494
        // sender messed up, or an intermediate node tampered with the HTLC.
2495
        timeDelta := policy.TimeLockDelta
33✔
2496
        if incomingTimeout < outgoingTimeout+timeDelta {
35✔
2497
                l.log.Warnf("incoming htlc(%x) has incorrect time-lock value: "+
2✔
2498
                        "expected at least %v block delta, got %v block delta",
2✔
2499
                        payHash[:], timeDelta, incomingTimeout-outgoingTimeout)
2✔
2500

2✔
2501
                // Grab the latest routing policy so the sending node is up to
2✔
2502
                // date with our current policy.
2✔
2503
                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage {
4✔
2504
                        return lnwire.NewIncorrectCltvExpiry(
2✔
2505
                                incomingTimeout, *upd,
2✔
2506
                        )
2✔
2507
                }
2✔
2508
                failure := l.createFailureWithUpdate(false, originalScid, cb)
2✔
2509
                return NewLinkError(failure)
2✔
2510
        }
2511

2512
        return nil
31✔
2513
}
2514

2515
// CheckHtlcTransit should return a nil error if the passed HTLC details
2516
// satisfy the current channel policy.  Otherwise, a LinkError with a
2517
// valid protocol failure message should be returned in order to signal
2518
// the violation. This call is intended to be used for locally initiated
2519
// payments for which there is no corresponding incoming htlc.
2520
func (l *channelLink) CheckHtlcTransit(payHash [32]byte,
2521
        amt lnwire.MilliSatoshi, timeout uint32, heightNow uint32,
2522
        customRecords lnwire.CustomRecords) *LinkError {
409✔
2523

409✔
2524
        l.RLock()
409✔
2525
        policy := l.cfg.FwrdingPolicy
409✔
2526
        l.RUnlock()
409✔
2527

409✔
2528
        // We pass in hop.Source here as this is only used in the Switch when
409✔
2529
        // trying to send over a local link. This causes the fallback mechanism
409✔
2530
        // to occur.
409✔
2531
        return l.canSendHtlc(
409✔
2532
                policy, payHash, amt, timeout, heightNow, hop.Source,
409✔
2533
                customRecords,
409✔
2534
        )
409✔
2535
}
409✔
2536

2537
// canSendHtlc checks whether the given htlc parameters satisfy
2538
// the channel's amount and time lock constraints.
2539
func (l *channelLink) canSendHtlc(policy models.ForwardingPolicy,
2540
        payHash [32]byte, amt lnwire.MilliSatoshi, timeout uint32,
2541
        heightNow uint32, originalScid lnwire.ShortChannelID,
2542
        customRecords lnwire.CustomRecords) *LinkError {
452✔
2543

452✔
2544
        // As our first sanity check, we'll ensure that the passed HTLC isn't
452✔
2545
        // too small for the next hop. If so, then we'll cancel the HTLC
452✔
2546
        // directly.
452✔
2547
        if amt < policy.MinHTLCOut {
463✔
2548
                l.log.Warnf("outgoing htlc(%x) is too small: min_htlc=%v, "+
11✔
2549
                        "htlc_value=%v", payHash[:], policy.MinHTLCOut,
11✔
2550
                        amt)
11✔
2551

11✔
2552
                // As part of the returned error, we'll send our latest routing
11✔
2553
                // policy so the sending node obtains the most up to date data.
11✔
2554
                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage {
22✔
2555
                        return lnwire.NewAmountBelowMinimum(amt, *upd)
11✔
2556
                }
11✔
2557
                failure := l.createFailureWithUpdate(false, originalScid, cb)
11✔
2558
                return NewLinkError(failure)
11✔
2559
        }
2560

2561
        // Next, ensure that the passed HTLC isn't too large. If so, we'll
2562
        // cancel the HTLC directly.
2563
        if policy.MaxHTLC != 0 && amt > policy.MaxHTLC {
450✔
2564
                l.log.Warnf("outgoing htlc(%x) is too large: max_htlc=%v, "+
6✔
2565
                        "htlc_value=%v", payHash[:], policy.MaxHTLC, amt)
6✔
2566

6✔
2567
                // As part of the returned error, we'll send our latest routing
6✔
2568
                // policy so the sending node obtains the most up-to-date data.
6✔
2569
                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage {
12✔
2570
                        return lnwire.NewTemporaryChannelFailure(upd)
6✔
2571
                }
6✔
2572
                failure := l.createFailureWithUpdate(false, originalScid, cb)
6✔
2573
                return NewDetailedLinkError(failure, OutgoingFailureHTLCExceedsMax)
6✔
2574
        }
2575

2576
        // We want to avoid offering an HTLC which will expire in the near
2577
        // future, so we'll reject an HTLC if the outgoing expiration time is
2578
        // too close to the current height.
2579
        if timeout <= heightNow+l.cfg.OutgoingCltvRejectDelta {
443✔
2580
                l.log.Warnf("htlc(%x) has an expiry that's too soon: "+
2✔
2581
                        "outgoing_expiry=%v, best_height=%v", payHash[:],
2✔
2582
                        timeout, heightNow)
2✔
2583

2✔
2584
                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage {
4✔
2585
                        return lnwire.NewExpiryTooSoon(*upd)
2✔
2586
                }
2✔
2587
                failure := l.createFailureWithUpdate(false, originalScid, cb)
2✔
2588
                return NewLinkError(failure)
2✔
2589
        }
2590

2591
        // Check absolute max delta.
2592
        if timeout > l.cfg.MaxOutgoingCltvExpiry+heightNow {
440✔
2593
                l.log.Warnf("outgoing htlc(%x) has a time lock too far in "+
1✔
2594
                        "the future: got %v, but maximum is %v", payHash[:],
1✔
2595
                        timeout-heightNow, l.cfg.MaxOutgoingCltvExpiry)
1✔
2596

1✔
2597
                return NewLinkError(&lnwire.FailExpiryTooFar{})
1✔
2598
        }
1✔
2599

2600
        // We now check the available bandwidth to see if this HTLC can be
2601
        // forwarded.
2602
        availableBandwidth := l.Bandwidth()
438✔
2603
        auxBandwidth, err := fn.MapOptionZ(
438✔
2604
                l.cfg.AuxTrafficShaper,
438✔
2605
                func(ts AuxTrafficShaper) fn.Result[OptionalBandwidth] {
438✔
2606
                        var htlcBlob fn.Option[tlv.Blob]
×
2607
                        blob, err := customRecords.Serialize()
×
2608
                        if err != nil {
×
2609
                                return fn.Err[OptionalBandwidth](
×
2610
                                        fmt.Errorf("unable to serialize "+
×
2611
                                                "custom records: %w", err))
×
2612
                        }
×
2613

2614
                        if len(blob) > 0 {
×
2615
                                htlcBlob = fn.Some(blob)
×
2616
                        }
×
2617

2618
                        return l.AuxBandwidth(amt, originalScid, htlcBlob, ts)
×
2619
                },
2620
        ).Unpack()
2621
        if err != nil {
438✔
2622
                l.log.Errorf("Unable to determine aux bandwidth: %v", err)
×
2623
                return NewLinkError(&lnwire.FailTemporaryNodeFailure{})
×
2624
        }
×
2625

2626
        if auxBandwidth.IsHandled && auxBandwidth.Bandwidth.IsSome() {
438✔
2627
                auxBandwidth.Bandwidth.WhenSome(
×
2628
                        func(bandwidth lnwire.MilliSatoshi) {
×
2629
                                availableBandwidth = bandwidth
×
2630
                        },
×
2631
                )
2632
        }
2633

2634
        // Check to see if there is enough balance in this channel.
2635
        if amt > availableBandwidth {
442✔
2636
                l.log.Warnf("insufficient bandwidth to route htlc: %v is "+
4✔
2637
                        "larger than %v", amt, availableBandwidth)
4✔
2638
                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage {
8✔
2639
                        return lnwire.NewTemporaryChannelFailure(upd)
4✔
2640
                }
4✔
2641
                failure := l.createFailureWithUpdate(false, originalScid, cb)
4✔
2642
                return NewDetailedLinkError(
4✔
2643
                        failure, OutgoingFailureInsufficientBalance,
4✔
2644
                )
4✔
2645
        }
2646

2647
        return nil
437✔
2648
}
2649

2650
// AuxBandwidth returns the bandwidth that can be used for a channel, expressed
2651
// in milli-satoshi. This might be different from the regular BTC bandwidth for
2652
// custom channels. This will always return fn.None() for a regular (non-custom)
2653
// channel.
2654
func (l *channelLink) AuxBandwidth(amount lnwire.MilliSatoshi,
2655
        cid lnwire.ShortChannelID, htlcBlob fn.Option[tlv.Blob],
2656
        ts AuxTrafficShaper) fn.Result[OptionalBandwidth] {
×
2657

×
2658
        fundingBlob := l.FundingCustomBlob()
×
2659
        shouldHandle, err := ts.ShouldHandleTraffic(cid, fundingBlob, htlcBlob)
×
2660
        if err != nil {
×
2661
                return fn.Err[OptionalBandwidth](fmt.Errorf("traffic shaper "+
×
2662
                        "failed to decide whether to handle traffic: %w", err))
×
2663
        }
×
2664

2665
        log.Debugf("ShortChannelID=%v: aux traffic shaper is handling "+
×
2666
                "traffic: %v", cid, shouldHandle)
×
2667

×
2668
        // If this channel isn't handled by the aux traffic shaper, we'll return
×
2669
        // early.
×
2670
        if !shouldHandle {
×
2671
                return fn.Ok(OptionalBandwidth{
×
2672
                        IsHandled: false,
×
2673
                })
×
2674
        }
×
2675

2676
        peerBytes := l.cfg.Peer.PubKey()
×
2677

×
2678
        peer, err := route.NewVertexFromBytes(peerBytes[:])
×
2679
        if err != nil {
×
2680
                return fn.Err[OptionalBandwidth](fmt.Errorf("failed to decode "+
×
2681
                        "peer pub key: %v", err))
×
2682
        }
×
2683

2684
        // Ask for a specific bandwidth to be used for the channel.
2685
        commitmentBlob := l.CommitmentCustomBlob()
×
2686
        auxBandwidth, err := ts.PaymentBandwidth(
×
2687
                fundingBlob, htlcBlob, commitmentBlob, l.Bandwidth(), amount,
×
2688
                l.channel.FetchLatestAuxHTLCView(), peer,
×
2689
        )
×
2690
        if err != nil {
×
2691
                return fn.Err[OptionalBandwidth](fmt.Errorf("failed to get "+
×
2692
                        "bandwidth from external traffic shaper: %w", err))
×
2693
        }
×
2694

2695
        log.Debugf("ShortChannelID=%v: aux traffic shaper reported available "+
×
2696
                "bandwidth: %v", cid, auxBandwidth)
×
2697

×
2698
        return fn.Ok(OptionalBandwidth{
×
2699
                IsHandled: true,
×
2700
                Bandwidth: fn.Some(auxBandwidth),
×
2701
        })
×
2702
}
2703

2704
// Stats returns the statistics of channel link.
2705
//
2706
// NOTE: Part of the ChannelLink interface.
2707
func (l *channelLink) Stats() (uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi) {
7✔
2708
        snapshot := l.channel.StateSnapshot()
7✔
2709

7✔
2710
        return snapshot.ChannelCommitment.CommitHeight,
7✔
2711
                snapshot.TotalMSatSent,
7✔
2712
                snapshot.TotalMSatReceived
7✔
2713
}
7✔
2714

2715
// String returns the string representation of channel link.
2716
//
2717
// NOTE: Part of the ChannelLink interface.
2718
func (l *channelLink) String() string {
×
2719
        return l.channel.ChannelPoint().String()
×
2720
}
×
2721

2722
// handleSwitchPacket handles the switch packets. This packets which might be
2723
// forwarded to us from another channel link in case the htlc update came from
2724
// another peer or if the update was created by user
2725
//
2726
// NOTE: Part of the packetHandler interface.
2727
func (l *channelLink) handleSwitchPacket(pkt *htlcPacket) error {
482✔
2728
        l.log.Tracef("received switch packet inkey=%v, outkey=%v",
482✔
2729
                pkt.inKey(), pkt.outKey())
482✔
2730

482✔
2731
        return l.mailBox.AddPacket(pkt)
482✔
2732
}
482✔
2733

2734
// HandleChannelUpdate handles the htlc requests as settle/add/fail which sent
2735
// to us from remote peer we have a channel with.
2736
//
2737
// NOTE: Part of the ChannelLink interface.
2738
func (l *channelLink) HandleChannelUpdate(message lnwire.Message) {
3,335✔
2739
        select {
3,335✔
2740
        case <-l.cg.Done():
×
2741
                // Return early if the link is already in the process of
×
2742
                // quitting. It doesn't make sense to hand the message to the
×
2743
                // mailbox here.
×
2744
                return
×
2745
        default:
3,335✔
2746
        }
2747

2748
        err := l.mailBox.AddMessage(message)
3,335✔
2749
        if err != nil {
3,335✔
2750
                l.log.Errorf("failed to add Message to mailbox: %v", err)
×
2751
        }
×
2752
}
2753

2754
// updateChannelFee updates the commitment fee-per-kw on this channel by
2755
// committing to an update_fee message.
2756
func (l *channelLink) updateChannelFee(ctx context.Context,
2757
        feePerKw chainfee.SatPerKWeight) error {
3✔
2758

3✔
2759
        l.log.Infof("updating commit fee to %v", feePerKw)
3✔
2760

3✔
2761
        // We skip sending the UpdateFee message if the channel is not
3✔
2762
        // currently eligible to forward messages.
3✔
2763
        if !l.eligibleToUpdate() {
3✔
2764
                l.log.Debugf("skipping fee update for inactive channel")
×
2765
                return nil
×
2766
        }
×
2767

2768
        // Check and see if our proposed fee-rate would make us exceed the fee
2769
        // threshold.
2770
        thresholdExceeded, err := l.exceedsFeeExposureLimit(feePerKw)
3✔
2771
        if err != nil {
3✔
2772
                // This shouldn't typically happen. If it does, it indicates
×
2773
                // something is wrong with our channel state.
×
2774
                return err
×
2775
        }
×
2776

2777
        if thresholdExceeded {
3✔
2778
                return fmt.Errorf("link fee threshold exceeded")
×
2779
        }
×
2780

2781
        // First, we'll update the local fee on our commitment.
2782
        if err := l.channel.UpdateFee(feePerKw); err != nil {
3✔
2783
                return err
×
2784
        }
×
2785

2786
        // The fee passed the channel's validation checks, so we update the
2787
        // mailbox feerate.
2788
        l.mailBox.SetFeeRate(feePerKw)
3✔
2789

3✔
2790
        // We'll then attempt to send a new UpdateFee message, and also lock it
3✔
2791
        // in immediately by triggering a commitment update.
3✔
2792
        msg := lnwire.NewUpdateFee(l.ChanID(), uint32(feePerKw))
3✔
2793
        if err := l.cfg.Peer.SendMessage(false, msg); err != nil {
3✔
2794
                return err
×
2795
        }
×
2796

2797
        return l.updateCommitTx(ctx)
3✔
2798
}
2799

2800
// processRemoteSettleFails accepts a batch of settle/fail payment descriptors
2801
// after receiving a revocation from the remote party, and reprocesses them in
2802
// the context of the provided forwarding package. Any settles or fails that
2803
// have already been acknowledged in the forwarding package will not be sent to
2804
// the switch.
2805
func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg) {
1,174✔
2806
        if len(fwdPkg.SettleFails) == 0 {
2,033✔
2807
                l.log.Trace("fwd package has no settle/fails to process " +
859✔
2808
                        "exiting early")
859✔
2809

859✔
2810
                return
859✔
2811
        }
859✔
2812

2813
        // Exit early if the fwdPkg is already processed.
2814
        if fwdPkg.State == channeldb.FwdStateCompleted {
318✔
2815
                l.log.Debugf("skipped processing completed fwdPkg %v", fwdPkg)
×
2816

×
2817
                return
×
2818
        }
×
2819

2820
        l.log.Debugf("settle-fail-filter: %v", fwdPkg.SettleFailFilter)
318✔
2821

318✔
2822
        var switchPackets []*htlcPacket
318✔
2823
        for i, update := range fwdPkg.SettleFails {
636✔
2824
                destRef := fwdPkg.DestRef(uint16(i))
318✔
2825

318✔
2826
                // Skip any settles or fails that have already been
318✔
2827
                // acknowledged by the incoming link that originated the
318✔
2828
                // forwarded Add.
318✔
2829
                if fwdPkg.SettleFailFilter.Contains(uint16(i)) {
318✔
2830
                        continue
×
2831
                }
2832

2833
                // TODO(roasbeef): rework log entries to a shared
2834
                // interface.
2835

2836
                switch msg := update.UpdateMsg.(type) {
318✔
2837
                // A settle for an HTLC we previously forwarded HTLC has been
2838
                // received. So we'll forward the HTLC to the switch which will
2839
                // handle propagating the settle to the prior hop.
2840
                case *lnwire.UpdateFulfillHTLC:
195✔
2841
                        // If hodl.SettleIncoming is requested, we will not
195✔
2842
                        // forward the SETTLE to the switch and will not signal
195✔
2843
                        // a free slot on the commitment transaction.
195✔
2844
                        if l.cfg.HodlMask.Active(hodl.SettleIncoming) {
195✔
2845
                                l.log.Warnf(hodl.SettleIncoming.Warning())
×
2846
                                continue
×
2847
                        }
2848

2849
                        settlePacket := &htlcPacket{
195✔
2850
                                outgoingChanID: l.ShortChanID(),
195✔
2851
                                outgoingHTLCID: msg.ID,
195✔
2852
                                destRef:        &destRef,
195✔
2853
                                htlc:           msg,
195✔
2854
                        }
195✔
2855

195✔
2856
                        // Add the packet to the batch to be forwarded, and
195✔
2857
                        // notify the overflow queue that a spare spot has been
195✔
2858
                        // freed up within the commitment state.
195✔
2859
                        switchPackets = append(switchPackets, settlePacket)
195✔
2860

2861
                // A failureCode message for a previously forwarded HTLC has
2862
                // been received. As a result a new slot will be freed up in
2863
                // our commitment state, so we'll forward this to the switch so
2864
                // the backwards undo can continue.
2865
                case *lnwire.UpdateFailHTLC:
126✔
2866
                        // If hodl.SettleIncoming is requested, we will not
126✔
2867
                        // forward the FAIL to the switch and will not signal a
126✔
2868
                        // free slot on the commitment transaction.
126✔
2869
                        if l.cfg.HodlMask.Active(hodl.FailIncoming) {
126✔
2870
                                l.log.Warnf(hodl.FailIncoming.Warning())
×
2871
                                continue
×
2872
                        }
2873

2874
                        // Fetch the reason the HTLC was canceled so we can
2875
                        // continue to propagate it. This failure originated
2876
                        // from another node, so the linkFailure field is not
2877
                        // set on the packet.
2878
                        failPacket := &htlcPacket{
126✔
2879
                                outgoingChanID: l.ShortChanID(),
126✔
2880
                                outgoingHTLCID: msg.ID,
126✔
2881
                                destRef:        &destRef,
126✔
2882
                                htlc:           msg,
126✔
2883
                        }
126✔
2884

126✔
2885
                        l.log.Debugf("Failed to send HTLC with ID=%d", msg.ID)
126✔
2886

126✔
2887
                        // If the failure message lacks an HMAC (but includes
126✔
2888
                        // the 4 bytes for encoding the message and padding
126✔
2889
                        // lengths, then this means that we received it as an
126✔
2890
                        // UpdateFailMalformedHTLC. As a result, we'll signal
126✔
2891
                        // that we need to convert this error within the switch
126✔
2892
                        // to an actual error, by encrypting it as if we were
126✔
2893
                        // the originating hop.
126✔
2894
                        convertedErrorSize := lnwire.FailureMessageLength + 4
126✔
2895
                        if len(msg.Reason) == convertedErrorSize {
132✔
2896
                                failPacket.convertedError = true
6✔
2897
                        }
6✔
2898

2899
                        // Add the packet to the batch to be forwarded, and
2900
                        // notify the overflow queue that a spare spot has been
2901
                        // freed up within the commitment state.
2902
                        switchPackets = append(switchPackets, failPacket)
126✔
2903
                }
2904
        }
2905

2906
        // Only spawn the task forward packets we have a non-zero number.
2907
        if len(switchPackets) > 0 {
636✔
2908
                go l.forwardBatch(false, switchPackets...)
318✔
2909
        }
318✔
2910
}
2911

2912
// processRemoteAdds serially processes each of the Add payment descriptors
2913
// which have been "locked-in" by receiving a revocation from the remote party.
2914
// The forwarding package provided instructs how to process this batch,
2915
// indicating whether this is the first time these Adds are being processed, or
2916
// whether we are reprocessing as a result of a failure or restart. Adds that
2917
// have already been acknowledged in the forwarding package will be ignored.
2918
//
2919
//nolint:funlen
2920
func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg) {
1,176✔
2921
        // Exit early if there are no adds to process.
1,176✔
2922
        if len(fwdPkg.Adds) == 0 {
2,047✔
2923
                l.log.Trace("fwd package has no adds to process exiting early")
871✔
2924

871✔
2925
                return
871✔
2926
        }
871✔
2927

2928
        // Exit early if the fwdPkg is already processed.
2929
        if fwdPkg.State == channeldb.FwdStateCompleted {
308✔
2930
                l.log.Debugf("skipped processing completed fwdPkg %v", fwdPkg)
×
2931

×
2932
                return
×
2933
        }
×
2934

2935
        l.log.Tracef("processing %d remote adds for height %d",
308✔
2936
                len(fwdPkg.Adds), fwdPkg.Height)
308✔
2937

308✔
2938
        // decodeReqs is a list of requests sent to the onion decoder. We expect
308✔
2939
        // the same length of responses to be returned.
308✔
2940
        decodeReqs := make([]hop.DecodeHopIteratorRequest, 0, len(fwdPkg.Adds))
308✔
2941

308✔
2942
        // unackedAdds is a list of ADDs that's waiting for the remote's
308✔
2943
        // settle/fail update.
308✔
2944
        unackedAdds := make([]*lnwire.UpdateAddHTLC, 0, len(fwdPkg.Adds))
308✔
2945

308✔
2946
        for i, update := range fwdPkg.Adds {
760✔
2947
                // If this index is already found in the ack filter, the
452✔
2948
                // response to this forwarding decision has already been
452✔
2949
                // committed by one of our commitment txns. ADDs in this state
452✔
2950
                // are waiting for the rest of the fwding package to get acked
452✔
2951
                // before being garbage collected.
452✔
2952
                if fwdPkg.State == channeldb.FwdStateProcessed &&
452✔
2953
                        fwdPkg.AckFilter.Contains(uint16(i)) {
452✔
2954

×
2955
                        continue
×
2956
                }
2957

2958
                if msg, ok := update.UpdateMsg.(*lnwire.UpdateAddHTLC); ok {
904✔
2959
                        // Before adding the new htlc to the state machine,
452✔
2960
                        // parse the onion object in order to obtain the
452✔
2961
                        // routing information with DecodeHopIterator function
452✔
2962
                        // which process the Sphinx packet.
452✔
2963
                        onionReader := bytes.NewReader(msg.OnionBlob[:])
452✔
2964

452✔
2965
                        req := hop.DecodeHopIteratorRequest{
452✔
2966
                                OnionReader:    onionReader,
452✔
2967
                                RHash:          msg.PaymentHash[:],
452✔
2968
                                IncomingCltv:   msg.Expiry,
452✔
2969
                                IncomingAmount: msg.Amount,
452✔
2970
                                BlindingPoint:  msg.BlindingPoint,
452✔
2971
                        }
452✔
2972

452✔
2973
                        decodeReqs = append(decodeReqs, req)
452✔
2974
                        unackedAdds = append(unackedAdds, msg)
452✔
2975
                }
452✔
2976
        }
2977

2978
        // If the fwdPkg has already been processed, it means we are
2979
        // reforwarding the packets again, which happens only on a restart.
2980
        reforward := fwdPkg.State == channeldb.FwdStateProcessed
308✔
2981

308✔
2982
        // Atomically decode the incoming htlcs, simultaneously checking for
308✔
2983
        // replay attempts. A particular index in the returned, spare list of
308✔
2984
        // channel iterators should only be used if the failure code at the
308✔
2985
        // same index is lnwire.FailCodeNone.
308✔
2986
        decodeResps, sphinxErr := l.cfg.DecodeHopIterators(
308✔
2987
                fwdPkg.ID(), decodeReqs, reforward,
308✔
2988
        )
308✔
2989
        if sphinxErr != nil {
308✔
2990
                l.failf(LinkFailureError{code: ErrInternalError},
×
2991
                        "unable to decode hop iterators: %v", sphinxErr)
×
2992
                return
×
2993
        }
×
2994

2995
        var switchPackets []*htlcPacket
308✔
2996

308✔
2997
        for i, update := range unackedAdds {
760✔
2998
                idx := uint16(i)
452✔
2999
                sourceRef := fwdPkg.SourceRef(idx)
452✔
3000
                add := *update
452✔
3001

452✔
3002
                // An incoming HTLC add has been full-locked in. As a result we
452✔
3003
                // can now examine the forwarding details of the HTLC, and the
452✔
3004
                // HTLC itself to decide if: we should forward it, cancel it,
452✔
3005
                // or are able to settle it (and it adheres to our fee related
452✔
3006
                // constraints).
452✔
3007

452✔
3008
                // Before adding the new htlc to the state machine, parse the
452✔
3009
                // onion object in order to obtain the routing information with
452✔
3010
                // DecodeHopIterator function which process the Sphinx packet.
452✔
3011
                chanIterator, failureCode := decodeResps[i].Result()
452✔
3012
                if failureCode != lnwire.CodeNone {
457✔
3013
                        // If we're unable to process the onion blob then we
5✔
3014
                        // should send the malformed htlc error to payment
5✔
3015
                        // sender.
5✔
3016
                        l.sendMalformedHTLCError(
5✔
3017
                                add.ID, failureCode, add.OnionBlob, &sourceRef,
5✔
3018
                        )
5✔
3019

5✔
3020
                        l.log.Errorf("unable to decode onion hop iterator "+
5✔
3021
                                "for htlc(id=%v, hash=%x): %v", add.ID,
5✔
3022
                                add.PaymentHash, failureCode)
5✔
3023

5✔
3024
                        continue
5✔
3025
                }
3026

3027
                heightNow := l.cfg.BestHeight()
450✔
3028

450✔
3029
                pld, routeRole, pldErr := chanIterator.HopPayload()
450✔
3030
                if pldErr != nil {
453✔
3031
                        // If we're unable to process the onion payload, or we
3✔
3032
                        // received invalid onion payload failure, then we
3✔
3033
                        // should send an error back to the caller so the HTLC
3✔
3034
                        // can be canceled.
3✔
3035
                        var failedType uint64
3✔
3036

3✔
3037
                        // We need to get the underlying error value, so we
3✔
3038
                        // can't use errors.As as suggested by the linter.
3✔
3039
                        //nolint:errorlint
3✔
3040
                        if e, ok := pldErr.(hop.ErrInvalidPayload); ok {
3✔
3041
                                failedType = uint64(e.Type)
×
3042
                        }
×
3043

3044
                        // If we couldn't parse the payload, make our best
3045
                        // effort at creating an error encrypter that knows
3046
                        // what blinding type we were, but if we couldn't
3047
                        // parse the payload we have no way of knowing whether
3048
                        // we were the introduction node or not.
3049
                        //
3050
                        //nolint:ll
3051
                        obfuscator, failCode := chanIterator.ExtractErrorEncrypter(
3✔
3052
                                l.cfg.ExtractErrorEncrypter,
3✔
3053
                                // We need our route role here because we
3✔
3054
                                // couldn't parse or validate the payload.
3✔
3055
                                routeRole == hop.RouteRoleIntroduction,
3✔
3056
                        )
3✔
3057
                        if failCode != lnwire.CodeNone {
3✔
3058
                                l.log.Errorf("could not extract error "+
×
3059
                                        "encrypter: %v", pldErr)
×
3060

×
3061
                                // We can't process this htlc, send back
×
3062
                                // malformed.
×
3063
                                l.sendMalformedHTLCError(
×
3064
                                        add.ID, failureCode, add.OnionBlob,
×
3065
                                        &sourceRef,
×
3066
                                )
×
3067

×
3068
                                continue
×
3069
                        }
3070

3071
                        // TODO: currently none of the test unit infrastructure
3072
                        // is setup to handle TLV payloads, so testing this
3073
                        // would require implementing a separate mock iterator
3074
                        // for TLV payloads that also supports injecting invalid
3075
                        // payloads. Deferring this non-trival effort till a
3076
                        // later date
3077
                        failure := lnwire.NewInvalidOnionPayload(failedType, 0)
3✔
3078

3✔
3079
                        l.sendHTLCError(
3✔
3080
                                add, sourceRef, NewLinkError(failure),
3✔
3081
                                obfuscator, false,
3✔
3082
                        )
3✔
3083

3✔
3084
                        l.log.Errorf("unable to decode forwarding "+
3✔
3085
                                "instructions: %v", pldErr)
3✔
3086

3✔
3087
                        continue
3✔
3088
                }
3089

3090
                // Retrieve onion obfuscator from onion blob in order to
3091
                // produce initial obfuscation of the onion failureCode.
3092
                obfuscator, failureCode := chanIterator.ExtractErrorEncrypter(
450✔
3093
                        l.cfg.ExtractErrorEncrypter,
450✔
3094
                        routeRole == hop.RouteRoleIntroduction,
450✔
3095
                )
450✔
3096
                if failureCode != lnwire.CodeNone {
451✔
3097
                        // If we're unable to process the onion blob than we
1✔
3098
                        // should send the malformed htlc error to payment
1✔
3099
                        // sender.
1✔
3100
                        l.sendMalformedHTLCError(
1✔
3101
                                add.ID, failureCode, add.OnionBlob,
1✔
3102
                                &sourceRef,
1✔
3103
                        )
1✔
3104

1✔
3105
                        l.log.Errorf("unable to decode onion "+
1✔
3106
                                "obfuscator: %v", failureCode)
1✔
3107

1✔
3108
                        continue
1✔
3109
                }
3110

3111
                fwdInfo := pld.ForwardingInfo()
449✔
3112

449✔
3113
                // Check whether the payload we've just processed uses our
449✔
3114
                // node as the introduction point (gave us a blinding key in
449✔
3115
                // the payload itself) and fail it back if we don't support
449✔
3116
                // route blinding.
449✔
3117
                if fwdInfo.NextBlinding.IsSome() &&
449✔
3118
                        l.cfg.DisallowRouteBlinding {
452✔
3119

3✔
3120
                        failure := lnwire.NewInvalidBlinding(
3✔
3121
                                fn.Some(add.OnionBlob),
3✔
3122
                        )
3✔
3123

3✔
3124
                        l.sendHTLCError(
3✔
3125
                                add, sourceRef, NewLinkError(failure),
3✔
3126
                                obfuscator, false,
3✔
3127
                        )
3✔
3128

3✔
3129
                        l.log.Error("rejected htlc that uses use as an " +
3✔
3130
                                "introduction point when we do not support " +
3✔
3131
                                "route blinding")
3✔
3132

3✔
3133
                        continue
3✔
3134
                }
3135

3136
                switch fwdInfo.NextHop {
449✔
3137
                case hop.Exit:
413✔
3138
                        err := l.processExitHop(
413✔
3139
                                add, sourceRef, obfuscator, fwdInfo,
413✔
3140
                                heightNow, pld,
413✔
3141
                        )
413✔
3142
                        if err != nil {
413✔
3143
                                l.failf(LinkFailureError{
×
3144
                                        code: ErrInternalError,
×
3145
                                }, err.Error()) //nolint
×
3146

×
3147
                                return
×
3148
                        }
×
3149

3150
                // There are additional channels left within this route. So
3151
                // we'll simply do some forwarding package book-keeping.
3152
                default:
39✔
3153
                        // If hodl.AddIncoming is requested, we will not
39✔
3154
                        // validate the forwarded ADD, nor will we send the
39✔
3155
                        // packet to the htlc switch.
39✔
3156
                        if l.cfg.HodlMask.Active(hodl.AddIncoming) {
39✔
3157
                                l.log.Warnf(hodl.AddIncoming.Warning())
×
3158
                                continue
×
3159
                        }
3160

3161
                        endorseValue := l.experimentalEndorsement(
39✔
3162
                                record.CustomSet(add.CustomRecords),
39✔
3163
                        )
39✔
3164
                        endorseType := uint64(
39✔
3165
                                lnwire.ExperimentalEndorsementType,
39✔
3166
                        )
39✔
3167

39✔
3168
                        switch fwdPkg.State {
39✔
3169
                        case channeldb.FwdStateProcessed:
3✔
3170
                                // This add was not forwarded on the previous
3✔
3171
                                // processing phase, run it through our
3✔
3172
                                // validation pipeline to reproduce an error.
3✔
3173
                                // This may trigger a different error due to
3✔
3174
                                // expiring timelocks, but we expect that an
3✔
3175
                                // error will be reproduced.
3✔
3176
                                if !fwdPkg.FwdFilter.Contains(idx) {
3✔
3177
                                        break
×
3178
                                }
3179

3180
                                // Otherwise, it was already processed, we can
3181
                                // can collect it and continue.
3182
                                outgoingAdd := &lnwire.UpdateAddHTLC{
3✔
3183
                                        Expiry:        fwdInfo.OutgoingCTLV,
3✔
3184
                                        Amount:        fwdInfo.AmountToForward,
3✔
3185
                                        PaymentHash:   add.PaymentHash,
3✔
3186
                                        BlindingPoint: fwdInfo.NextBlinding,
3✔
3187
                                }
3✔
3188

3✔
3189
                                endorseValue.WhenSome(func(e byte) {
6✔
3190
                                        custRecords := map[uint64][]byte{
3✔
3191
                                                endorseType: {e},
3✔
3192
                                        }
3✔
3193

3✔
3194
                                        outgoingAdd.CustomRecords = custRecords
3✔
3195
                                })
3✔
3196

3197
                                // Finally, we'll encode the onion packet for
3198
                                // the _next_ hop using the hop iterator
3199
                                // decoded for the current hop.
3200
                                buf := bytes.NewBuffer(
3✔
3201
                                        outgoingAdd.OnionBlob[0:0],
3✔
3202
                                )
3✔
3203

3✔
3204
                                // We know this cannot fail, as this ADD
3✔
3205
                                // was marked forwarded in a previous
3✔
3206
                                // round of processing.
3✔
3207
                                chanIterator.EncodeNextHop(buf)
3✔
3208

3✔
3209
                                inboundFee := l.cfg.FwrdingPolicy.InboundFee
3✔
3210

3✔
3211
                                //nolint:ll
3✔
3212
                                updatePacket := &htlcPacket{
3✔
3213
                                        incomingChanID:       l.ShortChanID(),
3✔
3214
                                        incomingHTLCID:       add.ID,
3✔
3215
                                        outgoingChanID:       fwdInfo.NextHop,
3✔
3216
                                        sourceRef:            &sourceRef,
3✔
3217
                                        incomingAmount:       add.Amount,
3✔
3218
                                        amount:               outgoingAdd.Amount,
3✔
3219
                                        htlc:                 outgoingAdd,
3✔
3220
                                        obfuscator:           obfuscator,
3✔
3221
                                        incomingTimeout:      add.Expiry,
3✔
3222
                                        outgoingTimeout:      fwdInfo.OutgoingCTLV,
3✔
3223
                                        inOnionCustomRecords: pld.CustomRecords(),
3✔
3224
                                        inboundFee:           inboundFee,
3✔
3225
                                        inWireCustomRecords:  add.CustomRecords.Copy(),
3✔
3226
                                }
3✔
3227
                                switchPackets = append(
3✔
3228
                                        switchPackets, updatePacket,
3✔
3229
                                )
3✔
3230

3✔
3231
                                continue
3✔
3232
                        }
3233

3234
                        // TODO(roasbeef): ensure don't accept outrageous
3235
                        // timeout for htlc
3236

3237
                        // With all our forwarding constraints met, we'll
3238
                        // create the outgoing HTLC using the parameters as
3239
                        // specified in the forwarding info.
3240
                        addMsg := &lnwire.UpdateAddHTLC{
39✔
3241
                                Expiry:        fwdInfo.OutgoingCTLV,
39✔
3242
                                Amount:        fwdInfo.AmountToForward,
39✔
3243
                                PaymentHash:   add.PaymentHash,
39✔
3244
                                BlindingPoint: fwdInfo.NextBlinding,
39✔
3245
                        }
39✔
3246

39✔
3247
                        endorseValue.WhenSome(func(e byte) {
78✔
3248
                                addMsg.CustomRecords = map[uint64][]byte{
39✔
3249
                                        endorseType: {e},
39✔
3250
                                }
39✔
3251
                        })
39✔
3252

3253
                        // Finally, we'll encode the onion packet for the
3254
                        // _next_ hop using the hop iterator decoded for the
3255
                        // current hop.
3256
                        buf := bytes.NewBuffer(addMsg.OnionBlob[0:0])
39✔
3257
                        err := chanIterator.EncodeNextHop(buf)
39✔
3258
                        if err != nil {
39✔
3259
                                l.log.Errorf("unable to encode the "+
×
3260
                                        "remaining route %v", err)
×
3261

×
3262
                                cb := func(upd *lnwire.ChannelUpdate1) lnwire.FailureMessage { //nolint:ll
×
3263
                                        return lnwire.NewTemporaryChannelFailure(upd)
×
3264
                                }
×
3265

3266
                                failure := l.createFailureWithUpdate(
×
3267
                                        true, hop.Source, cb,
×
3268
                                )
×
3269

×
3270
                                l.sendHTLCError(
×
3271
                                        add, sourceRef, NewLinkError(failure),
×
3272
                                        obfuscator, false,
×
3273
                                )
×
3274
                                continue
×
3275
                        }
3276

3277
                        // Now that this add has been reprocessed, only append
3278
                        // it to our list of packets to forward to the switch
3279
                        // this is the first time processing the add. If the
3280
                        // fwd pkg has already been processed, then we entered
3281
                        // the above section to recreate a previous error.  If
3282
                        // the packet had previously been forwarded, it would
3283
                        // have been added to switchPackets at the top of this
3284
                        // section.
3285
                        if fwdPkg.State == channeldb.FwdStateLockedIn {
78✔
3286
                                inboundFee := l.cfg.FwrdingPolicy.InboundFee
39✔
3287

39✔
3288
                                //nolint:ll
39✔
3289
                                updatePacket := &htlcPacket{
39✔
3290
                                        incomingChanID:       l.ShortChanID(),
39✔
3291
                                        incomingHTLCID:       add.ID,
39✔
3292
                                        outgoingChanID:       fwdInfo.NextHop,
39✔
3293
                                        sourceRef:            &sourceRef,
39✔
3294
                                        incomingAmount:       add.Amount,
39✔
3295
                                        amount:               addMsg.Amount,
39✔
3296
                                        htlc:                 addMsg,
39✔
3297
                                        obfuscator:           obfuscator,
39✔
3298
                                        incomingTimeout:      add.Expiry,
39✔
3299
                                        outgoingTimeout:      fwdInfo.OutgoingCTLV,
39✔
3300
                                        inOnionCustomRecords: pld.CustomRecords(),
39✔
3301
                                        inboundFee:           inboundFee,
39✔
3302
                                        inWireCustomRecords:  add.CustomRecords.Copy(),
39✔
3303
                                }
39✔
3304

39✔
3305
                                fwdPkg.FwdFilter.Set(idx)
39✔
3306
                                switchPackets = append(switchPackets,
39✔
3307
                                        updatePacket)
39✔
3308
                        }
39✔
3309
                }
3310
        }
3311

3312
        // Commit the htlcs we are intending to forward if this package has not
3313
        // been fully processed.
3314
        if fwdPkg.State == channeldb.FwdStateLockedIn {
613✔
3315
                err := l.channel.SetFwdFilter(fwdPkg.Height, fwdPkg.FwdFilter)
305✔
3316
                if err != nil {
305✔
3317
                        l.failf(LinkFailureError{code: ErrInternalError},
×
3318
                                "unable to set fwd filter: %v", err)
×
3319
                        return
×
3320
                }
×
3321
        }
3322

3323
        if len(switchPackets) == 0 {
580✔
3324
                return
272✔
3325
        }
272✔
3326

3327
        l.log.Debugf("forwarding %d packets to switch: reforward=%v",
39✔
3328
                len(switchPackets), reforward)
39✔
3329

39✔
3330
        // NOTE: This call is made synchronous so that we ensure all circuits
39✔
3331
        // are committed in the exact order that they are processed in the link.
39✔
3332
        // Failing to do this could cause reorderings/gaps in the range of
39✔
3333
        // opened circuits, which violates assumptions made by the circuit
39✔
3334
        // trimming.
39✔
3335
        l.forwardBatch(reforward, switchPackets...)
39✔
3336
}
3337

3338
// experimentalEndorsement returns the value to set for our outgoing
3339
// experimental endorsement field, and a boolean indicating whether it should
3340
// be populated on the outgoing htlc.
3341
func (l *channelLink) experimentalEndorsement(
3342
        customUpdateAdd record.CustomSet) fn.Option[byte] {
39✔
3343

39✔
3344
        // Only relay experimental signal if we are within the experiment
39✔
3345
        // period.
39✔
3346
        if !l.cfg.ShouldFwdExpEndorsement() {
42✔
3347
                return fn.None[byte]()
3✔
3348
        }
3✔
3349

3350
        // If we don't have any custom records or the experimental field is
3351
        // not set, just forward a zero value.
3352
        if len(customUpdateAdd) == 0 {
78✔
3353
                return fn.Some[byte](lnwire.ExperimentalUnendorsed)
39✔
3354
        }
39✔
3355

3356
        t := uint64(lnwire.ExperimentalEndorsementType)
3✔
3357
        value, set := customUpdateAdd[t]
3✔
3358
        if !set {
3✔
3359
                return fn.Some[byte](lnwire.ExperimentalUnendorsed)
×
3360
        }
×
3361

3362
        // We expect at least one byte for this field, consider it invalid if
3363
        // it has no data and just forward a zero value.
3364
        if len(value) == 0 {
3✔
3365
                return fn.Some[byte](lnwire.ExperimentalUnendorsed)
×
3366
        }
×
3367

3368
        // Only forward endorsed if the incoming link is endorsed.
3369
        if value[0] == lnwire.ExperimentalEndorsed {
6✔
3370
                return fn.Some[byte](lnwire.ExperimentalEndorsed)
3✔
3371
        }
3✔
3372

3373
        // Forward as unendorsed otherwise, including cases where we've
3374
        // received an invalid value that uses more than 3 bits of information.
3375
        return fn.Some[byte](lnwire.ExperimentalUnendorsed)
3✔
3376
}
3377

3378
// processExitHop handles an htlc for which this link is the exit hop. It
3379
// returns a boolean indicating whether the commitment tx needs an update.
3380
func (l *channelLink) processExitHop(add lnwire.UpdateAddHTLC,
3381
        sourceRef channeldb.AddRef, obfuscator hop.ErrorEncrypter,
3382
        fwdInfo hop.ForwardingInfo, heightNow uint32,
3383
        payload invoices.Payload) error {
413✔
3384

413✔
3385
        // If hodl.ExitSettle is requested, we will not validate the final hop's
413✔
3386
        // ADD, nor will we settle the corresponding invoice or respond with the
413✔
3387
        // preimage.
413✔
3388
        if l.cfg.HodlMask.Active(hodl.ExitSettle) {
523✔
3389
                l.log.Warnf("%s for htlc(rhash=%x,htlcIndex=%v)",
110✔
3390
                        hodl.ExitSettle.Warning(), add.PaymentHash, add.ID)
110✔
3391

110✔
3392
                return nil
110✔
3393
        }
110✔
3394

3395
        // In case the traffic shaper is active, we'll check if the HTLC has
3396
        // custom records and skip the amount check in the onion payload below.
3397
        isCustomHTLC := fn.MapOptionZ(
306✔
3398
                l.cfg.AuxTrafficShaper,
306✔
3399
                func(ts AuxTrafficShaper) bool {
306✔
3400
                        return ts.IsCustomHTLC(add.CustomRecords)
×
3401
                },
×
3402
        )
3403

3404
        // As we're the exit hop, we'll double check the hop-payload included in
3405
        // the HTLC to ensure that it was crafted correctly by the sender and
3406
        // is compatible with the HTLC we were extended. If an external
3407
        // validator is active we might bypass the amount check.
3408
        if !isCustomHTLC && add.Amount < fwdInfo.AmountToForward {
406✔
3409
                l.log.Errorf("onion payload of incoming htlc(%x) has "+
100✔
3410
                        "incompatible value: expected <=%v, got %v",
100✔
3411
                        add.PaymentHash, add.Amount, fwdInfo.AmountToForward)
100✔
3412

100✔
3413
                failure := NewLinkError(
100✔
3414
                        lnwire.NewFinalIncorrectHtlcAmount(add.Amount),
100✔
3415
                )
100✔
3416
                l.sendHTLCError(add, sourceRef, failure, obfuscator, true)
100✔
3417

100✔
3418
                return nil
100✔
3419
        }
100✔
3420

3421
        // We'll also ensure that our time-lock value has been computed
3422
        // correctly.
3423
        if add.Expiry < fwdInfo.OutgoingCTLV {
207✔
3424
                l.log.Errorf("onion payload of incoming htlc(%x) has "+
1✔
3425
                        "incompatible time-lock: expected <=%v, got %v",
1✔
3426
                        add.PaymentHash, add.Expiry, fwdInfo.OutgoingCTLV)
1✔
3427

1✔
3428
                failure := NewLinkError(
1✔
3429
                        lnwire.NewFinalIncorrectCltvExpiry(add.Expiry),
1✔
3430
                )
1✔
3431

1✔
3432
                l.sendHTLCError(add, sourceRef, failure, obfuscator, true)
1✔
3433

1✔
3434
                return nil
1✔
3435
        }
1✔
3436

3437
        // Notify the invoiceRegistry of the exit hop htlc. If we crash right
3438
        // after this, this code will be re-executed after restart. We will
3439
        // receive back a resolution event.
3440
        invoiceHash := lntypes.Hash(add.PaymentHash)
205✔
3441

205✔
3442
        circuitKey := models.CircuitKey{
205✔
3443
                ChanID: l.ShortChanID(),
205✔
3444
                HtlcID: add.ID,
205✔
3445
        }
205✔
3446

205✔
3447
        event, err := l.cfg.Registry.NotifyExitHopHtlc(
205✔
3448
                invoiceHash, add.Amount, add.Expiry, int32(heightNow),
205✔
3449
                circuitKey, l.hodlQueue.ChanIn(), add.CustomRecords, payload,
205✔
3450
        )
205✔
3451
        if err != nil {
205✔
3452
                return err
×
3453
        }
×
3454

3455
        // Create a hodlHtlc struct and decide either resolved now or later.
3456
        htlc := hodlHtlc{
205✔
3457
                add:        add,
205✔
3458
                sourceRef:  sourceRef,
205✔
3459
                obfuscator: obfuscator,
205✔
3460
        }
205✔
3461

205✔
3462
        // If the event is nil, the invoice is being held, so we save payment
205✔
3463
        // descriptor for future reference.
205✔
3464
        if event == nil {
264✔
3465
                l.hodlMap[circuitKey] = htlc
59✔
3466
                return nil
59✔
3467
        }
59✔
3468

3469
        // Process the received resolution.
3470
        return l.processHtlcResolution(event, htlc)
149✔
3471
}
3472

3473
// settleHTLC settles the HTLC on the channel.
3474
func (l *channelLink) settleHTLC(preimage lntypes.Preimage,
3475
        htlcIndex uint64, sourceRef channeldb.AddRef) error {
200✔
3476

200✔
3477
        hash := preimage.Hash()
200✔
3478

200✔
3479
        l.log.Infof("settling htlc %v as exit hop", hash)
200✔
3480

200✔
3481
        err := l.channel.SettleHTLC(
200✔
3482
                preimage, htlcIndex, &sourceRef, nil, nil,
200✔
3483
        )
200✔
3484
        if err != nil {
200✔
3485
                return fmt.Errorf("unable to settle htlc: %w", err)
×
3486
        }
×
3487

3488
        // If the link is in hodl.BogusSettle mode, replace the preimage with a
3489
        // fake one before sending it to the peer.
3490
        if l.cfg.HodlMask.Active(hodl.BogusSettle) {
203✔
3491
                l.log.Warnf(hodl.BogusSettle.Warning())
3✔
3492
                preimage = [32]byte{}
3✔
3493
                copy(preimage[:], bytes.Repeat([]byte{2}, 32))
3✔
3494
        }
3✔
3495

3496
        // HTLC was successfully settled locally send notification about it
3497
        // remote peer.
3498
        err = l.cfg.Peer.SendMessage(false, &lnwire.UpdateFulfillHTLC{
200✔
3499
                ChanID:          l.ChanID(),
200✔
3500
                ID:              htlcIndex,
200✔
3501
                PaymentPreimage: preimage,
200✔
3502
        })
200✔
3503
        if err != nil {
200✔
NEW
3504
                l.log.Errorf("failed to send UpdateFulfillHTLC: %v", err)
×
NEW
3505
        }
×
3506

3507
        // Once we have successfully settled the htlc, notify a settle event.
3508
        l.cfg.HtlcNotifier.NotifySettleEvent(
200✔
3509
                HtlcKey{
200✔
3510
                        IncomingCircuit: models.CircuitKey{
200✔
3511
                                ChanID: l.ShortChanID(),
200✔
3512
                                HtlcID: htlcIndex,
200✔
3513
                        },
200✔
3514
                },
200✔
3515
                preimage,
200✔
3516
                HtlcEventTypeReceive,
200✔
3517
        )
200✔
3518

200✔
3519
        return nil
200✔
3520
}
3521

3522
// forwardBatch forwards the given htlcPackets to the switch, and waits on the
3523
// err chan for the individual responses. This method is intended to be spawned
3524
// as a goroutine so the responses can be handled in the background.
3525
func (l *channelLink) forwardBatch(replay bool, packets ...*htlcPacket) {
579✔
3526
        // Don't forward packets for which we already have a response in our
579✔
3527
        // mailbox. This could happen if a packet fails and is buffered in the
579✔
3528
        // mailbox, and the incoming link flaps.
579✔
3529
        var filteredPkts = make([]*htlcPacket, 0, len(packets))
579✔
3530
        for _, pkt := range packets {
1,158✔
3531
                if l.mailBox.HasPacket(pkt.inKey()) {
582✔
3532
                        continue
3✔
3533
                }
3534

3535
                filteredPkts = append(filteredPkts, pkt)
579✔
3536
        }
3537

3538
        err := l.cfg.ForwardPackets(l.cg.Done(), replay, filteredPkts...)
579✔
3539
        if err != nil {
590✔
3540
                log.Errorf("Unhandled error while reforwarding htlc "+
11✔
3541
                        "settle/fail over htlcswitch: %v", err)
11✔
3542
        }
11✔
3543
}
3544

3545
// sendHTLCError functions cancels HTLC and send cancel message back to the
3546
// peer from which HTLC was received.
3547
func (l *channelLink) sendHTLCError(add lnwire.UpdateAddHTLC,
3548
        sourceRef channeldb.AddRef, failure *LinkError,
3549
        e hop.ErrorEncrypter, isReceive bool) {
108✔
3550

108✔
3551
        reason, err := e.EncryptFirstHop(failure.WireMessage())
108✔
3552
        if err != nil {
108✔
3553
                l.log.Errorf("unable to obfuscate error: %v", err)
×
3554
                return
×
3555
        }
×
3556

3557
        err = l.channel.FailHTLC(add.ID, reason, &sourceRef, nil, nil)
108✔
3558
        if err != nil {
108✔
3559
                l.log.Errorf("unable cancel htlc: %v", err)
×
3560
                return
×
3561
        }
×
3562

3563
        // Send the appropriate failure message depending on whether we're
3564
        // in a blinded route or not.
3565
        if err := l.sendIncomingHTLCFailureMsg(
108✔
3566
                add.ID, e, reason,
108✔
3567
        ); err != nil {
108✔
3568
                l.log.Errorf("unable to send HTLC failure: %v", err)
×
3569
                return
×
3570
        }
×
3571

3572
        // Notify a link failure on our incoming link. Outgoing htlc information
3573
        // is not available at this point, because we have not decrypted the
3574
        // onion, so it is excluded.
3575
        var eventType HtlcEventType
108✔
3576
        if isReceive {
216✔
3577
                eventType = HtlcEventTypeReceive
108✔
3578
        } else {
111✔
3579
                eventType = HtlcEventTypeForward
3✔
3580
        }
3✔
3581

3582
        l.cfg.HtlcNotifier.NotifyLinkFailEvent(
108✔
3583
                HtlcKey{
108✔
3584
                        IncomingCircuit: models.CircuitKey{
108✔
3585
                                ChanID: l.ShortChanID(),
108✔
3586
                                HtlcID: add.ID,
108✔
3587
                        },
108✔
3588
                },
108✔
3589
                HtlcInfo{
108✔
3590
                        IncomingTimeLock: add.Expiry,
108✔
3591
                        IncomingAmt:      add.Amount,
108✔
3592
                },
108✔
3593
                eventType,
108✔
3594
                failure,
108✔
3595
                true,
108✔
3596
        )
108✔
3597
}
3598

3599
// sendPeerHTLCFailure handles sending a HTLC failure message back to the
3600
// peer from which the HTLC was received. This function is primarily used to
3601
// handle the special requirements of route blinding, specifically:
3602
// - Forwarding nodes must switch out any errors with MalformedFailHTLC
3603
// - Introduction nodes should return regular HTLC failure messages.
3604
//
3605
// It accepts the original opaque failure, which will be used in the case
3606
// that we're not part of a blinded route and an error encrypter that'll be
3607
// used if we are the introduction node and need to present an error as if
3608
// we're the failing party.
3609
func (l *channelLink) sendIncomingHTLCFailureMsg(htlcIndex uint64,
3610
        e hop.ErrorEncrypter,
3611
        originalFailure lnwire.OpaqueReason) error {
124✔
3612

124✔
3613
        var msg lnwire.Message
124✔
3614
        switch {
124✔
3615
        // Our circuit's error encrypter will be nil if this was a locally
3616
        // initiated payment. We can only hit a blinded error for a locally
3617
        // initiated payment if we allow ourselves to be picked as the
3618
        // introduction node for our own payments and in that case we
3619
        // shouldn't reach this code. To prevent the HTLC getting stuck,
3620
        // we fail it back and log an error.
3621
        // code.
3622
        case e == nil:
×
3623
                msg = &lnwire.UpdateFailHTLC{
×
3624
                        ChanID: l.ChanID(),
×
3625
                        ID:     htlcIndex,
×
3626
                        Reason: originalFailure,
×
3627
                }
×
3628

×
3629
                l.log.Errorf("Unexpected blinded failure when "+
×
3630
                        "we are the sending node, incoming htlc: %v(%v)",
×
3631
                        l.ShortChanID(), htlcIndex)
×
3632

3633
        // For cleartext hops (ie, non-blinded/normal) we don't need any
3634
        // transformation on the error message and can just send the original.
3635
        case !e.Type().IsBlinded():
124✔
3636
                msg = &lnwire.UpdateFailHTLC{
124✔
3637
                        ChanID: l.ChanID(),
124✔
3638
                        ID:     htlcIndex,
124✔
3639
                        Reason: originalFailure,
124✔
3640
                }
124✔
3641

3642
        // When we're the introduction node, we need to convert the error to
3643
        // a UpdateFailHTLC.
3644
        case e.Type() == hop.EncrypterTypeIntroduction:
3✔
3645
                l.log.Debugf("Introduction blinded node switching out failure "+
3✔
3646
                        "error: %v", htlcIndex)
3✔
3647

3✔
3648
                // The specification does not require that we set the onion
3✔
3649
                // blob.
3✔
3650
                failureMsg := lnwire.NewInvalidBlinding(
3✔
3651
                        fn.None[[lnwire.OnionPacketSize]byte](),
3✔
3652
                )
3✔
3653
                reason, err := e.EncryptFirstHop(failureMsg)
3✔
3654
                if err != nil {
3✔
3655
                        return err
×
3656
                }
×
3657

3658
                msg = &lnwire.UpdateFailHTLC{
3✔
3659
                        ChanID: l.ChanID(),
3✔
3660
                        ID:     htlcIndex,
3✔
3661
                        Reason: reason,
3✔
3662
                }
3✔
3663

3664
        // If we are a relaying node, we need to switch out any error that
3665
        // we've received to a malformed HTLC error.
3666
        case e.Type() == hop.EncrypterTypeRelaying:
3✔
3667
                l.log.Debugf("Relaying blinded node switching out malformed "+
3✔
3668
                        "error: %v", htlcIndex)
3✔
3669

3✔
3670
                msg = &lnwire.UpdateFailMalformedHTLC{
3✔
3671
                        ChanID:      l.ChanID(),
3✔
3672
                        ID:          htlcIndex,
3✔
3673
                        FailureCode: lnwire.CodeInvalidBlinding,
3✔
3674
                }
3✔
3675

3676
        default:
×
3677
                return fmt.Errorf("unexpected encrypter: %d", e)
×
3678
        }
3679

3680
        if err := l.cfg.Peer.SendMessage(false, msg); err != nil {
124✔
3681
                l.log.Warnf("Send update fail failed: %v", err)
×
3682
        }
×
3683

3684
        return nil
124✔
3685
}
3686

3687
// sendMalformedHTLCError helper function which sends the malformed HTLC update
3688
// to the payment sender.
3689
func (l *channelLink) sendMalformedHTLCError(htlcIndex uint64,
3690
        code lnwire.FailCode, onionBlob [lnwire.OnionPacketSize]byte,
3691
        sourceRef *channeldb.AddRef) {
6✔
3692

6✔
3693
        shaOnionBlob := sha256.Sum256(onionBlob[:])
6✔
3694
        err := l.channel.MalformedFailHTLC(htlcIndex, code, shaOnionBlob, sourceRef)
6✔
3695
        if err != nil {
6✔
3696
                l.log.Errorf("unable cancel htlc: %v", err)
×
3697
                return
×
3698
        }
×
3699

3700
        err = l.cfg.Peer.SendMessage(false, &lnwire.UpdateFailMalformedHTLC{
6✔
3701
                ChanID:       l.ChanID(),
6✔
3702
                ID:           htlcIndex,
6✔
3703
                ShaOnionBlob: shaOnionBlob,
6✔
3704
                FailureCode:  code,
6✔
3705
        })
6✔
3706
        if err != nil {
6✔
NEW
3707
                l.log.Errorf("failed to send UpdateFailMalformedHTLC: %v", err)
×
NEW
3708
        }
×
3709
}
3710

3711
// failf is a function which is used to encapsulate the action necessary for
3712
// properly failing the link. It takes a LinkFailureError, which will be passed
3713
// to the OnChannelFailure closure, in order for it to determine if we should
3714
// force close the channel, and if we should send an error message to the
3715
// remote peer.
3716
func (l *channelLink) failf(linkErr LinkFailureError, format string,
3717
        a ...interface{}) {
16✔
3718

16✔
3719
        reason := fmt.Errorf(format, a...)
16✔
3720

16✔
3721
        // Return if we have already notified about a failure.
16✔
3722
        if l.failed {
19✔
3723
                l.log.Warnf("ignoring link failure (%v), as link already "+
3✔
3724
                        "failed", reason)
3✔
3725
                return
3✔
3726
        }
3✔
3727

3728
        l.log.Errorf("failing link: %s with error: %v", reason, linkErr)
16✔
3729

16✔
3730
        // Set failed, such that we won't process any more updates, and notify
16✔
3731
        // the peer about the failure.
16✔
3732
        l.failed = true
16✔
3733
        l.cfg.OnChannelFailure(l.ChanID(), l.ShortChanID(), linkErr)
16✔
3734
}
3735

3736
// FundingCustomBlob returns the custom funding blob of the channel that this
3737
// link is associated with. The funding blob represents static information about
3738
// the channel that was created at channel funding time.
3739
func (l *channelLink) FundingCustomBlob() fn.Option[tlv.Blob] {
×
3740
        if l.channel == nil {
×
3741
                return fn.None[tlv.Blob]()
×
3742
        }
×
3743

3744
        if l.channel.State() == nil {
×
3745
                return fn.None[tlv.Blob]()
×
3746
        }
×
3747

3748
        return l.channel.State().CustomBlob
×
3749
}
3750

3751
// CommitmentCustomBlob returns the custom blob of the current local commitment
3752
// of the channel that this link is associated with.
3753
func (l *channelLink) CommitmentCustomBlob() fn.Option[tlv.Blob] {
×
3754
        if l.channel == nil {
×
3755
                return fn.None[tlv.Blob]()
×
3756
        }
×
3757

3758
        return l.channel.LocalCommitmentBlob()
×
3759
}
3760

3761
// handleHtlcResolution takes an HTLC resolution and processes it by draining
3762
// the hodlQueue. Once processed, a commit_sig is sent to the remote to update
3763
// their commitment.
3764
func (l *channelLink) handleHtlcResolution(ctx context.Context,
3765
        hodlItem any) error {
58✔
3766

58✔
3767
        htlcResolution, ok := hodlItem.(invoices.HtlcResolution)
58✔
3768
        if !ok {
58✔
NEW
3769
                return fmt.Errorf("expect HtlcResolution, got %T", hodlItem)
×
NEW
3770
        }
×
3771

3772
        err := l.processHodlQueue(ctx, htlcResolution)
58✔
3773
        // No error, success.
58✔
3774
        if err == nil {
116✔
3775
                return nil
58✔
3776
        }
58✔
3777

NEW
3778
        switch {
×
3779
        // If the duplicate keystone error was encountered, fail back
3780
        // gracefully.
NEW
3781
        case errors.Is(err, ErrDuplicateKeystone):
×
NEW
3782
                l.failf(
×
NEW
3783
                        LinkFailureError{
×
NEW
3784
                                code: ErrCircuitError,
×
NEW
3785
                        },
×
NEW
3786
                        "process hodl queue: temporary circuit error: %v", err,
×
NEW
3787
                )
×
3788

3789
        // Send an Error message to the peer.
NEW
3790
        default:
×
NEW
3791
                l.failf(
×
NEW
3792
                        LinkFailureError{
×
NEW
3793
                                code: ErrInternalError,
×
NEW
3794
                        },
×
NEW
3795
                        "process hodl queue: unable to update commitment: %v",
×
NEW
3796
                        err,
×
NEW
3797
                )
×
3798
        }
3799

NEW
3800
        return err
×
3801
}
3802

3803
// handleQuiescenceReq takes a locally initialized (RPC) quiescence request and
3804
// forwards it to the quiescer for further processing.
3805
func (l *channelLink) handleQuiescenceReq(req StfuReq) error {
4✔
3806
        l.quiescer.InitStfu(req)
4✔
3807

4✔
3808
        if !l.noDanglingUpdates(lntypes.Local) {
4✔
NEW
3809
                return nil
×
NEW
3810
        }
×
3811

3812
        err := l.quiescer.SendOwedStfu()
4✔
3813
        if err != nil {
4✔
NEW
3814
                l.stfuFailf("SendOwedStfu: %s", err.Error())
×
NEW
3815
                res := fn.Err[lntypes.ChannelParty](err)
×
NEW
3816
                req.Resolve(res)
×
NEW
3817
        }
×
3818

3819
        return err
4✔
3820
}
3821

3822
// handleUpdateFee is called whenever the `updateFeeTimer` ticks. It is used to
3823
// decide whether we should send an `update_fee` msg to update the commitment's
3824
// feerate.
3825
func (l *channelLink) handleUpdateFee(ctx context.Context) error {
4✔
3826
        // If we're not the initiator of the channel, we don't control the fees,
4✔
3827
        // so we can ignore this.
4✔
3828
        if !l.channel.IsInitiator() {
4✔
NEW
3829
                return nil
×
NEW
3830
        }
×
3831

3832
        // If we are the initiator, then we'll sample the current fee rate to
3833
        // get into the chain within 3 blocks.
3834
        netFee, err := l.sampleNetworkFee()
4✔
3835
        if err != nil {
4✔
NEW
3836
                return fmt.Errorf("unable to sample network fee: %w", err)
×
NEW
3837
        }
×
3838

3839
        minRelayFee := l.cfg.FeeEstimator.RelayFeePerKW()
4✔
3840

4✔
3841
        newCommitFee := l.channel.IdealCommitFeeRate(
4✔
3842
                netFee, minRelayFee,
4✔
3843
                l.cfg.MaxAnchorsCommitFeeRate,
4✔
3844
                l.cfg.MaxFeeAllocation,
4✔
3845
        )
4✔
3846

4✔
3847
        // We determine if we should adjust the commitment fee based on the
4✔
3848
        // current commitment fee, the suggested new commitment fee and the
4✔
3849
        // current minimum relay fee rate.
4✔
3850
        commitFee := l.channel.CommitFeeRate()
4✔
3851
        if !shouldAdjustCommitFee(newCommitFee, commitFee, minRelayFee) {
5✔
3852
                return nil
1✔
3853
        }
1✔
3854

3855
        // If we do, then we'll send a new UpdateFee message to the remote
3856
        // party, to be locked in with a new update.
3857
        err = l.updateChannelFee(ctx, newCommitFee)
3✔
3858
        if err != nil {
3✔
NEW
3859
                return fmt.Errorf("unable to update fee rate: %w", err)
×
NEW
3860
        }
×
3861

3862
        return nil
3✔
3863
}
3864

3865
// toggleBatchTicker checks whether we need to resume or pause the batch ticker.
3866
// When we have no pending updates, the ticker is paused, otherwise resumed.
3867
func (l *channelLink) toggleBatchTicker() {
4,152✔
3868
        // If the previous event resulted in a non-empty batch, resume the batch
4,152✔
3869
        // ticker so that it can be cleared. Otherwise pause the ticker to
4,152✔
3870
        // prevent waking up the htlcManager while the batch is empty.
4,152✔
3871
        numUpdates := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)
4,152✔
3872
        if numUpdates > 0 {
4,669✔
3873
                l.cfg.BatchTicker.Resume()
517✔
3874
                l.log.Tracef("BatchTicker resumed, NumPendingUpdates(Local, "+
517✔
3875
                        "Remote)=%d", numUpdates)
517✔
3876

517✔
3877
                return
517✔
3878
        }
517✔
3879

3880
        l.cfg.BatchTicker.Pause()
3,638✔
3881
        l.log.Trace("BatchTicker paused due to zero NumPendingUpdates" +
3,638✔
3882
                "(Local, Remote)")
3,638✔
3883
}
3884

3885
// resumeLink is called when starting a previous link. It will go through the
3886
// reestablishment protocol and reforwarding packets that are yet resolved.
3887
func (l *channelLink) resumeLink(ctx context.Context) error {
216✔
3888
        // If this isn't the first time that this channel link has been created,
216✔
3889
        // then we'll need to check to see if we need to re-synchronize state
216✔
3890
        // with the remote peer. settledHtlcs is a map of HTLC's that we
216✔
3891
        // re-settled as part of the channel state sync.
216✔
3892
        if l.cfg.SyncStates {
389✔
3893
                err := l.syncChanStates(ctx)
173✔
3894
                if err != nil {
176✔
3895
                        l.handleChanSyncErr(err)
3✔
3896

3✔
3897
                        return err
3✔
3898
                }
3✔
3899
        }
3900

3901
        // If a shutdown message has previously been sent on this link, then we
3902
        // need to make sure that we have disabled any HTLC adds on the outgoing
3903
        // direction of the link and that we re-resend the same shutdown message
3904
        // that we previously sent.
3905
        //
3906
        // TODO(yy): we should either move this to chanCloser, or move all
3907
        // shutdown handling logic to be managed by the link, but not a mixed of
3908
        // partial management by two subsystems.
3909
        l.cfg.PreviouslySentShutdown.WhenSome(func(shutdown lnwire.Shutdown) {
219✔
3910
                // Immediately disallow any new outgoing HTLCs.
3✔
3911
                if !l.DisableAdds(Outgoing) {
3✔
NEW
3912
                        l.log.Warnf("Outgoing link adds already disabled")
×
NEW
3913
                }
×
3914

3915
                // Re-send the shutdown message the peer. Since syncChanStates
3916
                // would have sent any outstanding CommitSig, it is fine for us
3917
                // to immediately queue the shutdown message now.
3918
                err := l.cfg.Peer.SendMessage(false, &shutdown)
3✔
3919
                if err != nil {
3✔
NEW
3920
                        l.log.Warnf("Error sending shutdown message: %v", err)
×
NEW
3921
                }
×
3922
        })
3923

3924
        // We've successfully reestablished the channel, mark it as such to
3925
        // allow the switch to forward HTLCs in the outbound direction.
3926
        l.markReestablished()
216✔
3927

216✔
3928
        // With the channel states synced, we now reset the mailbox to ensure we
216✔
3929
        // start processing all unacked packets in order. This is done here to
216✔
3930
        // ensure that all acknowledgments that occur during channel
216✔
3931
        // resynchronization have taken affect, causing us only to pull unacked
216✔
3932
        // packets after starting to read from the downstream mailbox.
216✔
3933
        err := l.mailBox.ResetPackets()
216✔
3934
        if err != nil {
216✔
NEW
3935
                l.log.Errorf("failed to reset packets: %v", err)
×
NEW
3936
        }
×
3937

3938
        // If the channel is pending, there's no need to reforwarding packets.
3939
        if l.ShortChanID() == hop.Source {
216✔
NEW
3940
                return nil
×
NEW
3941
        }
×
3942

3943
        // After cleaning up any memory pertaining to incoming packets, we now
3944
        // replay our forwarding packages to handle any htlcs that can be
3945
        // processed locally, or need to be forwarded out to the switch. We will
3946
        // only attempt to resolve packages if our short chan id indicates that
3947
        // the channel is not pending, otherwise we should have no htlcs to
3948
        // reforward.
3949
        err = l.resolveFwdPkgs(ctx)
216✔
3950

216✔
3951
        // No error was encountered, success.
216✔
3952
        if err == nil {
431✔
3953
                return nil
215✔
3954
        }
215✔
3955

3956
        switch {
1✔
3957
        // If the duplicate keystone error was encountered, we'll fail without
3958
        // sending an Error message to the peer.
NEW
3959
        case errors.Is(err, ErrDuplicateKeystone):
×
NEW
3960
                l.failf(LinkFailureError{code: ErrCircuitError},
×
NEW
3961
                        "temporary circuit error: %v", err)
×
3962

3963
        // A non-nil error was encountered, send an Error message to the peer.
3964
        default:
1✔
3965
                l.failf(LinkFailureError{code: ErrInternalError},
1✔
3966
                        "unable to resolve fwd pkgs: %v", err)
1✔
3967
        }
3968

3969
        // With our link's in-memory state fully reconstructed, spawn a
3970
        // goroutine to manage the reclamation of disk space occupied by
3971
        // completed forwarding packages.
3972
        l.cg.WgAdd(1)
1✔
3973
        go l.fwdPkgGarbager()
1✔
3974

1✔
3975
        return err
1✔
3976
}
3977

3978
// processRemoteUpdateAddHTLC takes an `UpdateAddHTLC` msg sent from the remote
3979
// and processes it.
3980
func (l *channelLink) processRemoteUpdateAddHTLC(
3981
        msg *lnwire.UpdateAddHTLC) error {
453✔
3982

453✔
3983
        if l.IsFlushing(Incoming) {
453✔
NEW
3984
                // This is forbidden by the protocol specification. The best
×
NEW
3985
                // chance we have to deal with this is to drop the connection.
×
NEW
3986
                // This should roll back the channel state to the last
×
NEW
3987
                // CommitSig. If the remote has already sent a CommitSig we
×
NEW
3988
                // haven't received yet, channel state will be re-synchronized
×
NEW
3989
                // with a ChannelReestablish message upon reconnection and the
×
NEW
3990
                // protocol state that caused us to flush the link will be
×
NEW
3991
                // rolled back. In the event that there was some
×
NEW
3992
                // non-deterministic behavior in the remote that caused them to
×
NEW
3993
                // violate the protocol, we have a decent shot at correcting it
×
NEW
3994
                // this way, since reconnecting will put us in the cleanest
×
NEW
3995
                // possible state to try again.
×
NEW
3996
                //
×
NEW
3997
                // In addition to the above, it is possible for us to hit this
×
NEW
3998
                // case in situations where we improperly handle message
×
NEW
3999
                // ordering due to concurrency choices. An issue has been filed
×
NEW
4000
                // to address this here:
×
NEW
4001
                // https://github.com/lightningnetwork/lnd/issues/8393
×
NEW
4002
                err := errors.New("received add while link is flushing")
×
NEW
4003
                l.failf(
×
NEW
4004
                        LinkFailureError{
×
NEW
4005
                                code:             ErrInvalidUpdate,
×
NEW
4006
                                FailureAction:    LinkFailureDisconnect,
×
NEW
4007
                                PermanentFailure: false,
×
NEW
4008
                                Warning:          true,
×
NEW
4009
                        }, err.Error(),
×
NEW
4010
                )
×
NEW
4011

×
NEW
4012
                return err
×
NEW
4013
        }
×
4014

4015
        // Disallow htlcs with blinding points set if we haven't enabled the
4016
        // feature. This saves us from having to process the onion at all, but
4017
        // will only catch blinded payments where we are a relaying node (as the
4018
        // blinding point will be in the payload when we're the introduction
4019
        // node).
4020
        if msg.BlindingPoint.IsSome() && l.cfg.DisallowRouteBlinding {
453✔
NEW
4021
                err := errors.New("blinding point included when route " +
×
NEW
4022
                        "blinding is disabled")
×
NEW
4023

×
NEW
4024
                l.failf(LinkFailureError{code: ErrInvalidUpdate}, err.Error())
×
NEW
4025

×
NEW
4026
                return err
×
NEW
4027
        }
×
4028

4029
        // We have to check the limit here rather than later in the switch
4030
        // because the counterparty can keep sending HTLC's without sending a
4031
        // revoke. This would mean that the switch check would only occur later.
4032
        if l.isOverexposedWithHtlc(msg, true) {
453✔
NEW
4033
                err := errors.New("peer sent us an HTLC that exceeded our " +
×
NEW
4034
                        "max fee exposure")
×
NEW
4035
                l.failf(LinkFailureError{code: ErrInternalError}, err.Error())
×
NEW
4036

×
NEW
4037
                return err
×
NEW
4038
        }
×
4039

4040
        // We just received an add request from an upstream peer, so we add it
4041
        // to our state machine, then add the HTLC to our "settle" list in the
4042
        // event that we know the preimage.
4043
        index, err := l.channel.ReceiveHTLC(msg)
453✔
4044
        if err != nil {
453✔
NEW
4045
                l.failf(LinkFailureError{code: ErrInvalidUpdate},
×
NEW
4046
                        "unable to handle upstream add HTLC: %v", err)
×
NEW
4047

×
NEW
4048
                return err
×
NEW
4049
        }
×
4050

4051
        l.log.Tracef("receive upstream htlc with payment hash(%x), "+
453✔
4052
                "assigning index: %v", msg.PaymentHash[:], index)
453✔
4053

453✔
4054
        return nil
453✔
4055
}
4056

4057
// processRemoteUpdateFulfillHTLC takes an `UpdateFulfillHTLC` msg sent from the
4058
// remote and processes it.
4059
func (l *channelLink) processRemoteUpdateFulfillHTLC(
4060
        msg *lnwire.UpdateFulfillHTLC) error {
230✔
4061

230✔
4062
        pre := msg.PaymentPreimage
230✔
4063
        idx := msg.ID
230✔
4064

230✔
4065
        // Before we pipeline the settle, we'll check the set of active htlc's
230✔
4066
        // to see if the related UpdateAddHTLC has been fully locked-in.
230✔
4067
        var lockedin bool
230✔
4068
        htlcs := l.channel.ActiveHtlcs()
230✔
4069
        for _, add := range htlcs {
689✔
4070
                // The HTLC will be outgoing and match idx.
459✔
4071
                if !add.Incoming && add.HtlcIndex == idx {
687✔
4072
                        lockedin = true
228✔
4073
                        break
228✔
4074
                }
4075
        }
4076

4077
        if !lockedin {
232✔
4078
                err := errors.New("unable to handle upstream settle")
2✔
4079
                l.failf(LinkFailureError{code: ErrInvalidUpdate}, err.Error())
2✔
4080

2✔
4081
                return err
2✔
4082
        }
2✔
4083

4084
        if err := l.channel.ReceiveHTLCSettle(pre, idx); err != nil {
231✔
4085
                l.failf(
3✔
4086
                        LinkFailureError{
3✔
4087
                                code:          ErrInvalidUpdate,
3✔
4088
                                FailureAction: LinkFailureForceClose,
3✔
4089
                        },
3✔
4090
                        "unable to handle upstream settle HTLC: %v", err,
3✔
4091
                )
3✔
4092

3✔
4093
                return err
3✔
4094
        }
3✔
4095

4096
        settlePacket := &htlcPacket{
228✔
4097
                outgoingChanID: l.ShortChanID(),
228✔
4098
                outgoingHTLCID: idx,
228✔
4099
                htlc: &lnwire.UpdateFulfillHTLC{
228✔
4100
                        PaymentPreimage: pre,
228✔
4101
                },
228✔
4102
        }
228✔
4103

228✔
4104
        // Add the newly discovered preimage to our growing list of uncommitted
228✔
4105
        // preimage. These will be written to the witness cache just before
228✔
4106
        // accepting the next commitment signature from the remote peer.
228✔
4107
        l.uncommittedPreimages = append(l.uncommittedPreimages, pre)
228✔
4108

228✔
4109
        // Pipeline this settle, send it to the switch.
228✔
4110
        go l.forwardBatch(false, settlePacket)
228✔
4111

228✔
4112
        return nil
228✔
4113
}
4114

4115
// processRemoteUpdateFailMalformedHTLC takes an `UpdateFailMalformedHTLC` msg
4116
// sent from the remote and processes it.
4117
func (l *channelLink) processRemoteUpdateFailMalformedHTLC(
4118
        msg *lnwire.UpdateFailMalformedHTLC) error {
6✔
4119

6✔
4120
        // Convert the failure type encoded within the HTLC fail message to the
6✔
4121
        // proper generic lnwire error code.
6✔
4122
        var failure lnwire.FailureMessage
6✔
4123
        switch msg.FailureCode {
6✔
4124
        case lnwire.CodeInvalidOnionVersion:
4✔
4125
                failure = &lnwire.FailInvalidOnionVersion{
4✔
4126
                        OnionSHA256: msg.ShaOnionBlob,
4✔
4127
                }
4✔
NEW
4128
        case lnwire.CodeInvalidOnionHmac:
×
NEW
4129
                failure = &lnwire.FailInvalidOnionHmac{
×
NEW
4130
                        OnionSHA256: msg.ShaOnionBlob,
×
NEW
4131
                }
×
4132

NEW
4133
        case lnwire.CodeInvalidOnionKey:
×
NEW
4134
                failure = &lnwire.FailInvalidOnionKey{
×
NEW
4135
                        OnionSHA256: msg.ShaOnionBlob,
×
NEW
4136
                }
×
4137

4138
        // Handle malformed errors that are part of a blinded route. This case
4139
        // is slightly different, because we expect every relaying node in the
4140
        // blinded portion of the route to send malformed errors. If we're also
4141
        // a relaying node, we're likely going to switch this error out anyway
4142
        // for our own malformed error, but we handle the case here for
4143
        // completeness.
4144
        case lnwire.CodeInvalidBlinding:
3✔
4145
                failure = &lnwire.FailInvalidBlinding{
3✔
4146
                        OnionSHA256: msg.ShaOnionBlob,
3✔
4147
                }
3✔
4148

4149
        default:
2✔
4150
                l.log.Warnf("unexpected failure code received in "+
2✔
4151
                        "UpdateFailMailformedHTLC: %v", msg.FailureCode)
2✔
4152

2✔
4153
                // We don't just pass back the error we received from our
2✔
4154
                // successor. Otherwise we might report a failure that penalizes
2✔
4155
                // us more than needed. If the onion that we forwarded was
2✔
4156
                // correct, the node should have been able to send back its own
2✔
4157
                // failure. The node did not send back its own failure, so we
2✔
4158
                // assume there was a problem with the onion and report that
2✔
4159
                // back. We reuse the invalid onion key failure because there is
2✔
4160
                // no specific error for this case.
2✔
4161
                failure = &lnwire.FailInvalidOnionKey{
2✔
4162
                        OnionSHA256: msg.ShaOnionBlob,
2✔
4163
                }
2✔
4164
        }
4165

4166
        // With the error parsed, we'll convert the into it's opaque form.
4167
        var b bytes.Buffer
6✔
4168
        if err := lnwire.EncodeFailure(&b, failure, 0); err != nil {
6✔
NEW
4169
                return fmt.Errorf("unable to encode malformed error: %w", err)
×
NEW
4170
        }
×
4171

4172
        // If remote side have been unable to parse the onion blob we have sent
4173
        // to it, than we should transform the malformed HTLC message to the
4174
        // usual HTLC fail message.
4175
        err := l.channel.ReceiveFailHTLC(msg.ID, b.Bytes())
6✔
4176
        if err != nil {
6✔
NEW
4177
                l.failf(LinkFailureError{code: ErrInvalidUpdate},
×
NEW
4178
                        "unable to handle upstream fail HTLC: %v", err)
×
NEW
4179

×
NEW
4180
                return err
×
NEW
4181
        }
×
4182

4183
        return nil
6✔
4184
}
4185

4186
// processRemoteUpdateFailHTLC takes an `UpdateFailHTLC` msg sent from the
4187
// remote and processes it.
4188
func (l *channelLink) processRemoteUpdateFailHTLC(
4189
        msg *lnwire.UpdateFailHTLC) error {
123✔
4190

123✔
4191
        // Verify that the failure reason is at least 256 bytes plus overhead.
123✔
4192
        const minimumFailReasonLength = lnwire.FailureMessageLength + 2 + 2 + 32
123✔
4193

123✔
4194
        if len(msg.Reason) < minimumFailReasonLength {
124✔
4195
                // We've received a reason with a non-compliant length. Older
1✔
4196
                // nodes happily relay back these failures that may originate
1✔
4197
                // from a node further downstream. Therefore we can't just fail
1✔
4198
                // the channel.
1✔
4199
                //
1✔
4200
                // We want to be compliant ourselves, so we also can't pass back
1✔
4201
                // the reason unmodified. And we must make sure that we don't
1✔
4202
                // hit the magic length check of 260 bytes in
1✔
4203
                // processRemoteSettleFails either.
1✔
4204
                //
1✔
4205
                // Because the reason is unreadable for the payer anyway, we
1✔
4206
                // just replace it by a compliant-length series of random bytes.
1✔
4207
                msg.Reason = make([]byte, minimumFailReasonLength)
1✔
4208
                _, err := crand.Read(msg.Reason[:])
1✔
4209
                if err != nil {
1✔
NEW
4210
                        return fmt.Errorf("random generation error: %w", err)
×
NEW
4211
                }
×
4212
        }
4213

4214
        // Add fail to the update log.
4215
        idx := msg.ID
123✔
4216
        err := l.channel.ReceiveFailHTLC(idx, msg.Reason[:])
123✔
4217
        if err != nil {
123✔
NEW
4218
                l.failf(LinkFailureError{code: ErrInvalidUpdate},
×
NEW
4219
                        "unable to handle upstream fail HTLC: %v", err)
×
NEW
4220

×
NEW
4221
                return err
×
NEW
4222
        }
×
4223

4224
        return nil
123✔
4225
}
4226

4227
// processRemoteCommitSig takes a `CommitSig` msg sent from the remote and
4228
// processes it.
4229
func (l *channelLink) processRemoteCommitSig(ctx context.Context,
4230
        msg *lnwire.CommitSig) error {
1,187✔
4231

1,187✔
4232
        // Since we may have learned new preimages for the first time, we'll add
1,187✔
4233
        // them to our preimage cache. By doing this, we ensure any contested
1,187✔
4234
        // contracts watched by any on-chain arbitrators can now sweep this HTLC
1,187✔
4235
        // on-chain. We delay committing the preimages until just before
1,187✔
4236
        // accepting the new remote commitment, as afterwards the peer won't
1,187✔
4237
        // resend the Settle messages on the next channel reestablishment. Doing
1,187✔
4238
        // so allows us to more effectively batch this operation, instead of
1,187✔
4239
        // doing a single write per preimage.
1,187✔
4240
        err := l.cfg.PreimageCache.AddPreimages(l.uncommittedPreimages...)
1,187✔
4241
        if err != nil {
1,187✔
NEW
4242
                l.failf(
×
NEW
4243
                        LinkFailureError{code: ErrInternalError},
×
NEW
4244
                        "unable to add preimages=%v to cache: %v",
×
NEW
4245
                        l.uncommittedPreimages, err,
×
NEW
4246
                )
×
NEW
4247

×
NEW
4248
                return err
×
NEW
4249
        }
×
4250

4251
        // Instead of truncating the slice to conserve memory allocations, we
4252
        // simply set the uncommitted preimage slice to nil so that a new one
4253
        // will be initialized if any more witnesses are discovered. We do this
4254
        // because the maximum size that the slice can occupy is 15KB, and we
4255
        // want to ensure we release that memory back to the runtime.
4256
        l.uncommittedPreimages = nil
1,187✔
4257

1,187✔
4258
        // We just received a new updates to our local commitment chain,
1,187✔
4259
        // validate this new commitment, closing the link if invalid.
1,187✔
4260
        auxSigBlob, err := msg.CustomRecords.Serialize()
1,187✔
4261
        if err != nil {
1,187✔
NEW
4262
                l.failf(
×
NEW
4263
                        LinkFailureError{code: ErrInvalidCommitment},
×
NEW
4264
                        "unable to serialize custom records: %v", err,
×
NEW
4265
                )
×
NEW
4266

×
NEW
4267
                return err
×
NEW
4268
        }
×
4269
        err = l.channel.ReceiveNewCommitment(&lnwallet.CommitSigs{
1,187✔
4270
                CommitSig:  msg.CommitSig,
1,187✔
4271
                HtlcSigs:   msg.HtlcSigs,
1,187✔
4272
                PartialSig: msg.PartialSig,
1,187✔
4273
                AuxSigBlob: auxSigBlob,
1,187✔
4274
        })
1,187✔
4275
        if err != nil {
1,187✔
NEW
4276
                // If we were unable to reconstruct their proposed commitment,
×
NEW
4277
                // then we'll examine the type of error. If it's an
×
NEW
4278
                // InvalidCommitSigError, then we'll send a direct error.
×
NEW
4279
                var sendData []byte
×
NEW
4280
                switch {
×
NEW
4281
                case lnutils.ErrorAs[*lnwallet.InvalidCommitSigError](err):
×
NEW
4282
                        sendData = []byte(err.Error())
×
NEW
4283
                case lnutils.ErrorAs[*lnwallet.InvalidHtlcSigError](err):
×
NEW
4284
                        sendData = []byte(err.Error())
×
4285
                }
NEW
4286
                l.failf(
×
NEW
4287
                        LinkFailureError{
×
NEW
4288
                                code:          ErrInvalidCommitment,
×
NEW
4289
                                FailureAction: LinkFailureForceClose,
×
NEW
4290
                                SendData:      sendData,
×
NEW
4291
                        },
×
NEW
4292
                        "ChannelPoint(%v): unable to accept new "+
×
NEW
4293
                                "commitment: %v",
×
NEW
4294
                        l.channel.ChannelPoint(), err,
×
NEW
4295
                )
×
NEW
4296

×
NEW
4297
                return err
×
4298
        }
4299

4300
        // As we've just accepted a new state, we'll now immediately send the
4301
        // remote peer a revocation for our prior state.
4302
        nextRevocation, currentHtlcs, finalHTLCs, err :=
1,187✔
4303
                l.channel.RevokeCurrentCommitment()
1,187✔
4304
        if err != nil {
1,187✔
NEW
4305
                l.log.Errorf("unable to revoke commitment: %v", err)
×
NEW
4306

×
NEW
4307
                // We need to fail the channel in case revoking our local
×
NEW
4308
                // commitment does not succeed. We might have already advanced
×
NEW
4309
                // our channel state which would lead us to proceed with an
×
NEW
4310
                // unclean state.
×
NEW
4311
                //
×
NEW
4312
                // NOTE: We do not trigger a force close because this could
×
NEW
4313
                // resolve itself in case our db was just busy not accepting new
×
NEW
4314
                // transactions.
×
NEW
4315
                l.failf(
×
NEW
4316
                        LinkFailureError{
×
NEW
4317
                                code:          ErrInternalError,
×
NEW
4318
                                Warning:       true,
×
NEW
4319
                                FailureAction: LinkFailureDisconnect,
×
NEW
4320
                        },
×
NEW
4321
                        "ChannelPoint(%v): unable to accept new "+
×
NEW
4322
                                "commitment: %v",
×
NEW
4323
                        l.channel.ChannelPoint(), err,
×
NEW
4324
                )
×
NEW
4325

×
NEW
4326
                return err
×
NEW
4327
        }
×
4328

4329
        // As soon as we are ready to send our next revocation, we can invoke
4330
        // the incoming commit hooks.
4331
        l.Lock()
1,187✔
4332
        l.incomingCommitHooks.invoke()
1,187✔
4333
        l.Unlock()
1,187✔
4334

1,187✔
4335
        err = l.cfg.Peer.SendMessage(false, nextRevocation)
1,187✔
4336
        if err != nil {
1,188✔
4337
                l.log.Errorf("failed to send RevokeAndAck: %v", err)
1✔
4338
        }
1✔
4339

4340
        // Notify the incoming htlcs of which the resolutions were locked in.
4341
        for id, settled := range finalHTLCs {
1,521✔
4342
                l.cfg.HtlcNotifier.NotifyFinalHtlcEvent(
334✔
4343
                        models.CircuitKey{
334✔
4344
                                ChanID: l.ShortChanID(),
334✔
4345
                                HtlcID: id,
334✔
4346
                        },
334✔
4347
                        channeldb.FinalHtlcInfo{
334✔
4348
                                Settled:  settled,
334✔
4349
                                Offchain: true,
334✔
4350
                        },
334✔
4351
                )
334✔
4352
        }
334✔
4353

4354
        // Since we just revoked our commitment, we may have a new set of HTLC's
4355
        // on our commitment, so we'll send them using our function closure
4356
        // NotifyContractUpdate.
4357
        newUpdate := &contractcourt.ContractUpdate{
1,187✔
4358
                HtlcKey: contractcourt.LocalHtlcSet,
1,187✔
4359
                Htlcs:   currentHtlcs,
1,187✔
4360
        }
1,187✔
4361
        err = l.cfg.NotifyContractUpdate(newUpdate)
1,187✔
4362
        if err != nil {
1,187✔
NEW
4363
                return fmt.Errorf("unable to notify contract update: %w", err)
×
NEW
4364
        }
×
4365

4366
        select {
1,187✔
4367
        case <-l.cg.Done():
1✔
4368
                return nil
1✔
4369
        default:
1,186✔
4370
        }
4371

4372
        // If the remote party initiated the state transition, we'll reply with
4373
        // a signature to provide them with their version of the latest
4374
        // commitment. Otherwise, both commitment chains are fully synced from
4375
        // our PoV, then we don't need to reply with a signature as both sides
4376
        // already have a commitment with the latest accepted.
4377
        if l.channel.OweCommitment() {
1,826✔
4378
                if !l.updateCommitTxOrFail(ctx) {
640✔
NEW
4379
                        return nil
×
NEW
4380
                }
×
4381
        }
4382

4383
        // If we need to send out an Stfu, this would be the time to do so.
4384
        if l.noDanglingUpdates(lntypes.Local) {
2,269✔
4385
                err = l.quiescer.SendOwedStfu()
1,083✔
4386
                if err != nil {
1,083✔
NEW
4387
                        l.stfuFailf("sendOwedStfu: %v", err.Error())
×
NEW
4388
                }
×
4389
        }
4390

4391
        // Now that we have finished processing the incoming CommitSig and sent
4392
        // out our RevokeAndAck, we invoke the flushHooks if the channel state
4393
        // is clean.
4394
        l.Lock()
1,186✔
4395
        if l.channel.IsChannelClean() {
1,385✔
4396
                l.flushHooks.invoke()
199✔
4397
        }
199✔
4398
        l.Unlock()
1,186✔
4399

1,186✔
4400
        return nil
1,186✔
4401
}
4402

4403
// processRemoteRevokeAndAck takes a `RevokeAndAck` msg sent from the remote and
4404
// processes it.
4405
func (l *channelLink) processRemoteRevokeAndAck(ctx context.Context,
4406
        msg *lnwire.RevokeAndAck) error {
1,175✔
4407

1,175✔
4408
        // We've received a revocation from the remote chain, if valid, this
1,175✔
4409
        // moves the remote chain forward, and expands our revocation window.
1,175✔
4410

1,175✔
4411
        // We now process the message and advance our remote commit chain.
1,175✔
4412
        fwdPkg, remoteHTLCs, err := l.channel.ReceiveRevocation(msg)
1,175✔
4413
        if err != nil {
1,175✔
NEW
4414
                // TODO(halseth): force close?
×
NEW
4415
                l.failf(
×
NEW
4416
                        LinkFailureError{
×
NEW
4417
                                code:          ErrInvalidRevocation,
×
NEW
4418
                                FailureAction: LinkFailureDisconnect,
×
NEW
4419
                        },
×
NEW
4420
                        "unable to accept revocation: %v", err,
×
NEW
4421
                )
×
NEW
4422

×
NEW
4423
                return err
×
NEW
4424
        }
×
4425

4426
        // The remote party now has a new primary commitment, so we'll update
4427
        // the contract court to be aware of this new set (the prior old remote
4428
        // pending).
4429
        newUpdate := &contractcourt.ContractUpdate{
1,175✔
4430
                HtlcKey: contractcourt.RemoteHtlcSet,
1,175✔
4431
                Htlcs:   remoteHTLCs,
1,175✔
4432
        }
1,175✔
4433
        err = l.cfg.NotifyContractUpdate(newUpdate)
1,175✔
4434
        if err != nil {
1,175✔
NEW
4435
                return fmt.Errorf("unable to notify contract update: %w", err)
×
NEW
4436
        }
×
4437

4438
        select {
1,175✔
4439
        case <-l.cg.Done():
1✔
4440
                return nil
1✔
4441
        default:
1,174✔
4442
        }
4443

4444
        // If we have a tower client for this channel type, we'll create a
4445
        // backup for the current state.
4446
        if l.cfg.TowerClient != nil {
1,177✔
4447
                state := l.channel.State()
3✔
4448
                chanID := l.ChanID()
3✔
4449

3✔
4450
                err = l.cfg.TowerClient.BackupState(
3✔
4451
                        &chanID, state.RemoteCommitment.CommitHeight-1,
3✔
4452
                )
3✔
4453
                if err != nil {
3✔
NEW
4454
                        l.failf(LinkFailureError{
×
NEW
4455
                                code: ErrInternalError,
×
NEW
4456
                        }, "unable to queue breach backup: %v", err)
×
NEW
4457

×
NEW
4458
                        return err
×
NEW
4459
                }
×
4460
        }
4461

4462
        // If we can send updates then we can process adds in case we are the
4463
        // exit hop and need to send back resolutions, or in case there are
4464
        // validity issues with the packets. Otherwise we defer the action until
4465
        // resume.
4466
        //
4467
        // We are free to process the settles and fails without this check since
4468
        // processing those can't result in further updates to this channel
4469
        // link.
4470
        if l.quiescer.CanSendUpdates() {
2,347✔
4471
                l.processRemoteAdds(fwdPkg)
1,173✔
4472
        } else {
1,174✔
4473
                l.quiescer.OnResume(func() {
1✔
NEW
4474
                        l.processRemoteAdds(fwdPkg)
×
NEW
4475
                })
×
4476
        }
4477
        l.processRemoteSettleFails(fwdPkg)
1,174✔
4478

1,174✔
4479
        // If the link failed during processing the adds, we must return to
1,174✔
4480
        // ensure we won't attempted to update the state further.
1,174✔
4481
        if l.failed {
1,174✔
NEW
4482
                return nil
×
NEW
4483
        }
×
4484

4485
        // The revocation window opened up. If there are pending local updates,
4486
        // try to update the commit tx. Pending updates could already have been
4487
        // present because of a previously failed update to the commit tx or
4488
        // freshly added in by processRemoteAdds. Also in case there are no
4489
        // local updates, but there are still remote updates that are not in the
4490
        // remote commit tx yet, send out an update.
4491
        if l.channel.OweCommitment() {
1,486✔
4492
                if !l.updateCommitTxOrFail(ctx) {
319✔
4493
                        return nil
7✔
4494
                }
7✔
4495
        }
4496

4497
        // Now that we have finished processing the RevokeAndAck, we can invoke
4498
        // the flushHooks if the channel state is clean.
4499
        l.Lock()
1,167✔
4500
        if l.channel.IsChannelClean() {
1,330✔
4501
                l.flushHooks.invoke()
163✔
4502
        }
163✔
4503
        l.Unlock()
1,167✔
4504

1,167✔
4505
        return nil
1,167✔
4506
}
4507

4508
// processRemoteUpdateFee takes an `UpdateFee` msg sent from the remote and
4509
// processes it.
4510
func (l *channelLink) processRemoteUpdateFee(msg *lnwire.UpdateFee) error {
3✔
4511
        // Check and see if their proposed fee-rate would make us exceed the fee
3✔
4512
        // threshold.
3✔
4513
        fee := chainfee.SatPerKWeight(msg.FeePerKw)
3✔
4514

3✔
4515
        isDust, err := l.exceedsFeeExposureLimit(fee)
3✔
4516
        if err != nil {
3✔
NEW
4517
                // This shouldn't typically happen. If it does, it indicates
×
NEW
4518
                // something is wrong with our channel state.
×
NEW
4519
                l.log.Errorf("Unable to determine if fee threshold " +
×
NEW
4520
                        "exceeded")
×
NEW
4521
                l.failf(LinkFailureError{code: ErrInternalError},
×
NEW
4522
                        "error calculating fee exposure: %v", err)
×
NEW
4523

×
NEW
4524
                return err
×
NEW
4525
        }
×
4526

4527
        if isDust {
3✔
NEW
4528
                // The proposed fee-rate makes us exceed the fee threshold.
×
NEW
4529
                l.failf(LinkFailureError{code: ErrInternalError},
×
NEW
4530
                        "fee threshold exceeded: %v", err)
×
NEW
4531
                return err
×
NEW
4532
        }
×
4533

4534
        // We received fee update from peer. If we are the initiator we will
4535
        // fail the channel, if not we will apply the update.
4536
        if err := l.channel.ReceiveUpdateFee(fee); err != nil {
3✔
NEW
4537
                l.failf(LinkFailureError{code: ErrInvalidUpdate},
×
NEW
4538
                        "error receiving fee update: %v", err)
×
NEW
4539
                return err
×
NEW
4540
        }
×
4541

4542
        // Update the mailbox's feerate as well.
4543
        l.mailBox.SetFeeRate(fee)
3✔
4544

3✔
4545
        return nil
3✔
4546
}
4547

4548
// processRemoteError takes an `Error` msg sent from the remote and fails the
4549
// channel link.
4550
func (l *channelLink) processRemoteError(msg *lnwire.Error) {
2✔
4551
        // Error received from remote, MUST fail channel, but should only print
2✔
4552
        // the contents of the error message if all characters are printable
2✔
4553
        // ASCII.
2✔
4554
        l.failf(
2✔
4555
                // TODO(halseth): we currently don't fail the channel
2✔
4556
                // permanently, as there are some sync issues with other
2✔
4557
                // implementations that will lead to them sending an
2✔
4558
                // error message, but we can recover from on next
2✔
4559
                // connection. See
2✔
4560
                // https://github.com/ElementsProject/lightning/issues/4212
2✔
4561
                LinkFailureError{
2✔
4562
                        code:             ErrRemoteError,
2✔
4563
                        PermanentFailure: false,
2✔
4564
                },
2✔
4565
                "ChannelPoint(%v): received error from peer: %v",
2✔
4566
                l.channel.ChannelPoint(), msg.Error(),
2✔
4567
        )
2✔
4568
}
2✔
4569

4570
// processLocalUpdateFulfillHTLC takes an `UpdateFulfillHTLC` from the local and
4571
// processes it.
4572
func (l *channelLink) processLocalUpdateFulfillHTLC(ctx context.Context,
4573
        pkt *htlcPacket, htlc *lnwire.UpdateFulfillHTLC) {
26✔
4574

26✔
4575
        // If hodl.SettleOutgoing mode is active, we exit early to simulate
26✔
4576
        // arbitrary delays between the switch adding the SETTLE to the mailbox,
26✔
4577
        // and the HTLC being added to the commitment state.
26✔
4578
        if l.cfg.HodlMask.Active(hodl.SettleOutgoing) {
26✔
NEW
4579
                l.log.Warnf(hodl.SettleOutgoing.Warning())
×
NEW
4580
                l.mailBox.AckPacket(pkt.inKey())
×
NEW
4581

×
NEW
4582
                return
×
NEW
4583
        }
×
4584

4585
        // An HTLC we forward to the switch has just settled somewhere upstream.
4586
        // Therefore we settle the HTLC within the our local state machine.
4587
        inKey := pkt.inKey()
26✔
4588
        err := l.channel.SettleHTLC(
26✔
4589
                htlc.PaymentPreimage, pkt.incomingHTLCID, pkt.sourceRef,
26✔
4590
                pkt.destRef, &inKey,
26✔
4591
        )
26✔
4592
        if err != nil {
26✔
NEW
4593
                l.log.Errorf("unable to settle incoming HTLC for "+
×
NEW
4594
                        "circuit-key=%v: %v", inKey, err)
×
NEW
4595

×
NEW
4596
                // If the HTLC index for Settle response was not known to our
×
NEW
4597
                // commitment state, it has already been cleaned up by a prior
×
NEW
4598
                // response. We'll thus try to clean up any lingering state to
×
NEW
4599
                // ensure we don't continue reforwarding.
×
NEW
4600
                if lnutils.ErrorAs[lnwallet.ErrUnknownHtlcIndex](err) {
×
NEW
4601
                        l.cleanupSpuriousResponse(pkt)
×
NEW
4602
                }
×
4603

4604
                // Remove the packet from the link's mailbox to ensure it
4605
                // doesn't get replayed after a reconnection.
NEW
4606
                l.mailBox.AckPacket(inKey)
×
NEW
4607

×
NEW
4608
                return
×
4609
        }
4610

4611
        l.log.Debugf("queueing removal of SETTLE closed circuit: %s->%s",
26✔
4612
                pkt.inKey(), pkt.outKey())
26✔
4613

26✔
4614
        l.closedCircuits = append(l.closedCircuits, pkt.inKey())
26✔
4615

26✔
4616
        // With the HTLC settled, we'll need to populate the wire message to
26✔
4617
        // target the specific channel and HTLC to be canceled.
26✔
4618
        htlc.ChanID = l.ChanID()
26✔
4619
        htlc.ID = pkt.incomingHTLCID
26✔
4620

26✔
4621
        // Then we send the HTLC settle message to the connected peer so we can
26✔
4622
        // continue the propagation of the settle message.
26✔
4623
        err = l.cfg.Peer.SendMessage(false, htlc)
26✔
4624
        if err != nil {
26✔
NEW
4625
                l.log.Errorf("failed to send UpdateFulfillHTLC: %v", err)
×
NEW
4626
        }
×
4627

4628
        // Send a settle event notification to htlcNotifier.
4629
        l.cfg.HtlcNotifier.NotifySettleEvent(
26✔
4630
                newHtlcKey(pkt), htlc.PaymentPreimage, getEventType(pkt),
26✔
4631
        )
26✔
4632

26✔
4633
        // Immediately update the commitment tx to minimize latency.
26✔
4634
        l.updateCommitTxOrFail(ctx)
26✔
4635
}
4636

4637
// processLocalUpdateFailHTLC takes an `UpdateFailHTLC` from the local and
4638
// processes it.
4639
func (l *channelLink) processLocalUpdateFailHTLC(ctx context.Context,
4640
        pkt *htlcPacket, htlc *lnwire.UpdateFailHTLC) {
21✔
4641

21✔
4642
        // If hodl.FailOutgoing mode is active, we exit early to simulate
21✔
4643
        // arbitrary delays between the switch adding a FAIL to the mailbox, and
21✔
4644
        // the HTLC being added to the commitment state.
21✔
4645
        if l.cfg.HodlMask.Active(hodl.FailOutgoing) {
21✔
NEW
4646
                l.log.Warnf(hodl.FailOutgoing.Warning())
×
NEW
4647
                l.mailBox.AckPacket(pkt.inKey())
×
NEW
4648

×
NEW
4649
                return
×
NEW
4650
        }
×
4651

4652
        // An HTLC cancellation has been triggered somewhere upstream, we'll
4653
        // remove then HTLC from our local state machine.
4654
        inKey := pkt.inKey()
21✔
4655
        err := l.channel.FailHTLC(
21✔
4656
                pkt.incomingHTLCID, htlc.Reason, pkt.sourceRef, pkt.destRef,
21✔
4657
                &inKey,
21✔
4658
        )
21✔
4659
        if err != nil {
26✔
4660
                l.log.Errorf("unable to cancel incoming HTLC for "+
5✔
4661
                        "circuit-key=%v: %v", inKey, err)
5✔
4662

5✔
4663
                // If the HTLC index for Fail response was not known to our
5✔
4664
                // commitment state, it has already been cleaned up by a prior
5✔
4665
                // response. We'll thus try to clean up any lingering state to
5✔
4666
                // ensure we don't continue reforwarding.
5✔
4667
                if lnutils.ErrorAs[lnwallet.ErrUnknownHtlcIndex](err) {
7✔
4668
                        l.cleanupSpuriousResponse(pkt)
2✔
4669
                }
2✔
4670

4671
                // Remove the packet from the link's mailbox to ensure it
4672
                // doesn't get replayed after a reconnection.
4673
                l.mailBox.AckPacket(inKey)
5✔
4674

5✔
4675
                return
5✔
4676
        }
4677

4678
        l.log.Debugf("queueing removal of FAIL closed circuit: %s->%s",
19✔
4679
                pkt.inKey(), pkt.outKey())
19✔
4680

19✔
4681
        l.closedCircuits = append(l.closedCircuits, pkt.inKey())
19✔
4682

19✔
4683
        // With the HTLC removed, we'll need to populate the wire message to
19✔
4684
        // target the specific channel and HTLC to be canceled. The "Reason"
19✔
4685
        // field will have already been set within the switch.
19✔
4686
        htlc.ChanID = l.ChanID()
19✔
4687
        htlc.ID = pkt.incomingHTLCID
19✔
4688

19✔
4689
        // We send the HTLC message to the peer which initially created the
19✔
4690
        // HTLC. If the incoming blinding point is non-nil, we know that we are
19✔
4691
        // a relaying node in a blinded path. Otherwise, we're either an
19✔
4692
        // introduction node or not part of a blinded path at all.
19✔
4693
        err = l.sendIncomingHTLCFailureMsg(htlc.ID, pkt.obfuscator, htlc.Reason)
19✔
4694
        if err != nil {
19✔
NEW
4695
                l.log.Errorf("unable to send HTLC failure: %v", err)
×
NEW
4696

×
NEW
4697
                return
×
NEW
4698
        }
×
4699

4700
        // If the packet does not have a link failure set, it failed further
4701
        // down the route so we notify a forwarding failure. Otherwise, we
4702
        // notify a link failure because it failed at our node.
4703
        if pkt.linkFailure != nil {
32✔
4704
                l.cfg.HtlcNotifier.NotifyLinkFailEvent(
13✔
4705
                        newHtlcKey(pkt), newHtlcInfo(pkt), getEventType(pkt),
13✔
4706
                        pkt.linkFailure, false,
13✔
4707
                )
13✔
4708
        } else {
22✔
4709
                l.cfg.HtlcNotifier.NotifyForwardingFailEvent(
9✔
4710
                        newHtlcKey(pkt), getEventType(pkt),
9✔
4711
                )
9✔
4712
        }
9✔
4713

4714
        // Immediately update the commitment tx to minimize latency.
4715
        l.updateCommitTxOrFail(ctx)
19✔
4716
}
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