lnrpc/autopilotrpc: implement QueryScores RPC

This commit is contained in:
Johan T. Halseth 2018-12-19 14:54:56 +01:00
parent a654be5884
commit 8874be764e
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

View File

@ -4,9 +4,11 @@ package autopilotrpc
import (
"context"
"encoding/hex"
"os"
"sync/atomic"
"github.com/btcsuite/btcd/btcec"
"github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/lnrpc"
"google.golang.org/grpc"
@ -35,6 +37,10 @@ var (
Entity: "offchain",
Action: "write",
}},
"/autopilotrpc.Autopilot/QueryScores": {{
Entity: "info",
Action: "read",
}},
}
)
@ -154,3 +160,60 @@ func (s *Server) ModifyStatus(ctx context.Context,
}
return &ModifyStatusResponse{}, err
}
// QueryScores queries all available autopilot heuristics, in addition to any
// active combination of these heruristics, for the scores they would give to
// the given nodes.
//
// NOTE: Part of the AutopilotServer interface.
func (s *Server) QueryScores(ctx context.Context, in *QueryScoresRequest) (
*QueryScoresResponse, error) {
var nodes []autopilot.NodeID
for _, pubStr := range in.Pubkeys {
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)
nodes = append(nodes, nID)
}
// Query the heuristics.
heuristicScores, err := s.manager.QueryHeuristics(nodes)
if err != nil {
return nil, err
}
resp := &QueryScoresResponse{}
for heuristic, scores := range heuristicScores {
result := &QueryScoresResponse_HeuristicResult{
Heuristic: heuristic,
Scores: make(map[string]float64),
}
for pub, score := range scores {
pubkeyHex := hex.EncodeToString(pub[:])
result.Scores[pubkeyHex] = score
}
// Since a node not being part of the internally returned
// scores imply a zero score, we add these before we return the
// RPC results.
for _, node := range nodes {
if _, ok := scores[node]; ok {
continue
}
pubkeyHex := hex.EncodeToString(node[:])
result.Scores[pubkeyHex] = 0.0
}
resp.Results = append(resp.Results, result)
}
return resp, nil
}