From 33bc7faa1432521677a8be4db3d52442e10be2fa Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 17 Oct 2023 11:45:29 +0200 Subject: [PATCH 1/2] refactor(x/gov): refactor `q gov proposer` --- CHANGELOG.md | 3 +- x/gov/autocli.go | 1 + x/gov/client/cli/query.go | 74 ------------------------------------- x/gov/client/utils/query.go | 23 ------------ x/gov/module.go | 6 --- 5 files changed, 3 insertions(+), 104 deletions(-) delete mode 100644 x/gov/client/cli/query.go diff --git a/CHANGELOG.md b/CHANGELOG.md index ca90e7f67f7..02ef23feef1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (x/staking/keeper) [#]18049(https://github.com/cosmos/cosmos-sdk/pull/18049) return early if Slash encounters zero tokens to burn. +* (x/gov) [#18025](https://github.com/cosmos/cosmos-sdk/pull/18025) Improve ` q gov proposer` by querying directly a proposal instead of tx events. It is an alias of `q gov proposal` as the proposer is a field of the proposal. +* (x/staking/keeper) [#18049](https://github.com/cosmos/cosmos-sdk/pull/18049) return early if Slash encounters zero tokens to burn. * (x/staking/keeper) [#18035](https://github.com/cosmos/cosmos-sdk/pull/18035) Hoisted out of the redelegation loop, the non-changing validator and delegator addresses parsing. * (keyring) [#17913](https://github.com/cosmos/cosmos-sdk/pull/17913) Add `NewAutoCLIKeyring` for creating an AutoCLI keyring from a SDK keyring. * (codec) [#17913](https://github.com/cosmos/cosmos-sdk/pull/17913) `codectypes.NewAnyWithValue` supports proto v2 messages. diff --git a/x/gov/autocli.go b/x/gov/autocli.go index de030f2b26d..8bd6aa49a33 100644 --- a/x/gov/autocli.go +++ b/x/gov/autocli.go @@ -33,6 +33,7 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { { RpcMethod: "Proposal", Use: "proposal [proposal-id]", + Alias: []string{"proposer"}, Short: "Query details of a single proposal", Example: fmt.Sprintf("%s query gov proposal 1", version.AppName), PositionalArgs: []*autocliv1.PositionalArgDescriptor{ diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go deleted file mode 100644 index a6c1538c0ac..00000000000 --- a/x/gov/client/cli/query.go +++ /dev/null @@ -1,74 +0,0 @@ -package cli - -import ( - "encoding/json" - "fmt" - "strconv" - - "github.com/spf13/cobra" - - "cosmossdk.io/core/address" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/version" - gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" - "github.com/cosmos/cosmos-sdk/x/gov/types" -) - -// GetCustomQueryCmd returns the cli query commands for this module -// These commands do not rely on gRPC and cannot be autogenerated -func GetCustomQueryCmd(ac address.Codec) *cobra.Command { - // Group gov queries under a subcommand - govQueryCmd := &cobra.Command{ - Use: types.ModuleName, - Short: "Querying commands for the gov module", - DisableFlagParsing: true, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - - govQueryCmd.AddCommand( - GetCmdQueryProposer(), - ) - - return govQueryCmd -} - -// GetCmdQueryProposer implements the query proposer command. -func GetCmdQueryProposer() *cobra.Command { - cmd := &cobra.Command{ - Use: "proposer [proposal-id]", - Args: cobra.ExactArgs(1), - Short: "Query the proposer of a governance proposal", - Long: "Query which address proposed a proposal with a given ID", - Example: fmt.Sprintf("%s query gov proposer 1", version.AppName), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - // validate that the proposalID is a uint - proposalID, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return fmt.Errorf("proposal-id %s is not a valid uint", args[0]) - } - - prop, err := gcutils.QueryProposerByTxQuery(clientCtx, proposalID) - if err != nil { - return err - } - - output, err := json.Marshal(prop) - if err != nil { - return err - } - return clientCtx.PrintRaw(output) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index 6ee7ca4a654..5b08729493e 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -160,29 +160,6 @@ func QueryVoteByTxQuery(clientCtx client.Context, params v1.QueryVoteParams) ([] return nil, fmt.Errorf("address '%s' did not vote on proposalID %d", params.Voter, params.ProposalID) } -// QueryProposerByTxQuery will query for a proposer of a governance proposal by ID. -func QueryProposerByTxQuery(clientCtx client.Context, proposalID uint64) (Proposer, error) { - q := fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID) - searchResult, err := authtx.QueryTxsByEvents(clientCtx, defaultPage, defaultLimit, q, "") - if err != nil { - return Proposer{}, err - } - - for _, info := range searchResult.Txs { - for _, msg := range info.GetTx().GetMsgs() { - // there should only be a single proposal under the given conditions - if subMsg, ok := msg.(*v1beta1.MsgSubmitProposal); ok { - return NewProposer(proposalID, subMsg.Proposer), nil - } - if subMsg, ok := msg.(*v1.MsgSubmitProposal); ok { - return NewProposer(proposalID, subMsg.Proposer), nil - } - } - } - - return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID) -} - // convertVote converts a MsgVoteWeighted into a *v1.Vote. func convertVote(v *v1beta1.MsgVoteWeighted) *v1.Vote { opts := make([]*v1.WeightedVoteOption, len(v.Options)) diff --git a/x/gov/module.go b/x/gov/module.go index 4f39e462600..e4d9c863100 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -106,12 +106,6 @@ func (ab AppModuleBasic) GetTxCmd() *cobra.Command { return cli.NewTxCmd(legacyProposalCLIHandlers) } -// GetQueryCmd returns the custom query commands for the gov modules. -// This command will be enhanced by AutoCLI as defined in the AutoCLIOptions() method. -func (ab AppModuleBasic) GetQueryCmd() *cobra.Command { - return cli.GetCustomQueryCmd(ab.ac) -} - func getProposalCLIHandlers(handlers []govclient.ProposalHandler) []*cobra.Command { proposalCLIHandlers := make([]*cobra.Command, 0, len(handlers)) for _, proposalHandler := range handlers { From f8814e3fed48b839aec2d7c3014d0da2011d66d8 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 17 Oct 2023 13:26:37 +0200 Subject: [PATCH 2/2] remove test --- tests/e2e/gov/query.go | 56 ------------------------------------------ 1 file changed, 56 deletions(-) delete mode 100644 tests/e2e/gov/query.go diff --git a/tests/e2e/gov/query.go b/tests/e2e/gov/query.go deleted file mode 100644 index a600649d67b..00000000000 --- a/tests/e2e/gov/query.go +++ /dev/null @@ -1,56 +0,0 @@ -package gov - -import ( - "fmt" - "strings" - - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - "github.com/cosmos/cosmos-sdk/x/gov/client/cli" -) - -func (s *E2ETestSuite) TestCmdProposer() { - val := s.network.Validators[0] - - testCases := []struct { - name string - args []string - expectErr bool - expectedOutput string - }{ - { - "without proposal id", - []string{ - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - ``, - }, - { - "json output", - []string{ - "1", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - fmt.Sprintf("{\"proposal_id\":%d,\"proposer\":\"%s\"}", 1, val.Address.String()), - }, - } - - for _, tc := range testCases { - tc := tc - - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryProposer() - clientCtx := val.ClientCtx - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expectErr { - s.Require().Error(err) - } else { - s.Require().NoError(err) - s.Require().Equal(strings.TrimSpace(tc.expectedOutput), strings.TrimSpace(out.String())) - } - }) - } -}