-
Notifications
You must be signed in to change notification settings - Fork 670
/
service.go
444 lines (386 loc) · 13.4 KB
/
service.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package info
import (
"errors"
"fmt"
"net/http"
"github.com/gorilla/rpc/v2"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network"
"github.com/ava-labs/avalanchego/network/peer"
"github.com/ava-labs/avalanchego/snow/networking/benchlist"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
var errNoChainProvided = errors.New("argument 'chain' not given")
// Info is the API service for unprivileged info on a node
type Info struct {
Parameters
log logging.Logger
validators validators.Manager
myIP ips.DynamicIPPort
networking network.Network
chainManager chains.Manager
vmManager vms.Manager
benchlist benchlist.Manager
}
type Parameters struct {
Version *version.Application
NodeID ids.NodeID
NodePOP *signer.ProofOfPossession
NetworkID uint32
TxFee uint64
CreateAssetTxFee uint64
CreateSubnetTxFee uint64
TransformSubnetTxFee uint64
CreateBlockchainTxFee uint64
AddPrimaryNetworkValidatorFee uint64
AddPrimaryNetworkDelegatorFee uint64
AddSubnetValidatorFee uint64
AddSubnetDelegatorFee uint64
VMManager vms.Manager
}
func NewService(
parameters Parameters,
log logging.Logger,
validators validators.Manager,
chainManager chains.Manager,
vmManager vms.Manager,
myIP ips.DynamicIPPort,
network network.Network,
benchlist benchlist.Manager,
) (http.Handler, error) {
server := rpc.NewServer()
codec := json.NewCodec()
server.RegisterCodec(codec, "application/json")
server.RegisterCodec(codec, "application/json;charset=UTF-8")
return server, server.RegisterService(
&Info{
Parameters: parameters,
log: log,
validators: validators,
chainManager: chainManager,
vmManager: vmManager,
myIP: myIP,
networking: network,
benchlist: benchlist,
},
"info",
)
}
// GetNodeVersionReply are the results from calling GetNodeVersion
type GetNodeVersionReply struct {
Version string `json:"version"`
DatabaseVersion string `json:"databaseVersion"`
RPCProtocolVersion json.Uint32 `json:"rpcProtocolVersion"`
GitCommit string `json:"gitCommit"`
VMVersions map[string]string `json:"vmVersions"`
}
// GetNodeVersion returns the version this node is running
func (i *Info) GetNodeVersion(_ *http.Request, _ *struct{}, reply *GetNodeVersionReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getNodeVersion"),
)
vmVersions, err := i.vmManager.Versions()
if err != nil {
return err
}
reply.Version = i.Version.String()
reply.DatabaseVersion = version.CurrentDatabase.String()
reply.RPCProtocolVersion = json.Uint32(version.RPCChainVMProtocol)
reply.GitCommit = version.GitCommit
reply.VMVersions = vmVersions
return nil
}
// GetNodeIDReply are the results from calling GetNodeID
type GetNodeIDReply struct {
NodeID ids.NodeID `json:"nodeID"`
NodePOP *signer.ProofOfPossession `json:"nodePOP"`
}
// GetNodeID returns the node ID of this node
func (i *Info) GetNodeID(_ *http.Request, _ *struct{}, reply *GetNodeIDReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getNodeID"),
)
reply.NodeID = i.NodeID
reply.NodePOP = i.NodePOP
return nil
}
// GetNetworkIDReply are the results from calling GetNetworkID
type GetNetworkIDReply struct {
NetworkID json.Uint32 `json:"networkID"`
}
// GetNodeIPReply are the results from calling GetNodeIP
type GetNodeIPReply struct {
IP string `json:"ip"`
}
// GetNodeIP returns the IP of this node
func (i *Info) GetNodeIP(_ *http.Request, _ *struct{}, reply *GetNodeIPReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getNodeIP"),
)
reply.IP = i.myIP.IPPort().String()
return nil
}
// GetNetworkID returns the network ID this node is running on
func (i *Info) GetNetworkID(_ *http.Request, _ *struct{}, reply *GetNetworkIDReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getNetworkID"),
)
reply.NetworkID = json.Uint32(i.NetworkID)
return nil
}
// GetNetworkNameReply is the result from calling GetNetworkName
type GetNetworkNameReply struct {
NetworkName string `json:"networkName"`
}
// GetNetworkName returns the network name this node is running on
func (i *Info) GetNetworkName(_ *http.Request, _ *struct{}, reply *GetNetworkNameReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getNetworkName"),
)
reply.NetworkName = constants.NetworkName(i.NetworkID)
return nil
}
// GetBlockchainIDArgs are the arguments for calling GetBlockchainID
type GetBlockchainIDArgs struct {
Alias string `json:"alias"`
}
// GetBlockchainIDReply are the results from calling GetBlockchainID
type GetBlockchainIDReply struct {
BlockchainID ids.ID `json:"blockchainID"`
}
// GetBlockchainID returns the blockchain ID that resolves the alias that was supplied
func (i *Info) GetBlockchainID(_ *http.Request, args *GetBlockchainIDArgs, reply *GetBlockchainIDReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getBlockchainID"),
)
bID, err := i.chainManager.Lookup(args.Alias)
reply.BlockchainID = bID
return err
}
// PeersArgs are the arguments for calling Peers
type PeersArgs struct {
NodeIDs []ids.NodeID `json:"nodeIDs"`
}
type Peer struct {
peer.Info
Benched []string `json:"benched"`
}
// PeersReply are the results from calling Peers
type PeersReply struct {
// Number of elements in [Peers]
NumPeers json.Uint64 `json:"numPeers"`
// Each element is a peer
Peers []Peer `json:"peers"`
}
// Peers returns the list of current validators
func (i *Info) Peers(_ *http.Request, args *PeersArgs, reply *PeersReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "peers"),
)
peers := i.networking.PeerInfo(args.NodeIDs)
peerInfo := make([]Peer, len(peers))
for index, peer := range peers {
benchedIDs := i.benchlist.GetBenched(peer.ID)
benchedAliases := make([]string, len(benchedIDs))
for idx, id := range benchedIDs {
alias, err := i.chainManager.PrimaryAlias(id)
if err != nil {
return fmt.Errorf("failed to get primary alias for chain ID %s: %w", id, err)
}
benchedAliases[idx] = alias
}
peerInfo[index] = Peer{
Info: peer,
Benched: benchedAliases,
}
}
reply.Peers = peerInfo
reply.NumPeers = json.Uint64(len(reply.Peers))
return nil
}
// IsBootstrappedArgs are the arguments for calling IsBootstrapped
type IsBootstrappedArgs struct {
// Alias of the chain
// Can also be the string representation of the chain's ID
Chain string `json:"chain"`
}
// IsBootstrappedResponse are the results from calling IsBootstrapped
type IsBootstrappedResponse struct {
// True iff the chain exists and is done bootstrapping
IsBootstrapped bool `json:"isBootstrapped"`
}
// IsBootstrapped returns nil and sets [reply.IsBootstrapped] == true iff [args.Chain] exists and is done bootstrapping
// Returns an error if the chain doesn't exist
func (i *Info) IsBootstrapped(_ *http.Request, args *IsBootstrappedArgs, reply *IsBootstrappedResponse) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "isBootstrapped"),
logging.UserString("chain", args.Chain),
)
if args.Chain == "" {
return errNoChainProvided
}
chainID, err := i.chainManager.Lookup(args.Chain)
if err != nil {
return fmt.Errorf("there is no chain with alias/ID '%s'", args.Chain)
}
reply.IsBootstrapped = i.chainManager.IsBootstrapped(chainID)
return nil
}
// UptimeResponse are the results from calling Uptime
type UptimeResponse struct {
// RewardingStakePercentage shows what percent of network stake thinks we're
// above the uptime requirement.
RewardingStakePercentage json.Float64 `json:"rewardingStakePercentage"`
// WeightedAveragePercentage is the average perceived uptime of this node,
// weighted by stake.
// Note that this is different from RewardingStakePercentage, which shows
// the percent of the network stake that thinks this node is above the
// uptime requirement. WeightedAveragePercentage is weighted by uptime.
// i.e If uptime requirement is 85 and a peer reports 40 percent it will be
// counted (40*weight) in WeightedAveragePercentage but not in
// RewardingStakePercentage since 40 < 85
WeightedAveragePercentage json.Float64 `json:"weightedAveragePercentage"`
}
type UptimeRequest struct {
// if omitted, defaults to primary network
SubnetID ids.ID `json:"subnetID"`
}
func (i *Info) Uptime(_ *http.Request, args *UptimeRequest, reply *UptimeResponse) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "uptime"),
)
result, err := i.networking.NodeUptime(args.SubnetID)
if err != nil {
return fmt.Errorf("couldn't get node uptime: %w", err)
}
reply.WeightedAveragePercentage = json.Float64(result.WeightedAveragePercentage)
reply.RewardingStakePercentage = json.Float64(result.RewardingStakePercentage)
return nil
}
type ACP struct {
SupportWeight json.Uint64 `json:"supportWeight"`
Supporters set.Set[ids.NodeID] `json:"supporters"`
ObjectWeight json.Uint64 `json:"objectWeight"`
Objectors set.Set[ids.NodeID] `json:"objectors"`
AbstainWeight json.Uint64 `json:"abstainWeight"`
}
type ACPsReply struct {
ACPs map[uint32]*ACP `json:"acps"`
}
func (a *ACPsReply) getACP(acpNum uint32) *ACP {
acp, ok := a.ACPs[acpNum]
if !ok {
acp = &ACP{}
a.ACPs[acpNum] = acp
}
return acp
}
func (i *Info) Acps(_ *http.Request, _ *struct{}, reply *ACPsReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "acps"),
)
reply.ACPs = make(map[uint32]*ACP, constants.CurrentACPs.Len())
peers := i.networking.PeerInfo(nil)
for _, peer := range peers {
weight := json.Uint64(i.validators.GetWeight(constants.PrimaryNetworkID, peer.ID))
if weight == 0 {
continue
}
for acpNum := range peer.SupportedACPs {
acp := reply.getACP(acpNum)
acp.Supporters.Add(peer.ID)
acp.SupportWeight += weight
}
for acpNum := range peer.ObjectedACPs {
acp := reply.getACP(acpNum)
acp.Objectors.Add(peer.ID)
acp.ObjectWeight += weight
}
}
totalWeight, err := i.validators.TotalWeight(constants.PrimaryNetworkID)
if err != nil {
return err
}
for acpNum := range constants.CurrentACPs {
acp := reply.getACP(acpNum)
acp.AbstainWeight = json.Uint64(totalWeight) - acp.SupportWeight - acp.ObjectWeight
}
return nil
}
type GetTxFeeResponse struct {
TxFee json.Uint64 `json:"txFee"`
CreateAssetTxFee json.Uint64 `json:"createAssetTxFee"`
CreateSubnetTxFee json.Uint64 `json:"createSubnetTxFee"`
TransformSubnetTxFee json.Uint64 `json:"transformSubnetTxFee"`
CreateBlockchainTxFee json.Uint64 `json:"createBlockchainTxFee"`
AddPrimaryNetworkValidatorFee json.Uint64 `json:"addPrimaryNetworkValidatorFee"`
AddPrimaryNetworkDelegatorFee json.Uint64 `json:"addPrimaryNetworkDelegatorFee"`
AddSubnetValidatorFee json.Uint64 `json:"addSubnetValidatorFee"`
AddSubnetDelegatorFee json.Uint64 `json:"addSubnetDelegatorFee"`
}
// GetTxFee returns the transaction fee in nAVAX.
func (i *Info) GetTxFee(_ *http.Request, _ *struct{}, reply *GetTxFeeResponse) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getTxFee"),
)
reply.TxFee = json.Uint64(i.TxFee)
reply.CreateAssetTxFee = json.Uint64(i.CreateAssetTxFee)
reply.CreateSubnetTxFee = json.Uint64(i.CreateSubnetTxFee)
reply.TransformSubnetTxFee = json.Uint64(i.TransformSubnetTxFee)
reply.CreateBlockchainTxFee = json.Uint64(i.CreateBlockchainTxFee)
reply.AddPrimaryNetworkValidatorFee = json.Uint64(i.AddPrimaryNetworkValidatorFee)
reply.AddPrimaryNetworkDelegatorFee = json.Uint64(i.AddPrimaryNetworkDelegatorFee)
reply.AddSubnetValidatorFee = json.Uint64(i.AddSubnetValidatorFee)
reply.AddSubnetDelegatorFee = json.Uint64(i.AddSubnetDelegatorFee)
return nil
}
// GetVMsReply contains the response metadata for GetVMs
type GetVMsReply struct {
VMs map[ids.ID][]string `json:"vms"`
Fxs map[ids.ID]string `json:"fxs"`
}
// GetVMs lists the virtual machines installed on the node
func (i *Info) GetVMs(_ *http.Request, _ *struct{}, reply *GetVMsReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "getVMs"),
)
// Fetch the VMs registered on this node.
vmIDs, err := i.VMManager.ListFactories()
if err != nil {
return err
}
reply.VMs, err = ids.GetRelevantAliases(i.VMManager, vmIDs)
reply.Fxs = map[ids.ID]string{
secp256k1fx.ID: secp256k1fx.Name,
nftfx.ID: nftfx.Name,
propertyfx.ID: propertyfx.Name,
}
return err
}