Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

event.gno

15.58 Kb · 368 lines
  1package core
  2
  3import (
  4	"chain"
  5	"strconv"
  6
  7	"gno.land/p/onbloc/ibc/union/types"
  8)
  9
 10const (
 11	AppRegisteredEvent         = "AppRegistered"
 12	RegisterClientEvent        = "RegisterClient"
 13	CreateClientEvent          = "CreateClient"
 14	UpdateClientEvent          = "UpdateClient"
 15	ForceUpdateClientEvent     = "ForceUpdateClient"
 16	MisbehaviourEvent          = "Misbehaviour"
 17	ConnectionOpenInitEvent    = "ConnectionOpenInit"
 18	ConnectionOpenTryEvent     = "ConnectionOpenTry"
 19	ConnectionOpenAckEvent     = "ConnectionOpenAck"
 20	ConnectionOpenConfirmEvent = "ConnectionOpenConfirm"
 21	ChannelOpenInitEvent       = "ChannelOpenInit"
 22	ChannelOpenTryEvent        = "ChannelOpenTry"
 23	ChannelOpenAckEvent        = "ChannelOpenAck"
 24	ChannelOpenConfirmEvent    = "ChannelOpenConfirm"
 25	ChannelCloseInitEvent      = "ChannelCloseInit"
 26	ChannelCloseConfirmEvent   = "ChannelCloseConfirm"
 27	PacketSendEvent            = "PacketSend"
 28	BatchSendEvent             = "BatchSend"
 29	BatchAcksEvent             = "BatchAcks"
 30	PacketRecvEvent            = "PacketRecv"
 31	IntentPacketRecvEvent      = "IntentPacketRecv"
 32	WriteAckEvent              = "WriteAck"
 33	PacketAckEvent             = "PacketAck"
 34	PacketTimeoutEvent         = "PacketTimeout"
 35	CommitMembershipProofEvent = "CommitMembershipProof"
 36	CommitNonMembershipEvent   = "CommitNonMembershipProof"
 37)
 38
 39func channelOpenInitEventAttrs(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) []string {
 40	return []string{
 41		"port_id", string(portId),
 42		"channel_id", channelId.String(),
 43		"counterparty_port_id", channel.CounterpartyPortId.String(),
 44		"connection_id", channel.ConnectionId.String(),
 45		"connection_client_id", connection.ClientId.String(),
 46		"connection_counterparty_client_id", connection.CounterpartyClientId.String(),
 47		"connection_counterparty_connection_id", connection.CounterpartyConnectionId.String(),
 48		"version", channel.Version,
 49	}
 50}
 51
 52func channelOpenTryEventAttrs(portId types.Bytes, channelId types.ChannelId, channel types.Channel, counterpartyVersion string, connection types.Connection) []string {
 53	return []string{
 54		"port_id", string(portId),
 55		"channel_id", channelId.String(),
 56		"counterparty_port_id", channel.CounterpartyPortId.String(),
 57		"counterparty_channel_id", channel.CounterpartyChannelId.String(),
 58		"connection_id", channel.ConnectionId.String(),
 59		"connection_client_id", connection.ClientId.String(),
 60		"connection_counterparty_client_id", connection.CounterpartyClientId.String(),
 61		"connection_counterparty_connection_id", connection.CounterpartyConnectionId.String(),
 62		"version", channel.Version,
 63		"counterparty_version", counterpartyVersion,
 64	}
 65}
 66
 67func channelOpenAckConfirmEventAttrs(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) []string {
 68	return []string{
 69		"port_id", string(portId),
 70		"channel_id", channelId.String(),
 71		"counterparty_port_id", channel.CounterpartyPortId.String(),
 72		"counterparty_channel_id", channel.CounterpartyChannelId.String(),
 73		"connection_id", channel.ConnectionId.String(),
 74		"connection_client_id", connection.ClientId.String(),
 75		"connection_counterparty_client_id", connection.CounterpartyClientId.String(),
 76		"connection_counterparty_connection_id", connection.CounterpartyConnectionId.String(),
 77		"version", channel.Version,
 78	}
 79}
 80
 81func channelCloseEventAttrs(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) []string {
 82	return []string{
 83		"port_id", string(portId),
 84		"channel_id", channelId.String(),
 85		"counterparty_port_id", channel.CounterpartyPortId.String(),
 86		"counterparty_channel_id", channel.CounterpartyChannelId.String(),
 87		"connection_id", channel.ConnectionId.String(),
 88		"connection_client_id", connection.ClientId.String(),
 89		"connection_counterparty_client_id", connection.CounterpartyClientId.String(),
 90		"connection_counterparty_connection_id", connection.CounterpartyConnectionId.String(),
 91		"version", channel.Version,
 92	}
 93}
 94
 95func emitChannelOpenInitEvent(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) {
 96	chain.Emit(ChannelOpenInitEvent, channelOpenInitEventAttrs(portId, channelId, channel, connection)...)
 97}
 98
 99func emitChannelOpenTryEvent(portId types.Bytes, channelId types.ChannelId, channel types.Channel, counterpartyVersion string, connection types.Connection) {
100	chain.Emit(ChannelOpenTryEvent, channelOpenTryEventAttrs(portId, channelId, channel, counterpartyVersion, connection)...)
101}
102
103func emitChannelOpenAckEvent(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) {
104	chain.Emit(ChannelOpenAckEvent, channelOpenAckConfirmEventAttrs(portId, channelId, channel, connection)...)
105}
106
107func emitChannelOpenConfirmEvent(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) {
108	chain.Emit(ChannelOpenConfirmEvent, channelOpenAckConfirmEventAttrs(portId, channelId, channel, connection)...)
109}
110
111func emitChannelCloseInitEvent(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) {
112	chain.Emit(ChannelCloseInitEvent, channelCloseEventAttrs(portId, channelId, channel, connection)...)
113}
114
115func emitChannelCloseConfirmEvent(portId types.Bytes, channelId types.ChannelId, channel types.Channel, connection types.Connection) {
116	chain.Emit(ChannelCloseConfirmEvent, channelCloseEventAttrs(portId, channelId, channel, connection)...)
117}
118
119func emitConnectionOpenInitEvent(connectionId types.ConnectionId, clientId types.ClientId, counterpartyClientId types.ClientId) {
120	chain.Emit(
121		ConnectionOpenInitEvent,
122		"connection_id", connectionId.String(),
123		"client_id", clientId.String(),
124		"counterparty_client_id", counterpartyClientId.String(),
125	)
126}
127
128func emitConnectionOpenTryEvent(connectionId types.ConnectionId, clientId types.ClientId, counterpartyClientId types.ClientId, counterpartyConnectionId types.ConnectionId) {
129	chain.Emit(ConnectionOpenTryEvent, connectionOpenEventAttrs(connectionId, clientId, counterpartyClientId, counterpartyConnectionId)...)
130}
131
132func emitConnectionOpenAckEvent(connectionId types.ConnectionId, clientId types.ClientId, counterpartyClientId types.ClientId, counterpartyConnectionId types.ConnectionId) {
133	chain.Emit(ConnectionOpenAckEvent, connectionOpenEventAttrs(connectionId, clientId, counterpartyClientId, counterpartyConnectionId)...)
134}
135
136func emitConnectionOpenConfirmEvent(connectionId types.ConnectionId, clientId types.ClientId, counterpartyClientId types.ClientId, counterpartyConnectionId types.ConnectionId) {
137	chain.Emit(ConnectionOpenConfirmEvent, connectionOpenEventAttrs(connectionId, clientId, counterpartyClientId, counterpartyConnectionId)...)
138}
139
140func connectionOpenEventAttrs(connectionId types.ConnectionId, clientId types.ClientId, counterpartyClientId types.ClientId, counterpartyConnectionId types.ConnectionId) []string {
141	return []string{
142		"connection_id", connectionId.String(),
143		"client_id", clientId.String(),
144		"counterparty_client_id", counterpartyClientId.String(),
145		"counterparty_connection_id", counterpartyConnectionId.String(),
146	}
147}
148
149func emitRegisterClientEvent(clientType types.ClientType) {
150	chain.Emit(
151		RegisterClientEvent,
152		"client_type", clientType.String(),
153	)
154}
155
156func emitCreateClientEvent(clientId types.ClientId, clientType types.ClientType, counterpartyChainId string) {
157	chain.Emit(
158		CreateClientEvent,
159		"client_id", clientId.String(),
160		"client_type", clientType.String(),
161		"counterparty_chain_id", counterpartyChainId,
162	)
163}
164
165func emitUpdateClientEvent(clientId types.ClientId, height types.Height) {
166	chain.Emit(
167		UpdateClientEvent,
168		"client_id", clientId.String(),
169		"height", height.String(),
170	)
171}
172
173func emitForceUpdateClientEvent(clientId types.ClientId, height types.Height) {
174	chain.Emit(
175		ForceUpdateClientEvent,
176		"client_id", clientId.String(),
177		"height", height.String(),
178	)
179}
180
181func emitMisbehaviourEvent(clientId types.ClientId) {
182	chain.Emit(
183		MisbehaviourEvent,
184		"client_id", clientId.String(),
185	)
186}
187
188func emitAppRegisteredEvent(portId []byte, registrarPkgPath string, adminOverride bool) {
189	chain.Emit(
190		AppRegisteredEvent,
191		"port_id", string(portId),
192		"registrar_pkg_path", registrarPkgPath,
193		"admin_override", strconv.FormatBool(adminOverride),
194	)
195}
196
197func emitBatchSendEvent(channelId types.ChannelId, packetHash, batchHash types.H256) {
198	chain.Emit(
199		BatchSendEvent,
200		"channel_id", channelId.String(),
201		"packet_hash", packetHash.String(),
202		"batch_hash", batchHash.String(),
203	)
204}
205
206func emitBatchAcksEvent(channelId types.ChannelId, packetHash, batchHash types.H256) {
207	chain.Emit(
208		BatchAcksEvent,
209		"channel_id", channelId.String(),
210		"packet_hash", packetHash.String(),
211		"batch_hash", batchHash.String(),
212	)
213}
214
215// encodedEventAttrs returns the event attrs for an already-encoded value,
216// splitting it into indexed columns when SplitEventAttrs is enabled.
217func encodedEventAttrs(key string, value string) []string {
218	config := GetEventConfig()
219	if !config.SplitEventAttrs {
220		return []string{key, value}
221	}
222
223	attrs := []string{key + "_size", strconv.Itoa(len(value))}
224	return append(attrs, eventAttrColumns(key, value, int(config.MaxEventAttrValueLen))...)
225}
226
227// eventAttrColumns splits one encoded event attribute value into indexed
228// columns. Callers decide whether splitting is enabled before calling this
229// helper.
230func eventAttrColumns(key string, value string, maxValueLen int) []string {
231	if maxValueLen <= 0 {
232		panic("max event attr value len must be positive")
233	}
234
235	if len(value) == 0 {
236		return []string{key + "[0]", ""}
237	}
238
239	parts := (len(value) + maxValueLen - 1) / maxValueLen
240	attrs := make([]string, 0, parts*2)
241	for index := 0; index < parts; index++ {
242		start := index * maxValueLen
243		end := start + maxValueLen
244		if end > len(value) {
245			end = len(value)
246		}
247		attrs = append(attrs, key+"["+strconv.Itoa(index)+"]", value[start:end])
248	}
249
250	return attrs
251}
252
253func packetSendEventAttrs(packet types.Packet, sourceChannel types.Channel, sourceConnection types.Connection) []string {
254	attrs := []string{
255		"packet_hash", commitPacketHash(packet).String(),
256		"source_channel_id", packet.SourceChannelId.String(),
257		"source_channel_version", sourceChannel.Version,
258		"source_connection_id", sourceChannel.ConnectionId.String(),
259		"source_connection_client_id", sourceConnection.ClientId.String(),
260		"destination_channel_id", packet.DestinationChannelId.String(),
261		"destination_connection_id", sourceConnection.CounterpartyConnectionId.String(),
262		"destination_connection_client_id", sourceConnection.CounterpartyClientId.String(),
263		"timeout_timestamp", packet.TimeoutTimestamp.String(),
264	}
265	return append(attrs, encodedEventAttrs("packet_data", hexAttr(packet.Data))...)
266}
267
268func emitPacketSendEvent(packet types.Packet, sourceChannel types.Channel, sourceConnection types.Connection) {
269	chain.Emit(PacketSendEvent, packetSendEventAttrs(packet, sourceChannel, sourceConnection)...)
270}
271
272func packetRecvEventAttrs(packet types.Packet, destinationChannel types.Channel, connection types.Connection, msg []byte) []string {
273	attrs := []string{
274		"packet_hash", commitPacketHash(packet).String(),
275		"source_channel_id", packet.SourceChannelId.String(),
276		"source_connection_id", connection.CounterpartyConnectionId.String(),
277		"source_connection_client_id", connection.CounterpartyClientId.String(),
278		"destination_channel_id", packet.DestinationChannelId.String(),
279		"destination_channel_version", destinationChannel.Version,
280		"destination_connection_id", destinationChannel.ConnectionId.String(),
281		"destination_connection_client_id", connection.ClientId.String(),
282		"timeout_timestamp", packet.TimeoutTimestamp.String(),
283	}
284	attrs = append(attrs, encodedEventAttrs("packet_data", hexAttr(packet.Data))...)
285	return append(attrs, encodedEventAttrs("maker_msg", hexAttr(msg))...)
286}
287
288func emitPacketRecvEvent(eventType string, packet types.Packet, destinationChannel types.Channel, connection types.Connection, msg []byte) {
289	chain.Emit(eventType, packetRecvEventAttrs(packet, destinationChannel, connection, msg)...)
290}
291
292func writeAckEventAttrs(packet types.Packet, destinationChannel types.Channel, connection types.Connection, acknowledgement []byte) []string {
293	attrs := []string{
294		"packet_hash", commitPacketHash(packet).String(),
295		"source_channel_id", packet.SourceChannelId.String(),
296		"source_connection_id", connection.CounterpartyConnectionId.String(),
297		"source_connection_client_id", connection.CounterpartyClientId.String(),
298		"destination_channel_id", packet.DestinationChannelId.String(),
299		"destination_channel_version", destinationChannel.Version,
300		"destination_connection_id", destinationChannel.ConnectionId.String(),
301		"destination_connection_client_id", connection.ClientId.String(),
302		"timeout_timestamp", packet.TimeoutTimestamp.String(),
303	}
304	attrs = append(attrs, encodedEventAttrs("packet_data", hexAttr(packet.Data))...)
305	return append(attrs, encodedEventAttrs("acknowledgement", hexAttr(acknowledgement))...)
306}
307
308func emitWriteAckEvent(packet types.Packet, destinationChannel types.Channel, connection types.Connection, acknowledgement []byte) {
309	chain.Emit(WriteAckEvent, writeAckEventAttrs(packet, destinationChannel, connection, acknowledgement)...)
310}
311
312func packetAckEventAttrs(packet types.Packet, sourceChannel types.Channel, connection types.Connection, acknowledgement []byte) []string {
313	attrs := []string{
314		"packet_hash", commitPacketHash(packet).String(),
315		"source_channel_id", packet.SourceChannelId.String(),
316		"source_channel_version", sourceChannel.Version,
317		"source_connection_id", sourceChannel.ConnectionId.String(),
318		"source_connection_client_id", connection.ClientId.String(),
319		"destination_channel_id", packet.DestinationChannelId.String(),
320		"destination_connection_id", connection.CounterpartyConnectionId.String(),
321		"destination_connection_client_id", connection.CounterpartyClientId.String(),
322		"timeout_timestamp", packet.TimeoutTimestamp.String(),
323	}
324	attrs = append(attrs, encodedEventAttrs("packet_data", hexAttr(packet.Data))...)
325	return append(attrs, encodedEventAttrs("acknowledgement", hexAttr(acknowledgement))...)
326}
327
328func emitPacketAckEvent(packet types.Packet, sourceChannel types.Channel, connection types.Connection, acknowledgement []byte) {
329	chain.Emit(PacketAckEvent, packetAckEventAttrs(packet, sourceChannel, connection, acknowledgement)...)
330}
331
332func packetTimeoutEventAttrs(packet types.Packet, sourceChannel types.Channel, connection types.Connection) []string {
333	attrs := []string{
334		"packet_hash", commitPacketHash(packet).String(),
335		"source_channel_id", packet.SourceChannelId.String(),
336		"source_channel_version", sourceChannel.Version,
337		"source_connection_id", sourceChannel.ConnectionId.String(),
338		"source_connection_client_id", connection.ClientId.String(),
339		"destination_channel_id", packet.DestinationChannelId.String(),
340		"destination_connection_id", connection.CounterpartyConnectionId.String(),
341		"destination_connection_client_id", connection.CounterpartyClientId.String(),
342		"timeout_timestamp", packet.TimeoutTimestamp.String(),
343	}
344	return append(attrs, encodedEventAttrs("packet_data", hexAttr(packet.Data))...)
345}
346
347func emitPacketTimeoutEvent(packet types.Packet, sourceChannel types.Channel, connection types.Connection) {
348	chain.Emit(PacketTimeoutEvent, packetTimeoutEventAttrs(packet, sourceChannel, connection)...)
349}
350
351func emitCommitMembershipProofEvent(msg types.MsgCommitMembershipProof) {
352	chain.Emit(
353		CommitMembershipProofEvent,
354		"client_id", msg.ClientId.String(),
355		"proof_height", strconv.FormatUint(msg.ProofHeight, 10),
356		"path", hexAttr(msg.Path),
357		"value", hexAttr(msg.Value),
358	)
359}
360
361func emitCommitNonMembershipProofEvent(msg types.MsgCommitNonMembershipProof) {
362	chain.Emit(
363		CommitNonMembershipEvent,
364		"client_id", msg.ClientId.String(),
365		"proof_height", strconv.FormatUint(msg.ProofHeight, 10),
366		"path", hexAttr(msg.Path),
367	)
368}