@@ -74,6 +74,9 @@ const (
7474
7575 // staleThreshold is the maximum depth of the acceptable stale block.
7676 staleThreshold = 7
77+
78+ maxClockSkewSeconds = 600 // 10 mins for now
79+ timestampDelaySeconds = 300 // 5 mins for now
7780)
7881
7982// environment is the worker's current environment and holds all of the current state information.
@@ -220,6 +223,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
220223 go worker .newWorkLoop (recommit )
221224 go worker .resultLoop ()
222225 go worker .taskLoop ()
226+ go worker .timestampLoop ()
223227
224228 // Submit first work to initialize pending state.
225229 if init {
@@ -362,6 +366,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
362366 timer .Reset (recommit )
363367 continue
364368 }
369+ timestamp = w .chain .CurrentTimestamp ()
365370 commit (true , commitInterruptResubmit )
366371 }
367372
@@ -613,6 +618,31 @@ func (w *worker) resultLoop() {
613618 }
614619}
615620
621+ // timestampLoop is a loop that updates the timestamp used for blocks when it
622+ // is stale by more than a certain threshold.
623+ // TODO: Re-think this as everything comes together more.
624+ func (w * worker ) timestampLoop () {
625+ timer := time .NewTimer (0 )
626+
627+ for {
628+ select {
629+ case <- timer .C :
630+ currentTime := time .Now ().Unix ()
631+ skew := currentTime - w .chain .CurrentTimestamp ()
632+ if skew > maxClockSkewSeconds {
633+ newTime := currentTime - timestampDelaySeconds
634+ w .chain .SetCurrentTimestamp (newTime )
635+ timer .Reset ((maxClockSkewSeconds - timestampDelaySeconds ) * time .Second )
636+ log .Debug ("timestamp above max clock skew" , "maxSkew" , maxClockSkewSeconds , "overBy" , timestampDelaySeconds , "newTime" , newTime )
637+ } else {
638+ timer .Reset (time .Duration (maxClockSkewSeconds - skew ) * time .Second )
639+ }
640+ case <- w .exitCh :
641+ return
642+ }
643+ }
644+ }
645+
616646// makeCurrent creates a new environment for the current cycle.
617647func (w * worker ) makeCurrent (parent * types.Block , header * types.Header ) error {
618648 state , err := w .chain .StateAt (parent .Root ())
0 commit comments