Skip to content

Revive the previous Filecoin.EthSubscribe PR#5749

Merged
elmattic merged 46 commits intomainfrom
elmattic/eth-subscribe
Aug 1, 2025
Merged

Revive the previous Filecoin.EthSubscribe PR#5749
elmattic merged 46 commits intomainfrom
elmattic/eth-subscribe

Conversation

@elmattic
Copy link
Contributor

@elmattic elmattic commented Jun 17, 2025

Summary of changes

Changes introduced in this pull request:

Note:

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added support for Ethereum-compatible pubsub API methods, allowing clients to subscribe and unsubscribe to Ethereum event types such as new block headers ("heads") and logs.
    • Introduced random hexadecimal subscription IDs for improved compatibility with Ethereum clients.
    • Enhanced filtering options for log subscriptions, enabling more precise event tracking.
    • Implemented real-time event streaming for new block headers and filtered logs with automatic subscription management and error handling.
  • Documentation

    • Updated changelog to reflect the addition of Ethereum pubsub subscription support.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 15, 2025

Walkthrough

Support for Ethereum-compatible pubsub subscriptions was added, including new RPC methods Filecoin.EthSubscribe and Filecoin.EthUnsubscribe. The implementation introduces subscription handling for chain head updates and filtered Ethereum logs, new types for subscription IDs and filters, enhanced error logging for subscription sinks, and updates to RPC method registration and server configuration to integrate the subscription system.

Changes

File(s) Change Summary
CHANGELOG.md Added changelog entry documenting Filecoin.EthSubscribe and Filecoin.EthUnsubscribe API methods for Ethereum event subscriptions.
src/rpc/methods/chain.rs Added new_heads and logs asynchronous subscription functions for chain head and log events; introduced HEAD_CHANNEL_CAPACITY constant to unify channel capacity; imported relevant types.
src/rpc/methods/eth.rs Added public pubsub and crate-private pubsub_trait modules; made eth_logs_for_block_and_transaction public and refactored to use new eth_logs_with_filter; added EthSubscribe and EthUnsubscribe RPC methods with subscription flags and placeholder handlers.
src/rpc/methods/eth/pubsub.rs New file implementing Ethereum pubsub protocol: handling subscription requests for newHeads and logs, rejecting pendingTransactions; managing subscription lifecycle and forwarding events as JSON-RPC notifications with improved error handling and cancellation support.
src/rpc/methods/eth/pubsub_trait.rs Added EthPubSubApi trait defining async subscribe method with support for NewHeads, PendingTransactions, and Logs subscription kinds and optional filters.
src/rpc/methods/eth/types.rs Added SubscriptionID wrapper type, ApiHeaders struct, RandomHexStringIdProvider for generating subscription IDs, and conversion from LogFilter to EthFilterSpec.
src/rpc/mod.rs Extended for_each_rpc_method macro to include subscription methods; modified RPC module registration to exclude subscription methods; registered EthPubSub subscription API separately; configured server to use RandomHexStringIdProvider for IDs.
src/rpc/reflect/mod.rs Added SUBSCRIPTION constant (default false) to RpcMethod trait to identify subscription methods.
src/rpc/channel.rs Enhanced error handling when sending messages to subscription sinks by logging send failures explicitly.
src/tool/subcommands/api_cmd/test_snapshots_ignored.txt Added Filecoin.EthSubscribe and Filecoin.EthUnsubscribe to recognized API commands list.
Cargo.toml Added "macros" feature to jsonrpsee crate dependency features.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RPC_Server
    participant PubSub_Handler
    participant ChainStore

    Client->>RPC_Server: eth_subscribe("newHeads" or "logs", [filter])
    RPC_Server->>PubSub_Handler: Parse subscription, accept
    PubSub_Handler->>ChainStore: Subscribe to head/log events
    ChainStore-->>PubSub_Handler: Broadcast events (new head/logs)
    loop On event
        PubSub_Handler->>RPC_Server: Send JSON-RPC notification
        RPC_Server->>Client: Notify (new head/logs)
    end
    Client->>RPC_Server: eth_unsubscribe
    RPC_Server->>PubSub_Handler: Terminate subscription
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Poem

In the forest of chains, new whispers arise,
With heads and logs, we now synchronize.
Subscriptions bloom—events flow like streams,
Rabbits hop through Ethereum dreams.
Hex IDs twinkle, notifications abound,
In the warren of code, new features are found!
🐇✨

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 6a7de37 and e3811e8.

📒 Files selected for processing (1)
  • src/rpc/methods/chain.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/rpc/methods/chain.rs
⏰ 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). (10)
  • GitHub Check: cargo-publish-dry-run
  • GitHub Check: Build Ubuntu
  • GitHub Check: Build MacOS
  • GitHub Check: Build forest binaries on Linux AMD64
  • GitHub Check: tests
  • GitHub Check: tests-release
  • GitHub Check: All lint checks
  • GitHub Check: Check
  • GitHub Check: Analyze (rust)
  • 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 elmattic/eth-subscribe

