forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
330 lines (276 loc) · 9.4 KB
/
worker.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
package relayer
import (
"context"
"sync/atomic"
"time"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/lib/cchain"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/expbackoff"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/lib/xchain"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)
const (
// mempoolLimit is the maximum number of transactions we want to submit to the mempool at once.
mempoolLimit = 16
)
type Worker struct {
destChain netconf.Chain // Destination chain
network netconf.Network
cProvider cchain.Provider
xProvider xchain.Provider
creator CreateFunc
sendProvider func() (SendFunc, error)
awaitValSet awaitValSet
}
// NewWorker creates a new worker for a single destination chain.
func NewWorker(destChain netconf.Chain, network netconf.Network, cProvider cchain.Provider,
xProvider xchain.Provider, creator CreateFunc, sendProvider func() (SendFunc, error),
awaitValSet awaitValSet,
) *Worker {
return &Worker{
destChain: destChain,
network: network,
cProvider: cProvider,
xProvider: xProvider,
creator: creator,
sendProvider: sendProvider,
awaitValSet: awaitValSet,
}
}
func (w *Worker) Run(ctx context.Context) {
ctx = log.WithCtx(ctx, "dst_chain", w.destChain.Name)
backoff := expbackoff.NewWithAutoReset(ctx)
for ctx.Err() == nil {
err := w.runOnce(ctx)
if ctx.Err() != nil {
return
}
log.Error(ctx, "Worker failed, resetting", err)
workerResets.WithLabelValues(w.destChain.Name).Inc()
backoff()
}
}
func (w *Worker) runOnce(ctx context.Context) error {
log.Info(ctx, "Worker starting")
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cursors, err := getSubmittedCursors(ctx, w.network, w.destChain.ID, w.xProvider)
if err != nil {
return err
}
for _, cursor := range cursors {
log.Info(ctx, "Worker fetched submitted cursor",
"stream", w.network.StreamName(cursor.StreamID),
"attest_offset", cursor.AttestOffset,
"msg_offset", cursor.MsgOffset,
)
}
sender, err := w.sendProvider()
if err != nil {
return err
}
buf := newActiveBuffer(w.destChain.Name, mempoolLimit, sender)
attestOffsets, err := fromChainVersionOffsets(cursors, w.network.ChainVersionsTo(w.destChain.ID))
if err != nil {
return err
}
msgFilter, err := newMsgOffsetFilter(cursors)
if err != nil {
return err
}
var logAttrs []any //nolint:prealloc // Not worth it
for chainVer, fromOffset := range attestOffsets {
if chainVer.ID == w.destChain.ID { // Sanity check
return errors.New("unexpected chain version [BUG]")
}
callback := w.newCallback(msgFilter, buf.AddInput, newMsgStreamMapper(w.network))
w.cProvider.StreamAsync(ctx, chainVer, fromOffset, w.destChain.Name, callback)
logAttrs = append(logAttrs, w.network.ChainVersionName(chainVer), fromOffset)
}
log.Info(ctx, "Worker subscribed to chains", logAttrs...)
return buf.Run(ctx)
}
// awaitValSet blocks until the portal is aware of this validator set ID.
type awaitValSet func(ctx context.Context, valsetID uint64) error
// newValSetAwaiter creates a new awaitValSet function for the given portal.
func newValSetAwaiter(portal *bindings.OmniPortal, blockPeriod time.Duration) awaitValSet {
var prev atomic.Uint64 // Cache previous to reduce network lookups.
return func(ctx context.Context, valsetID uint64) error {
if prev.Load() == valsetID {
return nil
}
backoff := expbackoff.New(ctx, expbackoff.WithPeriodicConfig(blockPeriod))
var attempt int
for ctx.Err() == nil {
power, err := portal.ValSetTotalPower(&bind.CallOpts{Context: ctx}, valsetID)
if err != nil {
return errors.Wrap(err, "get validator set power")
}
if power == 0 {
attempt++
if attempt%10 == 0 {
log.Warn(ctx, "Validator set not known by portal (will retry)", nil, "valset_id", valsetID, "attempt", attempt)
}
backoff()
continue
}
prev.Store(valsetID)
return nil
}
return errors.Wrap(ctx.Err(), "context done")
}
}
// msgStreamMapper maps messages by stream ID.
type msgStreamMapper func([]xchain.Msg) map[xchain.StreamID][]xchain.Msg
// newMsgStreamMapper creates a new message stream mapper for the given network.
// It maps consensus chain messages to all EVM chains (broadcast), and normal messages to their stream ID.
func newMsgStreamMapper(network netconf.Network) msgStreamMapper {
consensusChain, _ := network.OmniConsensusChain()
return func(msgs []xchain.Msg) map[xchain.StreamID][]xchain.Msg {
resp := make(map[xchain.StreamID][]xchain.Msg)
for _, msg := range msgs {
// Normal messages are mapped to their stream ID.
if msg.SourceChainID != consensusChain.ID {
resp[msg.StreamID] = append(resp[msg.StreamID], msg)
continue
}
// Consensus chain messages are broadcasted to all EVM chains.
for _, evmChain := range network.EVMChains() {
streamID := xchain.StreamID{
SourceChainID: consensusChain.ID,
DestChainID: evmChain.ID,
ShardID: msg.ShardID,
}
resp[streamID] = append(resp[streamID], msg)
}
}
return resp
}
}
func (w *Worker) newCallback(
msgFilter *msgCursorFilter,
sender SendFunc,
msgStreamMapper msgStreamMapper,
) cchain.ProviderCallback {
return func(ctx context.Context, att xchain.Attestation) error {
block, ok, err := fetchXBlock(ctx, w.xProvider, att)
if err != nil {
return err
} else if !ok {
return nil // Mismatching fuzzy attestation, skip.
} else if len(block.Msgs) == 0 {
return nil // No messages, nothing to do.
}
msgTree, err := xchain.NewMsgTree(block.Msgs)
if err != nil {
return err
}
// Split into streams
for streamID, msgs := range msgStreamMapper(block.Msgs) {
if streamID.DestChainID != w.destChain.ID {
continue // Skip streams not destined for this worker.
} else if !attestationForShard(att, streamID.ShardID) {
continue // Skip streams not applicable to this attestation.
}
if err := w.awaitValSet(ctx, att.ValidatorSetID); err != nil {
return errors.Wrap(err, "await validator set")
}
// Filter out any previously submitted message offsets
msgs, err = filterMsgs(ctx, streamID, w.network.StreamName, msgs, msgFilter)
if err != nil {
return err
} else if len(msgs) == 0 {
continue
}
update := StreamUpdate{
StreamID: streamID,
Attestation: att,
Msgs: msgs,
MsgTree: msgTree,
}
submissions, err := w.creator(update)
if err != nil {
return err
}
for _, subs := range submissions {
if err := sender(ctx, subs); err != nil {
return err
}
}
}
return nil
}
}
// fetchXBlock gets the xblock from the source chain (retry up to 10s if block-not-finalized).
func fetchXBlock(rootCtx context.Context, xProvider xchain.Provider, att xchain.Attestation) (xchain.Block, bool, error) {
ctx, cancel := context.WithTimeout(rootCtx, 10*time.Second)
defer cancel()
backoff := expbackoff.New(ctx, expbackoff.WithPeriodicConfig(time.Second))
for {
req := xchain.ProviderRequest{
ChainID: att.ChainID,
Height: att.BlockHeight,
ConfLevel: att.ChainVersion.ConfLevel,
}
block, ok, err := xProvider.GetBlock(ctx, req)
if rootCtx.Err() != nil {
return xchain.Block{}, false, errors.Wrap(rootCtx.Err(), "canceled") // Root context closed, shutting down
} else if ctx.Err() != nil {
return xchain.Block{}, false, errors.New("attestation block still not finalized (node lagging?)")
} else if err != nil {
return xchain.Block{}, false, err
} else if !ok {
// This happens sometimes if the evm node relayer is querying is lagging behind
// the chain itself. Especially for omni_evm with instant finality, this does happen sometimes.
// Just backoff and retry a few times.
backoff()
continue
}
if err := verifyAttBlock(att, block); err != nil {
if att.ChainVersion.ConfLevel.IsFuzzy() {
log.Warn(ctx, "Skipping fuzzy attestation mismatching block", err)
return block, false, nil
}
return xchain.Block{}, false, errors.Wrap(err, "mismatching block vs finalized attestation [BUG]")
}
// We got the xblock, it is finalized and its hash matches the attestation block hash.
return block, true, nil
}
}
// verifyAttBlock verifies the attestation matches the xblock.
func verifyAttBlock(att xchain.Attestation, block xchain.Block) error {
if block.BlockHash != att.BlockHash {
return errors.New("attestation block hash mismatch",
log.Hex7("attestation_hash", att.BlockHash[:]),
log.Hex7("block_hash", block.BlockHash[:]),
)
} else if block.BlockHeader != att.BlockHeader {
return errors.New("attestation block header mismatch")
}
var msgRoot [32]byte
if len(block.Msgs) > 0 {
msgTree, err := xchain.NewMsgTree(block.Msgs)
if err != nil {
return err
}
msgRoot = msgTree.MsgRoot()
}
if att.MsgRoot != msgRoot {
return errors.New("attestation message root mismatch",
log.Hex7("att_msg_root", att.MsgRoot[:]),
log.Hex7("block_msg_root", msgRoot[:]),
)
}
return nil
}
// attestationForShard returns true if the attestation proof contains messages for the shard.
// Fuzzy attestations cannot be used to prove finalized shards. But finalized attestations can prove all shards.
func attestationForShard(att xchain.Attestation, shard xchain.ShardID) bool {
if att.ChainVersion.ConfLevel == xchain.ConfFinalized {
return true // Finalized attestation, matches all streams.
}
return att.ChainVersion.ConfLevel == shard.ConfLevel()
}