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

chore: refactor event service to 29-fee #7728

Merged
merged 3 commits into from
Dec 18, 2024
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
12 changes: 7 additions & 5 deletions modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func (k Keeper) escrowPacketFee(ctx context.Context, packetID channeltypes.Packe
packetFees := types.NewPacketFees(fees)
k.SetFeesInEscrow(ctx, packetID, packetFees)

emitIncentivizedPacketEvent(ctx, packetID, packetFees)

return nil
return k.emitIncentivizedPacketEvent(ctx, packetID, packetFees)
}

// DistributePacketFeesOnAcknowledgement pays all the acknowledgement & receive fees for a given packetID while refunding the timeout fees to the refund account.
Expand Down Expand Up @@ -172,9 +170,13 @@ func (k Keeper) distributeFee(ctx context.Context, receiver, refundAccAddress sd
return // if sending to the refund address fails, no-op
}

emitDistributeFeeEvent(ctx, refundAccAddress.String(), fee)
if err := k.emitDistributeFeeEvent(ctx, refundAccAddress.String(), fee); err != nil {
panic(err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

panics are fine here, they won't ever happen right now or the foreseeable future and 29-fee will likely become obsolete

}
} else {
emitDistributeFeeEvent(ctx, receiver.String(), fee)
if err := k.emitDistributeFeeEvent(ctx, receiver.String(), fee); err != nil {
panic(err)
}
}

// write the cache
Expand Down
119 changes: 61 additions & 58 deletions modules/apps/29-fee/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"cosmossdk.io/core/event"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types"
Expand All @@ -12,7 +14,7 @@ import (

// 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 context.Context, packetID channeltypes.PacketId, packetFees types.PacketFees) {
func (k Keeper) emitIncentivizedPacketEvent(ctx context.Context, packetID channeltypes.PacketId, packetFees types.PacketFees) error {
var (
totalRecvFees sdk.Coins
totalAckFees sdk.Coins
Expand All @@ -27,70 +29,71 @@ func emitIncentivizedPacketEvent(ctx context.Context, packetID channeltypes.Pack
totalTimeoutFees = totalTimeoutFees.Add(fee.Fee.TimeoutFee...)
}
}
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
sdkCtx.EventManager().EmitEvents(sdk.Events{
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, totalRecvFees.String()),
sdk.NewAttribute(types.AttributeKeyAckFee, totalAckFees.String()),
sdk.NewAttribute(types.AttributeKeyTimeoutFee, totalTimeoutFees.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
),
})

if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeIncentivizedPacket,
event.NewAttribute(channeltypes.AttributeKeyPortID, packetID.PortId),
event.NewAttribute(channeltypes.AttributeKeyChannelID, packetID.ChannelId),
event.NewAttribute(channeltypes.AttributeKeySequence, fmt.Sprint(packetID.Sequence)),
event.NewAttribute(types.AttributeKeyRecvFee, totalRecvFees.String()),
event.NewAttribute(types.AttributeKeyAckFee, totalAckFees.String()),
event.NewAttribute(types.AttributeKeyTimeoutFee, totalTimeoutFees.String()),
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// emitRegisterPayeeEvent emits an event containing information of a registered payee for a relayer on a particular channel
func emitRegisterPayeeEvent(ctx context.Context, relayer, payee, channelID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterPayee,
sdk.NewAttribute(types.AttributeKeyRelayer, relayer),
sdk.NewAttribute(types.AttributeKeyPayee, payee),
sdk.NewAttribute(types.AttributeKeyChannelID, channelID),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
),
})
func (k Keeper) emitRegisterPayeeEvent(ctx context.Context, relayer, payee, channelID string) error {
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeRegisterPayee,
event.NewAttribute(types.AttributeKeyRelayer, relayer),
event.NewAttribute(types.AttributeKeyPayee, payee),
event.NewAttribute(types.AttributeKeyChannelID, channelID),
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// emitRegisterCounterpartyPayeeEvent emits an event containing information of a registered counterparty payee for a relayer on a particular channel
func emitRegisterCounterpartyPayeeEvent(ctx context.Context, relayer, counterpartyPayee, channelID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterCounterpartyPayee,
sdk.NewAttribute(types.AttributeKeyRelayer, relayer),
sdk.NewAttribute(types.AttributeKeyCounterpartyPayee, counterpartyPayee),
sdk.NewAttribute(types.AttributeKeyChannelID, channelID),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
),
})
func (k Keeper) emitRegisterCounterpartyPayeeEvent(ctx context.Context, relayer, counterpartyPayee, channelID string) error {
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeRegisterCounterpartyPayee,
event.NewAttribute(types.AttributeKeyRelayer, relayer),
event.NewAttribute(types.AttributeKeyCounterpartyPayee, counterpartyPayee),
event.NewAttribute(types.AttributeKeyChannelID, channelID),
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// emitDistributeFeeEvent emits an event containing a distribution fee and receiver address
func emitDistributeFeeEvent(ctx context.Context, receiver string, fee sdk.Coins) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDistributeFee,
sdk.NewAttribute(types.AttributeKeyReceiver, receiver),
sdk.NewAttribute(types.AttributeKeyFee, fee.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
),
})
func (k Keeper) emitDistributeFeeEvent(ctx context.Context, receiver string, fee sdk.Coins) error {
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeDistributeFee,
event.NewAttribute(types.AttributeKeyReceiver, receiver),
event.NewAttribute(types.AttributeKeyFee, fee.String()),
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}
8 changes: 6 additions & 2 deletions modules/apps/29-fee/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func (k Keeper) RegisterPayee(ctx context.Context, msg *types.MsgRegisterPayee)

k.Logger.Info("registering payee address for relayer", "relayer", msg.Relayer, "payee", msg.Payee, "channel", msg.ChannelId)

emitRegisterPayeeEvent(ctx, msg.Relayer, msg.Payee, msg.ChannelId)
if err := k.emitRegisterPayeeEvent(ctx, msg.Relayer, msg.Payee, msg.ChannelId); err != nil {
return nil, err
}

return &types.MsgRegisterPayeeResponse{}, nil
}
Expand All @@ -66,7 +68,9 @@ func (k Keeper) RegisterCounterpartyPayee(ctx context.Context, msg *types.MsgReg

k.Logger.Info("registering counterparty payee for relayer", "relayer", msg.Relayer, "counterparty payee", msg.CounterpartyPayee, "channel", msg.ChannelId)

emitRegisterCounterpartyPayeeEvent(ctx, msg.Relayer, msg.CounterpartyPayee, msg.ChannelId)
if err := k.emitRegisterCounterpartyPayeeEvent(ctx, msg.Relayer, msg.CounterpartyPayee, msg.ChannelId); err != nil {
return nil, err
}

return &types.MsgRegisterCounterpartyPayeeResponse{}, nil
}
Expand Down
Loading