-
Notifications
You must be signed in to change notification settings - Fork 671
/
service.go
278 lines (237 loc) · 9.01 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
// Copyright (C) 2019-2021, 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/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"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"
)
var (
errNoChainProvided = errors.New("argument 'chain' not given")
errNotValidator = errors.New("this is not a validator node")
)
// Info is the API service for unprivileged info on a node
type Info struct {
Parameters
log logging.Logger
networking network.Network
chainManager chains.Manager
vmManager vms.Manager
versionParser version.ApplicationParser
validators validators.Set
}
type Parameters struct {
Version version.Application
NodeID ids.ShortID
NetworkID uint32
TxFee uint64
CreateAssetTxFee uint64
CreateSubnetTxFee uint64
CreateBlockchainTxFee uint64
}
// NewService returns a new admin API service
func NewService(
parameters Parameters,
log logging.Logger,
chainManager chains.Manager,
vmManager vms.Manager,
network network.Network,
versionParser version.ApplicationParser,
validators validators.Set,
) (*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,
networking: network,
versionParser: versionParser,
validators: validators,
}, "info"); err != nil {
return nil, err
}
return &common.HTTPHandler{Handler: newServer}, nil
}
// GetNodeVersionReply are the results from calling GetNodeVersion
type GetNodeVersionReply struct {
Version string `json:"version"`
DatabaseVersion string `json:"databaseVersion"`
GitCommit string `json:"gitCommit"`
VMVersions map[string]string `json:"vmVersions"`
}
// GetNodeVersion returns the version this node is running
func (service *Info) GetNodeVersion(_ *http.Request, _ *struct{}, reply *GetNodeVersionReply) error {
service.log.Debug("Info: GetNodeVersion called")
vmVersions, err := service.vmManager.Versions()
if err != nil {
return err
}
reply.Version = service.Version.String()
reply.DatabaseVersion = version.CurrentDatabase.String()
reply.GitCommit = version.GitCommit
reply.VMVersions = vmVersions
return nil
}
// GetNodeIDReply are the results from calling GetNodeID
type GetNodeIDReply struct {
NodeID string `json:"nodeID"`
}
// GetNodeID returns the node ID of this node
func (service *Info) GetNodeID(_ *http.Request, _ *struct{}, reply *GetNodeIDReply) error {
service.log.Debug("Info: GetNodeID called")
reply.NodeID = service.NodeID.PrefixedString(constants.NodeIDPrefix)
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 (service *Info) GetNodeIP(_ *http.Request, _ *struct{}, reply *GetNodeIPReply) error {
service.log.Debug("Info: GetNodeIP called")
reply.IP = service.networking.IP().String()
return nil
}
// GetNetworkID returns the network ID this node is running on
func (service *Info) GetNetworkID(_ *http.Request, _ *struct{}, reply *GetNetworkIDReply) error {
service.log.Debug("Info: GetNetworkID called")
reply.NetworkID = json.Uint32(service.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 (service *Info) GetNetworkName(_ *http.Request, _ *struct{}, reply *GetNetworkNameReply) error {
service.log.Debug("Info: GetNetworkName called")
reply.NetworkName = constants.NetworkName(service.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 (service *Info) GetBlockchainID(_ *http.Request, args *GetBlockchainIDArgs, reply *GetBlockchainIDReply) error {
service.log.Debug("Info: GetBlockchainID called")
bID, err := service.chainManager.Lookup(args.Alias)
reply.BlockchainID = bID
return err
}
// PeersArgs are the arguments for calling Peers
type PeersArgs struct {
NodeIDs []string `json:"nodeIDs"`
}
// 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 []network.PeerInfo `json:"peers"`
}
// Peers returns the list of current validators
func (service *Info) Peers(_ *http.Request, args *PeersArgs, reply *PeersReply) error {
service.log.Debug("Info: Peers called")
nodeIDs := make([]ids.ShortID, 0, len(args.NodeIDs))
for _, nodeID := range args.NodeIDs {
nID, err := ids.ShortFromPrefixedString(nodeID, constants.NodeIDPrefix)
if err != nil {
return err
}
nodeIDs = append(nodeIDs, nID)
}
reply.Peers = service.networking.Peers(nodeIDs)
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 (service *Info) IsBootstrapped(_ *http.Request, args *IsBootstrappedArgs, reply *IsBootstrappedResponse) error {
service.log.Debug("Info: IsBootstrapped called with chain: %s", args.Chain)
if args.Chain == "" {
return errNoChainProvided
}
chainID, err := service.chainManager.Lookup(args.Chain)
if err != nil {
return fmt.Errorf("there is no chain with alias/ID '%s'", args.Chain)
}
reply.IsBootstrapped = service.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"`
}
func (service *Info) Uptime(_ *http.Request, _ *struct{}, reply *UptimeResponse) error {
service.log.Debug("Info: Uptime called")
result, isValidator := service.networking.NodeUptime()
if !isValidator {
return errNotValidator
}
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"`
CreateBlockchainTxFee json.Uint64 `json:"createBlockchainTxFee"`
}
// GetTxFee returns the transaction fee in nAVAX.
func (service *Info) GetTxFee(_ *http.Request, args *struct{}, reply *GetTxFeeResponse) error {
reply.TxFee = json.Uint64(service.TxFee)
reply.CreationTxFee = json.Uint64(service.CreateAssetTxFee)
reply.CreateAssetTxFee = json.Uint64(service.CreateAssetTxFee)
reply.CreateSubnetTxFee = json.Uint64(service.CreateSubnetTxFee)
reply.CreateBlockchainTxFee = json.Uint64(service.CreateBlockchainTxFee)
return nil
}