Skip to content
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
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),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

safe to access zero element, we assert length of payloads is one in validation for send packet.

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 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

adding these here but could be factored out if there's a strong preference.

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