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

Reuse block preparation logic in streaming API #617

Merged
merged 2 commits into from
Oct 21, 2024

Conversation

peterargue
Copy link
Contributor

@peterargue peterargue commented Oct 18, 2024

Closes: #???

Related to: #524

Description

When using the go-ethereum client to stream block headers, I go this error:
error: hex string has length 0, want 512 for Bloom

This was because the code that produces the blocks that are returned does not include the bloom filter, and the stock go-ethereum client requires that the server returns a non-empy value.

The polling endpoint does include it as well as a few other fields. This PR updates the streaming endpoint to reuse the logic from the polling endpoint to generate the block responses.


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

Summary by CodeRabbit

  • New Features

    • Enhanced StreamAPI functionality to better handle block notifications and responses through integration with the BlockChainAPI.
    • Improved error handling for block response preparation, providing more descriptive error messages.
  • Bug Fixes

    • Updated notification system for subscribers to reflect changes in block response handling.

Copy link
Contributor

coderabbitai bot commented Oct 18, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes in this pull request involve modifications to the StreamAPI struct and its methods in api/stream.go, including the addition of a new field for BlockChainAPI. The NewHeads method is updated to utilize this new field for processing block notifications. The NewStreamAPI constructor is also updated to include this new parameter, enhancing the integration of the StreamAPI with blockchain services.

Changes

File Path Change Summary
api/stream.go - Added field api *BlockChainAPI to StreamAPI struct.
- Updated NewStreamAPI method signature to include api *BlockChainAPI.
- Modified NewHeads method to call s.api.prepareBlockResponse(block, false) and handle errors accordingly.

Possibly related PRs

  • Use buffered channel for subscription errors #618: This PR modifies the api/stream.go file, which is directly related to the changes made in the main PR that also involves the StreamAPI struct and its methods in the same file. The changes in both PRs focus on enhancing the functionality and error handling of the subscription mechanisms.

Suggested labels

EVM

Suggested reviewers

  • peterargue
  • zhangchiqing
  • ramtinms
  • janezpodhostnik

🐇 In the code, I hop and play,
With blocks and chains, I pave the way.
A new API, so bright and clear,
For streams of data, we now cheer!
Let notifications ring, let errors be few,
In the world of code, I'm hopping for you! 🐰✨


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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5872cfb and 71d07b2.

📒 Files selected for processing (2)
  • api/stream.go (4 hunks)
  • bootstrap/bootstrap.go (1 hunks)
🧰 Additional context used
🔇 Additional comments (3)
bootstrap/bootstrap.go (1)

295-295: LGTM: Approve the addition of blockchainAPI to NewStreamAPI.

The addition of blockchainAPI as a parameter to NewStreamAPI is consistent with the PR objectives. This change allows the streaming API to reuse block preparation logic from the polling endpoint, which should address the issue with empty Bloom filters in block responses.

To ensure consistency, let's verify the NewStreamAPI function signature:

api/stream.go (2)

20-20: Integration of BlockChainAPI into StreamAPI is appropriate

The addition of the api field to StreamAPI, along with its initialization in NewStreamAPI, allows StreamAPI to utilize BlockChainAPI for processing block responses. This enhances modularity and reusability by leveraging existing logic from BlockChainAPI.

Also applies to: 33-33, 44-44


62-67: Usage of prepareBlockResponse in NewHeads improves block handling

The NewHeads method now utilizes s.api.prepareBlockResponse to generate block responses, ensuring consistency between the streaming and polling endpoints. Error handling is appropriately implemented by returning a formatted error if the response preparation fails.

Comment on lines +62 to +67
response, err := s.api.prepareBlockResponse(block, false)
if err != nil {
return err
return fmt.Errorf("failed to get block response: %w", err)
}

return notifier.Notify(sub.ID, &Block{
Hash: h,
Number: hexutil.Uint64(block.Height),
ParentHash: block.ParentBlockHash,
ReceiptsRoot: block.ReceiptRoot,
Transactions: block.TransactionHashes,
Uncles: []common.Hash{},
GasLimit: hexutil.Uint64(blockGasLimit),
Nonce: gethTypes.BlockNonce{0x1},
Timestamp: hexutil.Uint64(block.Timestamp),
BaseFeePerGas: hexutil.Big(*big.NewInt(0)),
})
return notifier.Notify(sub.ID, response)
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 18, 2024

Choose a reason for hiding this comment

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

💡 Codebase verification

Bloom filter is not included in prepareBlockResponse

The prepareBlockResponse method does not include the Bloom field in the returned Block struct, which is contrary to the PR objective.

  • File: api/stream.go
  • Lines: 62-67
🔗 Analysis chain

Verify that the Bloom filter is included in block responses as intended

To fulfill the PR objective of including the Bloom filter in the streaming API's block responses, ensure that the prepareBlockResponse method correctly incorporates the Bloom filter.

Run the following script to confirm that the Bloom field is included in the block responses:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the Bloom filter is included in the block responses from the streaming API.

# Test: Search for the inclusion of the Bloom field in the block response preparation. Expect: The Bloom field should be part of the Block struct in the prepareBlockResponse method.

ast-grep --lang go --pattern $'func ($_ *BlockChainAPI) prepareBlockResponse($_, $_) {
    $$$
    return Block{
        $$$
        Bloom: $_,
        $$$
    }
}' --json

Length of output: 175

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its called LogsBloom

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterargue, thank you for the correction. I see that the Bloom filter is included as LogsBloom in the prepareBlockResponse method.


✏️ Learnings added
Learnt from: peterargue
PR: onflow/flow-evm-gateway#617
File: api/stream.go:62-67
Timestamp: 2024-10-18T19:26:37.250Z
Learning: In the `flow-evm-gateway` project, within the Go file `api/stream.go`, the `prepareBlockResponse` method includes the Bloom filter as the field `LogsBloom` in the returned `Block` struct.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Collaborator

@m-Peter m-Peter left a comment

Choose a reason for hiding this comment

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

LGTM 👏

@m-Peter m-Peter merged commit d21f299 into main Oct 21, 2024
2 checks passed
@m-Peter m-Peter deleted the petera/align-streaming-headers branch October 21, 2024 13:34
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.

3 participants