Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove .Type() and .Route() from msgs #14751

Merged
merged 13 commits into from
Jan 24, 2023
7 changes: 4 additions & 3 deletions testutil/testdata/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ func NewTestMsg(addrs ...sdk.AccAddress) *TestMsg {
}
}

var _ sdk.Msg = (*TestMsg)(nil)
var (
_ sdk.Msg = (*TestMsg)(nil)
_ sdk.HasAminoSigningCapability = (*TestMsg)(nil)
)

func (msg *TestMsg) Route() string { return "TestMsg" }
func (msg *TestMsg) Type() string { return "Test message" }
func (msg *TestMsg) GetSignBytes() []byte {
bz, err := json.Marshal(msg.Signers)
if err != nil {
Expand Down
9 changes: 3 additions & 6 deletions types/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

type WeightedProposalContent interface {
Expand Down Expand Up @@ -78,13 +77,11 @@ func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) Oper

// NewOperationMsg - create a new operation message from sdk.Msg
func NewOperationMsg(msg sdk.Msg, ok bool, comment string, cdc *codec.ProtoCodec) OperationMsg {
if legacyMsg, okType := msg.(legacytx.LegacyMsg); okType {
return NewOperationMsgBasic(legacyMsg.Route(), legacyMsg.Type(), comment, ok, legacyMsg.GetSignBytes())
if legacyMsg, okType := msg.(sdk.HasAminoSigningCapability); okType {
return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, legacyMsg.GetSignBytes())
}

bz := cdc.MustMarshalJSON(msg)

return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, bz)
return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, cdc.MustMarshalJSON(msg))
}

// NoOpMsg - create a no-operation message
Expand Down
10 changes: 10 additions & 0 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ type (
GetSigners() []AccAddress
}

// Extension interface for Msg that require Amino Signing
// Replaces legacytx.LegacyMsg.
// Deprecated: use Msg instead.
HasAminoSigningCapability interface {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
Msg

// Get the canonical byte representation of the Msg.
GetSignBytes() []byte
}

