From 2e15f67370a6c8c8cd233fbb34c2c4f3d18abd36 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 13 Dec 2024 21:01:29 +0100 Subject: [PATCH] refactor(client/v2): remove gov dependency (backport #22867) (#22869) --- client/v2/autocli/msg.go | 14 ++-- client/v2/go.mod | 4 +- client/v2/go.sum | 2 - client/v2/internal/governance/gov.go | 99 ++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 client/v2/internal/governance/gov.go diff --git a/client/v2/autocli/msg.go b/client/v2/autocli/msg.go index 9b30a56fe375..795b965adacb 100644 --- a/client/v2/autocli/msg.go +++ b/client/v2/autocli/msg.go @@ -5,7 +5,6 @@ import ( "context" "fmt" - gogoproto "github.com/cosmos/gogoproto/proto" "github.com/spf13/cobra" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" @@ -14,16 +13,13 @@ import ( autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" "cosmossdk.io/client/v2/autocli/flag" "cosmossdk.io/client/v2/internal/flags" + "cosmossdk.io/client/v2/internal/governance" "cosmossdk.io/client/v2/internal/print" "cosmossdk.io/client/v2/internal/util" v2tx "cosmossdk.io/client/v2/tx" addresscodec "cosmossdk.io/core/address" "cosmossdk.io/core/transaction" - // the following will be extracted to a separate module - // https://github.com/cosmos/cosmos-sdk/issues/14403 - govcli "cosmossdk.io/x/gov/client/cli" - govtypes "cosmossdk.io/x/gov/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/client" @@ -188,7 +184,7 @@ func (b *Builder) BuildMsgMethodCommand(descriptor protoreflect.MethodDescriptor // set gov proposal flags if command is a gov proposal if options.GovProposal { - govcli.AddGovPropFlagsToCmd(cmd) + governance.AddGovPropFlagsToCmd(cmd) cmd.Flags().Bool(flags.FlagNoProposal, false, "Skip gov proposal and submit a normal transaction") } @@ -203,7 +199,7 @@ func (b *Builder) handleGovProposal( addressCodec addresscodec.Codec, fd protoreflect.FieldDescriptor, ) error { - govAuthority := authtypes.NewModuleAddress(govtypes.ModuleName) + govAuthority := authtypes.NewModuleAddress(governance.ModuleName) authority, err := addressCodec.BytesToString(govAuthority.Bytes()) if err != nil { return fmt.Errorf("failed to convert gov authority: %w", err) @@ -216,7 +212,7 @@ func (b *Builder) handleGovProposal( return fmt.Errorf("failed to set signer on message, got %q: %w", signerFromFlag, err) } - proposal, err := govcli.ReadGovPropCmdFlags(signer, cmd.Flags()) + proposal, err := governance.ReadGovPropCmdFlags(signer, cmd.Flags()) if err != nil { return err } @@ -227,7 +223,7 @@ func (b *Builder) handleGovProposal( msg := dynamicpb.NewMessage(input.Descriptor()) proto.Merge(msg, input.Interface()) - if err := proposal.SetMsgs([]gogoproto.Message{msg}); err != nil { + if err := governance.SetGovMsgs(proposal, msg); err != nil { return fmt.Errorf("failed to set msg in proposal %w", err) } diff --git a/client/v2/go.mod b/client/v2/go.mod index 69501223cf60..d3bd2d376cd2 100644 --- a/client/v2/go.mod +++ b/client/v2/go.mod @@ -7,8 +7,7 @@ require ( cosmossdk.io/core v1.0.0-alpha.6 // main cosmossdk.io/depinject v1.1.0 cosmossdk.io/x/bank v0.0.0-20240226161501-23359a0b6d91 - cosmossdk.io/x/gov v0.0.0-20231113122742-912390d5fc4a - cosmossdk.io/x/tx v1.0.0-alpha.3 // main + cosmossdk.io/x/tx v1.0.0-alpha.3 github.com/cosmos/cosmos-proto v1.0.0-beta.5 github.com/cosmos/cosmos-sdk v0.52.0 github.com/spf13/cobra v1.8.1 @@ -184,6 +183,5 @@ replace ( // pseudo version lower than the latest tag cosmossdk.io/store => cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d // main cosmossdk.io/x/bank => ./../../x/bank - cosmossdk.io/x/gov => ./../../x/gov cosmossdk.io/x/staking => ./../../x/staking ) diff --git a/client/v2/go.sum b/client/v2/go.sum index 3cab2caea643..2563ecc59f45 100644 --- a/client/v2/go.sum +++ b/client/v2/go.sum @@ -24,8 +24,6 @@ cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0= cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ= cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d h1:KQM4Q6kjwlM4HuDZRV8/ZDXX3whjfStndYNTsRrbboQ= cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d/go.mod h1:oZBBY4BrkYnghr6MFL0MP5mGqpkPedHcWkXwXddd6tU= -cosmossdk.io/x/protocolpool v0.0.0-20230925135524-a1bc045b3190 h1:XQJj9Dv9Gtze0l2TF79BU5lkP6MkUveTUuKICmxoz+o= -cosmossdk.io/x/protocolpool v0.0.0-20230925135524-a1bc045b3190/go.mod h1:7WUGupOvmlHJoIMBz1JbObQxeo6/TDiuDBxmtod8HRg= cosmossdk.io/x/tx v1.0.0-alpha.3 h1:+55/JFH5QRqnFhOI2heH3DKsaNL0RpXcJOQNzUvHiaQ= cosmossdk.io/x/tx v1.0.0-alpha.3/go.mod h1:h4pQ/j6Gfu8goB1R3Jbl4qY4RjYVNAsoylcleTXdSRg= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= diff --git a/client/v2/internal/governance/gov.go b/client/v2/internal/governance/gov.go new file mode 100644 index 000000000000..11db05fcea8d --- /dev/null +++ b/client/v2/internal/governance/gov.go @@ -0,0 +1,99 @@ +package governance + +import ( + "fmt" + + gogoproto "github.com/cosmos/gogoproto/proto" + gogoprotoany "github.com/cosmos/gogoproto/types/any" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "google.golang.org/protobuf/types/known/anypb" + + govv1 "cosmossdk.io/api/cosmos/gov/v1" + "cosmossdk.io/client/v2/internal/coins" +) + +const ( + // ModuleName is the name of the governance module name. + // It should match the module name of the cosmossdk.io/x/gov module. + ModuleName = "gov" + + FlagDeposit = "deposit" + FlagMetadata = "metadata" + FlagTitle = "title" + FlagSummary = "summary" + FlagExpedited = "expedited" +) + +// AddGovPropFlagsToCmd adds governance proposal flags to the provided command. +func AddGovPropFlagsToCmd(cmd *cobra.Command) { + cmd.Flags().String(FlagDeposit, "", "The deposit to include with the governance proposal") + cmd.Flags().String(FlagMetadata, "", "The metadata to include with the governance proposal") + cmd.Flags().String(FlagTitle, "", "The title to put on the governance proposal") + cmd.Flags().String(FlagSummary, "", "The summary to include with the governance proposal") + cmd.Flags().Bool(FlagExpedited, false, "Whether to expedite the governance proposal") +} + +// ReadGovPropCmdFlags parses a MsgSubmitProposal from the provided context and flags. +func ReadGovPropCmdFlags(proposer string, flagSet *pflag.FlagSet) (*govv1.MsgSubmitProposal, error) { + rv := &govv1.MsgSubmitProposal{} + + deposit, err := flagSet.GetString(FlagDeposit) + if err != nil { + return nil, fmt.Errorf("could not read deposit: %w", err) + } + if len(deposit) > 0 { + rv.InitialDeposit, err = coins.ParseCoinsNormalized(deposit) + if err != nil { + return nil, fmt.Errorf("invalid deposit: %w", err) + } + } + + rv.Metadata, err = flagSet.GetString(FlagMetadata) + if err != nil { + return nil, fmt.Errorf("could not read metadata: %w", err) + } + + rv.Title, err = flagSet.GetString(FlagTitle) + if err != nil { + return nil, fmt.Errorf("could not read title: %w", err) + } + + rv.Summary, err = flagSet.GetString(FlagSummary) + if err != nil { + return nil, fmt.Errorf("could not read summary: %w", err) + } + + expedited, err := flagSet.GetBool(FlagExpedited) + if err != nil { + return nil, fmt.Errorf("could not read expedited: %w", err) + } + if expedited { + rv.Expedited = true //nolint:staticcheck // We set it in case the message is made for an earlier version of the SDK + rv.ProposalType = govv1.ProposalType_PROPOSAL_TYPE_EXPEDITED + } + + rv.Proposer = proposer + + return rv, nil +} + +func SetGovMsgs(proposal *govv1.MsgSubmitProposal, msgs ...gogoproto.Message) error { + if len(msgs) == 0 { + return fmt.Errorf("zero messages is not supported") + } + + for _, msg := range msgs { + anyMsg, err := gogoprotoany.NewAnyWithCacheWithValue(msg) + if err != nil { + return err + } + + proposal.Messages = append(proposal.Messages, &anypb.Any{ + TypeUrl: anyMsg.TypeUrl, + Value: anyMsg.Value, + }) + } + + return nil +}