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

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

  • Bug Fixes
    • Improved timestamp and mining deadline calculations for greater precision, now supporting millisecond-level intervals between blocks.
  • Documentation
    • Updated configuration comments to clarify that block periods are measured in milliseconds instead of seconds.
  • Chores
    • Temporarily set a fixed signer address for system contract operations, bypassing dynamic retrieval.

Copy link

coderabbitai bot commented Apr 27, 2025

Walkthrough

This update refines the handling of block timing and system contract address retrieval in the consensus and mining components. The calculation of block timestamps and mining deadlines now uses millisecond-level granularity, allowing for more precise scheduling based on the configured period. The logic for fetching the L1 signer address in the system contract is temporarily bypassed, with a hardcoded address returned instead of querying L1 storage. Additionally, documentation for the period configuration is updated to specify milliseconds as the unit. The patch version is incremented to reflect these changes.

Changes

File(s) Change Summary
consensus/system_contract/consensus.go Refined CalcTimestamp logic for millisecond-based block periods and granular timestamp calculation.
consensus/system_contract/system_contract.go Modified fetchAddressFromL1 to return a hardcoded address, skipping L1 storage queries.
miner/scroll_worker.go Updated mining deadline calculation to use millisecond and nanosecond precision.
params/config.go Changed comment for Period field to specify milliseconds instead of seconds.
params/version.go Incremented patch version from 40 to 41.

Sequence Diagram(s)

sequenceDiagram
    participant Miner
    participant ConsensusEngine
    participant SystemContract

    Miner->>ConsensusEngine: Request next block timestamp
    ConsensusEngine->>SystemContract: Get block period (ms)
    ConsensusEngine->>ConsensusEngine: Calculate blocks per second
    ConsensusEngine->>ConsensusEngine: Determine block index in current second
    ConsensusEngine->>ConsensusEngine: Compute precise timestamp
    ConsensusEngine->>Miner: Return timestamp

    Miner->>Miner: Calculate mining deadline with nanosecond precision
Loading

Possibly related PRs

  • scroll-tech/go-ethereum#1130: Also modifies timestamp calculation in consensus/system_contract/consensus.go, focusing on new methods and parameter changes for block preparation.

Suggested reviewers

  • omerfirmak
  • zimpha

Poem

In the warren of code, where time ticks so small,
Milliseconds march, and deadlines stand tall.
A hare hops ahead, with a timestamp precise,
No more coarse seconds, this timing is nice!
With addresses fixed and comments anew,
The blockchain hops forward—swift, bright, and true.
🐇⏱️


📜 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 bad6a6c and b8d0863.

📒 Files selected for processing (1)
  • miner/scroll_worker.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • miner/scroll_worker.go
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build-and-push
  • GitHub Check: test
  • GitHub Check: Analyze (go)
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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 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
Comment on lines +251 to +253
nowTimestamp := uint64(time.Now().Unix())
if s.config.RelaxedPeriod || baseTimestamp < nowTimestamp {
baseTimestamp = nowTimestamp
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.

Comment on lines +562 to +565
blocksPerSecond := uint64(1000) / periodMs
if blocksPerSecond == 0 {
blocksPerSecond = 1
}
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.

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.

}

// 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

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

Successfully merging this pull request may close these issues.

3 participants