-
Notifications
You must be signed in to change notification settings - Fork 201
/
tx_change_validator_weight.go
140 lines (120 loc) · 3.31 KB
/
tx_change_validator_weight.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
package cli
import (
"encoding/json"
"os"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/Stride-Labs/stride/v20/x/stakeibc/types"
)
type ValidatorWeightList struct {
ValidatorWeights []*types.ValidatorWeight `json:"validator_weights,omitempty"`
}
// Parse a JSON with a list of validators in the format
//
// {
// "validator_weights": [
// {"address": "cosmosXXX", "weight": 1},
// {"address": "cosmosXXX", "weight": 2}
// ]
// }
func parseChangeValidatorWeightsFile(validatorsFile string) (weights []*types.ValidatorWeight, err error) {
fileContents, err := os.ReadFile(validatorsFile)
if err != nil {
return weights, err
}
var weightsList ValidatorWeightList
if err = json.Unmarshal(fileContents, &weightsList); err != nil {
return weights, err
}
return weightsList.ValidatorWeights, nil
}
// Updates the weight for a single validator
func CmdChangeValidatorWeight() *cobra.Command {
cmd := &cobra.Command{
Use: "change-validator-weight [host-zone] [address] [weight]",
Short: "Broadcast message change-validator-weight to update the weight for a single validator",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
hostZone := args[0]
valAddress := args[1]
weight, err := cast.ToUint64E(args[2])
if err != nil {
return err
}
weights := []*types.ValidatorWeight{
{
Address: valAddress,
Weight: weight,
},
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgChangeValidatorWeights(
clientCtx.GetFromAddress().String(),
hostZone,
weights,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// Updates the weight for multiple validators
//
// Accepts a file in the following format:
//
// {
// "validator_weights": [
// {"address": "cosmosXXX", "weight": 1},
// {"address": "cosmosXXX", "weight": 2}
// ]
// }
func CmdChangeMultipleValidatorWeight() *cobra.Command {
cmd := &cobra.Command{
Use: "change-validator-weights [host-zone] [validator-weight-file]",
Short: "Broadcast message change-validator-weights to update the weights for multiple validators",
Long: strings.TrimSpace(
`Changes multiple validator weights at once, using a JSON file in the following format
{
"validator_weights": [
{"address": "cosmosXXX", "weight": 1},
{"address": "cosmosXXX", "weight": 2}
]
}
`),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
hostZone := args[0]
validatorWeightChangeFile := args[1]
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
weights, err := parseChangeValidatorWeightsFile(validatorWeightChangeFile)
if err != nil {
return err
}
msg := types.NewMsgChangeValidatorWeights(
clientCtx.GetFromAddress().String(),
hostZone,
weights,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}