Skip to content

Commit

Permalink
Make sdk.Msg implement proto.Message (#6327)
Browse files Browse the repository at this point in the history
* Make sdk.Msg implement proto.Message

* Cleaning up

* Cleaning up

* Update CHANGELOG.md

* Lint fixes

* Lint fixes

* fix tests

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
aaronc and alexanderbez committed Jun 3, 2020
1 parent cd272d5 commit 2e11c81
Show file tree
Hide file tree
Showing 53 changed files with 610 additions and 233 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa
```
* (client/rpc) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `RegisterRoutes` of rpc is moved from package client to client/rpc and client/rpc.RegisterRPCRoutes is removed.
* (client/lcd) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `CliCtx` of struct `RestServer` in package client/lcd has been renamed to `ClientCtx`.
* (types) [\#6327](https://github.com/cosmos/cosmos-sdk/pull/6327) `sdk.Msg` now inherits `proto.Message`, as a result all `sdk.Msg` types now use pointer semantics.

### Features

Expand Down
10 changes: 10 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,11 @@ type msgCounter struct {
FailOnHandler bool
}

// dummy implementation of proto.Message
func (msg msgCounter) Reset() {}
func (msg msgCounter) String() string { return "TODO" }
func (msg msgCounter) ProtoMessage() {}

// Implements Msg
func (msg msgCounter) Route() string { return routeMsgCounter }
func (msg msgCounter) Type() string { return "counter1" }
Expand Down Expand Up @@ -634,6 +639,11 @@ type msgCounter2 struct {
Counter int64
}

// dummy implementation of proto.Message
func (msg msgCounter2) Reset() {}
func (msg msgCounter2) String() string { return "TODO" }
func (msg msgCounter2) ProtoMessage() {}

// Implements Msg
func (msg msgCounter2) Route() string { return routeMsgCounter2 }
func (msg msgCounter2) Type() string { return "counter2" }
Expand Down
6 changes: 6 additions & 0 deletions server/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ type kvstoreTx struct {
bytes []byte
}

// dummy implementation of proto.Message
func (msg kvstoreTx) Reset() {}
func (msg kvstoreTx) String() string { return "TODO" }
func (msg kvstoreTx) ProtoMessage() {}

var _ sdk.Tx = kvstoreTx{}
var _ sdk.Msg = kvstoreTx{}

func NewTx(key, value string) kvstoreTx {
bytes := fmt.Sprintf("%s=%s", key, value)
Expand Down
8 changes: 8 additions & 0 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package types
import (
"encoding/json"

"github.com/gogo/protobuf/proto"

"github.com/tendermint/tendermint/crypto"
)

type (
// Msg defines the interface a transaction message must fulfill.
Msg interface {
proto.Message

// Return the message type.
// Must be alphanumeric or empty.
Expand Down Expand Up @@ -71,6 +74,11 @@ type TestMsg struct {
signers []AccAddress
}

// dummy implementation of proto.Message
func (msg *TestMsg) Reset() {}
func (msg *TestMsg) String() string { return "TODO" }
func (msg *TestMsg) ProtoMessage() {}

func NewTestMsg(addrs ...AccAddress) *TestMsg {
return &TestMsg{
signers: addrs,
Expand Down
12 changes: 6 additions & 6 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ var (

sendMsg1 = types.NewMsgSend(addr1, addr2, coins)

multiSendMsg1 = types.MsgMultiSend{
multiSendMsg1 = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1, coins)},
Outputs: []types.Output{types.NewOutput(addr2, coins)},
}
multiSendMsg2 = types.MsgMultiSend{
multiSendMsg2 = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1, coins)},
Outputs: []types.Output{
types.NewOutput(addr2, halfCoins),
types.NewOutput(addr3, halfCoins),
},
}
multiSendMsg3 = types.MsgMultiSend{
multiSendMsg3 = &types.MsgMultiSend{
Inputs: []types.Input{
types.NewInput(addr1, coins),
types.NewInput(addr4, coins),
Expand All @@ -70,15 +70,15 @@ var (
types.NewOutput(addr3, coins),
},
}
multiSendMsg4 = types.MsgMultiSend{
multiSendMsg4 = &types.MsgMultiSend{
Inputs: []types.Input{
types.NewInput(addr2, coins),
},
Outputs: []types.Output{
types.NewOutput(addr1, coins),
},
}
multiSendMsg5 = types.MsgMultiSend{
multiSendMsg5 = &types.MsgMultiSend{
Inputs: []types.Input{
types.NewInput(addr1, coins),
},
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestSendToModuleAcc(t *testing.T) {
tests := []struct {
name string
fromBalance sdk.Coins
msg types.MsgSend
msg *types.MsgSend
expSimPass bool
expPass bool
expFromBalance sdk.Coins
Expand Down
8 changes: 4 additions & 4 deletions x/bank/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case types.MsgSend:
case *types.MsgSend:
return handleMsgSend(ctx, k, msg)

case types.MsgMultiSend:
case *types.MsgMultiSend:
return handleMsgMultiSend(ctx, k, msg)

default:
Expand All @@ -26,7 +26,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
}

// Handle MsgSend.
func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) (*sdk.Result, error) {
func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg *types.MsgSend) (*sdk.Result, error) {
if !k.GetSendEnabled(ctx) {
return nil, types.ErrSendDisabled
}
Expand All @@ -51,7 +51,7 @@ func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) (*sdk.Re
}

// Handle MsgMultiSend.
func handleMsgMultiSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgMultiSend) (*sdk.Result, error) {
func handleMsgMultiSend(ctx sdk.Context, k keeper.Keeper, msg *types.MsgMultiSend) (*sdk.Result, error) {
// NOTE: totalIn == totalOut should already have been checked
if !k.GetSendEnabled(ctx) {
return nil, types.ErrSendDisabled
Expand Down
6 changes: 3 additions & 3 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Operatio
// nolint: interfacer
func sendMsgSend(
r *rand.Rand, app *baseapp.BaseApp, bk keeper.Keeper, ak types.AccountKeeper,
msg types.MsgSend, ctx sdk.Context, chainID string, privkeys []crypto.PrivKey,
msg *types.MsgSend, ctx sdk.Context, chainID string, privkeys []crypto.PrivKey,
) error {

var (
Expand Down Expand Up @@ -198,7 +198,7 @@ func SimulateMsgMultiSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Ope
}
}

msg := types.MsgMultiSend{
msg := &types.MsgMultiSend{
Inputs: inputs,
Outputs: outputs,
}
Expand All @@ -217,7 +217,7 @@ func SimulateMsgMultiSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Ope
// nolint: interfacer
func sendMsgMultiSend(
r *rand.Rand, app *baseapp.BaseApp, bk keeper.Keeper, ak types.AccountKeeper,
msg types.MsgMultiSend, ctx sdk.Context, chainID string, privkeys []crypto.PrivKey,
msg *types.MsgMultiSend, ctx sdk.Context, chainID string, privkeys []crypto.PrivKey,
) error {

accountNumbers := make([]uint64, len(msg.Inputs))
Expand Down
4 changes: 2 additions & 2 deletions x/bank/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*exported.SupplyI)(nil), nil)
cdc.RegisterConcrete(&Supply{}, "cosmos-sdk/Supply", nil)
cdc.RegisterConcrete(MsgSend{}, "cosmos-sdk/MsgSend", nil)
cdc.RegisterConcrete(MsgMultiSend{}, "cosmos-sdk/MsgMultiSend", nil)
cdc.RegisterConcrete(&MsgSend{}, "cosmos-sdk/MsgSend", nil)
cdc.RegisterConcrete(&MsgMultiSend{}, "cosmos-sdk/MsgMultiSend", nil)
}

func RegisterInterfaces(registry types.InterfaceRegistry) {
Expand Down
12 changes: 6 additions & 6 deletions x/bank/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const (
TypeMsgMultiSend = "multisend"
)

var _ sdk.Msg = MsgSend{}
var _ sdk.Msg = &MsgSend{}

// NewMsgSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins) MsgSend {
return MsgSend{FromAddress: fromAddr, ToAddress: toAddr, Amount: amount}
func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins) *MsgSend {
return &MsgSend{FromAddress: fromAddr, ToAddress: toAddr, Amount: amount}
}

// Route Implements Msg.
Expand Down Expand Up @@ -55,11 +55,11 @@ func (msg MsgSend) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.FromAddress}
}

var _ sdk.Msg = MsgMultiSend{}
var _ sdk.Msg = &MsgMultiSend{}

// NewMsgMultiSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgMultiSend(in []Input, out []Output) MsgMultiSend {
return MsgMultiSend{Inputs: in, Outputs: out}
func NewMsgMultiSend(in []Input, out []Output) *MsgMultiSend {
return &MsgMultiSend{Inputs: in, Outputs: out}
}

// Route Implements Msg
Expand Down
2 changes: 1 addition & 1 deletion x/bank/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestMsgSendValidation(t *testing.T) {

cases := []struct {
valid bool
tx MsgSend
tx *MsgSend
}{
{true, NewMsgSend(addr1, addr2, atom123)}, // valid send
{true, NewMsgSend(addr1, addr2, atom123eth123)}, // valid send with multiple coins
Expand Down
4 changes: 2 additions & 2 deletions x/crisis/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case types.MsgVerifyInvariant:
case *types.MsgVerifyInvariant:
return handleMsgVerifyInvariant(ctx, msg, k)

default:
Expand All @@ -24,7 +24,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
}
}

func handleMsgVerifyInvariant(ctx sdk.Context, msg types.MsgVerifyInvariant, k keeper.Keeper) (*sdk.Result, error) {
func handleMsgVerifyInvariant(ctx sdk.Context, msg *types.MsgVerifyInvariant, k keeper.Keeper) (*sdk.Result, error) {
constantFee := sdk.NewCoins(k.GetConstantFee(ctx))

if err := k.SendCoinsFromAccountToFeeCollector(ctx, msg.Sender, constantFee); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/crisis/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ var _ sdk.Msg = &MsgVerifyInvariant{}

// NewMsgVerifyInvariant creates a new MsgVerifyInvariant object
func NewMsgVerifyInvariant(sender sdk.AccAddress, invariantModuleName,
invariantRoute string) MsgVerifyInvariant {
invariantRoute string) *MsgVerifyInvariant {

return MsgVerifyInvariant{
return &MsgVerifyInvariant{
Sender: sender,
InvariantModuleName: invariantModuleName,
InvariantRoute: invariantRoute,
Expand Down
16 changes: 8 additions & 8 deletions x/distribution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case types.MsgSetWithdrawAddress:
case *types.MsgSetWithdrawAddress:
return handleMsgModifyWithdrawAddress(ctx, msg, k)

case types.MsgWithdrawDelegatorReward:
case *types.MsgWithdrawDelegatorReward:
return handleMsgWithdrawDelegatorReward(ctx, msg, k)

case types.MsgWithdrawValidatorCommission:
case *types.MsgWithdrawValidatorCommission:
return handleMsgWithdrawValidatorCommission(ctx, msg, k)

case types.MsgFundCommunityPool:
case *types.MsgFundCommunityPool:
return handleMsgFundCommunityPool(ctx, msg, k)

default:
Expand All @@ -33,7 +33,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {

// These functions assume everything has been authenticated (ValidateBasic passed, and signatures checked)

func handleMsgModifyWithdrawAddress(ctx sdk.Context, msg types.MsgSetWithdrawAddress, k keeper.Keeper) (*sdk.Result, error) {
func handleMsgModifyWithdrawAddress(ctx sdk.Context, msg *types.MsgSetWithdrawAddress, k keeper.Keeper) (*sdk.Result, error) {
err := k.SetWithdrawAddr(ctx, msg.DelegatorAddress, msg.WithdrawAddress)
if err != nil {
return nil, err
Expand All @@ -50,7 +50,7 @@ func handleMsgModifyWithdrawAddress(ctx sdk.Context, msg types.MsgSetWithdrawAdd
return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil
}

func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDelegatorReward, k keeper.Keeper) (*sdk.Result, error) {
func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg *types.MsgWithdrawDelegatorReward, k keeper.Keeper) (*sdk.Result, error) {
_, err := k.WithdrawDelegationRewards(ctx, msg.DelegatorAddress, msg.ValidatorAddress)
if err != nil {
return nil, err
Expand All @@ -67,7 +67,7 @@ func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDele
return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil
}

func handleMsgWithdrawValidatorCommission(ctx sdk.Context, msg types.MsgWithdrawValidatorCommission, k keeper.Keeper) (*sdk.Result, error) {
func handleMsgWithdrawValidatorCommission(ctx sdk.Context, msg *types.MsgWithdrawValidatorCommission, k keeper.Keeper) (*sdk.Result, error) {
_, err := k.WithdrawValidatorCommission(ctx, msg.ValidatorAddress)
if err != nil {
return nil, err
Expand All @@ -84,7 +84,7 @@ func handleMsgWithdrawValidatorCommission(ctx sdk.Context, msg types.MsgWithdraw
return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil
}

func handleMsgFundCommunityPool(ctx sdk.Context, msg types.MsgFundCommunityPool, k keeper.Keeper) (*sdk.Result, error) {
func handleMsgFundCommunityPool(ctx sdk.Context, msg *types.MsgFundCommunityPool, k keeper.Keeper) (*sdk.Result, error) {
if err := k.FundCommunityPool(ctx, msg.Amount, msg.Depositor); err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions x/distribution/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
// RegisterCodec registers the necessary x/distribution interfaces and concrete types
// on the provided Amino codec. These types are used for Amino JSON serialization.
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward", nil)
cdc.RegisterConcrete(MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValidatorCommission", nil)
cdc.RegisterConcrete(MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress", nil)
cdc.RegisterConcrete(MsgFundCommunityPool{}, "cosmos-sdk/MsgFundCommunityPool", nil)
cdc.RegisterConcrete(&MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward", nil)
cdc.RegisterConcrete(&MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValidatorCommission", nil)
cdc.RegisterConcrete(&MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress", nil)
cdc.RegisterConcrete(&MsgFundCommunityPool{}, "cosmos-sdk/MsgFundCommunityPool", nil)
cdc.RegisterConcrete(&CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal", nil)
}

Expand Down
16 changes: 8 additions & 8 deletions x/distribution/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
// Verify interface at compile time
var _, _, _ sdk.Msg = &MsgSetWithdrawAddress{}, &MsgWithdrawDelegatorReward{}, &MsgWithdrawValidatorCommission{}

func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) MsgSetWithdrawAddress {
return MsgSetWithdrawAddress{
func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) *MsgSetWithdrawAddress {
return &MsgSetWithdrawAddress{
DelegatorAddress: delAddr,
WithdrawAddress: withdrawAddr,
}
Expand Down Expand Up @@ -50,8 +50,8 @@ func (msg MsgSetWithdrawAddress) ValidateBasic() error {
return nil
}

func NewMsgWithdrawDelegatorReward(delAddr sdk.AccAddress, valAddr sdk.ValAddress) MsgWithdrawDelegatorReward {
return MsgWithdrawDelegatorReward{
func NewMsgWithdrawDelegatorReward(delAddr sdk.AccAddress, valAddr sdk.ValAddress) *MsgWithdrawDelegatorReward {
return &MsgWithdrawDelegatorReward{
DelegatorAddress: delAddr,
ValidatorAddress: valAddr,
}
Expand Down Expand Up @@ -82,8 +82,8 @@ func (msg MsgWithdrawDelegatorReward) ValidateBasic() error {
return nil
}

func NewMsgWithdrawValidatorCommission(valAddr sdk.ValAddress) MsgWithdrawValidatorCommission {
return MsgWithdrawValidatorCommission{
func NewMsgWithdrawValidatorCommission(valAddr sdk.ValAddress) *MsgWithdrawValidatorCommission {
return &MsgWithdrawValidatorCommission{
ValidatorAddress: valAddr,
}
}
Expand Down Expand Up @@ -112,8 +112,8 @@ func (msg MsgWithdrawValidatorCommission) ValidateBasic() error {

// NewMsgFundCommunityPool returns a new MsgFundCommunityPool with a sender and
// a funding amount.
func NewMsgFundCommunityPool(amount sdk.Coins, depositor sdk.AccAddress) MsgFundCommunityPool {
return MsgFundCommunityPool{
func NewMsgFundCommunityPool(amount sdk.Coins, depositor sdk.AccAddress) *MsgFundCommunityPool {
return &MsgFundCommunityPool{
Amount: amount,
Depositor: depositor,
}
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/tendermint/tendermint/crypto"
)

func newTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) staking.MsgCreateValidator {
func newTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) *staking.MsgCreateValidator {
commission := staking.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
return staking.NewMsgCreateValidator(
address, pubKey, sdk.NewCoin(sdk.DefaultBondDenom, amt),
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// evidence module.
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*exported.Evidence)(nil), nil)
cdc.RegisterConcrete(MsgSubmitEvidence{}, "cosmos-sdk/MsgSubmitEvidence", nil)
cdc.RegisterConcrete(&MsgSubmitEvidence{}, "cosmos-sdk/MsgSubmitEvidence", nil)
cdc.RegisterConcrete(&Equivocation{}, "cosmos-sdk/Equivocation", nil)
}

Expand Down
Loading

0 comments on commit 2e11c81

Please sign in to comment.