Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
op-service/node: Add time.Since to Clock & use it
Browse files Browse the repository at this point in the history
trianglesphere committed Jan 19, 2024

Verified

This commit was signed with the committer’s verified signature.
rubvs Ruben van Staden
1 parent 16951a5 commit 32cb7a8
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
@@ -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
}

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

0 comments on commit 32cb7a8

Please sign in to comment.