From 5d31d8f7f8e2608b7add5c9e1c005a0a6663118b Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:48:57 +0200 Subject: [PATCH 1/7] send packet eureka --- modules/core/04-channel/keeper/events.go | 2 +- modules/core/04-channel/keeper/packet.go | 2 +- modules/core/packet-server/keeper/keeper.go | 80 +++++++++++++++++++ .../packet-server/types/expected_keepers.go | 11 +++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/modules/core/04-channel/keeper/events.go b/modules/core/04-channel/keeper/events.go index b93f3d0c3d2..136c382bb12 100644 --- a/modules/core/04-channel/keeper/events.go +++ b/modules/core/04-channel/keeper/events.go @@ -121,7 +121,7 @@ func emitChannelCloseConfirmEvent(ctx sdk.Context, portID string, channelID stri // emitSendPacketEvent emits an event with packet data along with other packet information for relayer // to pick up and relay to other chain -func emitSendPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel, timeoutHeight exported.Height) { +func EmitSendPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel, timeoutHeight exported.Height) { ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeSendPacket, diff --git a/modules/core/04-channel/keeper/packet.go b/modules/core/04-channel/keeper/packet.go index 1ab81953cb9..0711722cc55 100644 --- a/modules/core/04-channel/keeper/packet.go +++ b/modules/core/04-channel/keeper/packet.go @@ -89,7 +89,7 @@ func (k *Keeper) SendPacket( k.SetNextSequenceSend(ctx, sourcePort, sourceChannel, sequence+1) k.SetPacketCommitment(ctx, sourcePort, sourceChannel, packet.GetSequence(), commitment) - emitSendPacketEvent(ctx, packet, channel, timeoutHeight) + EmitSendPacketEvent(ctx, packet, channel, timeoutHeight) k.Logger(ctx).Info( "packet sent", diff --git a/modules/core/packet-server/keeper/keeper.go b/modules/core/packet-server/keeper/keeper.go index 78ea2833ca2..987ab599a32 100644 --- a/modules/core/packet-server/keeper/keeper.go +++ b/modules/core/packet-server/keeper/keeper.go @@ -1,8 +1,15 @@ package keeper import ( + errorsmod "cosmossdk.io/errors" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" + clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" + "github.com/cosmos/ibc-go/v9/modules/core/exported" "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types" ) @@ -19,3 +26,76 @@ func NewKeeper(cdc codec.BinaryCodec, channelKeeper types.ChannelKeeper, clientK clientKeeper: clientKeeper, } } + +func (k Keeper) SendPacket( + ctx sdk.Context, + _ *capabilitytypes.Capability, + sourceChannel string, + sourcePort string, + destPort string, + timeoutHeight clienttypes.Height, + timeoutTimestamp uint64, + version string, + data []byte, +) (uint64, error) { + // Lookup counterparty associated with our source channel to retrieve the destination channel + counterparty, ok := k.clientKeeper.GetCounterparty(ctx, sourceChannel) + if !ok { + return 0, channeltypes.ErrChannelNotFound + } + destChannel := counterparty.ClientId + + // retrieve the sequence send for this channel + // if no packets have been sent yet, initialize the sequence to 1. + sequence, found := k.channelKeeper.GetNextSequenceSend(ctx, sourcePort, sourceChannel) + if !found { + sequence = 1 + } + + // construct packet from given fields and channel state + packet := channeltypes.NewPacketWithVersion(data, sequence, sourcePort, sourceChannel, + destPort, destChannel, timeoutHeight, timeoutTimestamp, version) + + if err := packet.ValidateBasic(); err != nil { + return 0, errorsmod.Wrap(err, "constructed packet failed basic validation") + } + + // check that the client of receiver chain is still active + status := k.clientKeeper.GetClientStatus(ctx, sourceChannel) + if status != exported.Active { + return 0, errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client state is not active: %s", status) + } + + // retrieve latest height and timestamp of the client of receiver chain + latestHeight := k.clientKeeper.GetClientLatestHeight(ctx, sourceChannel) + if latestHeight.IsZero() { + return 0, errorsmod.Wrapf(clienttypes.ErrInvalidHeight, "cannot send packet using client (%s) with zero height", sourceChannel) + } + + latestTimestamp, err := k.clientKeeper.GetClientTimestampAtHeight(ctx, sourceChannel, latestHeight) + if err != nil { + return 0, err + } + + // check if packet is timed out on the receiving chain + timeout := channeltypes.NewTimeout(packet.GetTimeoutHeight().(clienttypes.Height), packet.GetTimeoutTimestamp()) + if timeout.Elapsed(latestHeight, latestTimestamp) { + return 0, errorsmod.Wrap(timeout.ErrTimeoutElapsed(latestHeight, latestTimestamp), "invalid packet timeout") + } + + commitment := channeltypes.CommitPacket(packet) + + // bump the sequence and set the packet commitment so it is provable by the counterparty + k.channelKeeper.SetNextSequenceSend(ctx, sourcePort, sourceChannel, sequence+1) + k.channelKeeper.SetPacketCommitment(ctx, sourcePort, sourceChannel, packet.GetSequence(), commitment) + + // create sentinel channel for events + channel := channeltypes.Channel{ + Ordering: channeltypes.ORDERED, + ConnectionHops: []string{sourceChannel}, + } + k.channelKeeper.EmitSendPacketEvent(ctx, packet, channel, timeoutHeight) + + // return the sequence + return sequence, nil +} diff --git a/modules/core/packet-server/types/expected_keepers.go b/modules/core/packet-server/types/expected_keepers.go index a4aa0904947..28514212ed0 100644 --- a/modules/core/packet-server/types/expected_keepers.go +++ b/modules/core/packet-server/types/expected_keepers.go @@ -4,6 +4,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" + "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" ) @@ -32,6 +33,9 @@ type ChannelKeeper interface { // SetPacketAcknowledgement writes the acknowledgement hash under the acknowledgement path // This is a public path that is standardized by the IBC specification SetPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64, ackHash []byte) + + // event emission functions + EmitSendPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel, timeoutHeight exported.Height) } type ClientKeeper interface { @@ -43,4 +47,11 @@ type ClientKeeper interface { // the executing chain // This is a private path that is only used by the IBC lite module GetCounterparty(ctx sdk.Context, clientID string) (clienttypes.Counterparty, bool) + // GetClientStatus returns the status of a client given the client ID + GetClientStatus(ctx sdk.Context, clientID string) exported.Status + // GetClientLatestHeight returns the latest height of a client given the client ID + GetClientLatestHeight(ctx sdk.Context, clientID string) clienttypes.Height + // GetClientTimestampAtHeight returns the timestamp for a given height on the client + // given its client ID and height + GetClientTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error) } From 52e231901fd9be0e773b42d0e5684f0cef74c619 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 5 Aug 2024 17:55:36 +0200 Subject: [PATCH 2/7] test progress --- .../core/packet-server/keeper/keeper_test.go | 121 ++++++++++++++++++ testing/simapp/app.go | 4 + 2 files changed, 125 insertions(+) create mode 100644 modules/core/packet-server/keeper/keeper_test.go diff --git a/modules/core/packet-server/keeper/keeper_test.go b/modules/core/packet-server/keeper/keeper_test.go new file mode 100644 index 00000000000..630a83bc7db --- /dev/null +++ b/modules/core/packet-server/keeper/keeper_test.go @@ -0,0 +1,121 @@ +package keeper_test + +import ( + "fmt" + "testing" + + ibctesting "github.com/cosmos/ibc-go/v9/testing" + "github.com/cosmos/ibc-go/v9/testing/mock" +) + +// KeeperTestSuite is a testing suite to test keeper functions. +type KeeperTestSuite struct { + testifysuite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain +} + +func (suite *KeeperTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) + + // commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1) + suite.coordinator.CommitNBlocks(suite.chainA, 2) + suite.coordinator.CommitNBlocks(suite.chainB, 2) +} + +func TestKeeperTestSuite(t *testing.T) { + testifysuite.Run(t, new(KeeperTestSuite)) +} + +func (suite *KeeperTestSuite) TestSendPacket() { + var ( + path *ibctesting.Path + packet channeltypes.Packet + ) + + tests := []struct { + name string + malleate func() + expErr err + }{ + {"success", func() { + // set the counterparty for chain A + suite.chainA.App.IBCKeeper.ClientKeeper.SetCounterparty(suite.chainA.GetContext(), path.EndpointA.ClientID, clienttypes.Counterparty{ + ClientId: path.EndpointB.ClientID, + MerklePathPrefix: commitmenttypes.NewMerklePath([]byte("ibc"), []byte("")), + }) + }, nil}, + {"counterparty not found", func() {}, channeltypes.ErrChannelNotFound}, + {"packet failed basic validation", func() { + // set the counterparty for chain A + suite.chainA.App.IBCKeeper.ClientKeeper.SetCounterparty(suite.chainA.GetContext(), path.EndpointA.ClientID, clienttypes.Counterparty{ + ClientId: path.EndpointB.ClientID, + MerklePathPrefix: commitmenttypes.NewMerklePath([]byte("ibc"), []byte("")), + }) + // invalid port ID + packet.DestPort = "" + }, channeltypes.ErrInvalidPacket}, + {"client status invalid", func() { + // set the counterparty for chain A + suite.chainA.App.IBCKeeper.ClientKeeper.SetCounterparty(suite.chainA.GetContext(), path.EndpointA.ClientID, clienttypes.Counterparty{ + ClientId: path.EndpointB.ClientID, + MerklePathPrefix: commitmenttypes.NewMerklePath([]byte("ibc"), []byte("")), + }) + // change source channel id to get invalid status + packet.SourceChannel = "invalidClientID" + }, clienttypes.ErrClientNotActive}, + {"timeout elapsed", func() { + // set the counterparty for chain A + suite.chainA.App.IBCKeeper.ClientKeeper.SetCounterparty(suite.chainA.GetContext(), path.EndpointA.ClientID, clienttypes.Counterparty{ + ClientId: path.EndpointB.ClientID, + MerklePathPrefix: commitmenttypes.NewMerklePath([]byte("ibc"), []byte("")), + }) + packet.TimeoutTimestamp = 1 + }, channeltypes.ErrTimeoutElapsed}, + } + + for i, tc := range tests { + tc := tc + suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { + suite.SetupTest() // reset + + // create clients on both chains + path = ibctesting.NewPath(suite.chainA, suite.chainB) + path.SetupClients() + + // create standard packet that can be malleated + packet := channeltypes.NewPacketWithVersion(mock.MockPacketData, 1, mock.PortID, + path.EndpointA.ClientID, mock.PortID, path.EndpointB.ClientID, clienttypes.NewHeight(0, 1), 0, mock.Version) + + // malleate the test case + tc.malleate() + + // send packet + seq, err := suite.chainA.App.PacketServer.SendPacket(suite.chainA.GetContext(), nil, packet.SourceChannel, packet.SourcePort, + packet.DestPort, packet.TimeoutHeight, packet.TimeoutTimestamp, packet.Version, packet.Data) + + expPass := tc.expError == nil + if expPass { + suite.Require().NoError(err) + suite.Require().Equal(uint64(1), seq) + expCommitment := channeltypes.CommitPacket(packet) + suite.Require().Equal(expCommitment, suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.SourcePort, packet.SourceChannel, seq)) + } else { + suite.Require().Error(err) + suite.Require().ErrorIs(err, tc.expError) + suite.Require().Equal(uint64(0), seq) + suite.Require().Nil(suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.SourcePort, packet.SourceChannel, seq)) + + } + + }) + } + +} diff --git a/testing/simapp/app.go b/testing/simapp/app.go index dca117000b3..767e449364e 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -177,6 +177,7 @@ type SimApp struct { ICAHostKeeper icahostkeeper.Keeper TransferKeeper ibctransferkeeper.Keeper ConsensusParamsKeeper consensusparamkeeper.Keeper + PacketServer *packetserver.Keeper // make scoped keepers public for test purposes ScopedIBCKeeper capabilitykeeper.ScopedKeeper @@ -523,6 +524,9 @@ func NewSimApp( smLightClientModule := solomachine.NewLightClientModule(appCodec, storeProvider) clientKeeper.AddRoute(solomachine.ModuleName, &smLightClientModule) + // Set Packet Keeper for Eureka tests + app.PacketServer = packetserver.NewKeeper(appCodec, app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ClientKeeper) + // **** Module Options **** // NOTE: Any module instantiated in the module manager that is later modified From cb409fd19434e327aeb8073addacb23cdf6b4cee Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:29:02 +0200 Subject: [PATCH 3/7] add tests --- modules/core/packet-server/keeper/keeper.go | 3 +- .../core/packet-server/keeper/keeper_test.go | 34 +++++++++++-------- .../packet-server/types/expected_keepers.go | 4 --- testing/simapp/app.go | 3 -- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/modules/core/packet-server/keeper/keeper.go b/modules/core/packet-server/keeper/keeper.go index 5150b459a06..f57bd96ffeb 100644 --- a/modules/core/packet-server/keeper/keeper.go +++ b/modules/core/packet-server/keeper/keeper.go @@ -8,6 +8,7 @@ import ( capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" + channelkeeper "github.com/cosmos/ibc-go/v9/modules/core/04-channel/keeper" channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types" @@ -94,7 +95,7 @@ func (k Keeper) SendPacket( Ordering: channeltypes.ORDERED, ConnectionHops: []string{sourceChannel}, } - k.ChannelKeeper.EmitSendPacketEvent(ctx, packet, channel, timeoutHeight) + channelkeeper.EmitSendPacketEvent(ctx, packet, channel, timeoutHeight) // return the sequence return sequence, nil diff --git a/modules/core/packet-server/keeper/keeper_test.go b/modules/core/packet-server/keeper/keeper_test.go index 7c7e4a13820..f4f5a410f88 100644 --- a/modules/core/packet-server/keeper/keeper_test.go +++ b/modules/core/packet-server/keeper/keeper_test.go @@ -4,14 +4,14 @@ import ( "fmt" "testing" - "testing" - ibctesting "github.com/cosmos/ibc-go/v9/testing" "github.com/cosmos/ibc-go/v9/testing/mock" testifysuite "github.com/stretchr/testify/suite" - ibctesting "github.com/cosmos/ibc-go/v9/testing" + clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" + tmtypes "github.com/cosmos/ibc-go/v9/modules/light-clients/07-tendermint" ) // KeeperTestSuite is a testing suite to test keeper functions. @@ -47,10 +47,10 @@ func (suite *KeeperTestSuite) TestSendPacket() { packet channeltypes.Packet ) - tests := []struct { + testCases := []struct { name string malleate func() - expErr err + expError error }{ {"success", func() { // set the counterparties @@ -60,14 +60,18 @@ func (suite *KeeperTestSuite) TestSendPacket() { {"packet failed basic validation", func() { // set the counterparties path.SetupCounterparties() - // invalid port ID - packet.DestPort = "" + // invalid data + packet.Data = nil }, channeltypes.ErrInvalidPacket}, {"client status invalid", func() { // set the counterparties path.SetupCounterparties() - // change source channel id to get invalid status - packet.SourceChannel = "invalidClientID" + // make underlying client Frozen to get invalid client status + clientState, ok := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) + suite.Require().True(ok, "could not retreive client state") + tmClientState := clientState.(*tmtypes.ClientState) + tmClientState.FrozenHeight = clienttypes.NewHeight(0, 1) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmClientState) }, clienttypes.ErrClientNotActive}, {"timeout elapsed", func() { // set the counterparties @@ -76,9 +80,9 @@ func (suite *KeeperTestSuite) TestSendPacket() { }, channeltypes.ErrTimeoutElapsed}, } - for i, tc := range tests { + for i, tc := range testCases { tc := tc - suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { + suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.name, i, len(testCases)), func() { suite.SetupTest() // reset // create clients on both chains @@ -86,15 +90,15 @@ func (suite *KeeperTestSuite) TestSendPacket() { path.SetupClients() // create standard packet that can be malleated - packet := channeltypes.NewPacketWithVersion(mock.MockPacketData, 1, mock.PortID, - path.EndpointA.ClientID, mock.PortID, path.EndpointB.ClientID, clienttypes.NewHeight(0, 1), 0, mock.Version) + packet = channeltypes.NewPacketWithVersion(mock.MockPacketData, 1, mock.PortID, + path.EndpointA.ClientID, mock.PortID, path.EndpointB.ClientID, clienttypes.NewHeight(1, 100), 0, mock.Version) // malleate the test case tc.malleate() // send packet - seq, err := suite.chainA.App.PacketServer.SendPacket(suite.chainA.GetContext(), nil, packet.SourceChannel, packet.SourcePort, - packet.DestPort, packet.TimeoutHeight, packet.TimeoutTimestamp, packet.Version, packet.Data) + seq, err := suite.chainA.App.GetPacketServer().SendPacket(suite.chainA.GetContext(), nil, packet.SourceChannel, packet.SourcePort, + packet.DestinationPort, packet.TimeoutHeight, packet.TimeoutTimestamp, packet.AppVersion, packet.Data) expPass := tc.expError == nil if expPass { diff --git a/modules/core/packet-server/types/expected_keepers.go b/modules/core/packet-server/types/expected_keepers.go index f9536933868..2e9a701e2c8 100644 --- a/modules/core/packet-server/types/expected_keepers.go +++ b/modules/core/packet-server/types/expected_keepers.go @@ -4,7 +4,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" - "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" ) @@ -33,9 +32,6 @@ type ChannelKeeper interface { // SetPacketAcknowledgement writes the acknowledgement hash under the acknowledgement path // This is a public path that is standardized by the IBC specification SetPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64, ackHash []byte) - - // event emission functions - EmitSendPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel, timeoutHeight exported.Height) } type ClientKeeper interface { diff --git a/testing/simapp/app.go b/testing/simapp/app.go index 533793482e0..84d25b83495 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -528,9 +528,6 @@ func NewSimApp( smLightClientModule := solomachine.NewLightClientModule(appCodec, storeProvider) clientKeeper.AddRoute(solomachine.ModuleName, &smLightClientModule) - // Set Packet Keeper for Eureka tests - app.PacketServer = packetserver.NewKeeper(appCodec, app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ClientKeeper) - // **** Module Options **** // NOTE: Any module instantiated in the module manager that is later modified From 9f12cb56b65532e1159694c9a0f139cf00f808dc Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:37:07 +0200 Subject: [PATCH 4/7] lint --- modules/core/packet-server/keeper/keeper_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/core/packet-server/keeper/keeper_test.go b/modules/core/packet-server/keeper/keeper_test.go index f4f5a410f88..ad3604d9116 100644 --- a/modules/core/packet-server/keeper/keeper_test.go +++ b/modules/core/packet-server/keeper/keeper_test.go @@ -68,8 +68,9 @@ func (suite *KeeperTestSuite) TestSendPacket() { path.SetupCounterparties() // make underlying client Frozen to get invalid client status clientState, ok := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) - suite.Require().True(ok, "could not retreive client state") - tmClientState := clientState.(*tmtypes.ClientState) + suite.Require().True(ok, "could not retrieve client state") + tmClientState, ok := clientState.(*tmtypes.ClientState) + suite.Require().True(ok, "client is not tendermint client") tmClientState.FrozenHeight = clienttypes.NewHeight(0, 1) suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmClientState) }, clienttypes.ErrClientNotActive}, From 0b1997d3291da17349ad4151a23152f17ef468d1 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:42:30 +0200 Subject: [PATCH 5/7] nit --- modules/core/packet-server/keeper/keeper.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/core/packet-server/keeper/keeper.go b/modules/core/packet-server/keeper/keeper.go index f57bd96ffeb..49d6e9ed410 100644 --- a/modules/core/packet-server/keeper/keeper.go +++ b/modules/core/packet-server/keeper/keeper.go @@ -62,8 +62,7 @@ func (k Keeper) SendPacket( } // check that the client of receiver chain is still active - status := k.ClientKeeper.GetClientStatus(ctx, sourceChannel) - if status != exported.Active { + if status := k.ClientKeeper.GetClientStatus(ctx, sourceChannel); status != exported.Active { return 0, errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client state is not active: %s", status) } From b1ed08e40b8eef4b02c2765a4a7f3fba4cfe3809 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:59:43 +0200 Subject: [PATCH 6/7] lint moar --- modules/core/packet-server/keeper/keeper_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/core/packet-server/keeper/keeper_test.go b/modules/core/packet-server/keeper/keeper_test.go index ad3604d9116..0fb74aab1fb 100644 --- a/modules/core/packet-server/keeper/keeper_test.go +++ b/modules/core/packet-server/keeper/keeper_test.go @@ -4,14 +4,13 @@ import ( "fmt" "testing" - ibctesting "github.com/cosmos/ibc-go/v9/testing" - "github.com/cosmos/ibc-go/v9/testing/mock" - 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" tmtypes "github.com/cosmos/ibc-go/v9/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v9/testing" + "github.com/cosmos/ibc-go/v9/testing/mock" ) // KeeperTestSuite is a testing suite to test keeper functions. @@ -114,7 +113,6 @@ func (suite *KeeperTestSuite) TestSendPacket() { suite.Require().Nil(suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.SourcePort, packet.SourceChannel, seq)) } - }) } } From 4f323bcd3f4a0d2791085847032f01473e8868f9 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:36:39 +0200 Subject: [PATCH 7/7] refactor tests --- .../core/packet-server/keeper/keeper_test.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/modules/core/packet-server/keeper/keeper_test.go b/modules/core/packet-server/keeper/keeper_test.go index 0fb74aab1fb..aae084891a8 100644 --- a/modules/core/packet-server/keeper/keeper_test.go +++ b/modules/core/packet-server/keeper/keeper_test.go @@ -51,20 +51,15 @@ func (suite *KeeperTestSuite) TestSendPacket() { malleate func() expError error }{ - {"success", func() { - // set the counterparties - path.SetupCounterparties() - }, nil}, - {"counterparty not found", func() {}, channeltypes.ErrChannelNotFound}, + {"success", func() {}, nil}, + {"counterparty not found", func() { + packet.SourceChannel = ibctesting.FirstChannelID + }, channeltypes.ErrChannelNotFound}, {"packet failed basic validation", func() { - // set the counterparties - path.SetupCounterparties() // invalid data packet.Data = nil }, channeltypes.ErrInvalidPacket}, {"client status invalid", func() { - // set the counterparties - path.SetupCounterparties() // make underlying client Frozen to get invalid client status clientState, ok := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(ok, "could not retrieve client state") @@ -74,8 +69,6 @@ func (suite *KeeperTestSuite) TestSendPacket() { suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmClientState) }, clienttypes.ErrClientNotActive}, {"timeout elapsed", func() { - // set the counterparties - path.SetupCounterparties() packet.TimeoutTimestamp = 1 }, channeltypes.ErrTimeoutElapsed}, } @@ -85,9 +78,9 @@ func (suite *KeeperTestSuite) TestSendPacket() { suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.name, i, len(testCases)), func() { suite.SetupTest() // reset - // create clients on both chains + // create clients and set counterparties on both chains path = ibctesting.NewPath(suite.chainA, suite.chainB) - path.SetupClients() + path.SetupV2() // create standard packet that can be malleated packet = channeltypes.NewPacketWithVersion(mock.MockPacketData, 1, mock.PortID,