From a05c779f6efc6fe434aeba74afd6998b455f701f Mon Sep 17 00:00:00 2001 From: Jeb Bearer Date: Wed, 4 Oct 2023 17:54:33 +0200 Subject: [PATCH] Fail in processSequenceBatches if we don't have the parent batch This will cause an error if there has been a reorg since we fetched the batches. In normal mode, this should never happen, since we check for reorgs and then fetch batches synchronously. But in preconfirmations mode, the batches are fetched asynchronously wrt to the reorg detection, and so the L2 head can change under us. In this case the new check will fail and we will re-fetch preconfirmations after checking the new L2 head. Fixes EspressoSystems/espresso-polygon-zkevm-demo#246 --- synchronizer/synchronizer.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/synchronizer/synchronizer.go b/synchronizer/synchronizer.go index 195519d..9389f4e 100644 --- a/synchronizer/synchronizer.go +++ b/synchronizer/synchronizer.go @@ -611,6 +611,23 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman. return nil } + if lastBatch, err := s.state.GetLastBatchNumber(s.ctx, dbTx); err != nil { + log.Errorf("Error fetching previous batch number: %w", err) + rollbackErr := dbTx.Rollback(s.ctx) + if rollbackErr != nil { + log.Fatalf("error rolling back state. rollbackErr: %s, error : %w", rollbackErr.Error(), err) + } + return err + } else if sequencedBatches[0].BatchNumber != lastBatch + 1 { + err = fmt.Errorf("New batch %d is not successor of last synced batch %d, there may have been a reorg", sequencedBatches[0].BatchNumber, lastBatch) + log.Warnf("Possible reorg detected: %w", err) + rollbackErr := dbTx.Rollback(s.ctx) + if rollbackErr != nil { + log.Fatalf("error rolling back state. rollbackErr: %s, error : %w", rollbackErr.Error(), err) + } + return err + } + for _, sbatch := range sequencedBatches { // Ensure the L1 origin for this batch is in the database. Since the L1 origin assigned by // HotShot is not necessarily the same as an L1 block which we added to the database as a