forked from cosmos/relayer
/
cosmos_chain_processor.go
388 lines (328 loc) · 12.2 KB
/
cosmos_chain_processor.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package cosmos
import (
"context"
"errors"
"fmt"
"time"
"github.com/avast/retry-go/v4"
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/processor"
"github.com/defund-labs/relayer/v3/relayer/provider"
"github.com/defund-labs/relayer/v3/relayer/provider/cosmos"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
type CosmosChainProcessor struct {
log *zap.Logger
chainProvider *cosmos.CosmosProvider
pathProcessors processor.PathProcessors
// indicates whether queries are in sync with latest height of the chain
inSync bool
// highest block
latestBlock provider.LatestBlock
// holds highest consensus height and header for all clients
latestClientState
// holds open state for known connections
connectionStateCache processor.ConnectionStateCache
// holds open state for known channels
channelStateCache processor.ChannelStateCache
// map of connection ID to client ID
connectionClients map[string]string
// map of channel ID to connection ID
channelConnections map[string]string
}
func NewCosmosChainProcessor(log *zap.Logger, provider *cosmos.CosmosProvider) *CosmosChainProcessor {
return &CosmosChainProcessor{
log: log.With(zap.String("chain_name", provider.ChainName()), zap.String("chain_id", provider.ChainId())),
chainProvider: provider,
latestClientState: make(latestClientState),
connectionStateCache: make(processor.ConnectionStateCache),
channelStateCache: make(processor.ChannelStateCache),
connectionClients: make(map[string]string),
channelConnections: make(map[string]string),
}
}
const (
queryTimeout = 5 * time.Second
blockResultsQueryTimeout = 2 * time.Minute
latestHeightQueryRetryDelay = 1 * time.Second
latestHeightQueryRetries = 5
defaultMinQueryLoopDuration = 1 * time.Second
inSyncNumBlocksThreshold = 2
)
// latestClientState is a map of clientID to the latest clientInfo for that client.
type latestClientState map[string]provider.ClientState
func (l latestClientState) update(clientInfo clientInfo) {
existingClientInfo, ok := l[clientInfo.clientID]
if ok && clientInfo.consensusHeight.LT(existingClientInfo.ConsensusHeight) {
// height is less than latest, so no-op
return
}
// update latest if no existing state or provided consensus height is newer
l[clientInfo.clientID] = clientInfo.ClientState()
}
// Provider returns the ChainProvider, which provides the methods for querying, assembling IBC messages, and sending transactions.
func (ccp *CosmosChainProcessor) Provider() provider.ChainProvider {
return ccp.chainProvider
}
// Set the PathProcessors that this ChainProcessor should publish relevant IBC events to.
// ChainProcessors need reference to their PathProcessors and vice-versa, handled by EventProcessorBuilder.Build().
func (ccp *CosmosChainProcessor) SetPathProcessors(pathProcessors processor.PathProcessors) {
ccp.pathProcessors = pathProcessors
}
// latestHeightWithRetry will query for the latest height, retrying in case of failure.
// It will delay by latestHeightQueryRetryDelay between attempts, up to latestHeightQueryRetries.
func (ccp *CosmosChainProcessor) latestHeightWithRetry(ctx context.Context) (latestHeight int64, err error) {
return latestHeight, retry.Do(func() error {
latestHeightQueryCtx, cancelLatestHeightQueryCtx := context.WithTimeout(ctx, queryTimeout)
defer cancelLatestHeightQueryCtx()
var err error
latestHeight, err = ccp.chainProvider.QueryLatestHeight(latestHeightQueryCtx)
return err
}, retry.Context(ctx), retry.Attempts(latestHeightQueryRetries), retry.Delay(latestHeightQueryRetryDelay), retry.LastErrorOnly(true), retry.OnRetry(func(n uint, err error) {
ccp.log.Info(
"Failed to query latest height",
zap.Uint("attempt", n+1),
zap.Uint("max_attempts", latestHeightQueryRetries),
zap.Error(err),
)
}))
}
// clientState will return the most recent client state if client messages
// have already been observed for the clientID, otherwise it will query for it.
func (ccp *CosmosChainProcessor) clientState(ctx context.Context, clientID string) (provider.ClientState, error) {
if state, ok := ccp.latestClientState[clientID]; ok {
return state, nil
}
cs, err := ccp.chainProvider.QueryClientState(ctx, int64(ccp.latestBlock.Height), clientID)
if err != nil {
return provider.ClientState{}, err
}
clientState := provider.ClientState{
ClientID: clientID,
ConsensusHeight: cs.GetLatestHeight().(clienttypes.Height),
}
ccp.latestClientState[clientID] = clientState
return clientState, nil
}
// queryCyclePersistence hold the variables that should be retained across queryCycles.
type queryCyclePersistence struct {
latestHeight int64
latestQueriedBlock int64
minQueryLoopDuration time.Duration
}
// Run starts the query loop for the chain which will gather applicable ibc messages and push events out to the relevant PathProcessors.
// The initialBlockHistory parameter determines how many historical blocks should be fetched and processed before continuing with current blocks.
// ChainProcessors should obey the context and return upon context cancellation.
func (ccp *CosmosChainProcessor) Run(ctx context.Context, initialBlockHistory uint64) error {
// this will be used for persistence across query cycle loop executions
persistence := queryCyclePersistence{
minQueryLoopDuration: defaultMinQueryLoopDuration,
}
// Infinite retry to get initial latest height
for {
latestHeight, err := ccp.latestHeightWithRetry(ctx)
if err != nil {
ccp.log.Error(
"Failed to query latest height after max attempts",
zap.Uint("attempts", latestHeightQueryRetries),
zap.Error(err),
)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil
}
continue
}
persistence.latestHeight = latestHeight
break
}
// this will make initial QueryLoop iteration look back initialBlockHistory blocks in history
latestQueriedBlock := persistence.latestHeight - int64(initialBlockHistory)
if latestQueriedBlock < 0 {
latestQueriedBlock = 0
}
persistence.latestQueriedBlock = latestQueriedBlock
var eg errgroup.Group
eg.Go(func() error {
return ccp.initializeConnectionState(ctx)
})
eg.Go(func() error {
return ccp.initializeChannelState(ctx)
})
if err := eg.Wait(); err != nil {
return err
}
ccp.log.Debug("Entering main query loop")
ticker := time.NewTicker(persistence.minQueryLoopDuration)
for {
if err := ccp.queryCycle(ctx, &persistence); err != nil {
return err
}
select {
case <-ctx.Done():
return nil
case <-ticker.C:
ticker.Reset(persistence.minQueryLoopDuration)
}
}
}
// initializeConnectionState will bootstrap the connectionStateCache with the open connection state.
func (ccp *CosmosChainProcessor) initializeConnectionState(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, queryTimeout)
defer cancel()
connections, err := ccp.chainProvider.QueryConnections(ctx)
if err != nil {
return fmt.Errorf("error querying connections: %w", err)
}
for _, c := range connections {
ccp.connectionClients[c.Id] = c.ClientId
ccp.connectionStateCache[processor.ConnectionKey{
ConnectionID: c.Id,
ClientID: c.ClientId,
CounterpartyConnID: c.Counterparty.ConnectionId,
CounterpartyClientID: c.Counterparty.ClientId,
}] = c.State == conntypes.OPEN
}
return nil
}
// initializeChannelState will bootstrap the channelStateCache with the open channel state.
func (ccp *CosmosChainProcessor) initializeChannelState(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, queryTimeout)
defer cancel()
channels, err := ccp.chainProvider.QueryChannels(ctx)
if err != nil {
return fmt.Errorf("error querying channels: %w", err)
}
for _, ch := range channels {
if len(ch.ConnectionHops) != 1 {
ccp.log.Error("Found channel using multiple connection hops. Not currently supported, ignoring.",
zap.String("channel_id", ch.ChannelId),
zap.String("port_id", ch.PortId),
zap.Strings("connection_hops", ch.ConnectionHops),
)
continue
}
ccp.channelConnections[ch.ChannelId] = ch.ConnectionHops[0]
ccp.channelStateCache[processor.ChannelKey{
ChannelID: ch.ChannelId,
PortID: ch.PortId,
CounterpartyChannelID: ch.Counterparty.ChannelId,
CounterpartyPortID: ch.Counterparty.PortId,
}] = ch.State == chantypes.OPEN
}
return nil
}
func (ccp *CosmosChainProcessor) queryCycle(ctx context.Context, persistence *queryCyclePersistence) error {
var err error
persistence.latestHeight, err = ccp.latestHeightWithRetry(ctx)
// don't want to cause CosmosChainProcessor to quit here, can retry again next cycle.
if err != nil {
ccp.log.Error(
"Failed to query latest height after max attempts",
zap.Uint("attempts", latestHeightQueryRetries),
zap.Error(err),
)
return nil
}
ccp.log.Debug("Queried latest height",
zap.Int64("latest_height", persistence.latestHeight),
)
// used at the end of the cycle to send signal to path processors to start processing if both chains are in sync and no new messages came in this cycle
firstTimeInSync := false
if !ccp.inSync {
if (persistence.latestHeight - persistence.latestQueriedBlock) < inSyncNumBlocksThreshold {
ccp.inSync = true
firstTimeInSync = true
ccp.log.Info("Chain is in sync")
} else {
ccp.log.Info("Chain is not yet in sync",
zap.Int64("latest_queried_block", persistence.latestQueriedBlock),
zap.Int64("latest_height", persistence.latestHeight),
)
}
}
ibcMessagesCache := processor.NewIBCMessagesCache()
ibcHeaderCache := make(processor.IBCHeaderCache)
ppChanged := false
var latestHeader cosmos.CosmosIBCHeader
newLatestQueriedBlock := persistence.latestQueriedBlock
for i := persistence.latestQueriedBlock + 1; i <= persistence.latestHeight; i++ {
var eg errgroup.Group
var blockRes *ctypes.ResultBlockResults
var ibcHeader provider.IBCHeader
i := i
eg.Go(func() (err error) {
queryCtx, cancelQueryCtx := context.WithTimeout(ctx, blockResultsQueryTimeout)
defer cancelQueryCtx()
blockRes, err = ccp.chainProvider.RPCClient.BlockResults(queryCtx, &i)
return err
})
eg.Go(func() (err error) {
queryCtx, cancelQueryCtx := context.WithTimeout(ctx, queryTimeout)
defer cancelQueryCtx()
ibcHeader, err = ccp.chainProvider.IBCHeaderAtHeight(queryCtx, i)
return err
})
if err := eg.Wait(); err != nil {
ccp.log.Warn("Error querying block data", zap.Error(err))
break
}
latestHeader = ibcHeader.(cosmos.CosmosIBCHeader)
heightUint64 := uint64(i)
ccp.latestBlock = provider.LatestBlock{
Height: heightUint64,
Time: latestHeader.SignedHeader.Time,
}
ibcHeaderCache[heightUint64] = latestHeader
ppChanged = true
for _, tx := range blockRes.TxsResults {
if tx.Code != 0 {
// tx was not successful
continue
}
messages := ccp.ibcMessagesFromTransaction(tx, heightUint64)
for _, m := range messages {
ccp.handleMessage(m, ibcMessagesCache)
}
}
newLatestQueriedBlock = i
}
if newLatestQueriedBlock == persistence.latestQueriedBlock {
return nil
}
if !ppChanged {
if firstTimeInSync {
for _, pp := range ccp.pathProcessors {
pp.ProcessBacklogIfReady()
}
}
return nil
}
chainID := ccp.chainProvider.ChainId()
for _, pp := range ccp.pathProcessors {
clientID := pp.RelevantClientID(chainID)
clientState, err := ccp.clientState(ctx, clientID)
if err != nil {
ccp.log.Error("Error fetching client state",
zap.String("client_id", clientID),
zap.Error(err),
)
continue
}
pp.HandleNewData(chainID, processor.ChainProcessorCacheData{
LatestBlock: ccp.latestBlock,
LatestHeader: latestHeader,
IBCMessagesCache: ibcMessagesCache,
InSync: ccp.inSync,
ClientState: clientState,
ConnectionStateCache: ccp.connectionStateCache.FilterForClient(clientID),
ChannelStateCache: ccp.channelStateCache.FilterForClient(clientID, ccp.channelConnections, ccp.connectionClients),
IBCHeaderCache: ibcHeaderCache,
})
}
persistence.latestQueriedBlock = newLatestQueriedBlock
return nil
}