@@ -225,23 +225,46 @@ func (s *RollupSyncService) parseAndUpdateRollupEventLogs(logs []types.Log, endB
225
225
batchIndex := event .BatchIndex .Uint64 ()
226
226
log .Trace ("found new FinalizeBatch event" , "batch index" , batchIndex )
227
227
228
- parentBatchMeta , chunks , err := s .getLocalInfoForBatch (batchIndex )
229
- if err != nil {
230
- return fmt .Errorf ("failed to get local node info, batch index: %v, err: %w" , batchIndex , err )
228
+ lastFinalizedBatchIndex := rawdb .ReadLastFinalizedBatchIndex (s .db )
229
+
230
+ // After darwin, FinalizeBatch event emitted every bundle, which contains multiple batches.
231
+ // Therefore there are a range of finalized batches need to be saved into db.
232
+ //
233
+ // The range logic also applies to the batches before darwin when FinalizeBatch event emitted
234
+ // per single batch. In this situation, `batchIndex` just equals to `*lastFinalizedBatchIndex + 1`
235
+ // and only one batch is processed through the for loop.
236
+ startBatchIndex := batchIndex
237
+ if lastFinalizedBatchIndex != nil {
238
+ startBatchIndex = * lastFinalizedBatchIndex + 1
239
+ } else {
240
+ log .Warn ("got nil when reading last finalized batch index. This should happen only once." )
231
241
}
232
242
233
- endBlock , finalizedBatchMeta , err := validateBatch (event , parentBatchMeta , chunks , s .bc .Config (), s .stack )
234
- if err != nil {
235
- return fmt .Errorf ("fatal: validateBatch failed: finalize event: %v, err: %w" , event , err )
236
- }
243
+ var highestFinalizedBlockNumber uint64
244
+ for index := startBatchIndex ; index <= batchIndex ; index ++ {
245
+ parentBatchMeta , chunks , err := s .getLocalInfoForBatch (index )
246
+ if err != nil {
247
+ return fmt .Errorf ("failed to get local node info, batch index: %v, err: %w" , index , err )
248
+ }
237
249
238
- rawdb .WriteFinalizedL2BlockNumber (s .db , endBlock )
239
- rawdb .WriteFinalizedBatchMeta (s .db , batchIndex , finalizedBatchMeta )
250
+ endBlock , finalizedBatchMeta , err := validateBatch (event , parentBatchMeta , chunks , s .bc .Config (), s .stack )
251
+ if err != nil {
252
+ return fmt .Errorf ("fatal: validateBatch failed: finalize event: %v, err: %w" , event , err )
253
+ }
240
254
241
- if batchIndex % 100 == 0 {
242
- log .Info ("finalized batch progress" , "batch index" , batchIndex , "finalized l2 block height" , endBlock )
255
+ rawdb .WriteFinalizedBatchMeta (s .db , index , finalizedBatchMeta )
256
+
257
+ if index % 100 == 0 {
258
+ log .Info ("finalized batch progress" , "batch index" , index , "finalized l2 block height" , endBlock )
259
+ }
260
+
261
+ highestFinalizedBlockNumber = endBlock
243
262
}
244
263
264
+ rawdb .WriteFinalizedL2BlockNumber (s .db , highestFinalizedBlockNumber )
265
+ rawdb .WriteLastFinalizedBatchIndex (s .db , batchIndex )
266
+ log .Debug ("write finalized l2 block number" , "batch index" , batchIndex , "finalized l2 block height" , highestFinalizedBlockNumber )
267
+
245
268
default :
246
269
return fmt .Errorf ("unknown event, topic: %v, tx hash: %v" , vLog .Topics [0 ].Hex (), vLog .TxHash .Hex ())
247
270
}
0 commit comments