github.com/decred/dcrlnd@v0.7.6/autopilot/interface.go (about)

     1  package autopilot
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
     7  	"github.com/decred/dcrd/dcrutil/v4"
     8  	"github.com/decred/dcrd/wire"
     9  	"github.com/decred/dcrlnd/lnwire"
    10  )
    11  
    12  // DefaultConfTarget is the default confirmation target for autopilot channels.
    13  // TODO(halseth): possibly make dynamic, going aggressive->lax as more channels
    14  // are opened.
    15  const DefaultConfTarget = 3
    16  
    17  // Node is an interface which represents n abstract vertex within the
    18  // channel graph. All nodes should have at least a single edge to/from them
    19  // within the graph.
    20  //
    21  // TODO(roasbeef): combine with routing.ChannelGraphSource
    22  type Node interface {
    23  	// PubKey is the identity public key of the node. This will be used to
    24  	// attempt to target a node for channel opening by the main autopilot
    25  	// agent. The key will be returned in serialized compressed format.
    26  	PubKey() [33]byte
    27  
    28  	// Addrs returns a slice of publicly reachable public TCP addresses
    29  	// that the peer is known to be listening on.
    30  	Addrs() []net.Addr
    31  
    32  	// ForEachChannel is a higher-order function that will be used to
    33  	// iterate through all edges emanating from/to the target node. For
    34  	// each active channel, this function should be called with the
    35  	// populated ChannelEdge that describes the active channel.
    36  	ForEachChannel(func(ChannelEdge) error) error
    37  }
    38  
    39  // LocalChannel is a simple struct which contains relevant details of a
    40  // particular channel the local node has. The fields in this struct may be used
    41  // as signals for various AttachmentHeuristic implementations.
    42  type LocalChannel struct {
    43  	// ChanID is the short channel ID for this channel as defined within
    44  	// BOLT-0007.
    45  	ChanID lnwire.ShortChannelID
    46  
    47  	// Balance is the local balance of the channel expressed in satoshis.
    48  	Balance dcrutil.Amount
    49  
    50  	// Node is the peer that this channel has been established with.
    51  	Node NodeID
    52  
    53  	// TODO(roasbeef): also add other traits?
    54  	//  * fee, timelock, etc
    55  }
    56  
    57  // ChannelEdge is a struct that holds details concerning a channel, but also
    58  // contains a reference to the Node that this channel connects to as a directed
    59  // edge within the graph. The existence of this reference to the connected node
    60  // will allow callers to traverse the graph in an object-oriented manner.
    61  type ChannelEdge struct {
    62  	// ChanID is the short channel ID for this channel as defined within
    63  	// BOLT-0007.
    64  	ChanID lnwire.ShortChannelID
    65  
    66  	// Capacity is the capacity of the channel expressed in satoshis.
    67  	Capacity dcrutil.Amount
    68  
    69  	// Peer is the peer that this channel creates an edge to in the channel
    70  	// graph.
    71  	Peer Node
    72  }
    73  
    74  // ChannelGraph in an interface that represents a traversable channel graph.
    75  // The autopilot agent will use this interface as its source of graph traits in
    76  // order to make decisions concerning which channels should be opened, and to
    77  // whom.
    78  //
    79  // TODO(roasbeef): abstract??
    80  type ChannelGraph interface {
    81  	// ForEachNode is a higher-order function that should be called once
    82  	// for each connected node within the channel graph. If the passed
    83  	// callback returns an error, then execution should be terminated.
    84  	ForEachNode(func(Node) error) error
    85  }
    86  
    87  // NodeScore is a tuple mapping a NodeID to a score indicating the preference
    88  // of opening a channel with it.
    89  type NodeScore struct {
    90  	// NodeID is the serialized compressed pubkey of the node that is being
    91  	// scored.
    92  	NodeID NodeID
    93  
    94  	// Score is the score given by the heuristic for opening a channel of
    95  	// the given size to this node.
    96  	Score float64
    97  }
    98  
    99  // AttachmentDirective describes a channel attachment proscribed by an
   100  // AttachmentHeuristic. It details to which node a channel should be created
   101  // to, and also the parameters which should be used in the channel creation.
   102  type AttachmentDirective struct {
   103  	// NodeID is the serialized compressed pubkey of the target node for
   104  	// this attachment directive. It can be identified by its public key,
   105  	// and therefore can be used along with a ChannelOpener implementation
   106  	// to execute the directive.
   107  	NodeID NodeID
   108  
   109  	// ChanAmt is the size of the channel that should be opened, expressed
   110  	// in base units.
   111  	ChanAmt dcrutil.Amount
   112  
   113  	// Addrs is a list of addresses that the target peer may be reachable
   114  	// at.
   115  	Addrs []net.Addr
   116  }
   117  
   118  // AttachmentHeuristic is one of the primary interfaces within this package.
   119  // Implementations of this interface will be used to implement a control system
   120  // which automatically regulates channels of a particular agent, attempting to
   121  // optimize channels opened/closed based on various heuristics. The purpose of
   122  // the interface is to allow an auto-pilot agent to decide if it needs more
   123  // channels, and if so, which exact channels should be opened.
   124  type AttachmentHeuristic interface {
   125  	// Name returns the name of this heuristic.
   126  	Name() string
   127  
   128  	// NodeScores is a method that given the current channel graph and
   129  	// current set of local channels, scores the given nodes according to
   130  	// the preference of opening a channel of the given size with them. The
   131  	// returned channel candidates maps the NodeID to a NodeScore for the
   132  	// node.
   133  	//
   134  	// The returned scores will be in the range [0, 1.0], where 0 indicates
   135  	// no improvement in connectivity if a channel is opened to this node,
   136  	// while 1.0 is the maximum possible improvement in connectivity. The
   137  	// implementation of this interface must return scores in this range to
   138  	// properly allow the autopilot agent to make a reasonable choice based
   139  	// on the score from multiple heuristics.
   140  	//
   141  	// NOTE: A NodeID not found in the returned map is implicitly given a
   142  	// score of 0.
   143  	NodeScores(g ChannelGraph, chans []LocalChannel,
   144  		chanSize dcrutil.Amount, nodes map[NodeID]struct{}) (
   145  		map[NodeID]*NodeScore, error)
   146  }
   147  
   148  // NodeMetric is a common interface for all graph metrics that are not
   149  // directly used as autopilot node scores but may be used in compositional
   150  // heuristics or statistical information exposed to users.
   151  type NodeMetric interface {
   152  	// Name returns the unique name of this metric.
   153  	Name() string
   154  
   155  	// Refresh refreshes the metric values based on the current graph.
   156  	Refresh(graph ChannelGraph) error
   157  
   158  	// GetMetric returns the latest value of this metric. Values in the
   159  	// map are per node and can be in arbitrary domain. If normalize is
   160  	// set to true, then the returned values are normalized to either
   161  	// [0, 1] or [-1, 1] depending on the metric.
   162  	GetMetric(normalize bool) map[NodeID]float64
   163  }
   164  
   165  // ScoreSettable is an interface that indicates that the scores returned by the
   166  // heuristic can be mutated by an external caller. The ExternalScoreAttachment
   167  // currently implements this interface, and so should any heuristic that is
   168  // using the ExternalScoreAttachment as a sub-heuristic, or keeps their own
   169  // internal list of mutable scores, to allow access to setting the internal
   170  // scores.
   171  type ScoreSettable interface {
   172  	// SetNodeScores is used to set the internal map from NodeIDs to
   173  	// scores. The passed scores must be in the range [0, 1.0]. The first
   174  	// parameter is the name of the targeted heuristic, to allow
   175  	// recursively target specific sub-heuristics. The returned boolean
   176  	// indicates whether the targeted heuristic was found.
   177  	SetNodeScores(string, map[NodeID]float64) (bool, error)
   178  }
   179  
   180  var (
   181  	// availableHeuristics holds all heuristics possible to combine for use
   182  	// with the autopilot agent.
   183  	availableHeuristics = []AttachmentHeuristic{
   184  		NewPrefAttachment(),
   185  		NewExternalScoreAttachment(),
   186  		NewTopCentrality(),
   187  	}
   188  
   189  	// AvailableHeuristics is a map that holds the name of available
   190  	// heuristics to the actual heuristic for easy lookup. It will be
   191  	// filled during init().
   192  	AvailableHeuristics = make(map[string]AttachmentHeuristic)
   193  )
   194  
   195  func init() {
   196  	// Fill the map from heuristic names to available heuristics for easy
   197  	// lookup.
   198  	for _, h := range availableHeuristics {
   199  		AvailableHeuristics[h.Name()] = h
   200  	}
   201  }
   202  
   203  // ChannelController is a simple interface that allows an auto-pilot agent to
   204  // open a channel within the graph to a target peer, close targeted channels,
   205  // or add/remove funds from existing channels via a splice in/out mechanisms.
   206  type ChannelController interface {
   207  	// OpenChannel opens a channel to a target peer, using at most amt
   208  	// funds. This means that the resulting channel capacity might be
   209  	// slightly less to account for fees. This function should un-block
   210  	// immediately after the funding transaction that marks the channel
   211  	// open has been broadcast.
   212  	OpenChannel(target *secp256k1.PublicKey, amt dcrutil.Amount) error
   213  
   214  	// CloseChannel attempts to close out the target channel.
   215  	//
   216  	// TODO(roasbeef): add force option?
   217  	CloseChannel(chanPoint *wire.OutPoint) error
   218  }