forked from cosmos/relayer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_handlers.go
136 lines (121 loc) · 4.92 KB
/
message_handlers.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
package cosmos
import (
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/processor"
"github.com/defund-labs/relayer/v3/relayer/provider"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func (ccp *CosmosChainProcessor) handleMessage(m ibcMessage, c processor.IBCMessagesCache) {
switch t := m.info.(type) {
case *packetInfo:
ccp.handlePacketMessage(m.eventType, provider.PacketInfo(*t), c)
case *channelInfo:
ccp.handleChannelMessage(m.eventType, provider.ChannelInfo(*t), c)
case *connectionInfo:
ccp.handleConnectionMessage(m.eventType, provider.ConnectionInfo(*t), c)
case *clientInfo:
ccp.handleClientMessage(m.eventType, *t)
}
}
func (ccp *CosmosChainProcessor) handlePacketMessage(eventType string, pi provider.PacketInfo, c processor.IBCMessagesCache) {
k, err := processor.PacketInfoChannelKey(eventType, pi)
if err != nil {
ccp.log.Error("Unexpected error handling packet message",
zap.String("event_type", eventType),
zap.Uint64("sequence", pi.Sequence),
zap.Inline(k),
zap.Error(err),
)
return
}
if !c.PacketFlow.ShouldRetainSequence(ccp.pathProcessors, k, ccp.chainProvider.ChainId(), eventType, pi.Sequence) {
ccp.log.Debug("Not retaining packet message",
zap.String("event_type", eventType),
zap.Uint64("sequence", pi.Sequence),
zap.Inline(k),
)
return
}
ccp.log.Debug("Retaining packet message",
zap.String("event_type", eventType),
zap.Uint64("sequence", pi.Sequence),
zap.Inline(k),
)
c.PacketFlow.Retain(k, eventType, pi)
ccp.logPacketMessage(eventType, pi)
}
func (ccp *CosmosChainProcessor) handleChannelMessage(eventType string, ci provider.ChannelInfo, ibcMessagesCache processor.IBCMessagesCache) {
ccp.channelConnections[ci.ChannelID] = ci.ConnID
channelKey := processor.ChannelInfoChannelKey(ci)
switch eventType {
case chantypes.EventTypeChannelOpenInit, chantypes.EventTypeChannelOpenTry:
ccp.channelStateCache[channelKey] = false
case chantypes.EventTypeChannelOpenAck, chantypes.EventTypeChannelOpenConfirm:
ccp.channelStateCache[channelKey] = true
case chantypes.EventTypeChannelCloseInit, chantypes.EventTypeChannelCloseConfirm:
for k := range ccp.channelStateCache {
if k.PortID == ci.PortID && k.ChannelID == ci.ChannelID {
ccp.channelStateCache[k] = false
break
}
}
}
ibcMessagesCache.ChannelHandshake.Retain(channelKey, eventType, ci)
ccp.logChannelMessage(eventType, ci)
}
func (ccp *CosmosChainProcessor) handleConnectionMessage(eventType string, ci provider.ConnectionInfo, ibcMessagesCache processor.IBCMessagesCache) {
ccp.connectionClients[ci.ConnID] = ci.ClientID
connectionKey := processor.ConnectionInfoConnectionKey(ci)
open := (eventType == conntypes.EventTypeConnectionOpenAck || eventType == conntypes.EventTypeConnectionOpenConfirm)
ccp.connectionStateCache[connectionKey] = open
ibcMessagesCache.ConnectionHandshake.Retain(connectionKey, eventType, ci)
ccp.logConnectionMessage(eventType, ci)
}
func (ccp *CosmosChainProcessor) handleClientMessage(eventType string, ci clientInfo) {
ccp.latestClientState.update(ci)
ccp.logObservedIBCMessage(eventType, zap.String("client_id", ci.clientID))
}
func (ccp *CosmosChainProcessor) logObservedIBCMessage(m string, fields ...zap.Field) {
ccp.log.With(zap.String("event_type", m)).Debug("Observed IBC message", fields...)
}
func (ccp *CosmosChainProcessor) logPacketMessage(message string, pi provider.PacketInfo) {
if !ccp.log.Core().Enabled(zapcore.DebugLevel) {
return
}
fields := []zap.Field{
zap.Uint64("sequence", pi.Sequence),
zap.String("src_channel", pi.SourceChannel),
zap.String("src_port", pi.SourcePort),
zap.String("dst_channel", pi.DestChannel),
zap.String("dst_port", pi.DestPort),
}
if pi.TimeoutHeight.RevisionHeight > 0 {
fields = append(fields, zap.Uint64("timeout_height", pi.TimeoutHeight.RevisionHeight))
}
if pi.TimeoutHeight.RevisionNumber > 0 {
fields = append(fields, zap.Uint64("timeout_height_revision", pi.TimeoutHeight.RevisionNumber))
}
if pi.TimeoutTimestamp > 0 {
fields = append(fields, zap.Uint64("timeout_timestamp", pi.TimeoutTimestamp))
}
ccp.logObservedIBCMessage(message, fields...)
}
func (ccp *CosmosChainProcessor) logChannelMessage(message string, ci provider.ChannelInfo) {
ccp.logObservedIBCMessage(message,
zap.String("channel_id", ci.ChannelID),
zap.String("port_id", ci.PortID),
zap.String("counterparty_channel_id", ci.CounterpartyChannelID),
zap.String("counterparty_port_id", ci.CounterpartyPortID),
zap.String("connection_id", ci.ConnID),
)
}
func (ccp *CosmosChainProcessor) logConnectionMessage(message string, ci provider.ConnectionInfo) {
ccp.logObservedIBCMessage(message,
zap.String("client_id", ci.ClientID),
zap.String("connection_id", ci.ConnID),
zap.String("counterparty_client_id", ci.CounterpartyClientID),
zap.String("counterparty_connection_id", ci.CounterpartyConnID),
)
}