Skip to content

Commit

Permalink
imp: added simpler events (#7613)
Browse files Browse the repository at this point in the history
  • Loading branch information
srdtrk authored Dec 3, 2024
1 parent ce8dcf2 commit 3864793
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 68 deletions.
142 changes: 88 additions & 54 deletions modules/core/04-channel/v2/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/hex"
"fmt"

"github.com/cosmos/gogoproto/proto"

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

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
Expand All @@ -14,99 +16,131 @@ import (
func emitSendPacketEvents(ctx context.Context, packet types.Packet) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

encodedPacket, err := proto.Marshal(&packet)
if err != nil {
panic(err)
}

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.NewAttribute(types.AttributeKeyPacketDataHex, hex.EncodeToString(encodedPacket)),
),
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.
func emitRecvPacketEvents(ctx context.Context, packet types.Packet) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

encodedPacket, err := proto.Marshal(&packet)
if err != nil {
panic(err)
}

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRecvPacket,
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.NewAttribute(types.AttributeKeyPacketDataHex, hex.EncodeToString(encodedPacket)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}

for i, payload := range packet.Payloads {
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRecvPayload,
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),
),
})
// emitWriteAcknowledgementEvents emits events for WriteAcknowledgement.
func emitWriteAcknowledgementEvents(ctx context.Context, packet types.Packet, ack types.Acknowledgement) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

encodedPacket, err := proto.Marshal(&packet)
if err != nil {
panic(err)
}
}

// EmitWriteAcknowledgementEvents emits events for WriteAcknowledgement.
func EmitWriteAcknowledgementEvents(ctx context.Context, packet types.Packet, ack types.Acknowledgement) {
// TODO: https://github.com/cosmos/ibc-go/issues/7386
encodedAck, err := proto.Marshal(&ack)
if err != nil {
panic(err)
}

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeWriteAck,
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.AttributeKeyPacketDataHex, hex.EncodeToString(encodedPacket)),
sdk.NewAttribute(types.AttributeKeyAckDataHex, hex.EncodeToString(encodedAck)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}

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

encodedPacket, err := proto.Marshal(&packet)
if err != nil {
panic(err)
}

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAcknowledgePacket,
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.AttributeKeyPacketDataHex, hex.EncodeToString(encodedPacket)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}

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

encodedPacket, err := proto.Marshal(&packet)
if err != nil {
panic(err)
}

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeTimeoutPacket,
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.AttributeKeyPacketDataHex, hex.EncodeToString(encodedPacket)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}

// emitCreateChannelEvent emits a channel create event.
Expand Down
11 changes: 5 additions & 6 deletions modules/core/04-channel/v2/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ func (k Keeper) WriteAcknowledgement(

k.Logger(ctx).Info("acknowledgement written", "sequence", strconv.FormatUint(packet.Sequence, 10), "dest-channel", packet.DestinationChannel)

// TODO: decide how relayers will reconstruct the packet as it is not being passed.
// EmitWriteAcknowledgementEvents(ctx, packet, ack)
emitWriteAcknowledgementEvents(ctx, packet, ack)

// TODO: delete the packet that has been stored in ibc-core.

Expand All @@ -230,7 +229,7 @@ func (k *Keeper) acknowledgePacket(ctx context.Context, packet types.Packet, ack
commitment := k.GetPacketCommitment(ctx, packet.SourceChannel, packet.Sequence)
if len(commitment) == 0 {
// TODO: signal noop in events?
EmitAcknowledgePacketEvents(ctx, packet)
emitAcknowledgePacketEvents(ctx, packet)

// This error indicates that the acknowledgement has already been relayed
// or there is a misconfigured relayer attempting to prove an acknowledgement
Expand Down Expand Up @@ -265,7 +264,7 @@ func (k *Keeper) acknowledgePacket(ctx context.Context, packet types.Packet, ack

k.Logger(ctx).Info("packet acknowledged", "sequence", strconv.FormatUint(packet.GetSequence(), 10), "source_channel_id", packet.GetSourceChannel(), "destination_channel_id", packet.GetDestinationChannel())

EmitAcknowledgePacketEvents(ctx, packet)
emitAcknowledgePacketEvents(ctx, packet)

return nil
}
Expand Down Expand Up @@ -308,7 +307,7 @@ func (k *Keeper) timeoutPacket(
// check that the commitment has not been cleared and that it matches the packet sent by relayer
commitment := k.GetPacketCommitment(ctx, packet.SourceChannel, packet.Sequence)
if len(commitment) == 0 {
EmitTimeoutPacketEvents(ctx, packet)
emitTimeoutPacketEvents(ctx, packet)
// This error indicates that the timeout has already been relayed
// or there is a misconfigured relayer attempting to prove a timeout
// for a packet never sent. Core IBC will treat this error as a no-op in order to
Expand Down Expand Up @@ -342,7 +341,7 @@ func (k *Keeper) timeoutPacket(

k.Logger(ctx).Info("packet timed out", "sequence", strconv.FormatUint(packet.Sequence, 10), "src_channel_id", packet.SourceChannel, "dst_channel_id", packet.DestinationChannel)

EmitTimeoutPacketEvents(ctx, packet)
emitTimeoutPacketEvents(ctx, packet)

return nil
}
13 changes: 5 additions & 8 deletions modules/core/04-channel/v2/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const (
EventTypeRegisterCounterparty = "register_counterparty"
EventTypeSendPacket = "send_packet"
EventTypeRecvPacket = "recv_packet"

EventTypeSendPayload = "send_payload"
EventTypeRecvPayload = "recv_payload"
EventTypeTimeoutPacket = "timeout_packet"
EventTypeAcknowledgePacket = "acknowledge_packet"
EventTypeWriteAck = "write_acknowledgement"

AttributeKeyChannelID = "channel_id"
AttributeKeyClientID = "client_id"
Expand All @@ -23,11 +23,8 @@ const (
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"
AttributeKeyPacketDataHex = "packet_data_hex"
AttributeKeyAckDataHex = "acknowledgement_data_hex"
)

// IBC channel events vars
Expand Down

0 comments on commit 3864793

Please sign in to comment.