-
Notifications
You must be signed in to change notification settings - Fork 55
/
validators.go
126 lines (104 loc) · 3.18 KB
/
validators.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package subnetcmd
import (
"errors"
"os"
"strconv"
"time"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/subnet"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var validatorsSupportedNetworkOptions = []networkoptions.NetworkOption{networkoptions.Local, networkoptions.Fuji, networkoptions.Mainnet, networkoptions.Cluster, networkoptions.Devnet}
// avalanche subnet validators
func newValidatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validators [subnetName]",
Short: "List a subnet's validators",
Long: `The subnet validators command lists the validators of a subnet and provides
severarl statistics about them.`,
RunE: printValidators,
Args: cobrautils.ExactArgs(1),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, false, validatorsSupportedNetworkOptions)
return cmd
}
func printValidators(_ *cobra.Command, args []string) error {
subnetName := args[0]
network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
"",
globalNetworkFlags,
false,
false,
validatorsSupportedNetworkOptions,
subnetName,
)
if err != nil {
return err
}
// get the subnetID
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return err
}
deployInfo, ok := sc.Networks[network.Name()]
if !ok {
return errors.New("no deployment found for subnet")
}
subnetID := deployInfo.SubnetID
if network.Kind == models.Local {
return printLocalValidators(subnetID)
} else {
return printPublicValidators(subnetID, network)
}
}
func printLocalValidators(subnetID ids.ID) error {
validators, err := subnet.GetSubnetValidators(subnetID)
if err != nil {
return err
}
return printValidatorsFromList(validators)
}
func printPublicValidators(subnetID ids.ID, network models.Network) error {
validators, err := subnet.GetPublicSubnetValidators(subnetID, network)
if err != nil {
return err
}
return printValidatorsFromList(validators)
}
func printValidatorsFromList(validators []platformvm.ClientPermissionlessValidator) error {
header := []string{"NodeID", "Stake Amount", "Delegator Weight", "Start Time", "End Time", "Type"}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
table.SetRowLine(true)
for _, validator := range validators {
var delegatorWeight uint64
if validator.DelegatorWeight != nil {
delegatorWeight = *validator.DelegatorWeight
}
validatorType := "permissioned"
if validator.PotentialReward != nil && *validator.PotentialReward > 0 {
validatorType = "elastic"
}
table.Append([]string{
validator.NodeID.String(),
strconv.FormatUint(*validator.StakeAmount, 10),
strconv.FormatUint(delegatorWeight, 10),
formatUnixTime(validator.StartTime),
formatUnixTime(validator.EndTime),
validatorType,
})
}
table.Render()
return nil
}
func formatUnixTime(unixTime uint64) string {
return time.Unix(int64(unixTime), 0).Format(time.RFC3339)
}