Skip to content

Commit

Permalink
refactor: emit cumulative fees when incentivizing a packet multiple t…
Browse files Browse the repository at this point in the history
…imes (cosmos#1391)

* emit cumulative fees

* test: add test for emission of cumulative incentivized fees

* add check for nil relayer

* reassign sdk.Coins, fix bug
  • Loading branch information
colin-axner committed May 18, 2022
1 parent b7b4400 commit 2ae4f10
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 21 additions & 5 deletions modules/apps/29-fee/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
)
}
83 changes: 83 additions & 0 deletions modules/apps/29-fee/keeper/events_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
}

0 comments on commit 2ae4f10

Please sign in to comment.