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

R4R: Renamed msg.Name() and msg.Type() to msg.Type() and msg.Route() #2553

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ BREAKING CHANGES
* [x/stake] \#2531 Remove all inflation logic
* [x/mint] \#2531 Add minting module and inflation logic
* [x/auth] [\#2540](https://github.com/cosmos/cosmos-sdk/issues/2540) Rename `AccountMapper` to `AccountKeeper`.
* [types] \#2456 Renamed msg.Name() and msg.Type() to msg.Type() and msg.Route() respectively

* Tendermint
* Update tendermint version from v0.23.0 to v0.25.0, notable changes
Expand Down
8 changes: 4 additions & 4 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,18 +528,18 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re
var code sdk.ABCICodeType
for msgIdx, msg := range msgs {
// Match route.
msgType := msg.Type()
handler := app.router.Route(msgType)
msgRoute := msg.Route()
handler := app.router.Route(msgRoute)
if handler == nil {
return sdk.ErrUnknownRequest("Unrecognized Msg type: " + msgType).Result()
return sdk.ErrUnknownRequest("Unrecognized Msg type: " + msgRoute).Result()
}

var msgResult sdk.Result
// Skip actual execution for CheckTx
if mode != runTxModeCheck {
msgResult = handler(ctx, msg)
}
msgResult.Tags = append(msgResult.Tags, sdk.MakeTag("action", []byte(msg.Name())))
msgResult.Tags = append(msgResult.Tags, sdk.MakeTag("action", []byte(msg.Type())))

// NOTE: GasWanted is determined by ante handler and
// GasUsed by the GasMeter
Expand Down
34 changes: 18 additions & 16 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ type txTest struct {
func (tx txTest) GetMsgs() []sdk.Msg { return tx.Msgs }

const (
typeMsgCounter = "msgCounter"
typeMsgCounter2 = "msgCounter2"
routeMsgCounter = "msgCounter"
routeMsgCounter2 = "msgCounter2"
)

// ValidateBasic() fails on negative counters.
Expand All @@ -301,8 +301,8 @@ type msgCounter struct {
}

// Implements Msg
func (msg msgCounter) Type() string { return typeMsgCounter }
func (msg msgCounter) Name() string { return "counter1" }
func (msg msgCounter) Route() string { return routeMsgCounter }
func (msg msgCounter) Type() string { return "counter1" }
func (msg msgCounter) GetSignBytes() []byte { return nil }
func (msg msgCounter) GetSigners() []sdk.AccAddress { return nil }
func (msg msgCounter) ValidateBasic() sdk.Error {
Expand All @@ -325,23 +325,23 @@ type msgNoRoute struct {
msgCounter
}

func (tx msgNoRoute) Type() string { return "noroute" }
func (tx msgNoRoute) Route() string { return "noroute" }

// a msg we dont know how to decode
type msgNoDecode struct {
msgCounter
}

func (tx msgNoDecode) Type() string { return typeMsgCounter }
func (tx msgNoDecode) Route() string { return routeMsgCounter }

// Another counter msg. Duplicate of msgCounter
type msgCounter2 struct {
Counter int64
}

// Implements Msg
func (msg msgCounter2) Type() string { return typeMsgCounter2 }
func (msg msgCounter2) Name() string { return "counter2" }
func (msg msgCounter2) Route() string { return routeMsgCounter2 }
func (msg msgCounter2) Type() string { return "counter2" }
func (msg msgCounter2) GetSignBytes() []byte { return nil }
func (msg msgCounter2) GetSigners() []sdk.AccAddress { return nil }
func (msg msgCounter2) ValidateBasic() sdk.Error {
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestCheckTx(t *testing.T) {
anteOpt := func(bapp *BaseApp) { bapp.SetAnteHandler(anteHandlerTxTest(t, capKey1, counterKey)) }
routerOpt := func(bapp *BaseApp) {
// TODO: can remove this once CheckTx doesnt process msgs.
bapp.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result { return sdk.Result{} })
bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result { return sdk.Result{} })
}

app := setupBaseApp(t, anteOpt, routerOpt)
Expand Down Expand Up @@ -486,7 +486,9 @@ func TestDeliverTx(t *testing.T) {

// test increments in the handler
deliverKey := []byte("deliver-key")
routerOpt := func(bapp *BaseApp) { bapp.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) }
routerOpt := func(bapp *BaseApp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

bapp.Router().AddRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey))
}

app := setupBaseApp(t, anteOpt, routerOpt)

Expand Down Expand Up @@ -527,8 +529,8 @@ func TestMultiMsgDeliverTx(t *testing.T) {
deliverKey := []byte("deliver-key")
deliverKey2 := []byte("deliver-key2")
routerOpt := func(bapp *BaseApp) {
bapp.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey))
bapp.Router().AddRoute(typeMsgCounter2, handlerMsgCounter(t, capKey1, deliverKey2))
bapp.Router().AddRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey))
bapp.Router().AddRoute(routeMsgCounter2, handlerMsgCounter(t, capKey1, deliverKey2))
}

