Skip to content

Commit

Permalink
Add testing for ICA events (#5687)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Feb 8, 2024
1 parent 9b82260 commit 2c9ad16
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 35 deletions.
52 changes: 22 additions & 30 deletions e2e/tests/transfer/incentivized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
)

const (
legacyMessage = "balance should be lowered by sum of recv_fee, ack_fee and timeout_fee"
capitalEfficientMessage = "balance should be lowered by max(recv_fee + ack_fee, timeout_fee)"
)

type IncentivizedTransferTestSuite struct {
TransferTestSuite
}
Expand Down Expand Up @@ -118,12 +123,7 @@ func (s *IncentivizedTransferTestSuite) TestMsgPayPacketFee_AsyncSingleSender_Su
s.Require().True(actualFee.TimeoutFee.Equal(testFee.TimeoutFee))
})

msg := "balance should be lowered by max(recv_fee + ack_fee, timeout_fee)"
escrowTotalFee := testFee.Total()
if !testvalues.CapitalEfficientFeeEscrowFeatureReleases.IsSupported(chainAVersion) {
msg = "balance should be lowered by sum of recv_fee, ack_fee and timeout_fee"
escrowTotalFee = testFee.RecvFee.Add(testFee.AckFee...).Add(testFee.TimeoutFee...)
}
msg, escrowTotalFee := getMessageAndFee(testFee, chainAVersion)
t.Run(msg, func(t *testing.T) {
actualBalance, err := s.GetChainANativeBalance(ctx, chainAWallet)
s.Require().NoError(err)
Expand Down Expand Up @@ -237,12 +237,7 @@ func (s *IncentivizedTransferTestSuite) TestMsgPayPacketFee_InvalidReceiverAccou
s.Require().True(actualFee.TimeoutFee.Equal(testFee.TimeoutFee))
})

msg := "balance should be lowered by max(recv_fee + ack_fee, timeout_fee)"
escrowTotalFee := testFee.Total()
if !testvalues.CapitalEfficientFeeEscrowFeatureReleases.IsSupported(chainAVersion) {
msg = "balance should be lowered by sum of recv_fee, ack_fee and timeout_fee"
escrowTotalFee = testFee.RecvFee.Add(testFee.AckFee...).Add(testFee.TimeoutFee...)
}
msg, escrowTotalFee := getMessageAndFee(testFee, chainAVersion)
t.Run(msg, func(t *testing.T) {
actualBalance, err := s.GetChainANativeBalance(ctx, chainAWallet)
s.Require().NoError(err)
Expand Down Expand Up @@ -342,12 +337,7 @@ func (s *IncentivizedTransferTestSuite) TestMultiMsg_MsgPayPacketFeeSingleSender
s.Require().True(actualFee.TimeoutFee.Equal(testFee.TimeoutFee))
})

msg := "balance should be lowered by max(recv_fee + ack_fee, timeout_fee)"
escrowTotalFee := testFee.Total()
if !testvalues.CapitalEfficientFeeEscrowFeatureReleases.IsSupported(chainAVersion) {
msg = "balance should be lowered by sum of recv_fee, ack_fee and timeout_fee"
escrowTotalFee = testFee.RecvFee.Add(testFee.AckFee...).Add(testFee.TimeoutFee...)
}
msg, escrowTotalFee := getMessageAndFee(testFee, chainAVersion)
t.Run(msg, func(t *testing.T) {
actualBalance, err := s.GetChainANativeBalance(ctx, chainAWallet)
s.Require().NoError(err)
Expand Down Expand Up @@ -470,12 +460,7 @@ func (s *IncentivizedTransferTestSuite) TestMsgPayPacketFee_SingleSender_TimesOu
s.Require().True(actualFee.TimeoutFee.Equal(testFee.TimeoutFee))
})

