From dbeafe8832b06c8cd079bde06a407637b74b64ad Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 2 Oct 2020 14:49:02 +0200 Subject: [PATCH] autopilot: rename to Capacity to Balance, define ChannelInfo() We rename the field Capacity on local channels to Balance, define a new method ChannelInfo on the manager config that let us query the database for our latest channels state. Using this we will use the current local balance instead of the channel capacity when doing allocation calculations. --- autopilot/agent.go | 4 +-- autopilot/agent_constraints.go | 2 +- autopilot/agent_constraints_test.go | 40 ++++++++++++++--------------- autopilot/agent_test.go | 18 ++++++------- autopilot/interface.go | 4 +-- autopilot/manager.go | 25 ++++++++++-------- pilot.go | 25 +++++++++++++++--- 7 files changed, 70 insertions(+), 48 deletions(-) diff --git a/autopilot/agent.go b/autopilot/agent.go index fbe62026..182abb7f 100644 --- a/autopilot/agent.go +++ b/autopilot/agent.go @@ -829,8 +829,8 @@ func (a *Agent) executeDirective(directive AttachmentDirective) { // peers if the connection attempt happens to take too long. delete(a.pendingConns, nodeID) a.pendingOpens[nodeID] = LocalChannel{ - Capacity: directive.ChanAmt, - Node: nodeID, + Balance: directive.ChanAmt, + Node: nodeID, } a.pendingMtx.Unlock() diff --git a/autopilot/agent_constraints.go b/autopilot/agent_constraints.go index fc80b2af..821146d0 100644 --- a/autopilot/agent_constraints.go +++ b/autopilot/agent_constraints.go @@ -100,7 +100,7 @@ func (h *agentConstraints) ChannelBudget(channels []LocalChannel, // present within the set of active channels. var totalChanAllocation btcutil.Amount for _, channel := range channels { - totalChanAllocation += channel.Capacity + totalChanAllocation += channel.Balance } // With this value known, we'll now compute the total amount of fund diff --git a/autopilot/agent_constraints_test.go b/autopilot/agent_constraints_test.go index 19cdd5d6..144107a1 100644 --- a/autopilot/agent_constraints_test.go +++ b/autopilot/agent_constraints_test.go @@ -49,16 +49,16 @@ func TestConstraintsChannelBudget(t *testing.T) { { []LocalChannel{ { - ChanID: randChanID(), - Capacity: btcutil.Amount(prand.Int31()), + ChanID: randChanID(), + Balance: btcutil.Amount(prand.Int31()), }, { - ChanID: randChanID(), - Capacity: btcutil.Amount(prand.Int31()), + ChanID: randChanID(), + Balance: btcutil.Amount(prand.Int31()), }, { - ChanID: randChanID(), - Capacity: btcutil.Amount(prand.Int31()), + ChanID: randChanID(), + Balance: btcutil.Amount(prand.Int31()), }, }, btcutil.Amount(btcutil.SatoshiPerBitcoin * 10), @@ -72,12 +72,12 @@ func TestConstraintsChannelBudget(t *testing.T) { { []LocalChannel{ { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin), }, { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin), }, }, btcutil.Amount(btcutil.SatoshiPerBitcoin * 2), @@ -95,8 +95,8 @@ func TestConstraintsChannelBudget(t *testing.T) { { []LocalChannel{ { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin), }, }, btcutil.Amount(btcutil.SatoshiPerBitcoin * 9), @@ -115,12 +115,12 @@ func TestConstraintsChannelBudget(t *testing.T) { { []LocalChannel{ { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin), }, { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin * 3), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin * 3), }, }, btcutil.Amount(btcutil.SatoshiPerBitcoin * 10), @@ -134,12 +134,12 @@ func TestConstraintsChannelBudget(t *testing.T) { { []LocalChannel{ { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin), }, { - ChanID: randChanID(), - Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), + ChanID: randChanID(), + Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin), }, }, btcutil.Amount(btcutil.SatoshiPerBitcoin), diff --git a/autopilot/agent_test.go b/autopilot/agent_test.go index 1e842018..b12ab792 100644 --- a/autopilot/agent_test.go +++ b/autopilot/agent_test.go @@ -292,8 +292,8 @@ func TestAgentChannelOpenSignal(t *testing.T) { // Next we'll signal a new channel being opened by the backing LN node, // with a capacity of 1 BTC. newChan := LocalChannel{ - ChanID: randChanID(), - Capacity: btcutil.SatoshiPerBitcoin, + ChanID: randChanID(), + Balance: btcutil.SatoshiPerBitcoin, } testCtx.agent.OnChannelOpen(newChan) @@ -434,12 +434,12 @@ func TestAgentChannelCloseSignal(t *testing.T) { // We'll start the agent with two channels already being active. initialChans := []LocalChannel{ { - ChanID: randChanID(), - Capacity: btcutil.SatoshiPerBitcoin, + ChanID: randChanID(), + Balance: btcutil.SatoshiPerBitcoin, }, { - ChanID: randChanID(), - Capacity: btcutil.SatoshiPerBitcoin * 2, + ChanID: randChanID(), + Balance: btcutil.SatoshiPerBitcoin * 2, }, } @@ -730,9 +730,9 @@ func TestAgentPendingChannelState(t *testing.T) { t.Fatalf("should include pending chan in current "+ "state, instead have %v chans", len(req.chans)) } - if req.chans[0].Capacity != chanAmt { - t.Fatalf("wrong chan capacity: expected %v, got %v", - req.chans[0].Capacity, chanAmt) + if req.chans[0].Balance != chanAmt { + t.Fatalf("wrong chan balance: expected %v, got %v", + req.chans[0].Balance, chanAmt) } if req.chans[0].Node != nodeID { t.Fatalf("wrong node ID: expected %x, got %x", diff --git a/autopilot/interface.go b/autopilot/interface.go index 2b7b5509..9359489f 100644 --- a/autopilot/interface.go +++ b/autopilot/interface.go @@ -44,8 +44,8 @@ type LocalChannel struct { // BOLT-0007. ChanID lnwire.ShortChannelID - // Capacity is the capacity of the channel expressed in satoshis. - Capacity btcutil.Amount + // Balance is the local balance of the channel expressed in satoshis. + Balance btcutil.Amount // Node is the peer that this channel has been established with. Node NodeID diff --git a/autopilot/manager.go b/autopilot/manager.go index f1bdff87..ecdf879f 100644 --- a/autopilot/manager.go +++ b/autopilot/manager.go @@ -5,6 +5,7 @@ import ( "sync" "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing" @@ -25,6 +26,10 @@ type ManagerCfg struct { // channels managed by this node. ChannelState func() ([]LocalChannel, error) + // 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) + // SubscribeTransactions is used to get a subscription for transactions // relevant to this node's wallet. SubscribeTransactions func() (lnwallet.TransactionSubscription, error) @@ -194,18 +199,16 @@ func (m *Manager) StartAgent() error { // opened, then we'll convert it to the // autopilot.Channel format, and notify // the pilot of the new channel. - chanNode := NewNodeID( - edgeUpdate.ConnectingNode, - ) - chanID := lnwire.NewShortChanIDFromInt( - edgeUpdate.ChanID, - ) - edge := LocalChannel{ - ChanID: chanID, - Capacity: edgeUpdate.Capacity, - Node: chanNode, + 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 } - pilot.OnChannelOpen(edge) + + pilot.OnChannelOpen(*edge) } // For each closed channel, we'll obtain diff --git a/pilot.go b/pilot.go index 34ba0e81..87148121 100644 --- a/pilot.go +++ b/pilot.go @@ -260,16 +260,35 @@ func initAutoPilot(svr *server, cfg *lncfg.AutoPilot, chanState := make([]autopilot.LocalChannel, len(activeChannels)) for i, channel := range activeChannels { + localCommit := channel.LocalCommitment + balance := localCommit.LocalBalance.ToSatoshis() + chanState[i] = autopilot.LocalChannel{ - ChanID: channel.ShortChanID(), - Capacity: channel.Capacity, + ChanID: channel.ShortChanID(), + Balance: balance, Node: autopilot.NewNodeID( - channel.IdentityPub), + channel.IdentityPub, + ), } } return chanState, nil }, + ChannelInfo: func(chanPoint wire.OutPoint) ( + *autopilot.LocalChannel, error) { + + channel, err := svr.remoteChanDB.FetchChannel(chanPoint) + if err != nil { + return nil, err + } + + localCommit := channel.LocalCommitment + return &autopilot.LocalChannel{ + ChanID: channel.ShortChanID(), + Balance: localCommit.LocalBalance.ToSatoshis(), + Node: autopilot.NewNodeID(channel.IdentityPub), + }, nil + }, SubscribeTransactions: svr.cc.wallet.SubscribeTransactions, SubscribeTopology: svr.chanRouter.SubscribeTopology, }, nil