Update x/{mint,slashing,evidence} cli to use gRPC query client (#6704)

* changes cli to use gRPC in mint, slashing

* added command for signing infos

* gRPC query client migration of evidence

* review changes

* added unpack any

* fixed build error

* fixed failing tests issue

* added read flags

* added pagination flags

* updated docs

* fixed tests issue

* failing tests

* fixed tests

* review changes

* review changes

Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
This commit is contained in:
atheeshp 2020-07-19 15:46:57 +05:30 committed by GitHub
parent 13b5a8d670
commit 3123d07ad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 101 deletions

View File

@ -126,7 +126,7 @@ func AddTxFlagsToCmd(cmd *cobra.Command) {
viper.BindPFlag(FlagKeyringBackend, cmd.Flags().Lookup(FlagKeyringBackend))
}
// AddPaginationFlagsToCmd adds common paginations flags to command
// AddPaginationFlagsToCmd adds common pagination flags to cmd
func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) {
cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query))
cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query))

View File

@ -1,6 +1,7 @@
package cli
import (
"context"
"encoding/hex"
"fmt"
"strings"
@ -9,7 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
@ -17,7 +18,7 @@ import (
// GetQueryCmd returns the CLI command with all evidence module query commands
// mounted.
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Query for evidence by hash or for all (paginated) submitted evidence",
@ -34,81 +35,83 @@ $ %s query %s --page=2 --limit=50
Args: cobra.MaximumNArgs(1),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: QueryEvidenceCmd(cdc),
RunE: QueryEvidenceCmd(),
}
cmd.Flags().Int(flags.FlagPage, 1, "pagination page of evidence to to query for")
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of evidence to query for")
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "evidence")
return cmd
}
// QueryEvidenceCmd returns the command handler for evidence querying. Evidence
// can be queried for by hash or paginated evidence can be returned.
func QueryEvidenceCmd(cdc *codec.Codec) func(*cobra.Command, []string) error {
func QueryEvidenceCmd() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
if err := client.ValidateCmd(cmd, args); err != nil {
return err
}
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
if hash := args[0]; hash != "" {
return queryEvidence(cdc, clientCtx, hash)
return queryEvidence(clientCtx, hash)
}
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
return queryAllEvidence(clientCtx, page, limit)
return queryAllEvidence(clientCtx, client.ReadPageRequest(cmd.Flags()))
}
}
func queryEvidence(cdc *codec.Codec, clientCtx client.Context, hash string) error {
func queryEvidence(clientCtx client.Context, hash string) error {
decodedHash, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("invalid evidence hash: %w", err)
}
params := types.NewQueryEvidenceRequest(decodedHash)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return fmt.Errorf("failed to marshal query params: %w", err)
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryEvidenceRequest{EvidenceHash: decodedHash}
res, err := queryClient.Evidence(context.Background(), params)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryEvidence)
res, _, err := clientCtx.QueryWithData(route, bz)
if err != nil {
return err
}
var evidence exported.Evidence
err = cdc.UnmarshalJSON(res, &evidence)
if err != nil {
return fmt.Errorf("failed to unmarshal evidence: %w", err)
}
return clientCtx.PrintOutput(evidence)
}
func queryAllEvidence(clientCtx client.Context, page, limit int) error {
params := types.NewQueryAllEvidenceParams(page, limit)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
return fmt.Errorf("failed to marshal query params: %w", err)
}
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllEvidence)
res, _, err := clientCtx.QueryWithData(route, bz)
err = clientCtx.InterfaceRegistry.UnpackAny(res.Evidence, &evidence)
if err != nil {
return err
}
var evidence []exported.Evidence
err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &evidence)
return clientCtx.PrintOutput(evidence)
}
func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) error {
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAllEvidenceRequest{
Req: pageReq,
}
res, err := queryClient.AllEvidence(context.Background(), params)
if err != nil {
return fmt.Errorf("failed to unmarshal evidence: %w", err)
return err
}
evidence := make([]exported.Evidence, 0, len(res.Evidence))
for _, eviAny := range res.Evidence {
var evi exported.Evidence
err = clientCtx.InterfaceRegistry.UnpackAny(eviAny, &evi)
if err != nil {
return err
}
evidence = append(evidence, evi)
}
return clientCtx.PrintOutput(evidence)

View File

@ -96,9 +96,9 @@ func (a AppModuleBasic) GetTxCmd(clientCtx client.Context) *cobra.Command {
return cli.GetTxCmd(clientCtx, evidenceCLIHandlers)
}
// GetTxCmd returns the evidence module's root query command.
func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command {
return cli.GetQueryCmd(types.StoreKey, clientCtx.Codec)
// GetQueryCmd returns the evidence module's root query command.
func (AppModuleBasic) GetQueryCmd(_ client.Context) *cobra.Command {
return cli.GetQueryCmd()
}
func (AppModuleBasic) RegisterInterfaceTypes(registry codectypes.InterfaceRegistry) {

View File

@ -1,19 +1,17 @@
package cli
import (
"fmt"
"context"
"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/mint/types"
)
// GetQueryCmd returns the cli query commands for the minting module.
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
func GetQueryCmd() *cobra.Command {
mintingQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the minting module",
@ -23,9 +21,9 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
}
mintingQueryCmd.AddCommand(
GetCmdQueryParams(cdc),
GetCmdQueryInflation(cdc),
GetCmdQueryAnnualProvisions(cdc),
GetCmdQueryParams(),
GetCmdQueryInflation(),
GetCmdQueryAnnualProvisions(),
)
return mintingQueryCmd
@ -33,26 +31,28 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
// GetCmdQueryParams implements a command to return the current minting
// parameters.
func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command {
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current minting parameters",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters)
res, _, err := clientCtx.QueryWithData(route, nil)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
var params types.Params
if err := cdc.UnmarshalJSON(res, &params); err != nil {
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryParamsRequest{}
res, err := queryClient.Params(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintOutput(params)
return clientCtx.PrintOutput(res.Params)
},
}
@ -63,26 +63,28 @@ func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command {
// GetCmdQueryInflation implements a command to return the current minting
// inflation value.
func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command {
func GetCmdQueryInflation() *cobra.Command {
cmd := &cobra.Command{
Use: "inflation",
Short: "Query the current minting inflation value",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryInflation)
res, _, err := clientCtx.QueryWithData(route, nil)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
var inflation sdk.Dec
if err := cdc.UnmarshalJSON(res, &inflation); err != nil {
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryInflationRequest{}
res, err := queryClient.Inflation(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintOutput(inflation)
return clientCtx.PrintOutput(res.Inflation)
},
}
@ -93,26 +95,28 @@ func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command {
// GetCmdQueryAnnualProvisions implements a command to return the current minting
// annual provisions value.
func GetCmdQueryAnnualProvisions(cdc *codec.Codec) *cobra.Command {
func GetCmdQueryAnnualProvisions() *cobra.Command {
cmd := &cobra.Command{
Use: "annual-provisions",
Short: "Query the current minting annual provisions value",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAnnualProvisions)
res, _, err := clientCtx.QueryWithData(route, nil)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
var inflation sdk.Dec
if err := cdc.UnmarshalJSON(res, &inflation); err != nil {
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAnnualProvisionsRequest{}
res, err := queryClient.AnnualProvisions(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintOutput(inflation)
return clientCtx.PrintOutput(res.AnnualProvisions)
},
}

View File

@ -70,7 +70,7 @@ func (AppModuleBasic) GetTxCmd(_ client.Context) *cobra.Command { return nil }
// GetQueryCmd returns the root query command for the mint module.
func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command {
return cli.GetQueryCmd(clientCtx.Codec)
return cli.GetQueryCmd()
}
//____________________________________________________________________________

View File

@ -1,7 +1,7 @@
package cli
import (
"fmt"
"context"
"strings"
"github.com/spf13/cobra"
@ -27,6 +27,7 @@ func GetQueryCmd() *cobra.Command {
slashingQueryCmd.AddCommand(
GetCmdQuerySigningInfo(),
GetCmdQueryParams(),
GetCmdQuerySigningInfos(),
)
return slashingQueryCmd
@ -50,30 +51,21 @@ $ <appcli> query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdge
return err
}
queryClient := types.NewQueryClient(clientCtx)
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, types.StoreKey)
params := &types.QuerySigningInfoRequest{ConsAddress: consAddr}
res, err := queryClient.SigningInfo(context.Background(), params)
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)
return clientCtx.PrintOutput(res.ValSigningInfo)
},
}
@ -82,6 +74,41 @@ $ <appcli> query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdge
return cmd
}
// GetCmdQuerySigningInfos implements the command to query signing infos.
func GetCmdQuerySigningInfos() *cobra.Command {
cmd := &cobra.Command{
Use: "signing-infos",
Short: "Query signing information of all validators",
Long: strings.TrimSpace(`signing infos of validators:
$ <appcli> query slashing signing-infos
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QuerySigningInfosRequest{Req: client.ReadPageRequest(cmd.Flags())}
res, err := queryClient.SigningInfos(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintOutput(res.Info)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "signing infos")
return cmd
}
// GetCmdQueryParams implements a command to fetch slashing parameters.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
@ -99,18 +126,15 @@ $ <appcli> query slashing params
return err
}
route := fmt.Sprintf("custom/%s/parameters", types.StoreKey)
res, _, err := clientCtx.QueryWithData(route, nil)
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryParamsRequest{}
res, err := queryClient.Params(context.Background(), params)
if err != nil {
return err
}
var params types.Params
if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &params); err != nil {
return err
}
return clientCtx.PrintOutput(params)
return clientCtx.PrintOutput(res.Params)
},
}

View File

@ -125,7 +125,11 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.keeper)
}
func (am AppModule) RegisterQueryService(grpc.Server) {}
// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
}
// InitGenesis performs genesis initialization for the slashing module. It returns
// no validator updates.