Skip to content

SystemContract: support millisecond block generation #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions consensus/system_contract/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,34 @@ func (s *SystemContract) VerifyUncles(chain consensus.ChainReader, block *types.
}

func (s *SystemContract) CalcTimestamp(parent *types.Header) uint64 {
timestamp := parent.Time + s.config.Period
// Get the base timestamp (in seconds)
baseTimestamp := parent.Time

// Convert period to milliseconds and calculate blocks per second
// For example: if Period = 250ms = 0.25s, then periodMs = 250
periodMs := s.config.Period
blocksPerSecond := 1000 / periodMs // integer division, e.g. 1000/250 = 4
if blocksPerSecond == 0 {
blocksPerSecond = 1
}

// Calculate the block index within the current second
blockIndex := parent.Number.Uint64() % blocksPerSecond

// If this block is the last one in the current second, increment the timestamp
// We compare with blocksPerSecond-1 because blockIndex is 0-based
if blockIndex == blocksPerSecond-1 {
baseTimestamp++
}

// If RelaxedPeriod is enabled, always set the header timestamp to now (ie the time we start building it) as
// we don't know when it will be sealed
if s.config.RelaxedPeriod || timestamp < uint64(time.Now().Unix()) {
timestamp = uint64(time.Now().Unix())
nowTimestamp := uint64(time.Now().Unix())
if s.config.RelaxedPeriod || baseTimestamp < nowTimestamp {
baseTimestamp = nowTimestamp
Comment on lines +251 to +253
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can revert these changes to make total diff smaller.

}

return timestamp
return baseTimestamp
}

// Prepare initializes the consensus fields of a block header according to the
Expand Down
21 changes: 18 additions & 3 deletions miner/scroll_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,24 @@ func (w *worker) newWork(now time.Time, parentHash common.Hash, reorging bool, r
// clique with relaxed period uses time.Now() as the header.Time, calculate the deadline
deadline = time.Unix(int64(header.Time+w.chainConfig.Clique.Period), 0)
}
if w.chainConfig.SystemContract != nil && w.chainConfig.SystemContract.RelaxedPeriod {
// system contract with relaxed period uses time.Now() as the header.Time, calculate the deadline
deadline = time.Unix(int64(header.Time+w.chainConfig.SystemContract.Period), 0)
if w.chainConfig.SystemContract != nil {
periodMs := w.chainConfig.SystemContract.Period
blocksPerSecond := uint64(1000) / periodMs
if blocksPerSecond == 0 {
blocksPerSecond = 1
}
Comment on lines +562 to +565
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add this as a utility function since it's used in 2 places.


// Calculate the actual timing based on block number within the current second
blockIndex := header.Number.Uint64() % blocksPerSecond
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
blockIndex := header.Number.Uint64() % blocksPerSecond
blockIndex := (header.Number.Uint64() + 1) % blocksPerSecond


// Calculate base time and add the fraction of a second based on block index
baseTimeNano := int64(header.Time) * int64(time.Second)
fractionNano := int64(blockIndex) * int64(periodMs) * int64(time.Millisecond)

// Add one period to determine the deadline
nextBlockNano := baseTimeNano + fractionNano + int64(periodMs)*int64(time.Millisecond)

deadline = time.Unix(0, nextBlockNano)
}

w.current = &work{
Expand Down
2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func (c *CliqueConfig) String() string {

// SystemContractConfig is the consensus engine configs for rollup sequencer sealing.
type SystemContractConfig struct {
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Period uint64 `json:"period"` // Number of milliseconds between blocks to enforce

SystemContractAddress common.Address `json:"system_contract_address"` // address of system contract on L1
SystemContractSlot common.Hash `json:"system_contract_slot"` // slot of signer address in system contract on L1
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 8 // Minor version component of the current release
VersionPatch = 40 // Patch version component of the current release
VersionPatch = 41 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
Loading