Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 29 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
43d5e10
changes cli to use gRPC in mint, slashing
atheeshp Jul 13, 2020
38e5997
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 13, 2020
c62b7f9
added command for signing infos
atheeshp Jul 13, 2020
d05357b
fixed conflicts
atheeshp Jul 15, 2020
e41526b
gRPC query client migration of evidence
atheeshp Jul 15, 2020
9b496da
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 15, 2020
9330c0d
review changes
atheeshp Jul 15, 2020
b49a3d8
added unpack any
atheeshp Jul 15, 2020
b567595
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 15, 2020
376a1bb
fixed build error
atheeshp Jul 16, 2020
527ca98
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 16, 2020
fabd40b
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 16, 2020
3c1e672
Merge branch 'master' into atheesh/5921-grpc-cli-mint-slashing
anilcse Jul 16, 2020
ffcebd5
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 17, 2020
b21db20
Merge branch 'atheesh/5921-grpc-cli-mint-slashing' of github.com:cosm…
atheeshp Jul 17, 2020
90c9617
fixed failing tests issue
atheeshp Jul 17, 2020
ccdd48b
added read flags
atheeshp Jul 17, 2020
96bf62f
Merge branch 'master' into atheesh/5921-grpc-cli-mint-slashing
anilcse Jul 17, 2020
f5a48cb
Merge branch 'master' into atheesh/5921-grpc-cli-mint-slashing
anilcse Jul 17, 2020
e8f2988
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 18, 2020
9294e70
added pagination flags
atheeshp Jul 18, 2020
811f648
updated docs
atheeshp Jul 18, 2020
89df4e0
fixed tests issue
atheeshp Jul 18, 2020
ae543c5
Merge branch 'atheesh/5921-grpc-cli-mint-slashing' of github.com:cosm…
atheeshp Jul 18, 2020
7779cb0
failing tests
atheeshp Jul 18, 2020
aa4481c
fixed tests
atheeshp Jul 18, 2020
8f82500
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 19, 2020
1c1978f
review changes
atheeshp Jul 19, 2020
61e4101
review changes
atheeshp Jul 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
69 changes: 36 additions & 33 deletions x/evidence/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"encoding/hex"
"fmt"
"strings"
Expand All @@ -9,15 +10,15 @@ 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"
)

// 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",
Expand All @@ -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)
err = clientCtx.InterfaceRegistry.UnpackAny(res.Evidence, &evidence)
if err != nil {
return fmt.Errorf("failed to unmarshal evidence: %w", err)
return 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)
func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) error {
queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryAllEvidenceRequest{
Req: pageReq,
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllEvidence)
res, _, err := clientCtx.QueryWithData(route, bz)
res, err := queryClient.AllEvidence(context.Background(), params)

if err != nil {
return err
}

var evidence []exported.Evidence
err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &evidence)
if err != nil {
return fmt.Errorf("failed to unmarshal evidence: %w", 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)
Expand Down
6 changes: 3 additions & 3 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
66 changes: 35 additions & 31 deletions x/mint/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -23,36 +21,38 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
}

mintingQueryCmd.AddCommand(
GetCmdQueryParams(cdc),
GetCmdQueryInflation(cdc),
GetCmdQueryAnnualProvisions(cdc),
GetCmdQueryParams(),
GetCmdQueryInflation(),
GetCmdQueryAnnualProvisions(),
)

return mintingQueryCmd
}

// 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)
},
}

Expand All @@ -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)
},
}

Expand All @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

//____________________________________________________________________________
Expand Down
Loading