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

Rename DeserializeTx, enforce []sdk.Msg usage in SerializeCosmosTx #457

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 1 addition & 11 deletions modules/apps/27-interchain-accounts/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,7 @@ func NewKeeper(
}

// SerializeCosmosTx marshals data to bytes using the provided codec
func (k Keeper) SerializeCosmosTx(cdc codec.BinaryCodec, data interface{}) ([]byte, error) {
msgs := make([]sdk.Msg, 0)
switch data := data.(type) {
case sdk.Msg:
msgs = append(msgs, data)
case []sdk.Msg:
msgs = append(msgs, data...)
default:
return nil, types.ErrInvalidOutgoingData
}

func (k Keeper) SerializeCosmosTx(cdc codec.BinaryCodec, msgs []sdk.Msg) ([]byte, error) {
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
msgAnys := make([]*codectypes.Any, len(msgs))

for i, msg := range msgs {
Expand Down
21 changes: 17 additions & 4 deletions modules/apps/27-interchain-accounts/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,20 @@ func (k Keeper) createOutgoingPacket(
return []byte{}, types.ErrInvalidOutgoingData
}

txBytes, err := k.SerializeCosmosTx(k.cdc, data)
var (
txBytes []byte
err error
)

switch data := data.(type) {
case []sdk.Msg:
txBytes, err = k.SerializeCosmosTx(k.cdc, data)
default:
return nil, sdkerrors.Wrapf(types.ErrInvalidOutgoingData, "message type %T is not supported", data)
}

if err != nil {
return []byte{}, sdkerrors.Wrap(err, "invalid packet data or codec")
return nil, sdkerrors.Wrap(err, "serialization of transaction data failed")
}

channelCap, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
Expand Down Expand Up @@ -84,7 +95,9 @@ func (k Keeper) createOutgoingPacket(
return k.ComputeVirtualTxHash(packetData.Data, packet.Sequence), k.channelKeeper.SendPacket(ctx, channelCap, packet)
}

func (k Keeper) DeserializeTx(_ sdk.Context, txBytes []byte) ([]sdk.Msg, error) {
// DeserializeCosmosTx will unmarshal and unpack a slice of transaction bytes
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
// into a slice of sdk.Msg's.
func (k Keeper) DeserializeCosmosTx(_ sdk.Context, txBytes []byte) ([]sdk.Msg, error) {
var txRaw types.IBCTxRaw

err := k.cdc.Unmarshal(txBytes, &txRaw)
Expand Down Expand Up @@ -197,7 +210,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet) error

switch data.Type {
case types.EXECUTE_TX:
msgs, err := k.DeserializeTx(ctx, data.Data)
msgs, err := k.DeserializeCosmosTx(ctx, data.Data)
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions modules/apps/27-interchain-accounts/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
"success", func() {
amount, _ := sdk.ParseCoinsNormalized("100stake")
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
msg = []sdk.Msg{&banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}}
}, true,
},
{
"success with []sdk.Message", func() {
"success with multiple sdk.Msg", func() {
amount, _ := sdk.ParseCoinsNormalized("100stake")
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
msg1 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
Expand All @@ -46,6 +46,13 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
msg = []byte{}
}, false,
},
{
"incorrect outgoing data - []sdk.Msg is not used", func() {
amount, _ := sdk.ParseCoinsNormalized("100stake")
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
}, false,
},
{
"active channel not found", func() {
amount, _ := sdk.ParseCoinsNormalized("100stake")
Expand Down Expand Up @@ -121,7 +128,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
// build packet data
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, []sdk.Msg{msg})
suite.Require().NoError(err)

data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
Expand Down Expand Up @@ -177,7 +184,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
// Incorrect FromAddress
msg = &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
// build packet data
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, []sdk.Msg{msg})
suite.Require().NoError(err)
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
Data: txBytes}
Expand Down