Skip to content

Commit

Permalink
feat(taiko-client): improve chain head check in NeedReSync (#17431)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha authored May 30, 2024
1 parent ae64be4 commit 4dbe0af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,37 @@ func (t *SyncProgressTracker) ClearMeta() {
// 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
// 3, if the last triggered beacon sync is finished, but there are still new blocks
func (t *SyncProgressTracker) NeedReSync(newID *big.Int) bool {
func (t *SyncProgressTracker) NeedReSync(newID *big.Int) (bool, error) {
t.mutex.RLock()
defer t.mutex.RUnlock()

// If the beacon sync has not been triggered yet, we will simply trigger it.
if !t.triggered {
return true
return true, nil
}

if t.lastSyncedBlockID == nil {
return true
return true, nil
}

// If the new block is 64 blocks ahead of the last synced block, we will trigger a new beacon sync.
if new(big.Int).Sub(newID, t.lastSyncedBlockID).Cmp(gapToResync) >= 0 {
return true
return true, nil
}

head, err := t.client.HeaderByNumber(context.Background(), nil)
if err != nil {
return false, err
}

// If the last triggered beacon sync is finished, we will trigger a new beacon sync.
if t.lastSyncProgress != nil && t.lastSyncProgress.CurrentBlock >= t.lastSyncedBlockID.Uint64() {
return true
if t.lastSyncProgress != nil &&
(t.lastSyncProgress.CurrentBlock >= t.lastSyncedBlockID.Uint64() ||
head.Number.Uint64() >= t.lastSyncedBlockID.Uint64()) {
return true, nil
}

return false
return false, nil
}

// OutOfSync tells whether the L2 execution engine is marked as out of sync.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func NewSyncer(
// latest verified block has changed.
func (s *Syncer) TriggerBeaconSync(blockID uint64) error {
// If we don't need to trigger another beacon sync, just return.
if !s.progressTracker.NeedReSync(new(big.Int).SetUint64(blockID)) {
needResync, err := s.progressTracker.NeedReSync(new(big.Int).SetUint64(blockID))
if err != nil {
return err
}
if !needResync {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion packages/taiko-client/driver/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (s *State) setL2Head(l2Head *types.Header) {
return
}

log.Debug("New L2 head", "height", l2Head.Number, "hash", l2Head.Hash(), "timestamp", l2Head.Time)
log.Trace("New L2 head", "height", l2Head.Number, "hash", l2Head.Hash(), "timestamp", l2Head.Time)
metrics.DriverL2HeadHeightGauge.Set(float64(l2Head.Number.Uint64()))

s.l2Head.Store(l2Head)
Expand Down

0 comments on commit 4dbe0af

Please sign in to comment.