From 73095f844c031763e520ba512f997b73b99ac02e Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Sat, 26 Nov 2022 22:46:58 +0530 Subject: [PATCH] Add event testing to handshake, packets and timeouts functions --- .../core/04-channel/keeper/handshake_test.go | 86 ++++++++++++-- modules/core/04-channel/keeper/packet_test.go | 43 ++++--- .../core/04-channel/keeper/timeout_test.go | 108 ++++++++++++++++-- 3 files changed, 203 insertions(+), 34 deletions(-) diff --git a/modules/core/04-channel/keeper/handshake_test.go b/modules/core/04-channel/keeper/handshake_test.go index b607017f213..930facda250 100644 --- a/modules/core/04-channel/keeper/handshake_test.go +++ b/modules/core/04-channel/keeper/handshake_test.go @@ -101,8 +101,9 @@ func (suite *KeeperTestSuite) TestChanOpenInit() { counterparty := types.NewCounterparty(ibctesting.MockPort, ibctesting.FirstChannelID) + ctx := suite.chainA.GetContext() channelID, cap, err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanOpenInit( - suite.chainA.GetContext(), path.EndpointA.ChannelConfig.Order, []string{path.EndpointA.ConnectionID}, + ctx, path.EndpointA.ChannelConfig.Order, []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.PortID, portCap, counterparty, path.EndpointA.ChannelConfig.Version, ) @@ -122,7 +123,7 @@ func (suite *KeeperTestSuite) TestChanOpenInit() { suite.Require().Equal(types.FormatChannelIdentifier(0), channelID) chanCap, ok := suite.chainA.App.GetScopedIBCKeeper().GetCapability( - suite.chainA.GetContext(), + ctx, host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, channelID), ) suite.Require().True(ok, "could not retrieve channel capability after successful ChanOpenInit") @@ -132,6 +133,10 @@ func (suite *KeeperTestSuite) TestChanOpenInit() { suite.Require().Nil(cap) suite.Require().Equal("", channelID) } + + // Verify events + events := ctx.EventManager().Events() + suite.Require().Len(events, 0) // assert no events emitted } }) } @@ -254,8 +259,9 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { channelKey := host.ChannelKey(counterparty.PortId, counterparty.ChannelId) proof, proofHeight := suite.chainA.QueryProof(channelKey) + ctx := suite.chainB.GetContext() channelID, cap, err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanOpenTry( - suite.chainB.GetContext(), types.ORDERED, []string{path.EndpointB.ConnectionID}, + ctx, types.ORDERED, []string{path.EndpointB.ConnectionID}, path.EndpointB.ChannelConfig.PortID, portCap, counterparty, path.EndpointA.ChannelConfig.Version, proof, malleateHeight(proofHeight, heightDiff), ) @@ -265,7 +271,7 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { suite.Require().NotNil(cap) chanCap, ok := suite.chainB.App.GetScopedIBCKeeper().GetCapability( - suite.chainB.GetContext(), + ctx, host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, channelID), ) suite.Require().True(ok, "could not retrieve channel capapbility after successful ChanOpenTry") @@ -273,6 +279,10 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { } else { suite.Require().Error(err) } + + // Verify events + events := ctx.EventManager().Events() + suite.Require().Len(events, 0) // assert no events emitted }) } } @@ -434,8 +444,9 @@ func (suite *KeeperTestSuite) TestChanOpenAck() { channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) proof, proofHeight := suite.chainB.QueryProof(channelKey) + ctx := suite.chainA.GetContext() err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanOpenAck( - suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channelCap, path.EndpointB.ChannelConfig.Version, counterpartyChannelID, + ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channelCap, path.EndpointB.ChannelConfig.Version, counterpartyChannelID, proof, malleateHeight(proofHeight, heightDiff), ) @@ -444,6 +455,10 @@ func (suite *KeeperTestSuite) TestChanOpenAck() { } else { suite.Require().Error(err) } + + // Verify events + events := ctx.EventManager().Events() + suite.Require().Len(events, 0) // assert no events emitted }) } } @@ -574,8 +589,9 @@ func (suite *KeeperTestSuite) TestChanOpenConfirm() { channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID) proof, proofHeight := suite.chainA.QueryProof(channelKey) + ctx := suite.chainB.GetContext() err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanOpenConfirm( - suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, + ctx, path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, proof, malleateHeight(proofHeight, heightDiff), ) @@ -584,6 +600,10 @@ func (suite *KeeperTestSuite) TestChanOpenConfirm() { } else { suite.Require().Error(err) } + + // Verify events + events := ctx.EventManager().Events() + suite.Require().Len(events, 0) // assert no events emitted }) } } @@ -594,10 +614,12 @@ func (suite *KeeperTestSuite) TestChanCloseInit() { var ( path *ibctesting.Path channelCap *capabilitytypes.Capability + hasEvents bool ) testCases := []testCase{ {"success", func() { + hasEvents = true suite.coordinator.Setup(path) channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, @@ -655,12 +677,14 @@ func (suite *KeeperTestSuite) TestChanCloseInit() { tc := tc suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset + hasEvents = false path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() + ctx := suite.chainA.GetContext() err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanCloseInit( - suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, + ctx, path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, ) if tc.expPass { @@ -668,6 +692,27 @@ func (suite *KeeperTestSuite) TestChanCloseInit() { } else { suite.Require().Error(err) } + + // Verify events + events := ctx.EventManager().Events() + expEvents := map[string]map[string]string{ + "channel_close_init": { + "port_id": path.EndpointA.ChannelConfig.PortID, + "channel_id": path.EndpointA.ChannelID, + "counterparty_port_id": path.EndpointB.ChannelConfig.PortID, + "counterparty_channel_id": path.EndpointB.ChannelID, + "connection_id": path.EndpointA.ConnectionID, + }, + "message": { + "module": "ibc_channel", + }, + } + + if hasEvents { + ibctesting.AssertEvents(suite.Suite, expEvents, events) + } else { + suite.Require().Len(events, 0) + } }) } } @@ -680,10 +725,12 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() { path *ibctesting.Path channelCap *capabilitytypes.Capability heightDiff uint64 + hasEvents bool ) testCases := []testCase{ {"success", func() { + hasEvents = true suite.coordinator.Setup(path) channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) @@ -760,6 +807,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset heightDiff = 0 // must explicitly be changed + hasEvents = false path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() @@ -767,8 +815,9 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() { channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID) proof, proofHeight := suite.chainA.QueryProof(channelKey) + ctx := suite.chainB.GetContext() err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanCloseConfirm( - suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, + ctx, path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, proof, malleateHeight(proofHeight, heightDiff), ) @@ -777,6 +826,27 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() { } else { suite.Require().Error(err) } + + // Verify events + events := ctx.EventManager().Events() + expEvents := map[string]map[string]string{ + "channel_close_confirm": { + "port_id": path.EndpointB.ChannelConfig.PortID, + "channel_id": path.EndpointB.ChannelID, + "counterparty_port_id": path.EndpointA.ChannelConfig.PortID, + "counterparty_channel_id": path.EndpointA.ChannelID, + "connection_id": path.EndpointB.ConnectionID, + }, + "message": { + "module": "ibc_channel", + }, + } + + if hasEvents { + ibctesting.AssertEvents(suite.Suite, expEvents, events) + } else { + suite.Require().Len(events, 0) + } }) } } diff --git a/modules/core/04-channel/keeper/packet_test.go b/modules/core/04-channel/keeper/packet_test.go index b9005a81702..1bc1037a484 100644 --- a/modules/core/04-channel/keeper/packet_test.go +++ b/modules/core/04-channel/keeper/packet_test.go @@ -362,7 +362,6 @@ func (suite *KeeperTestSuite) TestRecvPacket() { packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp) // attempts to receive packet 2 without receiving packet 1 channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) - hasEvents = true }, false}, {"channel not found", func() { expError = types.ErrChannelNotFound @@ -489,6 +488,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() { suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sequence) packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp) channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + hasEvents = true }, false}, {"validation failed", func() { // skip error code check, downstream error code is used from light-client implementations @@ -671,26 +671,28 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() { // Verify events events := ctx.EventManager().Events() - expEvents := map[string]map[string]string{ - "write_acknowledgement": { - "packet_data": string(packet.GetData()), - "packet_data_hex": hex.EncodeToString(packet.GetData()), - "packet_timeout_height": packet.GetTimeoutHeight().String(), - "packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()), - "packet_sequence": fmt.Sprintf("%d", packet.GetSequence()), - "packet_src_port": path.EndpointB.ChannelConfig.PortID, - "packet_src_channel": path.EndpointB.ChannelID, - "packet_dst_port": path.EndpointA.ChannelConfig.PortID, - "packet_dst_channel": path.EndpointA.ChannelID, - "packet_channel_ordering": path.EndpointB.ChannelConfig.Order.String(), - "packet_connection": path.EndpointB.ConnectionID, - }, - "message": { - "module": "ibc_channel", - }, - } if hasEvents { + expEvents := map[string]map[string]string{ + "write_acknowledgement": { + "packet_data": string(packet.GetData()), + "packet_data_hex": hex.EncodeToString(packet.GetData()), + "packet_timeout_height": packet.GetTimeoutHeight().String(), + "packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()), + "packet_sequence": fmt.Sprintf("%d", packet.GetSequence()), + "packet_src_port": path.EndpointB.ChannelConfig.PortID, + "packet_src_channel": path.EndpointB.ChannelID, + "packet_dst_port": path.EndpointA.ChannelConfig.PortID, + "packet_dst_channel": path.EndpointA.ChannelID, + "packet_ack": string(ack.Acknowledgement()), + "packet_ack_hex": hex.EncodeToString(ack.Acknowledgement()), + "packet_connection": path.EndpointB.ConnectionID, + }, + "message": { + "module": "ibc_channel", + }, + } + ibctesting.AssertEvents(suite.Suite, expEvents, events) } else { suite.Require().Len(events, 0) @@ -763,6 +765,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { err = path.EndpointA.AcknowledgePacket(packet, ack.Acknowledgement()) suite.Require().NoError(err) + hasEvents = true }, false}, {"packet already acknowledged unordered channel (no-op)", func() { expError = types.ErrNoOpMsg @@ -783,6 +786,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { err = path.EndpointA.AcknowledgePacket(packet, ack.Acknowledgement()) suite.Require().NoError(err) + hasEvents = true }, false}, {"channel not found", func() { expError = types.ErrChannelNotFound @@ -872,6 +876,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { suite.coordinator.Setup(path) 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) + hasEvents = true }, false}, {"packet ack verification failed", func() { // skip error code check since error occurs in light-clients diff --git a/modules/core/04-channel/keeper/timeout_test.go b/modules/core/04-channel/keeper/timeout_test.go index 200ad68199b..4424bd5ca98 100644 --- a/modules/core/04-channel/keeper/timeout_test.go +++ b/modules/core/04-channel/keeper/timeout_test.go @@ -25,6 +25,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { nextSeqRecv uint64 ordered bool expError *sdkerrors.Error + hasEvents bool ) testCases := []testCase{ @@ -57,6 +58,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { {"packet already timed out: ORDERED", func() { expError = types.ErrNoOpMsg ordered = true + hasEvents = true path.SetChannelOrdered() suite.coordinator.Setup(path) @@ -75,6 +77,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { {"packet already timed out: UNORDERED", func() { expError = types.ErrNoOpMsg ordered = false + hasEvents = true suite.coordinator.Setup(path) timeoutHeight := clienttypes.GetSelfHeight(suite.chainB.GetContext()) @@ -156,6 +159,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { {"packet hasn't been sent", func() { expError = types.ErrNoOpMsg ordered = true + hasEvents = true path.SetChannelOrdered() suite.coordinator.Setup(path) @@ -205,6 +209,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { suite.SetupTest() // reset expError = nil // must be expliticly changed by failed cases + hasEvents = false // must be explicitly changed by functions emitting events nextSeqRecv = 1 // must be explicitly changed path = ibctesting.NewPath(suite.chainA, suite.chainB) @@ -221,7 +226,8 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { } } - err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutPacket(suite.chainA.GetContext(), packet, proof, proofHeight, nextSeqRecv) + ctx := suite.chainA.GetContext() + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutPacket(ctx, packet, proof, proofHeight, nextSeqRecv) if tc.expPass { suite.Require().NoError(err) @@ -232,6 +238,30 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { suite.Require().True(errors.Is(err, expError)) } } + + // Verify events + events := ctx.EventManager().Events() + expEvents := map[string]map[string]string{ + "timeout_packet": { + "packet_timeout_height": packet.GetTimeoutHeight().String(), + "packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()), + "packet_sequence": fmt.Sprintf("%d", packet.GetSequence()), + "packet_src_port": path.EndpointA.ChannelConfig.PortID, + "packet_src_channel": path.EndpointA.ChannelID, + "packet_dst_port": path.EndpointB.ChannelConfig.PortID, + "packet_dst_channel": path.EndpointB.ChannelID, + "packet_channel_ordering": path.EndpointA.ChannelConfig.Order.String(), + }, + "message": { + "module": "ibc_channel", + }, + } + + if hasEvents { + ibctesting.AssertEvents(suite.Suite, expEvents, events) + } else { + suite.Require().Len(events, 0) + } }) } } @@ -240,13 +270,15 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { // channel capabilities are verified. func (suite *KeeperTestSuite) TestTimeoutExecuted() { var ( - path *ibctesting.Path - packet types.Packet - chanCap *capabilitytypes.Capability + path *ibctesting.Path + packet types.Packet + chanCap *capabilitytypes.Capability + hasEvents bool ) testCases := []testCase{ {"success ORDERED", func() { + hasEvents = true path.SetChannelOrdered() suite.coordinator.Setup(path) @@ -282,13 +314,15 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() { for i, tc := range testCases { tc := tc suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { + hasEvents = false suite.SetupTest() // reset path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutExecuted(suite.chainA.GetContext(), chanCap, packet) - pc := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + ctx := suite.chainA.GetContext() + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutExecuted(ctx, chanCap, packet) + pc := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) if tc.expPass { suite.NoError(err) @@ -296,6 +330,38 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() { } else { suite.Error(err) } + + // Verify events + events := ctx.EventManager().Events() + expEvents := map[string]map[string]string{ + "timeout_packet": { + "packet_timeout_height": packet.GetTimeoutHeight().String(), + "packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()), + "packet_sequence": fmt.Sprintf("%d", packet.GetSequence()), + "packet_src_port": path.EndpointA.ChannelConfig.PortID, + "packet_src_channel": path.EndpointA.ChannelID, + "packet_dst_port": path.EndpointB.ChannelConfig.PortID, + "packet_dst_channel": path.EndpointB.ChannelID, + "packet_channel_ordering": path.EndpointA.ChannelConfig.Order.String(), + }, + "message": { + "module": "ibc_channel", + }, + "channel_close": { + "port_id": path.EndpointA.ChannelConfig.PortID, + "channel_id": path.EndpointA.ChannelID, + "counterparty_port_id": path.EndpointB.ChannelConfig.PortID, + "counterparty_channel_id": path.EndpointB.ChannelID, + "packet_channel_ordering": path.EndpointA.ChannelConfig.Order.String(), + "connection_id": path.EndpointA.ConnectionID, + }, + } + + if hasEvents { + ibctesting.AssertEvents(suite.Suite, expEvents, events) + } else { + suite.Require().Len(events, 0) + } }) } } @@ -309,6 +375,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { chanCap *capabilitytypes.Capability nextSeqRecv uint64 ordered bool + hasEvents bool ) testCases := []testCase{ @@ -375,6 +442,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet hasn't been sent ORDERED", func() { + hasEvents = true path.SetChannelOrdered() suite.coordinator.Setup(path) @@ -468,6 +536,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { suite.SetupTest() // reset nextSeqRecv = 1 // must be explicitly changed + hasEvents = false path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() @@ -484,13 +553,38 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { proof, _ = suite.chainB.QueryProof(unorderedPacketKey) } - err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutOnClose(suite.chainA.GetContext(), chanCap, packet, proof, proofClosed, proofHeight, nextSeqRecv) + ctx := suite.chainA.GetContext() + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutOnClose(ctx, chanCap, packet, proof, proofClosed, proofHeight, nextSeqRecv) if tc.expPass { suite.Require().NoError(err) } else { suite.Require().Error(err) } + + // Verify events + events := ctx.EventManager().Events() + expEvents := map[string]map[string]string{ + "timeout_packet": { + "packet_timeout_height": packet.GetTimeoutHeight().String(), + "packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()), + "packet_sequence": fmt.Sprintf("%d", packet.GetSequence()), + "packet_src_port": path.EndpointA.ChannelConfig.PortID, + "packet_src_channel": path.EndpointA.ChannelID, + "packet_dst_port": path.EndpointB.ChannelConfig.PortID, + "packet_dst_channel": path.EndpointB.ChannelID, + "packet_channel_ordering": path.EndpointA.ChannelConfig.Order.String(), + }, + "message": { + "module": "ibc_channel", + }, + } + + if hasEvents { + ibctesting.AssertEvents(suite.Suite, expEvents, events) + } else { + suite.Require().Len(events, 0) + } }) } }