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)) 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) { func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) {
cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query)) 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)) cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query))

View File

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"context"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"strings" "strings"
@ -9,7 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags" "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/version"
"github.com/cosmos/cosmos-sdk/x/evidence/exported" "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/types" "github.com/cosmos/cosmos-sdk/x/evidence/types"
@ -17,7 +18,7 @@ import (
// GetQueryCmd returns the CLI command with all evidence module query commands // GetQueryCmd returns the CLI command with all evidence module query commands
// mounted. // mounted.
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: types.ModuleName, Use: types.ModuleName,
Short: "Query for evidence by hash or for all (paginated) submitted evidence", 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), Args: cobra.MaximumNArgs(1),
DisableFlagParsing: true, DisableFlagParsing: true,
SuggestionsMinimumDistance: 2, 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.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "evidence")
return cmd return cmd
} }
// QueryEvidenceCmd returns the command handler for evidence querying. Evidence // QueryEvidenceCmd returns the command handler for evidence querying. Evidence
// can be queried for by hash or paginated evidence can be returned. // 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 { return func(cmd *cobra.Command, args []string) error {
if err := client.ValidateCmd(cmd, args); err != nil { if err := client.ValidateCmd(cmd, args); err != nil {
return err return err
} }
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc) clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if hash := args[0]; hash != "" { if err != nil {
return queryEvidence(cdc, clientCtx, hash) return err
} }
page, _ := cmd.Flags().GetInt(flags.FlagPage) if hash := args[0]; hash != "" {
limit, _ := cmd.Flags().GetInt(flags.FlagLimit) return queryEvidence(clientCtx, hash)
}
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) decodedHash, err := hex.DecodeString(hash)
if err != nil { if err != nil {
return fmt.Errorf("invalid evidence hash: %w", err) return fmt.Errorf("invalid evidence hash: %w", err)
} }
params := types.NewQueryEvidenceRequest(decodedHash) queryClient := types.NewQueryClient(clientCtx)
bz, err := cdc.MarshalJSON(params)
if err != nil { params := &types.QueryEvidenceRequest{EvidenceHash: decodedHash}
return fmt.Errorf("failed to marshal query params: %w", err) 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 { if err != nil {
return err return err
} }
var evidence exported.Evidence var evidence exported.Evidence
err = cdc.UnmarshalJSON(res, &evidence) err = clientCtx.InterfaceRegistry.UnpackAny(res.Evidence, &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)
if err != nil { if err != nil {
return err return err
} }
var evidence []exported.Evidence return clientCtx.PrintOutput(evidence)
err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &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 { 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) return clientCtx.PrintOutput(evidence)

View File

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

View File

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

View File

@ -1,7 +1,7 @@
package cli package cli
import ( import (
"fmt" "context"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -27,6 +27,7 @@ func GetQueryCmd() *cobra.Command {
slashingQueryCmd.AddCommand( slashingQueryCmd.AddCommand(
GetCmdQuerySigningInfo(), GetCmdQuerySigningInfo(),
GetCmdQueryParams(), GetCmdQueryParams(),
GetCmdQuerySigningInfos(),
) )
return slashingQueryCmd return slashingQueryCmd
@ -50,30 +51,21 @@ $ <appcli> query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdge
return err return err
} }
queryClient := types.NewQueryClient(clientCtx)
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, args[0]) pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, args[0])
if err != nil { if err != nil {
return err return err
} }
consAddr := sdk.ConsAddress(pk.Address()) consAddr := sdk.ConsAddress(pk.Address())
key := types.ValidatorSigningInfoKey(consAddr) params := &types.QuerySigningInfoRequest{ConsAddress: consAddr}
res, err := queryClient.SigningInfo(context.Background(), params)
res, _, err := clientCtx.QueryStore(key, types.StoreKey)
if err != nil { if err != nil {
return err return err
} }
if len(res) == 0 { return clientCtx.PrintOutput(res.ValSigningInfo)
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)
}, },
} }
@ -82,6 +74,41 @@ $ <appcli> query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdge
return cmd 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. // GetCmdQueryParams implements a command to fetch slashing parameters.
func GetCmdQueryParams() *cobra.Command { func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -99,18 +126,15 @@ $ <appcli> query slashing params
return err return err
} }
route := fmt.Sprintf("custom/%s/parameters", types.StoreKey) queryClient := types.NewQueryClient(clientCtx)
res, _, err := clientCtx.QueryWithData(route, nil)
params := &types.QueryParamsRequest{}
res, err := queryClient.Params(context.Background(), params)
if err != nil { if err != nil {
return err return err
} }
var params types.Params return clientCtx.PrintOutput(res.Params)
if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &params); err != nil {
return err
}
return clientCtx.PrintOutput(params)
}, },
} }

View File

@ -125,7 +125,11 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.keeper) 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 // InitGenesis performs genesis initialization for the slashing module. It returns
// no validator updates. // no validator updates.