Skip to content

Commit

Permalink
remove some unnecssary check or set
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Jan 10, 2025
1 parent a170740 commit 1eeda92
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,6 @@ func (t *SyncProgressTracker) UpdateMeta(id *big.Int, blockHash common.Hash) {
t.lastSyncedBlockHash = blockHash
}

// ClearMeta cleans the inner beacon sync metadata.
func (t *SyncProgressTracker) ClearMeta() {
t.mutex.Lock()
defer t.mutex.Unlock()

log.Debug("Clear sync progress tracker meta")

t.triggered = false
}

// NeedReSync checks if a new beacon sync request will be needed:
// 1, if the beacon sync has not been triggered yet
// 2, if there is 64 blocks gap between the last head to sync and the new block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ func (s *BeaconSyncProgressTrackerTestSuite) TestSyncProgressed() {
s.True(syncProgressed(&ethereum.SyncProgress{HealingBytecode: 0}, &ethereum.SyncProgress{HealingBytecode: 1}))
}

func (s *BeaconSyncProgressTrackerTestSuite) TestClearMeta() {
s.t.triggered = true
s.t.ClearMeta()
s.False(s.t.triggered)
}

func (s *BeaconSyncProgressTrackerTestSuite) TestHeadChanged() {
s.True(s.t.NeedReSync(common.Big256))
s.t.triggered = true
Expand Down
64 changes: 24 additions & 40 deletions packages/taiko-client/driver/chain_syncer/blob/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,28 @@ func (s *Syncer) onBlockProposed(

// If we are not inserting a block whose parent block is the latest verified block in protocol,
// and the node hasn't just finished the P2P sync, we check if the L1 chain has been reorged.
if !s.progressTracker.Triggered() {
reorgCheckResult, err := s.checkReorg(ctx, meta.GetBlockID())
if err != nil {
return err
}
reorgCheckResult, err := s.checkReorg(ctx, meta.GetBlockID())
if err != nil {
return err
}
if reorgCheckResult.IsReorged {
log.Info(
"Reset L1Current cursor due to L1 reorg",
"l1CurrentHeightOld", s.state.GetL1Current().Number,
"l1CurrentHashOld", s.state.GetL1Current().Hash(),
"l1CurrentHeightNew", reorgCheckResult.L1CurrentToReset.Number,
"l1CurrentHashNew", reorgCheckResult.L1CurrentToReset.Hash(),
"lastInsertedBlockIDOld", s.lastInsertedBlockID,
"lastInsertedBlockIDNew", reorgCheckResult.LastHandledBlockIDToReset,
)
s.state.SetL1Current(reorgCheckResult.L1CurrentToReset)
s.lastInsertedBlockID = reorgCheckResult.LastHandledBlockIDToReset
s.reorgDetectedFlag = true
endIter()

if reorgCheckResult.IsReorged {
log.Info(
"Reset L1Current cursor due to L1 reorg",
"l1CurrentHeightOld", s.state.GetL1Current().Number,
"l1CurrentHashOld", s.state.GetL1Current().Hash(),
"l1CurrentHeightNew", reorgCheckResult.L1CurrentToReset.Number,
"l1CurrentHashNew", reorgCheckResult.L1CurrentToReset.Hash(),
"lastInsertedBlockIDOld", s.lastInsertedBlockID,
"lastInsertedBlockIDNew", reorgCheckResult.LastHandledBlockIDToReset,
)
s.state.SetL1Current(reorgCheckResult.L1CurrentToReset)
s.lastInsertedBlockID = reorgCheckResult.LastHandledBlockIDToReset
s.reorgDetectedFlag = true
endIter()

return nil
}
return nil
}

// Ignore those already inserted blocks.
if s.lastInsertedBlockID != nil && meta.GetBlockID().Cmp(s.lastInsertedBlockID) <= 0 {
return nil
Expand All @@ -221,20 +219,11 @@ func (s *Syncer) onBlockProposed(

// Fetch the L2 parent block, if the node is just finished a P2P sync, we simply use the tracker's
// last synced verified block as the parent, otherwise, we fetch the parent block from L2 EE.
var (
parent *types.Header
err error
)
if s.progressTracker.Triggered() {
// Already synced through beacon sync, just skip this event.
if meta.GetBlockID().Cmp(s.progressTracker.LastSyncedBlockID()) <= 0 {
return nil
}

parent, err = s.rpc.L2.HeaderByHash(ctx, s.progressTracker.LastSyncedBlockHash())
} else {
parent, err = s.rpc.L2ParentByBlockID(ctx, meta.GetBlockID())
// Already synced through beacon sync, just skip this event.
if meta.GetBlockID().Cmp(s.progressTracker.LastSyncedBlockID()) <= 0 {
return nil
}
parent, err := s.rpc.L2.HeaderByHash(ctx, s.progressTracker.LastSyncedBlockHash())
if err != nil {
return fmt.Errorf("failed to fetch L2 parent block: %w", err)
}
Expand All @@ -243,7 +232,6 @@ func (s *Syncer) onBlockProposed(
"Parent block",
"blockID", parent.Number,
"hash", parent.Hash(),
"beaconSyncTriggered", s.progressTracker.Triggered(),
)

tx, err := s.rpc.L1.TransactionInBlock(ctx, meta.GetRawBlockHash(), meta.GetTxIndex())
Expand Down Expand Up @@ -299,10 +287,6 @@ func (s *Syncer) onBlockProposed(
metrics.DriverL1CurrentHeightGauge.Set(float64(meta.GetRawBlockHeight().Uint64()))
s.lastInsertedBlockID = meta.GetBlockID()

if s.progressTracker.Triggered() {
s.progressTracker.ClearMeta()
}

return nil
}

Expand Down
46 changes: 18 additions & 28 deletions packages/taiko-client/driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,28 @@ func (s *L2ChainSyncer) Sync() error {
// we will only check and trigger P2P sync progress once right after the driver starts
s.progressTracker.MarkFinished()

// We have triggered at least a beacon sync in L2 execution engine, we should reset the L1Current
// cursor at first, before start inserting pending L2 blocks one by one.
if s.progressTracker.Triggered() {
log.Info(
"Switch to insert pending blocks one by one",
"p2pEnabled", s.p2pSync,
"p2pOutOfSync", s.progressTracker.OutOfSync(),
)

// Get the execution engine's chain head.
l2Head, err := s.rpc.L2.HeaderByNumber(s.ctx, nil)
if err != nil {
return fmt.Errorf("failed to get L2 chain head: %w", err)
}

log.Info(
"L2 head information",
"number", l2Head.Number,
"hash", l2Head.Hash(),
"lastSyncedBlockID", s.progressTracker.LastSyncedBlockID(),
"lastSyncedBlockHash", s.progressTracker.LastSyncedBlockHash(),
)
// Get the execution engine's chain head.
l2Head, err := s.rpc.L2.HeaderByNumber(s.ctx, nil)
if err != nil {
return fmt.Errorf("failed to get L2 chain head: %w", err)
}

// Reset the L1Current cursor.
if err := s.state.ResetL1Current(s.ctx, l2Head.Number); err != nil {
return fmt.Errorf("failed to reset L1 current cursor: %w", err)
}
log.Info(
"L2 head information",
"number", l2Head.Number,
"hash", l2Head.Hash(),
"lastSyncedBlockID", s.progressTracker.LastSyncedBlockID(),
"lastSyncedBlockHash", s.progressTracker.LastSyncedBlockHash(),
)

// Reset to the latest L2 execution engine's chain status.
s.progressTracker.UpdateMeta(l2Head.Number, l2Head.Hash())
// Reset the L1Current cursor.
if err := s.state.ResetL1Current(s.ctx, l2Head.Number); err != nil {
return fmt.Errorf("failed to reset L1 current cursor: %w", err)
}

// Reset to the latest L2 execution engine's chain status.
s.progressTracker.UpdateMeta(l2Head.Number, l2Head.Hash())

// Insert the proposed block one by one.
return s.blobSyncer.ProcessL1Blocks(s.ctx)
}
Expand Down

0 comments on commit 1eeda92

Please sign in to comment.