Skip to content

Commit 8dbdfea

Browse files
authored
refactor: remove .Type() and .Route() from msgs (#14751)
1 parent bc0386e commit 8dbdfea

File tree

48 files changed

+409
-769
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+409
-769
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
7474

7575
### Improvements
7676

77+
* (x/auth/tx) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove `.Type()` and `Route()` methods from all msgs and `legacytx.LegacyMsg` interface.
7778
* [#14691](https://github.com/cosmos/cosmos-sdk/pull/14691) Change behavior of `sdk.StringifyEvents` to not flatten events attributes by events type.
7879
* This change only affects ABCI message logs, and not the actual events.
7980
* [#14692](https://github.com/cosmos/cosmos-sdk/pull/14692) Improve RPC queries error message when app is at height 0.
@@ -165,6 +166,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
165166

166167
### API Breaking Changes
167168

169+
* (x/simulation) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove the `MsgType` field from `simulation.OperationInput` struct.
168170
* (crypto/keyring) [#13734](https://github.com/cosmos/cosmos-sdk/pull/13834) The keyring's `Sign` method now takes a new `signMode` argument. It is only used if the signing key is a Ledger hardware device. You can set it to 0 in all other cases.
169171
* (x/evidence) [14724](https://github.com/cosmos/cosmos-sdk/pull/14724) Extract Evidence in its own go.mod and rename the package to `cosmossdk.io/x/evidence`.
170172
* (x/nft) [#14725](https://github.com/cosmos/cosmos-sdk/pull/14725) Extract NFT in its own go.mod and rename the package to `cosmossdk.io/x/nft`.

baseapp/baseapp.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,11 +835,8 @@ func createEvents(events sdk.Events, msg sdk.Msg) sdk.Events {
835835

836836
// verify that events have no module attribute set
837837
if _, found := events.GetAttributes(sdk.AttributeKeyModule); !found {
838-
// here we assume that routes module name is the second element of the route
839-
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
840-
moduleName := strings.Split(eventMsgName, ".")
841-
if len(moduleName) > 1 {
842-
msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeyModule, moduleName[1]))
838+
if moduleName := sdk.GetModuleNameFromTypeURL(eventMsgName); moduleName != "" {
839+
msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeyModule, moduleName))
843840
}
844841
}
845842

docs/docs/building-modules/02-messages-and-queries.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/types/tx_msg.go#L14-L26
6363

6464
It extends `proto.Message` and contains the following methods:
6565

66-
* `Route() string`: Name of the route for this message. Typically all `message`s in a module have the same route, which is most often the module's name.
67-
* `Type() string`: Type of the message, used primarily in [events](../core/08-events.md). This should return a message-specific `string`, typically the denomination of the message itself.
6866
* [`ValidateBasic() error`](../basics/01-tx-lifecycle.md#ValidateBasic).
6967
* `GetSignBytes() []byte`: Return the canonical byte representation of the message. Used to generate a signature.
7068
* `GetSigners() []AccAddress`: Return the list of signers. The Cosmos SDK will make sure that each `message` contained in a transaction is signed by all the signers listed in the list returned by this method.

docs/docs/core/08-events.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ The following examples show how to query Events using the Cosmos SDK.
5757
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
5858
| `tx.height=23` | Query all transactions at height 23 |
5959
| `message.action='/cosmos.bank.v1beta1.Msg/Send'` | Query all transactions containing a x/bank `Send` [Service `Msg`](../building-modules/03-msg-services.md). Note the `'`s around the value. |
60-
| `message.action='send'` | Query all transactions containing a x/bank `Send` [legacy `Msg`](../building-modules/03-msg-services.md#legacy-amino-msgs). Note the `'`s around the value. |
6160
| `message.module='bank'` | Query all transactions containing messages from the x/bank module. Note the `'`s around the value. |
6261
| `create_validator.validator='cosmosval1...'` | x/staking-specific Event, see [x/staking SPEC](../modules/staking/README.md). |
6362

testutil/testdata/tx.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ func NewTestMsg(addrs ...sdk.AccAddress) *TestMsg {
7878

7979
var _ sdk.Msg = (*TestMsg)(nil)
8080

81-
func (msg *TestMsg) Route() string { return "TestMsg" }
82-
func (msg *TestMsg) Type() string { return "Test message" }
8381
func (msg *TestMsg) GetSignBytes() []byte {
8482
bz, err := json.Marshal(msg.Signers)
8583
if err != nil {

types/simulation/types.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ type OperationMsg struct {
6666
}
6767

6868
// NewOperationMsgBasic creates a new operation message from raw input.
69-
func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) OperationMsg {
69+
func NewOperationMsgBasic(moduleName, msgType, comment string, ok bool, msg []byte) OperationMsg {
7070
return OperationMsg{
71-
Route: route,
72-
Name: name,
71+
Route: moduleName,
72+
Name: msgType,
7373
Comment: comment,
7474
OK: ok,
7575
Msg: msg,
@@ -78,18 +78,22 @@ func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) Oper
7878

7979
// NewOperationMsg - create a new operation message from sdk.Msg
8080
func NewOperationMsg(msg sdk.Msg, ok bool, comment string, cdc *codec.ProtoCodec) OperationMsg {
81-
if legacyMsg, okType := msg.(legacytx.LegacyMsg); okType {
82-
return NewOperationMsgBasic(legacyMsg.Route(), legacyMsg.Type(), comment, ok, legacyMsg.GetSignBytes())
81+
msgType := sdk.MsgTypeURL(msg)
82+
moduleName := sdk.GetModuleNameFromTypeURL(msgType)
83+
if moduleName == "" {
84+
moduleName = msgType
8385
}
8486

85-
bz := cdc.MustMarshalJSON(msg)
87+
if legacyMsg, okType := msg.(legacytx.LegacyMsg); okType {
88+
return NewOperationMsgBasic(moduleName, msgType, comment, ok, legacyMsg.GetSignBytes())
89+
}
8690

87-
return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, bz)
91+
return NewOperationMsgBasic(moduleName, msgType, comment, ok, cdc.MustMarshalJSON(msg))
8892
}
8993

9094
// NoOpMsg - create a no-operation message
91-
func NoOpMsg(route, msgType, comment string) OperationMsg {
92-
return NewOperationMsgBasic(route, msgType, comment, false, nil)
95+
func NoOpMsg(moduleName, msgType, comment string) OperationMsg {
96+
return NewOperationMsgBasic(moduleName, msgType, comment, false, nil)
9397
}
9498

9599
// log entry text for this operation msg

types/tx_msg.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"encoding/json"
55
fmt "fmt"
6+
strings "strings"
67

78
"github.com/cosmos/gogoproto/proto"
89

@@ -102,3 +103,15 @@ func GetMsgFromTypeURL(cdc codec.Codec, input string) (Msg, error) {
102103

103104
return msg, nil
104105
}
106+
107+
// GetModuleNameFromTypeURL assumes that module name is the second element of the msg type URL
108+
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
109+
// It returns an empty string if the input is not a valid type URL
110+
func GetModuleNameFromTypeURL(input string) string {
111+
moduleName := strings.Split(input, ".")
112+
if len(moduleName) > 1 {
113+
return moduleName[1]
114+
}
115+
116+
return ""
117+
}

types/tx_msg_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ func (s *testMsgSuite) TestMsg() {
2525
msg := testdata.NewTestMsg(accAddr)
2626
s.Require().NotNil(msg)
2727
s.Require().True(accAddr.Equals(msg.GetSigners()[0]))
28-
s.Require().Equal("TestMsg", msg.Route())
29-
s.Require().Equal("Test message", msg.Type())
3028
s.Require().Nil(msg.ValidateBasic())
3129
s.Require().NotPanics(func() { msg.GetSignBytes() })
3230
}

x/auth/migrations/legacytx/stdsign.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,14 @@ import (
1717
"github.com/cosmos/cosmos-sdk/types/tx/signing"
1818
)
1919

20-
// LegacyMsg defines the old interface a message must fulfill, containing
21-
// Amino signing method and legacy router info.
20+
// LegacyMsg defines the old interface a message must fulfill,
21+
// containing Amino signing method.
2222
// Deprecated: Please use `Msg` instead.
2323
type LegacyMsg interface {
2424
sdk.Msg
2525

2626
// Get the canonical byte representation of the Msg.
2727
GetSignBytes() []byte
28-
29-
// Return the message type.
30-
// Must be alphanumeric or empty.
31-
Route() string
32-
33-
// Returns a human-readable string for the message, intended for utilization
34-
// within tags
35-
Type() string
3628
}
3729

3830
// StdSignDoc is replay-prevention structure.

x/auth/types/msgs.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ package types
33
import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
55
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
6+
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
67
)
78

8-
var _ sdk.Msg = &MsgUpdateParams{}
9+
var (
10+
_ sdk.Msg = &MsgUpdateParams{}
11+
_ legacytx.LegacyMsg = &MsgUpdateParams{}
12+
)
913

1014
// GetSignBytes implements the LegacyMsg interface.
1115
func (msg MsgUpdateParams) GetSignBytes() []byte {
1216
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
1317
}
1418

1519
// GetSigners returns the expected signers for a MsgUpdateParams message.
16-
func (msg *MsgUpdateParams) GetSigners() []sdk.AccAddress {
20+
func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress {
1721
addr, _ := sdk.AccAddressFromBech32(msg.Authority)
1822
return []sdk.AccAddress{addr}
1923
}
2024

2125
// ValidateBasic does a sanity check on the provided data.
22-
func (msg *MsgUpdateParams) ValidateBasic() error {
26+
func (msg MsgUpdateParams) ValidateBasic() error {
2327
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
2428
return sdkerrors.Wrap(err, "invalid authority address")
2529
}

0 commit comments

Comments
 (0)