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 4 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.Add(fee.Fee.RecvFee...)
totalAckFees.Add(fee.Fee.AckFee...)
totalTimeoutFees.Add(fee.Fee.TimeoutFee...)
Copy link
Member

@damiannolan damiannolan May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we reassign as coins.Add(...) returns sdk.Coins?

totalRecvFees = totalRecvFees.Add(...)
// ...etc

I think this needs to be done for the singular coin.Add() function. Not 100% sure about Coins but may be good practise to keep consistent and handle return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a good idea to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch 🐛 - the tests were using an expected value of "", the lack of reassigning didn't change the variable values

}
}

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.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))
}
}
}