🪧 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.
    • @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 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.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

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

@elmattic elmattic marked this pull request as ready for review July 16, 2025 09:22
@elmattic elmattic requested a review from a team as a code owner July 16, 2025 09:22
@elmattic elmattic requested review from LesnyRumcajs and akaladarshi and removed request for a team July 16, 2025 09:22
Ok(v) => {
match jsonrpsee::SubscriptionMessage::new("eth_subscription", sink.subscription_id(), &v) {
Ok(msg) => {
// This fails only if the connection is closed
Copy link
Member

Choose a reason for hiding this comment

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

perhaps instead of a comment we can have a log message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

LesnyRumcajs
LesnyRumcajs previously approved these changes Jul 30, 2025
Copy link
Member

@LesnyRumcajs LesnyRumcajs left a comment

Choose a reason for hiding this comment

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

LGTM, though I'd love if there could be some unit/component tests for this feature.

@elmattic
Copy link
Contributor Author

elmattic commented Jul 30, 2025

LGTM, though I'd love if there could be some unit/component tests for this feature.

Yes, I'd like them to be part of the api run subcommand. I've created some issue here: #5795

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

♻️ Duplicate comments (1)
src/rpc/methods/eth/pubsub.rs (1)

140-142: Consider adding error handling for task spawn failures.

Same concern as the new heads handler - consider adding error handling for task spawn failures.

🧹 Nitpick comments (2)
src/rpc/methods/eth/pubsub.rs (2)

126-129: Consider adding error handling for task spawn failures.

While task spawning rarely fails, adding error handling would make the code more robust for edge cases like resource exhaustion.

-        tokio::spawn(async move {
-            handle_subscription(subscriber, accepted_sink, handle).await;
-        });
+        let task_handle = tokio::spawn(async move {
+            handle_subscription(subscriber, accepted_sink, handle).await;
+        });
+        if task_handle.is_finished() {
+            tracing::warn!("Failed to spawn new heads subscription task");
+        }

174-175: Consider logging lagged message events.

While ignoring lagged messages is often acceptable for real-time subscriptions, logging these events could provide valuable monitoring insights about system load and client performance.

                    Err(RecvError::Lagged(_)) => {
+                        tracing::debug!("Subscription lagged, skipping messages (id: {:?})", sink.subscription_id());
                    }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a25d9a7 and 0e68b0f.

📒 Files selected for processing (2)
  • src/rpc/channel.rs (1 hunks)
  • src/rpc/methods/eth/pubsub.rs (1 hunks)
⏰ 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). (10)
  • GitHub Check: tests-release
  • GitHub Check: tests
  • GitHub Check: Build MacOS
  • GitHub Check: cargo-publish-dry-run
  • GitHub Check: Build Ubuntu
  • GitHub Check: Build forest binaries on Linux AMD64
  • GitHub Check: Check
  • GitHub Check: All lint checks
  • GitHub Check: Analyze (go)
  • GitHub Check: Analyze (rust)
🔇 Additional comments (4)
src/rpc/channel.rs (1)

370-372: LGTM! Enhanced error logging for subscription failures.

The change from a simple error check to explicit error logging provides better observability for subscription failures while maintaining the same control flow. This aligns well with the similar error handling pattern already used elsewhere in the function.

src/rpc/methods/eth/pubsub.rs (3)

1-71: Well-documented API with clear sequence diagram.

The comprehensive documentation with sequence diagrams effectively explains the Ethereum pubsub protocol flow, making it easy for future maintainers to understand the implementation.


87-114: Proper error handling and subscription dispatch logic.

The subscription method correctly handles the different subscription types:

  • Uses ? operator for proper error propagation from pending.accept()
  • Properly rejects unimplemented pendingTransactions with a clear error message
  • Clean dispatch to specific handlers for implemented types

This addresses the previous review concerns about unwrap() usage and pendingTransactions handling.


146-186: Robust subscription lifecycle management.

The generic subscription handler demonstrates good practices:

  • Proper use of tokio::select! for concurrent event handling
  • Graceful handling of different error conditions (closed channel, lagged messages)
  • Task cleanup by aborting the producer handle when subscription ends
  • Informative logging for subscription lifecycle events

The error handling covers all relevant scenarios and ensures resources are properly cleaned up.

akaladarshi
akaladarshi previously approved these changes Jul 31, 2025
Copy link
Collaborator

@akaladarshi akaladarshi left a comment

Choose a reason for hiding this comment

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

Just a single comment. LGTM

@elmattic elmattic requested a review from LesnyRumcajs July 31, 2025 12:45
@elmattic elmattic added this pull request to the merge queue Aug 1, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 1, 2025
@elmattic elmattic added this pull request to the merge queue Aug 1, 2025
Merged via the queue into main with commit 9d35101 Aug 1, 2025
52 checks passed
@elmattic elmattic deleted the elmattic/eth-subscribe branch August 1, 2025 19:35
@coderabbitai coderabbitai bot mentioned this pull request Jan 28, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RPC requires calibnet RPC checks to run on CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants