Skip to content

Commit

Permalink
op-service/node: Add time.Since to Clock & use it
Browse files Browse the repository at this point in the history
  • Loading branch information
trianglesphere committed Jan 19, 2024
1 parent e15871d commit 81a7a87
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions op-node/rollup/derive/engine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -50,6 +51,7 @@ type EngineController struct {
syncStatus syncStatusEnum
rollupCfg *rollup.Config
elStart time.Time
clock clock.Clock

// Block Head State
unsafeHead eth.L2BlockRef
Expand Down Expand Up @@ -78,6 +80,7 @@ func NewEngineController(engine ExecEngine, log log.Logger, metrics Metrics, rol
rollupCfg: rollupCfg,
syncMode: syncMode,
syncStatus: syncStatus,
clock: clock.SystemClock,
}
}

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions op-service/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions op-service/clock/deterministic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 81a7a87

Please sign in to comment.