github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/04-channel/keeper/events.go (about) 1 package keeper 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 7 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 8 9 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 10 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/exported" 11 ) 12 13 // EmitSendPacketEvent emits an event with packet data along with other packet information for relayer 14 // to pick up and relay to other chain 15 func EmitSendPacketEvent(ctx sdk.Context, packet exported.PacketI, channel types.Channel, timeoutHeight exported.Height) { 16 ctx.EventManager().EmitEvents(sdk.Events{ 17 sdk.NewEvent( 18 types.EventTypeSendPacket, 19 sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())), // DEPRECATED 20 sdk.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), 21 sdk.NewAttribute(types.AttributeKeyTimeoutHeight, timeoutHeight.String()), 22 sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), 23 sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), 24 sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), 25 sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), 26 sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), 27 sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), 28 sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), 29 // we only support 1-hop packets now, and that is the most important hop for a relayer 30 // (is it going to a chain I am connected to) 31 sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), 32 ), 33 sdk.NewEvent( 34 sdk.EventTypeMessage, 35 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 36 ), 37 }) 38 } 39 40 // EmitRecvPacketEvent emits a receive packet event. It will be emitted both the first time a packet 41 // is received for a certain sequence and for all duplicate receives. 42 func EmitRecvPacketEvent(ctx sdk.Context, packet exported.PacketI, channel types.Channel) { 43 ctx.EventManager().EmitEvents(sdk.Events{ 44 sdk.NewEvent( 45 types.EventTypeRecvPacket, 46 sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())), // DEPRECATED 47 sdk.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), 48 sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), 49 sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), 50 sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), 51 sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), 52 sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), 53 sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), 54 sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), 55 sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), 56 // we only support 1-hop packets now, and that is the most important hop for a relayer 57 // (is it going to a chain I am connected to) 58 sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), 59 ), 60 sdk.NewEvent( 61 sdk.EventTypeMessage, 62 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 63 ), 64 }) 65 } 66 67 // EmitWriteAcknowledgementEvent emits an event that the relayer can query for 68 func EmitWriteAcknowledgementEvent(ctx sdk.Context, packet exported.PacketI, channel types.Channel, acknowledgement []byte) { 69 ctx.EventManager().EmitEvents(sdk.Events{ 70 sdk.NewEvent( 71 types.EventTypeWriteAck, 72 sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())), // DEPRECATED 73 sdk.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), 74 sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), 75 sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), 76 sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), 77 sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), 78 sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), 79 sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), 80 sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), 81 sdk.NewAttribute(types.AttributeKeyAck, string(acknowledgement)), 82 sdk.NewAttribute(types.AttributeKeyAckHex, hex.EncodeToString(acknowledgement)), 83 // we only support 1-hop packets now, and that is the most important hop for a relayer 84 // (is it going to a chain I am connected to) 85 sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), 86 ), 87 sdk.NewEvent( 88 sdk.EventTypeMessage, 89 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 90 ), 91 }) 92 } 93 94 // EmitAcknowledgePacketEvent emits an acknowledge packet event. It will be emitted both the first time 95 // a packet is acknowledged for a certain sequence and for all duplicate acknowledgements. 96 func EmitAcknowledgePacketEvent(ctx sdk.Context, packet exported.PacketI, channel types.Channel) { 97 ctx.EventManager().EmitEvents(sdk.Events{ 98 sdk.NewEvent( 99 types.EventTypeAcknowledgePacket, 100 sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), 101 sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), 102 sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), 103 sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), 104 sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), 105 sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), 106 sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), 107 sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), 108 // we only support 1-hop packets now, and that is the most important hop for a relayer 109 // (is it going to a chain I am connected to) 110 sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), 111 ), 112 sdk.NewEvent( 113 sdk.EventTypeMessage, 114 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 115 ), 116 }) 117 } 118 119 // EmitTimeoutPacketEvent emits a timeout packet event. It will be emitted both the first time a packet 120 // is timed out for a certain sequence and for all duplicate timeouts. 121 func EmitTimeoutPacketEvent(ctx sdk.Context, packet exported.PacketI, channel types.Channel) { 122 ctx.EventManager().EmitEvents(sdk.Events{ 123 sdk.NewEvent( 124 types.EventTypeTimeoutPacket, 125 sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), 126 sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), 127 sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), 128 sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), 129 sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), 130 sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), 131 sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), 132 sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), 133 ), 134 sdk.NewEvent( 135 sdk.EventTypeMessage, 136 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 137 ), 138 }) 139 } 140 141 // EmitChannelOpenInitEvent emits a channel open init event 142 func EmitChannelOpenInitEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { 143 ctx.EventManager().EmitEvents(sdk.Events{ 144 sdk.NewEvent( 145 types.EventTypeChannelOpenInit, 146 sdk.NewAttribute(types.AttributeKeyPortID, portID), 147 sdk.NewAttribute(types.AttributeKeyChannelID, channelID), 148 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 149 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 150 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 151 sdk.NewAttribute(types.AttributeVersion, channel.Version), 152 ), 153 }) 154 155 ctx.EventManager().EmitEvents(sdk.Events{ 156 sdk.NewEvent( 157 sdk.EventTypeMessage, 158 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 159 ), 160 }) 161 } 162 163 // EmitChannelOpenTryEvent emits a channel open try event 164 func EmitChannelOpenTryEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { 165 ctx.EventManager().EmitEvents(sdk.Events{ 166 sdk.NewEvent( 167 types.EventTypeChannelOpenTry, 168 sdk.NewAttribute(types.AttributeKeyPortID, portID), 169 sdk.NewAttribute(types.AttributeKeyChannelID, channelID), 170 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 171 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 172 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 173 sdk.NewAttribute(types.AttributeVersion, channel.Version), 174 ), 175 }) 176 ctx.EventManager().EmitEvents(sdk.Events{ 177 sdk.NewEvent( 178 sdk.EventTypeMessage, 179 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 180 ), 181 }) 182 } 183 184 // EmitChannelOpenAckEvent emits a channel open acknowledge event 185 func EmitChannelOpenAckEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { 186 ctx.EventManager().EmitEvents(sdk.Events{ 187 sdk.NewEvent( 188 types.EventTypeChannelOpenAck, 189 sdk.NewAttribute(types.AttributeKeyPortID, portID), 190 sdk.NewAttribute(types.AttributeKeyChannelID, channelID), 191 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 192 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 193 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 194 ), 195 sdk.NewEvent( 196 sdk.EventTypeMessage, 197 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 198 ), 199 }) 200 } 201 202 // EmitChannelOpenConfirmEvent emits a channel open confirm event 203 func EmitChannelOpenConfirmEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { 204 ctx.EventManager().EmitEvents(sdk.Events{ 205 sdk.NewEvent( 206 types.EventTypeChannelOpenConfirm, 207 sdk.NewAttribute(types.AttributeKeyPortID, portID), 208 sdk.NewAttribute(types.AttributeKeyChannelID, channelID), 209 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 210 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 211 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 212 ), 213 sdk.NewEvent( 214 sdk.EventTypeMessage, 215 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 216 ), 217 }) 218 } 219 220 // EmitChannelCloseInitEvent emits a channel close init event 221 func EmitChannelCloseInitEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { 222 ctx.EventManager().EmitEvents(sdk.Events{ 223 sdk.NewEvent( 224 types.EventTypeChannelCloseInit, 225 sdk.NewAttribute(types.AttributeKeyPortID, portID), 226 sdk.NewAttribute(types.AttributeKeyChannelID, channelID), 227 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 228 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 229 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 230 ), 231 sdk.NewEvent( 232 sdk.EventTypeMessage, 233 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 234 ), 235 }) 236 } 237 238 // EmitChannelCloseConfirmEvent emits a channel close confirm event 239 func EmitChannelCloseConfirmEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { 240 ctx.EventManager().EmitEvents(sdk.Events{ 241 sdk.NewEvent( 242 types.EventTypeChannelCloseConfirm, 243 sdk.NewAttribute(types.AttributeKeyPortID, portID), 244 sdk.NewAttribute(types.AttributeKeyChannelID, channelID), 245 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 246 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 247 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 248 ), 249 sdk.NewEvent( 250 sdk.EventTypeMessage, 251 sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), 252 ), 253 }) 254 } 255 256 // EmitChannelClosedEvent emits a channel closed event. 257 func EmitChannelClosedEvent(ctx sdk.Context, packet exported.PacketI, channel types.Channel) { 258 ctx.EventManager().EmitEvents(sdk.Events{ 259 sdk.NewEvent( 260 types.EventTypeChannelClosed, 261 sdk.NewAttribute(types.AttributeKeyPortID, packet.GetSourcePort()), 262 sdk.NewAttribute(types.AttributeKeyChannelID, packet.GetSourceChannel()), 263 sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), 264 sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), 265 sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), 266 sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), 267 ), 268 }) 269 }