2017-08-11 07:39:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-07 04:58:36 +03:00
|
|
|
"errors"
|
2017-08-11 07:39:08 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-08-11 07:39:08 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightningnetwork/lnd/autopilot"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2018-05-02 09:33:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/tor"
|
2017-08-11 07:39:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// chanController is an implementation of the autopilot.ChannelController
|
|
|
|
// interface that's backed by a running lnd instance.
|
|
|
|
type chanController struct {
|
2018-08-10 05:03:08 +03:00
|
|
|
server *server
|
|
|
|
private bool
|
|
|
|
minConfs int32
|
2017-08-11 07:39:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenChannel opens a channel to a target peer, with a capacity of the
|
|
|
|
// specified amount. This function should un-block immediately after the
|
|
|
|
// funding transaction that marks the channel open has been broadcast.
|
|
|
|
func (c *chanController) OpenChannel(target *btcec.PublicKey,
|
2018-08-07 04:58:36 +03:00
|
|
|
amt btcutil.Amount) error {
|
2017-08-11 07:39:08 +03:00
|
|
|
|
|
|
|
// With the connection established, we'll now establish our connection
|
|
|
|
// to the target peer, waiting for the first update before we exit.
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := c.server.cc.feeEstimator.EstimateFeePerKW(3)
|
2017-11-23 22:38:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-17 02:01:08 +03:00
|
|
|
|
|
|
|
// TODO(halseth): make configurable?
|
|
|
|
minHtlc := lnwire.NewMSatFromSatoshis(1)
|
|
|
|
|
2018-08-10 05:17:16 +03:00
|
|
|
// Construct the open channel request and send it to the server to begin
|
|
|
|
// the funding workflow.
|
|
|
|
req := &openChanReq{
|
|
|
|
targetPubkey: target,
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: amt,
|
|
|
|
pushAmt: 0,
|
|
|
|
minHtlc: minHtlc,
|
|
|
|
fundingFeePerKw: feePerKw,
|
|
|
|
private: c.private,
|
|
|
|
remoteCsvDelay: 0,
|
2018-08-10 05:03:08 +03:00
|
|
|
minConfs: c.minConfs,
|
2018-08-10 05:17:16 +03:00
|
|
|
}
|
2017-08-11 07:39:08 +03:00
|
|
|
|
2018-08-10 05:17:16 +03:00
|
|
|
updateStream, errChan := c.server.OpenChannel(req)
|
2017-08-11 07:39:08 +03:00
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
|
|
|
case <-updateStream:
|
|
|
|
return nil
|
|
|
|
case <-c.server.quit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *chanController) CloseChannel(chanPoint *wire.OutPoint) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (c *chanController) SpliceIn(chanPoint *wire.OutPoint,
|
|
|
|
amt btcutil.Amount) (*autopilot.Channel, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (c *chanController) SpliceOut(chanPoint *wire.OutPoint,
|
|
|
|
amt btcutil.Amount) (*autopilot.Channel, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time assertion to ensure chanController meets the
|
|
|
|
// autopilot.ChannelController interface.
|
|
|
|
var _ autopilot.ChannelController = (*chanController)(nil)
|
|
|
|
|
2018-12-13 14:26:29 +03:00
|
|
|
// initAutoPilot initializes a new autopilot.ManagerCfg to manage an
|
|
|
|
// autopilot.Agent instance based on the passed configuration struct. The agent
|
|
|
|
// and all interfaces needed to drive it won't be launched before the Manager's
|
|
|
|
// StartAgent method is called.
|
|
|
|
func initAutoPilot(svr *server, cfg *autoPilotConfig) *autopilot.ManagerCfg {
|
2017-08-11 07:39:08 +03:00
|
|
|
atplLog.Infof("Instantiating autopilot with cfg: %v", spew.Sdump(cfg))
|
|
|
|
|
2018-11-23 01:18:08 +03:00
|
|
|
// Set up the constraints the autopilot heuristics must adhere to.
|
2018-12-19 16:54:53 +03:00
|
|
|
atplConstraints := autopilot.NewConstraints(
|
|
|
|
btcutil.Amount(cfg.MinChannelSize),
|
|
|
|
btcutil.Amount(cfg.MaxChannelSize),
|
|
|
|
uint16(cfg.MaxChannels),
|
|
|
|
10,
|
|
|
|
cfg.Allocation,
|
|
|
|
)
|
2018-11-23 01:18:08 +03:00
|
|
|
|
2018-12-19 16:54:54 +03:00
|
|
|
// First, we'll create the preferential attachment heuristic.
|
|
|
|
prefAttachment := autopilot.NewPrefAttachment()
|
2017-08-11 07:39:08 +03:00
|
|
|
|
|
|
|
// With the heuristic itself created, we can now populate the remainder
|
|
|
|
// of the items that the autopilot agent needs to perform its duties.
|
|
|
|
self := svr.identityPriv.PubKey()
|
|
|
|
pilotCfg := autopilot.Config{
|
2018-08-07 00:02:31 +03:00
|
|
|
Self: self,
|
|
|
|
Heuristic: prefAttachment,
|
|
|
|
ChanController: &chanController{
|
2018-08-10 05:03:08 +03:00
|
|
|
server: svr,
|
|
|
|
private: cfg.Private,
|
|
|
|
minConfs: cfg.MinConfs,
|
2018-08-07 00:02:31 +03:00
|
|
|
},
|
2017-08-11 07:39:08 +03:00
|
|
|
WalletBalance: func() (btcutil.Amount, error) {
|
2018-08-10 05:03:08 +03:00
|
|
|
return svr.cc.wallet.ConfirmedBalance(cfg.MinConfs)
|
2017-08-11 07:39:08 +03:00
|
|
|
},
|
2018-11-23 01:18:08 +03:00
|
|
|
Graph: autopilot.ChannelGraphFromDatabase(svr.chanDB.ChannelGraph()),
|
|
|
|
Constraints: atplConstraints,
|
2018-08-07 04:58:36 +03:00
|
|
|
ConnectToPeer: func(target *btcec.PublicKey, addrs []net.Addr) (bool, error) {
|
|
|
|
// First, we'll check if we're already connected to the
|
|
|
|
// target peer. If we are, we can exit early. Otherwise,
|
|
|
|
// we'll need to establish a connection.
|
|
|
|
if _, err := svr.FindPeer(target); err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-08-17 10:26:54 +03:00
|
|
|
// We can't establish a channel if no addresses were
|
|
|
|
// provided for the peer.
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return false, errors.New("no addresses specified")
|
|
|
|
}
|
|
|
|
|
2018-08-07 04:58:36 +03:00
|
|
|
atplLog.Tracef("Attempting to connect to %x",
|
|
|
|
target.SerializeCompressed())
|
|
|
|
|
|
|
|
lnAddr := &lnwire.NetAddress{
|
|
|
|
IdentityKey: target,
|
|
|
|
ChainNet: activeNetParams.Net,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll attempt to successively connect to each of the
|
|
|
|
// advertised IP addresses until we've either exhausted
|
|
|
|
// the advertised IP addresses, or have made a
|
|
|
|
// connection.
|
|
|
|
var connected bool
|
|
|
|
for _, addr := range addrs {
|
|
|
|
switch addr.(type) {
|
|
|
|
case *net.TCPAddr, *tor.OnionAddr:
|
|
|
|
lnAddr.Address = addr
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("unknown "+
|
|
|
|
"address type %T", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := svr.ConnectToPeer(lnAddr, false)
|
|
|
|
if err != nil {
|
|
|
|
// If we weren't able to connect to the
|
|
|
|
// peer at this address, then we'll move
|
|
|
|
// onto the next.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
connected = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we weren't able to establish a connection at all,
|
|
|
|
// then we'll error out.
|
|
|
|
if !connected {
|
2018-08-29 12:04:54 +03:00
|
|
|
return false, errors.New("exhausted all " +
|
|
|
|
"advertised addresses")
|
2018-08-07 04:58:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
DisconnectPeer: svr.DisconnectPeer,
|
2017-08-11 07:39:08 +03:00
|
|
|
}
|
|
|
|
|
2018-12-13 14:26:29 +03:00
|
|
|
// Create and return the autopilot.ManagerCfg that administrates this
|
|
|
|
// agent-pilot instance.
|
|
|
|
return &autopilot.ManagerCfg{
|
|
|
|
Self: self,
|
|
|
|
PilotCfg: &pilotCfg,
|
|
|
|
ChannelState: func() ([]autopilot.Channel, error) {
|
|
|
|
// We'll fetch the current state of open
|
|
|
|
// channels from the database to use as initial
|
|
|
|
// state for the auto-pilot agent.
|
|
|
|
activeChannels, err := svr.chanDB.FetchAllChannels()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-01-09 04:44:28 +03:00
|
|
|
}
|
2018-12-13 14:26:29 +03:00
|
|
|
chanState := make([]autopilot.Channel,
|
|
|
|
len(activeChannels))
|
|
|
|
for i, channel := range activeChannels {
|
|
|
|
chanState[i] = autopilot.Channel{
|
|
|
|
ChanID: channel.ShortChanID(),
|
|
|
|
Capacity: channel.Capacity,
|
|
|
|
Node: autopilot.NewNodeID(
|
|
|
|
channel.IdentityPub),
|
2017-08-11 07:39:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 14:26:29 +03:00
|
|
|
return chanState, nil
|
|
|
|
},
|
|
|
|
SubscribeTransactions: svr.cc.wallet.SubscribeTransactions,
|
|
|
|
SubscribeTopology: svr.chanRouter.SubscribeTopology,
|
|
|
|
}
|
2017-08-11 07:39:08 +03:00
|
|
|
}
|