app := setupBaseApp(t, anteOpt, routerOpt)
Expand All @@ -538,7 +540,7 @@ func TestMultiMsgDeliverTx(t *testing.T) {
registerTestCodec(codec)

// run a multi-msg tx
// with all msgs the same type
// with all msgs the same route
{
app.BeginBlock(abci.RequestBeginBlock{})
tx := newTxCounter(0, 0, 1, 2)
Expand Down Expand Up @@ -604,7 +606,7 @@ func TestSimulateTx(t *testing.T) {
}

routerOpt := func(bapp *BaseApp) {
bapp.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx.GasMeter().ConsumeGas(gasConsumed, "test")
return sdk.Result{GasUsed: ctx.GasMeter().GasConsumed()}
})
Expand Down Expand Up @@ -666,7 +668,7 @@ func TestRunInvalidTransaction(t *testing.T) {
})
}
routerOpt := func(bapp *BaseApp) {
bapp.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (res sdk.Result) { return })
bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (res sdk.Result) { return })
}

app := setupBaseApp(t, anteOpt, routerOpt)
Expand Down Expand Up @@ -771,7 +773,7 @@ func TestTxGasLimits(t *testing.T) {
}

routerOpt := func(bapp *BaseApp) {
bapp.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
count := msg.(msgCounter).Counter
ctx.GasMeter().ConsumeGas(count, "counter-handler")
return sdk.Result{}
Expand Down
2 changes: 1 addition & 1 deletion baseapp/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestQuery(t *testing.T) {
}

routerOpt := func(bapp *BaseApp) {
bapp.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
store := ctx.KVStore(capKey1)
store.Set(key, value)
return sdk.Result{}
Expand Down
2 changes: 1 addition & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
var msg auth.StdTx
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &msg))
require.Equal(t, len(msg.Msgs), 1)
require.Equal(t, msg.Msgs[0].Type(), "bank")
require.Equal(t, msg.Msgs[0].Route(), "bank")
require.Equal(t, msg.Msgs[0].GetSigners(), []sdk.AccAddress{addr})
require.Equal(t, 0, len(msg.Signatures))
gasEstimate := msg.Fee.Gas
Expand Down
6 changes: 3 additions & 3 deletions docs/sdk/core/app1.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Developers can create messages by implementing the `Msg` interface:

```go
type Msg interface {
// Return the message type.
// Return the message Route.
// Must be alphanumeric or empty.
// Must correspond to name of message handler (XXX).
Type() string
Route() string

// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
Expand Down Expand Up @@ -49,7 +49,7 @@ type MsgSend struct {
}

// Implements Msg.
func (msg MsgSend) Type() string { return "send" }
func (msg MsgSend) Route() string { return "send" }
```

It specifies that the message should be JSON marshaled and signed by the sender:
Expand Down
6 changes: 3 additions & 3 deletions docs/sdk/core/app2.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ type MsgIssue struct {
}

