cosmos-sdk/x/slashing/client/cli/query.go
SaReN 39f53ac22f
client: rename CliContext to Context (#6290)
* Refactor CliContext as Context

* Fix lint issues

* Fix goimports

* Fix gov tests

* Resolved ci-lint issues

* Add changelog

* Rename cliCtx to clientCtx

* Fix mocks and routes

* Add changelog

* Update changelog

* Apply suggestions from code review

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* merge client/rpc/ro{ot,utes}.go

* Update docs

* client/rpc: remove redundant client/rpc.RegisterRPCRoutes

* regenerate mocks

* Update ADRs

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-06-01 12:46:03 +00:00

105 lines
2.9 KiB
Go

package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// Group slashing queries under a subcommand
slashingQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the slashing module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
slashingQueryCmd.AddCommand(
flags.GetCommands(
GetCmdQuerySigningInfo(queryRoute, cdc),
GetCmdQueryParams(cdc),
)...,
)
return slashingQueryCmd
}
// GetCmdQuerySigningInfo implements the command to query signing info.
func GetCmdQuerySigningInfo(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "signing-info [validator-conspub]",
Short: "Query a validator's signing information",
Long: strings.TrimSpace(`Use a validators' consensus public key to find the signing-info for that validator:
$ <appcli> query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdgexxhmz0l8c7sgswl7ulv7aulk364x4g5xsw7sr0k2g5
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, args[0])
if err != nil {
return err
}
consAddr := sdk.ConsAddress(pk.Address())
key := types.ValidatorSigningInfoKey(consAddr)
res, _, err := clientCtx.QueryStore(key, storeName)
if err != nil {
return err
}
if len(res) == 0 {
return fmt.Errorf("validator %s not found in slashing store", consAddr)
}
var signingInfo types.ValidatorSigningInfo
signingInfo, err = types.UnmarshalValSigningInfo(types.ModuleCdc, res)
if err != nil {
return err
}
return clientCtx.PrintOutput(signingInfo)
},
}
}
// GetCmdQueryParams implements a command to fetch slashing parameters.
func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "Query the current slashing parameters",
Args: cobra.NoArgs,
Long: strings.TrimSpace(`Query genesis parameters for the slashing module:
$ <appcli> query slashing params
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute)
res, _, err := clientCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var params types.Params
cdc.MustUnmarshalJSON(res, &params)
return clientCtx.PrintOutput(params)
},
}
}