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

lightningnetwork / lnd / 16073727682

04 Jul 2025 12:24PM UTC coverage: 57.969% (+0.1%) from 57.822%
16073727682

Pull #10036

github

web-flow
Merge ab4a75e5b into b3eb9a3cb
Pull Request #10036: [graph mig 1]: graph/db: migrate graph nodes from kvdb to SQL

0 of 192 new or added lines in 1 file covered. (0.0%)

35 existing lines in 6 files now uncovered.

99021 of 170816 relevant lines covered (57.97%)

1.79 hits per line

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

74.8
/invoices/invoiceregistry.go
1
package invoices
2

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

11
        "github.com/lightningnetwork/lnd/clock"
12
        "github.com/lightningnetwork/lnd/fn/v2"
13
        "github.com/lightningnetwork/lnd/lntypes"
14
        "github.com/lightningnetwork/lnd/lnwire"
15
        "github.com/lightningnetwork/lnd/queue"
16
        "github.com/lightningnetwork/lnd/record"
17
)
18

19
var (
20
        // ErrInvoiceExpiryTooSoon is returned when an invoice is attempted to
21
        // be accepted or settled with not enough blocks remaining.
22
        ErrInvoiceExpiryTooSoon = errors.New("invoice expiry too soon")
23

24
        // ErrInvoiceAmountTooLow is returned  when an invoice is attempted to
25
        // be accepted or settled with an amount that is too low.
26
        ErrInvoiceAmountTooLow = errors.New(
27
                "paid amount less than invoice amount",
28
        )
29

30
        // ErrShuttingDown is returned when an operation failed because the
31
        // invoice registry is shutting down.
32
        ErrShuttingDown = errors.New("invoice registry shutting down")
33
)
34

35
const (
36
        // DefaultHtlcHoldDuration defines the default for how long mpp htlcs
37
        // are held while waiting for the other set members to arrive.
38
        DefaultHtlcHoldDuration = 120 * time.Second
39
)
40

41
// RegistryConfig contains the configuration parameters for invoice registry.
42
type RegistryConfig struct {
43
        // FinalCltvRejectDelta defines the number of blocks before the expiry
44
        // of the htlc where we no longer settle it as an exit hop and instead
45
        // cancel it back. Normally this value should be lower than the cltv
46
        // expiry of any invoice we create and the code effectuating this should
47
        // not be hit.
48
        FinalCltvRejectDelta int32
49

50
        // HtlcHoldDuration defines for how long mpp htlcs are held while
51
        // waiting for the other set members to arrive.
52
        HtlcHoldDuration time.Duration
53

54
        // Clock holds the clock implementation that is used to provide
55
        // Now() and TickAfter() and is useful to stub out the clock functions
56
        // during testing.
57
        Clock clock.Clock
58

59
        // AcceptKeySend indicates whether we want to accept spontaneous key
60
        // send payments.
61
        AcceptKeySend bool
62

63
        // AcceptAMP indicates whether we want to accept spontaneous AMP
64
        // payments.
65
        AcceptAMP bool
66

67
        // GcCanceledInvoicesOnStartup if set, we'll attempt to garbage collect
68
        // all canceled invoices upon start.
69
        GcCanceledInvoicesOnStartup bool
70

71
        // GcCanceledInvoicesOnTheFly if set, we'll garbage collect all newly
72
        // canceled invoices on the fly.
73
        GcCanceledInvoicesOnTheFly bool
74

75
        // KeysendHoldTime indicates for how long we want to accept and hold
76
        // spontaneous keysend payments.
77
        KeysendHoldTime time.Duration
78

79
        // HtlcInterceptor is an interface that allows the invoice registry to
80
        // let clients intercept invoices before they are settled.
81
        HtlcInterceptor HtlcInterceptor
82
}
83

84
// htlcReleaseEvent describes an htlc auto-release event. It is used to release
85
// mpp htlcs for which the complete set didn't arrive in time.
86
type htlcReleaseEvent struct {
87
        // invoiceRef identifiers the invoice this htlc belongs to.
88
        invoiceRef InvoiceRef
89

90
        // key is the circuit key of the htlc to release.
91
        key CircuitKey
92

93
        // releaseTime is the time at which to release the htlc.
94
        releaseTime time.Time
95
}
96

97
// Less is used to order PriorityQueueItem's by their release time such that
98
// items with the older release time are at the top of the queue.
99
//
100
// NOTE: Part of the queue.PriorityQueueItem interface.
101
func (r *htlcReleaseEvent) Less(other queue.PriorityQueueItem) bool {
3✔
102
        return r.releaseTime.Before(other.(*htlcReleaseEvent).releaseTime)
3✔
103
}
3✔
104

105
// InvoiceRegistry is a central registry of all the outstanding invoices
106
// created by the daemon. The registry is a thin wrapper around a map in order
107
// to ensure that all updates/reads are thread safe.
108
type InvoiceRegistry struct {
109
        started atomic.Bool
110
        stopped atomic.Bool
111

112
        sync.RWMutex
113

114
        nextClientID uint32 // must be used atomically
115

116
        idb InvoiceDB
117

118
        // cfg contains the registry's configuration parameters.
119
        cfg *RegistryConfig
120

121
        // notificationClientMux locks notificationClients and
122
        // singleNotificationClients. Using a separate mutex for these maps is
123
        // necessary to avoid deadlocks in the registry when processing invoice
124
        // events.
125
        notificationClientMux sync.RWMutex
126

127
        notificationClients map[uint32]*InvoiceSubscription
128

129
        // TODO(yy): use map[lntypes.Hash]*SingleInvoiceSubscription for better
130
        // performance.
131
        singleNotificationClients map[uint32]*SingleInvoiceSubscription
132

133
        // invoiceEvents is a single channel over which invoice updates are
134
        // carried.
135
        invoiceEvents chan *invoiceEvent
136

137
        // hodlSubscriptionsMux locks the hodlSubscriptions and
138
        // hodlReverseSubscriptions. Using a separate mutex for these maps is
139
        // necessary to avoid deadlocks in the registry when processing invoice
140
        // events.
141
        hodlSubscriptionsMux sync.RWMutex
142

143
        // hodlSubscriptions is a map from a circuit key to a list of
144
        // subscribers. It is used for efficient notification of links.
145
        hodlSubscriptions map[CircuitKey]map[chan<- interface{}]struct{}
146

147
        // reverseSubscriptions tracks circuit keys subscribed to per
148
        // subscriber. This is used to unsubscribe from all hashes efficiently.
149
        hodlReverseSubscriptions map[chan<- interface{}]map[CircuitKey]struct{}
150

151
        // htlcAutoReleaseChan contains the new htlcs that need to be
152
        // auto-released.
153
        htlcAutoReleaseChan chan *htlcReleaseEvent
154

155
        expiryWatcher *InvoiceExpiryWatcher
156

157
        wg   sync.WaitGroup
158
        quit chan struct{}
159
}
160

161
// NewRegistry creates a new invoice registry. The invoice registry
162
// wraps the persistent on-disk invoice storage with an additional in-memory
163
// layer. The in-memory layer is in place such that debug invoices can be added
164
// which are volatile yet available system wide within the daemon.
165
func NewRegistry(idb InvoiceDB, expiryWatcher *InvoiceExpiryWatcher,
166
        cfg *RegistryConfig) *InvoiceRegistry {
3✔
167

3✔
168
        notificationClients := make(map[uint32]*InvoiceSubscription)
3✔
169
        singleNotificationClients := make(map[uint32]*SingleInvoiceSubscription)
3✔
170
        return &InvoiceRegistry{
3✔
171
                idb:                       idb,
3✔
172
                notificationClients:       notificationClients,
3✔
173
                singleNotificationClients: singleNotificationClients,
3✔
174
                invoiceEvents:             make(chan *invoiceEvent, 100),
3✔
175
                hodlSubscriptions: make(
3✔
176
                        map[CircuitKey]map[chan<- interface{}]struct{},
3✔
177
                ),
3✔
178
                hodlReverseSubscriptions: make(
3✔
179
                        map[chan<- interface{}]map[CircuitKey]struct{},
3✔
180
                ),
3✔
181
                cfg:                 cfg,
3✔
182
                htlcAutoReleaseChan: make(chan *htlcReleaseEvent),
3✔
183
                expiryWatcher:       expiryWatcher,
3✔
184
                quit:                make(chan struct{}),
3✔
185
        }
3✔
186
}
3✔
187

188
// scanInvoicesOnStart will scan all invoices on start and add active invoices
189
// to the invoice expiry watcher while also attempting to delete all canceled
190
// invoices.
191
func (i *InvoiceRegistry) scanInvoicesOnStart(ctx context.Context) error {
3✔
192
        pendingInvoices, err := i.idb.FetchPendingInvoices(ctx)
3✔
193
        if err != nil {
3✔
194
                return err
×
195
        }
×
196

197
        var pending []invoiceExpiry
3✔
198
        for paymentHash, invoice := range pendingInvoices {
6✔
199
                invoice := invoice
3✔
200
                expiryRef := makeInvoiceExpiry(paymentHash, &invoice)
3✔
201
                if expiryRef != nil {
6✔
202
                        pending = append(pending, expiryRef)
3✔
203
                }
3✔
204
        }
205

206
        log.Debugf("Adding %d pending invoices to the expiry watcher",
3✔
207
                len(pending))
3✔
208
        i.expiryWatcher.AddInvoices(pending...)
3✔
209

3✔
210
        if i.cfg.GcCanceledInvoicesOnStartup {
3✔
211
                log.Infof("Deleting canceled invoices")
×
212
                err = i.idb.DeleteCanceledInvoices(ctx)
×
213
                if err != nil {
×
214
                        log.Warnf("Deleting canceled invoices failed: %v", err)
×
215
                        return err
×
216
                }
×
217
        }
218

219
        return nil
3✔
220
}
221

222
// Start starts the registry and all goroutines it needs to carry out its task.
223
func (i *InvoiceRegistry) Start() error {
3✔
224
        var err error
3✔
225

3✔
226
        log.Info("InvoiceRegistry starting...")
3✔
227

3✔
228
        if i.started.Swap(true) {
3✔
229
                return fmt.Errorf("InvoiceRegistry started more than once")
×
230
        }
×
231
        // Start InvoiceExpiryWatcher and prepopulate it with existing
232
        // active invoices.
233
        err = i.expiryWatcher.Start(
3✔
234
                func(hash lntypes.Hash, force bool) error {
6✔
235
                        return i.cancelInvoiceImpl(
3✔
236
                                context.Background(), hash, force,
3✔
237
                        )
3✔
238
                })
3✔
239
        if err != nil {
3✔
240
                return err
×
241
        }
×
242

243
        i.wg.Add(1)
3✔
244
        go i.invoiceEventLoop()
3✔
245

3✔
246
        // Now scan all pending and removable invoices to the expiry
3✔
247
        // watcher or delete them.
3✔
248
        err = i.scanInvoicesOnStart(context.Background())
3✔
249
        if err != nil {
3✔
250
                _ = i.Stop()
×
251
        }
×
252

253
        log.Debug("InvoiceRegistry started")
3✔
254

3✔
255
        return err
3✔
256
}
257

