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 30 commits into
base: develop
Choose a base branch
from

Conversation

georgehao
Copy link
Member

@georgehao georgehao commented Apr 27, 2025

1. Purpose or design rationale of this PR

...

2. PR title

Your PR title must follow conventional commits (as we are doing squash merge for each PR), so it must start with one of the following types:

  • build: Changes that affect the build system or external dependencies (example scopes: yarn, eslint, typescript)
  • ci: Changes to our CI configuration files and scripts (example scopes: vercel, github, cypress)
  • docs: Documentation-only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that doesn't fix a bug, or add a feature, or improves performance
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • test: Adding missing tests or correcting existing tests

3. Deployment tag versioning

Has the version in params/version.go been updated?

  • This PR doesn't involve a new deployment, git tag, docker image tag, and it doesn't affect traces
  • Yes

4. Breaking change label

Does this PR have the breaking-change label?

  • This PR is not a breaking change
  • Yes

Summary by CodeRabbit

  • New Features
    • Introduced support for configuring blocks per second in system contract settings.
    • Enhanced block production timing with nanosecond precision for higher block rates.
  • Bug Fixes
    • Improved timestamp calculation logic for scenarios with multiple blocks per second.
  • Tests
    • Added tests to verify correct block interval and deadline calculations.
  • Chores
    • Updated dependencies for the export-headers toolkit.
    • Increased the patch version number.

Copy link

coderabbitai bot commented Apr 27, 2025

Walkthrough

This change introduces new helper functions and logic for calculating block timestamps and deadlines based on configurable blocks-per-second rates in the consensus system contract. It updates the configuration structure, modifies block production timing, adds a new deadline calculation helper, and introduces a dedicated test to validate block interval computations. Go module dependencies are also updated.

Changes

Cohort / File(s) Change Summary
Consensus Timestamp Calculation
consensus/system_contract/consensus.go
Adds CalcBlocksPerSecond and CalcPeriodMs helpers; refactors CalcTimestamp to handle Period == 1 with per-block index logic, maintaining overrides for relaxed periods.
Miner Deadline Logic & Test
miner/scroll_worker.go, miner/scroll_worker_test.go
Adds CalculateBlockDeadline for nanosecond-precision deadline calculation when Period == 1; updates deadline logic in newWork; adds TestBlockIntervalWithWorkerDeadline to verify deadline intervals across configurations.
System Contract Config Struct
params/config.go
Adds BlocksPerSecond field to SystemContractConfig struct with JSON tag.
Version Bump
params/version.go
Increments VersionPatch constant from 76 to 78.
Export Headers Toolkit Dependencies
rollup/missing_header_fields/export-headers-toolkit/go.mod
Updates da-codec and go-ethereum dependencies to newer commits.

Sequence Diagram(s)

sequenceDiagram
    participant Worker
    participant SystemContract
    participant Config

    Worker->>Config: Get Period, BlocksPerSecond
    alt Period == 1
        Worker->>SystemContract: CalcBlocksPerSecond()
        Worker->>SystemContract: CalcPeriodMs()
        Worker->>SystemContract: CalcTimestamp(parent)
        Worker->>Worker: CalculateBlockDeadline(config, header)
    else Period != 1
        Worker->>SystemContract: CalcTimestamp(parent)
        Worker->>Worker: Default deadline logic
    end
    Worker->>Worker: Log deadline and transaction status
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Possibly related PRs

  • scroll-tech/go-ethereum#1130: Adds a CalcTimestamp method and timestamp override parameter in the SystemContract consensus engine, directly modifying the same method and timestamp logic as this PR.

Suggested reviewers

  • georgehao
  • colinlyguo
  • zimpha

Poem

A hop, a skip, a block in time,
Now measured out in seconds fine.
With helpers new and tests in tow,
The rabbit watches deadlines grow.
Patch seventy-eight, onward we hop—
Blocks per second, never stop!
🐇⏱️

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 004f070 and eb9f213.

📒 Files selected for processing (1)
  • params/config.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • params/config.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: test
  • GitHub Check: check
  • GitHub Check: Analyze (go)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/support_ms_block_generation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5e956fe and 2f9555e.

📒 Files selected for processing (4)
  • consensus/system_contract/consensus.go (1 hunks)
  • consensus/system_contract/system_contract.go (1 hunks)
  • miner/scroll_worker.go (1 hunks)
  • params/config.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
consensus/system_contract/system_contract.go (1)
common/types.go (1)
  • HexToAddress (218-218)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: Analyze (go)
🔇 Additional comments (3)
params/config.go (1)

800-800: Documentation update aligns with implementation changes

The comment has been updated to clarify that the Period is measured in milliseconds rather than seconds, which aligns with the implementation changes in other files that now treat this value as milliseconds.

miner/scroll_worker.go (1)

