From acda4e99c60b721ba02c8160f5a3295bfc057d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Mon, 16 May 2022 18:24:30 +0200 Subject: [PATCH 1/4] emit cumulative fees --- modules/apps/29-fee/keeper/escrow.go | 2 +- modules/apps/29-fee/keeper/events.go | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 2c5b570320e..9a02175b62c 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..414e4a8b7ce 100644 --- a/modules/apps/29-fee/keeper/events.go +++ b/modules/apps/29-fee/keeper/events.go @@ -9,17 +9,30 @@ 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 { + totalRecvFees.Add(fee.Fee.RecvFee...) + totalAckFees.Add(fee.Fee.AckFee...) + 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()), ), ) } From dabac815199ff73ba1fa0292725cac1b019b5b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Mon, 16 May 2022 18:40:10 +0200 Subject: [PATCH 2/4] test: add test for emission of cumulative incentivized fees --- modules/apps/29-fee/keeper/events_test.go | 83 +++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 modules/apps/29-fee/keeper/events_test.go 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..ffdb256ac81 --- /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.Add(fee.RecvFee...) + expAckFees.Add(fee.AckFee...) + expAckFees.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.Add(fee.RecvFee...) + expAckFees.Add(fee.AckFee...) + expAckFees.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)) + } + } +} From 87ba07874c3caf1fddc288a24c2d890eec6449c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 17 May 2022 16:35:33 +0200 Subject: [PATCH 3/4] add check for nil relayer --- modules/apps/29-fee/keeper/events.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/apps/29-fee/keeper/events.go b/modules/apps/29-fee/keeper/events.go index 414e4a8b7ce..e5e9b524bf5 100644 --- a/modules/apps/29-fee/keeper/events.go +++ b/modules/apps/29-fee/keeper/events.go @@ -19,9 +19,12 @@ func EmitIncentivizedPacketEvent(ctx sdk.Context, packetID channeltypes.PacketId ) for _, fee := range packetFees.PacketFees { - totalRecvFees.Add(fee.Fee.RecvFee...) - totalAckFees.Add(fee.Fee.AckFee...) - totalTimeoutFees.Add(fee.Fee.TimeoutFee...) + // only emit total fees for packet fees which allow any relayer to relay + if fee.Relayers == nil { + totalRecvFees.Add(fee.Fee.RecvFee...) + totalAckFees.Add(fee.Fee.AckFee...) + totalTimeoutFees.Add(fee.Fee.TimeoutFee...) + } } ctx.EventManager().EmitEvent( From a485264c004a64730de49636a18feb399fb9243c 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 12:16:06 +0200 Subject: [PATCH 4/4] reassign sdk.Coins, fix bug --- modules/apps/29-fee/keeper/events.go | 6 +++--- modules/apps/29-fee/keeper/events_test.go | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/apps/29-fee/keeper/events.go b/modules/apps/29-fee/keeper/events.go index e5e9b524bf5..d60866e21d2 100644 --- a/modules/apps/29-fee/keeper/events.go +++ b/modules/apps/29-fee/keeper/events.go @@ -21,9 +21,9 @@ func EmitIncentivizedPacketEvent(ctx sdk.Context, packetID channeltypes.PacketId for _, fee := range packetFees.PacketFees { // only emit total fees for packet fees which allow any relayer to relay if fee.Relayers == nil { - totalRecvFees.Add(fee.Fee.RecvFee...) - totalAckFees.Add(fee.Fee.AckFee...) - totalTimeoutFees.Add(fee.Fee.TimeoutFee...) + totalRecvFees = totalRecvFees.Add(fee.Fee.RecvFee...) + totalAckFees = totalAckFees.Add(fee.Fee.AckFee...) + totalTimeoutFees = totalTimeoutFees.Add(fee.Fee.TimeoutFee...) } } diff --git a/modules/apps/29-fee/keeper/events_test.go b/modules/apps/29-fee/keeper/events_test.go index ffdb256ac81..8ab52295e73 100644 --- a/modules/apps/29-fee/keeper/events_test.go +++ b/modules/apps/29-fee/keeper/events_test.go @@ -25,9 +25,9 @@ func (suite *KeeperTestSuite) TestIncentivizePacketEvent() { nil, ) - expRecvFees.Add(fee.RecvFee...) - expAckFees.Add(fee.AckFee...) - expAckFees.Add(fee.TimeoutFee...) + 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) @@ -54,9 +54,9 @@ func (suite *KeeperTestSuite) TestIncentivizePacketEvent() { // send the same messages again a few times for i := 0; i < 3; i++ { - expRecvFees.Add(fee.RecvFee...) - expAckFees.Add(fee.AckFee...) - expAckFees.Add(fee.TimeoutFee...) + 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)