@@ -42,11 +42,15 @@ const (
42
42
43
43
// defaultLogInterval is the frequency at which we print the latest processed block.
44
44
defaultLogInterval = 5 * time .Minute
45
+
46
+ // rewindL1Height is the number of blocks to rewind the L1 sync height when a missing batch event is detected.
47
+ rewindL1Height = 100
45
48
)
46
49
47
50
var (
48
51
finalizedBlockGauge = metrics .NewRegisteredGauge ("chain/head/finalized" , nil )
49
52
ErrShouldResetSyncHeight = errors .New ("ErrShouldResetSyncHeight" )
53
+ ErrMissingBatchEvent = errors .New ("ErrMissingBatchEvent" )
50
54
)
51
55
52
56
// RollupSyncService collects ScrollChain batch commit/revert/finalize events and stores metadata into db.
@@ -223,6 +227,22 @@ func (s *RollupSyncService) fetchRollupEvents() error {
223
227
224
228
return nil
225
229
}
230
+ if errors .Is (err , ErrMissingBatchEvent ) {
231
+ // make sure no underflow
232
+ var rewindTo uint64
233
+ if prevL1Height > rewindL1Height {
234
+ rewindTo = prevL1Height - rewindL1Height
235
+ }
236
+
237
+ // If there's a missing batch event, rewind the L1 sync height by some blocks to re-fetch from L1 RPC and
238
+ // replay creating corresponding CommittedBatchMeta in local DB.
239
+ // This happens recursively until the missing event has been recovered as we will call fetchRollupEvents again
240
+ // with the `L1Height = prevL1Height - rewindL1Height`.
241
+ s .callDataBlobSource .SetL1Height (rewindTo )
242
+
243
+ return fmt .Errorf ("missing batch event, rewinding L1 sync height by %d blocks to %d: %w" , rewindL1Height , rewindTo , err )
244
+ }
245
+
226
246
// Reset the L1 height to the previous value to retry fetching the same data.
227
247
s .callDataBlobSource .SetL1Height (prevL1Height )
228
248
return fmt .Errorf ("failed to parse and update rollup event logs: %w" , err )
@@ -297,12 +317,18 @@ func (s *RollupSyncService) updateRollupEvents(daEntries da.Entries) error {
297
317
var err error
298
318
if index > 0 {
299
319
if parentCommittedBatchMeta , err = rawdb .ReadCommittedBatchMeta (s .db , index - 1 ); err != nil {
300
- return fmt .Errorf ("failed to read parent committed batch meta, batch index: %v, err: %w" , index - 1 , err )
320
+ return fmt .Errorf ("failed to read parent committed batch meta, batch index: %v, err: %w" , index - 1 , errors .Join (ErrMissingBatchEvent , err ))
321
+ }
322
+ if parentCommittedBatchMeta == nil {
323
+ return fmt .Errorf ("parent committed batch meta = nil, batch index: %v, err: %w" , index - 1 , ErrMissingBatchEvent )
301
324
}
302
325
}
303
326
committedBatchMeta , err := rawdb .ReadCommittedBatchMeta (s .db , index )
304
327
if err != nil {
305
- return fmt .Errorf ("failed to read committed batch meta, batch index: %v, err: %w" , index , err )
328
+ return fmt .Errorf ("failed to read committed batch meta, batch index: %v, err: %w" , index , errors .Join (ErrMissingBatchEvent , err ))
329
+ }
330
+ if committedBatchMeta == nil {
331
+ return fmt .Errorf ("committed batch meta = nil, batch index: %v, err: %w" , index , ErrMissingBatchEvent )
306
332
}
307
333
308
334
chunks , err := s .getLocalChunksForBatch (committedBatchMeta .ChunkBlockRanges )
@@ -447,7 +473,10 @@ func (s *RollupSyncService) getCommittedBatchMeta(commitedBatch da.EntryWithBloc
447
473
if commitedBatch .Version () == encoding .CodecV7 {
448
474
parentCommittedBatchMeta , err := rawdb .ReadCommittedBatchMeta (s .db , commitedBatch .BatchIndex ()- 1 )
449
475
if err != nil {
450
- return nil , fmt .Errorf ("failed to read parent committed batch meta, batch index: %v, err: %w" , commitedBatch .BatchIndex ()- 1 , err )
476
+ return nil , fmt .Errorf ("failed to read parent committed batch meta, batch index: %v, err: %w" , commitedBatch .BatchIndex ()- 1 , errors .Join (ErrMissingBatchEvent , err ))
477
+ }
478
+ if parentCommittedBatchMeta == nil {
479
+ return nil , fmt .Errorf ("parent committed batch meta = nil, batch index: %v, err: %w" , commitedBatch .BatchIndex ()- 1 , ErrMissingBatchEvent )
451
480
}
452
481
453
482
// If parent batch has a lower version this means this is the first batch of CodecV7.
0 commit comments