2018-12-13 14:26:29 +03:00
|
|
|
package autopilot
|
|
|
|
|
|
|
|
import (
|
2018-12-19 16:54:55 +03:00
|
|
|
"fmt"
|
2018-12-13 14:26:29 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2020-10-02 15:49:02 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-12-13 14:26:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ManagerCfg houses a set of values and methods that is passed to the Manager
|
|
|
|
// for it to properly manage its autopilot agent.
|
|
|
|
type ManagerCfg struct {
|
|
|
|
// Self is the public key of the lnd instance. It is used to making
|
|
|
|
// sure the autopilot is not opening channels to itself.
|
|
|
|
Self *btcec.PublicKey
|
|
|
|
|
|
|
|
// PilotCfg is the config of the autopilot agent managed by the
|
|
|
|
// Manager.
|
|
|
|
PilotCfg *Config
|
|
|
|
|
|
|
|
// ChannelState is a function closure that returns the current set of
|
|
|
|
// channels managed by this node.
|
2020-10-02 14:56:43 +03:00
|
|
|
ChannelState func() ([]LocalChannel, error)
|
2018-12-13 14:26:29 +03:00
|
|
|
|
2020-10-02 15:49:02 +03:00
|
|
|
// ChannelInfo is a function closure that returns the channel managed
|
|
|
|
// by the node given by the passed channel point.
|
|
|
|
ChannelInfo func(wire.OutPoint) (*LocalChannel, error)
|
|
|
|
|
2018-12-13 14:26:29 +03:00
|
|
|
// SubscribeTransactions is used to get a subscription for transactions
|
|
|
|
// relevant to this node's wallet.
|
|
|
|
SubscribeTransactions func() (lnwallet.TransactionSubscription, error)
|
|
|
|
|
|
|
|
// SubscribeTopology is used to get a subscription for topology changes
|
|
|
|
// on the network.
|
|
|
|
SubscribeTopology func() (*routing.TopologyClient, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manager is struct that manages an autopilot agent, making it possible to
|
|
|
|
// enable and disable it at will, and hand it relevant external information.
|
|
|
|
// It implements the autopilot grpc service, which is used to get data about
|
|
|
|
// the running autopilot, and give it relevant information.
|
|
|
|
type Manager struct {
|
2019-01-23 20:15:57 +03:00
|
|
|
started sync.Once
|
|
|
|
stopped sync.Once
|
2018-12-13 14:26:29 +03:00
|
|
|
|
|
|
|
cfg *ManagerCfg
|
|
|
|
|
|
|
|
// pilot is the current autopilot agent. It will be nil if the agent is
|
|
|
|
// disabled.
|
|
|
|
pilot *Agent
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager creates a new instance of the Manager from the passed config.
|
|
|
|
func NewManager(cfg *ManagerCfg) (*Manager, error) {
|
|
|
|
return &Manager{
|
|
|
|
cfg: cfg,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the Manager.
|
|
|
|
func (m *Manager) Start() error {
|
2019-01-23 20:15:57 +03:00
|
|
|
m.started.Do(func() {})
|
2018-12-13 14:26:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the Manager. If an autopilot agent is active, it will also be
|
|
|
|
// stopped.
|
|
|
|
func (m *Manager) Stop() error {
|
2019-01-23 20:15:57 +03:00
|
|
|
m.stopped.Do(func() {
|
|
|
|
if err := m.StopAgent(); err != nil {
|
|
|
|
log.Errorf("Unable to stop pilot: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(m.quit)
|
|
|
|
m.wg.Wait()
|
|
|
|
})
|
2018-12-13 14:26:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsActive returns whether the autopilot agent is currently active.
|
|
|
|
func (m *Manager) IsActive() bool {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
return m.pilot != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartAgent creates and starts an autopilot agent from the Manager's
|
|
|
|
// config.
|
|
|
|
func (m *Manager) StartAgent() error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// Already active.
|
|
|
|
if m.pilot != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll fetch the current state of open channels from the
|
|
|
|
// database to use as initial state for the auto-pilot agent.
|
|
|
|
initialChanState, err := m.cfg.ChannelState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have all the initial dependencies, we can create the
|
|
|
|
// auto-pilot instance itself.
|
2018-12-19 16:54:56 +03:00
|
|
|
pilot, err := New(*m.cfg.PilotCfg, initialChanState)
|
2018-12-13 14:26:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:54:56 +03:00
|
|
|
if err := pilot.Start(); err != nil {
|
2018-12-13 14:26:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll need to subscribe to two things: incoming
|
|
|
|
// transactions that modify the wallet's balance, and also any graph
|
|
|
|
// topology updates.
|
|
|
|
txnSubscription, err := m.cfg.SubscribeTransactions()
|
|
|
|
if err != nil {
|
2018-12-19 16:54:56 +03:00
|
|
|
pilot.Stop()
|
2018-12-13 14:26:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
graphSubscription, err := m.cfg.SubscribeTopology()
|
|
|
|
if err != nil {
|
2018-12-19 16:54:56 +03:00
|
|
|
txnSubscription.Cancel()
|
|
|
|
pilot.Stop()
|
2018-12-13 14:26:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:54:56 +03:00
|
|
|
m.pilot = pilot
|
|
|
|
|
2018-12-13 14:26:29 +03:00
|
|
|
// We'll launch a goroutine to provide the agent with notifications
|
|
|
|
// whenever the balance of the wallet changes.
|
|
|
|
// TODO(halseth): can lead to panic if in process of shutting down.
|
|
|
|
m.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer txnSubscription.Cancel()
|
|
|
|
defer m.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-txnSubscription.ConfirmedTransactions():
|
2018-12-19 16:54:56 +03:00
|
|
|
pilot.OnBalanceChange()
|
2018-12-13 14:26:29 +03:00
|
|
|
|
|
|
|
// We won't act upon new unconfirmed transaction, as
|
|
|
|
// we'll only use confirmed outputs when funding.
|
|
|
|
// However, we will still drain this request in order
|
|
|
|
// to avoid goroutine leaks, and ensure we promptly
|
|
|
|
// read from the channel if available.
|
|
|
|
case <-txnSubscription.UnconfirmedTransactions():
|
2018-12-19 16:54:56 +03:00
|
|
|
case <-pilot.quit:
|
2018-12-13 14:26:29 +03:00
|
|
|
return
|
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
// We'll also launch a goroutine to provide the agent with
|
|
|
|
// notifications for when the graph topology controlled by the node
|
|
|
|
// changes.
|
|
|
|
m.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer graphSubscription.Cancel()
|
|
|
|
defer m.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case topChange, ok := <-graphSubscription.TopologyChanges:
|
|
|
|
// If the router is shutting down, then we will
|
|
|
|
// as well.
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, edgeUpdate := range topChange.ChannelEdgeUpdates {
|
|
|
|
// If this isn't an advertisement by
|
|
|
|
// the backing lnd node, then we'll
|
|
|
|
// continue as we only want to add
|
|
|
|
// channels that we've created
|
|
|
|
// ourselves.
|
|
|
|
if !edgeUpdate.AdvertisingNode.IsEqual(m.cfg.Self) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is indeed a channel we
|
|
|
|
// opened, then we'll convert it to the
|
|
|
|
// autopilot.Channel format, and notify
|
|
|
|
// the pilot of the new channel.
|
2020-10-02 15:49:02 +03:00
|
|
|
cp := edgeUpdate.ChanPoint
|
|
|
|
edge, err := m.cfg.ChannelInfo(cp)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to fetch "+
|
|
|
|
"channel info for %v: "+
|
|
|
|
"%v", cp, err)
|
|
|
|
continue
|
2018-12-13 14:26:29 +03:00
|
|
|
}
|
2020-10-02 15:49:02 +03:00
|
|
|
|
|
|
|
pilot.OnChannelOpen(*edge)
|
2018-12-13 14:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For each closed channel, we'll obtain
|
|
|
|
// the chanID of the closed channel and send it
|
|
|
|
// to the pilot.
|
|
|
|
for _, chanClose := range topChange.ClosedChannels {
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(
|
|
|
|
chanClose.ChanID,
|
|
|
|
)
|
|
|
|
|
2018-12-19 16:54:56 +03:00
|
|
|
pilot.OnChannelClose(chanID)
|
2018-12-13 14:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If new nodes were added to the graph, or nod
|
|
|
|
// information has changed, we'll poke autopilot
|
|
|
|
// to see if it can make use of them.
|
|
|
|
if len(topChange.NodeUpdates) > 0 {
|
2018-12-19 16:54:56 +03:00
|
|
|
pilot.OnNodeUpdates()
|
2018-12-13 14:26:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 16:54:56 +03:00
|
|
|
case <-pilot.quit:
|
2018-12-13 14:26:29 +03:00
|
|
|
return
|
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Debugf("Manager started autopilot agent")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopAgent stops any active autopilot agent.
|
|
|
|
func (m *Manager) StopAgent() error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// Not active, so we can return early.
|
|
|
|
if m.pilot == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.pilot.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to nil the current agent, indicating it is no longer
|
|
|
|
// active.
|
|
|
|
m.pilot = nil
|
|
|
|
|
|
|
|
log.Debugf("Manager stopped autopilot agent")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-19 16:54:55 +03:00
|
|
|
|
2019-03-18 15:28:07 +03:00
|
|
|
// QueryHeuristics queries the available autopilot heuristics for node scores.
|
2019-03-18 16:41:45 +03:00
|
|
|
func (m *Manager) QueryHeuristics(nodes []NodeID, localState bool) (
|
|
|
|
HeuristicScores, error) {
|
|
|
|
|
2018-12-19 16:54:55 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
n := make(map[NodeID]struct{})
|
|
|
|
for _, node := range nodes {
|
|
|
|
n[node] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Querying heuristics for %d nodes", len(n))
|
2019-03-18 16:41:45 +03:00
|
|
|
return m.queryHeuristics(n, localState)
|
2019-03-18 15:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeuristicScores is an alias for a map that maps heuristic names to a map of
|
|
|
|
// scores for pubkeys.
|
|
|
|
type HeuristicScores map[string]map[NodeID]float64
|
|
|
|
|
|
|
|
// queryHeuristics gets node scores from all available simple heuristics, and
|
|
|
|
// the agent's current active heuristic.
|
|
|
|
//
|
|
|
|
// NOTE: Must be called with the manager's lock.
|
2019-03-18 16:41:45 +03:00
|
|
|
func (m *Manager) queryHeuristics(nodes map[NodeID]struct{}, localState bool) (
|
2019-03-18 15:28:07 +03:00
|
|
|
HeuristicScores, error) {
|
|
|
|
|
2019-03-18 16:41:45 +03:00
|
|
|
// If we want to take the local state into action when querying the
|
|
|
|
// heuristics, we fetch it. If not we'll just pass an emply slice to
|
|
|
|
// the heuristic.
|
2020-10-02 14:56:43 +03:00
|
|
|
var totalChans []LocalChannel
|
2019-03-18 16:41:45 +03:00
|
|
|
var err error
|
|
|
|
if localState {
|
|
|
|
// Fetch the current set of channels.
|
|
|
|
totalChans, err = m.cfg.ChannelState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-18 15:28:07 +03:00
|
|
|
|
2019-03-18 16:41:45 +03:00
|
|
|
// If the agent is active, we can merge the channel state with
|
|
|
|
// the channels pending open.
|
|
|
|
if m.pilot != nil {
|
|
|
|
m.pilot.chanStateMtx.Lock()
|
|
|
|
m.pilot.pendingMtx.Lock()
|
|
|
|
totalChans = mergeChanState(
|
|
|
|
m.pilot.pendingOpens, m.pilot.chanState,
|
|
|
|
)
|
|
|
|
m.pilot.pendingMtx.Unlock()
|
|
|
|
m.pilot.chanStateMtx.Unlock()
|
|
|
|
}
|
2019-03-18 15:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// As channel size we'll use the maximum size.
|
|
|
|
chanSize := m.cfg.PilotCfg.Constraints.MaxChanSize()
|
|
|
|
|
|
|
|
// We'll start by getting the scores from each available sub-heuristic,
|
|
|
|
// in addition the current agent heuristic.
|
2019-09-18 14:25:12 +03:00
|
|
|
var heuristics []AttachmentHeuristic
|
|
|
|
heuristics = append(heuristics, availableHeuristics...)
|
|
|
|
heuristics = append(heuristics, m.cfg.PilotCfg.Heuristic)
|
|
|
|
|
2019-03-18 15:28:07 +03:00
|
|
|
report := make(HeuristicScores)
|
2019-09-18 14:25:12 +03:00
|
|
|
for _, h := range heuristics {
|
2019-03-18 15:28:07 +03:00
|
|
|
name := h.Name()
|
|
|
|
|
|
|
|
// If the agent heuristic is among the simple heuristics it
|
|
|
|
// might get queried more than once. As an optimization we'll
|
|
|
|
// just skip it the second time.
|
|
|
|
if _, ok := report[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := h.NodeScores(
|
|
|
|
m.cfg.PilotCfg.Graph, totalChans, chanSize, nodes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get sub score: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Heuristic \"%v\" scored %d nodes", name, len(s))
|
|
|
|
|
|
|
|
scores := make(map[NodeID]float64)
|
|
|
|
for nID, score := range s {
|
|
|
|
scores[nID] = score.Score
|
|
|
|
}
|
|
|
|
|
|
|
|
report[name] = scores
|
|
|
|
}
|
|
|
|
|
|
|
|
return report, nil
|
2018-12-19 16:54:55 +03:00
|
|
|
}
|
2019-02-14 13:37:47 +03:00
|
|
|
|
|
|
|
// SetNodeScores is used to set the scores of the given heuristic, if it is
|
|
|
|
// active, and ScoreSettable.
|
|
|
|
func (m *Manager) SetNodeScores(name string, scores map[NodeID]float64) error {
|
2019-09-18 14:28:27 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-02-14 13:37:47 +03:00
|
|
|
// It must be ScoreSettable to be available for external
|
|
|
|
// scores.
|
|
|
|
s, ok := m.cfg.PilotCfg.Heuristic.(ScoreSettable)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("current heuristic doesn't support " +
|
|
|
|
"external scoring")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heuristic was found, set its node scores.
|
|
|
|
applied, err := s.SetNodeScores(name, scores)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !applied {
|
|
|
|
return fmt.Errorf("heuristic with name %v not found", name)
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:28:27 +03:00
|
|
|
// If the autopilot agent is active, notify about the updated
|
|
|
|
// heuristic.
|
|
|
|
if m.pilot != nil {
|
|
|
|
m.pilot.OnHeuristicUpdate(m.cfg.PilotCfg.Heuristic)
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:37:47 +03:00
|
|
|
return nil
|
|
|
|
}
|