autopilot+lnrpc: wire up SetNodeScores RPC to set scores of agent

This commit is contained in:
Johan T. Halseth 2019-02-14 11:37:47 +01:00
parent b23e53ea33
commit 5dabb1ae29
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
2 changed files with 42 additions and 0 deletions

View File

@ -287,3 +287,27 @@ func (m *Manager) QueryHeuristics(nodes []NodeID) (HeuristicScores, error) {
log.Debugf("Querying heuristics for %d nodes", len(n))
return m.pilot.queryHeuristics(n)
}
// 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 {
// It must be ScoreSettable to be available for external
// scores.
s, ok := m.cfg.PilotCfg.Heuristic.(ScoreSettable)
if !ok {
return fmt.Errorf("current heuristic doesn't support " +
"external scoring")
}
// Heuristic was found, set its node scores.
applied, err := s.SetNodeScores(name, scores)
if err != nil {
return err
}
if !applied {
return fmt.Errorf("heuristic with name %v not found", name)
}
return nil
}

View File

@ -220,5 +220,23 @@ func (s *Server) QueryScores(ctx context.Context, in *QueryScoresRequest) (
func (s *Server) SetScores(ctx context.Context,
in *SetScoresRequest) (*SetScoresResponse, error) {
scores := make(map[autopilot.NodeID]float64)
for pubStr, score := range in.Scores {
pubHex, err := hex.DecodeString(pubStr)
if err != nil {
return nil, err
}
pubKey, err := btcec.ParsePubKey(pubHex, btcec.S256())
if err != nil {
return nil, err
}
nID := autopilot.NewNodeID(pubKey)
scores[nID] = score
}
if err := s.manager.SetNodeScores(in.Heuristic, scores); err != nil {
return nil, err
}
return &SetScoresResponse{}, nil
}