From 38f1f4be1f808432009a98cbb6421a1f820f7613 Mon Sep 17 00:00:00 2001 From: Kriss <93033289+kriss39@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:25:31 +0400 Subject: [PATCH] fix(core/04-channel/v2): do not reuse TAO cache context for app callbacks in RecvPacket Since #8545 the application callbacks in MsgRecvPacket run in the same cached context that was already written after TAO verification. The second writeFn call re-emits every event recorded by recvPacket, so relayers and indexers see the core recv_packet (and message) events twice on every successful receive. When an application returns a failure the same TAO events are additionally converted and emitted as ibccallbackerror-* events. Create a fresh cached context for the application callbacks, matching the v1 handler, and add a regression test asserting the event counts. Signed-off-by: Kriss <93033289+kriss39@users.noreply.github.com> --- CHANGELOG.md | 1 + .../core/04-channel/v2/keeper/msg_server.go | 6 +++ .../04-channel/v2/keeper/msg_server_test.go | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f37e7e324..3cc1ccfb2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (core/04-channel/v2) [\#9078](https://github.com/cosmos/ibc-go/pull/9078) Use a fresh cached context for the application callbacks in `MsgRecvPacket` so the core `recv_packet` events are no longer emitted twice on success, and are no longer re-emitted as error events when an application fails. * (core/02-client) [\#9080](https://github.com/cosmos/ibc-go/pull/9080) Return an error from `RecoverClient` when the substitute client is of a different client type than the subject instead of panicking in the subject light client module. * (apps/pfm) [\#9006](https://github.com/cosmos/ibc-go/pull/9006) fix(apps/pfm): remove legacy params before the v3 to v4 migration * (apps/rate-limiting) [\#8767](https://github.com/cosmos/ibc-go/pull/8767) Fix string conflict in rate-limiting prefix iterator diff --git a/modules/core/04-channel/v2/keeper/msg_server.go b/modules/core/04-channel/v2/keeper/msg_server.go index 0382402092..097a56f876 100644 --- a/modules/core/04-channel/v2/keeper/msg_server.go +++ b/modules/core/04-channel/v2/keeper/msg_server.go @@ -85,6 +85,12 @@ func (k *Keeper) RecvPacket(goCtx context.Context, msg *types.MsgRecvPacket) (*t AppAcknowledgements: [][]byte{}, } + // Use a fresh cached context for the application callbacks so that the + // TAO state and events written above are not written (and emitted) a + // second time, and so that TAO events are not re-emitted as error events + // when an application returns a failure. + cacheCtx, writeFn = ctx.CacheContext() + var isAsync bool isSuccess := true for _, pd := range msg.Packet.Payloads { diff --git a/modules/core/04-channel/v2/keeper/msg_server_test.go b/modules/core/04-channel/v2/keeper/msg_server_test.go index 5834143ddf..87ccbba256 100644 --- a/modules/core/04-channel/v2/keeper/msg_server_test.go +++ b/modules/core/04-channel/v2/keeper/msg_server_test.go @@ -13,7 +13,9 @@ import ( clientv2types "github.com/lfdt-ibc/ibc-go/v11/modules/core/02-client/v2/types" "github.com/lfdt-ibc/ibc-go/v11/modules/core/04-channel/v2/types" commitmenttypes "github.com/lfdt-ibc/ibc-go/v11/modules/core/23-commitment/types" + hostv2 "github.com/lfdt-ibc/ibc-go/v11/modules/core/24-host/v2" ibcerrors "github.com/lfdt-ibc/ibc-go/v11/modules/core/errors" + coretypes "github.com/lfdt-ibc/ibc-go/v11/modules/core/types" ibctesting "github.com/lfdt-ibc/ibc-go/v11/testing" mockv1 "github.com/lfdt-ibc/ibc-go/v11/testing/mock" mockv2 "github.com/lfdt-ibc/ibc-go/v11/testing/mock/v2" @@ -709,3 +711,45 @@ func (s *KeeperTestSuite) TestMsgTimeout() { }) } } + +// TestMsgRecvPacketEventsNotDuplicated ensures that the core packet receipt +// events are emitted exactly once, and are not re-emitted as error events +// when an application callback fails. +func (s *KeeperTestSuite) TestMsgRecvPacketEventsNotDuplicated() { + testCases := []struct { + name string + payload types.Payload + }{ + {"success", mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB)}, + {"application failure", mockv2.NewErrorMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB)}, + } + + for _, tc := range testCases { + s.Run(tc.name, func() { + s.SetupTest() + + path := ibctesting.NewPath(s.chainA, s.chainB) + path.SetupV2() + + packet, err := path.EndpointA.MsgSendPacket(s.chainA.GetTimeoutTimestampSecs(), tc.payload) + s.Require().NoError(err) + + packetKey := hostv2.PacketCommitmentKey(packet.SourceClient, packet.Sequence) + proof, proofHeight := path.EndpointA.QueryProof(packetKey) + msg := types.NewMsgRecvPacket(packet, proof, proofHeight, s.chainB.SenderAccount.GetAddress().String()) + + ctx := s.chainB.GetContext() + _, err = s.chainB.App.GetIBCKeeper().ChannelKeeperV2.RecvPacket(ctx, msg) + s.Require().NoError(err) + + eventCounts := make(map[string]int) + for _, event := range ctx.EventManager().Events() { + eventCounts[event.Type]++ + } + + s.Require().Equal(1, eventCounts[types.EventTypeRecvPacket]) + s.Require().Equal(1, eventCounts[types.EventTypeWriteAck]) + s.Require().Zero(eventCounts[coretypes.ErrorAttributeKeyPrefix+types.EventTypeRecvPacket]) + }) + } +}