258
// Stop signals the registry for a graceful shutdown.
259
func (i *InvoiceRegistry) Stop() error {
3✔
260
        log.Info("InvoiceRegistry shutting down...")
3✔
261

3✔
262
        if i.stopped.Swap(true) {
3✔
263
                return fmt.Errorf("InvoiceRegistry stopped more than once")
×
264
        }
×
265

266
        log.Info("InvoiceRegistry shutting down...")
3✔
267
        defer log.Debug("InvoiceRegistry shutdown complete")
3✔
268

3✔
269
        var err error
3✔
270
        if i.expiryWatcher == nil {
3✔
271
                err = fmt.Errorf("InvoiceRegistry expiryWatcher is not " +
×
272
                        "initialized")
×
273
        } else {
3✔
274
                i.expiryWatcher.Stop()
3✔
275
        }
3✔
276

277
        close(i.quit)
3✔
278

3✔
279
        i.wg.Wait()
3✔
280

3✔
281
        log.Debug("InvoiceRegistry shutdown complete")
3✔
282

3✔
283
        return err
3✔
284
}
285

286
// invoiceEvent represents a new event that has modified on invoice on disk.
287
// Only two event types are currently supported: newly created invoices, and
288
// instance where invoices are settled.
289
type invoiceEvent struct {
290
        hash    lntypes.Hash
291
        invoice *Invoice
292
        setID   *[32]byte
293
}
294

295
// tickAt returns a channel that ticks at the specified time. If the time has
296
// already passed, it will tick immediately.
297
func (i *InvoiceRegistry) tickAt(t time.Time) <-chan time.Time {
3✔
298
        now := i.cfg.Clock.Now()
3✔
299
        return i.cfg.Clock.TickAfter(t.Sub(now))
3✔
300
}
3✔
301

302
// invoiceEventLoop is the dedicated goroutine responsible for accepting
303
// new notification subscriptions, cancelling old subscriptions, and
304
// dispatching new invoice events.
305
func (i *InvoiceRegistry) invoiceEventLoop() {
3✔
306
        defer i.wg.Done()
3✔
307

3✔
308
        // Set up a heap for htlc auto-releases.
3✔
309
        autoReleaseHeap := &queue.PriorityQueue{}
3✔
310

3✔
311
        for {
6✔
312
                // If there is something to release, set up a release tick
3✔
313
                // channel.
3✔
314
                var nextReleaseTick <-chan time.Time
3✔
315
                if autoReleaseHeap.Len() > 0 {
6✔
316
                        head := autoReleaseHeap.Top().(*htlcReleaseEvent)
3✔
317
                        nextReleaseTick = i.tickAt(head.releaseTime)
3✔
318
                }
3✔
319

320
                select {
3✔
321
                // A sub-systems has just modified the invoice state, so we'll
322
                // dispatch notifications to all registered clients.
323
                case event := <-i.invoiceEvents:
3✔
324
                        // For backwards compatibility, do not notify all
3✔
325
                        // invoice subscribers of cancel and accept events.
3✔
326
                        state := event.invoice.State
3✔
327
                        if state != ContractCanceled &&
3✔
328
                                state != ContractAccepted {
6✔
329

3✔
330
                                i.dispatchToClients(event)
3✔
331
                        }
3✔
332
                        i.dispatchToSingleClients(event)
3✔
333

334
                // A new htlc came in for auto-release.
335
                case event := <-i.htlcAutoReleaseChan:
3✔
336
                        log.Debugf("Scheduling auto-release for htlc: "+
3✔
337
                                "ref=%v, key=%v at %v",
3✔
338
                                event.invoiceRef, event.key, event.releaseTime)
3✔
339

3✔
340
                        // We use an independent timer for every htlc rather
3✔
341
                        // than a set timer that is reset with every htlc coming
3✔
342
                        // in. Otherwise the sender could keep resetting the
3✔
343
                        // timer until the broadcast window is entered and our
3✔
344
                        // channel is force closed.
3✔
345
                        autoReleaseHeap.Push(event)
3✔
346

347
                // The htlc at the top of the heap needs to be auto-released.
348
                case <-nextReleaseTick:
×
349
                        event := autoReleaseHeap.Pop().(*htlcReleaseEvent)
×
350
                        err := i.cancelSingleHtlc(
×
351
                                event.invoiceRef, event.key, ResultMppTimeout,
×
352
                        )
×
353
                        if err != nil {
×
354
                                log.Errorf("HTLC timer: %v", err)
×
355
                        }
×
356

357
                case <-i.quit:
3✔
358
                        return
3✔
359
                }
360
        }
361
}
362

363
// dispatchToSingleClients passes the supplied event to all notification
364
// clients that subscribed to all the invoice this event applies to.
365
func (i *InvoiceRegistry) dispatchToSingleClients(event *invoiceEvent) {
3✔
366
        // Dispatch to single invoice subscribers.
3✔
367
        clients := i.copySingleClients()
3✔
368
        for _, client := range clients {
6✔
369
                payHash := client.invoiceRef.PayHash()
3✔
370

3✔
371
                if payHash == nil || *payHash != event.hash {
6✔
372
                        continue
3✔
373
                }
374

375
                select {
3✔
376
                case <-client.backlogDelivered:
3✔
377
                        // We won't deliver any events until the backlog has
378
                        // went through first.
379
                case <-i.quit:
×
380
                        return
×
381
                }
382

383
                client.notify(event)
3✔
384
        }
385
}
386

387
// dispatchToClients passes the supplied event to all notification clients that
388
// subscribed to all invoices. Add and settle indices are used to make sure
389
// that clients don't receive duplicate or unwanted events.
390
func (i *InvoiceRegistry) dispatchToClients(event *invoiceEvent) {
3✔
391
        invoice := event.invoice
3✔
392

3✔
393
        clients := i.copyClients()
3✔
394
        for clientID, client := range clients {
6✔
395
                // Before we dispatch this event, we'll check
3✔
396
                // to ensure that this client hasn't already
3✔
397
                // received this notification in order to
3✔
398
                // ensure we don't duplicate any events.
3✔
399

3✔
400
                // TODO(joostjager): Refactor switches.
3✔
401
                state := event.invoice.State
3✔
402
                switch {
3✔
403
                // If we've already sent this settle event to
404
                // the client, then we can skip this.
405
                case state == ContractSettled &&
406
                        client.settleIndex >= invoice.SettleIndex:
×
407
                        continue
×
408

409
                // Similarly, if we've already sent this add to
410
                // the client then we can skip this one, but only if this isn't
411
                // an AMP invoice. AMP invoices always remain in the settle
412
                // state as a base invoice.
413
                case event.setID == nil && state == ContractOpen &&
414
                        client.addIndex >= invoice.AddIndex:
×
415
                        continue
×
416

417
                // These two states should never happen, but we
418
                // log them just in case so we can detect this
419
                // instance.
420
                case state == ContractOpen &&
421
                        client.addIndex+1 != invoice.AddIndex:
3✔
422
                        log.Warnf("client=%v for invoice "+
3✔
423
                                "notifications missed an update, "+
3✔
424
                                "add_index=%v, new add event index=%v",
3✔
425
                                clientID, client.addIndex,
3✔
426
                                invoice.AddIndex)
3✔
427

428
                case state == ContractSettled &&
429
                        client.settleIndex+1 != invoice.SettleIndex:
3✔
430
                        log.Warnf("client=%v for invoice "+
3✔
431
                                "notifications missed an update, "+
3✔
432
                                "settle_index=%v, new settle event index=%v",
3✔
433
                                clientID, client.settleIndex,
3✔
434
                                invoice.SettleIndex)
3✔
435
                }
436

437
                select {
3✔
438
                case <-client.backlogDelivered:
3✔
439
                        // We won't deliver any events until the backlog has
440
                        // been processed.
441
                case <-i.quit:
×
442
                        return
×
443
                }
444

445
                err := client.notify(&invoiceEvent{
3✔
446
                        invoice: invoice,
3✔
447
                        setID:   event.setID,
3✔
448
                })
3✔
449
                if err != nil {
3✔
450
                        log.Errorf("Failed dispatching to client: %v", err)
×
451
                        return
×
452
                }
×
453

454
                // Each time we send a notification to a client, we'll record
455
                // the latest add/settle index it has. We'll use this to ensure
456
                // we don't send a notification twice, which can happen if a new
457
                // event is added while we're catching up a new client.
458
                invState := event.invoice.State
3✔
459
                switch {
3✔
460
                case invState == ContractSettled:
3✔
461
                        client.settleIndex = invoice.SettleIndex
3✔
462

463
                case invState == ContractOpen && event.setID == nil:
3✔
464
                        client.addIndex = invoice.AddIndex
3✔
465

466
                // If this is an AMP invoice, then we'll need to use the set ID
467
                // to keep track of the settle index of the client. AMP
468
                // invoices never go to the open state, but if a setID is
469
                // passed, then we know it was just settled and will track the
470
                // highest settle index so far.
471
                case invState == ContractOpen && event.setID != nil:
3✔
472
                        setID := *event.setID
3✔
473
                        client.settleIndex = invoice.AMPState[setID].SettleIndex
3✔
474

475
                default:
×
476
                        log.Errorf("unexpected invoice state: %v",
×
477
                                event.invoice.State)
×
478
                }
479
        }
480
}
481

482
// deliverBacklogEvents will attempts to query the invoice database for any
483
// notifications that the client has missed since it reconnected last.
484
func (i *InvoiceRegistry) deliverBacklogEvents(ctx context.Context,
485
        client *InvoiceSubscription) error {
3✔
486

3✔
487
        log.Debugf("Collecting added invoices since %v for client %v",
3✔
488
                client.addIndex, client.id)
3✔
489

3✔
490
        addEvents, err := i.idb.InvoicesAddedSince(ctx, client.addIndex)
3✔
491
        if err != nil {
3✔
492
                return err
×
493
        }
×
494

495
        log.Debugf("Collecting settled invoices since %v for client %v",
3✔
496
                client.settleIndex, client.id)
3✔
497

3✔
498
        settleEvents, err := i.idb.InvoicesSettledSince(ctx, client.settleIndex)
3✔
499
        if err != nil {
3✔
500
                return err
×
501
        }
×
502

503
        log.Debugf("Delivering %d added invoices and %d settled invoices "+
3✔
504
                "for client %v", len(addEvents), len(settleEvents), client.id)
3✔
505

3✔
506
        // If we have any to deliver, then we'll append them to the end of the
3✔
507
        // notification queue in order to catch up the client before delivering
3✔
508
        // any new notifications.
3✔
509
        for _, addEvent := range addEvents {
6✔
510
                // We re-bind the loop variable to ensure we don't hold onto
3✔
511
                // the loop reference causing is to point to the same item.
3✔
512
                addEvent := addEvent
3✔
513

3✔
514
                select {
3✔
515
                case client.ntfnQueue.ChanIn() <- &invoiceEvent{
516
                        invoice: &addEvent,
517
                }:
3✔
518
                case <-i.quit:
×
519
                        return ErrShuttingDown
×
520
                }
521
        }
522

523
        for _, settleEvent := range settleEvents {
6✔
524
                // We re-bind the loop variable to ensure we don't hold onto
3✔
525
                // the loop reference causing is to point to the same item.
3✔
526
                settleEvent := settleEvent
3✔
527

3✔
528
                select {
3✔
529
                case client.ntfnQueue.ChanIn() <- &invoiceEvent{
530
                        invoice: &settleEvent,
531
                }:
3✔
532
                case <-i.quit:
×
533
                        return ErrShuttingDown
×
534
                }
535
        }
536

537
        return nil
3✔
538
}
539

