Skip to content

Commit

Permalink
test: adding tests for V1 and V2 packet flow
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton committed Sep 17, 2024
1 parent 45fbf6f commit 0c48c10
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
109 changes: 109 additions & 0 deletions modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3248,3 +3248,112 @@ func (suite *KeeperTestSuite) TestPruneAcknowledgements() {
})
}
}

// TestV2PacketFlow sets up clients and counterparties and sends a V2 packet.
// It ensures that a V2 ack structure is used and that the ack is correctly written to state.
func (suite *KeeperTestSuite) TestV2PacketFlow() {
suite.SetupTest()

path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupV2()

timeoutTimestamp := suite.chainA.GetTimeoutTimestamp()
sequence, err := path.EndpointA.SendPacketV2(clienttypes.ZeroHeight(), timeoutTimestamp, ibcmock.Version, ibctesting.MockPacketData)
suite.Require().NoError(err)

packet := channeltypes.NewPacketWithVersion(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ClientID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ClientID, clienttypes.ZeroHeight(), timeoutTimestamp, ibcmock.Version)

packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := path.EndpointA.QueryProof(packetKey)
suite.Require().NotNil(proof)
suite.Require().False(proofHeight.IsZero())

// RecvPacket
recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, suite.chainB.SenderAccount.GetAddress().String())
recvPacketResponse, err := path.EndpointB.Chain.SendMsgs(recvMsg)
suite.Require().NoError(path.EndpointA.UpdateClient())

suite.Require().NotNil(recvPacketResponse)
suite.Require().NoError(err)

// ensure that the ack that was written, is a multi ack with a single item that hat as a success status.
ack, found := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
suite.Require().True(found)
suite.Require().NotNil(ack)

expectedAck := channeltypes.MultiAcknowledgement{
AcknowledgementResults: []channeltypes.AcknowledgementResult{
{
AppName: path.EndpointB.ChannelConfig.PortID,
RecvPacketResult: channeltypes.RecvPacketResult{
Status: channeltypes.PacketStatus_Success,
Acknowledgement: ibctesting.MockAcknowledgement,
},
},
},
}

expectedBz := suite.chainB.Codec.MustMarshal(&expectedAck)
expectedCommittedBz := channeltypes.CommitAcknowledgement(expectedBz)
suite.Require().Equal(expectedCommittedBz, ack)

packetKey = host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
proof, proofHeight = path.EndpointB.QueryProof(packetKey)

legacyMultiAck := keeper.NewLegacyMultiAck(suite.chainA.Codec, ibcmock.MockAcknowledgement, path.EndpointB.ChannelConfig.PortID)

msgAck := channeltypes.NewMsgAcknowledgement(packet, legacyMultiAck.Acknowledgement(), proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String())

ackPacketResponse, err := path.EndpointA.Chain.SendMsgs(msgAck)
suite.Require().NoError(path.EndpointB.UpdateClient())

suite.Require().NoError(err)
suite.Require().NotNil(ackPacketResponse)
}

// TestV1PacketFlow sets up a channel and sends a V1 packet.
// It ensures that a V1 ack structure is used and that the ack is correctly written to state.
func (suite *KeeperTestSuite) TestV1PacketFlow() {
suite.SetupTest()

path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.Setup()

timeoutTimestamp := suite.chainA.GetTimeoutTimestamp()
sequence, err := path.EndpointA.SendPacket(clienttypes.ZeroHeight(), timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)

packet := channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.ZeroHeight(), timeoutTimestamp)

packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := path.EndpointA.QueryProof(packetKey)
suite.Require().NotNil(proof)
suite.Require().False(proofHeight.IsZero())

// RecvPacket
recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, suite.chainB.SenderAccount.GetAddress().String())
recvPacketResponse, err := path.EndpointB.Chain.SendMsgs(recvMsg)
suite.Require().NoError(path.EndpointA.UpdateClient())

suite.Require().NotNil(recvPacketResponse)
suite.Require().NoError(err)

//ensure that the ack that was written, is a multi ack with a single item that hat as a success status.

Check failure on line 3341 in modules/core/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 3341 in modules/core/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
ack, found := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
suite.Require().True(found)
suite.Require().NotNil(ack)

expectedCommittedBz := channeltypes.CommitAcknowledgement(ibcmock.MockAcknowledgement.Acknowledgement())
suite.Require().Equal(expectedCommittedBz, ack)

packetKey = host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
proof, proofHeight = path.EndpointB.QueryProof(packetKey)

msgAck := channeltypes.NewMsgAcknowledgement(packet, ibcmock.MockAcknowledgement.Acknowledgement(), proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String())

ackPacketResponse, err := path.EndpointA.Chain.SendMsgs(msgAck)
suite.Require().NoError(path.EndpointB.UpdateClient())

suite.Require().NoError(err)
suite.Require().NotNil(ackPacketResponse)
}
4 changes: 3 additions & 1 deletion modules/core/packet-server/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package keeper_test

import (
"fmt"
testifysuite "github.com/stretchr/testify/suite"
"testing"


testifysuite "github.com/stretchr/testify/suite"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
Expand Down

0 comments on commit 0c48c10

Please sign in to comment.