Skip to content

Commit

Permalink
autopilot: refresh available funds on channel open/close events
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Mar 11, 2018
1 parent ce2d5a2 commit 00ab664
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion autopilot/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ func (a *Agent) controller(startingBalance btcutil.Amount) {
pendingOpens := make(map[NodeID]Channel)
var pendingMtx sync.Mutex

updateBalance := func() {
newBalance, err := a.cfg.WalletBalance()
if err != nil {
log.Warnf("unable to update wallet balance: %v", err)
return
}

a.totalBalance = newBalance
}

// TODO(roasbeef): add 10-minute wake up timer
for {
select {
Expand All @@ -325,6 +335,8 @@ func (a *Agent) controller(startingBalance btcutil.Amount) {
case *chanOpenFailureUpdate:
log.Debug("Retrying after previous channel open failure.")

updateBalance()

// A new channel has been opened successfully. This was
// either opened by the Agent, or an external system
// that is able to drive the Lightning Node.
Expand All @@ -340,6 +352,8 @@ func (a *Agent) controller(startingBalance btcutil.Amount) {
delete(pendingOpens, newChan.Node)
pendingMtx.Unlock()

updateBalance()

// A channel has been closed, this may free up an
// available slot, triggering a new channel update.
case *chanCloseUpdate:
Expand All @@ -350,6 +364,8 @@ func (a *Agent) controller(startingBalance btcutil.Amount) {
for _, closedChan := range update.closedChans {
delete(a.chanState, closedChan)
}

updateBalance()
}

pendingMtx.Lock()
Expand All @@ -375,7 +391,8 @@ func (a *Agent) controller(startingBalance btcutil.Amount) {
continue
}

log.Infof("Triggering attachment directive dispatch")
log.Infof("Triggering attachment directive dispatch, "+
"total_funds=%v", a.totalBalance)

// We're to attempt an attachment so we'll o obtain the
// set of nodes that we currently have channels with so
Expand Down

0 comments on commit 00ab664

Please sign in to comment.