Skip to content

Commit

Permalink
fix ack structure for v2 transfer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bznein committed Nov 15, 2024
1 parent 739af65 commit d7c9437
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 49 deletions.
54 changes: 13 additions & 41 deletions modules/apps/transfer/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"bytes"
"time"

sdkmath "cosmossdk.io/math"
Expand All @@ -13,6 +14,7 @@ import (
channeltypesv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
mockv2 "github.com/cosmos/ibc-go/v9/testing/mock/v2"
)

// TestMsgSendPacketTransfer tests the MsgSendPacket rpc handler for the transfer v2 application.
Expand Down Expand Up @@ -130,11 +132,7 @@ func (suite *KeeperTestSuite) TestMsgRecvPacketTransfer() {
{
"failure: receive is disabled",
func() {
expectedAck.AcknowledgementResults[0].RecvPacketResult = channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Failure,
Acknowledgement: channeltypes.NewErrorAcknowledgement(transfertypes.ErrReceiveDisabled).Acknowledgement(),
}

expectedAck.AppAcknowledgements[0] = channeltypes.NewErrorAcknowledgement(transfertypes.ErrReceiveDisabled).Acknowledgement()
suite.chainB.GetSimApp().TransferKeeperV2.SetParams(suite.chainB.GetContext(),
transfertypes.Params{
ReceiveEnabled: false,
Expand Down Expand Up @@ -174,16 +172,8 @@ func (suite *KeeperTestSuite) TestMsgRecvPacketTransfer() {
suite.Require().NoError(err)

// by default, we assume a successful acknowledgement will be written.
expectedAck = channeltypesv2.Acknowledgement{AcknowledgementResults: []channeltypesv2.AcknowledgementResult{
{
AppName: transfertypes.ModuleName,
RecvPacketResult: channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Success,
Acknowledgement: channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement(),
},
},
}}

ackBytes := channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement()
expectedAck = channeltypesv2.Acknowledgement{AppAcknowledgements: [][]byte{ackBytes}}
tc.malleate()

err = path.EndpointB.MsgRecvPacket(packet)
Expand All @@ -208,7 +198,7 @@ func (suite *KeeperTestSuite) TestMsgRecvPacketTransfer() {

var expectedBalance sdk.Coin
// on a successful ack we expect the full amount to be transferred
if expectedAck.AcknowledgementResults[0].RecvPacketResult.Status == channeltypesv2.PacketStatus_Success {
if bytes.Equal(expectedAck.AppAcknowledgements[0], ackBytes) {
expectedBalance = sdk.NewCoin(denom.IBCDenom(), ibctesting.DefaultCoinAmount)
} else {
// otherwise the tokens do not make it to the address.
Expand Down Expand Up @@ -247,18 +237,15 @@ func (suite *KeeperTestSuite) TestMsgAckPacketTransfer() {
{
"failure: proof verification failure",
func() {
expectedAck.AcknowledgementResults[0].RecvPacketResult.Acknowledgement = channeltypes.NewResultAcknowledgement([]byte{byte(2)}).Acknowledgement()
expectedAck.AppAcknowledgements[0] = mockv2.MockFailRecvPacketResult.Acknowledgement
},
commitmenttypes.ErrInvalidProof,
false,
},
{
"failure: escrowed tokens are refunded",
func() {
expectedAck.AcknowledgementResults[0].RecvPacketResult = channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Failure,
Acknowledgement: channeltypes.NewErrorAcknowledgement(transfertypes.ErrReceiveDisabled).Acknowledgement(),
}
expectedAck.AppAcknowledgements[0] = channeltypes.NewErrorAcknowledgement(transfertypes.ErrReceiveDisabled).Acknowledgement()
},
nil,
true,
Expand Down Expand Up @@ -306,16 +293,8 @@ func (suite *KeeperTestSuite) TestMsgAckPacketTransfer() {
err = path.EndpointB.MsgRecvPacket(packet)
suite.Require().NoError(err)

expectedAck = channeltypesv2.Acknowledgement{AcknowledgementResults: []channeltypesv2.AcknowledgementResult{
{
AppName: transfertypes.ModuleName,
RecvPacketResult: channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Success,
Acknowledgement: channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement(),
},
},
}}

ackBytes := channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement()
expectedAck = channeltypesv2.Acknowledgement{AppAcknowledgements: [][]byte{ackBytes}}
tc.malleate()

err = path.EndpointA.MsgAcknowledgePacket(packet, expectedAck)
Expand All @@ -324,7 +303,7 @@ func (suite *KeeperTestSuite) TestMsgAckPacketTransfer() {
if expPass {
suite.Require().NoError(err)

if expectedAck.AcknowledgementResults[0].RecvPacketResult.Status == channeltypesv2.PacketStatus_Success {
if bytes.Equal(expectedAck.AppAcknowledgements[0], ackBytes) {
// tokens remain escrowed
for _, t := range tokens {
escrowedAmount := suite.chainA.GetSimApp().TransferKeeperV2.GetTotalEscrowForDenom(suite.chainA.GetContext(), t.Denom.IBCDenom())
Expand Down Expand Up @@ -466,15 +445,8 @@ func (suite *KeeperTestSuite) TestV2RetainsFungibility() {
},
}

successfulAck := channeltypesv2.Acknowledgement{AcknowledgementResults: []channeltypesv2.AcknowledgementResult{
{
AppName: transfertypes.ModuleName,
RecvPacketResult: channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Success,
Acknowledgement: channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement(),
},
},
}}
ackBytes := channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement()
successfulAck := channeltypesv2.Acknowledgement{AppAcknowledgements: [][]byte{ackBytes}}

originalAmount, ok := sdkmath.NewIntFromString(ibctesting.DefaultGenesisAccBalance)
suite.Require().True(ok)
Expand Down
4 changes: 2 additions & 2 deletions modules/core/04-channel/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (k *Keeper) RecvPacket(ctx context.Context, msg *types.MsgRecvPacket) (*typ

// TODO: store the packet for async applications to access if required.
defer telemetry.ReportRecvPacket(msg.Packet)

sdkCtx.Logger().Info("receive packet callback succeeded", "source-channel", msg.Packet.SourceChannel, "dest-channel", msg.Packet.DestinationChannel, "result", types.SUCCESS.String())
return &types.MsgRecvPacketResponse{Result: types.SUCCESS}, nil
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func (k *Keeper) Acknowledgement(ctx context.Context, msg *types.MsgAcknowledgem
for i, pd := range msg.Packet.Payloads {
cbs := k.Router.Route(pd.SourcePort)
ack := msg.Acknowledgement.AppAcknowledgements[i]
err := cbs.OnAcknowledgementPacket(ctx, msg.Packet.SourceChannel, msg.Packet.DestinationChannel, pd, ack, relayer)
err := cbs.OnAcknowledgementPacket(ctx, msg.Packet.SourceChannel, msg.Packet.DestinationChannel, msg.Packet.Sequence, ack, pd, relayer)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed OnAcknowledgementPacket for source port %s, source channel %s, destination channel %s", pd.SourcePort, msg.Packet.SourceChannel, msg.Packet.DestinationChannel)
}
Expand Down
10 changes: 5 additions & 5 deletions modules/core/04-channel/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (suite *KeeperTestSuite) TestMsgRecvPacket() {
expectedAck := types.Acknowledgement{AppAcknowledgements: [][]byte{expRecvRes.Acknowledgement}}

// modify the callback to return the expected recv result.
path.EndpointB.Chain.GetSimApp().MockModuleV2B.IBCApp.OnRecvPacket = func(ctx context.Context, sourceChannel string, destinationChannel string, data types.Payload, relayer sdk.AccAddress) types.RecvPacketResult {
path.EndpointB.Chain.GetSimApp().MockModuleV2B.IBCApp.OnRecvPacket = func(ctx context.Context, sourceChannel string, destinationChannel string, sequence uint64, data types.Payload, relayer sdk.AccAddress) types.RecvPacketResult {
return expRecvRes
}

Expand Down Expand Up @@ -377,15 +377,15 @@ func (suite *KeeperTestSuite) TestMsgAcknowledgement() {

// Modify the callback to return an error.
// This way, we can verify that the callback is not executed in a No-op case.
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnAcknowledgementPacket = func(context.Context, string, string, uint64, channeltypesv2.Payload, []byte, sdk.AccAddress) error {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnAcknowledgementPacket = func(context.Context, string, string, uint64, types.Payload, []byte, sdk.AccAddress) error {
return mock.MockApplicationCallbackError
}
},
},
{
name: "failure: callback fails",
malleate: func() {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnAcknowledgementPacket = func(context.Context, string, string, uint64, channeltypesv2.Payload, []byte, sdk.AccAddress) error {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnAcknowledgementPacket = func(context.Context, string, string, uint64, types.Payload, []byte, sdk.AccAddress) error {
return mock.MockApplicationCallbackError
}
},
Expand Down Expand Up @@ -471,7 +471,7 @@ func (suite *KeeperTestSuite) TestMsgTimeout() {

// Modify the callback to return a different error.
// This way, we can verify that the callback is not executed in a No-op case.
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnTimeoutPacket = func(context.Context, string, string, uint64, channeltypesv2.Payload, sdk.AccAddress) error {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnTimeoutPacket = func(context.Context, string, string, uint64, types.Payload, sdk.AccAddress) error {
return mock.MockApplicationCallbackError
}
},
Expand All @@ -480,7 +480,7 @@ func (suite *KeeperTestSuite) TestMsgTimeout() {
{
name: "failure: callback fails",
malleate: func() {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnTimeoutPacket = func(context.Context, string, string, uint64, channeltypesv2.Payload, sdk.AccAddress) error {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnTimeoutPacket = func(context.Context, string, string, uint64, types.Payload, sdk.AccAddress) error {
return mock.MockApplicationCallbackError
}
},
Expand Down
1 change: 0 additions & 1 deletion modules/core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
channelkeeperv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/keeper"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down

0 comments on commit d7c9437

Please sign in to comment.