forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 24
/
autopilot_server.go
294 lines (251 loc) · 8.26 KB
/
autopilot_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//go:build !no_autopilotrpc
// +build !no_autopilotrpc
package autopilotrpc
import (
"context"
"encoding/hex"
"sync/atomic"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/decred/dcrlnd/autopilot"
"github.com/decred/dcrlnd/lnrpc"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"gopkg.in/macaroon-bakery.v2/bakery"
)
const (
// subServerName is the name of the sub rpc server. We'll use this name
// to register ourselves, and we also require that the main
// SubServerConfigDispatcher instance recognize tt as the name of our
// RPC service.
subServerName = "AutopilotRPC"
)
var (
// macPermissions maps RPC calls to the permissions they require.
macPermissions = map[string][]bakery.Op{
"/autopilotrpc.Autopilot/Status": {{
Entity: "info",
Action: "read",
}},
"/autopilotrpc.Autopilot/ModifyStatus": {{
Entity: "onchain",
Action: "write",
}, {
Entity: "offchain",
Action: "write",
}},
"/autopilotrpc.Autopilot/QueryScores": {{
Entity: "info",
Action: "read",
}},
"/autopilotrpc.Autopilot/SetScores": {{
Entity: "onchain",
Action: "write",
}, {
Entity: "offchain",
Action: "write",
}},
}
)
// ServerShell is a shell struct holding a reference to the actual sub-server.
// It is used to register the gRPC sub-server with the root server before we
// have the necessary dependencies to populate the actual sub-server.
type ServerShell struct {
AutopilotServer
}
// Server is a sub-server of the main RPC server: the autopilot RPC. This sub
// RPC server allows external callers to access the status of the autopilot
// currently active within lnd, as well as configuring it at runtime.
type Server struct {
started int32 // To be used atomically.
shutdown int32 // To be used atomically.
cfg *Config
manager *autopilot.Manager
}
// A compile time check to ensure that Server fully implements the
// AutopilotServer gRPC service.
var _ AutopilotServer = (*Server)(nil)
// New returns a new instance of the autopilotrpc Autopilot sub-server. We also
// return the set of permissions for the macaroons that we may create within
// this method. If the macaroons we need aren't found in the filepath, then
// we'll create them on start up. If we're unable to locate, or create the
// macaroons we need, then we'll return with an error.
func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
// We don't create any new macaroons for this subserver, instead reuse
// existing onchain/offchain permissions.
server := &Server{
cfg: cfg,
manager: cfg.Manager,
}
return server, macPermissions, nil
}
// Start launches any helper goroutines required for the Server to function.
//
// NOTE: This is part of the lnrpc.SubServer interface.
func (s *Server) Start() error {
if atomic.AddInt32(&s.started, 1) != 1 {
return nil
}
return s.manager.Start()
}
// Stop signals any active goroutines for a graceful closure.
//
// NOTE: This is part of the lnrpc.SubServer interface.
func (s *Server) Stop() error {
if atomic.AddInt32(&s.shutdown, 1) != 1 {
return nil
}
return s.manager.Stop()
}
// Name returns a unique string representation of the sub-server. This can be
// used to identify the sub-server and also de-duplicate them.
//
// NOTE: This is part of the lnrpc.SubServer interface.
func (s *Server) Name() string {
return subServerName
}
// RegisterWithRootServer will be called by the root gRPC server to direct a
// sub RPC server to register itself with the main gRPC root server. Until this
// is called, each sub-server won't be able to have
// requests routed towards it.
//
// NOTE: This is part of the lnrpc.GrpcHandler interface.
func (r *ServerShell) RegisterWithRootServer(grpcServer *grpc.Server) error {
// We make sure that we register it with the main gRPC server to ensure
// all our methods are routed properly.
RegisterAutopilotServer(grpcServer, r)
log.Debugf("Autopilot RPC server successfully register with root " +
"gRPC server")
return nil
}
// RegisterWithRestServer will be called by the root REST mux to direct a sub
// RPC server to register itself with the main REST mux server. Until this is
// called, each sub-server won't be able to have requests routed towards it.
//
// NOTE: This is part of the lnrpc.GrpcHandler interface.
func (r *ServerShell) RegisterWithRestServer(ctx context.Context,
mux *runtime.ServeMux, dest string, opts []grpc.DialOption) error {
// We make sure that we register it with the main REST server to ensure
// all our methods are routed properly.
err := RegisterAutopilotHandlerFromEndpoint(ctx, mux, dest, opts)
if err != nil {
log.Errorf("Could not register Autopilot REST server "+
"with root REST server: %v", err)
return err
}
log.Debugf("Autopilot REST server successfully registered with " +
"root REST server")
return nil
}
// CreateSubServer populates the subserver's dependencies using the passed
// SubServerConfigDispatcher. This method should fully initialize the
// sub-server instance, making it ready for action. It returns the macaroon
// permissions that the sub-server wishes to pass on to the root server for all
// methods routed towards it.
//
// NOTE: This is part of the lnrpc.GrpcHandler interface.
func (r *ServerShell) CreateSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
subServer, macPermissions, err := createNewSubServer(configRegistry)
if err != nil {
return nil, nil, err
}
r.AutopilotServer = subServer
return subServer, macPermissions, nil
}
// Status returns the current status of the autopilot agent.
//
// NOTE: Part of the AutopilotServer interface.
func (s *Server) Status(ctx context.Context,
in *StatusRequest) (*StatusResponse, error) {
return &StatusResponse{
Active: s.manager.IsActive(),
}, nil
}
// ModifyStatus activates the current autopilot agent, if active.
//
// NOTE: Part of the AutopilotServer interface.
func (s *Server) ModifyStatus(ctx context.Context,
in *ModifyStatusRequest) (*ModifyStatusResponse, error) {
log.Debugf("Setting agent enabled=%v", in.Enable)
var err error
if in.Enable {
err = s.manager.StartAgent()
} else {
err = s.manager.StopAgent()
}
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 := secp256k1.ParsePubKey(pubHex)
if err != nil {
return nil, err
}
nID := autopilot.NewNodeID(pubKey)
nodes = append(nodes, nID)
}
// Query the heuristics.
heuristicScores, err := s.manager.QueryHeuristics(
nodes, !in.IgnoreLocalState,
)
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
}
// SetScores sets the scores of the external score heuristic, if active.
//
// NOTE: Part of the AutopilotServer interface.
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 := secp256k1.ParsePubKey(pubHex)
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
}