// Fee defines an interface for an application application-defined concrete
// transaction type to be able to set and return the transaction fee.
Fee interface {
Expand Down
2 changes: 0 additions & 2 deletions types/tx_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func (s *testMsgSuite) TestMsg() {
msg := testdata.NewTestMsg(accAddr)
s.Require().NotNil(msg)
s.Require().True(accAddr.Equals(msg.GetSigners()[0]))
s.Require().Equal("TestMsg", msg.Route())
s.Require().Equal("Test message", msg.Type())
s.Require().Nil(msg.ValidateBasic())
s.Require().NotPanics(func() { msg.GetSignBytes() })
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/migrations/legacytx/stdsign.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ type StdSignDoc struct {
func StdSignBytes(chainID string, accnum, sequence, timeout uint64, fee StdFee, msgs []sdk.Msg, memo string, tip *tx.Tip) []byte {
msgsBytes := make([]json.RawMessage, 0, len(msgs))
for _, msg := range msgs {
legacyMsg, ok := msg.(LegacyMsg)
legacyMsg, ok := msg.(sdk.HasAminoSigningCapability)
if !ok {
panic(fmt.Errorf("expected %T when using amino JSON", (*LegacyMsg)(nil)))
panic(fmt.Errorf("expected %T when using amino JSON", (*sdk.HasAminoSigningCapability)(nil)))
}

msgsBytes = append(msgsBytes, json.RawMessage(legacyMsg.GetSignBytes()))
Expand Down
41 changes: 9 additions & 32 deletions x/auth/vesting/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// TypeMsgCreateVestingAccount defines the type value for a MsgCreateVestingAccount.
const TypeMsgCreateVestingAccount = "msg_create_vesting_account"

// TypeMsgCreatePermanentLockedAccount defines the type value for a MsgCreatePermanentLockedAccount.
const TypeMsgCreatePermanentLockedAccount = "msg_create_permanent_locked_account"

// TypeMsgCreatePeriodicVestingAccount defines the type value for a MsgCreateVestingAccount.
const TypeMsgCreatePeriodicVestingAccount = "msg_create_periodic_vesting_account"

var _ sdk.Msg = &MsgCreateVestingAccount{}

var _ sdk.Msg = &MsgCreatePermanentLockedAccount{}

var _ sdk.Msg = &MsgCreatePeriodicVestingAccount{}
var (
_ sdk.Msg = &MsgCreateVestingAccount{}
_ sdk.Msg = &MsgCreatePermanentLockedAccount{}
_ sdk.Msg = &MsgCreatePeriodicVestingAccount{}

_ sdk.HasAminoSigningCapability = &MsgCreateVestingAccount{}
_ sdk.HasAminoSigningCapability = &MsgCreatePermanentLockedAccount{}
_ sdk.HasAminoSigningCapability = &MsgCreatePeriodicVestingAccount{}
)

// NewMsgCreateVestingAccount returns a reference to a new MsgCreateVestingAccount.
//
Expand All @@ -35,12 +30,6 @@ func NewMsgCreateVestingAccount(fromAddr, toAddr sdk.AccAddress, amount sdk.Coin
}
}

// Route returns the message route for a MsgCreateVestingAccount.
func (msg MsgCreateVestingAccount) Route() string { return RouterKey }

// Type returns the message type for a MsgCreateVestingAccount.
func (msg MsgCreateVestingAccount) Type() string { return TypeMsgCreateVestingAccount }

// ValidateBasic Implements Msg.
func (msg MsgCreateVestingAccount) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
Expand Down Expand Up @@ -88,12 +77,6 @@ func NewMsgCreatePermanentLockedAccount(fromAddr, toAddr sdk.AccAddress, amount
}
}

// Route returns the message route for a MsgCreatePermanentLockedAccount.
func (msg MsgCreatePermanentLockedAccount) Route() string { return RouterKey }

// Type returns the message type for a MsgCreatePermanentLockedAccount.
func (msg MsgCreatePermanentLockedAccount) Type() string { return TypeMsgCreatePermanentLockedAccount }

// ValidateBasic Implements Msg.
func (msg MsgCreatePermanentLockedAccount) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
Expand Down Expand Up @@ -138,12 +121,6 @@ func NewMsgCreatePeriodicVestingAccount(fromAddr, toAddr sdk.AccAddress, startTi
}
}

// Route returns the message route for a MsgCreatePeriodicVestingAccount.
func (msg MsgCreatePeriodicVestingAccount) Route() string { return RouterKey }

// Type returns the message type for a MsgCreatePeriodicVestingAccount.
func (msg MsgCreatePeriodicVestingAccount) Type() string { return TypeMsgCreatePeriodicVestingAccount }

// GetSigners returns the expected signers for a MsgCreatePeriodicVestingAccount.
func (msg MsgCreatePeriodicVestingAccount) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
Expand Down
43 changes: 6 additions & 37 deletions x/authz/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

var (
Expand All @@ -19,9 +18,9 @@ var (
_ sdk.Msg = &MsgExec{}

// For amino support.
_ legacytx.LegacyMsg = &MsgGrant{}
_ legacytx.LegacyMsg = &MsgRevoke{}
_ legacytx.LegacyMsg = &MsgExec{}
_ sdk.HasAminoSigningCapability = &MsgGrant{}
_ sdk.HasAminoSigningCapability = &MsgRevoke{}
_ sdk.HasAminoSigningCapability = &MsgExec{}

_ cdctypes.UnpackInterfacesMessage = &MsgGrant{}
_ cdctypes.UnpackInterfacesMessage = &MsgExec{}
Expand Down Expand Up @@ -66,17 +65,7 @@ func (msg MsgGrant) ValidateBasic() error {
return msg.Grant.ValidateBasic()
}

// Type implements the LegacyMsg.Type method.
func (msg MsgGrant) Type() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Route method.
func (msg MsgGrant) Route() string {
return sdk.MsgTypeURL(&msg)
}

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
// GetSignBytes implements the HasAminoSigningCapability.GetSignBytes method.
func (msg MsgGrant) GetSignBytes() []byte {
return sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msg))
}
Expand Down Expand Up @@ -157,17 +146,7 @@ func (msg MsgRevoke) ValidateBasic() error {
return nil
}

// Type implements the LegacyMsg.Type method.
func (msg MsgRevoke) Type() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Route method.
func (msg MsgRevoke) Route() string {
return sdk.MsgTypeURL(&msg)
}

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
// GetSignBytes implements the HasAminoSigningCapability.GetSignBytes method.
func (msg MsgRevoke) GetSignBytes() []byte {
return sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msg))
}
Expand Down Expand Up @@ -235,17 +214,7 @@ func (msg MsgExec) ValidateBasic() error {
return nil
}

