@@ -174,10 +174,10 @@ func (f *finalizer) listenForClosingSignals(ctx context.Context) {
174174 }
175175 f .nextGERMux .Unlock ()
176176 // L2Reorg ch
177- case l2ReorgEvent := <- f .closingSignalCh .L2ReorgCh :
177+ case <- f .closingSignalCh .L2ReorgCh :
178178 log .Debug ("finalizer received L2 reorg event" )
179179 f .handlingL2Reorg = true
180- f .worker . HandleL2Reorg ( l2ReorgEvent . TxHashes )
180+ f .halt ( ctx , fmt . Errorf ( "L2 reorg event received" ) )
181181 return
182182 // Too much time without batches in L1 ch
183183 case <- f .closingSignalCh .SendingToL1TimeoutCh :
@@ -203,26 +203,23 @@ func (f *finalizer) finalizeBatches(ctx context.Context) {
203203 log .Debugf ("processing tx: %s" , tx .Hash .Hex ())
204204 err := f .processTransaction (ctx , tx )
205205 if err != nil {
206- log .Errorf ("failed to process transaction in finalizeBatches, Err: %s " , err )
206+ log .Errorf ("failed to process transaction in finalizeBatches, Err: %v " , err )
207207 }
208208
209209 f .sharedResourcesMux .Unlock ()
210210 } else {
211- if f .isBatchAlmostFull () {
212- // Wait for all transactions to be stored in the DB
213- log .Infof ("Closing batch: %d, because it's almost full." , f .batch .batchNumber )
214- // The perfect moment to finalize the batch
215- f .finalizeBatch (ctx )
216- } else {
217- // wait for new txs
218- log .Debugf ("no transactions to be processed. Sleeping for %v" , f .cfg .SleepDurationInMs .Duration )
219- if f .cfg .SleepDurationInMs .Duration > 0 {
220- time .Sleep (f .cfg .SleepDurationInMs .Duration )
221- }
211+ // wait for new txs
212+ log .Debugf ("no transactions to be processed. Sleeping for %v" , f .cfg .SleepDurationInMs .Duration )
213+ if f .cfg .SleepDurationInMs .Duration > 0 {
214+ time .Sleep (f .cfg .SleepDurationInMs .Duration )
222215 }
223216 }
224217
225- if f .isDeadlineEncountered () || f .isBatchFull () {
218+ if f .isDeadlineEncountered () {
219+ log .Infof ("Closing batch: %d, because deadline was encountered." , f .batch .batchNumber )
220+ f .finalizeBatch (ctx )
221+ } else if f .isBatchFull () || f .isBatchAlmostFull () {
222+ log .Infof ("Closing batch: %d, because it's almost full." , f .batch .batchNumber )
226223 f .finalizeBatch (ctx )
227224 }
228225
@@ -256,6 +253,24 @@ func (f *finalizer) finalizeBatch(ctx context.Context) {
256253 }
257254}
258255
256+ func (f * finalizer ) halt (ctx context.Context , err error ) {
257+ debugInfo := & state.DebugInfo {
258+ ErrorType : state .DebugInfoErrorType_FINALIZER_HALT ,
259+ Timestamp : time .Now (),
260+ Payload : err .Error (),
261+ }
262+ debugInfoErr := f .dbManager .AddDebugInfo (ctx , debugInfo , nil )
263+ if debugInfoErr != nil {
264+ log .Errorf ("error storing finalizer halt debug info: %v" , debugInfoErr )
265+ }
266+
267+ for {
268+ log .Errorf ("fatal error: %s" , err )
269+ log .Error ("halting the finalizer" )
270+ time .Sleep (5 * time .Second ) //nolint:gomnd
271+ }
272+ }
273+
259274// newWIPBatch closes the current batch and opens a new one, potentially processing forced batches between the batch is closed and the resulting new empty batch
260275func (f * finalizer ) newWIPBatch (ctx context.Context ) (* WipBatch , error ) {
261276 f .sharedResourcesMux .Lock ()
@@ -266,24 +281,23 @@ func (f *finalizer) newWIPBatch(ctx context.Context) (*WipBatch, error) {
266281 return nil , errors .New ("state root and local exit root must have value to close batch" )
267282 }
268283
284+ // Reprocess full batch as sanity check
285+ processBatchResponse , err := f .reprocessFullBatch (ctx , f .batch .batchNumber , f .batch .stateRoot )
286+ if err != nil || ! processBatchResponse .IsBatchProcessed {
287+ log .Info ("halting the finalizer because of a reprocessing error" )
288+ if err != nil {
289+ f .halt (ctx , fmt .Errorf ("failed to reprocess batch, err: %v" , err ))
290+ } else {
291+ f .halt (ctx , fmt .Errorf ("out of counters during reprocessFullBath" ))
292+ }
293+ }
294+
295+ // Close the current batch
269296 err = f .closeBatch (ctx )
270297 if err != nil {
271298 return nil , fmt .Errorf ("failed to close batch, err: %w" , err )
272299 }
273300
274- // Reprocess full batch to persist the merkle tree
275- go func () {
276- processBatchResponse , err := f .reprocessFullBatch (ctx , f .batch .batchNumber , f .batch .stateRoot )
277- if err != nil || ! processBatchResponse .IsBatchProcessed {
278- log .Info ("restarting the sequencer node because of a reprocessing error" )
279- if err != nil {
280- log .Fatalf ("failed to reprocess batch, err: %v" , err )
281- } else {
282- log .Fatal ("Out of counters during reprocessFullBath" )
283- }
284- }
285- }()
286-
287301 // Metadata for the next batch
288302 stateRoot := f .batch .stateRoot
289303 lastBatchNumber := f .batch .batchNumber
@@ -691,7 +705,7 @@ func (f *finalizer) openBatch(ctx context.Context, num uint64, ger common.Hash,
691705func (f * finalizer ) reprocessFullBatch (ctx context.Context , batchNum uint64 , expectedStateRoot common.Hash ) (* state.ProcessBatchResponse , error ) {
692706 batch , err := f .dbManager .GetBatchByNumber (ctx , batchNum , nil )
693707 if err != nil {
694- return nil , fmt .Errorf ("failed to get batch by number, err: %w " , err )
708+ return nil , fmt .Errorf ("failed to get batch by number, err: %v " , err )
695709 }
696710 processRequest := state.ProcessRequest {
697711 BatchNumber : batch .BatchNumber ,
@@ -705,7 +719,8 @@ func (f *finalizer) reprocessFullBatch(ctx context.Context, batchNum uint64, exp
705719 log .Infof ("reprocessFullBatch: BatchNumber: %d, OldStateRoot: %s, Ger: %s" , batch .BatchNumber , f .batch .initialStateRoot .String (), batch .GlobalExitRoot .String ())
706720 txs , _ , err := state .DecodeTxs (batch .BatchL2Data )
707721 if err != nil {
708- log .Error ("reprocessFullBatch: error decoding BatchL2Data before reprocessing full batch: %d. Error: %v" , batch .BatchNumber , err )
722+ log .Errorf ("reprocessFullBatch: error decoding BatchL2Data before reprocessing full batch: %d. Error: %v" , batch .BatchNumber , err )
723+ return nil , fmt .Errorf ("reprocessFullBatch: error decoding BatchL2Data before reprocessing full batch: %d. Error: %v" , batch .BatchNumber , err )
709724 }
710725 for i , tx := range txs {
711726 log .Infof ("reprocessFullBatch: Tx position %d. TxHash: %s" , i , tx .Hash ())
@@ -739,6 +754,7 @@ func (f *finalizer) reprocessFullBatch(ctx context.Context, batchNum uint64, exp
739754
740755 if result .NewStateRoot != expectedStateRoot {
741756 log .Errorf ("batchNumber: %d, reprocessed batch has different state root, expected: %s, got: %s" , batch .BatchNumber , expectedStateRoot .Hex (), result .NewStateRoot .Hex ())
757+ return nil , fmt .Errorf ("batchNumber: %d, reprocessed batch has different state root, expected: %s, got: %s" , batch .BatchNumber , expectedStateRoot .Hex (), result .NewStateRoot .Hex ())
742758 }
743759
744760 return result , nil
0 commit comments