/
query.go
409 lines (355 loc) · 10.7 KB
/
query.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package cli
import (
"context"
"encoding/base64"
"fmt"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/confio/tgrade/x/poe/types"
)
func GetQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the PoE module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
queryCmd.AddCommand(
GetCmdShowPoEContract(),
GetCmdQueryValidators(),
GetCmdQueryValidator(),
GetCmdQueryValidatorDelegation(),
GetCmdQueryValidatorUnbondingDelegations(),
GetCmdQueryUnbondingPeriod(),
GetCmdQueryHistoricalInfo(),
GetCmdQueryValidatorReward(),
)
return queryCmd
}
func GetCmdShowPoEContract() *cobra.Command {
cmd := &cobra.Command{
Use: "contract-address <contract_type>",
Short: "Show contract address for given contract type",
Long: fmt.Sprintf("Show contract address for PoE type [%s]", allPoEContractTypeNames()),
Aliases: []string{"ca"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
cbt := types.PoEContractTypeFrom(args[0])
if cbt == types.PoEContractTypeUndefined {
return fmt.Errorf("unknown contract type: %q", args[0])
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ContractAddress(
cmd.Context(),
&types.QueryContractAddressRequest{
ContractType: cbt,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func GetCmdQueryUnbondingPeriod() *cobra.Command {
cmd := &cobra.Command{
Use: "unbonding-period",
Short: "Query the global unbonding period",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.UnbondingPeriod(
cmd.Context(),
&types.QueryUnbondingPeriodRequest{},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidators implements the query all validators command.
func GetCmdQueryValidators() *cobra.Command {
cmd := &cobra.Command{
Use: "validators",
Short: "Query for all validators",
Args: cobra.NoArgs,
Long: strings.TrimSpace(
fmt.Sprintf(`Query details about all validators on a network.
Example:
$ %s query poe validators
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(withPageKeyDecoded(cmd.Flags()))
if err != nil {
return err
}
result, err := queryClient.Validators(cmd.Context(), &stakingtypes.QueryValidatorsRequest{
// Leaving status empty on purpose to query all validators.
Pagination: pageReq,
})
if err != nil {
return err
}
return clientCtx.PrintProto(result)
},
}
flags.AddQueryFlagsToCmd(cmd)
AddPaginationFlagsToCmd(cmd, "validators")
return cmd
}
// GetCmdQueryValidator implements the validator query command.
func GetCmdQueryValidator() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "validator [operator-addr]",
Short: "Query a validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details about an individual validator.
Example:
$ %s query poe validator %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixValAddr,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
params := &stakingtypes.QueryValidatorRequest{ValidatorAddr: addr.String()}
res, err := queryClient.Validator(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Validator)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// returns comma separated list of all poe contract type names
func allPoEContractTypeNames() string {
var names []string
types.IteratePoEContractTypes(func(tp types.PoEContractType) bool {
names = append(names, tp.String())
return false
})
return strings.Join(names, ", ")
}
// GetCmdQueryHistoricalInfo implements the historical info query command
func GetCmdQueryHistoricalInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "historical-info [height]",
Args: cobra.ExactArgs(1),
Short: "Query historical info at given height",
Long: strings.TrimSpace(
fmt.Sprintf(`Query historical info at given height.
Example:
$ %s query poe historical-info 5
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
height, err := strconv.ParseInt(args[0], 10, 64)
if err != nil || height < 0 {
return fmt.Errorf("height argument provided must be a non-negative-integer: %v", err)
}
params := &stakingtypes.QueryHistoricalInfoRequest{Height: height}
res, err := queryClient.HistoricalInfo(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res.Hist)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorDelegation implements the command to query the
// self delegation of a specific validator.
func GetCmdQueryValidatorDelegation() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "self-delegation [validator-addr]",
Short: "Query the delegation made by one validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Query delegations on an individual validator.
Example:
$ %s query poe self-delegation %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixValAddr,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
params := &types.QueryValidatorDelegationRequest{
ValidatorAddr: valAddr.String(),
}
res, err := queryClient.ValidatorDelegation(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegatations from a validator command.
func GetCmdQueryValidatorUnbondingDelegations() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "unbonding-delegations [validator-addr]",
Short: "Query all unbonding delegations from a validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Query delegations that are unbonding _from_ a validator.
Example:
$ %s query poe unbonding-delegations %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixValAddr,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(withPageKeyDecoded(cmd.Flags()))
if err != nil {
return err
}
params := &types.QueryValidatorUnbondingDelegationsRequest{
ValidatorAddr: valAddr.String(),
Pagination: pageReq,
}
res, err := queryClient.ValidatorUnbondingDelegations(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
AddPaginationFlagsToCmd(cmd, "unbonding delegations")
return cmd
}
// GetCmdQueryValidatorReward implements the command to query the
// claimable reward of a specific validator.
func GetCmdQueryValidatorReward() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "validator-reward [validator-addr]",
Short: "Query the pending reward by one validator",
Aliases: []string{"validator-rewards", "rewards", "reward"},
Long: strings.TrimSpace(
fmt.Sprintf(`Query reward of an individual validator.
Example:
$ %s query poe validator-reward %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixValAddr,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
req := &types.QueryValidatorOutstandingRewardRequest{
ValidatorAddress: valAddr.String(),
}
res, err := queryClient.ValidatorOutstandingReward(context.Background(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// AddPaginationFlagsToCmd adds common pagination flags to cmd
func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) {
// we support only a subset in the contracts yet
cmd.Flags().String(flags.FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query))
cmd.Flags().Uint64(flags.FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query))
}
// sdk ReadPageRequest expects binary but we encoded to base64 in our marshaller
func withPageKeyDecoded(flagSet *flag.FlagSet) *flag.FlagSet {
encoded, err := flagSet.GetString(flags.FlagPageKey)
if err != nil {
panic(err.Error())
}
raw, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err.Error())
}
err = flagSet.Set(flags.FlagPageKey, string(raw))
if err != nil {
panic(err.Error())
}
return flagSet
}