msg := "balance should be lowered by max(recv_fee + ack_fee, timeout_fee)"
escrowTotalFee := testFee.Total()
if !testvalues.CapitalEfficientFeeEscrowFeatureReleases.IsSupported(chainAVersion) {
msg = "balance should be lowered by sum of recv_fee, ack_fee and timeout_fee"
escrowTotalFee = testFee.RecvFee.Add(testFee.AckFee...).Add(testFee.TimeoutFee...)
}
msg, escrowTotalFee := getMessageAndFee(testFee, chainAVersion)
t.Run(msg, func(t *testing.T) {
actualBalance, err := s.GetChainANativeBalance(ctx, chainAWallet)
s.Require().NoError(err)
Expand Down Expand Up @@ -574,12 +559,7 @@ func (s *IncentivizedTransferTestSuite) TestPayPacketFeeAsync_SingleSender_NoCou
})
})

msg := "balance should be lowered by max(recv_fee + ack_fee, timeout_fee)"
escrowTotalFee := testFee.Total()
if !testvalues.CapitalEfficientFeeEscrowFeatureReleases.IsSupported(chainAVersion) {
msg = "balance should be lowered by sum of recv_fee, ack_fee and timeout_fee"
escrowTotalFee = testFee.RecvFee.Add(testFee.AckFee...).Add(testFee.TimeoutFee...)
}
msg, escrowTotalFee := getMessageAndFee(testFee, chainAVersion)
t.Run(msg, func(t *testing.T) {
actualBalance, err := s.GetChainANativeBalance(ctx, chainAWallet)
s.Require().NoError(err)
Expand Down Expand Up @@ -760,3 +740,15 @@ func (s *IncentivizedTransferTestSuite) TestMsgPayPacketFee_AsyncMultipleSenders
s.Require().Equal(expected2, actualBalance2)
})
}

// getMessageAndFee returns the message that should be used for t.Run as well as the expected fee based on
// whether or not capital efficient fee escrow is supported.
func getMessageAndFee(fee feetypes.Fee, chainVersion string) (string, sdk.Coins) {
msg := capitalEfficientMessage
escrowTotalFee := fee.Total()
if !testvalues.CapitalEfficientFeeEscrowFeatureReleases.IsSupported(chainVersion) {
msg = legacyMessage
escrowTotalFee = fee.RecvFee.Add(fee.AckFee...).Add(fee.TimeoutFee...)
}
return msg, escrowTotalFee
}
8 changes: 6 additions & 2 deletions e2e/testsuite/sanitize/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func removeUnknownFields(tag string, msg sdk.Msg) sdk.Msg {
panic(err)
}
sanitizedMsgs := Messages(tag, msgs...)
msg.SetMsgs(sanitizedMsgs)
if err := msg.SetMsgs(sanitizedMsgs); err != nil {
panic(err)
}
return msg
case *grouptypes.MsgSubmitProposal:
if !groupsv1ProposalTitleAndSummary.IsSupported(tag) {
Expand All @@ -66,7 +68,9 @@ func removeUnknownFields(tag string, msg sdk.Msg) sdk.Msg {
panic(err)
}
sanitizedMsgs := Messages(tag, msgs...)
msg.SetMsgs(sanitizedMsgs)
if err := msg.SetMsgs(sanitizedMsgs); err != nil {
panic(err)
}
return msg
case *icacontrollertypes.MsgRegisterInterchainAccount:
if !icaUnorderedChannelFeatureReleases.IsSupported(tag) {
Expand Down
1 change: 1 addition & 0 deletions e2e/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
sdkmath "cosmossdk.io/math"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/cosmos/ibc-go/e2e/relayer"
"github.com/cosmos/ibc-go/e2e/testsuite/diagnostics"
feetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,22 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
0,
)

ack := cbs.OnRecvPacket(suite.chainA.GetContext(), packet, nil)
ctx := suite.chainA.GetContext()
ack := cbs.OnRecvPacket(ctx, packet, nil)
suite.Require().Equal(tc.expPass, ack.Success())

expectedEvents := sdk.Events{
sdk.NewEvent(
icatypes.EventTypePacket,
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
sdk.NewAttribute(icatypes.AttributeKeyControllerChannelID, packet.GetDestChannel()),
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, fmt.Sprintf("%t", false)),
sdk.NewAttribute(icatypes.AttributeKeyAckError, "cannot receive packet on controller chain: invalid message sent to channel end"),
),
}.ToABCIEvents()

