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

This commit is contained in:
Julien Robert
2023-01-24 17:03:06 +00:00
committed by GitHub
parent bc0386e996
commit 8dbdfea9ef
48 changed files with 409 additions and 769 deletions
+13 -9
View File
@@ -66,10 +66,10 @@ type OperationMsg struct {
}
// NewOperationMsgBasic creates a new operation message from raw input.
func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) OperationMsg {
func NewOperationMsgBasic(moduleName, msgType, comment string, ok bool, msg []byte) OperationMsg {
return OperationMsg{
Route: route,
Name: name,
Route: moduleName,
Name: msgType,
Comment: comment,
OK: ok,
Msg: msg,
@@ -78,18 +78,22 @@ 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())
msgType := sdk.MsgTypeURL(msg)
moduleName := sdk.GetModuleNameFromTypeURL(msgType)
if moduleName == "" {
moduleName = msgType
}
bz := cdc.MustMarshalJSON(msg)
if legacyMsg, okType := msg.(legacytx.LegacyMsg); okType {
return NewOperationMsgBasic(moduleName, msgType, comment, ok, legacyMsg.GetSignBytes())
}
return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, bz)
return NewOperationMsgBasic(moduleName, msgType, comment, ok, cdc.MustMarshalJSON(msg))
}
// NoOpMsg - create a no-operation message
func NoOpMsg(route, msgType, comment string) OperationMsg {
return NewOperationMsgBasic(route, msgType, comment, false, nil)
func NoOpMsg(moduleName, msgType, comment string) OperationMsg {
return NewOperationMsgBasic(moduleName, msgType, comment, false, nil)
}
// log entry text for this operation msg
+13
View File
@@ -3,6 +3,7 @@ package types
import (
"encoding/json"
fmt "fmt"
strings "strings"
"github.com/cosmos/gogoproto/proto"
@@ -102,3 +103,15 @@ func GetMsgFromTypeURL(cdc codec.Codec, input string) (Msg, error) {
return msg, nil
}
// GetModuleNameFromTypeURL assumes that module name is the second element of the msg type URL
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
// It returns an empty string if the input is not a valid type URL
func GetModuleNameFromTypeURL(input string) string {
moduleName := strings.Split(input, ".")
if len(moduleName) > 1 {
return moduleName[1]
}
return ""
}
-2
View File
@@ -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() })
}