// Type implements the LegacyMsg.Type method.
func (msg MsgExec) Type() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Route method.
func (msg MsgExec) Route() string {
return sdk.MsgTypeURL(&msg)
}

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
// GetSignBytes implements the HasAminoSigningCapability.GetSignBytes method.
func (msg MsgExec) GetSignBytes() []byte {
return sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msg))
}
2 changes: 1 addition & 1 deletion x/authz/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestMsgGrantGetAuthorization(t *testing.T) {

func TestAminoJSON(t *testing.T) {
tx := legacytx.StdTx{}
var msg legacytx.LegacyMsg
var msg sdk.HasAminoSigningCapability
blockTime := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC)
expiresAt := blockTime.Add(time.Hour)
msgSend := banktypes.MsgSend{FromAddress: "cosmos1ghi", ToAddress: "cosmos1jkl"}
Expand Down
24 changes: 4 additions & 20 deletions x/bank/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// bank message types
const (
TypeMsgSend = "send"
TypeMsgMultiSend = "multisend"
TypeMsgSetSendEnabled = "set_send_enabled"
TypeMsgUpdateParams = "update_params"
)

var (
_ sdk.Msg = &MsgSend{}
_ sdk.Msg = &MsgMultiSend{}
_ sdk.Msg = &MsgUpdateParams{}

_ sdk.HasAminoSigningCapability = &MsgSend{}
_ sdk.HasAminoSigningCapability = &MsgMultiSend{}
_ sdk.HasAminoSigningCapability = &MsgUpdateParams{}
)

// NewMsgSend - construct a msg to send coins from one account to another.
Expand All @@ -26,12 +22,6 @@ func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins) *MsgSend {
return &MsgSend{FromAddress: fromAddr.String(), ToAddress: toAddr.String(), Amount: amount}
}

// Route Implements Msg.
func (msg MsgSend) Route() string { return RouterKey }

// Type Implements Msg.
func (msg MsgSend) Type() string { return TypeMsgSend }

// ValidateBasic Implements Msg.
func (msg MsgSend) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
Expand Down Expand Up @@ -69,12 +59,6 @@ func NewMsgMultiSend(in []Input, out []Output) *MsgMultiSend {
return &MsgMultiSend{Inputs: in, Outputs: out}
}

// Route Implements Msg
func (msg MsgMultiSend) Route() string { return RouterKey }

// Type Implements Msg
func (msg MsgMultiSend) Type() string { return TypeMsgMultiSend }

// ValidateBasic Implements Msg.
func (msg MsgMultiSend) ValidateBasic() error {
// this just makes sure the input and all the outputs are properly formatted,
Expand Down
14 changes: 4 additions & 10 deletions x/consensus/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
tmtypes "github.com/tendermint/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

const (
TypeMsgUpdateParams = "update_params"
)

var _ legacytx.LegacyMsg = &MsgUpdateParams{}
var (
_ sdk.Msg = &MsgUpdateParams{}
_ sdk.HasAminoSigningCapability = &MsgUpdateParams{}
)

// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
Expand All @@ -27,14 +29,6 @@ func (msg MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

func (msg MsgUpdateParams) Route() string {
return sdk.MsgTypeURL(&msg)
}

func (msg MsgUpdateParams) Type() string {
return sdk.MsgTypeURL(&msg)
}

// ValidateBasic performs basic MsgUpdateParams message validation.
func (msg MsgUpdateParams) ValidateBasic() error {
params := tmtypes.ConsensusParamsFromProto(msg.ToProtoConsensusParams())
Expand Down
22 changes: 4 additions & 18 deletions x/crisis/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const (
TypeMsgVerifyInvariant = "verify_invariant"
TypeMsgUpdateParams = "update_params"
)

// ensure Msg interface compliance at compile time
var _, _ sdk.Msg = &MsgVerifyInvariant{}, &MsgUpdateParams{}
var (
_, _ sdk.Msg = &MsgVerifyInvariant{}, &MsgUpdateParams{}
_, _ sdk.HasAminoSigningCapability = &MsgVerifyInvariant{}, &MsgUpdateParams{}
)

// NewMsgVerifyInvariant creates a new MsgVerifyInvariant object
//
Expand All @@ -24,12 +22,6 @@ func NewMsgVerifyInvariant(sender sdk.AccAddress, invModeName, invRoute string)
}
}

// Route returns the MsgVerifyInvariant's route.
func (msg MsgVerifyInvariant) Route() string { return ModuleName }

// Type returns the MsgVerifyInvariant's type.
func (msg MsgVerifyInvariant) Type() string { return TypeMsgVerifyInvariant }

// get the bytes for the message signer to sign on
func (msg MsgVerifyInvariant) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
Expand All @@ -55,12 +47,6 @@ func (msg MsgVerifyInvariant) FullInvariantRoute() string {
return msg.InvariantModuleName + "/" + msg.InvariantRoute
}

// Route returns the MsgUpdateParams's route.
func (msg MsgUpdateParams) Route() string { return ModuleName }

// Type returns the MsgUpdateParams's type.
func (msg MsgUpdateParams) Type() string { return TypeMsgUpdateParams }

// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress {
Expand Down
Loading