-
Notifications
You must be signed in to change notification settings - Fork 662
/
service.go
339 lines (290 loc) · 11 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
// Copyright (C) 2019-2022, 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"
"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/engine/common"
"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/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
)
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
myIP ips.DynamicIPPort
networking network.Network
chainManager chains.Manager
vmManager vms.Manager
validators validators.Set
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
}
// NewService returns a new admin API service
func NewService(
parameters Parameters,
log logging.Logger,
chainManager chains.Manager,
vmManager vms.Manager,
myIP ips.DynamicIPPort,
network network.Network,
validators validators.Set,
benchlist benchlist.Manager,
) (*common.HTTPHandler, error) {
newServer := rpc.NewServer()
codec := json.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
if err := newServer.RegisterService(&Info{
Parameters: parameters,
log: log,
chainManager: chainManager,
vmManager: vmManager,
myIP: myIP,
networking: network,
validators: validators,
benchlist: benchlist,
}, "info"); err != nil {
return nil, err
}
return &common.HTTPHandler{
LockOptions: common.NoLock,
Handler: newServer,
}, nil
}
// 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("Info: GetNodeVersion called")
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("Info: GetNodeID called")
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("Info: GetNodeIP called")
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("Info: GetNetworkID called")
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("Info: GetNetworkName called")
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("Info: GetBlockchainID called")
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 []ids.ID `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("Info: Peers called")
peers := i.networking.PeerInfo(args.NodeIDs)
peerInfo := make([]Peer, len(peers))
for index, peer := range peers {
peerInfo[index] = Peer{
Info: peer,
Benched: i.benchlist.GetBenched(peer.ID),
}
}
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("Info: IsBootstrapped called",
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("Info: Uptime called")
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 GetTxFeeResponse struct {
TxFee json.Uint64 `json:"txFee"`
// TODO: remove [CreationTxFee] after enough time for dependencies to update
CreationTxFee json.Uint64 `json:"creationTxFee"`
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 {
reply.TxFee = json.Uint64(i.TxFee)
reply.CreationTxFee = json.Uint64(i.CreateAssetTxFee)
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"`
}
// GetVMs lists the virtual machines installed on the node
func (i *Info) GetVMs(_ *http.Request, _ *struct{}, reply *GetVMsReply) error {
i.log.Debug("Info: GetVMs called")
// 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)
return err
}