-
Notifications
You must be signed in to change notification settings - Fork 23
/
validator.go
200 lines (169 loc) · 5.34 KB
/
validator.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
package cmd
import (
"fmt"
"log"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
func printValidators(nodes []*sdk.Validator) {
if len(nodes) == 0 {
fmt.Println("no validators registered yet")
return
}
for _, validator := range nodes {
fmt.Println("id: ", validator.ID)
fmt.Println("url: ", validator.BaseURL)
fmt.Println("last_health_check: ", validator.LastHealthCheck.ToTime())
fmt.Println("is killed: ", validator.IsKilled)
fmt.Println("is shut down: ", validator.IsShutdown)
fmt.Println("settings:")
fmt.Println(" delegate_wallet:", validator.DelegateWallet)
fmt.Println(" min_stake: ", validator.MinStake)
fmt.Println(" max_stake: ", validator.MaxStake)
fmt.Println(" total_stake: ", validator.StakeTotal)
fmt.Println(" num_delegates: ", validator.NumDelegates)
fmt.Println(" service_charge: ", validator.ServiceCharge*100, "%")
}
}
// lsBlobers shows active blobbers
var lsValidators = &cobra.Command{
Use: "ls-validators",
Short: "Show active Validators.",
Long: `Show active Validators in the network.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
doJSON, _ := cmd.Flags().GetBool("json")
stakable, err := cmd.Flags().GetBool("stakable")
if err != nil {
log.Fatalf("error parsing stakable flag: %v", err)
}
list, err := sdk.GetValidators(stakable)
if err != nil {
log.Fatalf("Failed to get storage SC configurations: %v", err)
}
if doJSON {
util.PrintJSON(list)
} else {
printValidators(list)
}
},
}
var validatorInfoCmd = &cobra.Command{
Use: "validator-info",
Short: "Get validator info",
Long: `Get validator info`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
json bool
validatorID string
err error
)
if flags.Changed("json") {
if json, err = flags.GetBool("json"); err != nil {
log.Fatal("invalid 'json' flag: ", err)
}
}
if !flags.Changed("validator_id") {
log.Fatal("missing required 'validator_id' flag")
}
if validatorID, err = flags.GetString("validator_id"); err != nil {
log.Fatal("error in 'validator_id' flag: ", err)
}
var validator *sdk.Validator
if validator, err = sdk.GetValidator(validatorID); err != nil {
log.Fatal(err)
}
if json {
util.PrintJSON(validator)
} else {
printValidators([]*sdk.Validator{validator})
}
},
}
var validatorUpdateCmd = &cobra.Command{
Use: "validator-update",
Short: "Update validator settings by its delegate_wallet owner",
Long: `Update validator settings by its delegate_wallet owner`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
validatorID string
err error
)
if !flags.Changed("validator_id") {
log.Fatal("missing required 'validator_id' flag")
}
if validatorID, err = flags.GetString("validator_id"); err != nil {
log.Fatal("error in 'validator_id' flag: ", err)
}
// If validator with ID does not exist, throw.
if _, err = sdk.GetValidator(validatorID); err != nil {
log.Fatal(err)
}
updateValidator := new(sdk.UpdateValidator)
updateValidator.ID = common.Key(validatorID)
if flags.Changed("min_stake") {
var minStake float64
if minStake, err = flags.GetFloat64("min_stake"); err != nil {
log.Fatal(err)
}
stake, err := common.ToBalance(minStake)
if err != nil {
log.Fatal(err)
}
updateValidator.MinStake = &stake
}
if flags.Changed("max_stake") {
var maxStake float64
if maxStake, err = flags.GetFloat64("max_stake"); err != nil {
log.Fatal(err)
}
stake, err := common.ToBalance(maxStake)
if err != nil {
log.Fatal(err)
}
updateValidator.MaxStake = &stake
}
if flags.Changed("num_delegates") {
var nd int
if nd, err = flags.GetInt("num_delegates"); err != nil {
log.Fatal(err)
}
updateValidator.NumDelegates = &nd
}
if flags.Changed("service_charge") {
var sc float64
if sc, err = flags.GetFloat64("service_charge"); err != nil {
log.Fatal(err)
}
updateValidator.ServiceCharge = &sc
}
if _, _, err = sdk.UpdateValidatorSettings(updateValidator); err != nil {
log.Fatal(err)
}
fmt.Println("validator settings updated successfully")
},
}
func init() {
rootCmd.AddCommand(validatorUpdateCmd)
rootCmd.AddCommand(validatorInfoCmd)
rootCmd.AddCommand(lsValidators)
validatorInfoCmd.Flags().String("validator_id", "", "validator ID, required")
validatorInfoCmd.Flags().Bool("json", false,
"(default false) pass this option to print response as json data")
validatorInfoCmd.MarkFlagRequired("validator_id")
buf := validatorUpdateCmd.Flags()
buf.String("validator_id", "", "validator ID, required")
buf.Float64("min_stake", 0.0, "update min_stake, optional")
buf.Float64("max_stake", 0.0, "update max_stake, optional")
buf.Int("num_delegates", 0, "update num_delegates, optional")
buf.Float64("service_charge", 0.0, "update service_charge, optional")
validatorUpdateCmd.MarkFlagRequired("validator_id")
lsValidators.Flags().Bool("json", false, "(default false) pass this flag to get response as json object")
lsValidators.Flags().Bool("stakable", false, "(default false) Gets only validators that can be staked if set to true")
}