diff --git a/autopilot/agent.go b/autopilot/agent.go index 4bad0f93..6fe72f98 100644 --- a/autopilot/agent.go +++ b/autopilot/agent.go @@ -141,6 +141,10 @@ type Agent struct { // time. chanOpenFailures chan *chanOpenFailureUpdate + // heuristicUpdates is a channel where updates from active heurstics + // will be sent. + heuristicUpdates chan *heuristicUpdate + // totalBalance is the total number of satoshis the backing wallet is // known to control at any given instance. This value will be updated // when the agent receives external balance update signals. @@ -179,6 +183,7 @@ func New(cfg Config, initialState []Channel) (*Agent, error) { balanceUpdates: make(chan *balanceUpdate, 1), nodeUpdates: make(chan *nodeUpdates, 1), chanOpenFailures: make(chan *chanOpenFailureUpdate, 1), + heuristicUpdates: make(chan *heuristicUpdate, 1), pendingOpenUpdates: make(chan *chanPendingOpenUpdate, 1), failedNodes: make(map[NodeID]struct{}), pendingConns: make(map[NodeID]struct{}), @@ -256,6 +261,13 @@ type chanPendingOpenUpdate struct{} // a previous channel open failed, and that it might be possible to try again. type chanOpenFailureUpdate struct{} +// heuristicUpdate is an update sent when one of the autopilot heuristics has +// changed, and prompts the agent to make a new attempt at opening more +// channels. +type heuristicUpdate struct { + heuristic AttachmentHeuristic +} + // chanCloseUpdate is a type of external state update that indicates that the // backing Lightning Node has closed a previously open channel. type chanCloseUpdate struct { @@ -329,6 +341,17 @@ func (a *Agent) OnChannelClose(closedChans ...lnwire.ShortChannelID) { }() } +// OnHeuristicUpdate is a method called when a heuristic has been updated, to +// trigger the agent to do a new state assessment. +func (a *Agent) OnHeuristicUpdate(h AttachmentHeuristic) { + select { + case a.heuristicUpdates <- &heuristicUpdate{ + heuristic: h, + }: + default: + } +} + // mergeNodeMaps merges the Agent's set of nodes that it already has active // channels open to, with the other sets of nodes that should be removed from // consideration during heuristic selection. This ensures that the Agent doesn't @@ -470,6 +493,12 @@ func (a *Agent) controller() { log.Debugf("Node updates received, assessing " + "need for more channels") + // Any of the deployed heuristics has been updated, check + // whether we have new channel candidates available. + case upd := <-a.heuristicUpdates: + log.Debugf("Heuristic %v updated, assessing need for "+ + "more channels", upd.heuristic.Name()) + // The agent has been signalled to exit, so we'll bail out // immediately. case <-a.quit: diff --git a/autopilot/manager.go b/autopilot/manager.go index 86e0e08b..88f6c37d 100644 --- a/autopilot/manager.go +++ b/autopilot/manager.go @@ -358,6 +358,9 @@ func (m *Manager) queryHeuristics(nodes map[NodeID]struct{}, localState bool) ( // SetNodeScores is used to set the scores of the given heuristic, if it is // active, and ScoreSettable. func (m *Manager) SetNodeScores(name string, scores map[NodeID]float64) error { + m.Lock() + defer m.Unlock() + // It must be ScoreSettable to be available for external // scores. s, ok := m.cfg.PilotCfg.Heuristic.(ScoreSettable) @@ -376,5 +379,11 @@ func (m *Manager) SetNodeScores(name string, scores map[NodeID]float64) error { return fmt.Errorf("heuristic with name %v not found", name) } + // If the autopilot agent is active, notify about the updated + // heuristic. + if m.pilot != nil { + m.pilot.OnHeuristicUpdate(m.cfg.PilotCfg.Heuristic) + } + return nil } diff --git a/pilot.go b/pilot.go index b783c5b6..6fa3a6c6 100644 --- a/pilot.go +++ b/pilot.go @@ -31,7 +31,7 @@ func validateAtplCfg(cfg *autoPilotConfig) ([]*autopilot.WeightedHeuristic, for _, a := range autopilot.AvailableHeuristics { heuristicsStr += fmt.Sprintf(" '%v' ", a.Name()) } - availStr := fmt.Sprintf("Avaiblable heuristcs are: [%v]", heuristicsStr) + availStr := fmt.Sprintf("Available heuristics are: [%v]", heuristicsStr) // We'll go through the config and make sure all the heuristics exists, // and that the sum of their weights is 1.0.