Skip to content

Commit

Permalink
chore: add events for send packet (#7577)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim authored Nov 21, 2024
1 parent 5332645 commit f3afb2c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
45 changes: 42 additions & 3 deletions modules/core/04-channel/v2/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,54 @@ package keeper

import (
"context"
"encoding/hex"
"fmt"

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

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
)

// EmitSendPacketEvents emits events for the SendPacket handler.
func EmitSendPacketEvents(ctx context.Context, packet types.Packet) {
// TODO: https://github.com/cosmos/ibc-go/issues/7386
// emitSendPacketEvents emits events for the SendPacket handler.
func emitSendPacketEvents(ctx context.Context, packet types.Packet) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendPacket,
sdk.NewAttribute(types.AttributeKeySrcChannel, packet.SourceChannel),
sdk.NewAttribute(types.AttributeKeyDstChannel, packet.DestinationChannel),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.Sequence)),
sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.TimeoutTimestamp)),
sdk.NewAttribute(types.AttributeKeyPayloadLength, fmt.Sprintf("%d", len(packet.Payloads))),
sdk.NewAttribute(types.AttributeKeyVersion, packet.Payloads[0].Version),
sdk.NewAttribute(types.AttributeKeyEncoding, packet.Payloads[0].Encoding),
sdk.NewAttribute(types.AttributeKeyData, hex.EncodeToString(packet.Payloads[0].Value)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})

for i, payload := range packet.Payloads {
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendPayload,
sdk.NewAttribute(types.AttributeKeySrcChannel, packet.SourceChannel),
sdk.NewAttribute(types.AttributeKeyDstChannel, packet.DestinationChannel),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.Sequence)),
sdk.NewAttribute(types.AttributeKeyPayloadSequence, fmt.Sprintf("%d", i)),
sdk.NewAttribute(types.AttributeKeyVersion, payload.Version),
sdk.NewAttribute(types.AttributeKeyEncoding, payload.Encoding),
sdk.NewAttribute(types.AttributeKeyData, hex.EncodeToString(payload.Value)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}
}

// EmitRecvPacketEvents emits events for the RecvPacket handler.
Expand Down
2 changes: 1 addition & 1 deletion modules/core/04-channel/v2/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (k *Keeper) sendPacket(

k.Logger(ctx).Info("packet sent", "sequence", strconv.FormatUint(packet.Sequence, 10), "dest_channel_id", packet.DestinationChannel, "src_channel_id", packet.SourceChannel)

EmitSendPacketEvents(ctx, packet)
emitSendPacketEvents(ctx, packet)

return sequence, destChannel, nil
}
Expand Down
17 changes: 14 additions & 3 deletions modules/core/04-channel/v2/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ import (

// IBC channel events
const (
EventTypeCreateChannel = "create_channel"
EventTypeRegisterCounterparty = "register_counterparty"
EventTypeSendPacket = "send_packet"
EventTypeSendPayload = "send_payload"

AttributeKeyChannelID = "channel_id"
AttributeKeyClientID = "client_id"
AttributeKeyCounterpartyChannelID = "counterparty_channel_id"
AttributeKeySrcChannel = "packet_source_channel"
AttributeKeyDstChannel = "packet_dest_channel"
AttributeKeySequence = "packet_sequence"
AttributeKeyTimeoutTimestamp = "packet_timeout_timestamp"
AttributeKeyPayloadLength = "packet_payload_length"
AttributeKeyPayloadSequence = "payload_sequence"
AttributeKeyVersion = "payload_version"
AttributeKeyEncoding = "payload_encoding"
AttributeKeyData = "payload_data"
)

// IBC channel events vars
var (
EventTypeCreateChannel = "create_channel"
EventTypeRegisterCounterparty = "register_counterparty"

AttributeValueCategory = fmt.Sprintf("%s_%s", ibcexported.ModuleName, SubModuleName)
)

0 comments on commit f3afb2c

Please sign in to comment.