diff --git a/docs/architecture/adr-015-ibc-packet-receiver.md b/docs/architecture/adr-015-ibc-packet-receiver.md index 2ea26dcb5bae..58d8ea754811 100644 --- a/docs/architecture/adr-015-ibc-packet-receiver.md +++ b/docs/architecture/adr-015-ibc-packet-receiver.md @@ -192,7 +192,9 @@ func NewAnteHandler( } ``` -The implementation of this ADR will also change the `Data` field of the `Packet` type from `[]byte` (i.e. arbitrary data) to `PacketDataI`. We want to make application modules be able to register custom packet data type which is automatically unmarshaled at `TxDecoder` time and can be simply type switched inside the application handler. Also, by having `GetCommitment()` method instead of manually generate the commitment inside the IBC keeper, the applications can define their own commitment method, including bare bytes, hashing, etc. +The implementation of this ADR will also create a `RawData` field of the `Packet` of type `[]byte`, which can be deserialised by the receiving module into the `Data` field of type `PacketDataI`. It is up to the application modules to do this according to their own interpretation, not by the IBC keeper. This is crucial for dynamic IBC. + +By having the `GetCommitment()` method, the applications can define their own commitment method, including bare bytes, hashing, etc. This also removes the `Timeout` field from the `Packet` struct. This is because the `PacketDataI` interface now contains this information. You can see details about this in [ICS04](https://github.com/cosmos/ics/tree/master/spec/ics-004-channel-and-packet-semantics#definitions). @@ -234,9 +236,12 @@ func NewHandler(k Keeper) Handler { case MsgTransfer: return handleMsgTransfer(ctx, k, msg) case ibc.MsgPacket: - switch data := msg.Packet.Data.(type) { + msg.Data = k.MustUnmarshalPacketData(ctx, msg.RawData) + switch data := msg.Data.(type) { case PacketDataTransfer: // i.e fulfills the PacketDataI interface - return handlePacketDataTransfer(ctx, k, msg.Packet, data) + return handlePacketDataTransfer(ctx, k, msg, data) + default: + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized ICS-20 transfer packet data type: %T", data) } case ibc.MsgTimeoutPacket: switch packet := msg.Packet.Data.(type) { diff --git a/x/ibc/04-channel/client/utils/utils.go b/x/ibc/04-channel/client/utils/utils.go index 8cdcd3eb8eb1..b9fbd9175ba1 100644 --- a/x/ibc/04-channel/client/utils/utils.go +++ b/x/ibc/04-channel/client/utils/utils.go @@ -4,7 +4,6 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/client/context" - "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported" "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types" ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types" ) @@ -33,23 +32,14 @@ func QueryPacket( destPortID := channelRes.Channel.Counterparty.PortID destChannelID := channelRes.Channel.Counterparty.ChannelID - var data exported.PacketDataI - // TODO: commitment data is stored, not the data - // but we are unmarshalling the commitment in a json format - // because the current ICS 20 implementation uses json commitment form - // need to be changed to use external source of packet(e.g. event logs) - err = ctx.Codec.UnmarshalJSON(res.Value, &data) - if err != nil { - return types.PacketResponse{}, err - } - packet := types.NewPacket( - data, + res.Value, sequence, portID, channelID, destPortID, destChannelID, + timeout, ) // FIXME: res.Height+1 is hack, fix later diff --git a/x/ibc/04-channel/exported/exported.go b/x/ibc/04-channel/exported/exported.go index ba9dc7efdf6b..850b13d7dc3e 100644 --- a/x/ibc/04-channel/exported/exported.go +++ b/x/ibc/04-channel/exported/exported.go @@ -31,26 +31,10 @@ type PacketI interface { GetSourceChannel() string GetDestPort() string GetDestChannel() string - GetData() PacketDataI + GetData() []byte ValidateBasic() error } -// PacketDataI defines the packet data interface for IBC packets -// IBC application modules should define which data they want to -// send and receive over IBC channels. -type PacketDataI interface { - GetBytes() []byte // GetBytes returns the serialised packet data (without timeout) - GetTimeoutHeight() uint64 // GetTimeoutHeight returns the timeout height defined specifically for each packet data type instance - - ValidateBasic() error // ValidateBasic validates basic properties of the packet data, implements sdk.Msg - Type() string // Type returns human readable identifier, implements sdk.Msg -} - -// PacketAcknowledgementI defines the interface for IBC packet acknowledgements. -type PacketAcknowledgementI interface { - GetBytes() []byte -} - // Order defines if a channel is ORDERED or UNORDERED type Order byte diff --git a/x/ibc/04-channel/exported/opaque.go b/x/ibc/04-channel/exported/opaque.go new file mode 100644 index 000000000000..445678ed55b0 --- /dev/null +++ b/x/ibc/04-channel/exported/opaque.go @@ -0,0 +1,19 @@ +package exported + +// OpaquePacketData is useful as a struct for when a Keeper cannot +// unmarshal an IBC packet. +type OpaquePacketData struct { + data []byte +} + +// NewOpaquePacketData creates a wrapper for rawData. +func NewOpaquePacketData(data []byte) OpaquePacketData { + return OpaquePacketData{ + data: data, + } +} + +// GetData is a helper for serialising +func (opd OpaquePacketData) GetData() []byte { + return opd.data +} diff --git a/x/ibc/04-channel/keeper/packet.go b/x/ibc/04-channel/keeper/packet.go index 4cca23525f32..e2214fcd4385 100644 --- a/x/ibc/04-channel/keeper/packet.go +++ b/x/ibc/04-channel/keeper/packet.go @@ -107,8 +107,8 @@ func (k Keeper) SendPacket( ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeSendPacket, - sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData().GetBytes())), - sdk.NewAttribute(types.AttributeKeyTimeout, fmt.Sprintf("%d", packet.GetData().GetTimeoutHeight())), + sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())), + sdk.NewAttribute(types.AttributeKeyTimeout, fmt.Sprintf("%d", packet.GetTimeoutHeight())), sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), @@ -193,7 +193,7 @@ func (k Keeper) RecvPacket( func (k Keeper) PacketExecuted( ctx sdk.Context, packet exported.PacketI, - acknowledgement exported.PacketAcknowledgementI, + acknowledgement []byte, ) error { channel, found := k.GetChannel(ctx, packet.GetDestPort(), packet.GetDestChannel()) if !found { @@ -246,7 +246,7 @@ func (k Keeper) PacketExecuted( func (k Keeper) AcknowledgePacket( ctx sdk.Context, packet exported.PacketI, - acknowledgement exported.PacketAcknowledgementI, + acknowledgement []byte, proof commitmentexported.Proof, proofHeight uint64, ) (exported.PacketI, error) { @@ -301,7 +301,7 @@ func (k Keeper) AcknowledgePacket( if err := k.connectionKeeper.VerifyPacketAcknowledgement( ctx, connectionEnd, proofHeight, proof, packet.GetDestPort(), packet.GetDestChannel(), - packet.GetSequence(), acknowledgement.GetBytes(), + packet.GetSequence(), acknowledgement, ); err != nil { return nil, sdkerrors.Wrap(err, "invalid acknowledgement on counterparty chain") } diff --git a/x/ibc/04-channel/keeper/packet_test.go b/x/ibc/04-channel/keeper/packet_test.go index ca5239ca5a09..f9f85e1fc753 100644 --- a/x/ibc/04-channel/keeper/packet_test.go +++ b/x/ibc/04-channel/keeper/packet_test.go @@ -17,59 +17,59 @@ func (suite *KeeperTestSuite) TestSendPacket() { testCases := []testCase{ {"success", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainB.App.IBCKeeper.ChannelKeeper.SetNextSequenceSend(suite.chainB.GetContext(), testPort1, testChannel1, 1) }, true}, {"packet basic validation failed", func() { - packet = types.NewPacket(mockFailPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockFailPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) }, false}, {"channel not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) }, false}, {"channel closed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.CLOSED, exported.ORDERED, testConnectionIDA) }, false}, {"packet dest port ≠ channel counterparty port", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, testPort3, counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, testPort3, counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet dest channel ID ≠ channel counterparty channel ID", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), testChannel3) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), testChannel3, 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection is UNINITIALIZED", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.UNINITIALIZED) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"client state not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"timeout height passed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) commitNBlocks(suite.chainB, 10) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"next sequence send not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"next sequence wrong", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) @@ -110,40 +110,40 @@ func (suite *KeeperTestSuite) TestRecvPacket() { suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDB) suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceSend(suite.chainA.GetContext(), testPort2, testChannel2, 1) - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort2, testChannel2, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort2, testChannel2, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), testPort2, testChannel2, 1, types.CommitPacket(packet.(types.Packet).Data)) }, true}, {"channel not found", func() {}, false}, {"channel not open", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.INIT, exported.ORDERED, testConnectionIDA) }, false}, {"packet source port ≠ channel counterparty port", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort2, testChannel2, testPort3, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet source channel ID ≠ channel counterparty channel ID", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort2, testChannel2, testPort1, testChannel3, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not OPEN", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.INIT) suite.chainB.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"timeout passed", func() { commitNBlocks(suite.chainB, 10) - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"validation failed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, @@ -179,26 +179,26 @@ func (suite *KeeperTestSuite) TestPacketExecuted() { testCases := []testCase{ {"success: UNORDERED", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.UNORDERED, testConnectionIDA) suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), testPort2, testChannel2, 1) }, true}, {"success: ORDERED", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), testPort2, testChannel2, 1) }, true}, {"channel not found", func() {}, false}, {"channel not OPEN", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.CLOSED, exported.ORDERED, testConnectionIDA) }, false}, {"next sequence receive not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet sequence ≠ next sequence receive", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), testPort2, testChannel2, 5) }, false}, @@ -210,7 +210,7 @@ func (suite *KeeperTestSuite) TestPacketExecuted() { suite.SetupTest() // reset tc.malleate() - err := suite.chainA.App.IBCKeeper.ChannelKeeper.PacketExecuted(suite.chainA.GetContext(), packet, mockSuccessPacket{}) + err := suite.chainA.App.IBCKeeper.ChannelKeeper.PacketExecuted(suite.chainA.GetContext(), packet, mockSuccessPacket{}.GetBytes()) if tc.expPass { suite.Require().NoError(err) @@ -226,11 +226,11 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { var packet types.Packet packetKey := ibctypes.KeyPacketAcknowledgement(testPort2, testChannel2, 1) - ack := transfertypes.AckDataTransfer{} + ack := transfertypes.AckDataTransfer{}.GetBytes() testCases := []testCase{ {"success", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainA.CreateClient(suite.chainB) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) @@ -238,37 +238,37 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainA.createChannel(testPort2, testChannel2, testPort1, testChannel1, exported.OPEN, exported.ORDERED, testConnectionIDB) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), testPort1, testChannel1, 1, types.CommitPacket(packet.Data)) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), testPort2, testChannel2, 1, ack.GetBytes()) + suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), testPort2, testChannel2, 1, ack) }, true}, {"channel not found", func() {}, false}, {"channel not open", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.CLOSED, exported.ORDERED, testConnectionIDA) }, false}, {"packet source port ≠ channel counterparty port", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort3, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet source channel ID ≠ channel counterparty channel ID", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel3, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not OPEN", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.INIT) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet hasn't been sent", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet ack verification failed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), testPort1, testChannel1, 1, types.CommitPacket(packet.Data)) @@ -312,7 +312,7 @@ func (suite *KeeperTestSuite) TestCleanupPacket() { testCases := []testCase{ {"success", func() { nextSeqRecv = 10 - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainA.CreateClient(suite.chainB) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) @@ -324,47 +324,47 @@ func (suite *KeeperTestSuite) TestCleanupPacket() { }, true}, {"channel not found", func() {}, false}, {"channel not open", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.CLOSED, exported.ORDERED, testConnectionIDA) }, false}, {"packet source port ≠ channel counterparty port", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort3, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet source channel ID ≠ channel counterparty channel ID", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel3, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not OPEN", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.INIT) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet already received ", func() { - packet = types.NewPacket(mockSuccessPacket{}, 10, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 10, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet hasn't been sent", func() { nextSeqRecv = 10 - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"next seq receive verification failed", func() { nextSeqRecv = 10 - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), testPort1, testChannel1, 1, types.CommitPacket(packet.Data)) }, false}, {"packet ack verification failed", func() { nextSeqRecv = 10 - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.UNORDERED, testConnectionIDA) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), testPort1, testChannel1, 1, types.CommitPacket(packet.Data)) @@ -398,28 +398,10 @@ func (suite *KeeperTestSuite) TestCleanupPacket() { type mockSuccessPacket struct{} -// GetBytes returns the serialised packet data (without timeout) +// GetBytes returns the serialised packet data func (mp mockSuccessPacket) GetBytes() []byte { return []byte("THIS IS A SUCCESS PACKET") } -// GetTimeoutHeight returns the timeout height defined specifically for each packet data type instance -func (mp mockSuccessPacket) GetTimeoutHeight() uint64 { return 10 } - -// ValidateBasic validates basic properties of the packet data, implements sdk.Msg -func (mp mockSuccessPacket) ValidateBasic() error { return nil } - -// Type returns human readable identifier, implements sdk.Msg -func (mp mockSuccessPacket) Type() string { return "mock/packet/success" } - type mockFailPacket struct{} // GetBytes returns the serialised packet data (without timeout) func (mp mockFailPacket) GetBytes() []byte { return []byte("THIS IS A FAILURE PACKET") } - -// GetTimeoutHeight returns the timeout height defined specifically for each packet data type instance -func (mp mockFailPacket) GetTimeoutHeight() uint64 { return 10000 } - -// ValidateBasic validates basic properties of the packet data, implements sdk.Msg -func (mp mockFailPacket) ValidateBasic() error { return fmt.Errorf("Packet failed validate basic") } - -// Type returns human readable identifier, implements sdk.Msg -func (mp mockFailPacket) Type() string { return "mock/packet/failure" } diff --git a/x/ibc/04-channel/keeper/timeout_test.go b/x/ibc/04-channel/keeper/timeout_test.go index 5b406e2bdb74..81d7d5be697b 100644 --- a/x/ibc/04-channel/keeper/timeout_test.go +++ b/x/ibc/04-channel/keeper/timeout_test.go @@ -20,7 +20,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { testCases := []testCase{ {"success", func() { nextSeqRecv = 1 - packet = types.NewPacket(newMockTimeoutPacket(1), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(newMockTimeoutPacket().GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 1) suite.chainB.CreateClient(suite.chainA) suite.chainA.CreateClient(suite.chainB) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) @@ -31,48 +31,48 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { }, true}, {"channel not found", func() {}, false}, {"channel not open", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.CLOSED, exported.ORDERED, testConnectionIDA) }, false}, {"packet source port ≠ channel counterparty port", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort3, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet source channel ID ≠ channel counterparty channel ID", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel3, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"timeout", func() { - packet = types.NewPacket(mockSuccessPacket{}, 10, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 10, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet already received ", func() { nextSeqRecv = 2 - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet hasn't been sent", func() { nextSeqRecv = 1 - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"next seq receive verification failed", func() { nextSeqRecv = 1 - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), testPort1, testChannel1, 2, types.CommitPacket(packet.Data)) }, false}, {"packet ack verification failed", func() { nextSeqRecv = 1 - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.UNORDERED, testConnectionIDA) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), testPort1, testChannel1, 2, types.CommitPacket(packet.Data)) @@ -108,7 +108,7 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() { testCases := []testCase{ {"success ORDERED", func() { - packet = types.NewPacket(newMockTimeoutPacket(3), 1, testPort1, testChannel1, testPort2, testChannel2) + packet = types.NewPacket(newMockTimeoutPacket().GetBytes(), 1, testPort1, testChannel1, testPort2, testChannel2, 3) suite.chainA.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, true}, {"channel not found", func() {}, false}, @@ -142,7 +142,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { testCases := []testCase{ {"success", func() { - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainA.CreateClient(suite.chainB) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) @@ -154,24 +154,24 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { }, true}, {"channel not found", func() {}, false}, {"packet dest port ≠ channel counterparty port", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort3, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet dest channel ID ≠ channel counterparty channel ID", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel3, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"connection not found", func() { - packet = types.NewPacket(mockSuccessPacket{}, 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 1, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"packet hasn't been sent", func() { - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) }, false}, {"channel verification failed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.UNORDERED, testConnectionIDA) @@ -179,7 +179,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { suite.chainB.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainB.GetContext(), testPort1, testChannel1, nextSeqRecv) }, false}, {"next seq receive verification failed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.ORDERED, testConnectionIDA) @@ -187,7 +187,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { suite.chainB.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainB.GetContext(), testPort1, testChannel1, nextSeqRecv) }, false}, {"packet ack verification failed", func() { - packet = types.NewPacket(mockSuccessPacket{}, 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID()) + packet = types.NewPacket(mockSuccessPacket{}.GetBytes(), 2, testPort1, testChannel1, counterparty.GetPortID(), counterparty.GetChannelID(), 100) suite.chainB.CreateClient(suite.chainA) suite.chainB.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, connectionexported.OPEN) suite.chainB.createChannel(testPort1, testChannel1, testPort2, testChannel2, exported.OPEN, exported.UNORDERED, testConnectionIDA) @@ -226,18 +226,9 @@ type mockTimeoutPacket struct { timeoutHeight uint64 } -func newMockTimeoutPacket(timeoutHeight uint64) mockTimeoutPacket { - return mockTimeoutPacket{timeoutHeight} +func newMockTimeoutPacket() mockTimeoutPacket { + return mockTimeoutPacket{} } // GetBytes returns the serialised packet data (without timeout) func (mp mockTimeoutPacket) GetBytes() []byte { return []byte("THIS IS A TIMEOUT PACKET") } - -// GetTimeoutHeight returns the timeout height defined specifically for each packet data type instance -func (mp mockTimeoutPacket) GetTimeoutHeight() uint64 { return mp.timeoutHeight } - -// ValidateBasic validates basic properties of the packet data, implements sdk.Msg -func (mp mockTimeoutPacket) ValidateBasic() error { return nil } - -// Type returns human readable identifier, implements sdk.Msg -func (mp mockTimeoutPacket) Type() string { return "mock/packet/success" } diff --git a/x/ibc/04-channel/types/codec.go b/x/ibc/04-channel/types/codec.go index cfb57624b7ba..270852973248 100644 --- a/x/ibc/04-channel/types/codec.go +++ b/x/ibc/04-channel/types/codec.go @@ -21,8 +21,6 @@ func init() { // IBC channel. func RegisterCodec(cdc *codec.Codec) { cdc.RegisterInterface((*exported.PacketI)(nil), nil) - cdc.RegisterInterface((*exported.PacketDataI)(nil), nil) - cdc.RegisterInterface((*exported.PacketAcknowledgementI)(nil), nil) cdc.RegisterConcrete(Channel{}, "ibc/channel/Channel", nil) cdc.RegisterConcrete(Packet{}, "ibc/channel/Packet", nil) diff --git a/x/ibc/04-channel/types/msgs.go b/x/ibc/04-channel/types/msgs.go index 6797e942f57b..d0ac1c0310dd 100644 --- a/x/ibc/04-channel/types/msgs.go +++ b/x/ibc/04-channel/types/msgs.go @@ -1,6 +1,7 @@ package types import ( + "encoding/base64" "strings" sdk "github.com/cosmos/cosmos-sdk/types" @@ -440,11 +441,22 @@ func (msg MsgPacket) GetSignBytes() []byte { return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(msg)) } +// GetDataSignBytes returns the bytes used for the data field when signing the packet. +func (msg MsgPacket) GetDataSignBytes() []byte { + s := "\"" + base64.StdEncoding.EncodeToString(msg.Data) + "\"" + return []byte(s) +} + // GetSigners implements sdk.Msg func (msg MsgPacket) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Signer} } +// Type implements sdk.Msg +func (msg MsgPacket) Type() string { + return "ics04/opaque" +} + var _ sdk.Msg = MsgTimeout{} // MsgTimeout receives timed-out packet @@ -500,19 +512,24 @@ func (msg MsgTimeout) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Signer} } +// Type implements sdk.Msg +func (msg MsgTimeout) Type() string { + return "ics04/timeout" +} + var _ sdk.Msg = MsgAcknowledgement{} // MsgAcknowledgement receives incoming IBC acknowledgement type MsgAcknowledgement struct { Packet `json:"packet" yaml:"packet"` - Acknowledgement exported.PacketAcknowledgementI `json:"acknowledgement" yaml:"acknowledgement"` - Proof commitmentexported.Proof `json:"proof" yaml:"proof"` - ProofHeight uint64 `json:"proof_height" yaml:"proof_height"` - Signer sdk.AccAddress `json:"signer" yaml:"signer"` + Acknowledgement []byte `json:"acknowledgement" yaml:"acknowledgement"` + Proof commitmentexported.Proof `json:"proof" yaml:"proof"` + ProofHeight uint64 `json:"proof_height" yaml:"proof_height"` + Signer sdk.AccAddress `json:"signer" yaml:"signer"` } // NewMsgAcknowledgement constructs a new MsgAcknowledgement -func NewMsgAcknowledgement(packet Packet, ack exported.PacketAcknowledgementI, proof commitmentexported.Proof, proofHeight uint64, signer sdk.AccAddress) MsgAcknowledgement { +func NewMsgAcknowledgement(packet Packet, ack []byte, proof commitmentexported.Proof, proofHeight uint64, signer sdk.AccAddress) MsgAcknowledgement { return MsgAcknowledgement{ Packet: packet, Acknowledgement: ack, @@ -535,7 +552,7 @@ func (msg MsgAcknowledgement) ValidateBasic() error { if err := msg.Proof.ValidateBasic(); err != nil { return sdkerrors.Wrap(err, "proof ack cannot be nil") } - if len(msg.Acknowledgement.GetBytes()) > 100 { + if len(msg.Acknowledgement) > 100 { return sdkerrors.Wrap(ErrAcknowledgementTooLong, "acknowledgement cannot exceed 100 bytes") } if msg.ProofHeight == 0 { @@ -557,3 +574,8 @@ func (msg MsgAcknowledgement) GetSignBytes() []byte { func (msg MsgAcknowledgement) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Signer} } + +// Type implements sdk.Msg +func (msg MsgAcknowledgement) Type() string { + return "ics04/opaque" +} diff --git a/x/ibc/04-channel/types/msgs_test.go b/x/ibc/04-channel/types/msgs_test.go index bea0e43995be..aeb47f4ad640 100644 --- a/x/ibc/04-channel/types/msgs_test.go +++ b/x/ibc/04-channel/types/msgs_test.go @@ -1,7 +1,6 @@ package types import ( - "errors" "fmt" "testing" @@ -353,59 +352,16 @@ func (suite *MsgTestSuite) TestMsgChannelCloseConfirm() { } } -var _ exported.PacketDataI = validPacketT{} - -type validPacketT struct{} - -func (validPacketT) GetBytes() []byte { - return []byte("testdata") -} - -func (validPacketT) GetTimeoutHeight() uint64 { - return 100 -} - -func (validPacketT) ValidateBasic() error { - return nil -} - -func (validPacketT) Type() string { - return "valid" -} - -var _ exported.PacketDataI = invalidPacketT{} - -type invalidPacketT struct{} - -func (invalidPacketT) GetBytes() []byte { - return []byte("testdata") -} - -func (invalidPacketT) GetTimeoutHeight() uint64 { - return 100 -} - -func (invalidPacketT) ValidateBasic() error { - return errors.New("invalid packet") -} - -func (invalidPacketT) Type() string { - return "invalid" -} - -var _ exported.PacketAcknowledgementI = invalidAckT{} - -type invalidAckT struct{} - -func (invalidAckT) GetBytes() []byte { - return []byte("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") -} - // define variables used for testing var ( - packet = NewPacket(validPacketT{}, 1, portid, chanid, cpportid, cpchanid) - invalidPacket = NewPacket(invalidPacketT{}, 0, portid, chanid, cpportid, cpchanid) - invalidAck = invalidAckT{} + timeout = uint64(100) + validPacketData = []byte("testdata") + unknownPacketData = []byte("unknown") + invalidAckData = []byte("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") + + packet = NewPacket(validPacketData, 1, portid, chanid, cpportid, cpchanid, 100) + unknownPacket = NewPacket(unknownPacketData, 0, portid, chanid, cpportid, cpchanid, 100) + invalidAck = invalidAckData emptyProof = commitmenttypes.MerkleProof{Proof: nil} invalidProofs1 = commitmentexported.Proof(nil) @@ -431,7 +387,7 @@ func TestMsgPacketRoute(t *testing.T) { func TestMsgPacketType(t *testing.T) { msg := NewMsgPacket(packet, proof, 1, addr1) - require.Equal(t, "valid", msg.Type()) + require.Equal(t, []byte("testdata"), msg.GetData()) } // TestMsgPacketValidation tests ValidateBasic for MsgPacket @@ -443,7 +399,7 @@ func TestMsgPacketValidation(t *testing.T) { NewMsgPacket(packet, invalidProofs1, 1, addr1), // missing proof NewMsgPacket(packet, invalidProofs2, 1, addr1), // proof contain empty proof NewMsgPacket(packet, proof, 1, emptyAddr), // missing signer address - NewMsgPacket(invalidPacket, proof, 1, addr1), // invalid packet + NewMsgPacket(unknownPacket, proof, 1, addr1), // unknown packet } testCases := []struct { @@ -473,10 +429,12 @@ func TestMsgPacketValidation(t *testing.T) { // TestMsgPacketGetSignBytes tests GetSignBytes for MsgPacket func TestMsgPacketGetSignBytes(t *testing.T) { msg := NewMsgPacket(packet, proof, 1, addr1) - SubModuleCdc.RegisterConcrete(validPacketT{}, "test/validPacketT", nil) res := msg.GetSignBytes() - expected := `{"type":"ibc/channel/MsgPacket","value":{"packet":{"data":{"type":"test/validPacketT","value":{}},"destination_channel":"testcpchannel","destination_port":"testcpport","sequence":"1","source_channel":"testchannel","source_port":"testportid"},"proof":{"type":"ibc/commitment/MerkleProof","value":{"proof":{"ops":[]}}},"proof_height":"1","signer":"cosmos1w3jhxarpv3j8yvg4ufs4x"}}` + expected := fmt.Sprintf( + `{"type":"ibc/channel/MsgPacket","value":{"packet":{"data":%s,"destination_channel":"testcpchannel","destination_port":"testcpport","sequence":"1","source_channel":"testchannel","source_port":"testportid","timeout_height":"100"},"proof":{"type":"ibc/commitment/MerkleProof","value":{"proof":{"ops":[]}}},"proof_height":"1","signer":"cosmos1w3jhxarpv3j8yvg4ufs4x"}}`, + string(msg.GetDataSignBytes()), + ) require.Equal(t, expected, string(res)) } @@ -496,7 +454,7 @@ func (suite *MsgTestSuite) TestMsgTimeout() { NewMsgTimeout(packet, 0, proof, 0, addr), NewMsgTimeout(packet, 0, proof, 1, emptyAddr), NewMsgTimeout(packet, 0, emptyProof, 1, addr), - NewMsgTimeout(invalidPacket, 0, proof, 1, addr), + NewMsgTimeout(unknownPacket, 0, proof, 1, addr), NewMsgTimeout(packet, 0, invalidProofs1, 1, addr), } @@ -530,7 +488,7 @@ func (suite *MsgTestSuite) TestMsgAcknowledgement() { NewMsgAcknowledgement(packet, packet.GetData(), proof, 0, addr), NewMsgAcknowledgement(packet, packet.GetData(), proof, 1, emptyAddr), NewMsgAcknowledgement(packet, packet.GetData(), emptyProof, 1, addr), - NewMsgAcknowledgement(invalidPacket, packet.GetData(), proof, 1, addr), + NewMsgAcknowledgement(unknownPacket, packet.GetData(), proof, 1, addr), NewMsgAcknowledgement(packet, invalidAck, proof, 1, addr), NewMsgAcknowledgement(packet, packet.GetData(), invalidProofs1, 1, addr), } diff --git a/x/ibc/04-channel/types/packet.go b/x/ibc/04-channel/types/packet.go index cd5d1e271c5f..30554fbc6522 100644 --- a/x/ibc/04-channel/types/packet.go +++ b/x/ibc/04-channel/types/packet.go @@ -1,8 +1,6 @@ package types import ( - "encoding/binary" - "github.com/tendermint/tendermint/crypto/tmhash" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -10,40 +8,38 @@ import ( host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" ) -// CommitPacket appends bigendian encoded timeout height and commitment bytes -// and then returns the hash of the result bytes. +// CommitPacket return the hash of commitment bytes // TODO: no specification for packet commitment currently, // make it spec compatible once we have it -func CommitPacket(data exported.PacketDataI) []byte { - buf := make([]byte, 8) - binary.BigEndian.PutUint64(buf, data.GetTimeoutHeight()) - buf = append(buf, data.GetBytes()...) - return tmhash.Sum(buf) +func CommitPacket(data []byte) []byte { + return tmhash.Sum(data) } // CommitAcknowledgement returns the hash of commitment bytes -func CommitAcknowledgement(data exported.PacketAcknowledgementI) []byte { - return tmhash.Sum(data.GetBytes()) +func CommitAcknowledgement(data []byte) []byte { + return tmhash.Sum(data) } var _ exported.PacketI = Packet{} // Packet defines a type that carries data across different chains through IBC type Packet struct { - Data exported.PacketDataI `json:"data" yaml:"data"` // opaque value which can be defined by the application logic of the associated modules. + Data []byte `json:"data" yaml:"data"` // Actual opaque bytes transferred directly to the application module Sequence uint64 `json:"sequence" yaml:"sequence"` // number corresponds to the order of sends and receives, where a Packet with an earlier sequence number must be sent and received before a Packet with a later sequence number. SourcePort string `json:"source_port" yaml:"source_port"` // identifies the port on the sending chain. SourceChannel string `json:"source_channel" yaml:"source_channel"` // identifies the channel end on the sending chain. DestinationPort string `json:"destination_port" yaml:"destination_port"` // identifies the port on the receiving chain. DestinationChannel string `json:"destination_channel" yaml:"destination_channel"` // identifies the channel end on the receiving chain. + TimeoutHeight uint64 `json:"timeout_height" yaml:"timeout_height"` // block height after which the packet times out } // NewPacket creates a new Packet instance func NewPacket( - data exported.PacketDataI, + data []byte, sequence uint64, sourcePort, sourceChannel, destinationPort, destinationChannel string, + timeoutHeight uint64, ) Packet { return Packet{ Data: data, @@ -52,14 +48,10 @@ func NewPacket( SourceChannel: sourceChannel, DestinationPort: destinationPort, DestinationChannel: destinationChannel, + TimeoutHeight: timeoutHeight, } } -// Type exports Packet.Data.Type -func (p Packet) Type() string { - return p.Data.Type() -} - // GetSequence implements PacketI interface func (p Packet) GetSequence() uint64 { return p.Sequence } @@ -76,10 +68,10 @@ func (p Packet) GetDestPort() string { return p.DestinationPort } func (p Packet) GetDestChannel() string { return p.DestinationChannel } // GetData implements PacketI interface -func (p Packet) GetData() exported.PacketDataI { return p.Data } +func (p Packet) GetData() []byte { return p.Data } // GetTimeoutHeight implements PacketI interface -func (p Packet) GetTimeoutHeight() uint64 { return p.Data.GetTimeoutHeight() } +func (p Packet) GetTimeoutHeight() uint64 { return p.TimeoutHeight } // ValidateBasic implements PacketI interface func (p Packet) ValidateBasic() error { @@ -110,11 +102,11 @@ func (p Packet) ValidateBasic() error { if p.Sequence == 0 { return sdkerrors.Wrap(ErrInvalidPacket, "packet sequence cannot be 0") } - if p.Data.GetTimeoutHeight() == 0 { + if p.TimeoutHeight == 0 { return sdkerrors.Wrap(ErrInvalidPacket, "packet timeout cannot be 0") } - if len(p.Data.GetBytes()) == 0 { + if len(p.Data) == 0 { return sdkerrors.Wrap(ErrInvalidPacket, "packet data bytes cannot be empty") } - return p.Data.ValidateBasic() + return nil } diff --git a/x/ibc/04-channel/types/packet_test.go b/x/ibc/04-channel/types/packet_test.go index 9137bd7380e4..3efc7158db87 100644 --- a/x/ibc/04-channel/types/packet_test.go +++ b/x/ibc/04-channel/types/packet_test.go @@ -12,13 +12,13 @@ func TestPacketValidateBasic(t *testing.T) { expPass bool errMsg string }{ - {NewPacket(validPacketT{}, 1, portid, chanid, cpportid, cpchanid), true, ""}, - {NewPacket(validPacketT{}, 0, portid, chanid, cpportid, cpchanid), false, "invalid sequence"}, - {NewPacket(validPacketT{}, 1, invalidPort, chanid, cpportid, cpchanid), false, "invalid source port"}, - {NewPacket(validPacketT{}, 1, portid, invalidChannel, cpportid, cpchanid), false, "invalid source channel"}, - {NewPacket(validPacketT{}, 1, portid, chanid, invalidPort, cpchanid), false, "invalid destination port"}, - {NewPacket(validPacketT{}, 1, portid, chanid, cpportid, invalidChannel), false, "invalid destination channel"}, - {NewPacket(invalidPacketT{}, 1, portid, chanid, cpportid, cpchanid), false, "invalid packet data"}, + {NewPacket(validPacketData, 1, portid, chanid, cpportid, cpchanid, timeout), true, ""}, + {NewPacket(validPacketData, 0, portid, chanid, cpportid, cpchanid, timeout), false, "invalid sequence"}, + {NewPacket(validPacketData, 1, invalidPort, chanid, cpportid, cpchanid, timeout), false, "invalid source port"}, + {NewPacket(validPacketData, 1, portid, invalidChannel, cpportid, cpchanid, timeout), false, "invalid source channel"}, + {NewPacket(validPacketData, 1, portid, chanid, invalidPort, cpchanid, timeout), false, "invalid destination port"}, + {NewPacket(validPacketData, 1, portid, chanid, cpportid, invalidChannel, timeout), false, "invalid destination channel"}, + {NewPacket(unknownPacketData, 1, portid, chanid, cpportid, cpchanid, timeout), true, ""}, } for i, tc := range testCases { diff --git a/x/ibc/20-transfer/handler.go b/x/ibc/20-transfer/handler.go index 08dd6a5927d0..fa6f9d9008d3 100644 --- a/x/ibc/20-transfer/handler.go +++ b/x/ibc/20-transfer/handler.go @@ -14,14 +14,16 @@ func NewHandler(k Keeper) sdk.Handler { case MsgTransfer: return handleMsgTransfer(ctx, k, msg) case channeltypes.MsgPacket: - switch data := msg.Data.(type) { + pd := k.UnmarshalPacketData(ctx, msg.GetData()) + switch data := pd.(type) { case FungibleTokenPacketData: return handlePacketDataTransfer(ctx, k, msg, data) default: return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized ICS-20 transfer packet data type: %T", data) } case channeltypes.MsgTimeout: - switch data := msg.Data.(type) { + pd := k.UnmarshalPacketData(ctx, msg.GetData()) + switch data := pd.(type) { case FungibleTokenPacketData: return handleTimeoutDataTransfer(ctx, k, msg, data) default: @@ -74,7 +76,7 @@ func handlePacketDataTransfer( } acknowledgement := AckDataTransfer{} - if err := k.PacketExecuted(ctx, msg.Packet, acknowledgement); err != nil { + if err := k.PacketExecuted(ctx, msg.Packet, acknowledgement.GetBytes()); err != nil { return nil, err } diff --git a/x/ibc/20-transfer/keeper/keeper.go b/x/ibc/20-transfer/keeper/keeper.go index e5225cbe7137..484539c9ac95 100644 --- a/x/ibc/20-transfer/keeper/keeper.go +++ b/x/ibc/20-transfer/keeper/keeper.go @@ -65,7 +65,7 @@ func (k Keeper) GetTransferAccount(ctx sdk.Context) supplyexported.ModuleAccount // PacketExecuted defines a wrapper function for the channel Keeper's function // in order to expose it to the ICS20 transfer handler. -func (k Keeper) PacketExecuted(ctx sdk.Context, packet channelexported.PacketI, acknowledgement channelexported.PacketAcknowledgementI) error { +func (k Keeper) PacketExecuted(ctx sdk.Context, packet channelexported.PacketI, acknowledgement []byte) error { return k.channelKeeper.PacketExecuted(ctx, packet, acknowledgement) } @@ -80,3 +80,16 @@ func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error { func (k Keeper) TimeoutExecuted(ctx sdk.Context, packet channelexported.PacketI) error { return k.channelKeeper.TimeoutExecuted(ctx, packet) } + +// UnmarshalPacketData tries to extract an application type from raw channel packet data. +func (k Keeper) UnmarshalPacketData(ctx sdk.Context, rawData []byte) interface{} { + var ftpd types.FungibleTokenPacketData + + err := k.cdc.UnmarshalBinaryBare(rawData, &ftpd) + if err == nil { + return ftpd + } + + // Cannot unmarshal, so wrap in an opaque object. + return channelexported.NewOpaquePacketData(rawData) +} diff --git a/x/ibc/20-transfer/keeper/relay.go b/x/ibc/20-transfer/keeper/relay.go index 529a30cf28a8..6f13b0d43189 100644 --- a/x/ibc/20-transfer/keeper/relay.go +++ b/x/ibc/20-transfer/keeper/relay.go @@ -126,16 +126,17 @@ func (k Keeper) createOutgoingPacket( } packetData := types.NewFungibleTokenPacketData( - amount, sender, receiver, isSourceChain, destHeight+DefaultPacketTimeout, + amount, sender, receiver, isSourceChain, ) packet := channel.NewPacket( - packetData, + packetData.GetBytes(), seq, sourcePort, sourceChannel, destinationPort, destinationChannel, + destHeight+DefaultPacketTimeout, ) return k.channelKeeper.SendPacket(ctx, packet) diff --git a/x/ibc/20-transfer/keeper/relay_test.go b/x/ibc/20-transfer/keeper/relay_test.go index 101ec8698ce9..7db510decfdf 100644 --- a/x/ibc/20-transfer/keeper/relay_test.go +++ b/x/ibc/20-transfer/keeper/relay_test.go @@ -95,7 +95,7 @@ func (suite *KeeperTestSuite) TestSendTransfer() { } func (suite *KeeperTestSuite) TestReceiveTransfer() { - data := types.NewFungibleTokenPacketData(prefixCoins2, testAddr1, testAddr2, true, 100) + data := types.NewFungibleTokenPacketData(prefixCoins2, testAddr1, testAddr2, true) testCases := []struct { msg string @@ -132,7 +132,7 @@ func (suite *KeeperTestSuite) TestReceiveTransfer() { }, true}, } - packet := channeltypes.NewPacket(data, 1, testPort1, testChannel1, testPort2, testChannel2) + packet := channeltypes.NewPacket(data.GetBytes(), 1, testPort1, testChannel1, testPort2, testChannel2, 100) for i, tc := range testCases { tc := tc @@ -152,7 +152,7 @@ func (suite *KeeperTestSuite) TestReceiveTransfer() { } func (suite *KeeperTestSuite) TestTimeoutTransfer() { - data := types.NewFungibleTokenPacketData(prefixCoins, testAddr1, testAddr2, true, 100) + data := types.NewFungibleTokenPacketData(prefixCoins, testAddr1, testAddr2, true) testCases := []struct { msg string @@ -186,7 +186,7 @@ func (suite *KeeperTestSuite) TestTimeoutTransfer() { }, false}, } - packet := channeltypes.NewPacket(data, 1, testPort1, testChannel1, testPort2, testChannel2) + packet := channeltypes.NewPacket(data.GetBytes(), 1, testPort1, testChannel1, testPort2, testChannel2, 100) for i, tc := range testCases { tc := tc diff --git a/x/ibc/20-transfer/types/expected_keepers.go b/x/ibc/20-transfer/types/expected_keepers.go index a347fb4e476f..9cf6c4dc8549 100644 --- a/x/ibc/20-transfer/types/expected_keepers.go +++ b/x/ibc/20-transfer/types/expected_keepers.go @@ -19,7 +19,7 @@ type ChannelKeeper interface { GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channel.Channel, found bool) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) SendPacket(ctx sdk.Context, packet channelexported.PacketI) error - PacketExecuted(ctx sdk.Context, packet channelexported.PacketI, acknowledgement channelexported.PacketAcknowledgementI) error + PacketExecuted(ctx sdk.Context, packet channelexported.PacketI, acknowledgement []byte) error ChanCloseInit(ctx sdk.Context, portID, channelID string) error TimeoutExecuted(ctx sdk.Context, packet channelexported.PacketI) error } diff --git a/x/ibc/20-transfer/types/packet.go b/x/ibc/20-transfer/types/packet.go index edee9f60ae82..acb50b4e4a23 100644 --- a/x/ibc/20-transfer/types/packet.go +++ b/x/ibc/20-transfer/types/packet.go @@ -5,12 +5,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - - channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported" ) -var _ channelexported.PacketDataI = FungibleTokenPacketData{} - // FungibleTokenPacketData defines a struct for the packet payload // See FungibleTokenPacketData spec: https://github.com/cosmos/ics/tree/master/spec/ics-020-fungible-token-transfer#data-structures type FungibleTokenPacketData struct { @@ -18,19 +14,17 @@ type FungibleTokenPacketData struct { Sender sdk.AccAddress `json:"sender" yaml:"sender"` // the sender address Receiver sdk.AccAddress `json:"receiver" yaml:"receiver"` // the recipient address on the destination chain Source bool `json:"source" yaml:"source"` // indicates if the sending chain is the source chain of the tokens to be transferred - Timeout uint64 `json:"timeout" yaml:"timeout"` } // NewFungibleTokenPacketData contructs a new FungibleTokenPacketData instance func NewFungibleTokenPacketData( amount sdk.Coins, sender, receiver sdk.AccAddress, - source bool, timeout uint64) FungibleTokenPacketData { + source bool) FungibleTokenPacketData { return FungibleTokenPacketData{ Amount: amount, Sender: sender, Receiver: receiver, Source: source, - Timeout: timeout, } } @@ -48,7 +42,7 @@ func (ftpd FungibleTokenPacketData) String() string { ) } -// ValidateBasic implements channelexported.PacketDataI +// ValidateBasic is used for validating the token transfer func (ftpd FungibleTokenPacketData) ValidateBasic() error { if !ftpd.Amount.IsAllPositive() { return sdkerrors.ErrInsufficientFunds @@ -62,34 +56,19 @@ func (ftpd FungibleTokenPacketData) ValidateBasic() error { if ftpd.Receiver.Empty() { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing receiver address") } - if ftpd.Timeout == 0 { - return sdkerrors.Wrap(ErrInvalidPacketTimeout, "timeout cannot be 0") - } return nil } -// GetBytes implements channelexported.PacketDataI +// GetBytes is a helper for serialising func (ftpd FungibleTokenPacketData) GetBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(ftpd)) } -// GetTimeoutHeight implements channelexported.PacketDataI -func (ftpd FungibleTokenPacketData) GetTimeoutHeight() uint64 { - return ftpd.Timeout -} - -// Type implements channelexported.PacketDataI -func (ftpd FungibleTokenPacketData) Type() string { - return "ics20/transfer" -} - -var _ channelexported.PacketAcknowledgementI = AckDataTransfer{} - // AckDataTransfer is a no-op packet // See spec for onAcknowledgePacket: https://github.com/cosmos/ics/tree/master/spec/ics-020-fungible-token-transfer#packet-relay type AckDataTransfer struct{} -// GetBytes implements channelexported.PacketAcknowledgementI -func (ack AckDataTransfer) GetBytes() []byte { +// GetBytes is a helper for serialising +func (AckDataTransfer) GetBytes() []byte { return []byte("fungible token transfer ack") } diff --git a/x/ibc/20-transfer/types/packet_test.go b/x/ibc/20-transfer/types/packet_test.go index fc188708e60f..2f82a33c779b 100644 --- a/x/ibc/20-transfer/types/packet_test.go +++ b/x/ibc/20-transfer/types/packet_test.go @@ -9,12 +9,11 @@ import ( // TestFungibleTokenPacketDataValidateBasic tests ValidateBasic for FungibleTokenPacketData func TestFungibleTokenPacketDataValidateBasic(t *testing.T) { testPacketDataTransfer := []FungibleTokenPacketData{ - NewFungibleTokenPacketData(coins, addr1, addr2, true, 100), // valid msg - NewFungibleTokenPacketData(invalidDenomCoins, addr1, addr2, true, 100), // invalid amount - NewFungibleTokenPacketData(negativeCoins, addr1, addr2, false, 100), // amount contains negative coin - NewFungibleTokenPacketData(coins, emptyAddr, addr2, false, 100), // missing sender address - NewFungibleTokenPacketData(coins, addr1, emptyAddr, false, 100), - NewFungibleTokenPacketData(coins, addr1, emptyAddr, false, 0), // missing recipient address + NewFungibleTokenPacketData(coins, addr1, addr2, true), // valid msg + NewFungibleTokenPacketData(invalidDenomCoins, addr1, addr2, true), // invalid amount + NewFungibleTokenPacketData(negativeCoins, addr1, addr2, false), // amount contains negative coin + NewFungibleTokenPacketData(coins, emptyAddr, addr2, false), // missing sender address + NewFungibleTokenPacketData(coins, addr1, emptyAddr, false), // missing recipient address } testCases := []struct { @@ -27,7 +26,6 @@ func TestFungibleTokenPacketDataValidateBasic(t *testing.T) { {testPacketDataTransfer[2], false, "amount contains negative coin"}, {testPacketDataTransfer[3], false, "missing sender address"}, {testPacketDataTransfer[4], false, "missing recipient address"}, - {testPacketDataTransfer[5], false, "timeout is 0"}, } for i, tc := range testCases { diff --git a/x/ibc/ante/ante_test.go b/x/ibc/ante/ante_test.go index f690daab8cb8..b38d11b260c0 100644 --- a/x/ibc/ante/ante_test.go +++ b/x/ibc/ante/ante_test.go @@ -95,7 +95,7 @@ func (suite *HandlerTestSuite) TestHandleMsgPacketOrdered() { suite.chainA.App.IBCKeeper.ChannelKeeper, )) - packet := channel.NewPacket(newPacket(12345), 1, portid, chanid, cpportid, cpchanid) + packet := channel.NewPacket(newPacket(12345).GetData(), 1, portid, chanid, cpportid, cpchanid, 100) ctx := suite.chainA.GetContext() cctx, _ := ctx.CacheContext() @@ -149,7 +149,7 @@ func (suite *HandlerTestSuite) TestHandleMsgPacketUnordered() { var packet channeltypes.Packet for i := 0; i < 5; i++ { - packet = channel.NewPacket(newPacket(uint64(i)), uint64(i), portid, chanid, cpportid, cpchanid) + packet = channel.NewPacket(newPacket(uint64(i)).GetData(), uint64(i), portid, chanid, cpportid, cpchanid, 100) suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), packet.SourcePort, packet.SourceChannel, uint64(i), channeltypes.CommitPacket(packet.Data)) } @@ -161,7 +161,7 @@ func (suite *HandlerTestSuite) TestHandleMsgPacketUnordered() { for i := 10; i >= 0; i-- { cctx, write := suite.chainA.GetContext().CacheContext() - packet = channel.NewPacket(newPacket(uint64(i)), uint64(i), portid, chanid, cpportid, cpchanid) + packet = channel.NewPacket(newPacket(uint64(i)).GetData(), uint64(i), portid, chanid, cpportid, cpchanid, 100) packetCommitmentPath := ibctypes.PacketCommitmentPath(packet.SourcePort, packet.SourceChannel, uint64(i)) proof, proofHeight := queryProof(suite.chainB, packetCommitmentPath) msg := channel.NewMsgPacket(packet, proof, uint64(proofHeight), addr1) @@ -356,28 +356,14 @@ func nextHeader(chain *TestChain) ibctmtypes.Header { chain.Header.Time.Add(time.Minute), chain.Vals, chain.Signers) } -var _ channelexported.PacketDataI = packetT{} - type packetT struct { Data uint64 } -func (packet packetT) GetBytes() []byte { +func (packet packetT) GetData() []byte { return []byte(fmt.Sprintf("%d", packet.Data)) } -func (packetT) GetTimeoutHeight() uint64 { - return 100 -} - -func (packetT) ValidateBasic() error { - return nil -} - -func (packetT) Type() string { - return "valid" -} - func newPacket(data uint64) packetT { return packetT{data} }