561-578: Improved deadline calculation with millisecond precision

The mining deadline calculation has been refined to use millisecond-level granularity, correctly interpreting the system contract period as milliseconds. This provides more precise scheduling of blocks within each second.

The implementation includes proper handling of edge cases like zero blocksPerSecond and calculates timestamps with nanosecond precision, aligning with the changes in the consensus timestamp calculation.

consensus/system_contract/consensus.go (1)

229-256: Enhanced timestamp calculation with millisecond precision

The timestamp calculation has been improved to support millisecond-level block periods, calculating how many blocks fit within each second and appropriately incrementing the timestamp for the last block in each second interval.

This change allows for more flexible and precise block timing, while still maintaining compatibility with the Unix timestamp format (which uses seconds). The implementation also properly handles edge cases like when blocksPerSecond would be zero.

@georgehao georgehao changed the title SystemContract: support ms block generation SystemContract: support millisecond block generation Apr 27, 2025
@georgehao georgehao requested review from roylou and colinlyguo April 27, 2025 08:44
Copy link

@Thegaram Thegaram left a comment

Choose a reason for hiding this comment

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

Let's not merge this until we decide about the block time.

Copy link

@jonastheis jonastheis left a comment

Choose a reason for hiding this comment

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

Let's get this merged soon so we can test and deploy it

jonastheis
jonastheis previously approved these changes Jul 23, 2025
jonastheis
jonastheis previously approved these changes Jul 31, 2025
Copy link

@jonastheis jonastheis left a comment

Choose a reason for hiding this comment

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

We just need to be careful with this as it changes the meaning of the config parameter

