Skip to content

Commit 748fb82

Browse files
authored
fix: accept header at most 100ms earlier than timestamp (#1129)
* fix: address timing issue in system config worker * make code easier to understand * add 100ms header verification grace period to Clique
1 parent 40ebbd6 commit 748fb82

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

consensus/clique/clique.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,10 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
254254
}
255255
number := header.Number.Uint64()
256256

257-
// Don't waste time checking blocks from the future
258-
if header.Time > uint64(time.Now().Unix()) {
257+
// Don't waste time checking blocks from the future.
258+
// We add 100ms leeway since the scroll_worker internal timers might trigger early.
259+
now := time.Now()
260+
if header.Time > uint64(now.Unix()) && time.Unix(int64(header.Time), 0).Sub(now) > 100*time.Millisecond {
259261
return consensus.ErrFutureBlock
260262
}
261263
// Checkpoint blocks need to enforce zero beneficiary

consensus/system_contract/consensus.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ func (s *SystemContract) verifyHeader(chain consensus.ChainHeaderReader, header
115115
return errUnknownBlock
116116
}
117117

118-
// Don't waste time checking blocks from the future
119-
if header.Time > uint64(time.Now().Unix()) {
118+
// Don't waste time checking blocks from the future.
119+
// We add 100ms leeway since the scroll_worker internal timers might trigger early.
120+
now := time.Now()
121+
if header.Time > uint64(now.Unix()) && time.Unix(int64(header.Time), 0).Sub(now) > 100*time.Millisecond {
120122
return consensus.ErrFutureBlock
121123
}
122124
// Ensure that the coinbase is zero

miner/scroll_worker.go

+5
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ func (w *worker) collectPendingL1Messages(startIndex uint64) []types.L1MessageTx
464464
if len(l1MessagesV1) > 0 {
465465
// backdate the block to the parent block's timestamp -> not yet EuclidV2
466466
// TODO: might need to re-run Prepare here
467+
log.Warn("Back-labeling header timestamp to ensure it precedes the EuclidV2 transition", "blockNumber", w.current.header.Number, "oldTime", w.current.header.Time, "newTime", parent.Time)
467468
w.current.header.Time = parent.Time
468469
return l1MessagesV1
469470
}
@@ -541,6 +542,10 @@ func (w *worker) newWork(now time.Time, parentHash common.Hash, reorging bool, r
541542
// clique with relaxed period uses time.Now() as the header.Time, calculate the deadline
542543
deadline = time.Unix(int64(header.Time+w.chainConfig.Clique.Period), 0)
543544
}
545+
if w.chainConfig.SystemContract != nil && w.chainConfig.SystemContract.RelaxedPeriod {
546+
// system contract with relaxed period uses time.Now() as the header.Time, calculate the deadline
547+
deadline = time.Unix(int64(header.Time+w.chainConfig.SystemContract.Period), 0)
548+
}
544549

545550
w.current = &work{
546551
deadlineTimer: time.NewTimer(time.Until(deadline)),

params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 5 // Major version component of the current release
2626
VersionMinor = 8 // Minor version component of the current release
27-
VersionPatch = 18 // Patch version component of the current release
27+
VersionPatch = 19 // Patch version component of the current release
2828
VersionMeta = "mainnet" // Version metadata to append to the version string
2929
)
3030

0 commit comments

Comments
 (0)