refactor(x/gov): refactor q gov proposer (#18025)

This commit is contained in:
Julien Robert 2023-10-18 13:55:14 +02:00 committed by GitHub
parent 31f5bc07e9
commit 033b840a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 3 additions and 160 deletions

View File

@ -53,7 +53,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 `<appd> 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.

View File

@ -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()))
}
})
}
}

View File

@ -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{

View File

@ -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
}

View File

@ -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))

View File

@ -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 {