forked from cosmos/relayer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_parser.go
336 lines (311 loc) · 10.4 KB
/
event_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package cosmos
import (
"encoding/hex"
"strconv"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v4/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
"github.com/defund-labs/relayer/v3/relayer/provider"
abci "github.com/tendermint/tendermint/abci/types"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// ibcMessage is the type used for parsing all possible properties of IBC messages
type ibcMessage struct {
eventType string
info ibcMessageInfo
}
type ibcMessageInfo interface {
parseAttrs(log *zap.Logger, attrs []sdk.Attribute)
MarshalLogObject(enc zapcore.ObjectEncoder) error
}
// ibcMessagesFromTransaction parses all events within a transaction to find IBC messages
func (ccp *CosmosChainProcessor) ibcMessagesFromTransaction(tx *abci.ResponseDeliverTx, height uint64) []ibcMessage {
parsedLogs, err := sdk.ParseABCILogs(tx.Log)
if err != nil {
ccp.log.Info("Failed to parse abci logs", zap.Error(err))
return nil
}
return parseABCILogs(ccp.log, parsedLogs, height)
}
func parseABCILogs(log *zap.Logger, logs sdk.ABCIMessageLogs, height uint64) (messages []ibcMessage) {
for _, messageLog := range logs {
var info ibcMessageInfo
var eventType string
var packetAccumulator *packetInfo
for _, event := range messageLog.Events {
switch event.Type {
case clienttypes.EventTypeCreateClient, clienttypes.EventTypeUpdateClient,
clienttypes.EventTypeUpgradeClient, clienttypes.EventTypeSubmitMisbehaviour,
clienttypes.EventTypeUpdateClientProposal:
clientInfo := new(clientInfo)
clientInfo.parseAttrs(log, event.Attributes)
info = clientInfo
eventType = event.Type
case chantypes.EventTypeSendPacket, chantypes.EventTypeRecvPacket,
chantypes.EventTypeAcknowledgePacket, chantypes.EventTypeTimeoutPacket,
chantypes.EventTypeTimeoutPacketOnClose, chantypes.EventTypeWriteAck:
if packetAccumulator == nil {
packetAccumulator = &packetInfo{Height: height}
}
packetAccumulator.parseAttrs(log, event.Attributes)
info = packetAccumulator
if event.Type != chantypes.EventTypeWriteAck {
eventType = event.Type
}
case conntypes.EventTypeConnectionOpenInit, conntypes.EventTypeConnectionOpenTry,
conntypes.EventTypeConnectionOpenAck, conntypes.EventTypeConnectionOpenConfirm:
connectionInfo := &connectionInfo{Height: height}
connectionInfo.parseAttrs(log, event.Attributes)
info = connectionInfo
eventType = event.Type
case chantypes.EventTypeChannelOpenInit, chantypes.EventTypeChannelOpenTry,
chantypes.EventTypeChannelOpenAck, chantypes.EventTypeChannelOpenConfirm,
chantypes.EventTypeChannelCloseInit, chantypes.EventTypeChannelCloseConfirm:
channelInfo := &channelInfo{Height: height}
channelInfo.parseAttrs(log, event.Attributes)
info = channelInfo
eventType = event.Type
}
}
if info == nil {
// Not an IBC message, don't need to log here
continue
}
messages = append(messages, ibcMessage{
eventType: eventType,
info: info,
})
}
return messages
}
// clientInfo contains the consensus height of the counterparty chain for a client.
type clientInfo struct {
clientID string
consensusHeight clienttypes.Height
header []byte
}
func (c clientInfo) ClientState() provider.ClientState {
return provider.ClientState{
ClientID: c.clientID,
ConsensusHeight: c.consensusHeight,
}
}
func (res *clientInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("client_id", res.clientID)
enc.AddUint64("consensus_height", res.consensusHeight.RevisionHeight)
enc.AddUint64("consensus_height_revision", res.consensusHeight.RevisionNumber)
return nil
}
func (res *clientInfo) parseAttrs(log *zap.Logger, attributes []sdk.Attribute) {
for _, attr := range attributes {
res.parseClientAttribute(log, attr)
}
}
func (res *clientInfo) parseClientAttribute(log *zap.Logger, attr sdk.Attribute) {
switch attr.Key {
case clienttypes.AttributeKeyClientID:
res.clientID = attr.Value
case clienttypes.AttributeKeyConsensusHeight:
revisionSplit := strings.Split(attr.Value, "-")
if len(revisionSplit) != 2 {
log.Error("Error parsing client consensus height",
zap.String("client_id", res.clientID),
zap.String("value", attr.Value),
)
return
}
revisionNumberString := revisionSplit[0]
revisionNumber, err := strconv.ParseUint(revisionNumberString, 10, 64)
if err != nil {
log.Error("Error parsing client consensus height revision number",
zap.Error(err),
)
return
}
revisionHeightString := revisionSplit[1]
revisionHeight, err := strconv.ParseUint(revisionHeightString, 10, 64)
if err != nil {
log.Error("Error parsing client consensus height revision height",
zap.Error(err),
)
return
}
res.consensusHeight = clienttypes.Height{
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
}
case clienttypes.AttributeKeyHeader:
data, err := hex.DecodeString(attr.Value)
if err != nil {
log.Error("Error parsing client header",
zap.String("header", attr.Value),
zap.Error(err),
)
return
}
res.header = data
}
}
// alias type to the provider types, used for adding parser methods
type packetInfo provider.PacketInfo
func (res *packetInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddUint64("sequence", res.Sequence)
enc.AddString("src_channel", res.SourceChannel)
enc.AddString("src_port", res.SourcePort)
enc.AddString("dst_channel", res.DestChannel)
enc.AddString("dst_port", res.DestPort)
return nil
}
// parsePacketInfo is treated differently from the others since it can be constructed from the accumulation of multiple events
func (res *packetInfo) parseAttrs(log *zap.Logger, attrs []sdk.Attribute) {
for _, attr := range attrs {
res.parsePacketAttribute(log, attr)
}
}
func (res *packetInfo) parsePacketAttribute(log *zap.Logger, attr sdk.Attribute) {
var err error
switch attr.Key {
case chantypes.AttributeKeySequence:
res.Sequence, err = strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
log.Error("Error parsing packet sequence",
zap.String("value", attr.Value),
zap.Error(err),
)
return
}
case chantypes.AttributeKeyTimeoutTimestamp:
res.TimeoutTimestamp, err = strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
log.Error("Error parsing packet timestamp",
zap.Uint64("sequence", res.Sequence),
zap.String("value", attr.Value),
zap.Error(err),
)
return
}
// NOTE: deprecated per IBC spec
case chantypes.AttributeKeyData:
res.Data = []byte(attr.Value)
case chantypes.AttributeKeyDataHex:
data, err := hex.DecodeString(attr.Value)
if err != nil {
log.Error("Error parsing packet data",
zap.Uint64("sequence", res.Sequence),
zap.Error(err),
)
return
}
res.Data = data
// NOTE: deprecated per IBC spec
case chantypes.AttributeKeyAck:
res.Ack = []byte(attr.Value)
case chantypes.AttributeKeyAckHex:
data, err := hex.DecodeString(attr.Value)
if err != nil {
log.Error("Error parsing packet ack",
zap.Uint64("sequence", res.Sequence),
zap.String("value", attr.Value),
zap.Error(err),
)
return
}
res.Ack = data
case chantypes.AttributeKeyTimeoutHeight:
timeoutSplit := strings.Split(attr.Value, "-")
if len(timeoutSplit) != 2 {
log.Error("Error parsing packet height timeout",
zap.Uint64("sequence", res.Sequence),
zap.String("value", attr.Value),
)
return
}
revisionNumber, err := strconv.ParseUint(timeoutSplit[0], 10, 64)
if err != nil {
log.Error("Error parsing packet timeout height revision number",
zap.Uint64("sequence", res.Sequence),
zap.String("value", timeoutSplit[0]),
zap.Error(err),
)
return
}
revisionHeight, err := strconv.ParseUint(timeoutSplit[1], 10, 64)
if err != nil {
log.Error("Error parsing packet timeout height revision height",
zap.Uint64("sequence", res.Sequence),
zap.String("value", timeoutSplit[1]),
zap.Error(err),
)
return
}
res.TimeoutHeight = clienttypes.Height{
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
}
case chantypes.AttributeKeySrcPort:
res.SourcePort = attr.Value
case chantypes.AttributeKeySrcChannel:
res.SourceChannel = attr.Value
case chantypes.AttributeKeyDstPort:
res.DestPort = attr.Value
case chantypes.AttributeKeyDstChannel:
res.DestChannel = attr.Value
}
}
// alias type to the provider types, used for adding parser methods
type channelInfo provider.ChannelInfo
func (res *channelInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("channel_id", res.ChannelID)
enc.AddString("port_id", res.PortID)
enc.AddString("counterparty_channel_id", res.CounterpartyChannelID)
enc.AddString("counterparty_port_id", res.CounterpartyPortID)
return nil
}
func (res *channelInfo) parseAttrs(log *zap.Logger, attrs []sdk.Attribute) {
for _, attr := range attrs {
res.parseChannelAttribute(attr)
}
}
func (res *channelInfo) parseChannelAttribute(attr sdk.Attribute) {
switch attr.Key {
case chantypes.AttributeKeyPortID:
res.PortID = attr.Value
case chantypes.AttributeKeyChannelID:
res.ChannelID = attr.Value
case chantypes.AttributeCounterpartyPortID:
res.CounterpartyPortID = attr.Value
case chantypes.AttributeCounterpartyChannelID:
res.CounterpartyChannelID = attr.Value
case chantypes.AttributeKeyConnectionID:
res.ConnID = attr.Value
}
}
// alias type to the provider types, used for adding parser methods
type connectionInfo provider.ConnectionInfo
func (res *connectionInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("connection_id", res.ConnID)
enc.AddString("client_id", res.ClientID)
enc.AddString("counterparty_connection_id", res.CounterpartyConnID)
enc.AddString("counterparty_client_id", res.CounterpartyClientID)
return nil
}
func (res *connectionInfo) parseAttrs(log *zap.Logger, attrs []sdk.Attribute) {
for _, attr := range attrs {
res.parseConnectionAttribute(attr)
}
}
func (res *connectionInfo) parseConnectionAttribute(attr sdk.Attribute) {
switch attr.Key {
case conntypes.AttributeKeyConnectionID:
res.ConnID = attr.Value
case conntypes.AttributeKeyClientID:
res.ClientID = attr.Value
case conntypes.AttributeKeyCounterpartyConnectionID:
res.CounterpartyConnID = attr.Value
case conntypes.AttributeKeyCounterpartyClientID:
res.CounterpartyClientID = attr.Value
}
}