2019-05-29 08:57:04 +02:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2019-04-30 13:24:37 +02:00
|
|
|
"sync"
|
2019-05-29 08:57:04 +02:00
|
|
|
|
2019-04-30 13:24:37 +02:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-05-29 08:57:04 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-04-03 17:05:05 +02:00
|
|
|
"github.com/lightningnetwork/lnd/multimutex"
|
|
|
|
"github.com/lightningnetwork/lnd/queue"
|
2019-05-29 08:57:04 +02:00
|
|
|
)
|
|
|
|
|
2023-02-09 05:51:43 +01:00
|
|
|
// dbMPPayment is an interface derived from channeldb.MPPayment that is used by
|
|
|
|
// the payment lifecycle.
|
|
|
|
type dbMPPayment interface {
|
|
|
|
// GetState returns the current state of the payment.
|
|
|
|
GetState() *channeldb.MPPaymentState
|
|
|
|
|
|
|
|
// Terminated returns true if the payment is in a final state.
|
|
|
|
Terminated() bool
|
|
|
|
|
|
|
|
// GetStatus returns the current status of the payment.
|
|
|
|
GetStatus() channeldb.PaymentStatus
|
|
|
|
|
|
|
|
// NeedWaitAttempts specifies whether the payment needs to wait for the
|
|
|
|
// outcome of an attempt.
|
|
|
|
NeedWaitAttempts() (bool, error)
|
|
|
|
|
|
|
|
// GetHTLCs returns all HTLCs of this payment.
|
|
|
|
GetHTLCs() []channeldb.HTLCAttempt
|
|
|
|
|
|
|
|
// InFlightHTLCs returns all HTLCs that are in flight.
|
|
|
|
InFlightHTLCs() []channeldb.HTLCAttempt
|
|
|
|
|
2023-03-07 17:48:22 +01:00
|
|
|
// AllowMoreAttempts is used to decide whether we can safely attempt
|
|
|
|
// more HTLCs for a given payment state. Return an error if the payment
|
|
|
|
// is in an unexpected state.
|
|
|
|
AllowMoreAttempts() (bool, error)
|
2023-02-13 06:58:52 +01:00
|
|
|
|
|
|
|
// TerminalInfo returns the settled HTLC attempt or the payment's
|
|
|
|
// failure reason.
|
|
|
|
TerminalInfo() (*channeldb.HTLCAttempt, *channeldb.FailureReason)
|
2023-02-09 05:51:43 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 08:57:04 +02:00
|
|
|
// ControlTower tracks all outgoing payments made, whose primary purpose is to
|
|
|
|
// prevent duplicate payments to the same payment hash. In production, a
|
|
|
|
// persistent implementation is preferred so that tracking can survive across
|
|
|
|
// restarts. Payments are transitioned through various payment states, and the
|
|
|
|
// ControlTower interface provides access to driving the state transitions.
|
|
|
|
type ControlTower interface {
|
2022-01-13 17:29:43 +01:00
|
|
|
// This method checks that no succeeded payment exist for this payment
|
2019-05-29 08:57:04 +02:00
|
|
|
// hash.
|
|
|
|
InitPayment(lntypes.Hash, *channeldb.PaymentCreationInfo) error
|
|
|
|
|
2022-06-12 06:18:07 +02:00
|
|
|
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
|
|
|
|
// be called for a given payment whenever all inflight htlcs are
|
|
|
|
// completed, and the payment has reached a final settled state.
|
|
|
|
DeleteFailedAttempts(lntypes.Hash) error
|
|
|
|
|
2020-02-07 10:31:27 +01:00
|
|
|
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
|
|
|
|
RegisterAttempt(lntypes.Hash, *channeldb.HTLCAttemptInfo) error
|
2019-05-29 08:57:04 +02:00
|
|
|
|
2020-02-20 18:08:01 +01:00
|
|
|
// SettleAttempt marks the given attempt settled with the preimage. If
|
|
|
|
// this is a multi shard payment, this might implicitly mean the the
|
|
|
|
// full payment succeeded.
|
|
|
|
//
|
|
|
|
// After invoking this method, InitPayment should always return an
|
|
|
|
// error to prevent us from making duplicate payments to the same
|
|
|
|
// payment hash. The provided preimage is atomically saved to the DB
|
|
|
|
// for record keeping.
|
2020-05-06 15:44:36 +02:00
|
|
|
SettleAttempt(lntypes.Hash, uint64, *channeldb.HTLCSettleInfo) (
|
|
|
|
*channeldb.HTLCAttempt, error)
|
2020-02-20 18:08:01 +01:00
|
|
|
|
|
|
|
// FailAttempt marks the given payment attempt failed.
|
2020-05-06 15:44:36 +02:00
|
|
|
FailAttempt(lntypes.Hash, uint64, *channeldb.HTLCFailInfo) (
|
|
|
|
*channeldb.HTLCAttempt, error)
|
2019-05-29 08:57:04 +02:00
|
|
|
|
2020-04-01 00:13:23 +02:00
|
|
|
// FetchPayment fetches the payment corresponding to the given payment
|
|
|
|
// hash.
|
2023-02-09 05:51:43 +01:00
|
|
|
FetchPayment(paymentHash lntypes.Hash) (dbMPPayment, error)
|
2020-04-01 00:13:23 +02:00
|
|
|
|
2022-06-09 18:50:54 +02:00
|
|
|
// FailPayment transitions a payment into the Failed state, and records
|
|
|
|
// the ultimate reason the payment failed. Note that this should only
|
|
|
|
// be called when all active attempts are already failed. After
|
2020-02-20 18:08:01 +01:00
|
|
|
// invoking this method, InitPayment should return nil on its next call
|
|
|
|
// for this payment hash, allowing the user to make a subsequent
|
|
|
|
// payment.
|
2022-06-09 18:50:54 +02:00
|
|
|
FailPayment(lntypes.Hash, channeldb.FailureReason) error
|
2019-05-29 08:57:04 +02:00
|
|
|
|
|
|
|
// FetchInFlightPayments returns all payments with status InFlight.
|
2021-03-30 12:10:30 +02:00
|
|
|
FetchInFlightPayments() ([]*channeldb.MPPayment, error)
|
2019-04-30 13:24:37 +02:00
|
|
|
|
|
|
|
// SubscribePayment subscribes to updates for the payment with the given
|
2020-04-03 17:05:05 +02:00
|
|
|
// hash. A first update with the current state of the payment is always
|
|
|
|
// sent out immediately.
|
2022-09-05 13:20:38 +02:00
|
|
|
SubscribePayment(paymentHash lntypes.Hash) (ControlTowerSubscriber,
|
2019-04-30 13:24:37 +02:00
|
|
|
error)
|
2022-03-15 12:11:11 +01:00
|
|
|
|
|
|
|
// SubscribeAllPayments subscribes to updates for all payments. A first
|
|
|
|
// update with the current state of every inflight payment is always
|
|
|
|
// sent out immediately.
|
2022-09-05 13:20:38 +02:00
|
|
|
SubscribeAllPayments() (ControlTowerSubscriber, error)
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// ControlTowerSubscriber contains the state for a payment update subscriber.
|
2022-09-05 13:20:38 +02:00
|
|
|
type ControlTowerSubscriber interface {
|
2020-04-03 17:05:05 +02:00
|
|
|
// Updates is the channel over which *channeldb.MPPayment updates can be
|
|
|
|
// received.
|
2022-09-05 13:20:38 +02:00
|
|
|
Updates() <-chan interface{}
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2022-09-05 13:20:38 +02:00
|
|
|
// Close signals that the subscriber is no longer interested in updates.
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ControlTowerSubscriberImpl contains the state for a payment update
|
|
|
|
// subscriber.
|
|
|
|
type controlTowerSubscriberImpl struct {
|
|
|
|
updates <-chan interface{}
|
|
|
|
queue *queue.ConcurrentQueue
|
|
|
|
quit chan struct{}
|
2020-04-03 17:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newControlTowerSubscriber instantiates a new subscriber state object.
|
2022-09-05 13:20:38 +02:00
|
|
|
func newControlTowerSubscriber() *controlTowerSubscriberImpl {
|
2020-04-03 17:05:05 +02:00
|
|
|
// Create a queue for payment updates.
|
|
|
|
queue := queue.NewConcurrentQueue(20)
|
|
|
|
queue.Start()
|
|
|
|
|
2022-09-05 13:20:38 +02:00
|
|
|
return &controlTowerSubscriberImpl{
|
|
|
|
updates: queue.ChanOut(),
|
2020-04-03 17:05:05 +02:00
|
|
|
queue: queue,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// Close signals that the subscriber is no longer interested in updates.
|
2022-09-05 13:20:38 +02:00
|
|
|
func (s *controlTowerSubscriberImpl) Close() {
|
2020-04-03 17:05:05 +02:00
|
|
|
// Close quit channel so that any pending writes to the queue are
|
|
|
|
// cancelled.
|
|
|
|
close(s.quit)
|
2019-11-08 12:39:51 +01:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// Stop the queue goroutine so that it won't leak.
|
|
|
|
s.queue.Stop()
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 13:20:38 +02:00
|
|
|
// Updates is the channel over which *channeldb.MPPayment updates can be
|
|
|
|
// received.
|
|
|
|
func (s *controlTowerSubscriberImpl) Updates() <-chan interface{} {
|
|
|
|
return s.updates
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:57:04 +02:00
|
|
|
// controlTower is persistent implementation of ControlTower to restrict
|
|
|
|
// double payment sending.
|
|
|
|
type controlTower struct {
|
|
|
|
db *channeldb.PaymentControl
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2022-03-15 12:11:11 +01:00
|
|
|
// subscriberIndex is used to provide a unique id for each subscriber
|
|
|
|
// to all payments. This is used to easily remove the subscriber when
|
|
|
|
// necessary.
|
|
|
|
subscriberIndex uint64
|
2022-09-05 13:20:38 +02:00
|
|
|
subscribersAllPayments map[uint64]*controlTowerSubscriberImpl
|
|
|
|
subscribers map[lntypes.Hash][]*controlTowerSubscriberImpl
|
2022-03-15 12:11:11 +01:00
|
|
|
subscribersMtx sync.Mutex
|
2020-04-03 17:05:05 +02:00
|
|
|
|
|
|
|
// paymentsMtx provides synchronization on the payment level to ensure
|
|
|
|
// that no race conditions occur in between updating the database and
|
|
|
|
// sending a notification.
|
2023-06-02 02:38:37 +02:00
|
|
|
paymentsMtx *multimutex.Mutex[lntypes.Hash]
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewControlTower creates a new instance of the controlTower.
|
|
|
|
func NewControlTower(db *channeldb.PaymentControl) ControlTower {
|
|
|
|
return &controlTower{
|
2022-03-15 12:11:11 +01:00
|
|
|
db: db,
|
|
|
|
subscribersAllPayments: make(
|
2022-09-05 13:20:38 +02:00
|
|
|
map[uint64]*controlTowerSubscriberImpl,
|
2022-03-15 12:11:11 +01:00
|
|
|
),
|
2022-09-05 13:20:38 +02:00
|
|
|
subscribers: make(map[lntypes.Hash][]*controlTowerSubscriberImpl),
|
2023-06-02 02:38:37 +02:00
|
|
|
paymentsMtx: multimutex.NewMutex[lntypes.Hash](),
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitPayment checks or records the given PaymentCreationInfo with the DB,
|
|
|
|
// making sure it does not already exist as an in-flight payment. Then this
|
2023-11-15 12:45:20 +01:00
|
|
|
// method returns successfully, the payment is guaranteed to be in the
|
|
|
|
// Initiated state.
|
2019-05-29 08:57:04 +02:00
|
|
|
func (p *controlTower) InitPayment(paymentHash lntypes.Hash,
|
|
|
|
info *channeldb.PaymentCreationInfo) error {
|
|
|
|
|
2023-11-15 12:45:20 +01:00
|
|
|
err := p.db.InitPayment(paymentHash, info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take lock before querying the db to prevent missing or duplicating
|
|
|
|
// an update.
|
|
|
|
p.paymentsMtx.Lock(paymentHash)
|
|
|
|
defer p.paymentsMtx.Unlock(paymentHash)
|
|
|
|
|
|
|
|
payment, err := p.db.FetchPayment(paymentHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.notifySubscribers(paymentHash, payment)
|
|
|
|
|
|
|
|
return nil
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
|
2022-06-12 06:18:07 +02:00
|
|
|
// DeleteFailedAttempts deletes all failed htlcs if the payment was
|
|
|
|
// successfully settled.
|
|
|
|
func (p *controlTower) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
|
|
|
|
return p.db.DeleteFailedAttempts(paymentHash)
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:31:27 +01:00
|
|
|
// RegisterAttempt atomically records the provided HTLCAttemptInfo to the
|
2019-05-29 08:57:04 +02:00
|
|
|
// DB.
|
|
|
|
func (p *controlTower) RegisterAttempt(paymentHash lntypes.Hash,
|
2020-02-07 10:31:27 +01:00
|
|
|
attempt *channeldb.HTLCAttemptInfo) error {
|
2019-05-29 08:57:04 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
p.paymentsMtx.Lock(paymentHash)
|
|
|
|
defer p.paymentsMtx.Unlock(paymentHash)
|
|
|
|
|
|
|
|
payment, err := p.db.RegisterAttempt(paymentHash, attempt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify subscribers of the attempt registration.
|
|
|
|
p.notifySubscribers(paymentHash, payment)
|
|
|
|
|
|
|
|
return nil
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 18:08:01 +01:00
|
|
|
// SettleAttempt marks the given attempt settled with the preimage. If
|
|
|
|
// this is a multi shard payment, this might implicitly mean the the
|
|
|
|
// full payment succeeded.
|
|
|
|
func (p *controlTower) SettleAttempt(paymentHash lntypes.Hash,
|
2020-05-06 15:44:36 +02:00
|
|
|
attemptID uint64, settleInfo *channeldb.HTLCSettleInfo) (
|
|
|
|
*channeldb.HTLCAttempt, error) {
|
2019-05-29 08:57:04 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
p.paymentsMtx.Lock(paymentHash)
|
|
|
|
defer p.paymentsMtx.Unlock(paymentHash)
|
|
|
|
|
2020-02-20 18:08:01 +01:00
|
|
|
payment, err := p.db.SettleAttempt(paymentHash, attemptID, settleInfo)
|
2019-04-30 13:24:37 +02:00
|
|
|
if err != nil {
|
2020-05-06 15:44:36 +02:00
|
|
|
return nil, err
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify subscribers of success event.
|
2020-04-03 17:05:05 +02:00
|
|
|
p.notifySubscribers(paymentHash, payment)
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-05-06 15:44:36 +02:00
|
|
|
return payment.GetAttempt(attemptID)
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 18:08:01 +01:00
|
|
|
// FailAttempt marks the given payment attempt failed.
|
|
|
|
func (p *controlTower) FailAttempt(paymentHash lntypes.Hash,
|
2020-05-06 15:44:36 +02:00
|
|
|
attemptID uint64, failInfo *channeldb.HTLCFailInfo) (
|
|
|
|
*channeldb.HTLCAttempt, error) {
|
2020-02-20 18:08:01 +01:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
p.paymentsMtx.Lock(paymentHash)
|
|
|
|
defer p.paymentsMtx.Unlock(paymentHash)
|
|
|
|
|
|
|
|
payment, err := p.db.FailAttempt(paymentHash, attemptID, failInfo)
|
|
|
|
if err != nil {
|
2020-05-06 15:44:36 +02:00
|
|
|
return nil, err
|
2020-04-03 17:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify subscribers of failed attempt.
|
|
|
|
p.notifySubscribers(paymentHash, payment)
|
|
|
|
|
2020-05-06 15:44:36 +02:00
|
|
|
return payment.GetAttempt(attemptID)
|
2020-02-20 18:08:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 00:13:23 +02:00
|
|
|
// FetchPayment fetches the payment corresponding to the given payment hash.
|
|
|
|
func (p *controlTower) FetchPayment(paymentHash lntypes.Hash) (
|
2023-02-09 05:51:43 +01:00
|
|
|
dbMPPayment, error) {
|
2020-04-01 00:13:23 +02:00
|
|
|
|
|
|
|
return p.db.FetchPayment(paymentHash)
|
|
|
|
}
|
|
|
|
|
2022-06-09 18:50:54 +02:00
|
|
|
// FailPayment transitions a payment into the Failed state, and records the
|
|
|
|
// reason the payment failed. After invoking this method, InitPayment should
|
|
|
|
// return nil on its next call for this payment hash, allowing the switch to
|
|
|
|
// make a subsequent payment.
|
|
|
|
func (p *controlTower) FailPayment(paymentHash lntypes.Hash,
|
2019-05-29 08:57:04 +02:00
|
|
|
reason channeldb.FailureReason) error {
|
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
p.paymentsMtx.Lock(paymentHash)
|
|
|
|
defer p.paymentsMtx.Unlock(paymentHash)
|
|
|
|
|
2019-11-08 12:39:51 +01:00
|
|
|
payment, err := p.db.Fail(paymentHash, reason)
|
2019-04-30 13:24:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify subscribers of fail event.
|
2020-04-03 17:05:05 +02:00
|
|
|
p.notifySubscribers(paymentHash, payment)
|
2019-04-30 13:24:37 +02:00
|
|
|
|
|
|
|
return nil
|
2019-05-29 08:57:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchInFlightPayments returns all payments with status InFlight.
|
2021-03-30 12:10:30 +02:00
|
|
|
func (p *controlTower) FetchInFlightPayments() ([]*channeldb.MPPayment, error) {
|
2019-05-29 08:57:04 +02:00
|
|
|
return p.db.FetchInFlightPayments()
|
|
|
|
}
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// SubscribePayment subscribes to updates for the payment with the given hash. A
|
|
|
|
// first update with the current state of the payment is always sent out
|
|
|
|
// immediately.
|
2019-04-30 13:24:37 +02:00
|
|
|
func (p *controlTower) SubscribePayment(paymentHash lntypes.Hash) (
|
2022-09-05 13:20:38 +02:00
|
|
|
ControlTowerSubscriber, error) {
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// Take lock before querying the db to prevent missing or duplicating an
|
|
|
|
// update.
|
|
|
|
p.paymentsMtx.Lock(paymentHash)
|
|
|
|
defer p.paymentsMtx.Unlock(paymentHash)
|
2019-04-30 13:24:37 +02:00
|
|
|
|
|
|
|
payment, err := p.db.FetchPayment(paymentHash)
|
|
|
|
if err != nil {
|
2020-04-03 17:05:05 +02:00
|
|
|
return nil, err
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
subscriber := newControlTowerSubscriber()
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// Always write current payment state to the channel.
|
|
|
|
subscriber.queue.ChanIn() <- payment
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// Payment is currently in flight. Register this subscriber for further
|
|
|
|
// updates. Otherwise this update is the final update and the incoming
|
|
|
|
// channel can be closed. This will close the queue's outgoing channel
|
|
|
|
// when all updates have been written.
|
2022-11-17 03:28:22 +01:00
|
|
|
if !payment.Terminated() {
|
2020-04-03 17:05:05 +02:00
|
|
|
p.subscribersMtx.Lock()
|
2019-04-30 13:24:37 +02:00
|
|
|
p.subscribers[paymentHash] = append(
|
2020-04-03 17:05:05 +02:00
|
|
|
p.subscribers[paymentHash], subscriber,
|
2019-06-04 17:18:41 +02:00
|
|
|
)
|
2020-04-03 17:05:05 +02:00
|
|
|
p.subscribersMtx.Unlock()
|
|
|
|
} else {
|
|
|
|
close(subscriber.queue.ChanIn())
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
return subscriber, nil
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:11 +01:00
|
|
|
// SubscribeAllPayments subscribes to updates for all inflight payments. A first
|
|
|
|
// update with the current state of every inflight payment is always sent out
|
|
|
|
// immediately.
|
|
|
|
// Note: If payments are in-flight while starting a new subscription, the start
|
|
|
|
// of the payment stream could produce out-of-order and/or duplicate events. In
|
|
|
|
// order to get updates for every in-flight payment attempt make sure to
|
|
|
|
// subscribe to this method before initiating any payments.
|
2022-09-05 13:20:38 +02:00
|
|
|
func (p *controlTower) SubscribeAllPayments() (ControlTowerSubscriber, error) {
|
2022-03-15 12:11:11 +01:00
|
|
|
subscriber := newControlTowerSubscriber()
|
|
|
|
|
|
|
|
// Add the subscriber to the list before fetching in-flight payments, so
|
|
|
|
// no events are missed. If a payment attempt update occurs after
|
|
|
|
// appending and before fetching in-flight payments, an out-of-order
|
|
|
|
// duplicate may be produced, because it is then fetched in below call
|
|
|
|
// and notified through the subscription.
|
|
|
|
p.subscribersMtx.Lock()
|
|
|
|
p.subscribersAllPayments[p.subscriberIndex] = subscriber
|
|
|
|
p.subscriberIndex++
|
|
|
|
p.subscribersMtx.Unlock()
|
|
|
|
|
|
|
|
inflightPayments, err := p.db.FetchInFlightPayments()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for index := range inflightPayments {
|
|
|
|
// Always write current payment state to the channel.
|
|
|
|
subscriber.queue.ChanIn() <- inflightPayments[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriber, nil
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// notifySubscribers sends a final payment event to all subscribers of this
|
|
|
|
// payment. The channel will be closed after this. Note that this function must
|
|
|
|
// be executed atomically (by means of a lock) with the database update to
|
2022-01-13 17:29:43 +01:00
|
|
|
// guarantee consistency of the notifications.
|
2020-04-03 17:05:05 +02:00
|
|
|
func (p *controlTower) notifySubscribers(paymentHash lntypes.Hash,
|
|
|
|
event *channeldb.MPPayment) {
|
2019-04-30 13:24:37 +02:00
|
|
|
|
2020-04-03 17:05:05 +02:00
|
|
|
// Get all subscribers for this payment.
|
2019-04-30 13:24:37 +02:00
|
|
|
p.subscribersMtx.Lock()
|
2022-03-15 12:11:11 +01:00
|
|
|
|
|
|
|
subscribersPaymentHash, ok := p.subscribers[paymentHash]
|
|
|
|
if !ok && len(p.subscribersAllPayments) == 0 {
|
2019-04-30 13:24:37 +02:00
|
|
|
p.subscribersMtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2020-04-03 17:05:05 +02:00
|
|
|
|
|
|
|
// If the payment reached a terminal state, the subscriber list can be
|
|
|
|
// cleared. There won't be any more updates.
|
2022-11-17 03:28:22 +01:00
|
|
|
terminal := event.Terminated()
|
2020-04-03 17:05:05 +02:00
|
|
|
if terminal {
|
|
|
|
delete(p.subscribers, paymentHash)
|
|
|
|
}
|
2022-03-15 12:11:11 +01:00
|
|
|
|
|
|
|
// Copy subscribers to all payments locally while holding the lock in
|
|
|
|
// order to avoid concurrency issues while reading/writing the map.
|
2022-09-05 13:20:38 +02:00
|
|
|
subscribersAllPayments := make(map[uint64]*controlTowerSubscriberImpl)
|
2022-03-15 12:11:11 +01:00
|
|
|
for k, v := range p.subscribersAllPayments {
|
|
|
|
subscribersAllPayments[k] = v
|
|
|
|
}
|
2019-04-30 13:24:37 +02:00
|
|
|
p.subscribersMtx.Unlock()
|
|
|
|
|
2022-03-15 12:11:11 +01:00
|
|
|
// Notify all subscribers that subscribed to the current payment hash.
|
|
|
|
for _, subscriber := range subscribersPaymentHash {
|
2020-04-03 17:05:05 +02:00
|
|
|
select {
|
|
|
|
case subscriber.queue.ChanIn() <- event:
|
|
|
|
// If this event is the last, close the incoming channel
|
|
|
|
// of the queue. This will signal the subscriber that
|
|
|
|
// there won't be any more updates.
|
|
|
|
if terminal {
|
|
|
|
close(subscriber.queue.ChanIn())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If subscriber disappeared, skip notification. For further
|
|
|
|
// notifications, we'll keep skipping over this subscriber.
|
|
|
|
case <-subscriber.quit:
|
|
|
|
}
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|
2022-03-15 12:11:11 +01:00
|
|
|
|
|
|
|
// Notify all subscribers that subscribed to all payments.
|
|
|
|
for key, subscriber := range subscribersAllPayments {
|
|
|
|
select {
|
|
|
|
case subscriber.queue.ChanIn() <- event:
|
|
|
|
|
|
|
|
// If subscriber disappeared, remove it from the subscribers
|
|
|
|
// list.
|
|
|
|
case <-subscriber.quit:
|
|
|
|
p.subscribersMtx.Lock()
|
|
|
|
delete(p.subscribersAllPayments, key)
|
|
|
|
p.subscribersMtx.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 13:24:37 +02:00
|
|
|
}
|