Skip to content
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

Introduce dedicated BlockHeader type for usage in newHeads subscription endpoint #620

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Oct 21, 2024

Closes: #524

Description

This will allow us to return only the necessary fields for eth_subscribe("newHeads") subscription endpoint.


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new BlockHeader type for structured representation of Ethereum block header data.
    • Enhanced StreamAPI to prepare block headers more effectively and independently of the blockchain API.
    • Improved API server integration with streamlined setup for the StreamAPI.
  • Bug Fixes

    • Updated error handling in StreamAPI for clearer messaging related to block header responses.
  • Tests

    • Enhanced blockchain event subscription tests to validate block header properties against blockchain data.

Copy link
Contributor

coderabbitai bot commented Oct 21, 2024

Walkthrough

The pull request introduces a new BlockHeader struct in the api/models.go file, which encapsulates various fields related to Ethereum block headers. It modifies the StreamAPI struct in api/stream.go, removing an unnecessary dependency and adding a method to prepare block headers. Changes in bootstrap/bootstrap.go integrate the new StreamAPI, while tests/web3js/eth_streaming_test.js updates the subscription tests to validate block headers. Overall, these changes enhance the data model and improve the clarity and functionality of the streaming API.

Changes

File Change Summary
api/models.go Added new BlockHeader struct with fields for block header data (e.g., Number, Hash, ParentHash, etc.).
api/stream.go Removed api field from StreamAPI, updated NewStreamAPI constructor, added prepareBlockHeader method for creating BlockHeader from a block, and modified NewHeads method to use the new header preparation logic.
bootstrap/bootstrap.go Updated StartAPIServer method to remove blockchainAPI from streamAPI initialization, streamlining API server setup.
tests/web3js/eth_streaming_test.js Renamed blockTxHashes to blocksHeaders, updated subscription logic to capture entire block objects, and added validation for block header properties against blockchain data.

Assessment against linked issues

Objective Addressed Explanation
Payload for the web3.eth.subscribe endpoints should match the JSON-RPC specification (#524)

Possibly related PRs

Suggested labels

Improvement, Bugfix

Suggested reviewers

  • peterargue
  • zhangchiqing
  • ramtinms

Poem

🐇 In the blockchain's dance, we now embrace,
A BlockHeader struct, a structured space.
With fields so bright, our data's in sight,
StreamAPI flows, making everything right.
Tests now robust, with headers in hand,
A leap for our code, as we take a stand! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cf91dc8 and 5e57a61.

📒 Files selected for processing (4)
  • api/models.go (1 hunks)
  • api/stream.go (3 hunks)
  • bootstrap/bootstrap.go (0 hunks)
  • tests/web3js/eth_streaming_test.js (4 hunks)
💤 Files with no reviewable changes (1)
  • bootstrap/bootstrap.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • api/models.go
  • api/stream.go
  • tests/web3js/eth_streaming_test.js

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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
Contributor

@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: 5

🧹 Outside diff range and nitpick comments (3)
tests/web3js/eth_streaming_test.js (2)

35-37: LGTM: Enhanced data capture and consistent variable usage.

The changes improve the test by capturing the entire block object instead of just the transaction hash. This provides more comprehensive data for validation, aligning with the PR's objective.

Consider adding a comment explaining the significance of testValues.length in the condition, e.g.:

// Unsubscribe after receiving the expected number of blocks
if (blocksHeaders.length === testValues.length) {

94-113: LGTM: Comprehensive block header validation added.

The new assertions significantly enhance the test's robustness by validating all relevant properties of the block headers against the actual blockchain data. This thorough validation ensures that the newHeads subscription endpoint returns accurate and complete data, aligning perfectly with the PR's objectives.

Consider adding error messages to the assertions to provide more context in case of failures. For example:

assert.equal(blockHeader.number, block.number, `Block number mismatch for block ${blockHeader.number}`);

This will make debugging easier if an assertion fails in the future.

api/stream.go (1)

161-161: Evaluate the performance impact of computing LogsBloom

The comment suggests considering moving the computation of LogsBloom into storage for performance reasons. If computing LogsBloom becomes a bottleneck:

  • Analyze the performance impact of the current implementation.
  • Consider caching or precomputing LogsBloom during block processing.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 815998c and 19653f8.

📒 Files selected for processing (4)
  • api/models.go (1 hunks)
  • api/stream.go (3 hunks)
  • bootstrap/bootstrap.go (0 hunks)
  • tests/web3js/eth_streaming_test.js (4 hunks)
💤 Files with no reviewable changes (1)
  • bootstrap/bootstrap.go
🧰 Additional context used
🔇 Additional comments (5)
tests/web3js/eth_streaming_test.js (4)

28-29: LGTM: Improved variable naming and comment clarity.

The changes in these lines enhance the readability of the code by accurately reflecting the focus on block headers rather than transaction hashes. This aligns well with the PR's objective of implementing a dedicated BlockHeader type.


42-42: Minor grammatical improvement noted.

The change from "submission" to "submissions" in the comment is a small but welcome improvement in accuracy.


90-93: LGTM: Consistent updates reflecting focus on block headers.

The changes in these lines maintain consistency with the earlier modifications, removing the assertion for blockTxHashes and updating the comment to focus on transaction hashes. This aligns well with the PR's objective of streamlining the response to include only necessary fields.


Line range hint 1-113: Overall assessment: Excellent enhancements to the test suite.

The changes in this file significantly improve the test coverage for the new BlockHeader type and the newHeads subscription endpoint. The modifications align perfectly with the PR objectives, ensuring that the returned data structure matches the expected format and contains accurate information.

Key improvements:

  1. Focus shifted from transaction hashes to complete block headers.
  2. Comprehensive validation of all relevant block header properties.
  3. Consistent updates throughout the test to reflect the new focus on block headers.

These changes will help ensure the reliability and correctness of the eth_subscribe("newHeads") endpoint implementation.

api/models.go (1)

294-310: LGTM: New BlockHeader struct aligns with PR objectives

The newly added BlockHeader struct is well-structured and includes all the essential fields for an Ethereum block header. It aligns perfectly with the PR objective of introducing a dedicated type for the newHeads subscription endpoint. The use of appropriate types and consistent JSON tags ensures compatibility with Ethereum RPC responses.

api/stream.go Show resolved Hide resolved
api/stream.go Show resolved Hide resolved
api/stream.go Show resolved Hide resolved
api/stream.go Show resolved Hide resolved
api/stream.go Show resolved Hide resolved
@m-Peter m-Peter force-pushed the mpeter/fix-streaming-new-block-headers-response branch from cf91dc8 to 5648ef4 Compare October 21, 2024 16:49
@m-Peter m-Peter force-pushed the mpeter/fix-streaming-new-block-headers-response branch from 5648ef4 to 5e57a61 Compare October 23, 2024 12:10
@m-Peter m-Peter merged commit 6f99e01 into main Oct 23, 2024
2 checks passed
@m-Peter m-Peter deleted the mpeter/fix-streaming-new-block-headers-response branch October 23, 2024 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Payload for the web3.eth.subscribe endpoints should match the JSON-RPC specification
2 participants