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:
parent
d40cf6b592
commit
dbeafe8832
@ -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()
|
||||
|
||||
|
@ -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
|
||||
|
@ -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),
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
25
pilot.go
25
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
|
||||
|
Loading…
Reference in New Issue
Block a user