2018-12-20 13:57:44 +03:00
|
|
|
package invoices
|
2016-07-13 03:14:07 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-25 07:07:30 +03:00
|
|
|
"fmt"
|
2016-07-13 03:14:07 +03:00
|
|
|
"sync"
|
2018-04-25 07:07:30 +03:00
|
|
|
"sync/atomic"
|
2016-09-19 21:55:02 +03:00
|
|
|
"time"
|
2016-07-13 03:14:07 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2016-09-21 03:00:11 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2016-09-19 21:55:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
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"
|
2016-07-13 03:14:07 +03:00
|
|
|
)
|
|
|
|
|
2016-09-19 21:55:02 +03:00
|
|
|
var (
|
2018-12-20 13:57:44 +03:00
|
|
|
// DebugPre is the default debug preimage which is inserted into the
|
2016-09-19 21:55:02 +03:00
|
|
|
// invoice registry if the --debughtlc flag is activated on start up.
|
2017-01-13 08:01:50 +03:00
|
|
|
// All nodes initialized with the flag active will immediately settle
|
|
|
|
// any incoming HTLC whose rHash corresponds with the debug
|
2016-09-19 21:55:02 +03:00
|
|
|
// preimage.
|
2019-02-20 04:05:45 +03:00
|
|
|
DebugPre, _ = lntypes.MakePreimage(bytes.Repeat([]byte{1}, 32))
|
2016-07-13 03:14:07 +03:00
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
// DebugHash is the hash of the default preimage.
|
2019-01-15 13:31:22 +03:00
|
|
|
DebugHash = DebugPre.Hash()
|
2016-09-19 21:55:02 +03:00
|
|
|
)
|
2016-07-13 03:14:07 +03:00
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// HodlEvent describes how an htlc should be resolved. If HodlEvent.Preimage is
|
|
|
|
// set, the event indicates a settle event. If Preimage is nil, it is a cancel
|
|
|
|
// event.
|
|
|
|
type HodlEvent struct {
|
|
|
|
Preimage *lntypes.Preimage
|
|
|
|
Hash lntypes.Hash
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
newSubscriptions chan *InvoiceSubscription
|
|
|
|
newSingleSubscriptions chan *SingleInvoiceSubscription
|
|
|
|
subscriptionCancels chan uint32
|
|
|
|
invoiceEvents chan *invoiceEvent
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// debugInvoices is a map which stores special "debug" invoices which
|
2016-09-19 21:55:02 +03:00
|
|
|
// should be only created/used when manual tests require an invoice
|
|
|
|
// that *all* nodes are able to fully settle.
|
2019-01-15 13:31:22 +03:00
|
|
|
debugInvoices map[lntypes.Hash]*channeldb.Invoice
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2019-02-20 13:44:47 +03:00
|
|
|
// decodeFinalCltvExpiry is a function used to decode the final expiry
|
|
|
|
// value from the payment request.
|
|
|
|
decodeFinalCltvExpiry func(invoice string) (uint32, error)
|
2018-12-20 13:57:44 +03:00
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
// subscriptions is a map from a payment hash to a list of subscribers.
|
|
|
|
// It is used for efficient notification of links.
|
|
|
|
hodlSubscriptions map[lntypes.Hash]map[chan<- interface{}]struct{}
|
|
|
|
|
|
|
|
// reverseSubscriptions tracks hashes subscribed to per subscriber. This
|
|
|
|
// is used to unsubscribe from all hashes efficiently.
|
|
|
|
hodlReverseSubscriptions map[chan<- interface{}]map[lntypes.Hash]struct{}
|
|
|
|
|
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-02-20 13:44:47 +03:00
|
|
|
func NewRegistry(cdb *channeldb.DB, decodeFinalCltvExpiry func(invoice string) (
|
|
|
|
uint32, error)) *InvoiceRegistry {
|
2018-12-20 13:57:44 +03:00
|
|
|
|
|
|
|
return &InvoiceRegistry{
|
2018-12-20 20:54:11 +03:00
|
|
|
cdb: cdb,
|
|
|
|
debugInvoices: make(map[lntypes.Hash]*channeldb.Invoice),
|
|
|
|
notificationClients: make(map[uint32]*InvoiceSubscription),
|
|
|
|
singleNotificationClients: make(map[uint32]*SingleInvoiceSubscription),
|
|
|
|
newSubscriptions: make(chan *InvoiceSubscription),
|
|
|
|
newSingleSubscriptions: make(chan *SingleInvoiceSubscription),
|
|
|
|
subscriptionCancels: make(chan uint32),
|
|
|
|
invoiceEvents: make(chan *invoiceEvent, 100),
|
2019-02-11 14:01:05 +03:00
|
|
|
hodlSubscriptions: make(map[lntypes.Hash]map[chan<- interface{}]struct{}),
|
|
|
|
hodlReverseSubscriptions: make(map[chan<- interface{}]map[lntypes.Hash]struct{}),
|
2019-02-20 13:44:47 +03:00
|
|
|
decodeFinalCltvExpiry: decodeFinalCltvExpiry,
|
2018-12-20 20:54:11 +03:00
|
|
|
quit: make(chan struct{}),
|
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 {
|
2018-04-25 07:07:30 +03:00
|
|
|
i.wg.Add(1)
|
|
|
|
|
|
|
|
go i.invoiceEventNotifier()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals the registry for a graceful shutdown.
|
2018-12-20 13:57:44 +03:00
|
|
|
func (i *InvoiceRegistry) 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-19 17:56:26 +03:00
|
|
|
state channeldb.ContractState
|
2018-12-20 20:54:11 +03:00
|
|
|
hash lntypes.Hash
|
2018-04-25 07:07:30 +03:00
|
|
|
invoice *channeldb.Invoice
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoiceEventNotifier is the dedicated goroutine responsible for accepting
|
|
|
|
// new notification subscriptions, cancelling old subscriptions, and
|
|
|
|
// dispatching new invoice events.
|
2018-12-20 13:57:44 +03:00
|
|
|
func (i *InvoiceRegistry) invoiceEventNotifier() {
|
2018-04-25 07:07:30 +03:00
|
|
|
defer i.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
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:
|
|
|
|
// Before we add the client to our set of active
|
|
|
|
// clients, we'll first attempt to deliver any backlog
|
|
|
|
// invoice events.
|
|
|
|
err := i.deliverBacklogEvents(newClient)
|
|
|
|
if err != nil {
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Errorf("unable to deliver backlog invoice "+
|
2018-04-25 07:07:30 +03:00
|
|
|
"notifications: %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
// A new single invoice subscription has arrived. We'll query
|
|
|
|
// for any backlog notifications, then add it to the set of
|
|
|
|
// clients.
|
|
|
|
case newClient := <-i.newSingleSubscriptions:
|
|
|
|
err := i.deliverSingleBacklogEvents(newClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to deliver backlog invoice "+
|
|
|
|
"notifications: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("New single invoice subscription "+
|
|
|
|
"client: id=%v, hash=%v",
|
|
|
|
newClient.id, newClient.hash,
|
|
|
|
)
|
|
|
|
|
|
|
|
i.singleNotificationClients[newClient.id] = newClient
|
|
|
|
|
2018-04-25 07:07:30 +03:00
|
|
|
// 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
|
|
|
|
|
|
|
// A sub-systems has just modified the invoice state, so we'll
|
|
|
|
// dispatch notifications to all registered clients.
|
|
|
|
case event := <-i.invoiceEvents:
|
2019-01-11 13:19:16 +03:00
|
|
|
// For backwards compatibility, do not notify all
|
2019-02-11 14:01:05 +03:00
|
|
|
// invoice subscribers of cancel and accept events.
|
|
|
|
if event.state != channeldb.ContractCanceled &&
|
|
|
|
event.state != channeldb.ContractAccepted {
|
|
|
|
|
2019-01-11 13:19:16 +03:00
|
|
|
i.dispatchToClients(event)
|
|
|
|
}
|
2018-12-20 20:54:11 +03:00
|
|
|
i.dispatchToSingleClients(event)
|
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 {
|
|
|
|
if client.hash != event.hash {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case client.ntfnQueue.ChanIn() <- &invoiceEvent{
|
|
|
|
state: event.state,
|
|
|
|
invoice: event.invoice,
|
|
|
|
}:
|
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
switch {
|
|
|
|
// If we've already sent this settle event to
|
|
|
|
// the client, then we can skip this.
|
|
|
|
case event.state == channeldb.ContractSettled &&
|
|
|
|
client.settleIndex >= invoice.SettleIndex:
|
|
|
|
continue
|
|
|
|
|
|
|
|
// Similarly, if we've already sent this add to
|
|
|
|
// the client then we can skip this one.
|
|
|
|
case event.state == channeldb.ContractOpen &&
|
|
|
|
client.addIndex >= invoice.AddIndex:
|
|
|
|
continue
|
|
|
|
|
|
|
|
// These two states should never happen, but we
|
|
|
|
// log them just in case so we can detect this
|
|
|
|
// instance.
|
|
|
|
case event.state == channeldb.ContractOpen &&
|
|
|
|
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)
|
|
|
|
|
|
|
|
case event.state == channeldb.ContractSettled &&
|
|
|
|
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{
|
|
|
|
state: event.state,
|
|
|
|
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.
|
|
|
|
switch event.state {
|
|
|
|
case channeldb.ContractSettled:
|
|
|
|
client.settleIndex = invoice.SettleIndex
|
|
|
|
case channeldb.ContractOpen:
|
|
|
|
client.addIndex = invoice.AddIndex
|
|
|
|
default:
|
2019-01-11 13:19:16 +03:00
|
|
|
log.Errorf("unexpected invoice state: %v", event.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
|
|
|
// First, we'll query the database to see if based on the provided
|
|
|
|
// addIndex and settledIndex we need to deliver any backlog
|
|
|
|
// notifications.
|
|
|
|
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
|
|
|
state: channeldb.ContractOpen,
|
|
|
|
invoice: &addEvent,
|
2018-04-25 07:07:30 +03:00
|
|
|
}:
|
|
|
|
case <-i.quit:
|
|
|
|
return fmt.Errorf("registry shutting down")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
state: channeldb.ContractSettled,
|
|
|
|
invoice: &settleEvent,
|
2018-04-25 07:07:30 +03:00
|
|
|
}:
|
|
|
|
case <-i.quit:
|
|
|
|
return fmt.Errorf("registry shutting down")
|
|
|
|
}
|
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 {
|
|
|
|
|
|
|
|
invoice, err := i.cdb.LookupInvoice(client.hash)
|
|
|
|
|
|
|
|
// It is possible that the invoice does not exist yet, but the client is
|
|
|
|
// already watching it in anticipation.
|
|
|
|
if err == channeldb.ErrInvoiceNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.notify(&invoiceEvent{
|
|
|
|
hash: client.hash,
|
|
|
|
invoice: &invoice,
|
|
|
|
state: invoice.Terms.State,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 06:20:17 +03:00
|
|
|
// AddDebugInvoice adds a debug invoice for the specified amount, identified
|
2017-01-13 08:01:50 +03:00
|
|
|
// by the passed preimage. Once this invoice is added, subsystems within the
|
2018-07-03 06:20:17 +03:00
|
|
|
// daemon add/forward HTLCs that are able to obtain the proper preimage
|
|
|
|
// required for redemption in the case that we're the final destination.
|
2019-01-15 13:31:22 +03:00
|
|
|
func (i *InvoiceRegistry) AddDebugInvoice(amt btcutil.Amount,
|
|
|
|
preimage lntypes.Preimage) {
|
|
|
|
|
|
|
|
paymentHash := preimage.Hash()
|
2016-07-13 03:14:07 +03:00
|
|
|
|
2016-09-21 03:00:11 +03:00
|
|
|
invoice := &channeldb.Invoice{
|
2016-09-19 21:55:02 +03:00
|
|
|
CreationDate: time.Now(),
|
|
|
|
Terms: channeldb.ContractTerm{
|
2017-08-22 09:25:41 +03:00
|
|
|
Value: lnwire.NewMSatFromSatoshis(amt),
|
2016-09-19 21:55:02 +03:00
|
|
|
PaymentPreimage: preimage,
|
|
|
|
},
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
2016-09-21 03:00:11 +03:00
|
|
|
|
|
|
|
i.Lock()
|
|
|
|
i.debugInvoices[paymentHash] = invoice
|
2016-07-13 03:14:07 +03:00
|
|
|
i.Unlock()
|
2016-09-21 03:00:11 +03:00
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Debugf("Adding debug invoice %v", newLogClosure(func() string {
|
2016-09-21 03:00:11 +03:00
|
|
|
return spew.Sdump(invoice)
|
|
|
|
}))
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Debugf("Adding invoice %v", newLogClosure(func() string {
|
2016-09-21 03:00:11 +03:00
|
|
|
return spew.Sdump(invoice)
|
|
|
|
}))
|
|
|
|
|
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 {
|
|
|
|
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)
|
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
|
|
|
|
// then we're able to pull the funds pending within an HTLC. We'll also return
|
|
|
|
// what the expected min final CLTV delta is, pre-parsed from the payment
|
|
|
|
// request. This may be used by callers to determine if an HTLC is well formed
|
|
|
|
// according to the cltv delta.
|
|
|
|
//
|
2016-09-19 21:55:02 +03:00
|
|
|
// TODO(roasbeef): ignore if settled?
|
2019-01-15 13:31:22 +03:00
|
|
|
func (i *InvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (channeldb.Invoice, uint32, error) {
|
2016-09-19 21:55:02 +03:00
|
|
|
// First check the in-memory debug invoice index to see if this is an
|
|
|
|
// existing invoice added for debugging.
|
2016-07-13 03:14:07 +03:00
|
|
|
i.RLock()
|
2018-04-25 07:07:30 +03:00
|
|
|
debugInv, ok := i.debugInvoices[rHash]
|
2016-07-13 03:14:07 +03:00
|
|
|
i.RUnlock()
|
|
|
|
|
2016-09-19 21:55:02 +03:00
|
|
|
// If found, then simply return the invoice directly.
|
|
|
|
if ok {
|
2018-04-25 07:07:30 +03:00
|
|
|
return *debugInv, 0, nil
|
2016-09-19 21:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll check the database to see if there's an existing
|
|
|
|
// matching invoice.
|
2017-11-12 03:09:14 +03:00
|
|
|
invoice, err := i.cdb.LookupInvoice(rHash)
|
|
|
|
if err != nil {
|
2018-06-30 02:03:46 +03:00
|
|
|
return channeldb.Invoice{}, 0, err
|
2017-11-12 03:09:14 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 13:44:47 +03:00
|
|
|
expiry, err := i.decodeFinalCltvExpiry(string(invoice.PaymentRequest))
|
2018-06-30 02:03:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return channeldb.Invoice{}, 0, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:44:47 +03:00
|
|
|
return invoice, expiry, nil
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// NotifyExitHopHtlc attempts to mark an invoice as settled. If the invoice is a
|
2017-01-13 08:01:50 +03:00
|
|
|
// debug invoice, then this method is a noop as debug invoices are never fully
|
2019-02-20 14:11:15 +03:00
|
|
|
// 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-02-20 14:11:15 +03:00
|
|
|
func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
|
2019-02-11 14:01:05 +03:00
|
|
|
amtPaid lnwire.MilliSatoshi, hodlChan chan<- interface{}) (
|
|
|
|
*HodlEvent, error) {
|
2018-04-25 07:07:30 +03:00
|
|
|
|
2018-06-29 07:47:41 +03:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
log.Debugf("Settling invoice %x", rHash[:])
|
2016-09-21 03:00:11 +03:00
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
createEvent := func(preimage *lntypes.Preimage) *HodlEvent {
|
|
|
|
return &HodlEvent{
|
|
|
|
Hash: rHash,
|
|
|
|
Preimage: preimage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:55:02 +03:00
|
|
|
// First check the in-memory debug invoice index to see if this is an
|
|
|
|
// existing invoice added for debugging.
|
2019-02-20 14:11:15 +03:00
|
|
|
if invoice, ok := i.debugInvoices[rHash]; ok {
|
|
|
|
// Debug invoices are never fully settled, so we just settle the
|
|
|
|
// htlc in this case.
|
|
|
|
return createEvent(&invoice.Terms.PaymentPreimage), nil
|
2016-07-13 03:14:07 +03:00
|
|
|
}
|
2016-09-19 21:55:02 +03:00
|
|
|
|
|
|
|
// If this isn't a debug invoice, then we'll attempt to settle an
|
|
|
|
// invoice matching this rHash on disk (if one exists).
|
2019-02-11 14:01:05 +03:00
|
|
|
invoice, err := i.cdb.AcceptOrSettleInvoice(rHash, amtPaid)
|
2019-02-20 14:11:15 +03:00
|
|
|
switch err {
|
2019-01-14 14:03:26 +03:00
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// If invoice is already settled, settle htlc. This means we accept more
|
|
|
|
// payments to the same invoice hash.
|
|
|
|
case channeldb.ErrInvoiceAlreadySettled:
|
|
|
|
return createEvent(&invoice.Terms.PaymentPreimage), nil
|
2016-10-15 05:47:10 +03:00
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// If invoice is already canceled, cancel htlc.
|
|
|
|
case channeldb.ErrInvoiceAlreadyCanceled:
|
|
|
|
return createEvent(nil), nil
|
2017-01-15 05:18:08 +03:00
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
// If invoice is already accepted, add this htlc to the list of
|
|
|
|
// subscribers.
|
|
|
|
case channeldb.ErrInvoiceAlreadyAccepted:
|
|
|
|
i.hodlSubscribe(hodlChan, rHash)
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
// If this call settled the invoice, settle the htlc. Otherwise
|
|
|
|
// subscribe for a future hodl event.
|
2019-02-20 14:11:15 +03:00
|
|
|
case nil:
|
|
|
|
i.notifyClients(rHash, invoice, invoice.Terms.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
switch invoice.Terms.State {
|
|
|
|
case channeldb.ContractSettled:
|
|
|
|
return createEvent(&invoice.Terms.PaymentPreimage), nil
|
|
|
|
case channeldb.ContractAccepted:
|
|
|
|
// Subscribe to updates to this invoice.
|
|
|
|
i.hodlSubscribe(hodlChan, rHash)
|
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected invoice state %v",
|
|
|
|
invoice.Terms.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SettleHodlInvoice sets the preimage of a hodl invoice.
|
|
|
|
func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
invoice, err := i.cdb.SettleHoldInvoice(preimage)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Invoice SetPreimage %v: %v", preimage, err)
|
|
|
|
return err
|
2019-02-20 14:11:15 +03:00
|
|
|
}
|
2016-10-15 05:47:10 +03:00
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
hash := preimage.Hash()
|
|
|
|
log.Infof("Notifying clients of set preimage to %v",
|
|
|
|
invoice.Terms.PaymentPreimage)
|
|
|
|
|
|
|
|
i.notifyHodlSubscribers(HodlEvent{
|
|
|
|
Hash: hash,
|
|
|
|
Preimage: &preimage,
|
|
|
|
})
|
|
|
|
i.notifyClients(hash, invoice, invoice.Terms.State)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
log.Debugf("Canceling invoice %v", payHash)
|
|
|
|
|
|
|
|
invoice, err := i.cdb.CancelInvoice(payHash)
|
|
|
|
|
|
|
|
// Implement idempotency by returning success if the invoice was already
|
|
|
|
// canceled.
|
|
|
|
if err == channeldb.ErrInvoiceAlreadyCanceled {
|
|
|
|
log.Debugf("Invoice %v already canceled", payHash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Invoice %v canceled", payHash)
|
2019-02-11 14:01:05 +03:00
|
|
|
i.notifyHodlSubscribers(HodlEvent{
|
|
|
|
Hash: payHash,
|
|
|
|
})
|
2019-01-11 13:19:16 +03:00
|
|
|
i.notifyClients(payHash, invoice, channeldb.ContractCanceled)
|
|
|
|
|
|
|
|
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
|
|
|
state: state,
|
|
|
|
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
|
|
|
|
|
|
|
|
cancelled uint32 // To be used atomically.
|
|
|
|
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
|
|
|
|
2018-12-20 20:54:11 +03:00
|
|
|
hash lntypes.Hash
|
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() {
|
2018-04-25 07:07:30 +03:00
|
|
|
if !atomic.CompareAndSwapUint32(&i.cancelled, 0, 1) {
|
|
|
|
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:
|
|
|
|
return fmt.Errorf("registry shutting down")
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2018-12-20 13:57:44 +03:00
|
|
|
func (i *InvoiceRegistry) SubscribeNotifications(addIndex, settleIndex uint64) *InvoiceSubscription {
|
|
|
|
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
|
|
|
|
switch invoiceEvent.state {
|
|
|
|
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 "+
|
2018-12-19 17:56:26 +03:00
|
|
|
"state: %v", invoiceEvent.state)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case i.newSubscriptions <- client:
|
|
|
|
case <-i.quit:
|
|
|
|
}
|
|
|
|
|
2016-10-15 05:47:10 +03:00
|
|
|
return client
|
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(
|
|
|
|
hash lntypes.Hash) *SingleInvoiceSubscription {
|
|
|
|
|
|
|
|
client := &SingleInvoiceSubscription{
|
|
|
|
Updates: make(chan *channeldb.Invoice),
|
|
|
|
invoiceSubscriptionKit: invoiceSubscriptionKit{
|
|
|
|
inv: i,
|
|
|
|
ntfnQueue: queue.NewConcurrentQueue(20),
|
|
|
|
cancelChan: make(chan struct{}),
|
|
|
|
},
|
|
|
|
hash: hash,
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case i.newSingleSubscriptions <- client:
|
|
|
|
case <-i.quit:
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
// notifyHodlSubscribers sends out the hodl event to all current subscribers.
|
|
|
|
func (i *InvoiceRegistry) notifyHodlSubscribers(hodlEvent HodlEvent) {
|
|
|
|
subscribers, ok := i.hodlSubscriptions[hodlEvent.Hash]
|
|
|
|
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 {
|
|
|
|
case subscriber <- hodlEvent:
|
|
|
|
case <-i.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(i.hodlReverseSubscriptions[subscriber], hodlEvent.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(i.hodlSubscriptions, hodlEvent.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hodlSubscribe adds a new invoice subscription.
|
|
|
|
func (i *InvoiceRegistry) hodlSubscribe(subscriber chan<- interface{},
|
|
|
|
hash lntypes.Hash) {
|
|
|
|
|
|
|
|
log.Debugf("Hodl subscribe for %v", hash)
|
|
|
|
|
|
|
|
subscriptions, ok := i.hodlSubscriptions[hash]
|
|
|
|
if !ok {
|
|
|
|
subscriptions = make(map[chan<- interface{}]struct{})
|
|
|
|
i.hodlSubscriptions[hash] = subscriptions
|
|
|
|
}
|
|
|
|
subscriptions[subscriber] = struct{}{}
|
|
|
|
|
|
|
|
reverseSubscriptions, ok := i.hodlReverseSubscriptions[subscriber]
|
|
|
|
if !ok {
|
|
|
|
reverseSubscriptions = make(map[lntypes.Hash]struct{})
|
|
|
|
i.hodlReverseSubscriptions[subscriber] = reverseSubscriptions
|
|
|
|
}
|
|
|
|
reverseSubscriptions[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|