diff --git a/driver/chain_syncer/calldata/syncer.go b/driver/chain_syncer/calldata/syncer.go index ffb8a0ddd..5efc66f81 100644 --- a/driver/chain_syncer/calldata/syncer.go +++ b/driver/chain_syncer/calldata/syncer.go @@ -116,12 +116,35 @@ func (s *Syncer) onBlockProposed( if !s.progressTracker.Triggered() { // Check whteher we need to reorg the L2 chain at first. - reorged, l1CurrentToReset, lastInsertedBlockIDToReset, err := s.rpc.CheckL1Reorg( - ctx, - new(big.Int).Sub(event.Id, common.Big1), + // 1. Last verified block + var ( + reorged bool + l1CurrentToReset *types.Header + lastInsertedBlockIDToReset *big.Int + err error ) + reorged, err = s.checkLastVerifiedBlockL1Reorged(ctx) if err != nil { - return fmt.Errorf("failed to check whether L1 chain has been reorged: %w", err) + return fmt.Errorf("failed to check if last verified block in L2 EE has been reorged: %w", err) + } + + // 2. Parent block + if reorged { + genesisL1Header, err := s.rpc.GetGenesisL1Header(ctx) + if err != nil { + return fmt.Errorf("failed to fetch genesis L1 header: %w", err) + } + + l1CurrentToReset = genesisL1Header + lastInsertedBlockIDToReset = common.Big0 + } else { + reorged, l1CurrentToReset, lastInsertedBlockIDToReset, err = s.rpc.CheckL1Reorg( + ctx, + new(big.Int).Sub(event.Id, common.Big1), + ) + if err != nil { + return fmt.Errorf("failed to check whether L1 chain has been reorged: %w", err) + } } if reorged { @@ -427,3 +450,13 @@ func (s *Syncer) createExecutionPayloads( return payload, nil } + +func (s *Syncer) checkLastVerifiedBlockL1Reorged(ctx context.Context) (bool, error) { + lastVerifiedBlockInfo := s.state.GetLatestVerifiedBlock() + l2Header, err := s.rpc.L2.HeaderByNumber(ctx, lastVerifiedBlockInfo.Height) + if err != nil { + return false, err + } + + return l2Header.Hash() != lastVerifiedBlockInfo.Hash, nil +}