-
Notifications
You must be signed in to change notification settings - Fork 713
/
sequencer.go
505 lines (428 loc) · 16 KB
/
sequencer.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package sequencer
import (
"context"
"fmt"
"sync"
"time"
"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
"github.com/0xPolygonHermez/zkevm-node/event"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/datastream"
"github.com/ethereum/go-ethereum/common"
"google.golang.org/protobuf/proto"
)
const (
datastreamChannelBufferSize = 50
)
// Sequencer represents a sequencer
type Sequencer struct {
cfg Config
batchCfg state.BatchConfig
poolCfg pool.Config
pool txPool
stateIntf stateInterface
eventLog *event.EventLog
etherman ethermanInterface
worker *Worker
finalizer *finalizer
workerReadyTxsCond *timeoutCond
streamServer *datastreamer.StreamServer
dataToStream chan interface{}
numberOfStateInconsistencies uint64
}
// New init sequencer
func New(cfg Config, batchCfg state.BatchConfig, poolCfg pool.Config, txPool txPool, stateIntf stateInterface, etherman ethermanInterface, eventLog *event.EventLog) (*Sequencer, error) {
sequencer := &Sequencer{
cfg: cfg,
batchCfg: batchCfg,
poolCfg: poolCfg,
pool: txPool,
stateIntf: stateIntf,
etherman: etherman,
eventLog: eventLog,
}
sequencer.dataToStream = make(chan interface{}, datastreamChannelBufferSize)
return sequencer, nil
}
// Start starts the sequencer
func (s *Sequencer) Start(ctx context.Context) {
for !s.isSynced(ctx) {
log.Infof("waiting for synchronizer to sync...")
time.Sleep(time.Second)
}
err := s.pool.MarkWIPTxsAsPending(ctx)
if err != nil {
log.Fatalf("failed to mark wip txs as pending, error: %v", err)
}
// Start stream server if enabled
if s.cfg.StreamServer.Enabled {
s.streamServer, err = datastreamer.NewServer(s.cfg.StreamServer.Port, s.cfg.StreamServer.Version, s.cfg.StreamServer.ChainID, state.StreamTypeSequencer, s.cfg.StreamServer.Filename, s.cfg.StreamServer.WriteTimeout.Duration, s.cfg.StreamServer.InactivityTimeout.Duration, s.cfg.StreamServer.InactivityCheckInterval.Duration, &s.cfg.StreamServer.Log)
if err != nil {
log.Fatalf("failed to create stream server, error: %v", err)
}
err = s.streamServer.Start()
if err != nil {
log.Fatalf("failed to start stream server, error: %v", err)
}
s.updateDataStreamerFile(ctx, s.cfg.StreamServer.ChainID)
}
if s.streamServer != nil {
go s.sendDataToStreamer(s.cfg.StreamServer.ChainID, s.cfg.StreamServer.Version)
}
s.workerReadyTxsCond = newTimeoutCond(&sync.Mutex{})
s.worker = NewWorker(s.stateIntf, s.batchCfg.Constraints, s.workerReadyTxsCond)
s.finalizer = newFinalizer(s.cfg.Finalizer, s.poolCfg, s.worker, s.pool, s.stateIntf, s.etherman, s.cfg.L2Coinbase, s.isSynced, s.batchCfg.Constraints, s.eventLog, s.streamServer, s.workerReadyTxsCond, s.dataToStream)
go s.finalizer.Start(ctx)
go s.loadFromPool(ctx)
go s.deleteOldPoolTxs(ctx)
go s.expireOldWorkerTxs(ctx)
go s.checkStateInconsistency(ctx)
// Wait until context is done
<-ctx.Done()
}
// checkStateInconsistency checks if state inconsistency happened
func (s *Sequencer) checkStateInconsistency(ctx context.Context) {
var err error
s.numberOfStateInconsistencies, err = s.stateIntf.CountReorgs(ctx, nil)
if err != nil {
log.Error("failed to get initial number of reorgs, error: %v", err)
}
for {
stateInconsistenciesDetected, err := s.stateIntf.CountReorgs(ctx, nil)
if err != nil {
log.Error("failed to get number of reorgs, error: %v", err)
return
}
if stateInconsistenciesDetected != s.numberOfStateInconsistencies {
s.finalizer.Halt(ctx, fmt.Errorf("state inconsistency detected, halting finalizer"), false)
}
time.Sleep(s.cfg.StateConsistencyCheckInterval.Duration)
}
}
func (s *Sequencer) updateDataStreamerFile(ctx context.Context, chainID uint64) {
err := state.GenerateDataStreamFile(ctx, s.streamServer, s.stateIntf, true, nil, chainID, s.cfg.StreamServer.UpgradeEtrogBatchNumber, s.cfg.StreamServer.Version)
if err != nil {
log.Fatalf("failed to generate data streamer file, error: %v", err)
}
log.Info("data streamer file updated")
}
func (s *Sequencer) deleteOldPoolTxs(ctx context.Context) {
for {
time.Sleep(s.cfg.DeletePoolTxsCheckInterval.Duration)
if s.finalizer.haltFinalizer.Load() {
return
}
log.Infof("trying to get txs to delete from the pool...")
earliestTxHash, err := s.pool.GetEarliestProcessedTx(ctx)
if err != nil {
log.Errorf("failed to get earliest tx hash to delete, err: %v", err)
continue
}
txHashes, err := s.stateIntf.GetTxsOlderThanNL1BlocksUntilTxHash(ctx, s.cfg.DeletePoolTxsL1BlockConfirmations, earliestTxHash, nil)
if err != nil {
log.Errorf("failed to get txs hashes to delete, error: %v", err)
continue
}
log.Infof("trying to delete %d selected txs", len(txHashes))
err = s.pool.DeleteTransactionsByHashes(ctx, txHashes)
if err != nil {
log.Errorf("failed to delete selected txs from the pool, error: %v", err)
continue
}
log.Infof("deleted %d selected txs from the pool", len(txHashes))
log.Infof("trying to delete failed txs from the pool")
// Delete failed txs older than a certain date (14 seconds per L1 block)
err = s.pool.DeleteFailedTransactionsOlderThan(ctx, time.Now().Add(-time.Duration(s.cfg.DeletePoolTxsL1BlockConfirmations*14)*time.Second)) //nolint:gomnd
if err != nil {
log.Errorf("failed to delete failed txs from the pool, error: %v", err)
continue
}
log.Infof("failed txs deleted from the pool")
}
}
func (s *Sequencer) expireOldWorkerTxs(ctx context.Context) {
for {
time.Sleep(s.cfg.TxLifetimeCheckInterval.Duration)
if s.finalizer.haltFinalizer.Load() {
return
}
txTrackers := s.worker.ExpireTransactions(s.cfg.TxLifetimeMax.Duration)
failedReason := ErrExpiredTransaction.Error()
for _, txTracker := range txTrackers {
err := s.pool.UpdateTxStatus(ctx, txTracker.Hash, pool.TxStatusFailed, false, &failedReason)
if err != nil {
log.Errorf("failed to update tx status, error: %v", err)
}
}
}
}
// loadFromPool keeps loading transactions from the pool
func (s *Sequencer) loadFromPool(ctx context.Context) {
for {
if s.finalizer.haltFinalizer.Load() {
return
}
poolTransactions, err := s.pool.GetNonWIPPendingTxs(ctx)
if err != nil && err != pool.ErrNotFound {
log.Errorf("error loading txs from pool, error: %v", err)
}
for _, tx := range poolTransactions {
err := s.addTxToWorker(ctx, tx)
if err != nil {
log.Errorf("error adding transaction to worker, error: %v", err)
}
}
if len(poolTransactions) == 0 {
time.Sleep(s.cfg.LoadPoolTxsCheckInterval.Duration)
}
}
}
func (s *Sequencer) addTxToWorker(ctx context.Context, tx pool.Transaction) error {
txTracker, err := s.worker.NewTxTracker(tx.Transaction, tx.ZKCounters, tx.ReservedZKCounters, tx.IP)
if err != nil {
return err
}
replacedTx, dropReason := s.worker.AddTxTracker(ctx, txTracker)
if dropReason != nil {
failedReason := dropReason.Error()
return s.pool.UpdateTxStatus(ctx, txTracker.Hash, pool.TxStatusFailed, false, &failedReason)
} else {
if replacedTx != nil {
failedReason := ErrReplacedTransaction.Error()
err := s.pool.UpdateTxStatus(ctx, replacedTx.Hash, pool.TxStatusFailed, false, &failedReason)
if err != nil {
log.Warnf("error when setting as failed replacedTx %s, error: %v", replacedTx.HashStr, err)
}
}
return s.pool.UpdateTxWIPStatus(ctx, tx.Hash(), true)
}
}
// sendDataToStreamer sends data to the data stream server
func (s *Sequencer) sendDataToStreamer(chainID uint64, version uint8) {
var err error
for {
// Read error from previous iteration
if err != nil {
err = s.streamServer.RollbackAtomicOp()
if err != nil {
log.Errorf("failed to rollback atomic op, error: %v", err)
}
s.streamServer = nil
}
// Read data from channel
dataStream := <-s.dataToStream
s.finalizer.DataToStreamChannelCountAdd(-1)
if s.streamServer != nil {
switch data := dataStream.(type) {
// Stream a complete L2 block with its transactions
case state.DSL2FullBlock:
l2Block := data
err = s.streamServer.StartAtomicOp()
if err != nil {
log.Errorf("failed to start atomic op for l2block %d, error: %v ", l2Block.L2BlockNumber, err)
continue
}
bookMark := &datastream.BookMark{
Type: datastream.BookmarkType_BOOKMARK_TYPE_L2_BLOCK,
Value: l2Block.L2BlockNumber,
}
marshalledBookMark, err := proto.Marshal(bookMark)
if err != nil {
log.Errorf("failed to marshal bookmark for l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
_, err = s.streamServer.AddStreamBookmark(marshalledBookMark)
if err != nil {
log.Errorf("failed to add stream bookmark for l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
// Get previous block timestamp to calculate delta timestamp
previousL2Block := datastream.L2Block{}
if l2Block.L2BlockNumber > 0 {
bookMark = &datastream.BookMark{
Type: datastream.BookmarkType_BOOKMARK_TYPE_L2_BLOCK,
Value: l2Block.L2BlockNumber - 1,
}
marshalledBookMark, err := proto.Marshal(bookMark)
if err != nil {
log.Errorf("failed to marshal bookmark for l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
previousL2BlockEntry, err := s.streamServer.GetFirstEventAfterBookmark(marshalledBookMark)
if err != nil {
log.Errorf("failed to get previous l2block %d, error: %v", l2Block.L2BlockNumber-1, err)
continue
}
err = proto.Unmarshal(previousL2BlockEntry.Data, &previousL2Block)
if err != nil {
log.Errorf("failed to unmarshal previous l2block %d, error: %v", l2Block.L2BlockNumber-1, err)
continue
}
}
streamL2Block := &datastream.L2Block{
Number: l2Block.L2BlockNumber,
BatchNumber: l2Block.BatchNumber,
Timestamp: l2Block.Timestamp,
DeltaTimestamp: uint32(l2Block.Timestamp - previousL2Block.Timestamp),
MinTimestamp: l2Block.MinTimestamp,
L1Blockhash: l2Block.L1BlockHash.Bytes(),
L1InfotreeIndex: l2Block.L1InfoTreeIndex,
Hash: l2Block.BlockHash.Bytes(),
StateRoot: l2Block.StateRoot.Bytes(),
GlobalExitRoot: l2Block.GlobalExitRoot.Bytes(),
Coinbase: l2Block.Coinbase.Bytes(),
}
marshalledL2Block, err := proto.Marshal(streamL2Block)
if err != nil {
log.Errorf("failed to marshal l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
_, err = s.streamServer.AddStreamEntry(datastreamer.EntryType(datastream.EntryType_ENTRY_TYPE_L2_BLOCK), marshalledL2Block)
if err != nil {
log.Errorf("failed to add stream entry for l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
for _, l2Transaction := range l2Block.Txs {
streamL2Transaction := &datastream.Transaction{
L2BlockNumber: l2Transaction.L2BlockNumber,
IsValid: l2Transaction.IsValid != 0,
Encoded: l2Transaction.Encoded,
EffectiveGasPricePercentage: uint32(l2Transaction.EffectiveGasPricePercentage),
ImStateRoot: l2Transaction.ImStateRoot.Bytes(),
}
// Clear the state root if the ForkID is > ETROG
// currently this is redundant as the current implementation of the sequencer
// leaves the ImStateRoot empty
if l2Block.ForkID > state.FORKID_ETROG {
streamL2Transaction.ImStateRoot = common.Hash{}.Bytes()
}
marshalledL2Transaction, err := proto.Marshal(streamL2Transaction)
if err != nil {
log.Errorf("failed to marshal l2tx for l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
_, err = s.streamServer.AddStreamEntry(datastreamer.EntryType(datastream.EntryType_ENTRY_TYPE_TRANSACTION), marshalledL2Transaction)
if err != nil {
log.Errorf("failed to add l2tx stream entry for l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
}
if version >= state.DSVersion4 {
streamL2BlockEnd := &datastream.L2BlockEnd{
Number: l2Block.L2BlockNumber,
}
marshalledL2BlockEnd, err := proto.Marshal(streamL2BlockEnd)
if err != nil {
log.Errorf("failed to marshal l2block %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
_, err = s.streamServer.AddStreamEntry(datastreamer.EntryType(datastream.EntryType_ENTRY_TYPE_L2_BLOCK_END), marshalledL2BlockEnd)
if err != nil {
log.Errorf("failed to add stream entry for l2blockEnd %d, error: %v", l2Block.L2BlockNumber, err)
continue
}
}
err = s.streamServer.CommitAtomicOp()
if err != nil {
log.Errorf("failed to commit atomic op for l2block %d, error: %v ", l2Block.L2BlockNumber, err)
continue
}
// Stream a bookmark
case datastream.BookMark:
err = s.streamServer.StartAtomicOp()
if err != nil {
log.Errorf("failed to start atomic op for bookmark type %d, value %d, error: %v", data.Type, data.Value, err)
continue
}
marshalledBookMark, err := proto.Marshal(&data)
if err != nil {
log.Errorf("failed to marshal bookmark type %d, value %d, error: %v", data.Type, data.Value, err)
continue
}
_, err = s.streamServer.AddStreamBookmark(marshalledBookMark)
if err != nil {
log.Errorf("failed to add stream bookmark for bookmark type %d, value %d, error: %v", data.Type, data.Value, err)
continue
}
err = s.streamServer.CommitAtomicOp()
if err != nil {
log.Errorf("failed to commit atomic op for bookmark type %d, value %d, error: %v", data.Type, data.Value, err)
continue
}
case datastream.BatchStart:
err = s.streamServer.StartAtomicOp()
if err != nil {
log.Errorf("failed to start atomic op for batch start, error: %v", err)
continue
}
data.ChainId = chainID
marshalledBatchStart, err := proto.Marshal(&data)
if err != nil {
log.Errorf("failed to marshal batch start error: %v", err)
continue
}
_, err = s.streamServer.AddStreamEntry(datastreamer.EntryType(datastream.EntryType_ENTRY_TYPE_BATCH_START), marshalledBatchStart)
if err != nil {
log.Errorf("failed to add stream entry for batch start, error: %v", err)
continue
}
err = s.streamServer.CommitAtomicOp()
if err != nil {
log.Errorf("failed to commit atomic op for batch start, error: %v", err)
continue
}
case datastream.BatchEnd:
err = s.streamServer.StartAtomicOp()
if err != nil {
log.Errorf("failed to start atomic op for batch end, error: %v", err)
continue
}
marshalledBatchEnd, err := proto.Marshal(&data)
if err != nil {
log.Errorf("failed to marshal batch end, error: %v", err)
continue
}
_, err = s.streamServer.AddStreamEntry(datastreamer.EntryType(datastream.EntryType_ENTRY_TYPE_BATCH_END), marshalledBatchEnd)
if err != nil {
log.Errorf("failed to add stream entry for batch end, error: %v", err)
continue
}
err = s.streamServer.CommitAtomicOp()
if err != nil {
log.Errorf("failed to commit atomic op for batch end, error: %v", err)
continue
}
// Invalid stream message type
default:
log.Errorf("invalid stream message type received")
}
}
}
}
func (s *Sequencer) isSynced(ctx context.Context) bool {
lastVirtualBatchNum, err := s.stateIntf.GetLastVirtualBatchNum(ctx, nil)
if err != nil && err != state.ErrNotFound {
log.Errorf("failed to get last isSynced batch, error: %v", err)
return false
}
lastTrustedBatchNum, err := s.stateIntf.GetLastBatchNumber(ctx, nil)
if err != nil && err != state.ErrNotFound {
log.Errorf("failed to get last batch num, error: %v", err)
return false
}
if lastTrustedBatchNum > lastVirtualBatchNum {
return true
}
lastEthBatchNum, err := s.etherman.GetLatestBatchNumber()
if err != nil {
log.Errorf("failed to get last eth batch, error: %v", err)
return false
}
if lastVirtualBatchNum < lastEthBatchNum {
log.Infof("waiting for the state to be synced, lastVirtualBatchNum: %d, lastEthBatchNum: %d", lastVirtualBatchNum, lastEthBatchNum)
return false
}
return true
}