From a11c558664df2d4c3a21dbad6118c54ebcd8e9af Mon Sep 17 00:00:00 2001 From: Muku <44918265+muku314115@users.noreply.github.com> Date: Wed, 1 Nov 2023 13:43:41 +0530 Subject: [PATCH] simplifying code in packet_test (#4980) * reducing boilerplate code * lint * gofumpt --------- Co-authored-by: Carlos Rodriguez Co-authored-by: DimitrisJim --- modules/core/04-channel/keeper/packet_test.go | 130 ++++++------------ 1 file changed, 44 insertions(+), 86 deletions(-) diff --git a/modules/core/04-channel/keeper/packet_test.go b/modules/core/04-channel/keeper/packet_test.go index bd361cb2466..721a41d46de 100644 --- a/modules/core/04-channel/keeper/packet_test.go +++ b/modules/core/04-channel/keeper/packet_test.go @@ -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" @@ -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() @@ -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", @@ -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)", @@ -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)", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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), }, }