From 2ae4f10143e233ab69d408c12fe48a9ff58f24c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Wed, 18 May 2022 13:14:14 +0200 Subject: [PATCH] refactor: emit cumulative fees when incentivizing a packet multiple times (#1391) * emit cumulative fees * test: add test for emission of cumulative incentivized fees * add check for nil relayer * reassign sdk.Coins, fix bug --- modules/apps/29-fee/keeper/escrow.go | 2 +- modules/apps/29-fee/keeper/events.go | 26 +++++-- modules/apps/29-fee/keeper/events_test.go | 83 +++++++++++++++++++++++ 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 modules/apps/29-fee/keeper/events_test.go diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 7333b36353d..f5b786bbd9a 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -39,7 +39,7 @@ func (k Keeper) escrowPacketFee(ctx sdk.Context, packetID channeltypes.PacketId, packetFees := types.NewPacketFees(fees) k.SetFeesInEscrow(ctx, packetID, packetFees) - EmitIncentivizedPacket(ctx, packetID, packetFee) + EmitIncentivizedPacketEvent(ctx, packetID, packetFees) return nil } diff --git a/modules/apps/29-fee/keeper/events.go b/modules/apps/29-fee/keeper/events.go index 9ff6f320ffc..d60866e21d2 100644 --- a/modules/apps/29-fee/keeper/events.go +++ b/modules/apps/29-fee/keeper/events.go @@ -9,17 +9,33 @@ import ( channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" ) -// EmitIncentivizedPacket emits an event so that relayers know an incentivized packet is ready to be relayed -func EmitIncentivizedPacket(ctx sdk.Context, packetID channeltypes.PacketId, packetFee types.PacketFee) { +// EmitIncentivizedPacketEvent emits an event containing information on the total amount of fees incentivizing +// a specific packet. It should be emitted on every fee escrowed for the given packetID. +func EmitIncentivizedPacketEvent(ctx sdk.Context, packetID channeltypes.PacketId, packetFees types.PacketFees) { + var ( + totalRecvFees sdk.Coins + totalAckFees sdk.Coins + totalTimeoutFees sdk.Coins + ) + + for _, fee := range packetFees.PacketFees { + // only emit total fees for packet fees which allow any relayer to relay + if fee.Relayers == nil { + totalRecvFees = totalRecvFees.Add(fee.Fee.RecvFee...) + totalAckFees = totalAckFees.Add(fee.Fee.AckFee...) + totalTimeoutFees = totalTimeoutFees.Add(fee.Fee.TimeoutFee...) + } + } + ctx.EventManager().EmitEvent( 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, packetFee.Fee.RecvFee.String()), - sdk.NewAttribute(types.AttributeKeyAckFee, packetFee.Fee.AckFee.String()), - sdk.NewAttribute(types.AttributeKeyTimeoutFee, packetFee.Fee.TimeoutFee.String()), + sdk.NewAttribute(types.AttributeKeyRecvFee, totalRecvFees.String()), + sdk.NewAttribute(types.AttributeKeyAckFee, totalAckFees.String()), + sdk.NewAttribute(types.AttributeKeyTimeoutFee, totalTimeoutFees.String()), ), ) } diff --git a/modules/apps/29-fee/keeper/events_test.go b/modules/apps/29-fee/keeper/events_test.go new file mode 100644 index 00000000000..8ab52295e73 --- /dev/null +++ b/modules/apps/29-fee/keeper/events_test.go @@ -0,0 +1,83 @@ +package keeper_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abcitypes "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types" +) + +func (suite *KeeperTestSuite) TestIncentivizePacketEvent() { + var ( + expRecvFees sdk.Coins + expAckFees sdk.Coins + expTimeoutFees sdk.Coins + ) + + suite.coordinator.Setup(suite.path) + + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + msg := types.NewMsgPayPacketFee( + fee, + suite.path.EndpointA.ChannelConfig.PortID, + suite.path.EndpointA.ChannelID, + suite.chainA.SenderAccount.GetAddress().String(), + nil, + ) + + expRecvFees = expRecvFees.Add(fee.RecvFee...) + expAckFees = expAckFees.Add(fee.AckFee...) + expTimeoutFees = expTimeoutFees.Add(fee.TimeoutFee...) + + result, err := suite.chainA.SendMsgs(msg) + suite.Require().NoError(err) + + var incentivizedPacketEvent abcitypes.Event + for _, event := range result.Events { + if event.Type == types.EventTypeIncentivizedPacket { + incentivizedPacketEvent = event + } + } + + for _, attr := range incentivizedPacketEvent.Attributes { + switch string(attr.Key) { + case types.AttributeKeyRecvFee: + suite.Require().Equal(expRecvFees.String(), string(attr.Value)) + + case types.AttributeKeyAckFee: + suite.Require().Equal(expAckFees.String(), string(attr.Value)) + + case types.AttributeKeyTimeoutFee: + suite.Require().Equal(expTimeoutFees.String(), string(attr.Value)) + } + } + + // send the same messages again a few times + for i := 0; i < 3; i++ { + expRecvFees = expRecvFees.Add(fee.RecvFee...) + expAckFees = expAckFees.Add(fee.AckFee...) + expTimeoutFees = expTimeoutFees.Add(fee.TimeoutFee...) + + result, err = suite.chainA.SendMsgs(msg) + suite.Require().NoError(err) + } + + for _, event := range result.Events { + if event.Type == types.EventTypeIncentivizedPacket { + incentivizedPacketEvent = event + } + } + + for _, attr := range incentivizedPacketEvent.Attributes { + switch string(attr.Key) { + case types.AttributeKeyRecvFee: + suite.Require().Equal(expRecvFees.String(), string(attr.Value)) + + case types.AttributeKeyAckFee: + suite.Require().Equal(expAckFees.String(), string(attr.Value)) + + case types.AttributeKeyTimeoutFee: + suite.Require().Equal(expTimeoutFees.String(), string(attr.Value)) + } + } +}