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.
This commit is contained in:
Johan T. Halseth 2020-10-02 14:49:02 +02:00
parent d40cf6b592
commit dbeafe8832
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
7 changed files with 70 additions and 48 deletions

@ -829,7 +829,7 @@ func (a *Agent) executeDirective(directive AttachmentDirective) {
// peers if the connection attempt happens to take too long. // peers if the connection attempt happens to take too long.
delete(a.pendingConns, nodeID) delete(a.pendingConns, nodeID)
a.pendingOpens[nodeID] = LocalChannel{ a.pendingOpens[nodeID] = LocalChannel{
Capacity: directive.ChanAmt, Balance: directive.ChanAmt,
Node: nodeID, Node: nodeID,
} }
a.pendingMtx.Unlock() a.pendingMtx.Unlock()

@ -100,7 +100,7 @@ func (h *agentConstraints) ChannelBudget(channels []LocalChannel,
// present within the set of active channels. // present within the set of active channels.
var totalChanAllocation btcutil.Amount var totalChanAllocation btcutil.Amount
for _, channel := range channels { for _, channel := range channels {
totalChanAllocation += channel.Capacity totalChanAllocation += channel.Balance
} }
// With this value known, we'll now compute the total amount of fund // With this value known, we'll now compute the total amount of fund

@ -50,15 +50,15 @@ func TestConstraintsChannelBudget(t *testing.T) {
[]LocalChannel{ []LocalChannel{
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(prand.Int31()), Balance: btcutil.Amount(prand.Int31()),
}, },
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(prand.Int31()), Balance: btcutil.Amount(prand.Int31()),
}, },
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(prand.Int31()), Balance: btcutil.Amount(prand.Int31()),
}, },
}, },
btcutil.Amount(btcutil.SatoshiPerBitcoin * 10), btcutil.Amount(btcutil.SatoshiPerBitcoin * 10),
@ -73,11 +73,11 @@ func TestConstraintsChannelBudget(t *testing.T) {
[]LocalChannel{ []LocalChannel{
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin),
}, },
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin),
}, },
}, },
btcutil.Amount(btcutil.SatoshiPerBitcoin * 2), btcutil.Amount(btcutil.SatoshiPerBitcoin * 2),
@ -96,7 +96,7 @@ func TestConstraintsChannelBudget(t *testing.T) {
[]LocalChannel{ []LocalChannel{
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin),
}, },
}, },
btcutil.Amount(btcutil.SatoshiPerBitcoin * 9), btcutil.Amount(btcutil.SatoshiPerBitcoin * 9),
@ -116,11 +116,11 @@ func TestConstraintsChannelBudget(t *testing.T) {
[]LocalChannel{ []LocalChannel{
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin),
}, },
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin * 3), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin * 3),
}, },
}, },
btcutil.Amount(btcutil.SatoshiPerBitcoin * 10), btcutil.Amount(btcutil.SatoshiPerBitcoin * 10),
@ -135,11 +135,11 @@ func TestConstraintsChannelBudget(t *testing.T) {
[]LocalChannel{ []LocalChannel{
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin),
}, },
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.Amount(btcutil.SatoshiPerBitcoin), Balance: btcutil.Amount(btcutil.SatoshiPerBitcoin),
}, },
}, },
btcutil.Amount(btcutil.SatoshiPerBitcoin), btcutil.Amount(btcutil.SatoshiPerBitcoin),

