Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(driver): check the mismatch of last verified block #296

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.checkLastVerifiedBlockMismatch(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 {
Expand Down Expand Up @@ -427,3 +450,15 @@ func (s *Syncer) createExecutionPayloads(

return payload, nil
}

// checkLastVerifiedBlockMismatch checks if there is a mismatch between protocol's last verified block hash and
// the corresponding L2 EE block hash.
func (s *Syncer) checkLastVerifiedBlockMismatch(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
}