540
// deliverSingleBacklogEvents will attempt to query the invoice database to
541
// retrieve the current invoice state and deliver this to the subscriber. Single
542
// invoice subscribers will always receive the current state right after
543
// subscribing. Only in case the invoice does not yet exist, nothing is sent
544
// yet.
545
func (i *InvoiceRegistry) deliverSingleBacklogEvents(ctx context.Context,
546
        client *SingleInvoiceSubscription) error {
3✔
547

3✔
548
        invoice, err := i.idb.LookupInvoice(ctx, client.invoiceRef)
3✔
549

3✔
550
        // It is possible that the invoice does not exist yet, but the client is
3✔
551
        // already watching it in anticipation.
3✔
552
        isNotFound := errors.Is(err, ErrInvoiceNotFound)
3✔
553
        isNotCreated := errors.Is(err, ErrNoInvoicesCreated)
3✔
554
        if isNotFound || isNotCreated {
6✔
555
                return nil
3✔
556
        }
3✔
557
        if err != nil {
3✔
558
                return err
×
559
        }
×
560

561
        payHash := client.invoiceRef.PayHash()
3✔
562
        if payHash == nil {
3✔
563
                return nil
×
564
        }
×
565

566
        err = client.notify(&invoiceEvent{
3✔
567
                hash:    *payHash,
3✔
568
                invoice: &invoice,
3✔
569
        })
3✔
570
        if err != nil {
3✔
571
                return err
×
572
        }
×
573

574
        log.Debugf("Client(id=%v) delivered single backlog event: payHash=%v",
3✔
575
                client.id, payHash)
3✔
576

3✔
577
        return nil
3✔
578
}
579

580
// AddInvoice adds a regular invoice for the specified amount, identified by
581
// the passed preimage. Additionally, any memo or receipt data provided will
582
// also be stored on-disk. Once this invoice is added, subsystems within the
583
// daemon add/forward HTLCs are able to obtain the proper preimage required for
584
// redemption in the case that we're the final destination. We also return the
585
// addIndex of the newly created invoice which monotonically increases for each
586
// new invoice added.  A side effect of this function is that it also sets
587
// AddIndex on the invoice argument.
588
func (i *InvoiceRegistry) AddInvoice(ctx context.Context, invoice *Invoice,
589
        paymentHash lntypes.Hash) (uint64, error) {
3✔
590

3✔
591
        i.Lock()
3✔
592

3✔
593
        ref := InvoiceRefByHash(paymentHash)
3✔
594
        log.Debugf("Invoice%v: added with terms %v", ref, invoice.Terms)
3✔
595

3✔
596
        addIndex, err := i.idb.AddInvoice(ctx, invoice, paymentHash)
3✔
597
        if err != nil {
6✔
598
                i.Unlock()
3✔
599
                return 0, err
3✔
600
        }
3✔
601

602
        // Now that we've added the invoice, we'll send dispatch a message to
603
        // notify the clients of this new invoice.
604
        i.notifyClients(paymentHash, invoice, nil)
3✔
605
        i.Unlock()
3✔
606

3✔
607
        // InvoiceExpiryWatcher.AddInvoice must not be locked by InvoiceRegistry
3✔
608
        // to avoid deadlock when a new invoice is added while an other is being
3✔
609
        // canceled.
3✔
610
        invoiceExpiryRef := makeInvoiceExpiry(paymentHash, invoice)
3✔
611
        if invoiceExpiryRef != nil {
6✔
612
                i.expiryWatcher.AddInvoices(invoiceExpiryRef)
3✔
613
        }
3✔
614

615
        return addIndex, nil
3✔
616
}
617

618
// LookupInvoice looks up an invoice by its payment hash (R-Hash), if found
619
// then we're able to pull the funds pending within an HTLC.
620
//
621
// TODO(roasbeef): ignore if settled?
622
func (i *InvoiceRegistry) LookupInvoice(ctx context.Context,
623
        rHash lntypes.Hash) (Invoice, error) {
3✔
624

3✔
625
        // We'll check the database to see if there's an existing matching
3✔
626
        // invoice.
3✔
627
        ref := InvoiceRefByHash(rHash)
3✔
628
        return i.idb.LookupInvoice(ctx, ref)
3✔
629
}
3✔
630

631
// LookupInvoiceByRef looks up an invoice by the given reference, if found
632
// then we're able to pull the funds pending within an HTLC.
633
func (i *InvoiceRegistry) LookupInvoiceByRef(ctx context.Context,
634
        ref InvoiceRef) (Invoice, error) {
3✔
635

3✔
636
        return i.idb.LookupInvoice(ctx, ref)
3✔
637
}
3✔
638

639
// startHtlcTimer starts a new timer via the invoice registry main loop that
640
// cancels a single htlc on an invoice when the htlc hold duration has passed.
641
func (i *InvoiceRegistry) startHtlcTimer(invoiceRef InvoiceRef,
642
        key CircuitKey, acceptTime time.Time) error {
3✔
643

3✔
644
        releaseTime := acceptTime.Add(i.cfg.HtlcHoldDuration)
3✔
645
        event := &htlcReleaseEvent{
3✔
646
                invoiceRef:  invoiceRef,
3✔
647
                key:         key,
3✔
648
                releaseTime: releaseTime,
3✔
649
        }
3✔
650

3✔
651
        select {
3✔
652
        case i.htlcAutoReleaseChan <- event:
3✔
653
                return nil
3✔
654

655
        case <-i.quit:
×
656
                return ErrShuttingDown
×
657
        }
658
}
659

660
// cancelSingleHtlc cancels a single accepted htlc on an invoice. It takes
661
// a resolution result which will be used to notify subscribed links and
662
// resolvers of the details of the htlc cancellation.
663
func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef InvoiceRef,
664
        key CircuitKey, result FailResolutionResult) error {
×
665

×
666
        updateInvoice := func(invoice *Invoice, setID *SetID) (
×
667
                *InvoiceUpdateDesc, error) {
×
668

×
669
                // Only allow individual htlc cancellation on open invoices.
×
670
                if invoice.State != ContractOpen {
×
671
                        log.Debugf("CancelSingleHtlc: cannot cancel htlc %v "+
×
672
                                "on invoice %v, invoice is no longer open", key,
×
673
                                invoiceRef)
×
674

×
675
                        return nil, nil
×
676
                }
×
677

678
                // Also for AMP invoices we fetch the relevant HTLCs, so
679
                // the HTLC should be found, otherwise we return an error.
680
                htlc, ok := invoice.Htlcs[key]
×
681
                if !ok {
×
682
                        return nil, fmt.Errorf("htlc %v not found on "+
×
683
                                "invoice %v", key, invoiceRef)
×
684
                }
×
685

686
                htlcState := htlc.State
×
687

×
688
                // Cancellation is only possible if the htlc wasn't already
×
689
                // resolved.
×
690
                if htlcState != HtlcStateAccepted {
×
691
                        log.Debugf("CancelSingleHtlc: htlc %v on invoice %v "+
×
692
                                "is already resolved", key, invoiceRef)
×
693

×
694
                        return nil, nil
×
695
                }
×
696

697
                log.Debugf("CancelSingleHtlc: cancelling htlc %v on invoice %v",
×
698
                        key, invoiceRef)
×
699

×
700
                // Return an update descriptor that cancels htlc and keeps
×
701
                // invoice open.
×
702
                canceledHtlcs := map[CircuitKey]struct{}{
×
703
                        key: {},
×
704
                }
×
705

×
706
                return &InvoiceUpdateDesc{
×
707
                        UpdateType:  CancelHTLCsUpdate,
×
708
                        CancelHtlcs: canceledHtlcs,
×
709
                        SetID:       setID,
×
710
                }, nil
×
711
        }
712

713
        // Try to mark the specified htlc as canceled in the invoice database.
714
        // Intercept the update descriptor to set the local updated variable. If
715
        // no invoice update is performed, we can return early.
716
        // setID is only set for AMP HTLCs, so it can be nil and it is expected
717
        // to be nil for non-AMP HTLCs.
718
        setID := (*SetID)(invoiceRef.SetID())
×
719

×
720
        var updated bool
×
721
        invoice, err := i.idb.UpdateInvoice(
×
722
                context.Background(), invoiceRef, setID,
×
723
                func(invoice *Invoice) (
×
724
                        *InvoiceUpdateDesc, error) {
×
725

×
726
                        updateDesc, err := updateInvoice(invoice, setID)
×
727
                        if err != nil {
×
728
                                return nil, err
×
729
                        }
×
730
                        updated = updateDesc != nil
×
731

×
732
                        return updateDesc, err
×
733
                },
734
        )
735
        if err != nil {
×
736
                return err
×
737
        }
×
738
        if !updated {
×
739
                return nil
×
740
        }
×
741

742
        // The invoice has been updated. Notify subscribers of the htlc
743
        // resolution.
744
        htlc, ok := invoice.Htlcs[key]
×
745
        if !ok {
×
746
                return fmt.Errorf("htlc %v not found", key)
×
747
        }
×
748
        if htlc.State == HtlcStateCanceled {
×
749
                resolution := NewFailResolution(
×
750
                        key, int32(htlc.AcceptHeight), result,
×
751
                )
×
752

×
753
                log.Debugf("Signaling htlc(%v) cancellation of invoice(%v) "+
×
754
                        "with resolution(%v) to the link subsystem", key,
×
755
                        invoiceRef, result)
×
756

×
757
                i.notifyHodlSubscribers(resolution)
×
758
        }
×
759

760
        return nil
×
761
}
762

