2018-10-22 06:36:56 +03:00
|
|
|
package channelnotifier
|
|
|
|
|
|
|
|
import (
|
2019-06-06 20:15:11 +03:00
|
|
|
"sync"
|
2018-10-22 06:36:56 +03:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/subscribe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChannelNotifier is a subsystem which all active, inactive, and closed channel
|
|
|
|
// events pipe through. It takes subscriptions for its events, and whenever
|
|
|
|
// it receives a new event it notifies its subscribers over the proper channel.
|
|
|
|
type ChannelNotifier struct {
|
2019-06-06 20:15:11 +03:00
|
|
|
started sync.Once
|
|
|
|
stopped sync.Once
|
2018-10-22 06:36:56 +03:00
|
|
|
|
|
|
|
ntfnServer *subscribe.Server
|
|
|
|
|
|
|
|
chanDB *channeldb.DB
|
|
|
|
}
|
|
|
|
|
2019-09-29 13:13:01 +03:00
|
|
|
// PendingOpenChannelEvent represents a new event where a new channel has
|
|
|
|
// entered a pending open state.
|
|
|
|
type PendingOpenChannelEvent struct {
|
2020-02-10 13:02:14 +03:00
|
|
|
// ChannelPoint is the channel outpoint for the new channel.
|
2019-09-29 13:13:01 +03:00
|
|
|
ChannelPoint *wire.OutPoint
|
2020-02-10 13:02:14 +03:00
|
|
|
|
|
|
|
// PendingChannel is the channel configuration for the newly created
|
|
|
|
// channel. This might not have been persisted to the channel DB yet
|
|
|
|
// because we are still waiting for the final message from the remote
|
|
|
|
// peer.
|
|
|
|
PendingChannel *channeldb.OpenChannel
|
2019-09-29 13:13:01 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 06:36:56 +03:00
|
|
|
// OpenChannelEvent represents a new event where a channel goes from pending
|
|
|
|
// open to open.
|
|
|
|
type OpenChannelEvent struct {
|
|
|
|
// Channel is the channel that has become open.
|
|
|
|
Channel *channeldb.OpenChannel
|
|
|
|
}
|
|
|
|
|
2020-03-07 06:38:39 +03:00
|
|
|
// ActiveLinkEvent represents a new event where the link becomes active in the
|
|
|
|
// switch. This happens before the ActiveChannelEvent.
|
|
|
|
type ActiveLinkEvent struct {
|
|
|
|
// ChannelPoint is the channel point for the newly active channel.
|
|
|
|
ChannelPoint *wire.OutPoint
|
|
|
|
}
|
|
|
|
|
2018-10-22 06:36:56 +03:00
|
|
|
// ActiveChannelEvent represents a new event where a channel becomes active.
|
|
|
|
type ActiveChannelEvent struct {
|
|
|
|
// ChannelPoint is the channelpoint for the newly active channel.
|
|
|
|
ChannelPoint *wire.OutPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// InactiveChannelEvent represents a new event where a channel becomes inactive.
|
|
|
|
type InactiveChannelEvent struct {
|
|
|
|
// ChannelPoint is the channelpoint for the newly inactive channel.
|
|
|
|
ChannelPoint *wire.OutPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClosedChannelEvent represents a new event where a channel becomes closed.
|
|
|
|
type ClosedChannelEvent struct {
|
|
|
|
// CloseSummary is the summary of the channel close that has occurred.
|
|
|
|
CloseSummary *channeldb.ChannelCloseSummary
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new channel notifier. The ChannelNotifier gets channel
|
|
|
|
// events from peers and from the chain arbitrator, and dispatches them to
|
|
|
|
// its clients.
|
|
|
|
func New(chanDB *channeldb.DB) *ChannelNotifier {
|
|
|
|
return &ChannelNotifier{
|
|
|
|
ntfnServer: subscribe.NewServer(),
|
|
|
|
chanDB: chanDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the ChannelNotifier and all goroutines it needs to carry out its task.
|
|
|
|
func (c *ChannelNotifier) Start() error {
|
2019-06-06 20:15:11 +03:00
|
|
|
var err error
|
|
|
|
c.started.Do(func() {
|
2019-06-12 15:38:35 +03:00
|
|
|
log.Trace("ChannelNotifier starting")
|
2019-06-06 20:15:11 +03:00
|
|
|
err = c.ntfnServer.Start()
|
|
|
|
})
|
|
|
|
return err
|
2018-10-22 06:36:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals the notifier for a graceful shutdown.
|
2019-01-21 14:11:19 +03:00
|
|
|
func (c *ChannelNotifier) Stop() error {
|
|
|
|
var err error
|
2019-06-06 20:15:11 +03:00
|
|
|
c.stopped.Do(func() {
|
2019-01-21 14:11:19 +03:00
|
|
|
err = c.ntfnServer.Stop()
|
2019-06-06 20:15:11 +03:00
|
|
|
})
|
2019-01-21 14:11:19 +03:00
|
|
|
return err
|
2018-10-22 06:36:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeChannelEvents returns a subscribe.Client that will receive updates
|
2019-08-08 20:39:38 +03:00
|
|
|
// any time the Server is made aware of a new event. The subscription provides
|
|
|
|
// channel events from the point of subscription onwards.
|
|
|
|
//
|
|
|
|
// TODO(carlaKC): update to allow subscriptions to specify a block height from
|
|
|
|
// which we would like to subscribe to events.
|
2018-10-22 06:36:56 +03:00
|
|
|
func (c *ChannelNotifier) SubscribeChannelEvents() (*subscribe.Client, error) {
|
|
|
|
return c.ntfnServer.Subscribe()
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:02:14 +03:00
|
|
|
// NotifyPendingOpenChannelEvent notifies the channelEventNotifier goroutine
|
|
|
|
// that a new channel is pending. The pending channel is passed as a parameter
|
|
|
|
// instead of read from the database because it might not yet have been
|
|
|
|
// persisted to the DB because we still wait for the final message from the
|
|
|
|
// remote peer.
|
|
|
|
func (c *ChannelNotifier) NotifyPendingOpenChannelEvent(chanPoint wire.OutPoint,
|
|
|
|
pendingChan *channeldb.OpenChannel) {
|
|
|
|
|
|
|
|
event := PendingOpenChannelEvent{
|
|
|
|
ChannelPoint: &chanPoint,
|
|
|
|
PendingChannel: pendingChan,
|
|
|
|
}
|
2019-09-29 13:13:01 +03:00
|
|
|
|
|
|
|
if err := c.ntfnServer.SendUpdate(event); err != nil {
|
|
|
|
log.Warnf("Unable to send pending open channel update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 06:36:56 +03:00
|
|
|
// NotifyOpenChannelEvent notifies the channelEventNotifier goroutine that a
|
|
|
|
// channel has gone from pending open to open.
|
|
|
|
func (c *ChannelNotifier) NotifyOpenChannelEvent(chanPoint wire.OutPoint) {
|
|
|
|
|
|
|
|
// Fetch the relevant channel from the database.
|
|
|
|
channel, err := c.chanDB.FetchChannel(chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to fetch open channel from the db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the open event to all channel event subscribers.
|
|
|
|
event := OpenChannelEvent{Channel: channel}
|
|
|
|
if err := c.ntfnServer.SendUpdate(event); err != nil {
|
|
|
|
log.Warnf("Unable to send open channel update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyClosedChannelEvent notifies the channelEventNotifier goroutine that a
|
|
|
|
// channel has closed.
|
|
|
|
func (c *ChannelNotifier) NotifyClosedChannelEvent(chanPoint wire.OutPoint) {
|
|
|
|
// Fetch the relevant closed channel from the database.
|
|
|
|
closeSummary, err := c.chanDB.FetchClosedChannel(&chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to fetch closed channel summary from the db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the closed event to all channel event subscribers.
|
|
|
|
event := ClosedChannelEvent{CloseSummary: closeSummary}
|
|
|
|
if err := c.ntfnServer.SendUpdate(event); err != nil {
|
|
|
|
log.Warnf("Unable to send closed channel update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 06:38:39 +03:00
|
|
|
// NotifyActiveLinkEvent notifies the channelEventNotifier goroutine that a
|
|
|
|
// link has been added to the switch.
|
|
|
|
func (c *ChannelNotifier) NotifyActiveLinkEvent(chanPoint wire.OutPoint) {
|
|
|
|
event := ActiveLinkEvent{ChannelPoint: &chanPoint}
|
|
|
|
if err := c.ntfnServer.SendUpdate(event); err != nil {
|
|
|
|
log.Warnf("Unable to send active link update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 06:36:56 +03:00
|
|
|
// NotifyActiveChannelEvent notifies the channelEventNotifier goroutine that a
|
|
|
|
// channel is active.
|
|
|
|
func (c *ChannelNotifier) NotifyActiveChannelEvent(chanPoint wire.OutPoint) {
|
|
|
|
event := ActiveChannelEvent{ChannelPoint: &chanPoint}
|
|
|
|
if err := c.ntfnServer.SendUpdate(event); err != nil {
|
|
|
|
log.Warnf("Unable to send active channel update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyInactiveChannelEvent notifies the channelEventNotifier goroutine that a
|
|
|
|
// channel is inactive.
|
|
|
|
func (c *ChannelNotifier) NotifyInactiveChannelEvent(chanPoint wire.OutPoint) {
|
|
|
|
event := InactiveChannelEvent{ChannelPoint: &chanPoint}
|
|
|
|
if err := c.ntfnServer.SendUpdate(event); err != nil {
|
|
|
|
log.Warnf("Unable to send inactive channel update: %v", err)
|
|
|
|
}
|
|
|
|
}
|