autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
package autopilot
|
|
|
|
|
|
|
|
import (
|
2018-08-07 04:58:36 +03:00
|
|
|
"net"
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
)
|
|
|
|
|
2018-01-30 03:07:26 +03:00
|
|
|
// Config couples all the items that an autopilot agent needs to function.
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// All items within the struct MUST be populated for the Agent to be able to
|
|
|
|
// carry out its duties.
|
|
|
|
type Config struct {
|
|
|
|
// Self is the identity public key of the Lightning Network node that
|
|
|
|
// is being driven by the agent. This is used to ensure that we don't
|
|
|
|
// accidentally attempt to open a channel with ourselves.
|
|
|
|
Self *btcec.PublicKey
|
|
|
|
|
|
|
|
// Heuristic is an attachment heuristic which will govern to whom we
|
|
|
|
// open channels to, and also what those channels look like in terms of
|
|
|
|
// desired capacity. The Heuristic will take into account the current
|
|
|
|
// state of the graph, our set of open channels, and the amount of
|
|
|
|
// available funds when determining how channels are to be opened.
|
|
|
|
// Additionally, a heuristic make also factor in extra-graph
|
|
|
|
// information in order to make more pertinent recommendations.
|
|
|
|
Heuristic AttachmentHeuristic
|
|
|
|
|
|
|
|
// ChanController is an interface that is able to directly manage the
|
|
|
|
// creation, closing and update of channels within the network.
|
|
|
|
ChanController ChannelController
|
|
|
|
|
2018-08-07 04:58:36 +03:00
|
|
|
// ConnectToPeer attempts to connect to the peer using one of its
|
|
|
|
// advertised addresses. The boolean returned signals whether the peer
|
|
|
|
// was already connected.
|
|
|
|
ConnectToPeer func(*btcec.PublicKey, []net.Addr) (bool, error)
|
|
|
|
|
|
|
|
// DisconnectPeer attempts to disconnect the peer with the given public
|
|
|
|
// key.
|
|
|
|
DisconnectPeer func(*btcec.PublicKey) error
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// WalletBalance is a function closure that should return the current
|
|
|
|
// available balance o the backing wallet.
|
|
|
|
WalletBalance func() (btcutil.Amount, error)
|
|
|
|
|
|
|
|
// Graph is an abstract channel graph that the Heuristic and the Agent
|
|
|
|
// will use to make decisions w.r.t channel allocation and placement
|
|
|
|
// within the graph.
|
|
|
|
Graph ChannelGraph
|
|
|
|
|
2018-03-11 03:46:38 +03:00
|
|
|
// MaxPendingOpens is the maximum number of pending channel
|
|
|
|
// establishment goroutines that can be lingering. We cap this value in
|
|
|
|
// order to control the level of parallelism caused by the autopiloit
|
|
|
|
// agent.
|
|
|
|
MaxPendingOpens uint16
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// TODO(roasbeef): add additional signals from fee rates and revenue of
|
|
|
|
// currently opened channels
|
|
|
|
}
|
|
|
|
|
|
|
|
// channelState is a type that represents the set of active channels of the
|
|
|
|
// backing LN node that the Agent should be ware of. This type contains a few
|
|
|
|
// helper utility methods.
|
|
|
|
type channelState map[lnwire.ShortChannelID]Channel
|
|
|
|
|
2017-08-16 04:23:52 +03:00
|
|
|
// Channels returns a slice of all the active channels.
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
func (c channelState) Channels() []Channel {
|
|
|
|
chans := make([]Channel, 0, len(c))
|
|
|
|
for _, channel := range c {
|
|
|
|
chans = append(chans, channel)
|
|
|
|
}
|
|
|
|
return chans
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectedNodes returns the set of nodes we currently have a channel with.
|
|
|
|
// This information is needed as we want to avoid making repeated channels with
|
|
|
|
// any node.
|
|
|
|
func (c channelState) ConnectedNodes() map[NodeID]struct{} {
|
|
|
|
nodes := make(map[NodeID]struct{})
|
|
|
|
for _, channels := range c {
|
|
|
|
nodes[channels.Node] = struct{}{}
|
|
|
|
}
|
2017-08-22 08:20:54 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): add outgoing, nodes, allow incoming and outgoing to
|
|
|
|
// per node
|
|
|
|
// * only add node is chan as funding amt set
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Agent implements a closed-loop control system which seeks to autonomously
|
|
|
|
// optimize the allocation of satoshis within channels throughput the network's
|
|
|
|
// channel graph. An agent is configurable by swapping out different
|
|
|
|
// AttachmentHeuristic strategies. The agent uses external signals such as the
|
|
|
|
// wallet balance changing, or new channels being opened/closed for the local
|
|
|
|
// node as an indicator to re-examine its internal state, and the amount of
|
|
|
|
// available funds in order to make updated decisions w.r.t the channel graph.
|
|
|
|
// The Agent will automatically open, close, and splice in/out channel as
|
|
|
|
// necessary for it to step closer to its optimal state.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): prob re-word
|
|
|
|
type Agent struct {
|
|
|
|
// Only to be used atomically.
|
|
|
|
started uint32
|
|
|
|
stopped uint32
|
|
|
|
|
|
|
|
// cfg houses the configuration state of the Ant.
|
|
|
|
cfg Config
|
|
|
|
|
|
|
|
// chanState tracks the current set of open channels.
|
|
|
|
chanState channelState
|
|
|
|
|
|
|
|
// stateUpdates is a channel that any external state updates that may
|
|
|
|
// affect the heuristics of the agent will be sent over.
|
|
|
|
stateUpdates chan interface{}
|
|
|
|
|
|
|
|
// totalBalance is the total number of satoshis the backing wallet is
|
|
|
|
// known to control at any given instance. This value will be updated
|
|
|
|
// when the agent receives external balance update signals.
|
|
|
|
totalBalance btcutil.Amount
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the Agent instantiated using the passed
|
|
|
|
// configuration and initial channel state. The initial channel state slice
|
|
|
|
// should be populated with the set of Channels that are currently opened by
|
|
|
|
// the backing Lightning Node.
|
|
|
|
func New(cfg Config, initialState []Channel) (*Agent, error) {
|
|
|
|
a := &Agent{
|
|
|
|
cfg: cfg,
|
|
|
|
chanState: make(map[lnwire.ShortChannelID]Channel),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
stateUpdates: make(chan interface{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range initialState {
|
|
|
|
a.chanState[c.ChanID] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the agent along with any goroutines it needs to perform its
|
|
|
|
// normal duties.
|
|
|
|
func (a *Agent) Start() error {
|
|
|
|
if !atomic.CompareAndSwapUint32(&a.started, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Autopilot Agent starting")
|
|
|
|
|
|
|
|
startingBalance, err := a.cfg.WalletBalance()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.wg.Add(1)
|
|
|
|
go a.controller(startingBalance)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals the Agent to gracefully shutdown. This function will block
|
|
|
|
// until all goroutines have exited.
|
|
|
|
func (a *Agent) Stop() error {
|
|
|
|
if !atomic.CompareAndSwapUint32(&a.stopped, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Autopilot Agent stopping")
|
|
|
|
|
|
|
|
close(a.quit)
|
|
|
|
a.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// balanceUpdate is a type of external state update that reflects an
|
|
|
|
// increase/decrease in the funds currently available to the wallet.
|
|
|
|
type balanceUpdate struct {
|
|
|
|
balanceDelta btcutil.Amount
|
|
|
|
}
|
|
|
|
|
|
|
|
// chanOpenUpdate is a type of external state update the indicates a new
|
|
|
|
// channel has been opened, either by the Agent itself (within the main
|
|
|
|
// controller loop), or by an external user to the system.
|
|
|
|
type chanOpenUpdate struct {
|
|
|
|
newChan Channel
|
|
|
|
}
|
|
|
|
|
2017-12-05 07:56:40 +03:00
|
|
|
// chanOpenFailureUpdate is a type of external state update that indicates
|
|
|
|
// a previous channel open failed, and that it might be possible to try again.
|
|
|
|
type chanOpenFailureUpdate struct{}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// chanCloseUpdate is a type of external state update that indicates that the
|
|
|
|
// backing Lightning Node has closed a previously open channel.
|
|
|
|
type chanCloseUpdate struct {
|
|
|
|
closedChans []lnwire.ShortChannelID
|
|
|
|
}
|
|
|
|
|
2017-12-05 07:56:40 +03:00
|
|
|
// OnBalanceChange is a callback that should be executed each time the balance of
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// the backing wallet changes.
|
|
|
|
func (a *Agent) OnBalanceChange(delta btcutil.Amount) {
|
|
|
|
go func() {
|
|
|
|
a.stateUpdates <- &balanceUpdate{
|
|
|
|
balanceDelta: delta,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnChannelOpen is a callback that should be executed each time a new channel
|
|
|
|
// is manually opened by the user or any system outside the autopilot agent.
|
|
|
|
func (a *Agent) OnChannelOpen(c Channel) {
|
|
|
|
go func() {
|
|
|
|
a.stateUpdates <- &chanOpenUpdate{
|
|
|
|
newChan: c,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-12-05 07:56:40 +03:00
|
|
|
// OnChannelOpenFailure is a callback that should be executed when the
|
|
|
|
// autopilot has attempted to open a channel, but failed. In this case we can
|
|
|
|
// retry channel creation with a different node.
|
|
|
|
func (a *Agent) OnChannelOpenFailure() {
|
|
|
|
go func() {
|
|
|
|
a.stateUpdates <- &chanOpenFailureUpdate{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// OnChannelClose is a callback that should be executed each time a prior
|
|
|
|
// channel has been closed for any reason. This includes regular
|
|
|
|
// closes, force closes, and channel breaches.
|
|
|
|
func (a *Agent) OnChannelClose(closedChans ...lnwire.ShortChannelID) {
|
|
|
|
go func() {
|
|
|
|
a.stateUpdates <- &chanCloseUpdate{
|
|
|
|
closedChans: closedChans,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-08-16 04:23:52 +03:00
|
|
|
// mergeNodeMaps merges the Agent's set of nodes that it already has active
|
|
|
|
// channels open to, with the set of nodes that are pending new channels. This
|
|
|
|
// ensures that the Agent doesn't attempt to open any "duplicate" channels to
|
|
|
|
// the same node.
|
2018-01-13 13:01:55 +03:00
|
|
|
func mergeNodeMaps(a map[NodeID]struct{}, b map[NodeID]struct{},
|
|
|
|
c map[NodeID]Channel) map[NodeID]struct{} {
|
2017-08-16 04:23:52 +03:00
|
|
|
|
2018-01-13 13:01:55 +03:00
|
|
|
res := make(map[NodeID]struct{}, len(a)+len(b)+len(c))
|
2017-08-16 04:23:52 +03:00
|
|
|
for nodeID := range a {
|
2018-01-13 13:01:55 +03:00
|
|
|
res[nodeID] = struct{}{}
|
2017-08-16 04:23:52 +03:00
|
|
|
}
|
|
|
|
for nodeID := range b {
|
2018-01-13 13:01:55 +03:00
|
|
|
res[nodeID] = struct{}{}
|
|
|
|
}
|
|
|
|
for nodeID := range c {
|
|
|
|
res[nodeID] = struct{}{}
|
2017-08-16 04:23:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-13 13:01:55 +03:00
|
|
|
return res
|
2017-08-16 04:23:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// mergeChanState merges the Agent's set of active channels, with the set of
|
|
|
|
// channels awaiting confirmation. This ensures that the agent doesn't go over
|
|
|
|
// the prescribed channel limit or fund allocation limit.
|
|
|
|
func mergeChanState(pendingChans map[NodeID]Channel,
|
|
|
|
activeChans channelState) []Channel {
|
|
|
|
|
|
|
|
numChans := len(pendingChans) + len(activeChans)
|
|
|
|
totalChans := make([]Channel, 0, numChans)
|
|
|
|
|
|
|
|
for _, activeChan := range activeChans.Channels() {
|
|
|
|
totalChans = append(totalChans, activeChan)
|
|
|
|
}
|
|
|
|
for _, pendingChan := range pendingChans {
|
|
|
|
totalChans = append(totalChans, pendingChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalChans
|
|
|
|
}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// controller implements the closed-loop control system of the Agent. The
|
|
|
|
// controller will make a decision w.r.t channel placement within the graph
|
2018-02-07 06:13:07 +03:00
|
|
|
// based on: its current internal state of the set of active channels open,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// and external state changes as a result of decisions it makes w.r.t channel
|
|
|
|
// allocation, or attributes affecting its control loop being updated by the
|
|
|
|
// backing Lightning Node.
|
|
|
|
func (a *Agent) controller(startingBalance btcutil.Amount) {
|
|
|
|
defer a.wg.Done()
|
|
|
|
|
|
|
|
// We'll start off by assigning our starting balance, and injecting
|
|
|
|
// that amount as an initial wake up to the main controller goroutine.
|
|
|
|
a.OnBalanceChange(startingBalance)
|
|
|
|
|
|
|
|
// TODO(roasbeef): do we in fact need to maintain order?
|
|
|
|
// * use sync.Cond if so
|
|
|
|
|
2018-01-13 13:01:55 +03:00
|
|
|
// failedNodes lists nodes that we've previously attempted to initiate
|
|
|
|
// channels with, but didn't succeed.
|
|
|
|
failedNodes := make(map[NodeID]struct{})
|
|
|
|
|
2017-08-16 04:23:52 +03:00
|
|
|
// pendingOpens tracks the channels that we've requested to be
|
|
|
|
// initiated, but haven't yet been confirmed as being fully opened.
|
|
|
|
// This state is required as otherwise, we may go over our allotted
|
|
|
|
// channel limit, or open multiple channels to the same node.
|
|
|
|
pendingOpens := make(map[NodeID]Channel)
|
|
|
|
var pendingMtx sync.Mutex
|
|
|
|
|
2018-03-11 03:47:29 +03:00
|
|
|
updateBalance := func() {
|
|
|
|
newBalance, err := a.cfg.WalletBalance()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("unable to update wallet balance: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.totalBalance = newBalance
|
|
|
|
}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// TODO(roasbeef): add 10-minute wake up timer
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// A new external signal has arrived. We'll use this to update
|
|
|
|
// our internal state, then determine if we should trigger a
|
|
|
|
// channel state modification (open/close, splice in/out).
|
|
|
|
case signal := <-a.stateUpdates:
|
|
|
|
log.Infof("Processing new external signal")
|
|
|
|
|
|
|
|
switch update := signal.(type) {
|
|
|
|
// The balance of the backing wallet has changed, if
|
|
|
|
// more funds are now available, we may attempt to open
|
|
|
|
// up an additional channel, or splice in funds to an
|
|
|
|
// existing one.
|
|
|
|
case *balanceUpdate:
|
|
|
|
log.Debugf("Applying external balance state "+
|
|
|
|
"update of: %v", update.balanceDelta)
|
|
|
|
|
|
|
|
a.totalBalance += update.balanceDelta
|
|
|
|
|
2017-12-05 07:56:40 +03:00
|
|
|
// The channel we tried to open previously failed for
|
|
|
|
// whatever reason.
|
|
|
|
case *chanOpenFailureUpdate:
|
|
|
|
log.Debug("Retrying after previous channel open failure.")
|
|
|
|
|
2018-03-11 03:47:29 +03:00
|
|
|
updateBalance()
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// A new channel has been opened successfully. This was
|
|
|
|
// either opened by the Agent, or an external system
|
|
|
|
// that is able to drive the Lightning Node.
|
|
|
|
case *chanOpenUpdate:
|
|
|
|
log.Debugf("New channel successfully opened, "+
|
|
|
|
"updating state with: %v",
|
|
|
|
spew.Sdump(update.newChan))
|
|
|
|
|
|
|
|
newChan := update.newChan
|
|
|
|
a.chanState[newChan.ChanID] = newChan
|
|
|
|
|
2017-08-16 04:23:52 +03:00
|
|
|
pendingMtx.Lock()
|
|
|
|
delete(pendingOpens, newChan.Node)
|
|
|
|
pendingMtx.Unlock()
|
|
|
|
|
2018-03-11 03:47:29 +03:00
|
|
|
updateBalance()
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// A channel has been closed, this may free up an
|
|
|
|
// available slot, triggering a new channel update.
|
|
|
|
case *chanCloseUpdate:
|
|
|
|
log.Debugf("Applying closed channel "+
|
|
|
|
"updates: %v",
|
|
|
|
spew.Sdump(update.closedChans))
|
|
|
|
|
|
|
|
for _, closedChan := range update.closedChans {
|
|
|
|
delete(a.chanState, closedChan)
|
|
|
|
}
|
2018-03-11 03:47:29 +03:00
|
|
|
|
|
|
|
updateBalance()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
}
|
|
|
|
|
2017-12-11 02:46:39 +03:00
|
|
|
pendingMtx.Lock()
|
2017-08-22 08:20:54 +03:00
|
|
|
log.Debugf("Pending channels: %v", spew.Sdump(pendingOpens))
|
2017-12-11 02:46:39 +03:00
|
|
|
pendingMtx.Unlock()
|
2017-08-22 08:20:54 +03:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
// With all the updates applied, we'll obtain a set of
|
2017-08-16 04:23:52 +03:00
|
|
|
// the current active channels (confirmed channels),
|
|
|
|
// and also factor in our set of unconfirmed channels.
|
|
|
|
confirmedChans := a.chanState
|
2017-08-22 08:20:54 +03:00
|
|
|
pendingMtx.Lock()
|
2017-08-16 04:23:52 +03:00
|
|
|
totalChans := mergeChanState(pendingOpens, confirmedChans)
|
2017-08-22 08:20:54 +03:00
|
|
|
pendingMtx.Unlock()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
|
|
|
|
// Now that we've updated our internal state, we'll
|
|
|
|
// consult our channel attachment heuristic to
|
|
|
|
// determine if we should open up any additional
|
|
|
|
// channels or modify existing channels.
|
2018-02-09 07:06:57 +03:00
|
|
|
availableFunds, numChans, needMore := a.cfg.Heuristic.NeedMoreChans(
|
2017-08-16 04:23:52 +03:00
|
|
|
totalChans, a.totalBalance,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
)
|
|
|
|
if !needMore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-03-11 03:47:29 +03:00
|
|
|
log.Infof("Triggering attachment directive dispatch, "+
|
|
|
|
"total_funds=%v", a.totalBalance)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
|
|
|
|
// We're to attempt an attachment so we'll o obtain the
|
|
|
|
// set of nodes that we currently have channels with so
|
|
|
|
// we avoid duplicate edges.
|
2017-08-16 04:23:52 +03:00
|
|
|
connectedNodes := a.chanState.ConnectedNodes()
|
|
|
|
pendingMtx.Lock()
|
2018-01-13 13:01:55 +03:00
|
|
|
nodesToSkip := mergeNodeMaps(connectedNodes, failedNodes, pendingOpens)
|
2017-08-16 04:23:52 +03:00
|
|
|
pendingMtx.Unlock()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
|
|
|
|
// If we reach this point, then according to our
|
|
|
|
// heuristic we should modify our channel state to tend
|
|
|
|
// towards what it determines to the optimal state. So
|
|
|
|
// we'll call Select to get a fresh batch of attachment
|
|
|
|
// directives, passing in the amount of funds available
|
|
|
|
// for us to use.
|
|
|
|
chanCandidates, err := a.cfg.Heuristic.Select(
|
|
|
|
a.cfg.Self, a.cfg.Graph, availableFunds,
|
2018-02-09 07:06:57 +03:00
|
|
|
numChans, nodesToSkip,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to select candidates for "+
|
|
|
|
"attachment: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(chanCandidates) == 0 {
|
|
|
|
log.Infof("No eligible candidates to connect to")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Attempting to execute channel attachment "+
|
|
|
|
"directives: %v", spew.Sdump(chanCandidates))
|
|
|
|
|
|
|
|
// For each recommended attachment directive, we'll
|
|
|
|
// launch a new goroutine to attempt to carry out the
|
|
|
|
// directive. If any of these succeed, then we'll
|
|
|
|
// receive a new state update, taking us back to the
|
|
|
|
// top of our controller loop.
|
2017-08-16 04:23:52 +03:00
|
|
|
pendingMtx.Lock()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
for _, chanCandidate := range chanCandidates {
|
2018-03-11 03:46:38 +03:00
|
|
|
// Before we proceed, we'll check to see if
|
|
|
|
// this attempt would take us past the total
|
|
|
|
// number of allowed pending opens. If so, then
|
|
|
|
// we'll skip this round and wait for an
|
|
|
|
// attempt to either fail or succeed.
|
|
|
|
if uint16(len(pendingOpens))+1 >
|
|
|
|
a.cfg.MaxPendingOpens {
|
|
|
|
|
|
|
|
log.Debugf("Reached cap of %v pending "+
|
|
|
|
"channel opens, will retry "+
|
|
|
|
"after success/failure",
|
|
|
|
a.cfg.MaxPendingOpens)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
go func(directive AttachmentDirective) {
|
2018-08-07 04:58:36 +03:00
|
|
|
// We'll start out by attempting to
|
|
|
|
// connect to the peer in order to begin
|
|
|
|
// the funding workflow.
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
pub := directive.PeerKey
|
2018-08-07 04:58:36 +03:00
|
|
|
alreadyConnected, err := a.cfg.ConnectToPeer(
|
|
|
|
pub, directive.Addrs,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to connect "+
|
|
|
|
"to %x: %v",
|
|
|
|
pub.SerializeCompressed(),
|
|
|
|
err)
|
|
|
|
|
|
|
|
// Since we failed to connect to
|
|
|
|
// them, we'll mark them as
|
|
|
|
// failed so that we don't
|
|
|
|
// attempt to connect to them
|
|
|
|
// again.
|
|
|
|
nodeID := NewNodeID(pub)
|
|
|
|
pendingMtx.Lock()
|
|
|
|
failedNodes[nodeID] = struct{}{}
|
|
|
|
pendingMtx.Unlock()
|
|
|
|
|
|
|
|
// Finally, we'll trigger the
|
|
|
|
// agent to select new peers to
|
|
|
|
// connect to.
|
|
|
|
a.OnChannelOpenFailure()
|
2018-02-09 07:06:57 +03:00
|
|
|
|
2018-08-07 04:58:36 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were succesful, we'll track
|
|
|
|
// this peer in our set of pending
|
|
|
|
// opens. We do this here to ensure we
|
|
|
|
// don't stall on selecting new peers if
|
|
|
|
// the connection attempt happens to
|
|
|
|
// take too long.
|
|
|
|
pendingMtx.Lock()
|
|
|
|
if uint16(len(pendingOpens))+1 >
|
|
|
|
a.cfg.MaxPendingOpens {
|
|
|
|
|
|
|
|
pendingMtx.Unlock()
|
|
|
|
|
|
|
|
// Since we've reached our max
|
|
|
|
// number of pending opens,
|
|
|
|
// we'll disconnect this peer
|
|
|
|
// and exit. However, if we were
|
|
|
|
// previously connected to them,
|
|
|
|
// then we'll make sure to
|
|
|
|
// maintain the connection
|
|
|
|
// alive.
|
|
|
|
if alreadyConnected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.cfg.DisconnectPeer(
|
|
|
|
pub,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to "+
|
|
|
|
"disconnect peer "+
|
|
|
|
"%x: %v",
|
|
|
|
pub.SerializeCompressed(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeID := NewNodeID(directive.PeerKey)
|
|
|
|
pendingOpens[nodeID] = Channel{
|
|
|
|
Capacity: directive.ChanAmt,
|
|
|
|
Node: nodeID,
|
|
|
|
}
|
|
|
|
pendingMtx.Unlock()
|
|
|
|
|
|
|
|
// We can then begin the funding
|
|
|
|
// workflow with this peer.
|
|
|
|
err = a.cfg.ChanController.OpenChannel(
|
|
|
|
pub, directive.ChanAmt,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to open "+
|
|
|
|
"channel to %x of %v: %v",
|
|
|
|
pub.SerializeCompressed(),
|
|
|
|
directive.ChanAmt, err)
|
2017-08-16 04:23:52 +03:00
|
|
|
|
|
|
|
// As the attempt failed, we'll
|
2018-08-07 04:58:36 +03:00
|
|
|
// clear the peer from the set of
|
|
|
|
// pending opens and mark them
|
|
|
|
// as failed so we don't attempt
|
|
|
|
// to open a channel to them
|
|
|
|
// again.
|
2017-08-16 04:23:52 +03:00
|
|
|
pendingMtx.Lock()
|
2018-08-07 04:58:36 +03:00
|
|
|
delete(pendingOpens, nodeID)
|
|
|
|
failedNodes[nodeID] = struct{}{}
|
2017-08-16 04:23:52 +03:00
|
|
|
pendingMtx.Unlock()
|
|
|
|
|
2018-08-07 04:58:36 +03:00
|
|
|
// Trigger the agent to
|
|
|
|
// re-evaluate everything and
|
|
|
|
// possibly retry with a
|
|
|
|
// different node.
|
2017-12-05 07:56:40 +03:00
|
|
|
a.OnChannelOpenFailure()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
|
2018-08-07 04:58:36 +03:00
|
|
|
// Finally, we should also
|
|
|
|
// disconnect the peer if we
|
|
|
|
// weren't already connected to
|
|
|
|
// them beforehand by an
|
|
|
|
// external subsystem.
|
|
|
|
if alreadyConnected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.cfg.DisconnectPeer(pub)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to "+
|
|
|
|
"disconnect peer "+
|
|
|
|
"%x: %v",
|
|
|
|
pub.SerializeCompressed(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
}(chanCandidate)
|
|
|
|
}
|
2017-08-16 04:23:52 +03:00
|
|
|
pendingMtx.Unlock()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 07:14:41 +03:00
|
|
|
|
|
|
|
// The agent has been signalled to exit, so we'll bail out
|
|
|
|
// immediately.
|
|
|
|
case <-a.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|