expectedEvents = sdk.MarkEventsToIndex(expectedEvents, map[string]struct{}{})
ibctesting.AssertEvents(&suite.Suite, expectedEvents, ctx.EventManager().Events().ToABCIEvents())
})
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/apps/27-interchain-accounts/host/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (im IBCModule) OnRecvPacket(
logger := im.keeper.Logger(ctx)
if !im.keeper.GetParams(ctx).HostEnabled {
logger.Info("host submodule is disabled")
keeper.EmitHostDisabledEvent(ctx, packet)
return channeltypes.NewErrorAcknowledgement(types.ErrHostSubModuleDisabled)
}

Expand Down
38 changes: 36 additions & 2 deletions modules/apps/27-interchain-accounts/host/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,16 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
name string
malleate func()
expAckSuccess bool
eventErrorMsg string
}{
{
"success", func() {}, true,
"success", func() {}, true, "",
},
{
"host submodule disabled", func() {
suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false, []string{}))
}, false,
types.ErrHostSubModuleDisabled.Error(),
},
{
"success with ICA auth module callback failure", func() {
Expand All @@ -417,11 +419,13 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
return channeltypes.NewErrorAcknowledgement(fmt.Errorf("failed OnRecvPacket mock callback"))
}
}, true,
"failed OnRecvPacket mock callback",
},
{
"ICA OnRecvPacket fails - cannot unmarshal packet data", func() {
packetData = []byte("invalid data")
}, false,
"cannot unmarshal ICS-27 interchain account packet data: unknown data type",
},
}

Expand Down Expand Up @@ -487,12 +491,42 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module)
suite.Require().True(ok)

ack := cbs.OnRecvPacket(suite.chainB.GetContext(), packet, nil)
ctx := suite.chainB.GetContext()
ack := cbs.OnRecvPacket(ctx, packet, nil)

expectedAttributes := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
sdk.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())),
}

if tc.expAckSuccess {
suite.Require().True(ack.Success())
suite.Require().Equal(expectedAck, ack)

expectedEvents := sdk.Events{
sdk.NewEvent(
icatypes.EventTypePacket,
expectedAttributes...,
),
}.ToABCIEvents()

expectedEvents = sdk.MarkEventsToIndex(expectedEvents, map[string]struct{}{})
ibctesting.AssertEvents(&suite.Suite, expectedEvents, ctx.EventManager().Events().ToABCIEvents())

} else {
suite.Require().False(ack.Success())

expectedAttributes = append(expectedAttributes, sdk.NewAttribute(icatypes.AttributeKeyAckError, tc.eventErrorMsg))
expectedEvents := sdk.Events{
sdk.NewEvent(
icatypes.EventTypePacket,
expectedAttributes...,
),
}.ToABCIEvents()

expectedEvents = sdk.MarkEventsToIndex(expectedEvents, map[string]struct{}{})
ibctesting.AssertEvents(&suite.Suite, expectedEvents, ctx.EventManager().Events().ToABCIEvents())
}
})
}
Expand Down
14 changes: 14 additions & 0 deletions modules/apps/27-interchain-accounts/host/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
Expand All @@ -30,3 +31,16 @@ func EmitAcknowledgementEvent(ctx sdk.Context, packet channeltypes.Packet, ack e
),
)
}

// EmitHostDisabledEvent emits an event signalling that the host submodule is disabled.
func EmitHostDisabledEvent(ctx sdk.Context, packet channeltypes.Packet) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
icatypes.EventTypePacket,
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
sdk.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
sdk.NewAttribute(icatypes.AttributeKeyAckError, types.ErrHostSubModuleDisabled.Error()),
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, "false"),
),
)
}

0 comments on commit 2c9ad16

Please sign in to comment.