Skip to content

Commit

Permalink
Fail in processSequenceBatches if we don't have the parent batch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jbearer committed Oct 4, 2023
1 parent 57bd549 commit 24f34c1
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,32 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
return nil
}

lastBatch, err := s.state.GetLastBatchNumber(s.ctx, dbTx)
if 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. BlockNumber: %d, rollbackErr: %s, error : %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
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. BlockNumber: %d, rollbackErr: %s, error : %w", blockNumber, rollbackErr.Error(), err)
}
return err
}

prevTimestamp, err := s.state.GetLastBatchTime(s.ctx, dbTx)
if err != nil {
log.Warn("Error fetching previous timestamp")
log.Errorf("Error fetching previous timestamp: %w", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state. BlockNumber: %d, rollbackErr: %s, error : %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
for _, sbatch := range sequencedBatches {
Expand Down

0 comments on commit 24f34c1

Please sign in to comment.