Skip to content

Commit

Permalink
simplifying code in packet_test (cosmos#4980)
Browse files Browse the repository at this point in the history
* reducing boilerplate code

* lint

* gofumpt

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
  • Loading branch information
3 people authored Nov 1, 2023
1 parent 866ca27 commit a11c558
Showing 1 changed file with 44 additions and 86 deletions.
130 changes: 44 additions & 86 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper_test
import (
"fmt"

"cosmossdk.io/errors"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
Expand Down Expand Up @@ -671,6 +673,32 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
channelCap *capabilitytypes.Capability
)

assertErr := func(errType *errors.Error) func(commitment []byte, err error) {
return func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, errType)
suite.Require().NotNil(commitment)
}
}

assertNoOp := func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrNoOpMsg)
suite.Require().Nil(commitment)
}

assertSuccess := func(seq func() uint64, msg string) func(commitment []byte, err error) {
return func(commitment []byte, err error) {
suite.Require().NoError(err)
suite.Require().Nil(commitment)

nextSequenceAck, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel())

suite.Require().True(found)
suite.Require().Equal(seq(), nextSequenceAck, msg)
}
}

testCases := []struct {
name string
malleate func()
Expand All @@ -693,14 +721,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().NoError(err)
suite.Require().Nil(commitment)

nextSequenceAck, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel())
suite.Require().True(found)
suite.Require().Equal(packet.GetSequence()+1, nextSequenceAck, "sequence not incremented in ordered channel")
},
expResult: assertSuccess(func() uint64 { return packet.GetSequence() + 1 }, "sequence not incremented in ordered channel"),
},
{
name: "success on unordered channel",
Expand All @@ -719,14 +740,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().NoError(err)
suite.Require().Nil(commitment)

nextSequenceAck, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel())
suite.Require().True(found)
suite.Require().Equal(uint64(1), nextSequenceAck, "sequence incremented for UNORDERED channel")
},
expResult: assertSuccess(func() uint64 { return uint64(1) }, "sequence incremented for UNORDERED channel"),
},
{
name: "packet already acknowledged ordered channel (no-op)",
Expand All @@ -748,11 +762,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
err = path.EndpointA.AcknowledgePacket(packet, ack.Acknowledgement())
suite.Require().NoError(err)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrNoOpMsg)
suite.Require().Nil(commitment)
},
expResult: assertNoOp,
},
{
name: "packet already acknowledged unordered channel (no-op)",
Expand All @@ -774,11 +784,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
err = path.EndpointA.AcknowledgePacket(packet, ack.Acknowledgement())
suite.Require().NoError(err)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrNoOpMsg)
suite.Require().Nil(commitment)
},
expResult: assertNoOp,
},
{
name: "channel not found",
Expand All @@ -792,11 +798,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

packet = types.NewPacket(ibctesting.MockPacketData, sequence, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrChannelNotFound)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrChannelNotFound),
},
{
name: "channel not open",
Expand All @@ -813,11 +815,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.Require().NoError(err)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrInvalidChannelState)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrInvalidChannelState),
},
{
name: "capability authentication failed ORDERED",
Expand All @@ -836,11 +834,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = capabilitytypes.NewCapability(3)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrInvalidChannelCapability)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrInvalidChannelCapability),
},
{
name: "packet destination port ≠ channel counterparty port",
Expand All @@ -855,11 +849,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrInvalidPacket)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrInvalidPacket),
},
{
name: "packet destination channel ID ≠ channel counterparty channel ID",
Expand All @@ -874,11 +864,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, ibctesting.InvalidID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrInvalidPacket)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrInvalidPacket),
},
{
name: "connection not found",
Expand All @@ -901,11 +887,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, connectiontypes.ErrConnectionNotFound)
suite.Require().NotNil(commitment)
},
expResult: assertErr(connectiontypes.ErrConnectionNotFound),
},
{
name: "connection not OPEN",
Expand All @@ -931,11 +913,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, connectiontypes.ErrInvalidConnectionState)
suite.Require().NotNil(commitment)
},
expResult: assertErr(connectiontypes.ErrInvalidConnectionState),
},
{
name: "packet hasn't been sent",
Expand All @@ -945,11 +923,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrNoOpMsg) // NOTE: ibc core does not distinguish between unsent and already relayed packets.
suite.Require().Nil(commitment)
},
expResult: assertNoOp, // NOTE: ibc core does not distinguish between unsent and already relayed packets.
},
{
name: "packet ack verification failed",
Expand All @@ -965,11 +939,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, commitmenttypes.ErrInvalidProof)
suite.Require().NotNil(commitment)
},
expResult: assertErr(commitmenttypes.ErrInvalidProof),
},
{
name: "packet commitment bytes do not match",
Expand All @@ -990,11 +960,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

packet.Data = []byte("invalid packet commitment")
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrInvalidPacket)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrInvalidPacket),
},
{
name: "next ack sequence not found",
Expand All @@ -1018,11 +984,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrSequenceAckNotFound)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrSequenceAckNotFound),
},
{
name: "next ack sequence mismatch ORDERED",
Expand All @@ -1043,11 +1005,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceAck(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 10)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrPacketSequenceOutOfOrder)
suite.Require().NotNil(commitment)
},
expResult: assertErr(types.ErrPacketSequenceOutOfOrder),
},
}

Expand Down

0 comments on commit a11c558

Please sign in to comment.