From 3ec64c3d1dfead58c99a09a8c821faf465e23631 Mon Sep 17 00:00:00 2001 From: Kriss <93033289+kriss39@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:25:34 +0400 Subject: [PATCH] fix(apps/callbacks): use raw app ack bytes in v2 async receive callback In the IBC v2 callbacks middleware the destination callback invoked from OnRecvPacket wraps the application acknowledgement in RecvAcknowledgement, so Acknowledgement() returns the raw application bytes. The asynchronous WriteAcknowledgement path instead wrapped the same bytes in a channeltypesv2.Acknowledgement, whose Acknowledgement() returns the protobuf encoding of the wrapper. A contract therefore observed different acknowledgement bytes for the same packet depending on whether the application acknowledged synchronously or asynchronously. Use RecvAcknowledgement in both paths and add a regression test that compares the bytes delivered to the contract keeper. Signed-off-by: Kriss <93033289+kriss39@users.noreply.github.com> --- CHANGELOG.md | 1 + modules/apps/callbacks/v2/ibc_middleware.go | 17 ++----- .../apps/callbacks/v2/ibc_middleware_test.go | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f37e7e324..519ef48794 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (apps/callbacks) [\#9079](https://github.com/cosmos/ibc-go/pull/9079) Pass the raw application acknowledgement bytes to the destination callback in the IBC v2 asynchronous `WriteAcknowledgement` path, matching the synchronous `OnRecvPacket` path (previously the contract received a protobuf-encoded `channeltypesv2.Acknowledgement` wrapper). * (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/apps/callbacks/v2/ibc_middleware.go b/modules/apps/callbacks/v2/ibc_middleware.go index 16a7138bc7..3fca55f4ed 100644 --- a/modules/apps/callbacks/v2/ibc_middleware.go +++ b/modules/apps/callbacks/v2/ibc_middleware.go @@ -417,10 +417,6 @@ func (im *IBCMiddleware) WriteAcknowledgement( return err } - recvResult := channeltypesv2.RecvPacketResult{ - Status: channeltypesv2.PacketStatus_Success, - Acknowledgement: ack.AppAcknowledgements[0], - } callbackExecutor := func(cachedCtx sdk.Context) error { // reconstruct a channel v1 packet from the v2 packet // in order to preserve the same interface for the contract keeper @@ -434,15 +430,12 @@ func (im *IBCMiddleware) WriteAcknowledgement( TimeoutHeight: clienttypes.Height{}, TimeoutTimestamp: 0, } - // wrap the individual acknowledgement into the channeltypesv2.Acknowledgement since it implements the exported.Acknowledgement interface - var ack channeltypesv2.Acknowledgement - if recvResult.Status == channeltypesv2.PacketStatus_Failure { - ack = channeltypesv2.NewAcknowledgement(channeltypesv2.ErrorAcknowledgement[:]) - } else { - ack = channeltypesv2.NewAcknowledgement(recvResult.Acknowledgement) - } + // The contract must observe the same acknowledgement representation regardless + // of whether the acknowledgement was written synchronously in OnRecvPacket or + // asynchronously here: the raw application acknowledgement bytes. + recvAck := RecvAcknowledgement(ack.AppAcknowledgements[0]) return im.contractKeeper.IBCReceivePacketCallback( - cachedCtx, packetv1, ack, cbData.CallbackAddress, payload.Version, + cachedCtx, packetv1, recvAck, cbData.CallbackAddress, payload.Version, ) } diff --git a/modules/apps/callbacks/v2/ibc_middleware_test.go b/modules/apps/callbacks/v2/ibc_middleware_test.go index 55f16d3707..cb7bec5956 100644 --- a/modules/apps/callbacks/v2/ibc_middleware_test.go +++ b/modules/apps/callbacks/v2/ibc_middleware_test.go @@ -21,6 +21,7 @@ import ( channeltypesv2 "github.com/lfdt-ibc/ibc-go/v11/modules/core/04-channel/v2/types" "github.com/lfdt-ibc/ibc-go/v11/modules/core/api" ibcerrors "github.com/lfdt-ibc/ibc-go/v11/modules/core/errors" + ibcexported "github.com/lfdt-ibc/ibc-go/v11/modules/core/exported" ibctesting "github.com/lfdt-ibc/ibc-go/v11/testing" ibcmock "github.com/lfdt-ibc/ibc-go/v11/testing/mock" ibcmockv2 "github.com/lfdt-ibc/ibc-go/v11/testing/mock/v2" @@ -852,3 +853,53 @@ func (s *CallbacksTestSuite) TestWriteAcknowledgement() { }) } } + +// TestWriteAcknowledgementCallbackAckMatchesSyncPath ensures that a contract +// receives the same acknowledgement representation (the raw application +// acknowledgement bytes) whether the acknowledgement is written synchronously +// via OnRecvPacket or asynchronously via WriteAcknowledgement. +func (s *CallbacksTestSuite) TestWriteAcknowledgementCallbackAckMatchesSyncPath() { + s.SetupTest() + + packetData := transfertypes.NewFungibleTokenPacketData( + ibctesting.TestCoin.Denom, + ibctesting.TestCoin.Amount.String(), + ibctesting.TestAccAddress, + s.chainB.SenderAccount.GetAddress().String(), + fmt.Sprintf(`{"dest_callback": {"address":"%s", "gas_limit":"600000"}}`, ibctesting.TestAccAddress), + ) + payload := channeltypesv2.NewPayload( + transfertypes.PortID, transfertypes.PortID, + transfertypes.V1, transfertypes.EncodingJSON, + packetData.GetBytes(), + ) + + var receivedAcks [][]byte + GetSimApp(s.chainB).MockContractKeeper.IBCReceivePacketCallbackFn = func( + _ sdk.Context, _ ibcexported.PacketI, ack ibcexported.Acknowledgement, _, _ string, + ) error { + receivedAcks = append(receivedAcks, ack.Acknowledgement()) + return nil + } + + cbs := s.chainB.App.GetIBCKeeper().ChannelKeeperV2.Router.Route(ibctesting.TransferPort) + mw, ok := cbs.(api.WriteAcknowledgementWrapper) + s.Require().True(ok) + + // synchronous path + ctx := s.chainB.GetContext() + res := cbs.OnRecvPacket(ctx, s.path.EndpointA.ClientID, s.path.EndpointB.ClientID, 1, payload, s.chainB.SenderAccount.GetAddress()) + s.Require().Equal(channeltypesv2.PacketStatus_Success, res.Status) + s.Require().Len(receivedAcks, 1) + s.Require().Equal(res.Acknowledgement, receivedAcks[0]) + + // asynchronous path with the same application acknowledgement + packet := channeltypesv2.NewPacket(2, s.path.EndpointA.ClientID, s.path.EndpointB.ClientID, uint64(ctx.BlockTime().Unix()), payload) + s.chainB.App.GetIBCKeeper().ChannelKeeperV2.SetAsyncPacket(ctx, packet.DestinationClient, packet.Sequence, packet) + s.chainB.App.GetIBCKeeper().ChannelKeeperV2.SetPacketReceipt(ctx, packet.DestinationClient, packet.Sequence) + + err := mw.WriteAcknowledgement(ctx, packet.DestinationClient, packet.Sequence, channeltypesv2.NewAcknowledgement(res.Acknowledgement)) + s.Require().NoError(err) + s.Require().Len(receivedAcks, 2) + s.Require().Equal(receivedAcks[0], receivedAcks[1], "async callback must receive the same acknowledgement bytes as the sync callback") +}