From 51281473c87202ad026e1c56f1687a8aaa06bce2 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 9 Jul 2020 16:58:43 +0200 Subject: [PATCH 01/16] refactor CLI to use grpc query service --- x/params/client/cli/query.go | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index ce5bc77afd10..adb0b504e82b 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -1,18 +1,18 @@ 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" "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/cosmos/cosmos-sdk/x/params/types/proposal" ) // NewQueryCmd returns a root CLI command handler for all x/params query commands. -func NewQueryCmd(m codec.JSONMarshaler) *cobra.Command { +func NewQueryCmd() *cobra.Command { cmd := &cobra.Command{ Use: types.ModuleName, Short: "Querying commands for the params module", @@ -21,42 +21,32 @@ func NewQueryCmd(m codec.JSONMarshaler) *cobra.Command { RunE: client.ValidateCmd, } - cmd.AddCommand(NewQuerySubspaceParamsCmd(m)) + cmd.AddCommand(flags.GetCommands( + NewQuerySubspaceParamsCmd(), + )...) return cmd } // NewQuerySubspaceParamsCmd returns a CLI command handler for querying subspace // parameters managed by the x/params module. -func NewQuerySubspaceParamsCmd(m codec.JSONMarshaler) *cobra.Command { - cmd := &cobra.Command{ +func NewQuerySubspaceParamsCmd() *cobra.Command { + return &cobra.Command{ Use: "subspace [subspace] [key]", Short: "Query for raw parameters by subspace and key", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := client.NewContext().WithJSONMarshaler(m) - - params := types.NewQuerySubspaceParams(args[0], args[1]) - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams) + clientCtx := client.GetClientContextFromCmd(cmd) + queryClient := proposal.NewQueryClient(clientCtx) - bz, err := m.MarshalJSON(params) - if err != nil { - return fmt.Errorf("failed to marshal params: %w", err) - } + params := proposal.NewQueryParametersRequest(args[0], args[1]) - bz, _, err = clientCtx.QueryWithData(route, bz) + resp, err := queryClient.Parameters(context.Background(), params) if err != nil { return err } - var resp types.SubspaceParamsResponse - if err := m.UnmarshalJSON(bz, &resp); err != nil { - return err - } - - return clientCtx.PrintOutput(resp) + return clientCtx.PrintOutput(resp.Params) }, } - - return flags.GetCommands(cmd)[0] } From 6acad213c1e35f64e9ab045950b6d035507747ca Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 10 Jul 2020 17:01:59 +0200 Subject: [PATCH 02/16] In process CLI for gov --- x/gov/client/cli/query.go | 183 +++++++++++++++++++++++--------------- 1 file changed, 112 insertions(+), 71 deletions(-) diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index 561a45d1385b..4b4901f747ab 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -9,7 +9,6 @@ import ( "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/version" gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" @@ -17,7 +16,7 @@ import ( ) // GetQueryCmd returns the cli query commands for this module -func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetQueryCmd() *cobra.Command { // Group gov queries under a subcommand govQueryCmd := &cobra.Command{ Use: types.ModuleName, @@ -28,22 +27,23 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { } govQueryCmd.AddCommand(flags.GetCommands( - GetCmdQueryProposal(queryRoute, cdc), - GetCmdQueryProposals(queryRoute, cdc), - GetCmdQueryVote(queryRoute, cdc), - GetCmdQueryVotes(queryRoute, cdc), - GetCmdQueryParam(queryRoute, cdc), - GetCmdQueryParams(queryRoute, cdc), - GetCmdQueryProposer(queryRoute, cdc), - GetCmdQueryDeposit(queryRoute, cdc), - GetCmdQueryDeposits(queryRoute, cdc), - GetCmdQueryTally(queryRoute, cdc))...) + GetCmdQueryProposal(), + GetCmdQueryProposals(), + GetCmdQueryVote(), + GetCmdQueryVotes(), + GetCmdQueryParam(), + GetCmdQueryParams(), + GetCmdQueryProposer(), + GetCmdQueryDeposit(), + GetCmdQueryDeposits(), + GetCmdQueryTally(), + )...) return govQueryCmd } // GetCmdQueryProposal implements the query proposal command. -func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryProposal() *cobra.Command { return &cobra.Command{ Use: "proposal [proposal-id]", Args: cobra.ExactArgs(1), @@ -59,7 +59,11 @@ $ %s query gov proposal 1 ), ), 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()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -68,20 +72,20 @@ $ %s query gov proposal 1 } // Query the proposal - res, err := gcutils.QueryProposalByID(proposalID, clientCtx, queryRoute) + res, err := gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) if err != nil { return err } var proposal types.Proposal - cdc.MustUnmarshalJSON(res, &proposal) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &proposal) return clientCtx.PrintOutput(proposal) // nolint:errcheck }, } } // GetCmdQueryProposals implements a query proposals command. -func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryProposals() *cobra.Command { cmd := &cobra.Command{ Use: "proposals", Short: "Query proposals with optional filters", @@ -134,20 +138,24 @@ $ %s query gov proposals --page=2 --limit=100 params.ProposalStatus = proposalStatus } - bz, err := cdc.MarshalJSON(params) + clientCtx := client.GetClientContextFromCmd(cmd) + clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags()) if err != nil { return err } - clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) + if err != nil { + return err + } - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", queryRoute), bz) + res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", types.QuerierRoute), bz) if err != nil { return err } var matchingProposals types.Proposals - err = cdc.UnmarshalJSON(res, &matchingProposals) + err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &matchingProposals) if err != nil { return err } @@ -171,7 +179,7 @@ $ %s query gov proposals --page=2 --limit=100 // Command to Get a Proposal Information // GetCmdQueryVote implements the query proposal vote command. -func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryVote() *cobra.Command { return &cobra.Command{ Use: "vote [proposal-id] [voter-addr]", Args: cobra.ExactArgs(2), @@ -186,7 +194,11 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk ), ), 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()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -195,7 +207,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // check to see if the proposal is in the store - _, err = gcutils.QueryProposalByID(proposalID, clientCtx, queryRoute) + _, err = gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } @@ -206,12 +218,12 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } params := types.NewQueryVoteParams(proposalID, voterAddr) - bz, err := cdc.MarshalJSON(params) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if err != nil { return err } - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", queryRoute), bz) + res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", types.QuerierRoute), bz) if err != nil { return err } @@ -221,7 +233,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk // XXX: Allow the decoding to potentially fail as the vote may have been // pruned from state. If so, decoding will fail and so we need to check the // Empty() case. Consider updating Vote JSON decoding to not fail when empty. - _ = cdc.UnmarshalJSON(res, &vote) + _ = clientCtx.JSONMarshaler.UnmarshalJSON(res, &vote) if vote.Empty() { res, err = gcutils.QueryVoteByTxQuery(clientCtx, params) @@ -229,7 +241,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk return err } - if err := cdc.UnmarshalJSON(res, &vote); err != nil { + if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &vote); err != nil { return err } } @@ -240,7 +252,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // GetCmdQueryVotes implements the command to query for proposal votes. -func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryVotes() *cobra.Command { cmd := &cobra.Command{ Use: "votes [proposal-id]", Args: cobra.ExactArgs(1), @@ -256,7 +268,11 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 ), ), 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()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -268,25 +284,25 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 limit, _ := cmd.Flags().GetInt(flags.FlagLimit) params := types.NewQueryProposalVotesParams(proposalID, page, limit) - bz, err := cdc.MarshalJSON(params) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if err != nil { return err } // check to see if the proposal is in the store - res, err := gcutils.QueryProposalByID(proposalID, clientCtx, queryRoute) + res, err := gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } var proposal types.Proposal - cdc.MustUnmarshalJSON(res, &proposal) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &proposal) propStatus := proposal.Status if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) { res, err = gcutils.QueryVotesByTxQuery(clientCtx, params) } else { - res, _, err = clientCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz) + res, _, err = clientCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", types.QuerierRoute), bz) } if err != nil { @@ -294,7 +310,7 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 } var votes types.Votes - cdc.MustUnmarshalJSON(res, &votes) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &votes) return clientCtx.PrintOutput(votes) }, } @@ -305,7 +321,7 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 // Command to Get a specific Deposit Information // GetCmdQueryDeposit implements the query proposal deposit command. -func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryDeposit() *cobra.Command { return &cobra.Command{ Use: "deposit [proposal-id] [depositer-addr]", Args: cobra.ExactArgs(2), @@ -320,7 +336,11 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk ), ), 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()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -329,7 +349,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // check to see if the proposal is in the store - _, err = gcutils.QueryProposalByID(proposalID, clientCtx, queryRoute) + _, err = gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } @@ -340,25 +360,25 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } params := types.NewQueryDepositParams(proposalID, depositorAddr) - bz, err := cdc.MarshalJSON(params) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if err != nil { return err } - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute), bz) + res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", types.QuerierRoute), bz) if err != nil { return err } var deposit types.Deposit - cdc.MustUnmarshalJSON(res, &deposit) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &deposit) if deposit.Empty() { res, err = gcutils.QueryDepositByTxQuery(clientCtx, params) if err != nil { return err } - cdc.MustUnmarshalJSON(res, &deposit) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &deposit) } return clientCtx.PrintOutput(deposit) @@ -367,7 +387,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // GetCmdQueryDeposits implements the command to query for proposal deposits. -func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryDeposits() *cobra.Command { return &cobra.Command{ Use: "deposits [proposal-id]", Args: cobra.ExactArgs(1), @@ -383,7 +403,11 @@ $ %s query gov deposits 1 ), ), 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()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -392,25 +416,25 @@ $ %s query gov deposits 1 } params := types.NewQueryProposalParams(proposalID) - bz, err := cdc.MarshalJSON(params) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if err != nil { return err } // check to see if the proposal is in the store - res, err := gcutils.QueryProposalByID(proposalID, clientCtx, queryRoute) + res, err := gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) if err != nil { return fmt.Errorf("failed to fetch proposal with id %d: %s", proposalID, err) } var proposal types.Proposal - cdc.MustUnmarshalJSON(res, &proposal) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &proposal) propStatus := proposal.Status if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) { res, err = gcutils.QueryDepositsByTxQuery(clientCtx, params) } else { - res, _, err = clientCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz) + res, _, err = clientCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", types.QuerierRoute), bz) } if err != nil { @@ -418,14 +442,14 @@ $ %s query gov deposits 1 } var dep types.Deposits - cdc.MustUnmarshalJSON(res, &dep) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &dep) return clientCtx.PrintOutput(dep) }, } } // GetCmdQueryTally implements the command to query for proposal tally result. -func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryTally() *cobra.Command { return &cobra.Command{ Use: "tally [proposal-id]", Args: cobra.ExactArgs(1), @@ -441,7 +465,11 @@ $ %s query gov tally 1 ), ), 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()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -450,33 +478,33 @@ $ %s query gov tally 1 } // check to see if the proposal is in the store - _, err = gcutils.QueryProposalByID(proposalID, clientCtx, queryRoute) + _, err = gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } // Construct query params := types.NewQueryProposalParams(proposalID) - bz, err := cdc.MarshalJSON(params) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if err != nil { return err } // Query store - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", queryRoute), bz) + res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", types.QuerierRoute), bz) if err != nil { return err } var tally types.TallyResult - cdc.MustUnmarshalJSON(res, &tally) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &tally) return clientCtx.PrintOutput(tally) }, } } // GetCmdQueryProposal implements the query proposal command. -func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryParams() *cobra.Command { return &cobra.Command{ Use: "params", Short: "Query the parameters of the governance process", @@ -491,26 +519,31 @@ $ %s query gov params ), Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc) - tp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/tallying", queryRoute), nil) + clientCtx := client.GetClientContextFromCmd(cmd) + clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags()) if err != nil { return err } - dp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/deposit", queryRoute), nil) + + tp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/tallying", types.QuerierRoute), nil) if err != nil { return err } - vp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/voting", queryRoute), nil) + dp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/deposit", types.QuerierRoute), nil) + if err != nil { + return err + } + vp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/voting", types.QuerierRoute), nil) if err != nil { return err } var tallyParams types.TallyParams - cdc.MustUnmarshalJSON(tp, &tallyParams) + clientCtx.JSONMarshaler.MustUnmarshalJSON(tp, &tallyParams) var depositParams types.DepositParams - cdc.MustUnmarshalJSON(dp, &depositParams) + clientCtx.JSONMarshaler.MustUnmarshalJSON(dp, &depositParams) var votingParams types.VotingParams - cdc.MustUnmarshalJSON(vp, &votingParams) + clientCtx.JSONMarshaler.MustUnmarshalJSON(vp, &votingParams) return clientCtx.PrintOutput(types.NewParams(votingParams, tallyParams, depositParams)) }, @@ -518,7 +551,7 @@ $ %s query gov params } // GetCmdQueryProposal implements the query proposal command. -func GetCmdQueryParam(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryParam() *cobra.Command { return &cobra.Command{ Use: "param [param-type]", Args: cobra.ExactArgs(1), @@ -535,10 +568,14 @@ $ %s query gov param deposit ), ), 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()) + if err != nil { + return err + } // Query store - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/%s", queryRoute, args[0]), nil) + res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/%s", types.QuerierRoute, args[0]), nil) if err != nil { return err } @@ -546,15 +583,15 @@ $ %s query gov param deposit switch args[0] { case "voting": var param types.VotingParams - cdc.MustUnmarshalJSON(res, ¶m) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, ¶m) out = param case "tallying": var param types.TallyParams - cdc.MustUnmarshalJSON(res, ¶m) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, ¶m) out = param case "deposit": var param types.DepositParams - cdc.MustUnmarshalJSON(res, ¶m) + clientCtx.JSONMarshaler.MustUnmarshalJSON(res, ¶m) out = param default: return fmt.Errorf("argument must be one of (voting|tallying|deposit), was %s", args[0]) @@ -566,7 +603,7 @@ $ %s query gov param deposit } // GetCmdQueryProposer implements the query proposer command. -func GetCmdQueryProposer(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryProposer() *cobra.Command { return &cobra.Command{ Use: "proposer [proposal-id]", Args: cobra.ExactArgs(1), @@ -581,7 +618,11 @@ $ %s query gov proposer 1 ), ), 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()) + if err != nil { + return err + } // validate that the proposalID is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) From a5971cb7a61de7f1d2d8ded19dcb91f986604ed9 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 10 Jul 2020 17:04:53 +0200 Subject: [PATCH 03/16] ReadQueryCommandFlags --- x/upgrade/client/cli/query.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/x/upgrade/client/cli/query.go b/x/upgrade/client/cli/query.go index 21783826f159..3bfd6c21f840 100644 --- a/x/upgrade/client/cli/query.go +++ b/x/upgrade/client/cli/query.go @@ -34,6 +34,10 @@ func GetCurrentPlanCmd() *cobra.Command { Args: cobra.ExactArgs(0), 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.NewQueryCurrentPlanRequest() @@ -65,6 +69,10 @@ func GetAppliedPlanCmd() *cobra.Command { 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) name := args[0] From 14b50360d95941fe455b31247c1abd6c14ccc077 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 10 Jul 2020 17:07:46 +0200 Subject: [PATCH 04/16] gov tx --- x/gov/client/cli/tx.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index fe5e7733f2cf..6a414a968a32 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -62,14 +62,14 @@ func NewTxCmd( RunE: client.ValidateCmd, } - cmdSubmitProp := NewCmdSubmitProposal(ctx) + cmdSubmitProp := NewCmdSubmitProposal() for _, pcmd := range pcmds { cmdSubmitProp.AddCommand(flags.PostCommands(pcmd)[0]) } govTxCmd.AddCommand(flags.PostCommands( - NewCmdDeposit(ctx), - NewCmdVote(ctx), + NewCmdDeposit(), + NewCmdVote(), cmdSubmitProp, )...) @@ -77,7 +77,7 @@ func NewTxCmd( } // NewCmdSubmitProposal implements submitting a proposal transaction command. -func NewCmdSubmitProposal(clientCtx client.Context) *cobra.Command { +func NewCmdSubmitProposal() *cobra.Command { cmd := &cobra.Command{ Use: "submit-proposal", Short: "Submit a proposal along with an initial deposit", @@ -105,7 +105,11 @@ $ %s tx gov submit-proposal --title="Test Proposal" --description="My awesome pr ), ), RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := clientCtx.InitWithInput(cmd.InOrStdin()) + clientCtx := client.GetClientContextFromCmd(cmd) + clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags()) + if err != nil { + return err + } proposal, err := parseSubmitProposalFlags(cmd.Flags()) if err != nil { @@ -142,7 +146,7 @@ $ %s tx gov submit-proposal --title="Test Proposal" --description="My awesome pr } // NewCmdDeposit implements depositing tokens for an active proposal. -func NewCmdDeposit(clientCtx client.Context) *cobra.Command { +func NewCmdDeposit() *cobra.Command { return &cobra.Command{ Use: "deposit [proposal-id] [deposit]", Args: cobra.ExactArgs(2), @@ -158,7 +162,11 @@ $ %s tx gov deposit 1 10stake --from mykey ), ), RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := clientCtx.InitWithInput(cmd.InOrStdin()) + clientCtx := client.GetClientContextFromCmd(cmd) + clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags()) + if err != nil { + return err + } // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -187,7 +195,7 @@ $ %s tx gov deposit 1 10stake --from mykey } // NewCmdVote implements creating a new vote command. -func NewCmdVote(clientCtx client.Context) *cobra.Command { +func NewCmdVote() *cobra.Command { return &cobra.Command{ Use: "vote [proposal-id] [option]", Args: cobra.ExactArgs(2), @@ -204,7 +212,11 @@ $ %s tx gov vote 1 yes --from mykey ), ), RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := clientCtx.InitWithInput(cmd.InOrStdin()) + clientCtx := client.GetClientContextFromCmd(cmd) + clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags()) + if err != nil { + return err + } // Get voting address from := clientCtx.GetFromAddress() From eb73cbd225055298fd0c3e4d0df31d8698a4dc42 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 10 Jul 2020 17:31:33 +0200 Subject: [PATCH 05/16] Fix compiler errors --- x/gov/client/cli/parse_test.go | 3 +-- x/gov/client/cli/tx.go | 1 - x/gov/module.go | 8 ++++---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/x/gov/client/cli/parse_test.go b/x/gov/client/cli/parse_test.go index 8ac3e70e2fcf..37e41f57c56d 100644 --- a/x/gov/client/cli/parse_test.go +++ b/x/gov/client/cli/parse_test.go @@ -5,7 +5,6 @@ import ( "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/testutil" ) @@ -23,7 +22,7 @@ func TestParseSubmitProposalFlags(t *testing.T) { badJSON, cleanup2 := testutil.WriteToNewTempFile(t, "bad json") t.Cleanup(cleanup2) - fs := NewCmdSubmitProposal(client.Context{}).Flags() + fs := NewCmdSubmitProposal().Flags() // nonexistent json fs.Set(FlagProposal, "fileDoesNotExist") diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 6a414a968a32..05e5e3c247d5 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -51,7 +51,6 @@ var ProposalFlags = []string{ // to proposal type handlers that are implemented in other modules but are mounted // under the governance CLI (eg. parameter change proposals). func NewTxCmd( - ctx client.Context, pcmds []*cobra.Command, ) *cobra.Command { govTxCmd := &cobra.Command{ diff --git a/x/gov/module.go b/x/gov/module.go index cc5b179511ed..2e4fdb696f56 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -85,18 +85,18 @@ func (a AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Ro } // GetTxCmd returns the root tx command for the gov module. -func (a AppModuleBasic) GetTxCmd(clientCtx client.Context) *cobra.Command { +func (a AppModuleBasic) GetTxCmd(_ client.Context) *cobra.Command { proposalCLIHandlers := make([]*cobra.Command, 0, len(a.proposalHandlers)) for _, proposalHandler := range a.proposalHandlers { proposalCLIHandlers = append(proposalCLIHandlers, proposalHandler.CLIHandler()) } - return cli.NewTxCmd(clientCtx, proposalCLIHandlers) + return cli.NewTxCmd(proposalCLIHandlers) } // GetQueryCmd returns the root query command for the gov module. -func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command { - return cli.GetQueryCmd(types.StoreKey, clientCtx.Codec) +func (AppModuleBasic) GetQueryCmd(_ client.Context) *cobra.Command { + return cli.GetQueryCmd() } // RegisterInterfaceTypes implements InterfaceModule.RegisterInterfaceTypes From adac5e15a6b9ed0e5d1f591cae6dce6e20a6125f Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Mon, 13 Jul 2020 11:09:45 +0200 Subject: [PATCH 06/16] Formatting --- x/gov/client/cli/tx.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 2512c2efecd2..d7145ec6b4ac 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -50,9 +50,7 @@ var ProposalFlags = []string{ // it contains a slice of "proposal" child commands. These commands are respective // to proposal type handlers that are implemented in other modules but are mounted // under the governance CLI (eg. parameter change proposals). -func NewTxCmd( - pcmds []*cobra.Command, -) *cobra.Command { +func NewTxCmd(propCmds []*cobra.Command) *cobra.Command { govTxCmd := &cobra.Command{ Use: types.ModuleName, Short: "Governance transactions subcommands", @@ -62,8 +60,8 @@ func NewTxCmd( } cmdSubmitProp := NewCmdSubmitProposal() - for _, pcmd := range pcmds { - cmdSubmitProp.AddCommand(pcmd) + for _, propCmd := range propCmds { + cmdSubmitProp.AddCommand(propCmd) } govTxCmd.AddCommand( From b1fa4e39fe828175b96452bc5f2b6855f0b2a762 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 15 Jul 2020 10:48:36 +0200 Subject: [PATCH 07/16] x/distribution: use gRPC query --- x/distribution/client/cli/query.go | 121 +++++++++++------------------ 1 file changed, 44 insertions(+), 77 deletions(-) diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index d0364aa39825..0803f4dd0cff 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -1,6 +1,7 @@ package cli import ( + "context" "fmt" "strconv" "strings" @@ -11,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" - "github.com/cosmos/cosmos-sdk/x/distribution/client/common" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) @@ -49,19 +49,14 @@ func GetCmdQueryParams() *cobra.Command { if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams) - res, _, err := clientCtx.QueryWithData(route, nil) + res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{}) if err != nil { return err } - var params types.Params - if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, ¶ms); err != nil { - return fmt.Errorf("failed to unmarshal params: %w", err) - } - - return clientCtx.PrintOutput(params) + return clientCtx.PrintOutput(res.GetParams()) }, } @@ -91,32 +86,22 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) - valAddr, err := sdk.ValAddressFromBech32(args[0]) - if err != nil { - return err - } - - params := types.NewQueryValidatorOutstandingRewardsParams(valAddr) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) + validatorAddr, err := sdk.ValAddressFromBech32(args[0]) if err != nil { return err } - resp, _, err := clientCtx.QueryWithData( - fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorOutstandingRewards), - bz, + res, err := queryClient.ValidatorOutstandingRewards( + context.Background(), + &types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: validatorAddr}, ) if err != nil { return err } - var outstandingRewards types.ValidatorOutstandingRewards - if err := clientCtx.JSONMarshaler.UnmarshalJSON(resp, &outstandingRewards); err != nil { - return err - } - - return clientCtx.PrintOutput(outstandingRewards) + return clientCtx.PrintOutput(res.GetRewards()) }, } @@ -145,23 +130,22 @@ $ %s query distribution commission cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9l if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) validatorAddr, err := sdk.ValAddressFromBech32(args[0]) if err != nil { return err } - res, err := common.QueryValidatorCommission(clientCtx, validatorAddr) + res, err := queryClient.ValidatorCommission( + context.Background(), + &types.QueryValidatorCommissionRequest{ValidatorAddress: validatorAddr}, + ) if err != nil { return err } - var valCom types.ValidatorAccumulatedCommission - if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &valCom); err != nil { - return err - } - - return clientCtx.PrintOutput(valCom) + return clientCtx.PrintOutput(res.GetCommission()) }, } @@ -190,6 +174,7 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) validatorAddr, err := sdk.ValAddressFromBech32(args[0]) if err != nil { @@ -206,23 +191,15 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq return fmt.Errorf("end-height %s not a valid uint, please input a valid end-height", args[2]) } - params := types.NewQueryValidatorSlashesParams(validatorAddr, startHeight, endHeight) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return err - } - - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", types.QuerierRoute), bz) + res, err := queryClient.ValidatorSlashes( + context.Background(), + &types.QueryValidatorSlashesRequest{ValidatorAddress: validatorAddr, StartingHeight: startHeight, EndingHeight: endHeight}, + ) if err != nil { return err } - var slashes []types.ValidatorSlashEvent - if err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &slashes); err != nil { - return fmt.Errorf("failed to unmarshal response: %w", err) - } - - return clientCtx.PrintOutput(slashes) + return clientCtx.PrintOutput(res.GetSlashes()) }, } @@ -252,46 +229,40 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) + + delegatorAddr, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } // query for rewards from a particular delegation if len(args) == 2 { - resp, _, err := common.QueryDelegationRewards(clientCtx, args[0], args[1]) + validatorAddr, err := sdk.ValAddressFromBech32(args[1]) if err != nil { return err } - var result sdk.DecCoins - if err = clientCtx.JSONMarshaler.UnmarshalJSON(resp, &result); err != nil { - return fmt.Errorf("failed to unmarshal response: %w", err) + res, err := queryClient.DelegationRewards( + context.Background(), + &types.QueryDelegationRewardsRequest{DelegatorAddress: delegatorAddr, ValidatorAddress: validatorAddr}, + ) + if err != nil { + return err } - return clientCtx.PrintOutput(result) + return clientCtx.PrintOutput(res.GetRewards()) } - delegatorAddr, err := sdk.AccAddressFromBech32(args[0]) - if err != nil { - return err - } - - params := types.NewQueryDelegatorParams(delegatorAddr) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return fmt.Errorf("failed to marshal params: %w", err) - } - - // query for delegator total rewards - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorTotalRewards) - res, _, err := clientCtx.QueryWithData(route, bz) + res, err := queryClient.DelegationTotalRewards( + context.Background(), + &types.QueryDelegationTotalRewardsRequest{DelegatorAddress: delegatorAddr}, + ) if err != nil { return err } - var result types.QueryDelegatorTotalRewardsResponse - if err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &result); err != nil { - return fmt.Errorf("failed to unmarshal response: %w", err) - } - - return clientCtx.PrintOutput(result) + return clientCtx.PrintOutput(res.GetRewards()) }, } @@ -320,18 +291,14 @@ $ %s query distribution community-pool if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", types.QuerierRoute), nil) + res, err := queryClient.CommunityPool(context.Background(), &types.QueryCommunityPoolRequest{}) if err != nil { return err } - var result sdk.DecCoins - if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &result); err != nil { - return err - } - - return clientCtx.PrintOutput(result) + return clientCtx.PrintOutput(res.GetPool()) }, } From 1012219814d78df4761512257879b9725a4d6ca0 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 15 Jul 2020 10:52:22 +0200 Subject: [PATCH 08/16] Consistent --- x/distribution/client/cli/query.go | 2 +- x/params/client/cli/query.go | 4 ++-- x/upgrade/client/cli/query.go | 11 ++++------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index 0803f4dd0cff..e5715827fb63 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -270,7 +270,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co return cmd } -// GetCmdQueryCommunityPool returns the command for fetching community pool info +// GetCmdQueryCommunityPool returns the command for fetching community pool info. func GetCmdQueryCommunityPool() *cobra.Command { cmd := &cobra.Command{ Use: "community-pool", diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 370c9ba69290..0cfd5513bdf5 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -41,12 +41,12 @@ func NewQuerySubspaceParamsCmd() *cobra.Command { params := proposal.NewQueryParametersRequest(args[0], args[1]) - resp, err := queryClient.Parameters(context.Background(), params) + res, err := queryClient.Parameters(context.Background(), params) if err != nil { return err } - return clientCtx.PrintOutput(resp.Params) + return clientCtx.PrintOutput(res.GetParams()) }, } diff --git a/x/upgrade/client/cli/query.go b/x/upgrade/client/cli/query.go index dc65bf61f472..50f65816787d 100644 --- a/x/upgrade/client/cli/query.go +++ b/x/upgrade/client/cli/query.go @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) -// GetQueryCmd returns the parent command for all x/upgrade CLi query commands +// GetQueryCmd returns the parent command for all x/upgrade CLi query commands. func GetQueryCmd() *cobra.Command { cmd := &cobra.Command{ Use: types.ModuleName, @@ -26,7 +26,7 @@ func GetQueryCmd() *cobra.Command { return cmd } -// GetCurrentPlanCmd returns the query upgrade plan command +// GetCurrentPlanCmd returns the query upgrade plan command. func GetCurrentPlanCmd() *cobra.Command { cmd := &cobra.Command{ Use: "plan", @@ -51,10 +51,7 @@ func GetCurrentPlanCmd() *cobra.Command { return fmt.Errorf("no upgrade scheduled") } - if err != nil { - return err - } - return clientCtx.PrintOutput(res.Plan) + return clientCtx.PrintOutput(res.GetPlan()) }, } @@ -64,7 +61,7 @@ func GetCurrentPlanCmd() *cobra.Command { } // GetAppliedPlanCmd returns information about the block at which a completed -// upgrade was applied +// upgrade was applied. func GetAppliedPlanCmd() *cobra.Command { cmd := &cobra.Command{ Use: "applied [upgrade-name]", From 4e53b3671d35bb1558bd3c7f2587329a089436b9 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 15 Jul 2020 11:16:43 +0200 Subject: [PATCH 09/16] Fix x/distrib test --- x/distribution/client/cli/query.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index e5715827fb63..64e6a52fc6b2 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -262,7 +262,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co return err } - return clientCtx.PrintOutput(res.GetRewards()) + return clientCtx.PrintOutput(res) }, } From 94e37d0ff26970c410e207e76fb7c6f9f253ec5a Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 16 Jul 2020 12:16:53 +0200 Subject: [PATCH 10/16] Update x/gov --- x/gov/client/cli/query.go | 275 ++++++++++++++++++-------------------- 1 file changed, 128 insertions(+), 147 deletions(-) diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index 053b95787b7f..a78a4b901c82 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -1,6 +1,7 @@ package cli import ( + "context" "fmt" "strconv" "strings" @@ -10,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/version" gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -64,6 +66,7 @@ $ %s query gov proposal 1 if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -72,14 +75,15 @@ $ %s query gov proposal 1 } // Query the proposal - res, err := gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) + res, err := queryClient.Proposal( + context.Background(), + &types.QueryProposalRequest{ProposalId: proposalID}, + ) if err != nil { return err } - var proposal types.Proposal - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &proposal) - return clientCtx.PrintOutput(proposal) // nolint:errcheck + return clientCtx.PrintOutput(res.GetProposal()) }, } @@ -88,7 +92,8 @@ $ %s query gov proposal 1 return cmd } -// GetCmdQueryProposals implements a query proposals command. +// GetCmdQueryProposals implements a query proposals command. Command to Get a +// Proposal Information. func GetCmdQueryProposals() *cobra.Command { cmd := &cobra.Command{ Use: "proposals", @@ -109,71 +114,50 @@ $ %s query gov proposals --page=2 --limit=100 bechDepositorAddr, _ := cmd.Flags().GetString(flagDepositor) bechVoterAddr, _ := cmd.Flags().GetString(flagVoter) strProposalStatus, _ := cmd.Flags().GetString(flagStatus) - page, _ := cmd.Flags().GetInt(flags.FlagPage) - limit, _ := cmd.Flags().GetInt(flags.FlagLimit) - var depositorAddr sdk.AccAddress - var voterAddr sdk.AccAddress - var proposalStatus types.ProposalStatus - - params := types.NewQueryProposalsParams(page, limit, proposalStatus, voterAddr, depositorAddr) - - if len(bechDepositorAddr) != 0 { - depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr) - if err != nil { - return err - } - params.Depositor = depositorAddr - } - - if len(bechVoterAddr) != 0 { - voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr) - if err != nil { - return err - } - params.Voter = voterAddr - } - - if len(strProposalStatus) != 0 { - proposalStatus, err := types.ProposalStatusFromString(gcutils.NormalizeProposalStatus(strProposalStatus)) - if err != nil { - return err - } - params.ProposalStatus = proposalStatus + depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr) + if err != nil { + return err } - clientCtx := client.GetClientContextFromCmd(cmd) - clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags()) + voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr) if err != nil { return err } - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) + proposalStatus, err := types.ProposalStatusFromString(gcutils.NormalizeProposalStatus(strProposalStatus)) if err != nil { return err } - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", types.QuerierRoute), bz) + clientCtx := client.GetClientContextFromCmd(cmd) + clientCtx, err = client.ReadQueryCommandFlags(clientCtx, cmd.Flags()) if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) - var matchingProposals types.Proposals - err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &matchingProposals) + res, err := queryClient.Proposals( + context.Background(), + &types.QueryProposalsRequest{ + ProposalStatus: proposalStatus, + Voter: voterAddr, + Depositor: depositorAddr, + Req: &query.PageRequest{}, + }, + ) if err != nil { return err } - if len(matchingProposals) == 0 { + if len(res.GetProposals()) == 0 { return fmt.Errorf("no matching proposals found") } - return clientCtx.PrintOutput(matchingProposals) // nolint:errcheck + return clientCtx.PrintOutput(res.GetProposals()) }, } - cmd.Flags().Int(flags.FlagPage, 1, "pagination page of proposals to to query for") - cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of proposals to query for") cmd.Flags().String(flagDepositor, "", "(optional) filter by proposals deposited on by depositor") cmd.Flags().String(flagVoter, "", "(optional) filter by proposals voted on by voted") cmd.Flags().String(flagStatus, "", "(optional) filter proposals by proposal status, status: deposit_period/voting_period/passed/rejected") @@ -182,8 +166,8 @@ $ %s query gov proposals --page=2 --limit=100 return cmd } -// Command to Get a Proposal Information -// GetCmdQueryVote implements the query proposal vote command. +// GetCmdQueryVote implements the query proposal vote command. Command to Get a +// Proposal Information. func GetCmdQueryVote() *cobra.Command { cmd := &cobra.Command{ Use: "vote [proposal-id] [voter-addr]", @@ -204,6 +188,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -212,7 +197,10 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // check to see if the proposal is in the store - _, err = gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) + _, err = queryClient.Proposal( + context.Background(), + &types.QueryProposalRequest{ProposalId: proposalID}, + ) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } @@ -222,36 +210,29 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk return err } - params := types.NewQueryVoteParams(proposalID, voterAddr) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return err - } - - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", types.QuerierRoute), bz) + res, err := queryClient.Vote( + context.Background(), + &types.QueryVoteRequest{ProposalId: proposalID, Voter: voterAddr}, + ) if err != nil { return err } - var vote types.Vote - - // XXX: Allow the decoding to potentially fail as the vote may have been - // pruned from state. If so, decoding will fail and so we need to check the - // Empty() case. Consider updating Vote JSON decoding to not fail when empty. - _ = clientCtx.JSONMarshaler.UnmarshalJSON(res, &vote) - + vote := res.GetVote() if vote.Empty() { - res, err = gcutils.QueryVoteByTxQuery(clientCtx, params) + params := types.NewQueryVoteParams(proposalID, voterAddr) + resByTxQuery, err := gcutils.QueryVoteByTxQuery(clientCtx, params) + if err != nil { return err } - if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &vote); err != nil { + if err := clientCtx.JSONMarshaler.UnmarshalJSON(resByTxQuery, &vote); err != nil { return err } } - return clientCtx.PrintOutput(vote) + return clientCtx.PrintOutput(res.GetVote()) }, } @@ -282,6 +263,7 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -289,50 +271,52 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 return fmt.Errorf("proposal-id %s not a valid int, please input a valid proposal-id", args[0]) } - page, _ := cmd.Flags().GetInt(flags.FlagPage) - limit, _ := cmd.Flags().GetInt(flags.FlagLimit) - - params := types.NewQueryProposalVotesParams(proposalID, page, limit) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return err - } - // check to see if the proposal is in the store - res, err := gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) + proposalRes, err := queryClient.Proposal( + context.Background(), + &types.QueryProposalRequest{ProposalId: proposalID}, + ) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } - var proposal types.Proposal - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &proposal) - - propStatus := proposal.Status + propStatus := proposalRes.GetProposal().Status if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) { - res, err = gcutils.QueryVotesByTxQuery(clientCtx, params) - } else { - res, _, err = clientCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", types.QuerierRoute), bz) + page, _ := cmd.Flags().GetInt(flags.FlagPage) + limit, _ := cmd.Flags().GetInt(flags.FlagLimit) + + params := types.NewQueryProposalVotesParams(proposalID, page, limit) + resByTxQuery, err := gcutils.QueryVotesByTxQuery(clientCtx, params) + if err != nil { + return err + } + + var votes types.Votes + clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &votes) + return clientCtx.PrintOutput(votes) + } + res, err := queryClient.Votes( + context.Background(), + &types.QueryVotesRequest{ProposalId: proposalID, Req: &query.PageRequest{}}, + ) if err != nil { return err } - var votes types.Votes - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &votes) - return clientCtx.PrintOutput(votes) + return clientCtx.PrintOutput(res.GetVotes()) + }, } - cmd.Flags().Int(flags.FlagPage, 1, "pagination page of votes to to query for") - cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of votes to query for") flags.AddQueryFlagsToCmd(cmd) return cmd } -// Command to Get a specific Deposit Information -// GetCmdQueryDeposit implements the query proposal deposit command. +// GetCmdQueryDeposit implements the query proposal deposit command. Command to +// get a specific Deposit Information func GetCmdQueryDeposit() *cobra.Command { cmd := &cobra.Command{ Use: "deposit [proposal-id] [depositer-addr]", @@ -353,6 +337,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -361,7 +346,10 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // check to see if the proposal is in the store - _, err = gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) + _, err = queryClient.Proposal( + context.Background(), + &types.QueryProposalRequest{ProposalId: proposalID}, + ) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } @@ -371,26 +359,22 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk return err } - params := types.NewQueryDepositParams(proposalID, depositorAddr) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return err - } - - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", types.QuerierRoute), bz) + res, err := queryClient.Deposit( + context.Background(), + &types.QueryDepositRequest{ProposalId: proposalID, Depositor: depositorAddr}, + ) if err != nil { return err } - var deposit types.Deposit - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &deposit) - + deposit := res.GetDeposit() if deposit.Empty() { - res, err = gcutils.QueryDepositByTxQuery(clientCtx, params) + params := types.NewQueryDepositParams(proposalID, depositorAddr) + resByTxQuery, err := gcutils.QueryDepositByTxQuery(clientCtx, params) if err != nil { return err } - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &deposit) + clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &deposit) } return clientCtx.PrintOutput(deposit) @@ -424,6 +408,7 @@ $ %s query gov deposits 1 if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -431,35 +416,37 @@ $ %s query gov deposits 1 return fmt.Errorf("proposal-id %s not a valid uint, please input a valid proposal-id", args[0]) } - params := types.NewQueryProposalParams(proposalID) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return err - } - // check to see if the proposal is in the store - res, err := gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) + proposalRes, err := queryClient.Proposal( + context.Background(), + &types.QueryProposalRequest{ProposalId: proposalID}, + ) if err != nil { - return fmt.Errorf("failed to fetch proposal with id %d: %s", proposalID, err) + return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } - var proposal types.Proposal - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &proposal) - - propStatus := proposal.Status + propStatus := proposalRes.GetProposal().Status if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) { - res, err = gcutils.QueryDepositsByTxQuery(clientCtx, params) - } else { - res, _, err = clientCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", types.QuerierRoute), bz) + params := types.NewQueryProposalParams(proposalID) + resByTxQuery, err := gcutils.QueryDepositsByTxQuery(clientCtx, params) + if err != nil { + return err + } + + var dep types.Deposits + clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &dep) + return clientCtx.PrintOutput(dep) } + res, err := queryClient.Deposits( + context.Background(), + &types.QueryDepositsRequest{ProposalId: proposalID, Req: &query.PageRequest{}}, + ) if err != nil { return err } - var dep types.Deposits - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &dep) - return clientCtx.PrintOutput(dep) + return clientCtx.PrintOutput(res.GetDeposits()) }, } @@ -490,6 +477,7 @@ $ %s query gov tally 1 if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // validate that the proposal id is a uint proposalID, err := strconv.ParseUint(args[0], 10, 64) @@ -498,27 +486,21 @@ $ %s query gov tally 1 } // check to see if the proposal is in the store - _, err = gcutils.QueryProposalByID(proposalID, clientCtx, types.QuerierRoute) + _, err = queryClient.Proposal( + context.Background(), + &types.QueryProposalRequest{ProposalId: proposalID}, + ) if err != nil { return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err) } - // Construct query - params := types.NewQueryProposalParams(proposalID) - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return err - } - // Query store - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", types.QuerierRoute), bz) - if err != nil { - return err - } + res, err := queryClient.TallyResult( + context.Background(), + &types.QueryTallyResultRequest{ProposalId: proposalID}, + ) - var tally types.TallyResult - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, &tally) - return clientCtx.PrintOutput(tally) + return clientCtx.PrintOutput(res.GetTally()) }, } @@ -527,7 +509,7 @@ $ %s query gov tally 1 return cmd } -// GetCmdQueryProposal implements the query proposal command. +// GetCmdQueryParams implements the query params command. func GetCmdQueryParams() *cobra.Command { cmd := &cobra.Command{ Use: "params", @@ -578,7 +560,7 @@ $ %s query gov params return cmd } -// GetCmdQueryProposal implements the query proposal command. +// GetCmdQueryParam implements the query param command. func GetCmdQueryParam() *cobra.Command { cmd := &cobra.Command{ Use: "param [param-type]", @@ -601,26 +583,25 @@ $ %s query gov param deposit if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) // Query store - res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/%s", types.QuerierRoute, args[0]), nil) + res, err := queryClient.Params( + context.Background(), + &types.QueryParamsRequest{ParamsType: args[0]}, + ) if err != nil { return err } + var out fmt.Stringer switch args[0] { case "voting": - var param types.VotingParams - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, ¶m) - out = param + out = res.GetVotingParams() case "tallying": - var param types.TallyParams - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, ¶m) - out = param + out = res.GetTallyParams() case "deposit": - var param types.DepositParams - clientCtx.JSONMarshaler.MustUnmarshalJSON(res, ¶m) - out = param + out = res.GetDepositParams() default: return fmt.Errorf("argument must be one of (voting|tallying|deposit), was %s", args[0]) } From 439990e4eb03660bbfda471f73195be40c6eb6b7 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 16 Jul 2020 12:41:56 +0200 Subject: [PATCH 11/16] Add ReadQueryCommandFlags --- x/params/client/cli/query.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 0cfd5513bdf5..a0750f57cd4a 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -37,6 +37,10 @@ func NewQuerySubspaceParamsCmd() *cobra.Command { Args: cobra.ExactArgs(2), 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 := proposal.NewQueryClient(clientCtx) params := proposal.NewQueryParametersRequest(args[0], args[1]) From d078b578a2af3973aed6ebd38a1982d52ffc8f16 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 16 Jul 2020 12:51:10 +0200 Subject: [PATCH 12/16] Fix lint --- x/gov/client/cli/query.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index a78a4b901c82..b27514fbd4a5 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -499,6 +499,9 @@ $ %s query gov tally 1 context.Background(), &types.QueryTallyResultRequest{ProposalId: proposalID}, ) + if err != nil { + return err + } return clientCtx.PrintOutput(res.GetTally()) }, From ac7790c45320d88bc083d4af5571d18f46955d65 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 16 Jul 2020 13:05:29 +0200 Subject: [PATCH 13/16] Revert x/params --- x/params/client/cli/query.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index a0750f57cd4a..687ff74bf4c8 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -1,14 +1,13 @@ package cli import ( - "context" + "fmt" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/x/params/types" - "github.com/cosmos/cosmos-sdk/x/params/types/proposal" ) // NewQueryCmd returns a root CLI command handler for all x/params query commands. @@ -21,9 +20,7 @@ func NewQueryCmd() *cobra.Command { RunE: client.ValidateCmd, } - cmd.AddCommand( - NewQuerySubspaceParamsCmd(), - ) + cmd.AddCommand(NewQuerySubspaceParamsCmd()) return cmd } @@ -41,16 +38,26 @@ func NewQuerySubspaceParamsCmd() *cobra.Command { if err != nil { return err } - queryClient := proposal.NewQueryClient(clientCtx) - params := proposal.NewQueryParametersRequest(args[0], args[1]) + params := types.NewQuerySubspaceParams(args[0], args[1]) + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams) - res, err := queryClient.Parameters(context.Background(), params) + bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if err != nil { + return fmt.Errorf("failed to marshal params: %w", err) + } + + bz, _, err = clientCtx.QueryWithData(route, bz) + if err != nil { + return err + } + + var resp types.SubspaceParamsResponse + if err := clientCtx.JSONMarshaler.UnmarshalJSON(bz, &resp); err != nil { return err } - return clientCtx.PrintOutput(res.GetParams()) + return clientCtx.PrintOutput(resp) }, } From 5296f5850741619cd9ffb47e5a44be8924f71668 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 16 Jul 2020 13:08:44 +0200 Subject: [PATCH 14/16] x/params use grpc query --- x/params/client/cli/query.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 687ff74bf4c8..b9c4b24d1778 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -1,13 +1,14 @@ 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/x/params/types" + "github.com/cosmos/cosmos-sdk/x/params/types/proposal" ) // NewQueryCmd returns a root CLI command handler for all x/params query commands. @@ -38,26 +39,15 @@ func NewQuerySubspaceParamsCmd() *cobra.Command { if err != nil { return err } + queryClient := proposal.NewQueryClient(clientCtx) - params := types.NewQuerySubspaceParams(args[0], args[1]) - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams) - - bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) - if err != nil { - return fmt.Errorf("failed to marshal params: %w", err) - } - - bz, _, err = clientCtx.QueryWithData(route, bz) + params := proposal.NewQueryParametersRequest(args[0], args[1]) + res, err := queryClient.Parameters(context.Background(), params) if err != nil { return err } - var resp types.SubspaceParamsResponse - if err := clientCtx.JSONMarshaler.UnmarshalJSON(bz, &resp); err != nil { - return err - } - - return clientCtx.PrintOutput(resp) + return clientCtx.PrintOutput(res.GetParams()) }, } From 0c3f42acbc3bbcb031414a299b81cb4c072b1029 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 16 Jul 2020 18:35:46 +0200 Subject: [PATCH 15/16] Fix tests --- x/auth/module.go | 2 ++ x/bank/module.go | 2 ++ x/capability/module.go | 2 ++ x/crisis/module.go | 2 ++ x/distribution/module.go | 2 ++ x/evidence/module.go | 2 ++ x/gov/module.go | 2 ++ x/ibc-transfer/module.go | 2 ++ x/mint/module.go | 2 ++ x/params/client/cli/cli_test.go | 8 ++++---- x/params/module.go | 6 +++++- x/slashing/module.go | 2 ++ x/staking/module.go | 2 ++ x/upgrade/module.go | 6 +++++- 14 files changed, 36 insertions(+), 6 deletions(-) diff --git a/x/auth/module.go b/x/auth/module.go index 3bf8136a45ba..00e66491fd11 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -118,6 +118,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.accountKeeper) } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // InitGenesis performs genesis initialization for the auth module. It returns diff --git a/x/bank/module.go b/x/bank/module.go index 6719d364ce62..ee6659e3411b 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -87,6 +87,8 @@ type AppModule struct { accountKeeper types.AccountKeeper } +// 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) } diff --git a/x/capability/module.go b/x/capability/module.go index 841265336fa2..f2a36ac5476b 100644 --- a/x/capability/module.go +++ b/x/capability/module.go @@ -104,6 +104,8 @@ func (AppModule) QuerierRoute() string { return "" } // NewQuerierHandler returns the capability module's Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return nil } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // RegisterInvariants registers the capability module's invariants. diff --git a/x/crisis/module.go b/x/crisis/module.go index 54b6d40eadc6..b01bcfc94b01 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -109,6 +109,8 @@ func (AppModule) QuerierRoute() string { return "" } // NewQuerierHandler returns no sdk.Querier. func (AppModule) NewQuerierHandler() sdk.Querier { return nil } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // InitGenesis performs genesis initialization for the crisis module. It returns diff --git a/x/distribution/module.go b/x/distribution/module.go index 6ed56e413d04..738bcfbf3861 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -134,6 +134,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } +// 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) } diff --git a/x/evidence/module.go b/x/evidence/module.go index 342991b02609..b5df41d34aad 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -143,6 +143,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // RegisterInvariants registers the evidence module's invariants. diff --git a/x/gov/module.go b/x/gov/module.go index b5d21b4cbae4..40e8ebf45cdc 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -150,6 +150,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } +// 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) } diff --git a/x/ibc-transfer/module.go b/x/ibc-transfer/module.go index 8542c4b9e994..6f53de7bec56 100644 --- a/x/ibc-transfer/module.go +++ b/x/ibc-transfer/module.go @@ -118,6 +118,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return nil } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // InitGenesis performs genesis initialization for the ibc transfer module. It returns diff --git a/x/mint/module.go b/x/mint/module.go index 22cecf1e1672..7ae12b09dd08 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -113,6 +113,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // InitGenesis performs genesis initialization for the mint module. It returns diff --git a/x/params/client/cli/cli_test.go b/x/params/client/cli/cli_test.go index ff487548c599..8f99689337e9 100644 --- a/x/params/client/cli/cli_test.go +++ b/x/params/client/cli/cli_test.go @@ -53,7 +53,7 @@ func (s *IntegrationTestSuite) TestNewQuerySubspaceParamsCmd() { []string{ "staking", "MaxValidators", }, - `{"Subspace":"staking","Key":"MaxValidators","Value":"100"}`, + `{"subspace":"staking","key":"MaxValidators","value":"100"}`, }, { "text output", @@ -61,9 +61,9 @@ func (s *IntegrationTestSuite) TestNewQuerySubspaceParamsCmd() { "staking", "MaxValidators", fmt.Sprintf("--%s=text", tmcli.OutputFlag), }, - `Key: MaxValidators -Subspace: staking -Value: "100"`, + `key: MaxValidators +subspace: staking +value: "100"`, }, } diff --git a/x/params/module.go b/x/params/module.go index 9e4256a2b7aa..148113e60ecd 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -102,7 +102,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) { + proposal.RegisterQueryServer(server, am.keeper) +} // ProposalContents returns all the params content functions used to // simulate governance proposals. diff --git a/x/slashing/module.go b/x/slashing/module.go index c9a86cf33b86..81f17d1e6d67 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -125,6 +125,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // InitGenesis performs genesis initialization for the slashing module. It returns diff --git a/x/staking/module.go b/x/staking/module.go index bd2538ee6263..e37325531f80 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -121,6 +121,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } +// RegisterQueryService registers a GRPC query service to respond to the +// module-specific GRPC queries. func (am AppModule) RegisterQueryService(grpc.Server) {} // InitGenesis performs genesis initialization for the staking module. It returns diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 472300445b99..7caa7b55b658 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -94,7 +94,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 is ignored, no sense in serializing future upgrades func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate { From eaffb1af84d342a5efb04ddb953c0124a54a329f Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 17 Jul 2020 18:10:43 +0200 Subject: [PATCH 16/16] Use page request --- x/distribution/client/cli/query.go | 10 +++++- x/gov/client/cli/query.go | 51 ++++++++++++++++++++--------- x/upgrade/client/cli/query.go | 9 +++-- x/upgrade/client/rest/query.go | 2 +- x/upgrade/keeper/grpc_query_test.go | 8 ++--- x/upgrade/types/querier.go | 10 ------ 6 files changed, 54 insertions(+), 36 deletions(-) diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index 64e6a52fc6b2..5733bd5abfe1 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -191,9 +191,16 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq return fmt.Errorf("end-height %s not a valid uint, please input a valid end-height", args[2]) } + pageReq := client.ReadPageRequest(cmd.Flags()) + res, err := queryClient.ValidatorSlashes( context.Background(), - &types.QueryValidatorSlashesRequest{ValidatorAddress: validatorAddr, StartingHeight: startHeight, EndingHeight: endHeight}, + &types.QueryValidatorSlashesRequest{ + ValidatorAddress: validatorAddr, + StartingHeight: startHeight, + EndingHeight: endHeight, + Req: pageReq, + }, ) if err != nil { return err @@ -204,6 +211,7 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq } flags.AddQueryFlagsToCmd(cmd) + flags.AddPaginationFlagsToCmd(cmd, "validator slashes") return cmd } diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index b27514fbd4a5..47b7b91710d0 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/version" gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -137,13 +136,15 @@ $ %s query gov proposals --page=2 --limit=100 } queryClient := types.NewQueryClient(clientCtx) + pageReq := client.ReadPageRequest(cmd.Flags()) + res, err := queryClient.Proposals( context.Background(), &types.QueryProposalsRequest{ ProposalStatus: proposalStatus, Voter: voterAddr, Depositor: depositorAddr, - Req: &query.PageRequest{}, + Req: pageReq, }, ) if err != nil { @@ -161,6 +162,7 @@ $ %s query gov proposals --page=2 --limit=100 cmd.Flags().String(flagDepositor, "", "(optional) filter by proposals deposited on by depositor") cmd.Flags().String(flagVoter, "", "(optional) filter by proposals voted on by voted") cmd.Flags().String(flagStatus, "", "(optional) filter proposals by proposal status, status: deposit_period/voting_period/passed/rejected") + flags.AddPaginationFlagsToCmd(cmd, "proposals") flags.AddQueryFlagsToCmd(cmd) return cmd @@ -297,9 +299,11 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 } + pageReq := client.ReadPageRequest(cmd.Flags()) + res, err := queryClient.Votes( context.Background(), - &types.QueryVotesRequest{ProposalId: proposalID, Req: &query.PageRequest{}}, + &types.QueryVotesRequest{ProposalId: proposalID, Req: pageReq}, ) if err != nil { return err @@ -310,6 +314,10 @@ $ %[1]s query gov votes 1 --page=2 --limit=100 }, } + // Deprecated, remove line when removing FlagPage altogether. + cmd.Flags().Int(flags.FlagPage, 1, "pagination page of proposals to to query for") + + flags.AddPaginationFlagsToCmd(cmd, "votes") flags.AddQueryFlagsToCmd(cmd) return cmd @@ -438,9 +446,11 @@ $ %s query gov deposits 1 return clientCtx.PrintOutput(dep) } + pageReq := client.ReadPageRequest(cmd.Flags()) + res, err := queryClient.Deposits( context.Background(), - &types.QueryDepositsRequest{ProposalId: proposalID, Req: &query.PageRequest{}}, + &types.QueryDepositsRequest{ProposalId: proposalID, Req: pageReq}, ) if err != nil { return err @@ -450,6 +460,7 @@ $ %s query gov deposits 1 }, } + flags.AddPaginationFlagsToCmd(cmd, "deposits") flags.AddQueryFlagsToCmd(cmd) return cmd @@ -533,28 +544,38 @@ $ %s query gov params if err != nil { return err } + queryClient := types.NewQueryClient(clientCtx) - tp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/tallying", types.QuerierRoute), nil) + // Query store for all 3 params + votingRes, err := queryClient.Params( + context.Background(), + &types.QueryParamsRequest{ParamsType: "voting"}, + ) if err != nil { return err } - dp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/deposit", types.QuerierRoute), nil) + + tallyRes, err := queryClient.Params( + context.Background(), + &types.QueryParamsRequest{ParamsType: "tallying"}, + ) if err != nil { return err } - vp, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/params/voting", types.QuerierRoute), nil) + + depositRes, err := queryClient.Params( + context.Background(), + &types.QueryParamsRequest{ParamsType: "deposit"}, + ) if err != nil { return err } - var tallyParams types.TallyParams - clientCtx.JSONMarshaler.MustUnmarshalJSON(tp, &tallyParams) - var depositParams types.DepositParams - clientCtx.JSONMarshaler.MustUnmarshalJSON(dp, &depositParams) - var votingParams types.VotingParams - clientCtx.JSONMarshaler.MustUnmarshalJSON(vp, &votingParams) - - return clientCtx.PrintOutput(types.NewParams(votingParams, tallyParams, depositParams)) + return clientCtx.PrintOutput(types.NewParams( + votingRes.GetVotingParams(), + tallyRes.GetTallyParams(), + depositRes.GetDepositParams(), + )) }, } diff --git a/x/upgrade/client/cli/query.go b/x/upgrade/client/cli/query.go index 50f65816787d..a555cfc90089 100644 --- a/x/upgrade/client/cli/query.go +++ b/x/upgrade/client/cli/query.go @@ -41,8 +41,8 @@ func GetCurrentPlanCmd() *cobra.Command { } queryClient := types.NewQueryClient(clientCtx) - params := types.NewQueryCurrentPlanRequest() - res, err := queryClient.CurrentPlan(context.Background(), params) + params := types.QueryCurrentPlanRequest{} + res, err := queryClient.CurrentPlan(context.Background(), ¶ms) if err != nil { return err } @@ -77,9 +77,8 @@ func GetAppliedPlanCmd() *cobra.Command { } queryClient := types.NewQueryClient(clientCtx) - name := args[0] - params := types.NewQueryAppliedPlanRequest(name) - res, err := queryClient.AppliedPlan(context.Background(), params) + params := types.QueryAppliedPlanRequest{Name: args[0]} + res, err := queryClient.AppliedPlan(context.Background(), ¶ms) if err != nil { return err } diff --git a/x/upgrade/client/rest/query.go b/x/upgrade/client/rest/query.go index 02f0303a4c50..709b78687c68 100644 --- a/x/upgrade/client/rest/query.go +++ b/x/upgrade/client/rest/query.go @@ -45,7 +45,7 @@ func getDonePlanHandler(clientCtx client.Context) func(http.ResponseWriter, *htt return func(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] - params := types.NewQueryAppliedPlanRequest(name) + params := types.QueryAppliedPlanRequest{Name: name} bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) if rest.CheckBadRequestError(w, err) { return diff --git a/x/upgrade/keeper/grpc_query_test.go b/x/upgrade/keeper/grpc_query_test.go index c0f0601b4758..1ffb0038f6cd 100644 --- a/x/upgrade/keeper/grpc_query_test.go +++ b/x/upgrade/keeper/grpc_query_test.go @@ -46,7 +46,7 @@ func (suite *UpgradeTestSuite) TestQueryCurrentPlan() { { "without current upgrade plan", func() { - req = types.NewQueryCurrentPlanRequest() + req = &types.QueryCurrentPlanRequest{} expResponse = types.QueryCurrentPlanResponse{} }, true, @@ -57,7 +57,7 @@ func (suite *UpgradeTestSuite) TestQueryCurrentPlan() { plan := types.Plan{Name: "test-plan", Height: 5} suite.app.UpgradeKeeper.ScheduleUpgrade(suite.ctx, plan) - req = types.NewQueryCurrentPlanRequest() + req = &types.QueryCurrentPlanRequest{} expResponse = types.QueryCurrentPlanResponse{Plan: &plan} }, true, @@ -97,7 +97,7 @@ func (suite *UpgradeTestSuite) TestAppliedCurrentPlan() { { "with non-existent upgrade plan", func() { - req = types.NewQueryAppliedPlanRequest("foo") + req = &types.QueryAppliedPlanRequest{Name: "foo"} }, true, }, @@ -114,7 +114,7 @@ func (suite *UpgradeTestSuite) TestAppliedCurrentPlan() { suite.app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx sdk.Context, plan types.Plan) {}) suite.app.UpgradeKeeper.ApplyUpgrade(suite.ctx, plan) - req = types.NewQueryAppliedPlanRequest(planName) + req = &types.QueryAppliedPlanRequest{Name: planName} }, true, }, diff --git a/x/upgrade/types/querier.go b/x/upgrade/types/querier.go index b6b6457ca833..d636c6aa9676 100644 --- a/x/upgrade/types/querier.go +++ b/x/upgrade/types/querier.go @@ -5,13 +5,3 @@ const ( QueryCurrent = "current" QueryApplied = "applied" ) - -// NewQueryCurrentPlanRequest creates a new instance of QueryCurrentPlanRequest. -func NewQueryCurrentPlanRequest() *QueryCurrentPlanRequest { - return &QueryCurrentPlanRequest{} -} - -// NewQueryAppliedPlanRequest creates a new instance of QueryAppliedPlanRequest. -func NewQueryAppliedPlanRequest(name string) *QueryAppliedPlanRequest { - return &QueryAppliedPlanRequest{Name: name} -}