diff --git a/op-node/rollup/derive/engine_controller.go b/op-node/rollup/derive/engine_controller.go index 5a463082862ab..a77ee8a3876c7 100644 --- a/op-node/rollup/derive/engine_controller.go +++ b/op-node/rollup/derive/engine_controller.go @@ -9,6 +9,7 @@ import ( "github.com/ethereum-optimism/optimism/op-node/rollup" "github.com/ethereum-optimism/optimism/op-node/rollup/async" "github.com/ethereum-optimism/optimism/op-node/rollup/sync" + "github.com/ethereum-optimism/optimism/op-service/clock" "github.com/ethereum-optimism/optimism/op-service/eth" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" @@ -50,6 +51,7 @@ type EngineController struct { syncStatus syncStatusEnum rollupCfg *rollup.Config elStart time.Time + clock clock.Clock // Block Head State unsafeHead eth.L2BlockRef @@ -78,6 +80,7 @@ func NewEngineController(engine ExecEngine, log log.Logger, metrics Metrics, rol rollupCfg: rollupCfg, syncMode: syncMode, syncStatus: syncStatus, + clock: clock.SystemClock, } } @@ -297,7 +300,7 @@ func (e *EngineController) InsertUnsafePayload(ctx context.Context, envelope *et if errors.Is(err, ethereum.NotFound) { e.syncStatus = syncStatusStartedEL e.log.Info("Starting EL sync") - e.elStart = time.Now() + e.elStart = e.clock.Now() } else if err == nil { e.syncStatus = syncStatusFinishedEL e.log.Info("Skipping EL sync and going straight to CL sync because there is a finalized block", "id", b.ID()) @@ -352,7 +355,7 @@ func (e *EngineController) InsertUnsafePayload(ctx context.Context, envelope *et e.needFCUCall = false if e.syncStatus == syncStatusFinishedELButNotFinalized { - e.log.Info("Finished EL sync", "sync_duration", time.Since(e.elStart)) + e.log.Info("Finished EL sync", "sync_duration", e.clock.Since(e.elStart)) e.syncStatus = syncStatusFinishedEL } diff --git a/op-service/clock/clock.go b/op-service/clock/clock.go index 834d2984c911b..d38eaa2569089 100644 --- a/op-service/clock/clock.go +++ b/op-service/clock/clock.go @@ -13,6 +13,9 @@ type Clock interface { // Now provides the current local time. Equivalent to time.Now Now() time.Time + // Since returns the time elapsed since t. It is shorthand for time.Now().Sub(t). + Since(time.Time) time.Duration + // After waits for the duration to elapse and then sends the current time on the returned channel. // It is equivalent to time.After After(d time.Duration) <-chan time.Time @@ -81,6 +84,10 @@ func (s systemClock) Now() time.Time { return time.Now() } +func (s systemClock) Since(t time.Time) time.Duration { + return time.Since(t) +} + func (s systemClock) After(d time.Duration) <-chan time.Time { return time.After(d) } diff --git a/op-service/clock/deterministic.go b/op-service/clock/deterministic.go index 8d28fb9173409..ce11773232ce2 100644 --- a/op-service/clock/deterministic.go +++ b/op-service/clock/deterministic.go @@ -138,6 +138,12 @@ func (s *DeterministicClock) Now() time.Time { return s.now } +func (s *DeterministicClock) Since(t time.Time) time.Duration { + s.lock.Lock() + defer s.lock.Unlock() + return s.now.Sub(t) +} + func (s *DeterministicClock) After(d time.Duration) <-chan time.Time { s.lock.Lock() defer s.lock.Unlock()