Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: emit cumulative fees when incentivizing a packet multiple times #1391

Merged
merged 6 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
}