763
// processKeySend just-in-time inserts an invoice if this htlc is a keysend
764
// htlc.
765
func (i *InvoiceRegistry) processKeySend(ctx invoiceUpdateCtx) error {
3✔
766
        // Retrieve keysend record if present.
3✔
767
        preimageSlice, ok := ctx.customRecords[record.KeySendType]
3✔
768
        if !ok {
6✔
769
                return nil
3✔
770
        }
3✔
771

772
        // Cancel htlc is preimage is invalid.
773
        preimage, err := lntypes.MakePreimage(preimageSlice)
3✔
774
        if err != nil {
3✔
775
                return err
×
776
        }
×
777
        if preimage.Hash() != ctx.hash {
3✔
778
                return fmt.Errorf("invalid keysend preimage %v for hash %v",
×
779
                        preimage, ctx.hash)
×
780
        }
×
781

782
        // Only allow keysend for non-mpp payments.
783
        if ctx.mpp != nil {
3✔
784
                return errors.New("no mpp keysend supported")
×
785
        }
×
786

787
        // Create an invoice for the htlc amount.
788
        amt := ctx.amtPaid
3✔
789

3✔
790
        // Set tlv required feature vector on the invoice. Otherwise we wouldn't
3✔
791
        // be able to pay to it with keysend.
3✔
792
        rawFeatures := lnwire.NewRawFeatureVector(
3✔
793
                lnwire.TLVOnionPayloadRequired,
3✔
794
        )
3✔
795
        features := lnwire.NewFeatureVector(rawFeatures, lnwire.Features)
3✔
796

3✔
797
        // Use the minimum block delta that we require for settling htlcs.
3✔
798
        finalCltvDelta := i.cfg.FinalCltvRejectDelta
3✔
799

3✔
800
        // Pre-check expiry here to prevent inserting an invoice that will not
3✔
801
        // be settled.
3✔
802
        if ctx.expiry < uint32(ctx.currentHeight+finalCltvDelta) {
3✔
803
                return errors.New("final expiry too soon")
×
804
        }
×
805

806
        // The invoice database indexes all invoices by payment address, however
807
        // legacy keysend payment do not have one. In order to avoid a new
808
        // payment type on-disk wrt. to indexing, we'll continue to insert a
809
        // blank payment address which is special cased in the insertion logic
810
        // to not be indexed. In the future, once AMP is merged, this should be
811
        // replaced by generating a random payment address on the behalf of the
812
        // sender.
813
        payAddr := BlankPayAddr
3✔
814

3✔
815
        // Create placeholder invoice.
3✔
816
        invoice := &Invoice{
3✔
817
                CreationDate: i.cfg.Clock.Now(),
3✔
818
                Terms: ContractTerm{
3✔
819
                        FinalCltvDelta:  finalCltvDelta,
3✔
820
                        Value:           amt,
3✔
821
                        PaymentPreimage: &preimage,
3✔
822
                        PaymentAddr:     payAddr,
3✔
823
                        Features:        features,
3✔
824
                },
3✔
825
        }
3✔
826

3✔
827
        if i.cfg.KeysendHoldTime != 0 {
3✔
828
                invoice.HodlInvoice = true
×
829
                invoice.Terms.Expiry = i.cfg.KeysendHoldTime
×
830
        }
×
831

832
        // Insert invoice into database. Ignore duplicates, because this
833
        // may be a replay.
834
        _, err = i.AddInvoice(context.Background(), invoice, ctx.hash)
3✔
835
        if err != nil && !errors.Is(err, ErrDuplicateInvoice) {
3✔
836
                return err
×
837
        }
×
838

839
        return nil
3✔
840
}
841

842
// processAMP just-in-time inserts an invoice if this htlc is a keysend
843
// htlc.
844
func (i *InvoiceRegistry) processAMP(ctx invoiceUpdateCtx) error {
3✔
845
        // AMP payments MUST also include an MPP record.
3✔
846
        if ctx.mpp == nil {
3✔
847
                return errors.New("no MPP record for AMP")
×
848
        }
×
849

850
        // Create an invoice for the total amount expected, provided in the MPP
851
        // record.
852
        amt := ctx.mpp.TotalMsat()
3✔
853

3✔
854
        // Set the TLV required and MPP optional features on the invoice. We'll
3✔
855
        // also make the AMP features required so that it can't be paid by
3✔
856
        // legacy or MPP htlcs.
3✔
857
        rawFeatures := lnwire.NewRawFeatureVector(
3✔
858
                lnwire.TLVOnionPayloadRequired,
3✔
859
                lnwire.PaymentAddrOptional,
3✔
860
                lnwire.AMPRequired,
3✔
861
        )
3✔
862
        features := lnwire.NewFeatureVector(rawFeatures, lnwire.Features)
3✔
863

3✔
864
        // Use the minimum block delta that we require for settling htlcs.
3✔
865
        finalCltvDelta := i.cfg.FinalCltvRejectDelta
3✔
866

3✔
867
        // Pre-check expiry here to prevent inserting an invoice that will not
3✔
868
        // be settled.
3✔
869
        if ctx.expiry < uint32(ctx.currentHeight+finalCltvDelta) {
3✔
870
                return errors.New("final expiry too soon")
×
871
        }
×
872

873
        // We'll use the sender-generated payment address provided in the HTLC
874
        // to create our AMP invoice.
875
        payAddr := ctx.mpp.PaymentAddr()
3✔
876

3✔
877
        // Create placeholder invoice.
3✔
878
        invoice := &Invoice{
3✔
879
                CreationDate: i.cfg.Clock.Now(),
3✔
880
                Terms: ContractTerm{
3✔
881
                        FinalCltvDelta:  finalCltvDelta,
3✔
882
                        Value:           amt,
3✔
883
                        PaymentPreimage: nil,
3✔
884
                        PaymentAddr:     payAddr,
3✔
885
                        Features:        features,
3✔
886
                },
3✔
887
        }
3✔
888

3✔
889
        // Insert invoice into database. Ignore duplicates payment hashes and
3✔
890
        // payment addrs, this may be a replay or a different HTLC for the AMP
3✔
891
        // invoice.
3✔
892
        _, err := i.AddInvoice(context.Background(), invoice, ctx.hash)
3✔
893
        isDuplicatedInvoice := errors.Is(err, ErrDuplicateInvoice)
3✔
894
        isDuplicatedPayAddr := errors.Is(err, ErrDuplicatePayAddr)
3✔
895
        switch {
3✔
896
        case isDuplicatedInvoice || isDuplicatedPayAddr:
3✔
897
                return nil
3✔
898
        default:
3✔
899
                return err
3✔
900
        }
901
}
902

903
// NotifyExitHopHtlc attempts to mark an invoice as settled. The return value
904
// describes how the htlc should be resolved.
905
//
906
// When the preimage of the invoice is not yet known (hodl invoice), this
907
// function moves the invoice to the accepted state. When SettleHoldInvoice is
908
// called later, a resolution message will be send back to the caller via the
909
// provided hodlChan. Invoice registry sends on this channel what action needs
910
// to be taken on the htlc (settle or cancel). The caller needs to ensure that
911
// the channel is either buffered or received on from another goroutine to
912
// prevent deadlock.
913
//
914
// In the case that the htlc is part of a larger set of htlcs that pay to the
915
// same invoice (multi-path payment), the htlc is held until the set is
916
// complete. If the set doesn't fully arrive in time, a timer will cancel the
917
// held htlc.
918
func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
919
        amtPaid lnwire.MilliSatoshi, expiry uint32, currentHeight int32,
920
        circuitKey CircuitKey, hodlChan chan<- interface{},
921
        wireCustomRecords lnwire.CustomRecords,
922
        payload Payload) (HtlcResolution, error) {
3✔
923

3✔
924
        // Create the update context containing the relevant details of the
3✔
925
        // incoming htlc.
3✔
926
        ctx := invoiceUpdateCtx{
3✔
927
                hash:                 rHash,
3✔
928
                circuitKey:           circuitKey,
3✔
929
                amtPaid:              amtPaid,
3✔
930
                expiry:               expiry,
3✔
931
                currentHeight:        currentHeight,
3✔
932
                finalCltvRejectDelta: i.cfg.FinalCltvRejectDelta,
3✔
933
                wireCustomRecords:    wireCustomRecords,
3✔
934
                customRecords:        payload.CustomRecords(),
3✔
935
                mpp:                  payload.MultiPath(),
3✔
936
                amp:                  payload.AMPRecord(),
3✔
937
                metadata:             payload.Metadata(),
3✔
938
                pathID:               payload.PathID(),
3✔
939
                totalAmtMsat:         payload.TotalAmtMsat(),
3✔
940
        }
3✔
941

3✔
942
        switch {
3✔
943
        // If we are accepting spontaneous AMP payments and this payload
944
        // contains an AMP record, create an AMP invoice that will be settled
945
        // below.
946
        case i.cfg.AcceptAMP && ctx.amp != nil:
3✔
947
                err := i.processAMP(ctx)
3✔
948
                if err != nil {
3✔
949
                        ctx.log(fmt.Sprintf("amp error: %v", err))
×
950

×
951
                        return NewFailResolution(
×
952
                                circuitKey, currentHeight, ResultAmpError,
×
953
                        ), nil
×
954
                }
×
955

956
        // If we are accepting spontaneous keysend payments, create a regular
957
        // invoice that will be settled below. We also enforce that this is only
958
        // done when no AMP payload is present since it will only be settle-able
959
        // by regular HTLCs.
960
        case i.cfg.AcceptKeySend && ctx.amp == nil:
3✔
961
                err := i.processKeySend(ctx)
3✔
962
                if err != nil {
3✔
963
                        ctx.log(fmt.Sprintf("keysend error: %v", err))
×
964

×
965
                        return NewFailResolution(
×
966
                                circuitKey, currentHeight, ResultKeySendError,
×
967
                        ), nil
×
968
                }
×
969
        }
970

971
        // Execute locked notify exit hop logic.
972
        i.Lock()
3✔
973
        resolution, invoiceToExpire, err := i.notifyExitHopHtlcLocked(
3✔
974
                &ctx, hodlChan,
3✔
975
        )
3✔
976
        i.Unlock()
3✔
977
        if err != nil {
3✔
978
                return nil, err
×
979
        }
×
980

981
        if invoiceToExpire != nil {
6✔
982
                i.expiryWatcher.AddInvoices(invoiceToExpire)
3✔
983
        }
3✔
984

985
        switch r := resolution.(type) {
3✔
986
        // The htlc is held. Start a timer outside the lock if the htlc should
987
        // be auto-released, because otherwise a deadlock may happen with the
988
        // main event loop.
989
        case *htlcAcceptResolution:
3✔
990
                if r.autoRelease {
6✔
991
                        var invRef InvoiceRef
3✔
992
                        if ctx.amp != nil {
6✔
993
                                invRef = InvoiceRefBySetID(*ctx.setID())
3✔
994
                        } else {
6✔
995
                                invRef = ctx.invoiceRef()
3✔
996
                        }
3✔
997

998
                        err := i.startHtlcTimer(
3✔
999
                                invRef, circuitKey, r.acceptTime,
3✔
1000
                        )
3✔
1001
                        if err != nil {
3✔
1002
                                return nil, err
×
1003
                        }
×
1004
                }
1005

1006
                // We return a nil resolution because htlc acceptances are
1007
                // represented as nil resolutions externally.
1008
                // TODO(carla) update calling code to handle accept resolutions.
1009
                return nil, nil
3✔
1010

1011
        // A direct resolution was received for this htlc.
1012
        case HtlcResolution:
3✔
1013
                return r, nil
3✔
1014

1015
        // Fail if an unknown resolution type was received.
1016
        default:
×
1017
                return nil, errors.New("invalid resolution type")
×
1018
        }
1019
}
1020

1021
// notifyExitHopHtlcLocked is the internal implementation of NotifyExitHopHtlc
1022
// that should be executed inside the registry lock. The returned invoiceExpiry
1023
// (if not nil) needs to be added to the expiry watcher outside of the lock.
1024
func (i *InvoiceRegistry) notifyExitHopHtlcLocked(
1025
        ctx *invoiceUpdateCtx, hodlChan chan<- interface{}) (
1026
        HtlcResolution, invoiceExpiry, error) {
3✔
1027

3✔
1028
        invoiceRef := ctx.invoiceRef()
3✔
1029

3✔
1030
        // This setID is only set for AMP HTLCs, so it can be nil and it is
3✔
1031
        // also expected to be nil for non-AMP HTLCs.
3✔
1032
        setID := (*SetID)(ctx.setID())
3✔
1033

3✔
1034
        // We need to look up the current state of the invoice in order to send
3✔
1035
        // the previously accepted/settled HTLCs to the interceptor.
3✔
1036
        existingInvoice, err := i.idb.LookupInvoice(
3✔
1037
                context.Background(), invoiceRef,
3✔
1038
        )
3✔
1039
        switch {
3✔
1040
        case errors.Is(err, ErrInvoiceNotFound) ||
1041
                errors.Is(err, ErrNoInvoicesCreated) ||
1042
                errors.Is(err, ErrInvRefEquivocation):
3✔
1043

3✔
1044
                log.Debugf("Invoice not found with error: %v, failing htlc",
3✔
1045
                        err)
3✔
1046

3✔
1047
                // If the invoice was not found, return a failure resolution
3✔
1048
                // with an invoice not found result.
3✔
1049
                return NewFailResolution(
3✔
1050
                        ctx.circuitKey, ctx.currentHeight,
3✔
1051
                        ResultInvoiceNotFound,
3✔
1052
                ), nil, nil
3✔
1053

1054
        case err != nil:
×
1055
                ctx.log(err.Error())
×
1056
                return nil, nil, err
×
1057
        }
1058

1059
        var cancelSet bool
3✔
1060

3✔
1061
        // Provide the invoice to the settlement interceptor to allow
3✔
1062
        // the interceptor's client an opportunity to manipulate the
3✔
1063
        // settlement process.
3✔
1064
        err = i.cfg.HtlcInterceptor.Intercept(HtlcModifyRequest{
3✔
1065
                WireCustomRecords:  ctx.wireCustomRecords,
3✔
1066
                ExitHtlcCircuitKey: ctx.circuitKey,
3✔
1067
                ExitHtlcAmt:        ctx.amtPaid,
3✔
1068
                ExitHtlcExpiry:     ctx.expiry,
3✔
1069
                CurrentHeight:      uint32(ctx.currentHeight),
3✔
1070
                Invoice:            existingInvoice,
3✔
1071
        }, func(resp HtlcModifyResponse) {
6✔
1072
                log.Debugf("Received invoice HTLC interceptor response: %v",
3✔
1073
                        resp)
3✔
1074

3✔
1075
                if resp.AmountPaid != 0 {
6✔
1076
                        ctx.amtPaid = resp.AmountPaid
3✔
1077
                }
3✔
1078

1079
                cancelSet = resp.CancelSet
3✔
1080
        })
1081
        if err != nil {
3✔
1082
                err := fmt.Errorf("error during invoice HTLC interception: %w",
×
1083
                        err)
×
1084
                ctx.log(err.Error())
×
1085

×
1086
                return nil, nil, err
×
1087
        }
×
1088

1089
        // We'll attempt to settle an invoice matching this rHash on disk (if
1090
        // one exists). The callback will update the invoice state and/or htlcs.
1091
        var (
3✔
1092
                resolution        HtlcResolution
3✔
1093
                updateSubscribers bool
3✔
1094
        )
3✔
1095
        callback := func(inv *Invoice) (*InvoiceUpdateDesc, error) {
6✔
1096
                // First check if this is a replayed htlc and resolve it
3✔
1097
                // according to its current state. We cannot decide differently
3✔
1098
                // once the HTLC has already been processed before.
3✔
1099
                isReplayed, res, err := resolveReplayedHtlc(ctx, inv)
3✔
1100
                if err != nil {
3✔
1101
                        return nil, err
×
1102
                }
×
1103
                if isReplayed {
6✔
1104
                        resolution = res
3✔
1105
                        return nil, nil
3✔
1106
                }
3✔
1107

1108
                // In case the HTLC interceptor cancels the HTLC set, we do NOT
1109
                // cancel the invoice however we cancel the complete HTLC set.
1110
                if cancelSet {
6✔
1111
                        // If the invoice is not open, something is wrong, we
3✔
1112
                        // fail just the HTLC with the specific error.
3✔
1113
                        if inv.State != ContractOpen {
3✔
1114
                                log.Errorf("Invoice state (%v) is not OPEN, "+
×
1115
                                        "cancelling HTLC set not allowed by "+
×
1116
                                        "external source", inv.State)
×
1117

×
1118
                                resolution = NewFailResolution(
×
1119
                                        ctx.circuitKey, ctx.currentHeight,
×
1120
                                        ResultInvoiceNotOpen,
×
1121
                                )
×
1122

×
1123
                                return nil, nil
×
1124
                        }
×
1125

1126
                        // The error `ExternalValidationFailed` error
1127
                        // information will be packed in the
1128
                        // `FailIncorrectDetails` msg when sending the msg to
1129
                        // the peer. Error codes are defined by the BOLT 04
1130
                        // specification. The error text can be arbitrary
1131
                        // therefore we return a custom error msg.
1132
                        resolution = NewFailResolution(
3✔
1133
                                ctx.circuitKey, ctx.currentHeight,
3✔
1134
                                ExternalValidationFailed,
3✔
1135
                        )
3✔
1136

3✔
1137
                        // We cancel all HTLCs which are in the accepted state.
3✔
1138
                        //
3✔
1139
                        // NOTE: The current HTLC is not included because it
3✔
1140
                        // was never accepted in the first place.
3✔
1141
                        htlcs := inv.HTLCSet(ctx.setID(), HtlcStateAccepted)
3✔
1142
                        htlcKeys := fn.KeySet[CircuitKey](htlcs)
3✔
1143

3✔
1144
                        // The external source did cancel the htlc set, so we
3✔
1145
                        // cancel all HTLCs in the set. We however keep the
3✔
1146
                        // invoice in the open state.
3✔
1147
                        //
3✔
1148
                        // NOTE: The invoice event loop will still call the
3✔
1149
                        // `cancelSingleHTLC` method for MPP payments, however
3✔
1150
                        // because the HTLCs are already cancled back it will be
3✔
1151
                        // a NOOP.
3✔
1152
                        update := &InvoiceUpdateDesc{
3✔
1153
                                UpdateType:  CancelHTLCsUpdate,
3✔
1154
                                CancelHtlcs: htlcKeys,
3✔
1155
                                SetID:       setID,
3✔
1156
                        }
3✔
1157

3✔
1158
                        return update, nil
3✔
1159
                }
1160

1161
                updateDesc, res, err := updateInvoice(ctx, inv)
3✔
1162
                if err != nil {
3✔
1163
                        return nil, err
×
1164
                }
×
1165

1166
                // Set resolution in outer scope only after successful update.
1167
                resolution = res
3✔
1168

3✔
1169
                // Only send an update if the invoice state was changed.
3✔
1170
                updateSubscribers = updateDesc != nil &&
3✔
1171
                        updateDesc.State != nil
3✔
1172

3✔
1173
                return updateDesc, nil
3✔
1174
        }
1175

1176
        invoice, err := i.idb.UpdateInvoice(
3✔
1177
                context.Background(), invoiceRef, setID, callback,
3✔
1178
        )
3✔
1179

3✔
1180
        var duplicateSetIDErr ErrDuplicateSetID
3✔
1181
        if errors.As(err, &duplicateSetIDErr) {
3✔
1182
                return NewFailResolution(
×
1183
                        ctx.circuitKey, ctx.currentHeight,
×
1184
                        ResultInvoiceNotFound,
×
1185
                ), nil, nil
×
1186
        }
×
1187

1188
        switch {
3✔
1189
        case errors.Is(err, ErrInvoiceNotFound):
×
1190
                // If the invoice was not found, return a failure resolution
×
1191
                // with an invoice not found result.
×
1192
                return NewFailResolution(
×
1193
                        ctx.circuitKey, ctx.currentHeight,
×
1194
                        ResultInvoiceNotFound,
×
1195
                ), nil, nil
×
1196

1197
        case errors.Is(err, ErrInvRefEquivocation):
×
1198
                return NewFailResolution(
×
1199
                        ctx.circuitKey, ctx.currentHeight,
×
1200
                        ResultInvoiceNotFound,
×
1201
                ), nil, nil
×
1202

1203
        case err == nil:
3✔
1204

1205
        default:
×
1206
                ctx.log(err.Error())
×
1207
                return nil, nil, err
×
1208
        }
1209

1210
        var invoiceToExpire invoiceExpiry
3✔
1211

3✔
1212
        log.Tracef("Settlement resolution: %T %v", resolution, resolution)
3✔
1213

3✔
1214
        switch res := resolution.(type) {
3✔
1215
        case *HtlcFailResolution:
3✔
1216
                // Inspect latest htlc state on the invoice. If it is found,
3✔
1217
                // we will update the accept height as it was recorded in the
3✔
1218
                // invoice database (which occurs in the case where the htlc
3✔
1219
                // reached the database in a previous call). If the htlc was
3✔
1220
                // not found on the invoice, it was immediately failed so we
3✔
1221
                // send the failure resolution as is, which has the current
3✔
1222
                // height set as the accept height.
3✔
1223
                invoiceHtlc, ok := invoice.Htlcs[ctx.circuitKey]
3✔
1224
                if ok {
6✔
1225
                        res.AcceptHeight = int32(invoiceHtlc.AcceptHeight)
3✔
1226
                }
3✔
1227

1228
                ctx.log(fmt.Sprintf("failure resolution result "+
3✔
1229
                        "outcome: %v, at accept height: %v",
3✔
1230
                        res.Outcome, res.AcceptHeight))
3✔
1231

3✔
1232
                // Some failures apply to the entire HTLC set. Break here if
3✔
1233
                // this isn't one of them.
3✔
1234
                if !res.Outcome.IsSetFailure() {
6✔
1235
                        break
3✔
1236
                }
1237

1238
                // Also cancel any HTLCs in the HTLC set that are also in the
1239
                // canceled state with the same failure result.
1240
                setID := ctx.setID()
3✔
1241
                canceledHtlcSet := invoice.HTLCSet(setID, HtlcStateCanceled)
3✔
1242
                for key, htlc := range canceledHtlcSet {
3✔
1243
                        htlcFailResolution := NewFailResolution(
×
1244
                                key, int32(htlc.AcceptHeight), res.Outcome,
×
1245
                        )
×
1246

×
1247
                        i.notifyHodlSubscribers(htlcFailResolution)
×
1248
                }
×
1249

1250
        // If the htlc was settled, we will settle any previously accepted
1251
        // htlcs and notify our peer to settle them.
1252
        case *HtlcSettleResolution:
3✔
1253
                ctx.log(fmt.Sprintf("settle resolution result "+
3✔
1254
                        "outcome: %v, at accept height: %v",
3✔
1255
                        res.Outcome, res.AcceptHeight))
3✔
1256

3✔
1257
                // Also settle any previously accepted htlcs. If a htlc is
3✔
1258
                // marked as settled, we should follow now and settle the htlc
3✔
1259
                // with our peer.
3✔
1260
                setID := ctx.setID()
3✔
1261
                settledHtlcSet := invoice.HTLCSet(setID, HtlcStateSettled)
3✔
1262
                for key, htlc := range settledHtlcSet {
6✔
1263
                        preimage := res.Preimage
3✔
1264
                        if htlc.AMP != nil && htlc.AMP.Preimage != nil {
6✔
1265
                                preimage = *htlc.AMP.Preimage
3✔
1266
                        }
3✔
1267

1268
                        // Notify subscribers that the htlcs should be settled
1269
                        // with our peer. Note that the outcome of the
1270
                        // resolution is set based on the outcome of the single
1271
                        // htlc that we just settled, so may not be accurate
1272
                        // for all htlcs.
1273
                        htlcSettleResolution := NewSettleResolution(
3✔
1274
                                preimage, key,
3✔
1275
                                int32(htlc.AcceptHeight), res.Outcome,
3✔
1276
                        )
3✔
1277

3✔
1278
                        // Notify subscribers that the htlc should be settled
3✔
1279
                        // with our peer.
3✔
1280
                        i.notifyHodlSubscribers(htlcSettleResolution)
3✔
1281
                }
1282

1283
                // If concurrent payments were attempted to this invoice before
1284
                // the current one was ultimately settled, cancel back any of
1285
                // the HTLCs immediately. As a result of the settle, the HTLCs
1286
                // in other HTLC sets are automatically converted to a canceled
1287
                // state when updating the invoice.
1288
                //
1289
                // TODO(roasbeef): can remove now??
1290
                canceledHtlcSet := invoice.HTLCSetCompliment(
3✔
1291
                        setID, HtlcStateCanceled,
3✔
1292
                )
3✔
1293
                for key, htlc := range canceledHtlcSet {
3✔
1294
                        htlcFailResolution := NewFailResolution(
×
1295
                                key, int32(htlc.AcceptHeight),
×
1296
                                ResultInvoiceAlreadySettled,
×
1297
                        )
×
1298

×
1299
                        i.notifyHodlSubscribers(htlcFailResolution)
×
1300
                }
×
1301

1302
        // If we accepted the htlc, subscribe to the hodl invoice and return
1303
        // an accept resolution with the htlc's accept time on it.
1304
        case *htlcAcceptResolution:
3✔
1305
                invoiceHtlc, ok := invoice.Htlcs[ctx.circuitKey]
3✔
1306
                if !ok {
3✔
1307
                        return nil, nil, fmt.Errorf("accepted htlc: %v not"+
×
1308
                                " present on invoice: %x", ctx.circuitKey,
×
1309
                                ctx.hash[:])
×
1310
                }
×
1311

1312
                // Determine accepted height of this htlc. If the htlc reached
1313
                // the invoice database (possibly in a previous call to the
1314
                // invoice registry), we'll take the original accepted height
1315
                // as it was recorded in the database.
1316
                acceptHeight := int32(invoiceHtlc.AcceptHeight)
3✔
1317

3✔
1318
                ctx.log(fmt.Sprintf("accept resolution result "+
3✔
1319
                        "outcome: %v, at accept height: %v",
3✔
1320
                        res.outcome, acceptHeight))
3✔
1321

3✔
1322
                // Auto-release the htlc if the invoice is still open. It can
3✔
1323
                // only happen for mpp payments that there are htlcs in state
3✔
1324
                // Accepted while the invoice is Open.
3✔
1325
                if invoice.State == ContractOpen {
6✔
1326
                        res.acceptTime = invoiceHtlc.AcceptTime
3✔
1327
                        res.autoRelease = true
3✔
1328
                }
3✔
1329

1330
                // If we have fully accepted the set of htlcs for this invoice,
1331
                // we can now add it to our invoice expiry watcher. We do not
1332
                // add invoices before they are fully accepted, because it is
1333
                // possible that we MppTimeout the htlcs, and then our relevant
1334
                // expiry height could change.
1335
                if res.outcome == resultAccepted {
6✔
1336
                        invoiceToExpire = makeInvoiceExpiry(ctx.hash, invoice)
3✔
1337
                }
3✔
1338

1339
                // Subscribe to the resolution if the caller specified a
1340
                // notification channel.
1341
                if hodlChan != nil {
6✔
1342
                        i.hodlSubscribe(hodlChan, ctx.circuitKey)
3✔
1343
                }
3✔
1344

1345
        default:
×
1346
                panic("unknown action")
×
1347
        }
1348

1349
        // Now that the links have been notified of any state changes to their
1350
        // HTLCs, we'll go ahead and notify any clients waiting on the invoice
1351
        // state changes.
1352
        if updateSubscribers {
6✔
1353
                // We'll add a setID onto the notification, but only if this is
3✔
1354
                // an AMP invoice being settled.
3✔
1355
                var setID *[32]byte
3✔
1356
                if _, ok := resolution.(*HtlcSettleResolution); ok {
6✔
1357
                        setID = ctx.setID()
3✔
1358
                }
3✔
1359

1360
                i.notifyClients(ctx.hash, invoice, setID)
3✔
1361
        }
1362

1363
        return resolution, invoiceToExpire, nil
3✔
1364
}
1365

1366
// SettleHodlInvoice sets the preimage of a hodl invoice.
1367
func (i *InvoiceRegistry) SettleHodlInvoice(ctx context.Context,
1368
        preimage lntypes.Preimage) error {
3✔
1369

3✔
1370
        i.Lock()
3✔
1371
        defer i.Unlock()
3✔
1372

3✔
1373
        updateInvoice := func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
6✔
1374
                switch invoice.State {
3✔
1375
                case ContractOpen:
×
1376
                        return nil, ErrInvoiceStillOpen
×
1377

1378
                case ContractCanceled:
×
1379
                        return nil, ErrInvoiceAlreadyCanceled
×
1380

1381
                case ContractSettled:
×
1382
                        return nil, ErrInvoiceAlreadySettled
×
1383
                }
1384

1385
                return &InvoiceUpdateDesc{
3✔
1386
                        UpdateType: SettleHodlInvoiceUpdate,
3✔
1387
                        State: &InvoiceStateUpdateDesc{
3✔
1388
                                NewState: ContractSettled,
3✔
1389
                                Preimage: &preimage,
3✔
1390
                        },
3✔
1391
                }, nil
3✔
1392
        }
1393

1394
        hash := preimage.Hash()
3✔
1395
        invoiceRef := InvoiceRefByHash(hash)
3✔
1396

3✔
1397
        // AMP hold invoices are not supported so we set the setID to nil.
3✔
1398
        // For non-AMP invoices this parameter is ignored during the fetching
3✔
1399
        // of the database state.
3✔
1400
        setID := (*SetID)(nil)
3✔
1401

3✔
1402
        invoice, err := i.idb.UpdateInvoice(
3✔
1403
                ctx, invoiceRef, setID, updateInvoice,
3✔
1404
        )
3✔
1405
        if err != nil {
3✔
1406
                log.Errorf("SettleHodlInvoice with preimage %v: %v",
×
1407
                        preimage, err)
×
1408

×
1409
                return err
×
1410
        }
×
1411

1412
        log.Debugf("Invoice%v: settled with preimage %v", invoiceRef,
3✔
1413
                invoice.Terms.PaymentPreimage)
3✔
1414

3✔
1415
        // In the callback, we marked the invoice as settled. UpdateInvoice will
3✔
1416
        // have seen this and should have moved all htlcs that were accepted to
3✔
1417
        // the settled state. In the loop below, we go through all of these and
3✔
1418
        // notify links and resolvers that are waiting for resolution. Any htlcs
3✔
1419
        // that were already settled before, will be notified again. This isn't
3✔
1420
        // necessary but doesn't hurt either.
3✔
1421
        for key, htlc := range invoice.Htlcs {
6✔
1422
                if htlc.State != HtlcStateSettled {
3✔
1423
                        continue
×
1424
                }
1425

1426
                resolution := NewSettleResolution(
3✔
1427
                        preimage, key, int32(htlc.AcceptHeight), ResultSettled,
3✔
1428
                )
3✔
1429

3✔
1430
                i.notifyHodlSubscribers(resolution)
3✔
1431
        }
1432
        i.notifyClients(hash, invoice, nil)
3✔
1433

3✔
1434
        return nil
3✔
1435
}
1436

1437
// CancelInvoice attempts to cancel the invoice corresponding to the passed
1438
// payment hash.
1439
func (i *InvoiceRegistry) CancelInvoice(ctx context.Context,
1440
        payHash lntypes.Hash) error {
3✔
1441

3✔
1442
        return i.cancelInvoiceImpl(ctx, payHash, true)
3✔
1443
}
3✔
1444

1445
// shouldCancel examines the state of an invoice and whether we want to
1446
// cancel already accepted invoices, taking our force cancel boolean into
1447
// account. This is pulled out into its own function so that tests that mock
1448
// cancelInvoiceImpl can reuse this logic.
1449
func shouldCancel(state ContractState, cancelAccepted bool) bool {
3✔
1450
        if state != ContractAccepted {
6✔
1451
                return true
3✔
1452
        }
3✔
1453

1454
        // If the invoice is accepted, we should only cancel if we want to
1455
        // force cancellation of accepted invoices.
1456
        return cancelAccepted
3✔
1457
}
1458

1459
// cancelInvoice attempts to cancel the invoice corresponding to the passed
1460
// payment hash. Accepted invoices will only be canceled if explicitly
1461
// requested to do so. It notifies subscribing links and resolvers that
1462
// the associated htlcs were canceled if they change state.
1463
func (i *InvoiceRegistry) cancelInvoiceImpl(ctx context.Context,
1464
        payHash lntypes.Hash, cancelAccepted bool) error {
3✔
1465

3✔
1466
        i.Lock()
3✔
1467
        defer i.Unlock()
3✔
1468

3✔
1469
        ref := InvoiceRefByHash(payHash)
3✔
1470
        log.Debugf("Invoice%v: canceling invoice", ref)
3✔
1471

3✔
1472
        updateInvoice := func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
6✔
1473
                if !shouldCancel(invoice.State, cancelAccepted) {
3✔
1474
                        return nil, nil
×
1475
                }
×
1476

1477
                // Move invoice to the canceled state. Rely on validation in
1478
                // channeldb to return an error if the invoice is already
1479
                // settled or canceled.
1480
                return &InvoiceUpdateDesc{
3✔
1481
                        UpdateType: CancelInvoiceUpdate,
3✔
1482
                        State: &InvoiceStateUpdateDesc{
3✔
1483
                                NewState: ContractCanceled,
3✔
1484
                        },
3✔
1485
                }, nil
3✔
1486
        }
1487

1488
        // If it's an AMP invoice we need to fetch all AMP HTLCs here so that
1489
        // we can cancel all of HTLCs which are in the accepted state across
1490
        // different setIDs.
1491
        setID := (*SetID)(nil)
3✔
1492
        invoiceRef := InvoiceRefByHash(payHash)
3✔
1493
        invoice, err := i.idb.UpdateInvoice(
3✔
1494
                ctx, invoiceRef, setID, updateInvoice,
3✔
1495
        )
3✔
1496

3✔
1497
        // Implement idempotency by returning success if the invoice was already
3✔
1498
        // canceled.
3✔
1499
        if errors.Is(err, ErrInvoiceAlreadyCanceled) {
3✔
1500
                log.Debugf("Invoice%v: already canceled", ref)
×
1501
                return nil
×
1502
        }
×
1503
        if err != nil {
6✔
1504
                return err
3✔
1505
        }
3✔
1506

1507
        // Return without cancellation if the invoice state is ContractAccepted.
1508
        if invoice.State == ContractAccepted {
3✔
1509
                log.Debugf("Invoice%v: remains accepted as cancel wasn't"+
×
1510
                        "explicitly requested.", ref)
×
1511
                return nil
×
1512
        }
×
1513

1514
        log.Debugf("Invoice%v: canceled", ref)
3✔
1515

3✔
1516
        // In the callback, some htlcs may have been moved to the canceled
3✔
1517
        // state. We now go through all of these and notify links and resolvers
3✔
1518
        // that are waiting for resolution. Any htlcs that were already canceled
3✔
1519
        // before, will be notified again. This isn't necessary but doesn't hurt
3✔
1520
        // either.
3✔
1521
        // For AMP invoices we fetched all AMP HTLCs for all sub AMP invoices
3✔
1522
        // here so we can clean up all of them.
3✔
1523
        for key, htlc := range invoice.Htlcs {
6✔
1524
                if htlc.State != HtlcStateCanceled {
3✔
1525
                        continue
×
1526
                }
1527

1528
                i.notifyHodlSubscribers(
3✔
1529
                        NewFailResolution(
3✔
1530
                                key, int32(htlc.AcceptHeight), ResultCanceled,
3✔
1531
                        ),
3✔
1532
                )
3✔
1533
        }
1534

1535
        i.notifyClients(payHash, invoice, nil)
3✔
1536

3✔
1537
        // Attempt to also delete the invoice if requested through the registry
3✔
1538
        // config.
3✔
1539
        if i.cfg.GcCanceledInvoicesOnTheFly {
3✔
1540
                // Assemble the delete reference and attempt to delete through
×
1541
                // the invocice from the DB.
×
1542
                deleteRef := InvoiceDeleteRef{
×
1543
                        PayHash:     payHash,
×
1544
                        AddIndex:    invoice.AddIndex,
×
1545
                        SettleIndex: invoice.SettleIndex,
×
1546
                }
×
1547
                if invoice.Terms.PaymentAddr != BlankPayAddr {
×
1548
                        deleteRef.PayAddr = &invoice.Terms.PaymentAddr
×
1549
                }
×
1550

1551
                err = i.idb.DeleteInvoice(ctx, []InvoiceDeleteRef{deleteRef})
×
1552
                // If by any chance deletion failed, then log it instead of
×
1553
                // returning the error, as the invoice itself has already been
×
1554
                // canceled.
×
1555
                if err != nil {
×
1556
                        log.Warnf("Invoice %v could not be deleted: %v", ref,
×
1557
                                err)
×
1558
                }
×
1559
        }
1560

1561
        return nil
3✔
1562
}
1563

1564
// notifyClients notifies all currently registered invoice notification clients
1565
// of a newly added/settled invoice.
1566
func (i *InvoiceRegistry) notifyClients(hash lntypes.Hash,
1567
        invoice *Invoice, setID *[32]byte) {
3✔
1568

3✔
1569
        event := &invoiceEvent{
3✔
1570
                invoice: invoice,
3✔
1571
                hash:    hash,
3✔
1572
                setID:   setID,
3✔
1573
        }
3✔
1574

3✔
1575
        select {
3✔
1576
        case i.invoiceEvents <- event:
3✔
1577
        case <-i.quit:
×
1578
        }
1579
}
1580

1581
// invoiceSubscriptionKit defines that are common to both all invoice
1582
// subscribers and single invoice subscribers.
1583
type invoiceSubscriptionKit struct {
1584
        id uint32 // nolint:structcheck
1585

1586
        // quit is a chan mouted to InvoiceRegistry that signals a shutdown.
1587
        quit chan struct{}
1588

1589
        ntfnQueue *queue.ConcurrentQueue
1590

1591
        canceled   uint32 // To be used atomically.
1592
        cancelChan chan struct{}
1593

1594
        // backlogDelivered is closed when the backlog events have been
1595
        // delivered.
1596
        backlogDelivered chan struct{}
1597
}
1598

1599
// InvoiceSubscription represents an intent to receive updates for newly added
1600
// or settled invoices. For each newly added invoice, a copy of the invoice
1601
// will be sent over the NewInvoices channel. Similarly, for each newly settled
1602
// invoice, a copy of the invoice will be sent over the SettledInvoices
1603
// channel.
1604
type InvoiceSubscription struct {
1605
        invoiceSubscriptionKit
1606

1607
        // NewInvoices is a channel that we'll use to send all newly created
1608
        // invoices with an invoice index greater than the specified
1609
        // StartingInvoiceIndex field.
1610
        NewInvoices chan *Invoice
1611

1612
        // SettledInvoices is a channel that we'll use to send all settled
1613
        // invoices with an invoices index greater than the specified
1614
        // StartingInvoiceIndex field.
1615
        SettledInvoices chan *Invoice
1616

1617
        // addIndex is the highest add index the caller knows of. We'll use
1618
        // this information to send out an event backlog to the notifications
1619
        // subscriber. Any new add events with an index greater than this will
1620
        // be dispatched before any new notifications are sent out.
1621
        addIndex uint64
1622

1623
        // settleIndex is the highest settle index the caller knows of. We'll
1624
        // use this information to send out an event backlog to the
1625
        // notifications subscriber. Any new settle events with an index
1626
        // greater than this will be dispatched before any new notifications
1627
        // are sent out.
1628
        settleIndex uint64
1629
}
1630

1631
// SingleInvoiceSubscription represents an intent to receive updates for a
1632
// specific invoice.
1633
type SingleInvoiceSubscription struct {
1634
        invoiceSubscriptionKit
1635

1636
        invoiceRef InvoiceRef
1637

1638
        // Updates is a channel that we'll use to send all invoice events for
1639
        // the invoice that is subscribed to.
1640
        Updates chan *Invoice
1641
}
1642

1643
// PayHash returns the optional payment hash of the target invoice.
1644
//
1645
// TODO(positiveblue): This method is only supposed to be used in tests. It will
1646
// be deleted as soon as invoiceregistery_test is in the same module.
1647
func (s *SingleInvoiceSubscription) PayHash() *lntypes.Hash {
×
1648
        return s.invoiceRef.PayHash()
×
1649
}
×
1650

1651
// Cancel unregisters the InvoiceSubscription, freeing any previously allocated
1652
// resources.
1653
func (i *invoiceSubscriptionKit) Cancel() {
3✔
1654
        if !atomic.CompareAndSwapUint32(&i.canceled, 0, 1) {
3✔
1655
                return
×
1656
        }
×
1657

1658
        i.ntfnQueue.Stop()
3✔
1659
        close(i.cancelChan)
3✔
1660
}
1661

1662
func (i *invoiceSubscriptionKit) notify(event *invoiceEvent) error {
3✔
1663
        select {
3✔
1664
        case i.ntfnQueue.ChanIn() <- event:
3✔
1665

UNCOV
1666
        case <-i.cancelChan:
×
UNCOV
1667
                // This can only be triggered by delivery of non-backlog
×
UNCOV
1668
                // events.
×
UNCOV
1669
                return ErrShuttingDown
×
1670
        case <-i.quit:
×
1671
                return ErrShuttingDown
×
1672
        }
1673

1674
        return nil
3✔
1675
}
1676

1677
// SubscribeNotifications returns an InvoiceSubscription which allows the
1678
// caller to receive async notifications when any invoices are settled or
1679
// added. The invoiceIndex parameter is a streaming "checkpoint". We'll start
1680
// by first sending out all new events with an invoice index _greater_ than
1681
// this value. Afterwards, we'll send out real-time notifications.
1682
func (i *InvoiceRegistry) SubscribeNotifications(ctx context.Context,
1683
        addIndex, settleIndex uint64) (*InvoiceSubscription, error) {
3✔
1684

3✔
1685
        client := &InvoiceSubscription{
3✔
1686
                NewInvoices:     make(chan *Invoice),
3✔
1687
                SettledInvoices: make(chan *Invoice),
3✔
1688
                addIndex:        addIndex,
3✔
1689
                settleIndex:     settleIndex,
3✔
1690
                invoiceSubscriptionKit: invoiceSubscriptionKit{
3✔
1691
                        quit:             i.quit,
3✔
1692
                        ntfnQueue:        queue.NewConcurrentQueue(20),
3✔
1693
                        cancelChan:       make(chan struct{}),
3✔
1694
                        backlogDelivered: make(chan struct{}),
3✔
1695
                },
3✔
1696
        }
3✔
1697
        client.ntfnQueue.Start()
3✔
1698

3✔
1699
        // This notifies other goroutines that the backlog phase is over.
3✔
1700
        defer close(client.backlogDelivered)
3✔
1701

3✔
1702
        // Always increment by 1 first, and our client ID will start with 1,
3✔
1703
        // not 0.
3✔
1704
        client.id = atomic.AddUint32(&i.nextClientID, 1)
3✔
1705

3✔
1706
        // Before we register this new invoice subscription, we'll launch a new
3✔
1707
        // goroutine that will proxy all notifications appended to the end of
3✔
1708
        // the concurrent queue to the two client-side channels the caller will
3✔
1709
        // feed off of.
3✔
1710
        i.wg.Add(1)
3✔
1711
        go func() {
6✔
1712
                defer i.wg.Done()
3✔
1713
                defer i.deleteClient(client.id)
3✔
1714

3✔
1715
                for {
6✔
1716
                        select {
3✔
1717
                        // A new invoice event has been sent by the
1718
                        // invoiceRegistry! We'll figure out if this is an add
1719
                        // event or a settle event, then dispatch the event to
1720
                        // the client.
1721
                        case ntfn := <-client.ntfnQueue.ChanOut():
3✔
1722
                                invoiceEvent := ntfn.(*invoiceEvent)
3✔
1723

3✔
1724
                                var targetChan chan *Invoice
3✔
1725
                                state := invoiceEvent.invoice.State
3✔
1726
                                switch {
3✔
1727
                                // AMP invoices never move to settled, but will
1728
                                // be sent with a set ID if an HTLC set is
1729
                                // being settled.
1730
                                case state == ContractOpen &&
1731
                                        invoiceEvent.setID != nil:
3✔
1732
                                        fallthrough
3✔
1733

1734
                                case state == ContractSettled:
3✔
1735
                                        targetChan = client.SettledInvoices
3✔
1736

1737
                                case state == ContractOpen:
3✔
1738
                                        targetChan = client.NewInvoices
3✔
1739

1740
                                default:
×
1741
                                        log.Errorf("unknown invoice state: %v",
×
1742
                                                state)
×
1743

×
1744
                                        continue
×
1745
                                }
1746

1747
                                select {
3✔
1748
                                case targetChan <- invoiceEvent.invoice:
3✔
1749

1750
                                case <-client.cancelChan:
×
1751
                                        return
×
1752

1753
                                case <-i.quit:
×
1754
                                        return
×
1755
                                }
1756

1757
                        case <-client.cancelChan:
3✔
1758
                                return
3✔
1759

1760
                        case <-i.quit:
×
1761
                                return
×
1762
                        }
1763
                }
1764
        }()
1765

1766
        i.notificationClientMux.Lock()
3✔
1767
        i.notificationClients[client.id] = client
3✔
1768
        i.notificationClientMux.Unlock()
3✔
1769

3✔
1770
        // Query the database to see if based on the provided addIndex and
3✔
1771
        // settledIndex we need to deliver any backlog notifications.
3✔
1772
        err := i.deliverBacklogEvents(ctx, client)
3✔
1773
        if err != nil {
3✔
1774
                return nil, err
×
1775
        }
×
1776

1777
        log.Infof("New invoice subscription client: id=%v", client.id)
3✔
1778

3✔
1779
        return client, nil
3✔
1780
}
1781

1782
// SubscribeSingleInvoice returns an SingleInvoiceSubscription which allows the
1783
// caller to receive async notifications for a specific invoice.
1784
func (i *InvoiceRegistry) SubscribeSingleInvoice(ctx context.Context,
1785
        hash lntypes.Hash) (*SingleInvoiceSubscription, error) {
3✔
1786

3✔
1787
        client := &SingleInvoiceSubscription{
3✔
1788
                Updates: make(chan *Invoice),
3✔
1789
                invoiceSubscriptionKit: invoiceSubscriptionKit{
3✔
1790
                        quit:             i.quit,
3✔
1791
                        ntfnQueue:        queue.NewConcurrentQueue(20),
3✔
1792
                        cancelChan:       make(chan struct{}),
3✔
1793
                        backlogDelivered: make(chan struct{}),
3✔
1794
                },
3✔
1795
                invoiceRef: InvoiceRefByHash(hash),
3✔
1796
        }
3✔
1797
        client.ntfnQueue.Start()
3✔
1798

3✔
1799
        // This notifies other goroutines that the backlog phase is done.
3✔
1800
        defer close(client.backlogDelivered)
3✔
1801

3✔
1802
        // Always increment by 1 first, and our client ID will start with 1,
3✔
1803
        // not 0.
3✔
1804
        client.id = atomic.AddUint32(&i.nextClientID, 1)
3✔
1805

3✔
1806
        // Before we register this new invoice subscription, we'll launch a new
3✔
1807
        // goroutine that will proxy all notifications appended to the end of
3✔
1808
        // the concurrent queue to the two client-side channels the caller will
3✔
1809
        // feed off of.
3✔
1810
        i.wg.Add(1)
3✔
1811
        go func() {
6✔
1812
                defer i.wg.Done()
3✔
1813
                defer i.deleteClient(client.id)
3✔
1814

3✔
1815
                for {
6✔
1816
                        select {
3✔
1817
                        // A new invoice event has been sent by the
1818
                        // invoiceRegistry. We will dispatch the event to the
1819
                        // client.
1820
                        case ntfn := <-client.ntfnQueue.ChanOut():
3✔
1821
                                invoiceEvent := ntfn.(*invoiceEvent)
3✔
1822

3✔
1823
                                select {
3✔
1824
                                case client.Updates <- invoiceEvent.invoice:
3✔
1825

1826
                                case <-client.cancelChan:
×
1827
                                        return
×
1828

1829
                                case <-i.quit:
×
1830
                                        return
×
1831
                                }
1832

1833
                        case <-client.cancelChan:
3✔
1834
                                return
3✔
1835

1836
                        case <-i.quit:
×
1837
                                return
×
1838
                        }
1839
                }
1840
        }()
1841

1842
        i.notificationClientMux.Lock()
3✔
1843
        i.singleNotificationClients[client.id] = client
3✔
1844
        i.notificationClientMux.Unlock()
3✔
1845

3✔
1846
        err := i.deliverSingleBacklogEvents(ctx, client)
3✔
1847
        if err != nil {
3✔
1848
                return nil, err
×
1849
        }
×
1850

1851
        log.Infof("New single invoice subscription client: id=%v, ref=%v",
3✔
1852
                client.id, client.invoiceRef)
3✔
1853

3✔
1854
        return client, nil
3✔
1855
}
1856

1857
// notifyHodlSubscribers sends out the htlc resolution to all current
1858
// subscribers.
1859
func (i *InvoiceRegistry) notifyHodlSubscribers(htlcResolution HtlcResolution) {
3✔
1860
        i.hodlSubscriptionsMux.Lock()
3✔
1861
        defer i.hodlSubscriptionsMux.Unlock()
3✔
1862

3✔
1863
        subscribers, ok := i.hodlSubscriptions[htlcResolution.CircuitKey()]
3✔
1864
        if !ok {
6✔
1865
                return
3✔
1866
        }
3✔
1867

1868
        // Notify all interested subscribers and remove subscription from both
1869
        // maps. The subscription can be removed as there only ever will be a
1870
        // single resolution for each hash.
1871
        for subscriber := range subscribers {
6✔
1872
                select {
3✔
1873
                case subscriber <- htlcResolution:
3✔
1874
                case <-i.quit:
×
1875
                        return
×
1876
                }
1877

1878
                delete(
3✔
1879
                        i.hodlReverseSubscriptions[subscriber],
3✔
1880
                        htlcResolution.CircuitKey(),
3✔
1881
                )
3✔
1882
        }
1883

1884
        delete(i.hodlSubscriptions, htlcResolution.CircuitKey())
3✔
1885
}
1886

1887
// hodlSubscribe adds a new invoice subscription.
1888
func (i *InvoiceRegistry) hodlSubscribe(subscriber chan<- interface{},
1889
        circuitKey CircuitKey) {
3✔
1890

3✔
1891
        i.hodlSubscriptionsMux.Lock()
3✔
1892
        defer i.hodlSubscriptionsMux.Unlock()
3✔
1893

3✔
1894
        log.Debugf("Hodl subscribe for %v", circuitKey)
3✔
1895

3✔
1896
        subscriptions, ok := i.hodlSubscriptions[circuitKey]
3✔
1897
        if !ok {
6✔
1898
                subscriptions = make(map[chan<- interface{}]struct{})
3✔
1899
                i.hodlSubscriptions[circuitKey] = subscriptions
3✔
1900
        }
3✔
1901
        subscriptions[subscriber] = struct{}{}
3✔
1902

3✔
1903
        reverseSubscriptions, ok := i.hodlReverseSubscriptions[subscriber]
3✔
1904
        if !ok {
6✔
1905
                reverseSubscriptions = make(map[CircuitKey]struct{})
3✔
1906
                i.hodlReverseSubscriptions[subscriber] = reverseSubscriptions
3✔
1907
        }
3✔
1908
        reverseSubscriptions[circuitKey] = struct{}{}
3✔
1909
}
1910

1911
// HodlUnsubscribeAll cancels the subscription.
1912
func (i *InvoiceRegistry) HodlUnsubscribeAll(subscriber chan<- interface{}) {
3✔
1913
        i.hodlSubscriptionsMux.Lock()
3✔
1914
        defer i.hodlSubscriptionsMux.Unlock()
3✔
1915

3✔
1916
        hashes := i.hodlReverseSubscriptions[subscriber]
3✔
1917
        for hash := range hashes {
6✔
1918
                delete(i.hodlSubscriptions[hash], subscriber)
3✔
1919
        }
3✔
1920

1921
        delete(i.hodlReverseSubscriptions, subscriber)
3✔
1922
}
1923

1924
// copySingleClients copies i.SingleInvoiceSubscription inside a lock. This is
1925
// useful when we need to iterate the map to send notifications.
1926
func (i *InvoiceRegistry) copySingleClients() map[uint32]*SingleInvoiceSubscription { //nolint:ll
3✔
1927
        i.notificationClientMux.RLock()
3✔
1928
        defer i.notificationClientMux.RUnlock()
3✔
1929

3✔
1930
        clients := make(map[uint32]*SingleInvoiceSubscription)
3✔
1931
        for k, v := range i.singleNotificationClients {
6✔
1932
                clients[k] = v
3✔
1933
        }
3✔
1934
        return clients
3✔
1935
}
1936

1937
// copyClients copies i.notificationClients inside a lock. This is useful when
1938
// we need to iterate the map to send notifications.
1939
func (i *InvoiceRegistry) copyClients() map[uint32]*InvoiceSubscription {
3✔
1940
        i.notificationClientMux.RLock()
3✔
1941
        defer i.notificationClientMux.RUnlock()
3✔
1942

3✔
1943
        clients := make(map[uint32]*InvoiceSubscription)
3✔
1944
        for k, v := range i.notificationClients {
6✔
1945
                clients[k] = v
3✔
1946
        }
3✔
1947
        return clients
3✔
1948
}
1949

1950
// deleteClient removes a client by its ID inside a lock. Noop if the client is
1951
// not found.
1952
func (i *InvoiceRegistry) deleteClient(clientID uint32) {
3✔
1953
        i.notificationClientMux.Lock()
3✔
1954
        defer i.notificationClientMux.Unlock()
3✔
1955

3✔
1956
        log.Infof("Cancelling invoice subscription for client=%v", clientID)
3✔
1957
        delete(i.notificationClients, clientID)
3✔
1958
        delete(i.singleNotificationClients, clientID)
3✔
1959
}
3✔
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