2019-08-08 20:39:38 +03:00
|
|
|
// Package chanfitness monitors the behaviour of channels to provide insight
|
|
|
|
// into the health and performance of a channel. This is achieved by maintaining
|
|
|
|
// an event store which tracks events for each channel.
|
|
|
|
//
|
|
|
|
// Lifespan: the period that the channel has been known to the scoring system.
|
|
|
|
// Note that lifespan may not equal the channel's full lifetime because data is
|
|
|
|
// not currently persisted.
|
|
|
|
//
|
|
|
|
// Uptime: the total time within a given period that the channel's remote peer
|
|
|
|
// has been online.
|
|
|
|
package chanfitness
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-12-17 18:36:28 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2019-08-08 20:39:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/channelnotifier"
|
2020-09-08 14:47:15 +03:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2019-08-08 20:39:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/peernotifier"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
"github.com/lightningnetwork/lnd/subscribe"
|
2020-09-08 14:47:21 +03:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// FlapCountFlushRate determines how often we write peer total flap
|
|
|
|
// count to disk.
|
|
|
|
FlapCountFlushRate = time.Hour
|
2019-08-08 20:39:38 +03:00
|
|
|
)
|
|
|
|
|
2019-12-17 18:36:21 +03:00
|
|
|
var (
|
2020-09-08 14:47:12 +03:00
|
|
|
// errShuttingDown is returned when the store cannot respond to a query
|
|
|
|
// because it has received the shutdown signal.
|
2019-12-17 18:36:21 +03:00
|
|
|
errShuttingDown = errors.New("channel event store shutting down")
|
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// ErrChannelNotFound is returned when a query is made for a channel
|
|
|
|
// that the event store does not have knowledge of.
|
2019-12-17 18:36:21 +03:00
|
|
|
ErrChannelNotFound = errors.New("channel not found in event store")
|
2020-09-08 14:47:18 +03:00
|
|
|
|
|
|
|
// ErrPeerNotFound is returned when a query is made for a channel
|
|
|
|
// that has a peer that the event store is not currently tracking.
|
|
|
|
ErrPeerNotFound = errors.New("peer not found in event store")
|
2019-12-17 18:36:21 +03:00
|
|
|
)
|
2019-08-08 20:39:38 +03:00
|
|
|
|
|
|
|
// ChannelEventStore maintains a set of event logs for the node's channels to
|
|
|
|
// provide insight into the performance and health of channels.
|
|
|
|
type ChannelEventStore struct {
|
|
|
|
cfg *Config
|
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
// peers tracks all of our currently monitored peers and their channels.
|
|
|
|
peers map[route.Vertex]peerMonitor
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
// chanInfoRequests serves requests for information about our channel.
|
|
|
|
chanInfoRequests chan channelInfoRequest
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-08-27 10:20:46 +03:00
|
|
|
// peerRequests serves requests for information about a peer.
|
|
|
|
peerRequests chan peerRequest
|
|
|
|
|
2019-08-08 20:39:38 +03:00
|
|
|
quit chan struct{}
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config provides the event store with functions required to monitor channel
|
|
|
|
// activity. All elements of the config must be non-nil for the event store to
|
|
|
|
// operate.
|
|
|
|
type Config struct {
|
2020-09-08 14:47:12 +03:00
|
|
|
// SubscribeChannelEvents provides a subscription client which provides
|
|
|
|
// a stream of channel events.
|
2020-09-08 14:47:13 +03:00
|
|
|
SubscribeChannelEvents func() (subscribe.Subscription, error)
|
2019-08-08 20:39:38 +03:00
|
|
|
|
|
|
|
// SubscribePeerEvents provides a subscription client which provides a
|
|
|
|
// stream of peer online/offline events.
|
2020-09-08 14:47:13 +03:00
|
|
|
SubscribePeerEvents func() (subscribe.Subscription, error)
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// GetOpenChannels provides a list of existing open channels which is
|
|
|
|
// used to populate the ChannelEventStore with a set of channels on
|
|
|
|
// startup.
|
2019-08-08 20:39:38 +03:00
|
|
|
GetOpenChannels func() ([]*channeldb.OpenChannel, error)
|
2020-09-08 14:47:15 +03:00
|
|
|
|
|
|
|
// Clock is the time source that the subsystem uses, provided here
|
|
|
|
// for ease of testing.
|
|
|
|
Clock clock.Clock
|
2020-09-08 14:47:21 +03:00
|
|
|
|
|
|
|
// WriteFlapCounts records the flap count for a set of peers on disk.
|
|
|
|
WriteFlapCount func(map[route.Vertex]*channeldb.FlapCount) error
|
|
|
|
|
|
|
|
// ReadFlapCount gets the flap count for a peer on disk.
|
|
|
|
ReadFlapCount func(route.Vertex) (*channeldb.FlapCount, error)
|
|
|
|
|
|
|
|
// FlapCountTicker is a ticker which controls how often we flush our
|
|
|
|
// peer's flap count to disk.
|
|
|
|
FlapCountTicker ticker.Ticker
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:21 +03:00
|
|
|
// peerFlapCountMap is the map used to map peers to flap counts, declared here
|
|
|
|
// to allow shorter function signatures.
|
|
|
|
type peerFlapCountMap map[route.Vertex]*channeldb.FlapCount
|
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
type channelInfoRequest struct {
|
2020-09-08 14:47:18 +03:00
|
|
|
peer route.Vertex
|
2019-12-17 18:36:28 +03:00
|
|
|
channelPoint wire.OutPoint
|
2020-09-08 14:47:17 +03:00
|
|
|
responseChan chan channelInfoResponse
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
type channelInfoResponse struct {
|
|
|
|
info *ChannelInfo
|
|
|
|
err error
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 10:20:46 +03:00
|
|
|
type peerRequest struct {
|
|
|
|
peer route.Vertex
|
|
|
|
responseChan chan peerResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerResponse struct {
|
|
|
|
flapCount int
|
|
|
|
ts *time.Time
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:39:38 +03:00
|
|
|
// NewChannelEventStore initializes an event store with the config provided.
|
|
|
|
// Note that this function does not start the main event loop, Start() must be
|
|
|
|
// called.
|
|
|
|
func NewChannelEventStore(config *Config) *ChannelEventStore {
|
|
|
|
store := &ChannelEventStore{
|
|
|
|
cfg: config,
|
2020-09-08 14:47:18 +03:00
|
|
|
peers: make(map[route.Vertex]peerMonitor),
|
2020-09-08 14:47:17 +03:00
|
|
|
chanInfoRequests: make(chan channelInfoRequest),
|
2020-08-27 10:20:46 +03:00
|
|
|
peerRequests: make(chan peerRequest),
|
2019-08-08 20:39:38 +03:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start adds all existing open channels to the event store and starts the main
|
|
|
|
// loop which records channel and peer events, and serves requests for
|
|
|
|
// information from the store. If this function fails, it cancels its existing
|
|
|
|
// subscriptions and returns an error.
|
|
|
|
func (c *ChannelEventStore) Start() error {
|
|
|
|
// Create a subscription to channel events.
|
|
|
|
channelClient, err := c.cfg.SubscribeChannelEvents()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a subscription to peer events. If an error occurs, cancel the
|
|
|
|
// existing subscription to channel events and return.
|
|
|
|
peerClient, err := c.cfg.SubscribePeerEvents()
|
|
|
|
if err != nil {
|
|
|
|
channelClient.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// cancel should be called to cancel all subscriptions if an error
|
|
|
|
// occurs.
|
2019-08-08 20:39:38 +03:00
|
|
|
cancel := func() {
|
|
|
|
channelClient.Cancel()
|
|
|
|
peerClient.Cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the existing set of channels to the event store. This is required
|
|
|
|
// because channel events will not be triggered for channels that exist
|
|
|
|
// at startup time.
|
|
|
|
channels, err := c.cfg.GetOpenChannels()
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Adding %v channels to event store", len(channels))
|
|
|
|
|
|
|
|
for _, ch := range channels {
|
|
|
|
peerKey, err := route.NewVertexFromBytes(
|
|
|
|
ch.IdentityPub.SerializeCompressed(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// Add existing channels to the channel store with an initial
|
|
|
|
// peer online or offline event.
|
2019-12-17 18:36:28 +03:00
|
|
|
c.addChannel(ch.FundingOutpoint, peerKey)
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a goroutine that consumes events from all subscriptions.
|
|
|
|
c.wg.Add(1)
|
|
|
|
go c.consume(&subscriptions{
|
|
|
|
channelUpdates: channelClient.Updates(),
|
|
|
|
peerUpdates: peerClient.Updates(),
|
|
|
|
cancel: cancel,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates all goroutines started by the event store.
|
|
|
|
func (c *ChannelEventStore) Stop() {
|
|
|
|
log.Info("Stopping event store")
|
|
|
|
|
|
|
|
// Stop the consume goroutine.
|
|
|
|
close(c.quit)
|
|
|
|
c.wg.Wait()
|
2020-10-27 14:28:53 +03:00
|
|
|
|
|
|
|
// Stop the ticker after the goroutine reading from it has exited, to
|
|
|
|
// avoid a race.
|
|
|
|
c.cfg.FlapCountTicker.Stop()
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
// addChannel checks whether we are already tracking a channel's peer, creates a
|
|
|
|
// new peer log to track it if we are not yet monitoring it, and adds the
|
|
|
|
// channel.
|
2019-12-17 18:36:28 +03:00
|
|
|
func (c *ChannelEventStore) addChannel(channelPoint wire.OutPoint,
|
|
|
|
peer route.Vertex) {
|
|
|
|
|
2020-09-08 14:47:21 +03:00
|
|
|
peerMonitor, err := c.getPeerMonitor(peer)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not create monitor: %v", err)
|
|
|
|
return
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
if err := peerMonitor.addChannel(channelPoint); err != nil {
|
|
|
|
log.Errorf("could not add channel: %v", err)
|
2020-01-07 17:32:47 +03:00
|
|
|
}
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:21 +03:00
|
|
|
// getPeerMonitor tries to get an existing peer monitor from our in memory list,
|
|
|
|
// and falls back to creating a new monitor if it is not currently known.
|
|
|
|
func (c *ChannelEventStore) getPeerMonitor(peer route.Vertex) (peerMonitor,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
peerMonitor, ok := c.peers[peer]
|
|
|
|
if ok {
|
|
|
|
return peerMonitor, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
flapCount int
|
|
|
|
lastFlap *time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
historicalFlap, err := c.cfg.ReadFlapCount(peer)
|
|
|
|
switch err {
|
|
|
|
// If we do not have any records for this peer we set a 0 flap count
|
|
|
|
// and timestamp.
|
|
|
|
case channeldb.ErrNoPeerBucket:
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
flapCount = int(historicalFlap.Count)
|
|
|
|
lastFlap = &historicalFlap.LastFlap
|
|
|
|
|
|
|
|
// Return if we get an unexpected error.
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerMonitor = newPeerLog(c.cfg.Clock, flapCount, lastFlap)
|
|
|
|
c.peers[peer] = peerMonitor
|
|
|
|
|
|
|
|
return peerMonitor, nil
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:39:38 +03:00
|
|
|
// closeChannel records a closed time for a channel, and returns early is the
|
2020-09-08 14:47:18 +03:00
|
|
|
// channel is not known to the event store. We log warnings (rather than errors)
|
|
|
|
// when we cannot find a peer/channel because channels that we restore from a
|
|
|
|
// static channel backup do not have their open notified, so the event store
|
|
|
|
// never learns about them, but they are closed using the regular flow so we
|
|
|
|
// will try to remove them on close. At present, we cannot easily distinguish
|
|
|
|
// between these closes and others.
|
|
|
|
func (c *ChannelEventStore) closeChannel(channelPoint wire.OutPoint,
|
|
|
|
peer route.Vertex) {
|
|
|
|
|
|
|
|
peerMonitor, ok := c.peers[peer]
|
2019-08-08 20:39:38 +03:00
|
|
|
if !ok {
|
2020-09-08 14:47:18 +03:00
|
|
|
log.Warnf("peer not known to store: %v", peer)
|
2019-08-08 20:39:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
if err := peerMonitor.removeChannel(channelPoint); err != nil {
|
|
|
|
log.Warnf("could not remove channel: %v", err)
|
|
|
|
}
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
// peerEvent creates a peer monitor for a peer if we do not currently have
|
|
|
|
// one, and adds an online event to it.
|
|
|
|
func (c *ChannelEventStore) peerEvent(peer route.Vertex, online bool) {
|
2020-09-08 14:47:21 +03:00
|
|
|
peerMonitor, err := c.getPeerMonitor(peer)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not create monitor: %v", err)
|
|
|
|
return
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
2020-09-08 14:47:18 +03:00
|
|
|
|
|
|
|
peerMonitor.onlineEvent(online)
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// subscriptions abstracts away from subscription clients to allow for mocking.
|
|
|
|
type subscriptions struct {
|
|
|
|
channelUpdates <-chan interface{}
|
|
|
|
peerUpdates <-chan interface{}
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// consume is the event store's main loop. It consumes subscriptions to update
|
|
|
|
// the event store with channel and peer events, and serves requests for channel
|
|
|
|
// uptime and lifespan.
|
|
|
|
func (c *ChannelEventStore) consume(subscriptions *subscriptions) {
|
2020-09-08 14:47:21 +03:00
|
|
|
// Start our flap count ticker.
|
|
|
|
c.cfg.FlapCountTicker.Resume()
|
|
|
|
|
|
|
|
// On exit, we will cancel our subscriptions and write our most recent
|
|
|
|
// flap counts to disk. This ensures that we have consistent data in
|
|
|
|
// the case of a graceful shutdown. If we do not shutdown gracefully,
|
|
|
|
// our worst case is data from our last flap count tick (1H).
|
|
|
|
defer func() {
|
|
|
|
subscriptions.cancel()
|
|
|
|
|
|
|
|
if err := c.recordFlapCount(); err != nil {
|
|
|
|
log.Errorf("error recording flap on shutdown: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.wg.Done()
|
|
|
|
}()
|
2019-08-08 20:39:38 +03:00
|
|
|
|
|
|
|
// Consume events until the channel is closed.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Process channel opened and closed events.
|
|
|
|
case e := <-subscriptions.channelUpdates:
|
|
|
|
switch event := e.(type) {
|
2020-09-08 14:47:12 +03:00
|
|
|
// A new channel has been opened, we must add the
|
|
|
|
// channel to the store and record a channel open event.
|
2019-08-08 20:39:38 +03:00
|
|
|
case channelnotifier.OpenChannelEvent:
|
2020-09-08 14:47:12 +03:00
|
|
|
compressed := event.Channel.IdentityPub.SerializeCompressed()
|
2019-08-08 20:39:38 +03:00
|
|
|
peerKey, err := route.NewVertexFromBytes(
|
2020-09-08 14:47:12 +03:00
|
|
|
compressed,
|
2019-08-08 20:39:38 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-09-08 14:47:12 +03:00
|
|
|
log.Errorf("Could not get vertex "+
|
|
|
|
"from: %v", compressed)
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
c.addChannel(
|
|
|
|
event.Channel.FundingOutpoint, peerKey,
|
|
|
|
)
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// A channel has been closed, we must remove the channel
|
|
|
|
// from the store and record a channel closed event.
|
2019-08-08 20:39:38 +03:00
|
|
|
case channelnotifier.ClosedChannelEvent:
|
2020-09-08 14:47:18 +03:00
|
|
|
compressed := event.CloseSummary.RemotePub.SerializeCompressed()
|
|
|
|
peerKey, err := route.NewVertexFromBytes(
|
|
|
|
compressed,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not get vertex "+
|
|
|
|
"from: %v", compressed)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.closeChannel(
|
|
|
|
event.CloseSummary.ChanPoint, peerKey,
|
|
|
|
)
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process peer online and offline events.
|
|
|
|
case e := <-subscriptions.peerUpdates:
|
|
|
|
switch event := e.(type) {
|
2020-09-08 14:47:12 +03:00
|
|
|
// We have reestablished a connection with our peer,
|
|
|
|
// and should record an online event for any channels
|
|
|
|
// with that peer.
|
2019-08-08 20:39:38 +03:00
|
|
|
case peernotifier.PeerOnlineEvent:
|
2020-09-08 14:47:18 +03:00
|
|
|
c.peerEvent(event.PubKey, true)
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// We have lost a connection with our peer, and should
|
|
|
|
// record an offline event for any channels with that
|
|
|
|
// peer.
|
2019-08-08 20:39:38 +03:00
|
|
|
case peernotifier.PeerOfflineEvent:
|
2020-09-08 14:47:18 +03:00
|
|
|
c.peerEvent(event.PubKey, false)
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve all requests for channel lifetime.
|
2020-09-08 14:47:17 +03:00
|
|
|
case req := <-c.chanInfoRequests:
|
|
|
|
var resp channelInfoResponse
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
resp.info, resp.err = c.getChanInfo(req)
|
2019-08-08 20:39:38 +03:00
|
|
|
req.responseChan <- resp
|
|
|
|
|
2020-08-27 10:20:46 +03:00
|
|
|
// Serve all requests for information about our peer.
|
|
|
|
case req := <-c.peerRequests:
|
|
|
|
var resp peerResponse
|
|
|
|
|
|
|
|
resp.flapCount, resp.ts, resp.err = c.flapCount(
|
|
|
|
req.peer,
|
|
|
|
)
|
|
|
|
req.responseChan <- resp
|
|
|
|
|
2020-09-08 14:47:21 +03:00
|
|
|
case <-c.cfg.FlapCountTicker.Ticks():
|
|
|
|
if err := c.recordFlapCount(); err != nil {
|
|
|
|
log.Errorf("could not record flap "+
|
|
|
|
"count: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:39:38 +03:00
|
|
|
// Exit if the store receives the signal to shutdown.
|
|
|
|
case <-c.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
// ChannelInfo provides the set of information that the event store has recorded
|
|
|
|
// for a channel.
|
|
|
|
type ChannelInfo struct {
|
|
|
|
// Lifetime is the total amount of time we have monitored the channel
|
|
|
|
// for.
|
|
|
|
Lifetime time.Duration
|
|
|
|
|
|
|
|
// Uptime is the total amount of time that the channel peer has been
|
|
|
|
// observed as online during the monitored lifespan.
|
|
|
|
Uptime time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChanInfo gets all the information we have on a channel in the event store.
|
2020-09-08 14:47:18 +03:00
|
|
|
func (c *ChannelEventStore) GetChanInfo(channelPoint wire.OutPoint,
|
|
|
|
peer route.Vertex) (*ChannelInfo, error) {
|
2019-12-17 18:36:28 +03:00
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
request := channelInfoRequest{
|
2020-09-08 14:47:18 +03:00
|
|
|
peer: peer,
|
2019-12-17 18:36:28 +03:00
|
|
|
channelPoint: channelPoint,
|
2020-09-08 14:47:17 +03:00
|
|
|
responseChan: make(chan channelInfoResponse),
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
// Send a request for the channel's information to the main event loop,
|
|
|
|
// or return early with an error if the store has already received a
|
2020-09-08 14:47:12 +03:00
|
|
|
// shutdown signal.
|
2019-08-08 20:39:38 +03:00
|
|
|
select {
|
2020-09-08 14:47:17 +03:00
|
|
|
case c.chanInfoRequests <- request:
|
2019-08-08 20:39:38 +03:00
|
|
|
case <-c.quit:
|
2020-09-08 14:47:17 +03:00
|
|
|
return nil, errShuttingDown
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:12 +03:00
|
|
|
// Return the response we receive on the response channel or exit early
|
|
|
|
// if the store is instructed to exit.
|
2019-08-08 20:39:38 +03:00
|
|
|
select {
|
|
|
|
case resp := <-request.responseChan:
|
2020-09-08 14:47:17 +03:00
|
|
|
return resp.info, resp.err
|
2019-08-08 20:39:38 +03:00
|
|
|
|
|
|
|
case <-c.quit:
|
2020-09-08 14:47:17 +03:00
|
|
|
return nil, errShuttingDown
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:17 +03:00
|
|
|
// getChanInfo collects channel information for a channel. It gets uptime over
|
|
|
|
// the full lifetime of the channel.
|
|
|
|
func (c *ChannelEventStore) getChanInfo(req channelInfoRequest) (*ChannelInfo,
|
|
|
|
error) {
|
2019-08-08 20:39:38 +03:00
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
peerMonitor, ok := c.peers[req.peer]
|
2020-09-08 14:47:17 +03:00
|
|
|
if !ok {
|
2020-09-08 14:47:18 +03:00
|
|
|
return nil, ErrPeerNotFound
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:18 +03:00
|
|
|
lifetime, uptime, err := peerMonitor.channelUptime(req.channelPoint)
|
2020-09-08 14:47:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
2020-09-08 14:47:17 +03:00
|
|
|
|
|
|
|
return &ChannelInfo{
|
2020-09-08 14:47:18 +03:00
|
|
|
Lifetime: lifetime,
|
2020-09-08 14:47:17 +03:00
|
|
|
Uptime: uptime,
|
|
|
|
}, nil
|
2019-08-08 20:39:38 +03:00
|
|
|
}
|
2020-09-08 14:47:21 +03:00
|
|
|
|
2020-08-27 10:20:46 +03:00
|
|
|
// FlapCount returns the flap count we have for a peer and the timestamp of its
|
|
|
|
// last flap. If we do not have any flaps recorded for the peer, the last flap
|
|
|
|
// timestamp will be nil.
|
|
|
|
func (c *ChannelEventStore) FlapCount(peer route.Vertex) (int, *time.Time,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
request := peerRequest{
|
|
|
|
peer: peer,
|
|
|
|
responseChan: make(chan peerResponse),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a request for the peer's information to the main event loop,
|
|
|
|
// or return early with an error if the store has already received a
|
|
|
|
// shutdown signal.
|
|
|
|
select {
|
|
|
|
case c.peerRequests <- request:
|
|
|
|
case <-c.quit:
|
|
|
|
return 0, nil, errShuttingDown
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the response we receive on the response channel or exit early
|
|
|
|
// if the store is instructed to exit.
|
|
|
|
select {
|
|
|
|
case resp := <-request.responseChan:
|
|
|
|
return resp.flapCount, resp.ts, resp.err
|
|
|
|
|
|
|
|
case <-c.quit:
|
|
|
|
return 0, nil, errShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flapCount gets our peer flap count and last flap timestamp from our in memory
|
|
|
|
// record of a peer, falling back to on disk if we are not currently tracking
|
|
|
|
// the peer. If we have no flap count recorded for the peer, a nil last flap
|
|
|
|
// time will be returned.
|
|
|
|
func (c *ChannelEventStore) flapCount(peer route.Vertex) (int, *time.Time,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
// First check whether we are tracking this peer in memory, because this
|
|
|
|
// record will have the most accurate flap count. We do not fail if we
|
|
|
|
// can't find the peer in memory, because we may have previously
|
|
|
|
// recorded its flap count on disk.
|
|
|
|
peerMonitor, ok := c.peers[peer]
|
|
|
|
if ok {
|
|
|
|
count, ts := peerMonitor.getFlapCount()
|
|
|
|
return count, ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get our flap count from the database. If this value is not
|
|
|
|
// recorded, we return a nil last flap time to indicate that we have no
|
|
|
|
// record of the peer's flap count.
|
|
|
|
flapCount, err := c.cfg.ReadFlapCount(peer)
|
|
|
|
switch err {
|
|
|
|
case channeldb.ErrNoPeerBucket:
|
|
|
|
return 0, nil, nil
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
return int(flapCount.Count), &flapCount.LastFlap, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:47:21 +03:00
|
|
|
// recordFlapCount will record our flap count for each peer that we are
|
|
|
|
// currently tracking, skipping peers that have a 0 flap count.
|
|
|
|
func (c *ChannelEventStore) recordFlapCount() error {
|
|
|
|
updates := make(peerFlapCountMap)
|
|
|
|
|
|
|
|
for peer, monitor := range c.peers {
|
|
|
|
flapCount, lastFlap := monitor.getFlapCount()
|
|
|
|
if lastFlap == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
updates[peer] = &channeldb.FlapCount{
|
|
|
|
Count: uint32(flapCount),
|
|
|
|
LastFlap: *lastFlap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("recording flap count for: %v peers", len(updates))
|
|
|
|
|
|
|
|
return c.cfg.WriteFlapCount(updates)
|
|
|
|
}
|