colinlyguo
colinlyguo previously approved these changes Jul 31, 2025
Thegaram
Thegaram previously approved these changes Aug 4, 2025
// CalcBlocksPerSecond converts period to milliseconds and calculate blocks per second
// For example: if Period = 250ms = 0.25s, then periodMs = 250
func CalcBlocksPerSecond(periodMs uint64) uint64 {
blocksPerSecond := 1000 / periodMs // integer division, e.g. 1000/250 = 4
Copy link

Choose a reason for hiding this comment

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

periodMs can be 0. It is for devnet mode that we don't use, but still safer to handle that case here.

Comment on lines 231 to 242
timestamp := parent.Time + s.config.Period
// Get the base timestamp (in seconds)
timestamp := parent.Time
Copy link

Choose a reason for hiding this comment

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

This change essentially means that we won't be able to support >1s block times, is that intentional?

Choose a reason for hiding this comment

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

yeah I was thinking the same. maybe we should introduce an additional (optional) parameter that allows us to specify the amount of blocks if the period is 1. This way we can still support block times bigger than one and there's less risk when deploying the new version

Copy link

Choose a reason for hiding this comment

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

Sure. But I guess we also do not need to support longer block times, for now.

Comment on lines 571 to 572
// Add one period to determine the deadline
nextBlockNano := baseTimeNano + fractionNano + int64(periodMs)*int64(time.Millisecond)
Copy link

Choose a reason for hiding this comment

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

What would happen if we didn't add one more period here?

Choose a reason for hiding this comment

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

I guess the deadline would be at the current time and thus the block would timeout immediately

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, true

@georgehao georgehao dismissed stale reviews from Thegaram, colinlyguo, and jonastheis via 7c997fb August 5, 2025 03:23
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
consensus/system_contract/consensus.go (1)

240-245: Consider edge cases with non-divisible blocks per second values.

The implementation looks correct for typical values. However, be aware that for blocks per second values that don't divide evenly into 1000 (e.g., 3, 6, 7), integer division will truncate the result, potentially causing slight timing drift.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3abad55 and 7c997fb.

📒 Files selected for processing (5)
  • consensus/system_contract/consensus.go (1 hunks)
  • consensus/system_contract/consensus_test.go (1 hunks)
  • miner/scroll_worker.go (2 hunks)
  • miner/scroll_worker_test.go (1 hunks)
  • params/config.go (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • params/config.go
  • miner/scroll_worker_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • miner/scroll_worker.go
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: the `override` parameter in `dasyncer.synconeblock()` is intentionally designed to allow rewriting e...
Learnt from: jonastheis
PR: scroll-tech/go-ethereum#1115
File: rollup/da_syncer/da_syncer.go:35-41
Timestamp: 2025-02-14T04:10:06.754Z
Learning: The `override` parameter in `DASyncer.SyncOneBlock()` is intentionally designed to allow rewriting existing chain data during recovery mode, enabling reconstruction of a valid canonical L2 chain from permissionless committed batches.

Applied to files:

  • consensus/system_contract/consensus.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: build-and-push
  • GitHub Check: check
  • GitHub Check: test
  • GitHub Check: Analyze (go)
🔇 Additional comments (7)
consensus/system_contract/consensus.go (2)

230-238: LGTM - Good default handling for blocks per second.

The function correctly handles the zero case by defaulting to 1 block per second, which addresses the devnet mode concern mentioned in previous reviews.


247-280: Complex but correct timestamp calculation logic.

The refactored logic correctly implements period-based timestamp increments using modular arithmetic. The approach properly handles:

  • Sub-second block intervals through blocks per second configuration
  • Period boundaries for timestamp increments
  • Genesis block edge case (nextBlockNumber > 0 check)
  • Existing RelaxedPeriod override behavior

While significantly more complex than the previous implementation, this enables the millisecond block generation feature as intended.

consensus/system_contract/consensus_test.go (5)

12-105: Excellent comprehensive test coverage for timestamp calculation.

The table-driven tests thoroughly validate the new timestamp calculation logic across various scenarios:

  • Different blocks per second configurations (1, 2)
  • Different period lengths (1s, 2s)
  • Period boundary detection and timestamp increment behavior
  • Clear test descriptions and detailed error messages

The use of future timestamps to avoid timing conflicts with the current time is a good practice.


107-165: Good validation of millisecond interval calculations.

The tests properly validate the CalcPeriodMs helper function with various blocks per second configurations. The mathematical verification that blocksPerSecond * interval = 1 second adds confidence in the timing calculations.


167-205: Excellent simulation of progressive timestamp behavior.

This test effectively validates the cumulative timestamp behavior over multiple blocks, ensuring that the modular arithmetic approach works correctly across period boundaries. The simulation of blockchain progression by updating baseTime is realistic and thorough.


207-238: Good validation of default configuration handling.

The test properly validates that zero/unset configuration values default to sensible values (period=1s, blocksPerSecond=1), ensuring backward compatibility and robust behavior when configuration is incomplete.


240-282: Precise validation of timestamp increment logic.

This test effectively validates the core increment logic by simulating blockchain progression and verifying that timestamps increment only at period boundaries (even-numbered blocks for 2 blocks per second). The modulo arithmetic in the test matches the implementation logic.

blocksPerSecond := CalcBlocksPerSecond(s.config.BlocksPerSecond)

// Calculate blocks per period
blocksPerPeriod := blocksPerSecond * period

Choose a reason for hiding this comment

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

I think for simplicity we should do something like this in this function:

if period == 1 {
blocksPerSecond := CalcBlocksPerSecond(s.config.BlocksPerSecond)
... 
} else {
timestamp := parent.Time + s.config.Period
}

Copy link
Member Author

Choose a reason for hiding this comment

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

fix in 00f37de

}

blocksPerSecond := system_contract.CalcBlocksPerSecond(config.BlocksPerSecond)
periodMs := system_contract.CalcPeriodMs(config.BlocksPerSecond)

Choose a reason for hiding this comment

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

Suggested change
periodMs := system_contract.CalcPeriodMs(config.BlocksPerSecond)
periodMs := system_contract.CalcPeriodMs(blocksPerSecond)

Copy link
Member Author

Choose a reason for hiding this comment

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

fix in 00f37de

periodMs := system_contract.CalcPeriodMs(config.BlocksPerSecond)

// Calculate blocks per period
blocksPerPeriod := blocksPerSecond * period

Choose a reason for hiding this comment

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

also here I would simplify this by wrapping it into a big if:

if period == 1 {
// do blockIndex stuff
} else {
deadline = time.Unix(int64(header.Time+w.chainConfig.SystemContract.Period), 0)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

fix in 00f37de

Copy link

@Thegaram Thegaram left a comment

Choose a reason for hiding this comment

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

Let's merge this only after #1230 (and the subsequent version bump) has been merged.

@@ -558,10 +558,13 @@ func (w *worker) newWork(now time.Time, parent *types.Block, reorging bool, reor
// 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 {
if w.chainConfig.SystemContract != nil && w.chainConfig.SystemContract.RelaxedPeriod && w.chainConfig.SystemContract.Period != 1 {

Choose a reason for hiding this comment

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

Why do we need to add && w.chainConfig.SystemContract.Period != 1 here?

Copy link
Member Author

Choose a reason for hiding this comment

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

nice catch

Copy link
Member Author

Choose a reason for hiding this comment

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

update 71dd65e

Comment on lines +1199 to +1205
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)

return time.Unix(0, nextBlockNano)

Choose a reason for hiding this comment

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

Suggested change
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)
return time.Unix(0, nextBlockNano)
fractionNano := int64(blockIndex) * int64(periodMs) * int64(time.Millisecond)
// Add one period to determine the deadline
nextBlockNano := fractionNano + int64(periodMs)*int64(time.Millisecond)
return time.Unix(int64(header.Time), nextBlockNano)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see anything optimization of the new format. How about keeping the original one (because this format have been tested) @jonastheis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants