2018-12-20 13:57:44 +03:00
|
|
|
package invoices
|
2016-07-13 03:14:07 +03:00
|
|
|
|
|
|
|
import (
|
2019-04-16 13:11:20 +03:00
|
|
|
"errors"
|
2019-09-03 13:23:39 +03:00
|
|
|
"fmt"
|
2016-07-13 03:14:07 +03:00
|
|
|
"sync"
|
2018-04-25 07:07:30 +03:00
|
|
|
"sync/atomic"
|
2019-09-03 13:23:39 +03:00
|
|
|
"time"
|
2016-07-13 03:14:07 +03:00
|
|
|
|
2016-09-19 21:55:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-11-25 13:46:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2019-01-15 13:31:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2017-08-22 09:25:41 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2018-10-12 18:08:14 +03:00
|
|
|
"github.com/lightningnetwork/lnd/queue"
|
2019-12-05 14:55:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2016-07-13 03:14:07 +03:00
|
|
|
)
|
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
var (
|
|
|
|
// ErrInvoiceExpiryTooSoon is returned when an invoice is attempted to be
|
|
|
|
// accepted or settled with not enough blocks remaining.
|
|
|
|
ErrInvoiceExpiryTooSoon = errors.New("invoice expiry too soon")
|
|
|
|
|
|
|
|
// ErrInvoiceAmountTooLow is returned when an invoice is attempted to be
|
|
|
|
// accepted or settled with an amount that is too low.
|
|
|
|
ErrInvoiceAmountTooLow = errors.New("paid amount less than invoice amount")
|
2019-08-13 19:57:27 +03:00
|
|
|
|
|
|
|
// ErrShuttingDown is returned when an operation failed because the
|
|
|
|
// invoice registry is shutting down.
|
|
|
|
ErrShuttingDown = errors.New("invoice registry shutting down")
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
const (
|
|
|
|
// DefaultHtlcHoldDuration defines the default for how long mpp htlcs
|
|
|
|
// are held while waiting for the other set members to arrive.
|
|
|
|
DefaultHtlcHoldDuration = 120 * time.Second
|
|
|
|
)
|
|
|
|
|
2019-12-04 20:47:53 +03:00
|
|
|
// RegistryConfig contains the configuration parameters for invoice registry.
|
|
|
|
type RegistryConfig struct {
|
|
|
|
// FinalCltvRejectDelta defines the number of blocks before the expiry
|
|
|
|
// of the htlc where we no longer settle it as an exit hop and instead
|
|
|
|
// cancel it back. Normally this value should be lower than the cltv
|
|
|
|
// expiry of any invoice we create and the code effectuating this should
|
|
|
|
// not be hit.
|
|
|
|
FinalCltvRejectDelta int32
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
// HtlcHoldDuration defines for how long mpp htlcs are held while
|
|
|
|
// waiting for the other set members to arrive.
|
|
|
|
HtlcHoldDuration time.Duration
|
|
|
|
|
2019-11-25 13:46:29 +03:00
|
|
|
// Clock holds the clock implementation that is used to provide
|
|
|
|
// Now() and TickAfter() and is useful to stub out the clock functions
|
|
|
|
// during testing.
|
|
|
|
Clock clock.Clock
|
2019-12-05 14:55:17 +03:00
|
|
|
|
|
|
|
// AcceptKeySend indicates whether we want to accept spontaneous key
|
|
|
|
// send payments.
|
|
|
|
AcceptKeySend bool
|
2020-04-09 11:42:10 +03:00
|
|
|
|
2020-07-29 00:57:51 +03:00
|
|
|
// GcCanceledInvoicesOnStartup if set, we'll attempt to garbage collect
|
|
|
|
// all canceled invoices upon start.
|
|
|
|
GcCanceledInvoicesOnStartup bool
|
|
|
|
|
2020-08-04 19:28:15 +03:00
|
|
|
// GcCanceledInvoicesOnTheFly if set, we'll garbage collect all newly
|
|
|
|
// canceled invoices on the fly.
|
|
|
|
GcCanceledInvoicesOnTheFly bool
|
|
|
|
|
2020-04-09 11:42:10 +03:00
|
|
|
// KeysendHoldTime indicates for how long we want to accept and hold
|
|
|
|
// spontaneous keysend payments.
|
|
|
|
KeysendHoldTime time.Duration
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// htlcReleaseEvent describes an htlc auto-release event. It is used to release
|
|
|
|
// mpp htlcs for which the complete set didn't arrive in time.
|
|
|
|
type htlcReleaseEvent struct {
|
2020-05-22 01:37:10 +03:00
|
|
|
// invoiceRef identifiers the invoice this htlc belongs to.
|
|
|
|
invoiceRef channeldb.InvoiceRef
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
// key is the circuit key of the htlc to release.
|
|
|
|
key channeldb.CircuitKey
|
|
|
|
|
|
|
|
// releaseTime is the time at which to release the htlc.
|
|
|
|
releaseTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less is used to order PriorityQueueItem's by their release time such that
|
|
|
|
// items with the older release time are at the top of the queue.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the queue.PriorityQueueItem interface.
|
|
|
|
func (r *htlcReleaseEvent) Less(other queue.PriorityQueueItem) bool {
|
|
|
|
return r.releaseTime.Before(other.(*htlcReleaseEvent).releaseTime)
|
2019-12-04 20:47:53 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
// InvoiceRegistry is a central registry of all the outstanding invoices
|
2016-07-13 03:14:07 +03:00
|
|
|
// created by the daemon. The registry is a thin wrapper around a map in order
|
|
|
|
// to ensure that all updates/reads are thread safe.
|
2018-12-20 13:57:44 +03:00
|
|
|
type InvoiceRegistry struct {
|
2016-07-13 03:14:07 +03:00
|
|
|
sync.RWMutex
|
2016-09-19 21:55:02 +03:00
|
|
|
|
|
|
|
cdb *channeldb.DB
|
|
|
|
|
2019-12-04 20:47:53 +03:00
|
|
|
// cfg contains the registry's configuration parameters.
|
|
|
|
cfg *RegistryConfig
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
clientMtx sync.Mutex
|
|
|
|
nextClientID uint32
|
|
|
|
notificationClients map[uint32]*InvoiceSubscription
|
|
|
|
singleNotificationClients map[uint32]*SingleInvoiceSubscription
|
2016-10-15 05:47:10 +03:00
|
|
|
|
2019-08-09 13:22:53 +03:00
|
|
|
newSubscriptions chan *InvoiceSubscription
|
|
|
|
subscriptionCancels chan uint32
|
|
|
|
|
|
|
|
// invoiceEvents is a single channel over which both invoice updates and
|
|
|
|
// new single invoice subscriptions are carried.
|
|
|
|
invoiceEvents chan interface{}
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2019-08-14 22:11:34 +03:00
|
|
|
// subscriptions is a map from a circuit key to a list of subscribers.
|
2019-02-11 14:01:05 +03:00
|
|
|
// It is used for efficient notification of links.
|
2019-08-14 22:11:34 +03:00
|
|
|
hodlSubscriptions map[channeldb.CircuitKey]map[chan<- interface{}]struct{}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-08-14 22:11:34 +03:00
|
|
|
// reverseSubscriptions tracks circuit keys subscribed to per
|
|
|
|
// subscriber. This is used to unsubscribe from all hashes efficiently.
|
|
|
|
hodlReverseSubscriptions map[chan<- interface{}]map[channeldb.CircuitKey]struct{}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// htlcAutoReleaseChan contains the new htlcs that need to be
|
|
|
|
// auto-released.
|
|
|
|
htlcAutoReleaseChan chan *htlcReleaseEvent
|
|
|
|
|
2019-12-09 19:50:11 +03:00
|
|
|
expiryWatcher *InvoiceExpiryWatcher
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
// NewRegistry creates a new invoice registry. The invoice registry
|
2016-09-19 21:55:02 +03:00
|
|
|
// wraps the persistent on-disk invoice storage with an additional in-memory
|
2017-01-13 08:01:50 +03:00
|
|
|
// layer. The in-memory layer is in place such that debug invoices can be added
|
2016-09-19 21:55:02 +03:00
|
|
|
// which are volatile yet available system wide within the daemon.
|
2019-12-09 19:50:11 +03:00
|
|
|
func NewRegistry(cdb *channeldb.DB, expiryWatcher *InvoiceExpiryWatcher,
|
|
|
|
cfg *RegistryConfig) *InvoiceRegistry {
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
return &InvoiceRegistry{
|
2018-12-20 20:54:11 +03:00
|
|
|
cdb: cdb,
|
|
|
|
notificationClients: make(map[uint32]*InvoiceSubscription),
|
|
|
|
singleNotificationClients: make(map[uint32]*SingleInvoiceSubscription),
|
|
|
|
newSubscriptions: make(chan *InvoiceSubscription),
|
|
|
|
subscriptionCancels: make(chan uint32),
|
2019-08-09 13:22:53 +03:00
|
|
|
invoiceEvents: make(chan interface{}, 100),
|
2019-08-14 22:11:34 +03:00
|
|
|
hodlSubscriptions: make(map[channeldb.CircuitKey]map[chan<- interface{}]struct{}),
|
|
|
|
hodlReverseSubscriptions: make(map[chan<- interface{}]map[channeldb.CircuitKey]struct{}),
|
2019-12-04 20:47:53 +03:00
|
|
|
cfg: cfg,
|
2019-09-03 13:23:39 +03:00
|
|
|
htlcAutoReleaseChan: make(chan *htlcReleaseEvent),
|
2019-12-09 19:50:11 +03:00
|
|
|
expiryWatcher: expiryWatcher,
|
2018-12-20 20:54:11 +03:00
|
|
|
quit: make(chan struct{}),
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 22:22:23 +03:00
|
|
|
// scanInvoicesOnStart will scan all invoices on start and add active invoices
|
2020-07-29 00:39:11 +03:00
|
|
|
// to the invoice expirt watcher while also attempting to delete all canceled
|
|
|
|
// invoices.
|
2020-07-28 22:22:23 +03:00
|
|
|
func (i *InvoiceRegistry) scanInvoicesOnStart() error {
|
2020-07-29 00:39:11 +03:00
|
|
|
var (
|
2020-10-21 14:21:35 +03:00
|
|
|
pending []*invoiceExpiry
|
2020-07-29 00:39:11 +03:00
|
|
|
removable []channeldb.InvoiceDeleteRef
|
|
|
|
)
|
2020-07-28 22:22:23 +03:00
|
|
|
|
|
|
|
reset := func() {
|
|
|
|
// Zero out our results on start and if the scan is ever run
|
|
|
|
// more than once. This latter case can happen if the kvdb
|
|
|
|
// layer needs to retry the View transaction underneath (eg.
|
|
|
|
// using the etcd driver, where all transactions are allowed
|
|
|
|
// to retry for serializability).
|
2020-10-21 14:21:35 +03:00
|
|
|
pending = nil
|
2020-07-29 00:39:11 +03:00
|
|
|
removable = make([]channeldb.InvoiceDeleteRef, 0)
|
2020-07-28 22:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
scanFunc := func(
|
|
|
|
paymentHash lntypes.Hash, invoice *channeldb.Invoice) error {
|
|
|
|
|
|
|
|
if invoice.IsPending() {
|
2020-10-21 14:21:35 +03:00
|
|
|
expiryRef := makeInvoiceExpiry(paymentHash, invoice)
|
|
|
|
if expiryRef != nil {
|
|
|
|
pending = append(pending, expiryRef)
|
|
|
|
}
|
2020-07-29 00:57:51 +03:00
|
|
|
} else if i.cfg.GcCanceledInvoicesOnStartup &&
|
|
|
|
invoice.State == channeldb.ContractCanceled {
|
|
|
|
|
2020-07-29 00:39:11 +03:00
|
|
|
// Consider invoice for removal if it is already
|
|
|
|
// canceled. Invoices that are expired but not yet
|
|
|
|
// canceled, will be queued up for cancellation after
|
|
|
|
// startup and will be deleted afterwards.
|
|
|
|
ref := channeldb.InvoiceDeleteRef{
|
|
|
|
PayHash: paymentHash,
|
|
|
|
AddIndex: invoice.AddIndex,
|
|
|
|
SettleIndex: invoice.SettleIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoice.Terms.PaymentAddr != channeldb.BlankPayAddr {
|
|
|
|
ref.PayAddr = &invoice.Terms.PaymentAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
removable = append(removable, ref)
|
2020-07-28 22:22:23 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.cdb.ScanInvoices(scanFunc, reset)
|
|
|
|
if err != nil {
|
2019-12-09 19:50:11 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:24:22 +03:00
|
|
|
log.Debugf("Adding %d pending invoices to the expiry watcher",
|
2020-07-28 22:22:23 +03:00
|
|
|
len(pending))
|
2020-10-21 14:21:35 +03:00
|
|
|
i.expiryWatcher.AddInvoices(pending...)
|
2020-07-28 22:22:23 +03:00
|
|
|
|
2020-07-29 00:39:11 +03:00
|
|
|
if err := i.cdb.DeleteInvoice(removable); err != nil {
|
|
|
|
log.Warnf("Deleting old invoices failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:50:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
// Start starts the registry and all goroutines it needs to carry out its task.
|
2018-12-20 13:57:44 +03:00
|
|
|
func (i *InvoiceRegistry) Start() error {
|
2019-12-09 19:50:11 +03:00
|
|
|
// Start InvoiceExpiryWatcher and prepopulate it with existing active
|
|
|
|
// invoices.
|
2020-04-09 11:42:10 +03:00
|
|
|
err := i.expiryWatcher.Start(i.cancelInvoiceImpl)
|
2019-12-09 19:50:11 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2019-12-09 19:50:11 +03:00
|
|
|
i.wg.Add(1)
|
2019-09-03 13:23:39 +03:00
|
|
|
go i.invoiceEventLoop()
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2020-07-28 22:22:23 +03:00
|
|
|
// Now scan all pending and removable invoices to the expiry watcher or
|
|
|
|
// delete them.
|
|
|
|
err = i.scanInvoicesOnStart()
|
2019-12-09 19:50:11 +03:00
|
|
|
if err != nil {
|
|
|
|
i.Stop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals the registry for a graceful shutdown.
|
2018-12-20 13:57:44 +03:00
|
|
|
func (i *InvoiceRegistry) Stop() {
|
2019-12-09 19:50:11 +03:00
|
|
|
i.expiryWatcher.Stop()
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
close(i.quit)
|
|
|
|
|
|
|
|
i.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoiceEvent represents a new event that has modified on invoice on disk.
|
|
|
|
// Only two event types are currently supported: newly created invoices, and
|
|
|
|
// instance where invoices are settled.
|
|
|
|
type invoiceEvent struct {
|
2018-12-20 20:54:11 +03:00
|
|
|
hash lntypes.Hash
|
2018-04-25 07:07:30 +03:00
|
|
|
invoice *channeldb.Invoice
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// tickAt returns a channel that ticks at the specified time. If the time has
|
|
|
|
// already passed, it will tick immediately.
|
|
|
|
func (i *InvoiceRegistry) tickAt(t time.Time) <-chan time.Time {
|
2019-11-25 13:46:29 +03:00
|
|
|
now := i.cfg.Clock.Now()
|
|
|
|
return i.cfg.Clock.TickAfter(t.Sub(now))
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// invoiceEventLoop is the dedicated goroutine responsible for accepting
|
2018-04-25 07:07:30 +03:00
|
|
|
// new notification subscriptions, cancelling old subscriptions, and
|
|
|
|
// dispatching new invoice events.
|
2019-09-03 13:23:39 +03:00
|
|
|
func (i *InvoiceRegistry) invoiceEventLoop() {
|
2018-04-25 07:07:30 +03:00
|
|
|
defer i.wg.Done()
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// Set up a heap for htlc auto-releases.
|
|
|
|
autoReleaseHeap := &queue.PriorityQueue{}
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
for {
|
2019-09-03 13:23:39 +03:00
|
|
|
// If there is something to release, set up a release tick
|
|
|
|
// channel.
|
|
|
|
var nextReleaseTick <-chan time.Time
|
|
|
|
if autoReleaseHeap.Len() > 0 {
|
|
|
|
head := autoReleaseHeap.Top().(*htlcReleaseEvent)
|
|
|
|
nextReleaseTick = i.tickAt(head.releaseTime)
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
select {
|
2018-12-20 20:54:11 +03:00
|
|
|
// A new invoice subscription for all invoices has just arrived!
|
|
|
|
// We'll query for any backlog notifications, then add it to the
|
|
|
|
// set of clients.
|
2018-04-25 07:07:30 +03:00
|
|
|
case newClient := <-i.newSubscriptions:
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Infof("New invoice subscription "+
|
2018-04-25 07:07:30 +03:00
|
|
|
"client: id=%v", newClient.id)
|
|
|
|
|
|
|
|
// With the backlog notifications delivered (if any),
|
|
|
|
// we'll add this to our active subscriptions and
|
|
|
|
// continue.
|
|
|
|
i.notificationClients[newClient.id] = newClient
|
|
|
|
|
|
|
|
// A client no longer wishes to receive invoice notifications.
|
|
|
|
// So we'll remove them from the set of active clients.
|
|
|
|
case clientID := <-i.subscriptionCancels:
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Infof("Cancelling invoice subscription for "+
|
2018-04-25 07:07:30 +03:00
|
|
|
"client=%v", clientID)
|
|
|
|
|
|
|
|
delete(i.notificationClients, clientID)
|
2018-12-20 20:54:11 +03:00
|
|
|
delete(i.singleNotificationClients, clientID)
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2019-08-09 13:22:53 +03:00
|
|
|
// An invoice event has come in. This can either be an update to
|
|
|
|
// an invoice or a new single invoice subscriber. Both type of
|
|
|
|
// events are passed in via the same channel, to make sure that
|
|
|
|
// subscribers get a consistent view of the event sequence.
|
2018-04-25 07:07:30 +03:00
|
|
|
case event := <-i.invoiceEvents:
|
2019-08-09 13:22:53 +03:00
|
|
|
switch e := event.(type) {
|
|
|
|
|
|
|
|
// A sub-systems has just modified the invoice state, so
|
|
|
|
// we'll dispatch notifications to all registered
|
|
|
|
// clients.
|
|
|
|
case *invoiceEvent:
|
|
|
|
// For backwards compatibility, do not notify
|
|
|
|
// all invoice subscribers of cancel and accept
|
|
|
|
// events.
|
2019-11-22 13:25:02 +03:00
|
|
|
state := e.invoice.State
|
2019-08-09 13:22:53 +03:00
|
|
|
if state != channeldb.ContractCanceled &&
|
|
|
|
state != channeldb.ContractAccepted {
|
|
|
|
|
|
|
|
i.dispatchToClients(e)
|
|
|
|
}
|
|
|
|
i.dispatchToSingleClients(e)
|
|
|
|
|
|
|
|
// A new single invoice subscription has arrived. Add it
|
|
|
|
// to the set of clients. It is important to do this in
|
|
|
|
// sequence with any other invoice events, because an
|
|
|
|
// initial invoice update has already been sent out to
|
|
|
|
// the subscriber.
|
|
|
|
case *SingleInvoiceSubscription:
|
|
|
|
log.Infof("New single invoice subscription "+
|
2020-05-22 01:37:10 +03:00
|
|
|
"client: id=%v, ref=%v", e.id,
|
|
|
|
e.invoiceRef)
|
2019-08-09 13:22:53 +03:00
|
|
|
|
|
|
|
i.singleNotificationClients[e.id] = e
|
2019-01-11 13:19:16 +03:00
|
|
|
}
|
2018-12-19 18:15:09 +03:00
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// A new htlc came in for auto-release.
|
|
|
|
case event := <-i.htlcAutoReleaseChan:
|
|
|
|
log.Debugf("Scheduling auto-release for htlc: "+
|
2020-05-22 01:37:10 +03:00
|
|
|
"ref=%v, key=%v at %v",
|
|
|
|
event.invoiceRef, event.key, event.releaseTime)
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
// We use an independent timer for every htlc rather
|
|
|
|
// than a set timer that is reset with every htlc coming
|
|
|
|
// in. Otherwise the sender could keep resetting the
|
|
|
|
// timer until the broadcast window is entered and our
|
|
|
|
// channel is force closed.
|
|
|
|
autoReleaseHeap.Push(event)
|
|
|
|
|
|
|
|
// The htlc at the top of the heap needs to be auto-released.
|
|
|
|
case <-nextReleaseTick:
|
|
|
|
event := autoReleaseHeap.Pop().(*htlcReleaseEvent)
|
|
|
|
err := i.cancelSingleHtlc(
|
2020-05-22 01:37:10 +03:00
|
|
|
event.invoiceRef, event.key, ResultMppTimeout,
|
2019-09-03 13:23:39 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("HTLC timer: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:15:09 +03:00
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
// dispatchToSingleClients passes the supplied event to all notification clients
|
|
|
|
// that subscribed to all the invoice this event applies to.
|
|
|
|
func (i *InvoiceRegistry) dispatchToSingleClients(event *invoiceEvent) {
|
|
|
|
// Dispatch to single invoice subscribers.
|
|
|
|
for _, client := range i.singleNotificationClients {
|
2021-03-25 05:48:32 +03:00
|
|
|
payHash := client.invoiceRef.PayHash()
|
|
|
|
if payHash == nil || *payHash != event.hash {
|
2018-12-20 20:54:11 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:44:27 +03:00
|
|
|
client.notify(event)
|
2018-12-20 20:54:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:15:09 +03:00
|
|
|
// dispatchToClients passes the supplied event to all notification clients that
|
|
|
|
// subscribed to all invoices. Add and settle indices are used to make sure that
|
|
|
|
// clients don't receive duplicate or unwanted events.
|
|
|
|
func (i *InvoiceRegistry) dispatchToClients(event *invoiceEvent) {
|
|
|
|
invoice := event.invoice
|
|
|
|
|
|
|
|
for clientID, client := range i.notificationClients {
|
|
|
|
// Before we dispatch this event, we'll check
|
|
|
|
// to ensure that this client hasn't already
|
|
|
|
// received this notification in order to
|
|
|
|
// ensure we don't duplicate any events.
|
|
|
|
|
|
|
|
// TODO(joostjager): Refactor switches.
|
2019-11-22 13:25:02 +03:00
|
|
|
state := event.invoice.State
|
2018-12-19 18:15:09 +03:00
|
|
|
switch {
|
|
|
|
// If we've already sent this settle event to
|
|
|
|
// the client, then we can skip this.
|
2019-08-09 12:41:34 +03:00
|
|
|
case state == channeldb.ContractSettled &&
|
2018-12-19 18:15:09 +03:00
|
|
|
client.settleIndex >= invoice.SettleIndex:
|
|
|
|
continue
|
|
|
|
|
|
|
|
// Similarly, if we've already sent this add to
|
|
|
|
// the client then we can skip this one.
|
2019-08-09 12:41:34 +03:00
|
|
|
case state == channeldb.ContractOpen &&
|
2018-12-19 18:15:09 +03:00
|
|
|
client.addIndex >= invoice.AddIndex:
|
|
|
|
continue
|
|
|
|
|
|
|
|
// These two states should never happen, but we
|
|
|
|
// log them just in case so we can detect this
|
|
|
|
// instance.
|
2019-08-09 12:41:34 +03:00
|
|
|
case state == channeldb.ContractOpen &&
|
2018-12-19 18:15:09 +03:00
|
|
|
client.addIndex+1 != invoice.AddIndex:
|
|
|
|
log.Warnf("client=%v for invoice "+
|
|
|
|
"notifications missed an update, "+
|
|
|
|
"add_index=%v, new add event index=%v",
|
|
|
|
clientID, client.addIndex,
|
|
|
|
invoice.AddIndex)
|
|
|
|
|
2019-08-09 12:41:34 +03:00
|
|
|
case state == channeldb.ContractSettled &&
|
2018-12-19 18:15:09 +03:00
|
|
|
client.settleIndex+1 != invoice.SettleIndex:
|
|
|
|
log.Warnf("client=%v for invoice "+
|
|
|
|
"notifications missed an update, "+
|
|
|
|
"settle_index=%v, new settle event index=%v",
|
|
|
|
clientID, client.settleIndex,
|
|
|
|
invoice.SettleIndex)
|
|
|
|
}
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2018-12-19 18:15:09 +03:00
|
|
|
select {
|
|
|
|
case client.ntfnQueue.ChanIn() <- &invoiceEvent{
|
|
|
|
invoice: invoice,
|
|
|
|
}:
|
2018-04-25 07:07:30 +03:00
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
2018-12-19 18:15:09 +03:00
|
|
|
|
|
|
|
// Each time we send a notification to a client, we'll record
|
|
|
|
// the latest add/settle index it has. We'll use this to ensure
|
|
|
|
// we don't send a notification twice, which can happen if a new
|
|
|
|
// event is added while we're catching up a new client.
|
2019-11-22 13:25:02 +03:00
|
|
|
switch event.invoice.State {
|
2018-12-19 18:15:09 +03:00
|
|
|
case channeldb.ContractSettled:
|
|
|
|
client.settleIndex = invoice.SettleIndex
|
|
|
|
case channeldb.ContractOpen:
|
|
|
|
client.addIndex = invoice.AddIndex
|
|
|
|
default:
|
2019-08-09 12:41:34 +03:00
|
|
|
log.Errorf("unexpected invoice state: %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
event.invoice.State)
|
2018-12-19 18:15:09 +03:00
|
|
|
}
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deliverBacklogEvents will attempts to query the invoice database for any
|
|
|
|
// notifications that the client has missed since it reconnected last.
|
2018-12-20 13:57:44 +03:00
|
|
|
func (i *InvoiceRegistry) deliverBacklogEvents(client *InvoiceSubscription) error {
|
2018-04-25 07:07:30 +03:00
|
|
|
addEvents, err := i.cdb.InvoicesAddedSince(client.addIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-20 20:54:11 +03:00
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
settleEvents, err := i.cdb.InvoicesSettledSince(client.settleIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have any to deliver, then we'll append them to the end of the
|
|
|
|
// notification queue in order to catch up the client before delivering
|
|
|
|
// any new notifications.
|
|
|
|
for _, addEvent := range addEvents {
|
2018-06-30 04:07:13 +03:00
|
|
|
// We re-bind the loop variable to ensure we don't hold onto
|
|
|
|
// the loop reference causing is to point to the same item.
|
|
|
|
addEvent := addEvent
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
select {
|
|
|
|
case client.ntfnQueue.ChanIn() <- &invoiceEvent{
|
2018-12-19 17:56:26 +03:00
|
|
|
invoice: &addEvent,
|
2018-04-25 07:07:30 +03:00
|
|
|
}:
|
|
|
|
case <-i.quit:
|
2019-08-13 19:57:27 +03:00
|
|
|
return ErrShuttingDown
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 20:54:11 +03:00
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
for _, settleEvent := range settleEvents {
|
2018-06-30 04:07:13 +03:00
|
|
|
// We re-bind the loop variable to ensure we don't hold onto
|
|
|
|
// the loop reference causing is to point to the same item.
|
|
|
|
settleEvent := settleEvent
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
select {
|
|
|
|
case client.ntfnQueue.ChanIn() <- &invoiceEvent{
|
2018-12-19 17:56:26 +03:00
|
|
|
invoice: &settleEvent,
|
2018-04-25 07:07:30 +03:00
|
|
|
}:
|
|
|
|
case <-i.quit:
|
2019-08-13 19:57:27 +03:00
|
|
|
return ErrShuttingDown
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
2018-04-25 07:07:30 +03:00
|
|
|
|
|
|
|
return nil
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
// deliverSingleBacklogEvents will attempt to query the invoice database to
|
|
|
|
// retrieve the current invoice state and deliver this to the subscriber. Single
|
|
|
|
// invoice subscribers will always receive the current state right after
|
|
|
|
// subscribing. Only in case the invoice does not yet exist, nothing is sent
|
|
|
|
// yet.
|
|
|
|
func (i *InvoiceRegistry) deliverSingleBacklogEvents(
|
|
|
|
client *SingleInvoiceSubscription) error {
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
invoice, err := i.cdb.LookupInvoice(client.invoiceRef)
|
2018-12-20 20:54:11 +03:00
|
|
|
|
|
|
|
// It is possible that the invoice does not exist yet, but the client is
|
|
|
|
// already watching it in anticipation.
|
2019-08-09 13:22:53 +03:00
|
|
|
if err == channeldb.ErrInvoiceNotFound ||
|
|
|
|
err == channeldb.ErrNoInvoicesCreated {
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-25 05:48:32 +03:00
|
|
|
payHash := client.invoiceRef.PayHash()
|
|
|
|
if payHash == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
err = client.notify(&invoiceEvent{
|
2021-03-25 05:48:32 +03:00
|
|
|
hash: *payHash,
|
2018-12-20 20:54:11 +03:00
|
|
|
invoice: &invoice,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:55:02 +03:00
|
|
|
// AddInvoice adds a regular invoice for the specified amount, identified by
|
2017-01-13 08:01:50 +03:00
|
|
|
// the passed preimage. Additionally, any memo or receipt data provided will
|
|
|
|
// also be stored on-disk. Once this invoice is added, subsystems within the
|
2018-04-25 07:07:30 +03:00
|
|
|
// daemon add/forward HTLCs are able to obtain the proper preimage required for
|
2018-06-30 04:06:49 +03:00
|
|
|
// redemption in the case that we're the final destination. We also return the
|
|
|
|
// addIndex of the newly created invoice which monotonically increases for each
|
2019-01-15 12:06:48 +03:00
|
|
|
// new invoice added. A side effect of this function is that it also sets
|
|
|
|
// AddIndex on the invoice argument.
|
2018-12-20 20:54:11 +03:00
|
|
|
func (i *InvoiceRegistry) AddInvoice(invoice *channeldb.Invoice,
|
|
|
|
paymentHash lntypes.Hash) (uint64, error) {
|
|
|
|
|
2018-06-29 07:47:41 +03:00
|
|
|
i.Lock()
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
ref := channeldb.InvoiceRefByHash(paymentHash)
|
|
|
|
log.Debugf("Invoice%v: added with terms %v", ref, invoice.Terms)
|
2016-09-21 03:00:11 +03:00
|
|
|
|
2018-10-05 11:14:56 +03:00
|
|
|
addIndex, err := i.cdb.AddInvoice(invoice, paymentHash)
|
2018-06-30 04:06:49 +03:00
|
|
|
if err != nil {
|
2019-12-09 19:50:11 +03:00
|
|
|
i.Unlock()
|
2018-06-30 04:06:49 +03:00
|
|
|
return 0, err
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
|
|
|
|
2018-06-30 04:07:13 +03:00
|
|
|
// Now that we've added the invoice, we'll send dispatch a message to
|
|
|
|
// notify the clients of this new invoice.
|
2018-12-20 20:54:11 +03:00
|
|
|
i.notifyClients(paymentHash, invoice, channeldb.ContractOpen)
|
2019-12-09 19:50:11 +03:00
|
|
|
i.Unlock()
|
|
|
|
|
|
|
|
// InvoiceExpiryWatcher.AddInvoice must not be locked by InvoiceRegistry
|
|
|
|
// to avoid deadlock when a new invoice is added while an other is being
|
|
|
|
// canceled.
|
2020-10-21 14:21:35 +03:00
|
|
|
invoiceExpiryRef := makeInvoiceExpiry(paymentHash, invoice)
|
|
|
|
if invoiceExpiryRef != nil {
|
|
|
|
i.expiryWatcher.AddInvoices(invoiceExpiryRef)
|
|
|
|
}
|
2016-10-15 05:47:10 +03:00
|
|
|
|
2018-06-30 04:06:49 +03:00
|
|
|
return addIndex, nil
|
2016-09-19 21:55:02 +03:00
|
|
|
}
|
|
|
|
|
2018-06-30 02:03:46 +03:00
|
|
|
// LookupInvoice looks up an invoice by its payment hash (R-Hash), if found
|
2019-08-20 15:54:39 +03:00
|
|
|
// then we're able to pull the funds pending within an HTLC.
|
2018-06-30 02:03:46 +03:00
|
|
|
//
|
2016-09-19 21:55:02 +03:00
|
|
|
// TODO(roasbeef): ignore if settled?
|
2019-08-20 15:54:39 +03:00
|
|
|
func (i *InvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (channeldb.Invoice,
|
|
|
|
error) {
|
|
|
|
|
2019-08-14 20:57:31 +03:00
|
|
|
// We'll check the database to see if there's an existing matching
|
|
|
|
// invoice.
|
2020-05-22 01:37:10 +03:00
|
|
|
ref := channeldb.InvoiceRefByHash(rHash)
|
|
|
|
return i.cdb.LookupInvoice(ref)
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// startHtlcTimer starts a new timer via the invoice registry main loop that
|
|
|
|
// cancels a single htlc on an invoice when the htlc hold duration has passed.
|
2020-05-22 01:37:10 +03:00
|
|
|
func (i *InvoiceRegistry) startHtlcTimer(invoiceRef channeldb.InvoiceRef,
|
2019-09-03 13:23:39 +03:00
|
|
|
key channeldb.CircuitKey, acceptTime time.Time) error {
|
|
|
|
|
|
|
|
releaseTime := acceptTime.Add(i.cfg.HtlcHoldDuration)
|
|
|
|
event := &htlcReleaseEvent{
|
2020-05-22 01:37:10 +03:00
|
|
|
invoiceRef: invoiceRef,
|
2019-09-03 13:23:39 +03:00
|
|
|
key: key,
|
|
|
|
releaseTime: releaseTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case i.htlcAutoReleaseChan <- event:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case <-i.quit:
|
|
|
|
return ErrShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// cancelSingleHtlc cancels a single accepted htlc on an invoice. It takes
|
|
|
|
// a resolution result which will be used to notify subscribed links and
|
|
|
|
// resolvers of the details of the htlc cancellation.
|
2020-05-22 01:37:10 +03:00
|
|
|
func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef channeldb.InvoiceRef,
|
2020-02-06 20:35:16 +03:00
|
|
|
key channeldb.CircuitKey, result FailResolutionResult) error {
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
updateInvoice := func(invoice *channeldb.Invoice) (
|
|
|
|
*channeldb.InvoiceUpdateDesc, error) {
|
|
|
|
|
|
|
|
// Only allow individual htlc cancelation on open invoices.
|
|
|
|
if invoice.State != channeldb.ContractOpen {
|
|
|
|
log.Debugf("cancelSingleHtlc: invoice %v no longer "+
|
2020-05-22 01:37:10 +03:00
|
|
|
"open", invoiceRef)
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the current status of the htlc in the database.
|
|
|
|
htlc, ok := invoice.Htlcs[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("htlc %v not found", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancelation is only possible if the htlc wasn't already
|
|
|
|
// resolved.
|
|
|
|
if htlc.State != channeldb.HtlcStateAccepted {
|
|
|
|
log.Debugf("cancelSingleHtlc: htlc %v on invoice %v "+
|
2020-05-22 01:37:10 +03:00
|
|
|
"is already resolved", key, invoiceRef)
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("cancelSingleHtlc: cancelling htlc %v on invoice %v",
|
2020-05-22 01:37:10 +03:00
|
|
|
key, invoiceRef)
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
// Return an update descriptor that cancels htlc and keeps
|
|
|
|
// invoice open.
|
|
|
|
canceledHtlcs := map[channeldb.CircuitKey]struct{}{
|
|
|
|
key: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &channeldb.InvoiceUpdateDesc{
|
|
|
|
CancelHtlcs: canceledHtlcs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to mark the specified htlc as canceled in the invoice database.
|
|
|
|
// Intercept the update descriptor to set the local updated variable. If
|
|
|
|
// no invoice update is performed, we can return early.
|
|
|
|
var updated bool
|
2020-05-22 01:37:10 +03:00
|
|
|
invoice, err := i.cdb.UpdateInvoice(invoiceRef,
|
2019-09-03 13:23:39 +03:00
|
|
|
func(invoice *channeldb.Invoice) (
|
|
|
|
*channeldb.InvoiceUpdateDesc, error) {
|
|
|
|
|
|
|
|
updateDesc, err := updateInvoice(invoice)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
updated = updateDesc != nil
|
|
|
|
|
|
|
|
return updateDesc, err
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !updated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The invoice has been updated. Notify subscribers of the htlc
|
|
|
|
// resolution.
|
|
|
|
htlc, ok := invoice.Htlcs[key]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("htlc %v not found", key)
|
|
|
|
}
|
|
|
|
if htlc.State == channeldb.HtlcStateCanceled {
|
2020-02-06 20:35:10 +03:00
|
|
|
resolution := NewFailResolution(
|
2019-12-20 13:25:07 +03:00
|
|
|
key, int32(htlc.AcceptHeight), result,
|
2019-12-20 13:25:07 +03:00
|
|
|
)
|
2019-12-20 13:25:07 +03:00
|
|
|
|
|
|
|
i.notifyHodlSubscribers(resolution)
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
// processKeySend just-in-time inserts an invoice if this htlc is a keysend
|
2019-12-05 14:55:17 +03:00
|
|
|
// htlc.
|
2020-02-04 17:15:48 +03:00
|
|
|
func (i *InvoiceRegistry) processKeySend(ctx invoiceUpdateCtx) error {
|
2020-01-16 15:13:59 +03:00
|
|
|
// Retrieve keysend record if present.
|
2019-12-05 14:55:17 +03:00
|
|
|
preimageSlice, ok := ctx.customRecords[record.KeySendType]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel htlc is preimage is invalid.
|
|
|
|
preimage, err := lntypes.MakePreimage(preimageSlice)
|
2021-03-03 20:59:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if preimage.Hash() != ctx.hash {
|
|
|
|
return fmt.Errorf("invalid keysend preimage %v for hash %v",
|
|
|
|
preimage, ctx.hash)
|
2019-12-05 14:55:17 +03:00
|
|
|
}
|
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
// Only allow keysend for non-mpp payments.
|
2019-12-05 14:55:17 +03:00
|
|
|
if ctx.mpp != nil {
|
2020-01-16 15:13:59 +03:00
|
|
|
return errors.New("no mpp keysend supported")
|
2019-12-05 14:55:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an invoice for the htlc amount.
|
|
|
|
amt := ctx.amtPaid
|
|
|
|
|
|
|
|
// Set tlv optional feature vector on the invoice. Otherwise we wouldn't
|
2020-01-16 15:13:59 +03:00
|
|
|
// be able to pay to it with keysend.
|
2019-12-05 14:55:17 +03:00
|
|
|
rawFeatures := lnwire.NewRawFeatureVector(
|
|
|
|
lnwire.TLVOnionPayloadOptional,
|
|
|
|
)
|
|
|
|
features := lnwire.NewFeatureVector(rawFeatures, lnwire.Features)
|
|
|
|
|
|
|
|
// Use the minimum block delta that we require for settling htlcs.
|
|
|
|
finalCltvDelta := i.cfg.FinalCltvRejectDelta
|
|
|
|
|
2020-01-10 23:35:10 +03:00
|
|
|
// Pre-check expiry here to prevent inserting an invoice that will not
|
|
|
|
// be settled.
|
|
|
|
if ctx.expiry < uint32(ctx.currentHeight+finalCltvDelta) {
|
|
|
|
return errors.New("final expiry too soon")
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:17:18 +03:00
|
|
|
// The invoice database indexes all invoices by payment address, however
|
|
|
|
// legacy keysend payment do not have one. In order to avoid a new
|
|
|
|
// payment type on-disk wrt. to indexing, we'll continue to insert a
|
|
|
|
// blank payment address which is special cased in the insertion logic
|
|
|
|
// to not be indexed. In the future, once AMP is merged, this should be
|
|
|
|
// replaced by generating a random payment address on the behalf of the
|
|
|
|
// sender.
|
|
|
|
payAddr := channeldb.BlankPayAddr
|
|
|
|
|
2019-12-05 14:55:17 +03:00
|
|
|
// Create placeholder invoice.
|
|
|
|
invoice := &channeldb.Invoice{
|
|
|
|
CreationDate: i.cfg.Clock.Now(),
|
|
|
|
Terms: channeldb.ContractTerm{
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
Value: amt,
|
2020-04-08 14:47:10 +03:00
|
|
|
PaymentPreimage: &preimage,
|
2020-06-26 12:17:18 +03:00
|
|
|
PaymentAddr: payAddr,
|
2019-12-05 14:55:17 +03:00
|
|
|
Features: features,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-04-09 11:42:10 +03:00
|
|
|
if i.cfg.KeysendHoldTime != 0 {
|
|
|
|
invoice.HodlInvoice = true
|
|
|
|
invoice.Terms.Expiry = i.cfg.KeysendHoldTime
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:55:17 +03:00
|
|
|
// Insert invoice into database. Ignore duplicates, because this
|
|
|
|
// may be a replay.
|
2020-02-04 17:15:48 +03:00
|
|
|
_, err = i.AddInvoice(invoice, ctx.hash)
|
2019-12-05 14:55:17 +03:00
|
|
|
if err != nil && err != channeldb.ErrDuplicateInvoice {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 22:37:23 +03:00
|
|
|
// processAMP just-in-time inserts an invoice if this htlc is a keysend
|
|
|
|
// htlc.
|
|
|
|
func (i *InvoiceRegistry) processAMP(ctx invoiceUpdateCtx) error {
|
|
|
|
// AMP payments MUST also include an MPP record.
|
|
|
|
if ctx.mpp == nil {
|
|
|
|
return errors.New("no MPP record for AMP")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an invoice for the total amount expected, provided in the MPP
|
|
|
|
// record.
|
|
|
|
amt := ctx.mpp.TotalMsat()
|
|
|
|
|
|
|
|
// Set the TLV and MPP optional features on the invoice. We'll also make
|
|
|
|
// the AMP features required so that it can't be paid by legacy or MPP
|
|
|
|
// htlcs.
|
|
|
|
rawFeatures := lnwire.NewRawFeatureVector(
|
|
|
|
lnwire.TLVOnionPayloadOptional,
|
|
|
|
lnwire.PaymentAddrOptional,
|
|
|
|
lnwire.AMPRequired,
|
|
|
|
)
|
|
|
|
features := lnwire.NewFeatureVector(rawFeatures, lnwire.Features)
|
|
|
|
|
|
|
|
// Use the minimum block delta that we require for settling htlcs.
|
|
|
|
finalCltvDelta := i.cfg.FinalCltvRejectDelta
|
|
|
|
|
|
|
|
// Pre-check expiry here to prevent inserting an invoice that will not
|
|
|
|
// be settled.
|
|
|
|
if ctx.expiry < uint32(ctx.currentHeight+finalCltvDelta) {
|
|
|
|
return errors.New("final expiry too soon")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll use the sender-generated payment address provided in the HTLC
|
|
|
|
// to create our AMP invoice.
|
|
|
|
payAddr := ctx.mpp.PaymentAddr()
|
|
|
|
|
|
|
|
// Create placeholder invoice.
|
|
|
|
invoice := &channeldb.Invoice{
|
|
|
|
CreationDate: i.cfg.Clock.Now(),
|
|
|
|
Terms: channeldb.ContractTerm{
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
Value: amt,
|
|
|
|
PaymentPreimage: nil,
|
|
|
|
PaymentAddr: payAddr,
|
|
|
|
Features: features,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert invoice into database. Ignore duplicates payment hashes and
|
|
|
|
// payment addrs, this may be a replay or a different HTLC for the AMP
|
|
|
|
// invoice.
|
|
|
|
_, err := i.AddInvoice(invoice, ctx.hash)
|
|
|
|
switch {
|
|
|
|
case err == channeldb.ErrDuplicateInvoice:
|
|
|
|
return nil
|
|
|
|
case err == channeldb.ErrDuplicatePayAddr:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:33:14 +03:00
|
|
|
// NotifyExitHopHtlc attempts to mark an invoice as settled. The return value
|
|
|
|
// describes how the htlc should be resolved.
|
2019-02-11 14:01:05 +03:00
|
|
|
//
|
|
|
|
// When the preimage of the invoice is not yet known (hodl invoice), this
|
|
|
|
// function moves the invoice to the accepted state. When SettleHoldInvoice is
|
|
|
|
// called later, a resolution message will be send back to the caller via the
|
|
|
|
// provided hodlChan. Invoice registry sends on this channel what action needs
|
|
|
|
// to be taken on the htlc (settle or cancel). The caller needs to ensure that
|
|
|
|
// the channel is either buffered or received on from another goroutine to
|
|
|
|
// prevent deadlock.
|
2019-09-03 13:23:39 +03:00
|
|
|
//
|
|
|
|
// In the case that the htlc is part of a larger set of htlcs that pay to the
|
|
|
|
// same invoice (multi-path payment), the htlc is held until the set is
|
|
|
|
// complete. If the set doesn't fully arrive in time, a timer will cancel the
|
|
|
|
// held htlc.
|
2019-02-20 14:11:15 +03:00
|
|
|
func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
|
2019-04-16 13:11:20 +03:00
|
|
|
amtPaid lnwire.MilliSatoshi, expiry uint32, currentHeight int32,
|
2019-08-08 16:48:31 +03:00
|
|
|
circuitKey channeldb.CircuitKey, hodlChan chan<- interface{},
|
2020-02-06 20:35:10 +03:00
|
|
|
payload Payload) (HtlcResolution, error) {
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2019-11-27 12:05:14 +03:00
|
|
|
// Create the update context containing the relevant details of the
|
|
|
|
// incoming htlc.
|
2020-05-22 01:36:44 +03:00
|
|
|
ctx := invoiceUpdateCtx{
|
2020-02-04 17:15:48 +03:00
|
|
|
hash: rHash,
|
2019-11-27 12:05:14 +03:00
|
|
|
circuitKey: circuitKey,
|
|
|
|
amtPaid: amtPaid,
|
|
|
|
expiry: expiry,
|
|
|
|
currentHeight: currentHeight,
|
2019-12-04 20:47:53 +03:00
|
|
|
finalCltvRejectDelta: i.cfg.FinalCltvRejectDelta,
|
2019-11-19 15:33:05 +03:00
|
|
|
customRecords: payload.CustomRecords(),
|
2020-05-22 01:36:44 +03:00
|
|
|
mpp: payload.MultiPath(),
|
2021-03-03 20:54:11 +03:00
|
|
|
amp: payload.AMPRecord(),
|
2019-11-27 12:05:14 +03:00
|
|
|
}
|
2019-08-09 16:09:57 +03:00
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
// Process keysend if present. Do this outside of the lock, because
|
2019-12-05 14:55:17 +03:00
|
|
|
// AddInvoice obtains its own lock. This is no problem, because the
|
|
|
|
// operation is idempotent.
|
|
|
|
if i.cfg.AcceptKeySend {
|
2021-03-22 22:37:23 +03:00
|
|
|
if ctx.amp != nil {
|
|
|
|
err := i.processAMP(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.log(fmt.Sprintf("amp error: %v", err))
|
2019-12-05 14:55:17 +03:00
|
|
|
|
2021-03-22 22:37:23 +03:00
|
|
|
return NewFailResolution(
|
|
|
|
circuitKey, currentHeight, ResultAmpError,
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := i.processKeySend(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.log(fmt.Sprintf("keysend error: %v", err))
|
|
|
|
|
|
|
|
return NewFailResolution(
|
|
|
|
circuitKey, currentHeight, ResultKeySendError,
|
|
|
|
), nil
|
|
|
|
}
|
2019-12-05 14:55:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:07:56 +03:00
|
|
|
// Execute locked notify exit hop logic.
|
2019-12-05 14:54:56 +03:00
|
|
|
i.Lock()
|
2020-05-22 01:36:44 +03:00
|
|
|
resolution, err := i.notifyExitHopHtlcLocked(&ctx, hodlChan)
|
2020-02-04 12:07:56 +03:00
|
|
|
i.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r := resolution.(type) {
|
|
|
|
// The htlc is held. Start a timer outside the lock if the htlc should
|
|
|
|
// be auto-released, because otherwise a deadlock may happen with the
|
|
|
|
// main event loop.
|
2020-02-06 20:35:10 +03:00
|
|
|
case *htlcAcceptResolution:
|
2020-02-04 12:07:56 +03:00
|
|
|
if r.autoRelease {
|
2020-05-22 01:37:10 +03:00
|
|
|
err := i.startHtlcTimer(
|
|
|
|
ctx.invoiceRef(), circuitKey, r.acceptTime,
|
|
|
|
)
|
2020-02-04 12:07:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// We return a nil resolution because htlc acceptances are
|
|
|
|
// represented as nil resolutions externally.
|
|
|
|
// TODO(carla) update calling code to handle accept resolutions.
|
2020-02-04 12:07:56 +03:00
|
|
|
return nil, nil
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// A direct resolution was received for this htlc.
|
|
|
|
case HtlcResolution:
|
|
|
|
return r, nil
|
|
|
|
|
|
|
|
// Fail if an unknown resolution type was received.
|
2020-02-04 12:07:56 +03:00
|
|
|
default:
|
|
|
|
return nil, errors.New("invalid resolution type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifyExitHopHtlcLocked is the internal implementation of NotifyExitHopHtlc
|
|
|
|
// that should be executed inside the registry lock.
|
|
|
|
func (i *InvoiceRegistry) notifyExitHopHtlcLocked(
|
|
|
|
ctx *invoiceUpdateCtx, hodlChan chan<- interface{}) (
|
2020-02-06 20:35:10 +03:00
|
|
|
HtlcResolution, error) {
|
2019-12-05 14:54:56 +03:00
|
|
|
|
2019-11-27 12:05:14 +03:00
|
|
|
// We'll attempt to settle an invoice matching this rHash on disk (if
|
|
|
|
// one exists). The callback will update the invoice state and/or htlcs.
|
|
|
|
var (
|
2020-02-06 20:35:10 +03:00
|
|
|
resolution HtlcResolution
|
2019-11-27 12:05:14 +03:00
|
|
|
updateSubscribers bool
|
|
|
|
)
|
|
|
|
invoice, err := i.cdb.UpdateInvoice(
|
2020-05-22 01:37:10 +03:00
|
|
|
ctx.invoiceRef(),
|
2019-11-27 12:05:14 +03:00
|
|
|
func(inv *channeldb.Invoice) (
|
|
|
|
*channeldb.InvoiceUpdateDesc, error) {
|
2019-08-09 16:09:57 +03:00
|
|
|
|
2020-02-04 12:07:56 +03:00
|
|
|
updateDesc, res, err := updateInvoice(ctx, inv)
|
2019-11-27 12:05:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-09 16:09:57 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:05:14 +03:00
|
|
|
// Only send an update if the invoice state was changed.
|
|
|
|
updateSubscribers = updateDesc != nil &&
|
2019-11-27 15:20:14 +03:00
|
|
|
updateDesc.State != nil
|
2017-01-15 05:18:08 +03:00
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// Assign resolution to outer scope variable.
|
|
|
|
resolution = res
|
2019-08-09 16:09:57 +03:00
|
|
|
|
2019-11-27 12:05:14 +03:00
|
|
|
return updateDesc, nil
|
|
|
|
},
|
|
|
|
)
|
2019-12-20 13:25:08 +03:00
|
|
|
switch err {
|
|
|
|
case channeldb.ErrInvoiceNotFound:
|
|
|
|
// If the invoice was not found, return a failure resolution
|
|
|
|
// with an invoice not found result.
|
2020-02-06 20:35:10 +03:00
|
|
|
return NewFailResolution(
|
2020-02-04 12:07:56 +03:00
|
|
|
ctx.circuitKey, ctx.currentHeight,
|
|
|
|
ResultInvoiceNotFound,
|
2019-12-20 13:25:08 +03:00
|
|
|
), nil
|
2019-04-16 13:11:20 +03:00
|
|
|
|
2019-12-20 13:25:08 +03:00
|
|
|
case nil:
|
|
|
|
|
|
|
|
default:
|
2020-02-04 12:07:56 +03:00
|
|
|
ctx.log(err.Error())
|
2019-02-11 14:01:05 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-20 13:25:08 +03:00
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
switch res := resolution.(type) {
|
|
|
|
case *HtlcFailResolution:
|
|
|
|
// Inspect latest htlc state on the invoice. If it is found,
|
|
|
|
// we will update the accept height as it was recorded in the
|
|
|
|
// invoice database (which occurs in the case where the htlc
|
|
|
|
// reached the database in a previous call). If the htlc was
|
|
|
|
// not found on the invoice, it was immediately failed so we
|
|
|
|
// send the failure resolution as is, which has the current
|
|
|
|
// height set as the accept height.
|
|
|
|
invoiceHtlc, ok := invoice.Htlcs[ctx.circuitKey]
|
|
|
|
if ok {
|
|
|
|
res.AcceptHeight = int32(invoiceHtlc.AcceptHeight)
|
|
|
|
}
|
2019-08-15 20:33:30 +03:00
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
ctx.log(fmt.Sprintf("failure resolution result "+
|
|
|
|
"outcome: %v, at accept height: %v",
|
|
|
|
res.Outcome, res.AcceptHeight))
|
2019-08-15 20:33:30 +03:00
|
|
|
|
2021-03-25 05:52:18 +03:00
|
|
|
// Some failures apply to the entire HTLC set. Break here if
|
|
|
|
// this isn't one of them.
|
|
|
|
if !res.Outcome.IsSetFailure() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also cancel any HTLCs in the HTLC set that are also in the
|
|
|
|
// canceled state with the same failure result.
|
|
|
|
setID := ctx.setID()
|
|
|
|
for key, htlc := range invoice.Htlcs {
|
|
|
|
if htlc.State != channeldb.HtlcStateCanceled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !htlc.IsInHTLCSet(setID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
htlcFailResolution := NewFailResolution(
|
|
|
|
key, int32(htlc.AcceptHeight), res.Outcome,
|
|
|
|
)
|
|
|
|
|
|
|
|
i.notifyHodlSubscribers(htlcFailResolution)
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// If the htlc was settled, we will settle any previously accepted
|
|
|
|
// htlcs and notify our peer to settle them.
|
|
|
|
case *HtlcSettleResolution:
|
|
|
|
ctx.log(fmt.Sprintf("settle resolution result "+
|
|
|
|
"outcome: %v, at accept height: %v",
|
|
|
|
res.Outcome, res.AcceptHeight))
|
2019-08-15 20:33:30 +03:00
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// Also settle any previously accepted htlcs. If a htlc is
|
|
|
|
// marked as settled, we should follow now and settle the htlc
|
|
|
|
// with our peer.
|
2019-09-03 13:23:39 +03:00
|
|
|
for key, htlc := range invoice.Htlcs {
|
|
|
|
if htlc.State != channeldb.HtlcStateSettled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-25 05:52:01 +03:00
|
|
|
preimage := res.Preimage
|
|
|
|
if htlc.AMP != nil && htlc.AMP.Preimage != nil {
|
|
|
|
preimage = *htlc.AMP.Preimage
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// Notify subscribers that the htlcs should be settled
|
|
|
|
// with our peer. Note that the outcome of the
|
|
|
|
// resolution is set based on the outcome of the single
|
|
|
|
// htlc that we just settled, so may not be accurate
|
|
|
|
// for all htlcs.
|
2020-02-06 20:35:10 +03:00
|
|
|
htlcSettleResolution := NewSettleResolution(
|
2021-03-25 05:52:01 +03:00
|
|
|
preimage, key,
|
2020-02-06 20:35:10 +03:00
|
|
|
int32(htlc.AcceptHeight), res.Outcome,
|
2019-12-20 13:25:07 +03:00
|
|
|
)
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// Notify subscribers that the htlc should be settled
|
|
|
|
// with our peer.
|
|
|
|
i.notifyHodlSubscribers(htlcSettleResolution)
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
// If we accepted the htlc, subscribe to the hodl invoice and return
|
|
|
|
// an accept resolution with the htlc's accept time on it.
|
|
|
|
case *htlcAcceptResolution:
|
|
|
|
invoiceHtlc, ok := invoice.Htlcs[ctx.circuitKey]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("accepted htlc: %v not"+
|
|
|
|
" present on invoice: %x", ctx.circuitKey,
|
|
|
|
ctx.hash[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine accepted height of this htlc. If the htlc reached
|
|
|
|
// the invoice database (possibly in a previous call to the
|
|
|
|
// invoice registry), we'll take the original accepted height
|
|
|
|
// as it was recorded in the database.
|
|
|
|
acceptHeight := int32(invoiceHtlc.AcceptHeight)
|
|
|
|
|
|
|
|
ctx.log(fmt.Sprintf("accept resolution result "+
|
|
|
|
"outcome: %v, at accept height: %v",
|
|
|
|
res.outcome, acceptHeight))
|
2020-02-04 12:07:56 +03:00
|
|
|
|
|
|
|
// Auto-release the htlc if the invoice is still open. It can
|
2019-09-03 13:23:39 +03:00
|
|
|
// only happen for mpp payments that there are htlcs in state
|
|
|
|
// Accepted while the invoice is Open.
|
|
|
|
if invoice.State == channeldb.ContractOpen {
|
2020-02-06 20:35:10 +03:00
|
|
|
res.acceptTime = invoiceHtlc.AcceptTime
|
|
|
|
res.autoRelease = true
|
2020-02-04 12:07:56 +03:00
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 12:07:56 +03:00
|
|
|
i.hodlSubscribe(hodlChan, ctx.circuitKey)
|
2019-08-15 20:33:30 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unknown action")
|
|
|
|
}
|
2020-04-24 09:24:30 +03:00
|
|
|
|
|
|
|
// Now that the links have been notified of any state changes to their
|
|
|
|
// HTLCs, we'll go ahead and notify any clients wiaiting on the invoice
|
|
|
|
// state changes.
|
|
|
|
if updateSubscribers {
|
|
|
|
i.notifyClients(ctx.hash, invoice, invoice.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolution, nil
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettleHodlInvoice sets the preimage of a hodl invoice.
|
|
|
|
func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
2019-08-21 16:24:21 +03:00
|
|
|
updateInvoice := func(invoice *channeldb.Invoice) (
|
|
|
|
*channeldb.InvoiceUpdateDesc, error) {
|
|
|
|
|
2019-11-22 13:25:02 +03:00
|
|
|
switch invoice.State {
|
2019-08-21 16:24:21 +03:00
|
|
|
case channeldb.ContractOpen:
|
|
|
|
return nil, channeldb.ErrInvoiceStillOpen
|
|
|
|
case channeldb.ContractCanceled:
|
|
|
|
return nil, channeldb.ErrInvoiceAlreadyCanceled
|
|
|
|
case channeldb.ContractSettled:
|
|
|
|
return nil, channeldb.ErrInvoiceAlreadySettled
|
|
|
|
}
|
|
|
|
|
|
|
|
return &channeldb.InvoiceUpdateDesc{
|
2019-11-27 15:20:14 +03:00
|
|
|
State: &channeldb.InvoiceStateUpdateDesc{
|
|
|
|
NewState: channeldb.ContractSettled,
|
2020-04-08 14:47:10 +03:00
|
|
|
Preimage: &preimage,
|
2019-11-27 15:20:14 +03:00
|
|
|
},
|
2019-08-21 16:24:21 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := preimage.Hash()
|
2020-05-22 01:37:10 +03:00
|
|
|
invoiceRef := channeldb.InvoiceRefByHash(hash)
|
|
|
|
invoice, err := i.cdb.UpdateInvoice(invoiceRef, updateInvoice)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != nil {
|
2019-12-20 13:25:07 +03:00
|
|
|
log.Errorf("SettleHodlInvoice with preimage %v: %v",
|
|
|
|
preimage, err)
|
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
return err
|
2019-02-20 14:11:15 +03:00
|
|
|
}
|
2016-10-15 05:47:10 +03:00
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
log.Debugf("Invoice%v: settled with preimage %v", invoiceRef,
|
2019-02-11 14:01:05 +03:00
|
|
|
invoice.Terms.PaymentPreimage)
|
|
|
|
|
2019-08-14 22:11:34 +03:00
|
|
|
// In the callback, we marked the invoice as settled. UpdateInvoice will
|
|
|
|
// have seen this and should have moved all htlcs that were accepted to
|
|
|
|
// the settled state. In the loop below, we go through all of these and
|
|
|
|
// notify links and resolvers that are waiting for resolution. Any htlcs
|
|
|
|
// that were already settled before, will be notified again. This isn't
|
|
|
|
// necessary but doesn't hurt either.
|
|
|
|
for key, htlc := range invoice.Htlcs {
|
|
|
|
if htlc.State != channeldb.HtlcStateSettled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
resolution := NewSettleResolution(
|
2019-12-20 13:25:07 +03:00
|
|
|
preimage, key, int32(htlc.AcceptHeight), ResultSettled,
|
2019-12-20 13:25:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
i.notifyHodlSubscribers(resolution)
|
2019-08-14 22:11:34 +03:00
|
|
|
}
|
2019-11-22 13:25:02 +03:00
|
|
|
i.notifyClients(hash, invoice, invoice.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
return nil
|
2016-10-15 05:47:10 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 13:19:16 +03:00
|
|
|
// CancelInvoice attempts to cancel the invoice corresponding to the passed
|
|
|
|
// payment hash.
|
|
|
|
func (i *InvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error {
|
2019-12-09 19:50:11 +03:00
|
|
|
return i.cancelInvoiceImpl(payHash, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelInvoice attempts to cancel the invoice corresponding to the passed
|
|
|
|
// payment hash. Accepted invoices will only be canceled if explicitly
|
2019-12-20 13:25:07 +03:00
|
|
|
// requested to do so. It notifies subscribing links and resolvers that
|
|
|
|
// the associated htlcs were canceled if they change state.
|
2019-12-09 19:50:11 +03:00
|
|
|
func (i *InvoiceRegistry) cancelInvoiceImpl(payHash lntypes.Hash,
|
|
|
|
cancelAccepted bool) error {
|
|
|
|
|
2019-01-11 13:19:16 +03:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
ref := channeldb.InvoiceRefByHash(payHash)
|
|
|
|
log.Debugf("Invoice%v: canceling invoice", ref)
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2019-08-09 16:30:33 +03:00
|
|
|
updateInvoice := func(invoice *channeldb.Invoice) (
|
|
|
|
*channeldb.InvoiceUpdateDesc, error) {
|
|
|
|
|
2019-12-09 19:50:11 +03:00
|
|
|
// Only cancel the invoice in ContractAccepted state if explicitly
|
|
|
|
// requested to do so.
|
|
|
|
if invoice.State == channeldb.ContractAccepted && !cancelAccepted {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// Move invoice to the canceled state. Rely on validation in
|
|
|
|
// channeldb to return an error if the invoice is already
|
|
|
|
// settled or canceled.
|
2019-08-09 16:30:33 +03:00
|
|
|
return &channeldb.InvoiceUpdateDesc{
|
2019-11-27 15:20:14 +03:00
|
|
|
State: &channeldb.InvoiceStateUpdateDesc{
|
|
|
|
NewState: channeldb.ContractCanceled,
|
|
|
|
},
|
2019-08-09 16:30:33 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
invoiceRef := channeldb.InvoiceRefByHash(payHash)
|
|
|
|
invoice, err := i.cdb.UpdateInvoice(invoiceRef, updateInvoice)
|
2019-01-11 13:19:16 +03:00
|
|
|
|
|
|
|
// Implement idempotency by returning success if the invoice was already
|
|
|
|
// canceled.
|
|
|
|
if err == channeldb.ErrInvoiceAlreadyCanceled {
|
2020-05-22 01:37:10 +03:00
|
|
|
log.Debugf("Invoice%v: already canceled", ref)
|
2019-01-11 13:19:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-09 19:50:11 +03:00
|
|
|
|
|
|
|
// Return without cancellation if the invoice state is ContractAccepted.
|
|
|
|
if invoice.State == channeldb.ContractAccepted {
|
2020-05-22 01:37:10 +03:00
|
|
|
log.Debugf("Invoice%v: remains accepted as cancel wasn't"+
|
|
|
|
"explicitly requested.", ref)
|
2019-12-09 19:50:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
log.Debugf("Invoice%v: canceled", ref)
|
2019-08-14 22:11:34 +03:00
|
|
|
|
|
|
|
// In the callback, some htlcs may have been moved to the canceled
|
|
|
|
// state. We now go through all of these and notify links and resolvers
|
|
|
|
// that are waiting for resolution. Any htlcs that were already canceled
|
|
|
|
// before, will be notified again. This isn't necessary but doesn't hurt
|
|
|
|
// either.
|
|
|
|
for key, htlc := range invoice.Htlcs {
|
2019-10-03 18:22:43 +03:00
|
|
|
if htlc.State != channeldb.HtlcStateCanceled {
|
2019-08-14 22:11:34 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
i.notifyHodlSubscribers(
|
2020-02-06 20:35:10 +03:00
|
|
|
NewFailResolution(
|
2019-12-20 13:25:07 +03:00
|
|
|
key, int32(htlc.AcceptHeight), ResultCanceled,
|
|
|
|
),
|
2019-12-20 13:25:07 +03:00
|
|
|
)
|
2019-08-14 22:11:34 +03:00
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
i.notifyClients(payHash, invoice, channeldb.ContractCanceled)
|
|
|
|
|
2020-08-04 19:28:15 +03:00
|
|
|
// Attempt to also delete the invoice if requested through the registry
|
|
|
|
// config.
|
|
|
|
if i.cfg.GcCanceledInvoicesOnTheFly {
|
|
|
|
// Assemble the delete reference and attempt to delete through
|
|
|
|
// the invocice from the DB.
|
|
|
|
deleteRef := channeldb.InvoiceDeleteRef{
|
|
|
|
PayHash: payHash,
|
|
|
|
AddIndex: invoice.AddIndex,
|
|
|
|
SettleIndex: invoice.SettleIndex,
|
|
|
|
}
|
|
|
|
if invoice.Terms.PaymentAddr != channeldb.BlankPayAddr {
|
|
|
|
deleteRef.PayAddr = &invoice.Terms.PaymentAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.cdb.DeleteInvoice(
|
|
|
|
[]channeldb.InvoiceDeleteRef{deleteRef},
|
|
|
|
)
|
|
|
|
// If by any chance deletion failed, then log it instead of
|
|
|
|
// returning the error, as the invoice itsels has already been
|
|
|
|
// canceled.
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Invoice%v could not be deleted: %v",
|
|
|
|
ref, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 13:19:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:51:25 +03:00
|
|
|
// notifyClients notifies all currently registered invoice notification clients
|
2016-10-15 05:47:10 +03:00
|
|
|
// of a newly added/settled invoice.
|
2018-12-20 20:54:11 +03:00
|
|
|
func (i *InvoiceRegistry) notifyClients(hash lntypes.Hash,
|
|
|
|
invoice *channeldb.Invoice,
|
2018-12-19 17:56:26 +03:00
|
|
|
state channeldb.ContractState) {
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
event := &invoiceEvent{
|
2018-12-19 17:56:26 +03:00
|
|
|
invoice: invoice,
|
2018-12-20 20:54:11 +03:00
|
|
|
hash: hash,
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
2016-10-15 05:47:10 +03:00
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
select {
|
|
|
|
case i.invoiceEvents <- event:
|
|
|
|
case <-i.quit:
|
2016-10-15 05:47:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
// invoiceSubscriptionKit defines that are common to both all invoice
|
|
|
|
// subscribers and single invoice subscribers.
|
|
|
|
type invoiceSubscriptionKit struct {
|
|
|
|
id uint32
|
|
|
|
inv *InvoiceRegistry
|
|
|
|
ntfnQueue *queue.ConcurrentQueue
|
|
|
|
|
2019-10-03 18:22:43 +03:00
|
|
|
canceled uint32 // To be used atomically.
|
2018-12-20 20:54:11 +03:00
|
|
|
cancelChan chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
// InvoiceSubscription represents an intent to receive updates for newly added
|
2016-10-15 05:47:10 +03:00
|
|
|
// or settled invoices. For each newly added invoice, a copy of the invoice
|
|
|
|
// will be sent over the NewInvoices channel. Similarly, for each newly settled
|
|
|
|
// invoice, a copy of the invoice will be sent over the SettledInvoices
|
|
|
|
// channel.
|
2018-12-20 13:57:44 +03:00
|
|
|
type InvoiceSubscription struct {
|
2018-12-20 20:54:11 +03:00
|
|
|
invoiceSubscriptionKit
|
2018-04-25 07:07:30 +03:00
|
|
|
|
|
|
|
// NewInvoices is a channel that we'll use to send all newly created
|
|
|
|
// invoices with an invoice index greater than the specified
|
|
|
|
// StartingInvoiceIndex field.
|
|
|
|
NewInvoices chan *channeldb.Invoice
|
|
|
|
|
|
|
|
// SettledInvoices is a channel that we'll use to send all setted
|
|
|
|
// invoices with an invoices index greater than the specified
|
|
|
|
// StartingInvoiceIndex field.
|
2016-10-15 05:47:10 +03:00
|
|
|
SettledInvoices chan *channeldb.Invoice
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
// addIndex is the highest add index the caller knows of. We'll use
|
|
|
|
// this information to send out an event backlog to the notifications
|
|
|
|
// subscriber. Any new add events with an index greater than this will
|
|
|
|
// be dispatched before any new notifications are sent out.
|
|
|
|
addIndex uint64
|
|
|
|
|
|
|
|
// settleIndex is the highest settle index the caller knows of. We'll
|
|
|
|
// use this information to send out an event backlog to the
|
|
|
|
// notifications subscriber. Any new settle events with an index
|
|
|
|
// greater than this will be dispatched before any new notifications
|
|
|
|
// are sent out.
|
|
|
|
settleIndex uint64
|
2018-12-20 20:54:11 +03:00
|
|
|
}
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
// SingleInvoiceSubscription represents an intent to receive updates for a
|
|
|
|
// specific invoice.
|
|
|
|
type SingleInvoiceSubscription struct {
|
|
|
|
invoiceSubscriptionKit
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
invoiceRef channeldb.InvoiceRef
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
// Updates is a channel that we'll use to send all invoice events for
|
|
|
|
// the invoice that is subscribed to.
|
|
|
|
Updates chan *channeldb.Invoice
|
2016-10-15 05:47:10 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
// Cancel unregisters the InvoiceSubscription, freeing any previously allocated
|
2016-10-15 05:47:10 +03:00
|
|
|
// resources.
|
2018-12-20 20:54:11 +03:00
|
|
|
func (i *invoiceSubscriptionKit) Cancel() {
|
2019-10-03 18:22:43 +03:00
|
|
|
if !atomic.CompareAndSwapUint32(&i.canceled, 0, 1) {
|
2018-04-25 07:07:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case i.inv.subscriptionCancels <- i.id:
|
|
|
|
case <-i.inv.quit:
|
|
|
|
}
|
|
|
|
|
|
|
|
i.ntfnQueue.Stop()
|
|
|
|
close(i.cancelChan)
|
|
|
|
|
|
|
|
i.wg.Wait()
|
2016-10-15 05:47:10 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
func (i *invoiceSubscriptionKit) notify(event *invoiceEvent) error {
|
|
|
|
select {
|
|
|
|
case i.ntfnQueue.ChanIn() <- event:
|
|
|
|
case <-i.inv.quit:
|
2019-08-13 19:57:27 +03:00
|
|
|
return ErrShuttingDown
|
2018-12-20 20:54:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
// SubscribeNotifications returns an InvoiceSubscription which allows the
|
2016-10-15 05:47:10 +03:00
|
|
|
// caller to receive async notifications when any invoices are settled or
|
2018-04-25 07:07:30 +03:00
|
|
|
// added. The invoiceIndex parameter is a streaming "checkpoint". We'll start
|
|
|
|
// by first sending out all new events with an invoice index _greater_ than
|
|
|
|
// this value. Afterwards, we'll send out real-time notifications.
|
2020-05-20 06:31:38 +03:00
|
|
|
func (i *InvoiceRegistry) SubscribeNotifications(
|
|
|
|
addIndex, settleIndex uint64) (*InvoiceSubscription, error) {
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
client := &InvoiceSubscription{
|
2016-10-15 05:47:10 +03:00
|
|
|
NewInvoices: make(chan *channeldb.Invoice),
|
|
|
|
SettledInvoices: make(chan *channeldb.Invoice),
|
2018-04-25 07:07:30 +03:00
|
|
|
addIndex: addIndex,
|
|
|
|
settleIndex: settleIndex,
|
2018-12-20 20:54:11 +03:00
|
|
|
invoiceSubscriptionKit: invoiceSubscriptionKit{
|
|
|
|
inv: i,
|
|
|
|
ntfnQueue: queue.NewConcurrentQueue(20),
|
|
|
|
cancelChan: make(chan struct{}),
|
|
|
|
},
|
2016-10-15 05:47:10 +03:00
|
|
|
}
|
2018-04-25 07:07:30 +03:00
|
|
|
client.ntfnQueue.Start()
|
2016-10-15 05:47:10 +03:00
|
|
|
|
|
|
|
i.clientMtx.Lock()
|
|
|
|
client.id = i.nextClientID
|
|
|
|
i.nextClientID++
|
|
|
|
i.clientMtx.Unlock()
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
// Before we register this new invoice subscription, we'll launch a new
|
|
|
|
// goroutine that will proxy all notifications appended to the end of
|
|
|
|
// the concurrent queue to the two client-side channels the caller will
|
|
|
|
// feed off of.
|
|
|
|
i.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer i.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// A new invoice event has been sent by the
|
|
|
|
// invoiceRegistry! We'll figure out if this is an add
|
|
|
|
// event or a settle event, then dispatch the event to
|
|
|
|
// the client.
|
|
|
|
case ntfn := <-client.ntfnQueue.ChanOut():
|
|
|
|
invoiceEvent := ntfn.(*invoiceEvent)
|
|
|
|
|
2018-12-19 17:56:26 +03:00
|
|
|
var targetChan chan *channeldb.Invoice
|
2019-11-22 13:25:02 +03:00
|
|
|
state := invoiceEvent.invoice.State
|
2019-08-09 12:41:34 +03:00
|
|
|
switch state {
|
2018-12-19 17:56:26 +03:00
|
|
|
case channeldb.ContractOpen:
|
|
|
|
targetChan = client.NewInvoices
|
|
|
|
case channeldb.ContractSettled:
|
2018-04-25 07:07:30 +03:00
|
|
|
targetChan = client.SettledInvoices
|
2018-12-19 17:56:26 +03:00
|
|
|
default:
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Errorf("unknown invoice "+
|
2019-08-09 12:41:34 +03:00
|
|
|
"state: %v", state)
|
2018-12-19 17:56:26 +03:00
|
|
|
|
|
|
|
continue
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case targetChan <- invoiceEvent.invoice:
|
|
|
|
|
|
|
|
case <-client.cancelChan:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-client.cancelChan:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-20 06:31:52 +03:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
// Query the database to see if based on the provided addIndex and
|
|
|
|
// settledIndex we need to deliver any backlog notifications.
|
|
|
|
err := i.deliverBacklogEvents(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
select {
|
|
|
|
case i.newSubscriptions <- client:
|
|
|
|
case <-i.quit:
|
2020-05-20 06:31:38 +03:00
|
|
|
return nil, ErrShuttingDown
|
2018-04-25 07:07:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 06:31:38 +03:00
|
|
|
return client, nil
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
2018-12-20 20:54:11 +03:00
|
|
|
|
|
|
|
// SubscribeSingleInvoice returns an SingleInvoiceSubscription which allows the
|
|
|
|
// caller to receive async notifications for a specific invoice.
|
|
|
|
func (i *InvoiceRegistry) SubscribeSingleInvoice(
|
2019-08-09 13:22:53 +03:00
|
|
|
hash lntypes.Hash) (*SingleInvoiceSubscription, error) {
|
2018-12-20 20:54:11 +03:00
|
|
|
|
|
|
|
client := &SingleInvoiceSubscription{
|
|
|
|
Updates: make(chan *channeldb.Invoice),
|
|
|
|
invoiceSubscriptionKit: invoiceSubscriptionKit{
|
|
|
|
inv: i,
|
|
|
|
ntfnQueue: queue.NewConcurrentQueue(20),
|
|
|
|
cancelChan: make(chan struct{}),
|
|
|
|
},
|
2020-05-22 01:37:10 +03:00
|
|
|
invoiceRef: channeldb.InvoiceRefByHash(hash),
|
2018-12-20 20:54:11 +03:00
|
|
|
}
|
|
|
|
client.ntfnQueue.Start()
|
|
|
|
|
|
|
|
i.clientMtx.Lock()
|
|
|
|
client.id = i.nextClientID
|
|
|
|
i.nextClientID++
|
|
|
|
i.clientMtx.Unlock()
|
|
|
|
|
|
|
|
// Before we register this new invoice subscription, we'll launch a new
|
|
|
|
// goroutine that will proxy all notifications appended to the end of
|
|
|
|
// the concurrent queue to the two client-side channels the caller will
|
|
|
|
// feed off of.
|
|
|
|
i.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer i.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// A new invoice event has been sent by the
|
|
|
|
// invoiceRegistry. We will dispatch the event to the
|
|
|
|
// client.
|
|
|
|
case ntfn := <-client.ntfnQueue.ChanOut():
|
|
|
|
invoiceEvent := ntfn.(*invoiceEvent)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case client.Updates <- invoiceEvent.invoice:
|
|
|
|
|
|
|
|
case <-client.cancelChan:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-client.cancelChan:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-09 13:22:53 +03:00
|
|
|
// Within the lock, we both query the invoice state and pass the client
|
|
|
|
// subscription to the invoiceEvents channel. This is to make sure that
|
|
|
|
// the client receives a consistent stream of events.
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
err := i.deliverSingleBacklogEvents(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
select {
|
2019-08-09 13:22:53 +03:00
|
|
|
case i.invoiceEvents <- client:
|
2018-12-20 20:54:11 +03:00
|
|
|
case <-i.quit:
|
2019-08-09 13:22:53 +03:00
|
|
|
return nil, ErrShuttingDown
|
2018-12-20 20:54:11 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 13:22:53 +03:00
|
|
|
return client, nil
|
2018-12-20 20:54:11 +03:00
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// notifyHodlSubscribers sends out the htlc resolution to all current
|
|
|
|
// subscribers.
|
|
|
|
func (i *InvoiceRegistry) notifyHodlSubscribers(htlcResolution HtlcResolution) {
|
2020-02-06 20:35:10 +03:00
|
|
|
subscribers, ok := i.hodlSubscriptions[htlcResolution.CircuitKey()]
|
2019-02-11 14:01:05 +03:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all interested subscribers and remove subscription from both
|
|
|
|
// maps. The subscription can be removed as there only ever will be a
|
|
|
|
// single resolution for each hash.
|
|
|
|
for subscriber := range subscribers {
|
|
|
|
select {
|
2019-12-20 13:25:07 +03:00
|
|
|
case subscriber <- htlcResolution:
|
2019-02-11 14:01:05 +03:00
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-14 22:11:34 +03:00
|
|
|
delete(
|
|
|
|
i.hodlReverseSubscriptions[subscriber],
|
2020-02-06 20:35:10 +03:00
|
|
|
htlcResolution.CircuitKey(),
|
2019-08-14 22:11:34 +03:00
|
|
|
)
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 20:35:10 +03:00
|
|
|
delete(i.hodlSubscriptions, htlcResolution.CircuitKey())
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// hodlSubscribe adds a new invoice subscription.
|
|
|
|
func (i *InvoiceRegistry) hodlSubscribe(subscriber chan<- interface{},
|
2019-08-14 22:11:34 +03:00
|
|
|
circuitKey channeldb.CircuitKey) {
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-08-14 22:11:34 +03:00
|
|
|
log.Debugf("Hodl subscribe for %v", circuitKey)
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-08-14 22:11:34 +03:00
|
|
|
subscriptions, ok := i.hodlSubscriptions[circuitKey]
|
2019-02-11 14:01:05 +03:00
|
|
|
if !ok {
|
|
|
|
subscriptions = make(map[chan<- interface{}]struct{})
|
2019-08-14 22:11:34 +03:00
|
|
|
i.hodlSubscriptions[circuitKey] = subscriptions
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
subscriptions[subscriber] = struct{}{}
|
|
|
|
|
|
|
|
reverseSubscriptions, ok := i.hodlReverseSubscriptions[subscriber]
|
|
|
|
if !ok {
|
2019-08-14 22:11:34 +03:00
|
|
|
reverseSubscriptions = make(map[channeldb.CircuitKey]struct{})
|
2019-02-11 14:01:05 +03:00
|
|
|
i.hodlReverseSubscriptions[subscriber] = reverseSubscriptions
|
|
|
|
}
|
2019-08-14 22:11:34 +03:00
|
|
|
reverseSubscriptions[circuitKey] = struct{}{}
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HodlUnsubscribeAll cancels the subscription.
|
|
|
|
func (i *InvoiceRegistry) HodlUnsubscribeAll(subscriber chan<- interface{}) {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
hashes := i.hodlReverseSubscriptions[subscriber]
|
|
|
|
for hash := range hashes {
|
|
|
|
delete(i.hodlSubscriptions[hash], subscriber)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(i.hodlReverseSubscriptions, subscriber)
|
|
|
|
}
|