@ -293,7 +293,7 @@ func TestAgentChannelOpenSignal(t *testing.T) {
// with a capacity of 1 BTC. // with a capacity of 1 BTC.
newChan := LocalChannel{ newChan := LocalChannel{
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.SatoshiPerBitcoin, Balance: btcutil.SatoshiPerBitcoin,
} }
testCtx.agent.OnChannelOpen(newChan) testCtx.agent.OnChannelOpen(newChan)
@ -435,11 +435,11 @@ func TestAgentChannelCloseSignal(t *testing.T) {
initialChans := []LocalChannel{ initialChans := []LocalChannel{
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.SatoshiPerBitcoin, Balance: btcutil.SatoshiPerBitcoin,
}, },
{ {
ChanID: randChanID(), ChanID: randChanID(),
Capacity: btcutil.SatoshiPerBitcoin * 2, Balance: btcutil.SatoshiPerBitcoin * 2,
}, },
} }
@ -730,9 +730,9 @@ func TestAgentPendingChannelState(t *testing.T) {
t.Fatalf("should include pending chan in current "+ t.Fatalf("should include pending chan in current "+
"state, instead have %v chans", len(req.chans)) "state, instead have %v chans", len(req.chans))
} }
if req.chans[0].Capacity != chanAmt { if req.chans[0].Balance != chanAmt {
t.Fatalf("wrong chan capacity: expected %v, got %v", t.Fatalf("wrong chan balance: expected %v, got %v",
req.chans[0].Capacity, chanAmt) req.chans[0].Balance, chanAmt)
} }
if req.chans[0].Node != nodeID { if req.chans[0].Node != nodeID {
t.Fatalf("wrong node ID: expected %x, got %x", t.Fatalf("wrong node ID: expected %x, got %x",

@ -44,8 +44,8 @@ type LocalChannel struct {
// BOLT-0007. // BOLT-0007.
ChanID lnwire.ShortChannelID ChanID lnwire.ShortChannelID
// Capacity is the capacity of the channel expressed in satoshis. // Balance is the local balance of the channel expressed in satoshis.
Capacity btcutil.Amount Balance btcutil.Amount
// Node is the peer that this channel has been established with. // Node is the peer that this channel has been established with.
Node NodeID Node NodeID

@ -5,6 +5,7 @@ import (
"sync" "sync"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing" "github.com/lightningnetwork/lnd/routing"
@ -25,6 +26,10 @@ type ManagerCfg struct {
// channels managed by this node. // channels managed by this node.
ChannelState func() ([]LocalChannel, error) 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 // SubscribeTransactions is used to get a subscription for transactions
// relevant to this node's wallet. // relevant to this node's wallet.
SubscribeTransactions func() (lnwallet.TransactionSubscription, error) SubscribeTransactions func() (lnwallet.TransactionSubscription, error)
@ -194,18 +199,16 @@ func (m *Manager) StartAgent() error {
// opened, then we'll convert it to the // opened, then we'll convert it to the
// autopilot.Channel format, and notify // autopilot.Channel format, and notify
// the pilot of the new channel. // the pilot of the new channel.
chanNode := NewNodeID( cp := edgeUpdate.ChanPoint
edgeUpdate.ConnectingNode, edge, err := m.cfg.ChannelInfo(cp)
) if err != nil {
chanID := lnwire.NewShortChanIDFromInt( log.Errorf("Unable to fetch "+
edgeUpdate.ChanID, "channel info for %v: "+
) "%v", cp, err)
edge := LocalChannel{ continue
ChanID: chanID,
Capacity: edgeUpdate.Capacity,
Node: chanNode,
} }
pilot.OnChannelOpen(edge)
pilot.OnChannelOpen(*edge)
} }
// For each closed channel, we'll obtain // For each closed channel, we'll obtain

@ -260,16 +260,35 @@ func initAutoPilot(svr *server, cfg *lncfg.AutoPilot,
chanState := make([]autopilot.LocalChannel, chanState := make([]autopilot.LocalChannel,
len(activeChannels)) len(activeChannels))
for i, channel := range activeChannels { for i, channel := range activeChannels {
localCommit := channel.LocalCommitment
balance := localCommit.LocalBalance.ToSatoshis()
chanState[i] = autopilot.LocalChannel{ chanState[i] = autopilot.LocalChannel{
ChanID: channel.ShortChanID(), ChanID: channel.ShortChanID(),
Capacity: channel.Capacity, Balance: balance,
Node: autopilot.NewNodeID( Node: autopilot.NewNodeID(
channel.IdentityPub), channel.IdentityPub,
),
} }
} }
return chanState, nil 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, SubscribeTransactions: svr.cc.wallet.SubscribeTransactions,
SubscribeTopology: svr.chanRouter.SubscribeTopology, SubscribeTopology: svr.chanRouter.SubscribeTopology,
}, nil }, nil