|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/cosmos/cosmos-sdk/client" |
| 8 | + "github.com/cosmos/cosmos-sdk/client/flags" |
| 9 | + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" |
| 10 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 11 | + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +// GetQueryCmd returns the slashing query commands. |
| 16 | +func GetQueryCmd() *cobra.Command { |
| 17 | + slashingQueryCmd := &cobra.Command{ |
| 18 | + Use: slashingtypes.ModuleName, |
| 19 | + Short: fmt.Sprintf("Querying commands for the %s module", slashingtypes.ModuleName), |
| 20 | + DisableFlagParsing: true, |
| 21 | + SuggestionsMinimumDistance: 2, |
| 22 | + RunE: client.ValidateCmd, |
| 23 | + } |
| 24 | + |
| 25 | + slashingQueryCmd.AddCommand( |
| 26 | + GetCmdQueryParams(), |
| 27 | + GetCmdQuerySigningInfo(), |
| 28 | + GetCmdQuerySigningInfos(), |
| 29 | + ) |
| 30 | + |
| 31 | + return slashingQueryCmd |
| 32 | +} |
| 33 | + |
| 34 | +// GetCmdQueryParams returns the current slashing parameters. |
| 35 | +func GetCmdQueryParams() *cobra.Command { |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Use: "params", |
| 38 | + Short: "Query the current slashing parameters", |
| 39 | + Args: cobra.NoArgs, |
| 40 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 41 | + clientCtx, err := client.GetClientQueryContext(cmd) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + queryClient := slashingtypes.NewQueryClient(clientCtx) |
| 47 | + res, err := queryClient.Params(cmd.Context(), &slashingtypes.QueryParamsRequest{}) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + return clientCtx.PrintProto(res) |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + flags.AddQueryFlagsToCmd(cmd) |
| 57 | + |
| 58 | + return cmd |
| 59 | +} |
| 60 | + |
| 61 | +// GetCmdQuerySigningInfo queries a validator's signing information. |
| 62 | +func GetCmdQuerySigningInfo() *cobra.Command { |
| 63 | + cmd := &cobra.Command{ |
| 64 | + Use: "signing-info [validator-conspub/address]", |
| 65 | + Short: "Query a validator's signing information", |
| 66 | + Long: "Query a validator's signing information, with a pubkey ('<appd> comet show-validator') or a validator consensus address", |
| 67 | + Args: cobra.ExactArgs(1), |
| 68 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 69 | + clientCtx, err := client.GetClientQueryContext(cmd) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + consAddr, err := normalizeConsAddressArg(clientCtx, args[0]) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + queryClient := slashingtypes.NewQueryClient(clientCtx) |
| 80 | + res, err := queryClient.SigningInfo( |
| 81 | + cmd.Context(), |
| 82 | + &slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddr}, |
| 83 | + ) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + return clientCtx.PrintProto(res) |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + flags.AddQueryFlagsToCmd(cmd) |
| 93 | + |
| 94 | + return cmd |
| 95 | +} |
| 96 | + |
| 97 | +// GetCmdQuerySigningInfos queries signing information for all validators. |
| 98 | +func GetCmdQuerySigningInfos() *cobra.Command { |
| 99 | + cmd := &cobra.Command{ |
| 100 | + Use: "signing-infos", |
| 101 | + Short: "Query signing information of all validators", |
| 102 | + Args: cobra.NoArgs, |
| 103 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 104 | + clientCtx, err := client.GetClientQueryContext(cmd) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + pageReq, err := client.ReadPageRequest(cmd.Flags()) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + queryClient := slashingtypes.NewQueryClient(clientCtx) |
| 115 | + res, err := queryClient.SigningInfos( |
| 116 | + cmd.Context(), |
| 117 | + &slashingtypes.QuerySigningInfosRequest{Pagination: pageReq}, |
| 118 | + ) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + return clientCtx.PrintProto(res) |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + flags.AddQueryFlagsToCmd(cmd) |
| 128 | + flags.AddPaginationFlagsToCmd(cmd, "signing infos") |
| 129 | + |
| 130 | + return cmd |
| 131 | +} |
| 132 | + |
| 133 | +func normalizeConsAddressArg(clientCtx client.Context, arg string) (string, error) { |
| 134 | + arg = strings.TrimSpace(arg) |
| 135 | + |
| 136 | + if _, err := sdk.ConsAddressFromBech32(arg); err == nil { |
| 137 | + return arg, nil |
| 138 | + } |
| 139 | + |
| 140 | + var pubKey cryptotypes.PubKey |
| 141 | + if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(arg), &pubKey); err == nil && pubKey != nil { |
| 142 | + return sdk.GetConsAddress(pubKey).String(), nil |
| 143 | + } |
| 144 | + |
| 145 | + return "", fmt.Errorf("invalid consensus address or pubkey JSON: %q", arg) |
| 146 | +} |
0 commit comments