// Implements Msg.
func (msg MsgIssue) Type() string { return "issue" }
func (msg MsgIssue) Route() string { return "issue" }
```

Note the `Type()` method returns `"issue"`, so this message is of a different
type and will be executed by a different handler than `MsgSend`. The other
Note the `Route()` method returns `"issue"`, so this message is of a different
route and will be executed by a different handler than `MsgSend`. The other
methods for `MsgIssue` are similar to `MsgSend`.

## Handler
Expand Down
5 changes: 3 additions & 2 deletions docs/sdk/core/examples/app1.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func NewMsgSend(from, to sdk.AccAddress, amt sdk.Coins) MsgSend {
}

// Implements Msg.
func (msg MsgSend) Type() string { return "send" }
func (msg MsgSend) Name() string { return "send" }
// nolint
func (msg MsgSend) Route() string { return "send" }
func (msg MsgSend) Type() string { return "send" }

// Implements Msg. Ensure the addresses are good and the
// amount is positive.
Expand Down
5 changes: 3 additions & 2 deletions docs/sdk/core/examples/app2.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ type MsgIssue struct {
}

// Implements Msg.
func (msg MsgIssue) Type() string { return "issue" }
func (msg MsgIssue) Name() string { return "issue" }
// nolint
func (msg MsgIssue) Route() string { return "issue" }
func (msg MsgIssue) Type() string { return "issue" }

// Implements Msg. Ensures addresses are valid and Coin is positive
func (msg MsgIssue) ValidateBasic() sdk.Error {
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/cool/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewHandler(k Keeper) sdk.Handler {
case MsgQuiz:
return handleMsgQuiz(ctx, k, msg)
default:
errMsg := fmt.Sprintf("Unrecognized cool Msg type: %v", msg.Name())
errMsg := fmt.Sprintf("Unrecognized cool Msg type: %v", msg.Type())
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/democoin/x/cool/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func NewMsgSetTrend(sender sdk.AccAddress, cool string) MsgSetTrend {
var _ sdk.Msg = MsgSetTrend{}

// nolint
func (msg MsgSetTrend) Type() string { return "cool" }
func (msg MsgSetTrend) Name() string { return "set_trend" }
func (msg MsgSetTrend) Route() string { return "cool" }
func (msg MsgSetTrend) Type() string { return "set_trend" }
func (msg MsgSetTrend) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Sender} }
func (msg MsgSetTrend) String() string {
return fmt.Sprintf("MsgSetTrend{Sender: %v, Cool: %v}", msg.Sender, msg.Cool)
Expand Down Expand Up @@ -83,8 +83,8 @@ func NewMsgQuiz(sender sdk.AccAddress, coolerthancool string) MsgQuiz {
var _ sdk.Msg = MsgQuiz{}

// nolint
func (msg MsgQuiz) Type() string { return "cool" }
func (msg MsgQuiz) Name() string { return "quiz" }
func (msg MsgQuiz) Route() string { return "cool" }
func (msg MsgQuiz) Type() string { return "quiz" }
func (msg MsgQuiz) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Sender} }
func (msg MsgQuiz) String() string {
return fmt.Sprintf("MsgQuiz{Sender: %v, CoolAnswer: %v}", msg.Sender, msg.CoolAnswer)
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/oracle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type MyPayload struct {
}
```

When you write a payload, its `.Type()` should return same name with your module is registered on the router. It is because `oracle.Msg` inherits `.Type()` from its embedded payload and it should be handled on the user modules.
When you write a payload, its `.Route()` should return same route with your module is registered on the router. It is because `oracle.Msg` inherits `.Route()` from its embedded payload and it should be handled on the user modules.

Then route every incoming `oracle.Msg` to `oracle.Keeper.Handler()` with the function that implements `oracle.Handler`.

Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/oracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type seqOracle struct {
Nonce int
}

