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

Commit

Permalink
feat(driver): improve ProcessL1Blocks for reorg handling (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Jul 23, 2023
1 parent 790f2f2 commit f740013
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
21 changes: 20 additions & 1 deletion driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,29 @@ func (s *Syncer) ProcessL1Blocks(ctx context.Context, l1End *types.Header) error
s.reorgDetectedFlag = false
firstTry = false

startHeight := s.state.GetL1Current().Number
// If there is a L1 reorg, sometimes this will happen.
if startHeight.Uint64() >= l1End.Number.Uint64() {
startHeight = new(big.Int).Sub(l1End.Number, common.Big1)
newL1Current, err := s.rpc.L1.HeaderByNumber(ctx, startHeight)
if err != nil {
return err
}
s.state.SetL1Current(newL1Current)
s.lastInsertedBlockID = nil

log.Info(
"Reorg detected",
"oldL1Current", s.state.GetL1Current().Number,
"newL1Current", startHeight,
"l1Head", l1End.Number,
)
}

iter, err := eventIterator.NewBlockProposedIterator(ctx, &eventIterator.BlockProposedIteratorConfig{
Client: s.rpc.L1,
TaikoL1: s.rpc.TaikoL1,
StartHeight: s.state.GetL1Current().Number,
StartHeight: startHeight,
EndHeight: l1End.Number,
FilterQuery: nil,
OnBlockProposedEvent: s.onBlockProposed,
Expand Down
47 changes: 44 additions & 3 deletions pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,33 @@ func (c *Client) CheckL1Reorg(ctx context.Context, blockID *big.Int) (bool, *typ

l1Origin, err := c.L2.L1OriginByID(ctx, blockID)
if err != nil {
// If the L2 EE is just synced through P2P, there is a chance that the EE do not have
// the chain head L1Origin information recorded.
if err.Error() == ethereum.NotFound.Error() {
log.Info("L1Origin not found", "blockID", blockID)
return false, nil, nil, nil

// If the L2 EE is just synced through P2P, there is a chance that the EE do not have
// the chain head L1Origin information recorded.
justSyncedByP2P, err := c.IsJustSyncedByP2P(ctx)
if err != nil {
return false,
nil,
nil,
fmt.Errorf("failed to check whether the L2 execution engine has just finished a P2P sync: %w", err)
}

log.Info(
"Check whether the L2 execution engine has just finished a P2P sync",
"justSyncedByP2P",
justSyncedByP2P,
)

if justSyncedByP2P {
return false, nil, nil, nil
}

log.Info("Reorg detected due to L1Origin not found", "blockID", blockID)
reorged = true
blockID = new(big.Int).Sub(blockID, common.Big1)
continue
}
return false, nil, nil, err
}
Expand Down Expand Up @@ -401,3 +423,22 @@ func (c *Client) CheckL1Reorg(ctx context.Context, blockID *big.Int) (bool, *typ

return reorged, l1CurrentToReset, blockIDToReset, nil
}

// IsJustSyncedByP2P checks whether the given L2 execution engine has just finished a P2P
// sync.
func (c *Client) IsJustSyncedByP2P(ctx context.Context) (bool, error) {
l2Head, err := c.L2.HeaderByNumber(ctx, nil)
if err != nil {
return false, err
}

if _, err = c.L2.L1OriginByID(ctx, l2Head.Number); err != nil {
if err.Error() == ethereum.NotFound.Error() {
return true, nil
}

return false, err
}

return false, nil
}

0 comments on commit f740013

Please sign in to comment.