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

View File

@ -829,7 +829,7 @@ 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,
Balance: directive.ChanAmt,
Node: nodeID,
}
a.pendingMtx.Unlock()

View File

@ -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

View File

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

View File

@ -293,7 +293,7 @@ func TestAgentChannelOpenSignal(t *testing.T) {
// with a capacity of 1 BTC.
newChan := LocalChannel{
ChanID: randChanID(),
Capacity: btcutil.SatoshiPerBitcoin,
Balance: btcutil.SatoshiPerBitcoin,
}
testCtx.agent.OnChannelOpen(newChan)
@ -435,11 +435,11 @@ func TestAgentChannelCloseSignal(t *testing.T) {
initialChans := []LocalChannel{
{
ChanID: randChanID(),
Capacity: btcutil.SatoshiPerBitcoin,
Balance: btcutil.SatoshiPerBitcoin,
},
{
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 "+
"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",

View File

@ -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

View File

@ -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

View File

@ -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,
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