func (o seqOracle) Type() string {
func (o seqOracle) Route() string {
return "seq"
}
func (o seqOracle) Name() string {
func (o seqOracle) Type() string {
return "seq"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/oracle/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (msg Msg) GetSigners() []sdk.AccAddress {

// Payload defines inner data for actual execution
type Payload interface {
Route() string
Type() string
Name() string
ValidateBasic() sdk.Error
}
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (pk Keeper) Handler(ctx sdk.Context, msg sdk.Msg) sdk.Result {
case MsgMine:
return handleMsgMine(ctx, pk, msg)
default:
errMsg := "Unrecognized pow Msg type: " + msg.Name()
errMsg := "Unrecognized pow Msg type: " + msg.Type()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/pow/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func NewMsgMine(sender sdk.AccAddress, difficulty uint64, count uint64, nonce ui
}

// nolint
func (msg MsgMine) Type() string { return "pow" }
func (msg MsgMine) Name() string { return "mine" }
func (msg MsgMine) Route() string { return "pow" }
func (msg MsgMine) Type() string { return "mine" }
func (msg MsgMine) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Sender} }
func (msg MsgMine) String() string {
return fmt.Sprintf("MsgMine{Sender: %s, Difficulty: %d, Count: %d, Nonce: %d, Proof: %s}", msg.Sender, msg.Difficulty, msg.Count, msg.Nonce, msg.Proof)
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNewMsgMine(t *testing.T) {
func TestMsgMineType(t *testing.T) {
addr := sdk.AccAddress([]byte("sender"))
msg := MsgMine{addr, 0, 0, 0, []byte("")}
require.Equal(t, msg.Type(), "pow")
require.Equal(t, msg.Route(), "pow")
}

func TestMsgMineValidation(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions examples/democoin/x/simplestake/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewMsgBond(addr sdk.AccAddress, stake sdk.Coin, pubKey crypto.PubKey) MsgBo
}

//nolint
func (msg MsgBond) Type() string { return moduleName } //TODO update "stake/createvalidator"
func (msg MsgBond) Name() string { return "bond" }
func (msg MsgBond) Route() string { return moduleName } //TODO update "stake/createvalidator"
func (msg MsgBond) Type() string { return "bond" }
func (msg MsgBond) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Address} }

// basic validation of the bond message
Expand Down Expand Up @@ -66,8 +66,8 @@ func NewMsgUnbond(addr sdk.AccAddress) MsgUnbond {
}

//nolint
func (msg MsgUnbond) Type() string { return moduleName } //TODO update "stake/createvalidator"
func (msg MsgUnbond) Name() string { return "unbond" }
func (msg MsgUnbond) Route() string { return moduleName } //TODO update "stake/createvalidator"
func (msg MsgUnbond) Type() string { return "unbond" }
func (msg MsgUnbond) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Address} }
func (msg MsgUnbond) ValidateBasic() sdk.Error { return nil }

Expand Down
4 changes: 2 additions & 2 deletions examples/kvstore/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type kvstoreTx struct {
bytes []byte
}

func (tx kvstoreTx) Type() string {
func (tx kvstoreTx) Route() string {
return "kvstore"
}

func (tx kvstoreTx) Name() string {
func (tx kvstoreTx) Type() string {
return "kvstore"
}

Expand Down
4 changes: 2 additions & 2 deletions server/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func NewTx(key, value string) kvstoreTx {
}
}

func (tx kvstoreTx) Type() string {
func (tx kvstoreTx) Route() string {
return "kvstore"
}

func (tx kvstoreTx) Name() string {
func (tx kvstoreTx) Type() string {
return "kvstore_tx"
}

Expand Down
8 changes: 4 additions & 4 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type Msg interface {

// Return the message type.
// Must be alphanumeric or empty.
Type() string
Route() string

// Returns a human-readable string for the message, intended for utilization
// within tags
Name() string
Type() string

// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
Expand Down Expand Up @@ -58,8 +58,8 @@ func NewTestMsg(addrs ...AccAddress) *TestMsg {
}

//nolint
func (msg *TestMsg) Type() string { return "TestMsg" }
func (msg *TestMsg) Name() string { return "Test message" }
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
Loading