diff --git a/autopilot/manager.go b/autopilot/manager.go index 80c99435..283aab02 100644 --- a/autopilot/manager.go +++ b/autopilot/manager.go @@ -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 +} diff --git a/lnrpc/autopilotrpc/autopilot_server.go b/lnrpc/autopilotrpc/autopilot_server.go index af6713ec..5f31384b 100644 --- a/lnrpc/autopilotrpc/autopilot_server.go +++ b/lnrpc/autopilotrpc/autopilot_server.go @@ -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 }