From 100531dfb64d365fbe849370f3ffbe4b45449460 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 09:02:41 +0000 Subject: [PATCH] feat(x/gov): add MsgSubmitProposal SetMsgs method (backport #17387) (#17388) Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + x/gov/types/v1/msgs.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5f8cd57c21..018da697567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (x/gov) [#17387](https://github.com/cosmos/cosmos-sdk/pull/17387) Add `MsgSubmitProposal` `SetMsgs` method. * (x/gov) [#17354](https://github.com/cosmos/cosmos-sdk/issues/17354) Emit `VoterAddr` in `proposal_vote` event. * (x/genutil) [#17296](https://github.com/cosmos/cosmos-sdk/pull/17296) Add `MigrateHandler` to allow reuse migrate genesis related function. * In v0.46, v0.47 this function is additive to the `genesis migrate` command. However in v0.50+, adding custom migrations to the `genesis migrate` command is directly possible. diff --git a/x/gov/types/v1/msgs.go b/x/gov/types/v1/msgs.go index fd8c5e5fb0e..1fb9be0c652 100644 --- a/x/gov/types/v1/msgs.go +++ b/x/gov/types/v1/msgs.go @@ -58,6 +58,41 @@ func (m *MsgSubmitProposal) SetMsgs(msgs []sdk.Msg) error { return nil } +// Route implements Msg +func (m MsgSubmitProposal) Route() string { return types.RouterKey } + +// Type implements Msg +func (m MsgSubmitProposal) Type() string { return sdk.MsgTypeURL(&m) } + +// ValidateBasic implements Msg +func (m MsgSubmitProposal) ValidateBasic() error { + if _, err := sdk.AccAddressFromBech32(m.Proposer); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid proposer address: %s", err) + } + + deposit := sdk.NewCoins(m.InitialDeposit...) + if !deposit.IsValid() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, deposit.String()) + } + + if deposit.IsAnyNegative() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, deposit.String()) + } + + // Check that either metadata or Msgs length is non nil. + if len(m.Messages) == 0 && len(m.Metadata) == 0 { + return sdkerrors.Wrap(types.ErrNoProposalMsgs, "either metadata or Msgs length must be non-nil") + } + + msgs, err := m.GetMsgs() + if err != nil { + return err + } + + m.Messages = anys + return nil +} + // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces func (m MsgSubmitProposal) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error { return sdktx.UnpackInterfaces(unpacker, m.Messages)