Skip to content

Commit

Permalink
Add tests for fee events (cosmos#5706)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Feb 14, 2024
1 parent e42d0d2 commit b68ba7d
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions modules/apps/29-fee/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
"fmt"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -72,7 +74,8 @@ func (suite *KeeperTestSuite) TestRegisterPayee() {

tc.malleate()

res, err := suite.chainA.GetSimApp().IBCFeeKeeper.RegisterPayee(suite.chainA.GetContext(), msg)
ctx := suite.chainA.GetContext()
res, err := suite.chainA.GetSimApp().IBCFeeKeeper.RegisterPayee(ctx, msg)

if tc.expPass {
suite.Require().NoError(err)
Expand All @@ -86,6 +89,19 @@ func (suite *KeeperTestSuite) TestRegisterPayee() {

suite.Require().True(found)
suite.Require().Equal(suite.chainA.SenderAccounts[1].SenderAccount.GetAddress().String(), payeeAddr)

expectedEvents := sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterPayee,
sdk.NewAttribute(types.AttributeKeyRelayer, suite.chainA.SenderAccount.GetAddress().String()),
sdk.NewAttribute(types.AttributeKeyPayee, payeeAddr),
sdk.NewAttribute(types.AttributeKeyChannelID, suite.path.EndpointA.ChannelID),
),
}.ToABCIEvents()

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

} else {
suite.Require().Error(err)
}
Expand Down Expand Up @@ -148,7 +164,8 @@ func (suite *KeeperTestSuite) TestRegisterCounterpartyPayee() {

tc.malleate()

res, err := suite.chainA.GetSimApp().IBCFeeKeeper.RegisterCounterpartyPayee(suite.chainA.GetContext(), msg)
ctx := suite.chainA.GetContext()
res, err := suite.chainA.GetSimApp().IBCFeeKeeper.RegisterCounterpartyPayee(ctx, msg)

if tc.expPass {
suite.Require().NoError(err)
Expand All @@ -162,6 +179,19 @@ func (suite *KeeperTestSuite) TestRegisterCounterpartyPayee() {

suite.Require().True(found)
suite.Require().Equal(expCounterpartyPayee, counterpartyPayee)

expectedEvents := sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterCounterpartyPayee,
sdk.NewAttribute(types.AttributeKeyRelayer, suite.chainA.SenderAccount.GetAddress().String()),
sdk.NewAttribute(types.AttributeKeyCounterpartyPayee, counterpartyPayee),
sdk.NewAttribute(types.AttributeKeyChannelID, ibctesting.FirstChannelID),
),
}.ToABCIEvents()

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

} else {
suite.Require().Error(err)
}
Expand All @@ -174,6 +204,7 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
expFeesInEscrow []types.PacketFee
msg *types.MsgPayPacketFee
fee types.Fee
eventFee types.Fee
)

testCases := []struct {
Expand All @@ -189,18 +220,20 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
{
"success with existing packet fees in escrow",
func() {
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
escrowFee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)

packetID := channeltypes.NewPacketID(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount.GetAddress().String(), nil)
packetFee := types.NewPacketFee(escrowFee, suite.chainA.SenderAccount.GetAddress().String(), nil)
feesInEscrow := types.NewPacketFees([]types.PacketFee{packetFee})

suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, feesInEscrow)
err := suite.chainA.GetSimApp().BankKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), types.ModuleName, fee.Total())
err := suite.chainA.GetSimApp().BankKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), types.ModuleName, escrowFee.Total())
suite.Require().NoError(err)

expEscrowBalance = expEscrowBalance.Add(fee.Total()...)
expEscrowBalance = expEscrowBalance.Add(escrowFee.Total()...)
expFeesInEscrow = append(expFeesInEscrow, packetFee)

eventFee = types.NewFee(defaultRecvFee.Add(escrowFee.RecvFee...), defaultAckFee.Add(escrowFee.AckFee...), defaultTimeoutFee.Add(escrowFee.TimeoutFee...))
},
true,
},
Expand Down Expand Up @@ -317,10 +350,12 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
expEscrowBalance = fee.Total()
expPacketFee := types.NewPacketFee(fee, suite.chainA.SenderAccount.GetAddress().String(), nil)
expFeesInEscrow = []types.PacketFee{expPacketFee}
eventFee = fee

tc.malleate()

_, err := suite.chainA.GetSimApp().IBCFeeKeeper.PayPacketFee(suite.chainA.GetContext(), msg)
ctx := suite.chainA.GetContext()
_, err := suite.chainA.GetSimApp().IBCFeeKeeper.PayPacketFee(ctx, msg)

if tc.expPass {
suite.Require().NoError(err) // message committed
Expand All @@ -332,6 +367,22 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {

escrowBalance := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.GetSimApp().IBCFeeKeeper.GetFeeModuleAddress(), sdk.DefaultBondDenom)
suite.Require().Equal(expEscrowBalance.AmountOf(sdk.DefaultBondDenom), escrowBalance.Amount)

expectedEvents := sdk.Events{
sdk.NewEvent(
types.EventTypeIncentivizedPacket,
sdk.NewAttribute(channeltypes.AttributeKeyPortID, packetID.PortId),
sdk.NewAttribute(channeltypes.AttributeKeyChannelID, packetID.ChannelId),
sdk.NewAttribute(channeltypes.AttributeKeySequence, fmt.Sprint(packetID.Sequence)),
sdk.NewAttribute(types.AttributeKeyRecvFee, eventFee.RecvFee.String()),
sdk.NewAttribute(types.AttributeKeyAckFee, eventFee.AckFee.String()),
sdk.NewAttribute(types.AttributeKeyTimeoutFee, eventFee.TimeoutFee.String()),
),
}.ToABCIEvents()

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

} else {
suite.Require().Error(err)

Expand Down

0 comments on commit b68ba7d

Please sign in to comment.