Skip to content

Commit

Permalink
refactor(client/v2): remove gov dependency (backport #22867) (#22869)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Dec 13, 2024
1 parent f1e881e commit 2e15f67
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
14 changes: 5 additions & 9 deletions client/v2/autocli/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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")
}

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

Expand Down
4 changes: 1 addition & 3 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
99 changes: 99 additions & 0 deletions client/v2/internal/governance/gov.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2e15f67

Please sign in to comment.