Skip to content

Fix getEvents with forking Devnet#787

Merged
FabijanC merged 10 commits intomainfrom
fix-forked-events
May 21, 2025
Merged

Fix getEvents with forking Devnet#787
FabijanC merged 10 commits intomainfrom
fix-forked-events

Conversation

@FabijanC
Copy link
Copy Markdown
Contributor

@FabijanC FabijanC commented May 20, 2025

Usage related changes

Development related changes

  • Refactor get_events
  • Replace usize with u64 to avoid try_from conversion
  • Replace String with Url to avoid try_from conversion
  • In a few places expect is used:
    • This is justified with explanation strings.

Checklist:

  • Checked out the contribution guidelines
  • Applied formatting - ./scripts/format.sh
  • No linter errors - ./scripts/clippy_check.sh
  • No unused dependencies - ./scripts/check_unused_deps.sh
  • No spelling errors - ./scripts/check_spelling.sh
  • Performed code self-review
  • Rebased to the latest commit of the target branch (or merged it into my branch)
    • Once you make the PR reviewable, please avoid force-pushing
  • Updated the docs if needed - ./website/README.md
  • Linked the issues resolvable by this PR - linking info
  • Updated the tests if needed; all passing - execution info

Summary by CodeRabbit

  • New Features
    • Event queries now support seamless retrieval across both forked origin and local chains, with improved handling of block ranges and continuation tokens.
  • Bug Fixes
    • Improved handling of continuation tokens and error forwarding during event queries.
  • Refactor
    • Pagination parameters for event queries updated to use 64-bit integers for greater consistency and scalability.
  • Tests
    • Added comprehensive integration tests to verify event retrieval behavior in forked environments, including pagination and multi-source queries.
  • Chores
    • Updated dependencies and internal structures to support enhanced event querying and forking capabilities.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented May 20, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce support for querying events across both the forked origin chain and the local devnet in the starknet_getEvents RPC method. The event pagination parameters (skip and limit) are updated from usize to u64 throughout the core and server code. The get_events endpoint is refactored to split the block range between origin and local blocks, query each source as appropriate, and manage continuation tokens across both. Additional dependencies are added for interacting with the origin chain. New integration tests are included to verify correct event retrieval and pagination in various forked scenarios.

Assessment against linked issues

Objective Addressed Explanation
Support querying events via starknet_getEvents on forked Devnet when to_block <= forking_block (#785)
Correct handling of pagination and continuation tokens for events across fork and local blocks (#785)
Proper error forwarding for invalid continuation tokens in forked mode (#785)
Integration tests to verify event retrieval on forked devnet (#785)

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

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.

@FabijanC
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented May 20, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@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.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 180dc4d and d8e9b80.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (10)
  • crates/starknet-devnet-core/src/starknet/events.rs (1 hunks)
  • crates/starknet-devnet-core/src/starknet/mod.rs (1 hunks)
  • crates/starknet-devnet-server/Cargo.toml (1 hunks)
  • crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs (3 hunks)
  • crates/starknet-devnet-server/src/api/json_rpc/error.rs (1 hunks)
  • crates/starknet-devnet-server/src/api/json_rpc/mod.rs (1 hunks)
  • crates/starknet-devnet-server/src/api/json_rpc/origin_forwarder.rs (4 hunks)
  • crates/starknet-devnet-types/src/rpc/emitted_event.rs (1 hunks)
  • crates/starknet-devnet-types/src/rpc/transactions.rs (3 hunks)
  • tests/integration/test_get_events.rs (2 hunks)
🔇 Additional comments (26)
crates/starknet-devnet-server/src/api/json_rpc/mod.rs (1)

234-234: Code improvement: Direct URL object usage

The change from converting the URL to a string to directly using the URL object is a good improvement. This avoids unnecessary string conversion and aligns with the updated OriginForwarder::new constructor which now accepts a url::Url type instead of a String.

crates/starknet-devnet-server/src/api/json_rpc/error.rs (1)

252-259: Enhanced error forwarding for pagination support

Moving the InvalidContinuationToken error variant to the forwardable errors list properly supports the enhanced event querying across both the forked origin chain and local devnet. This is crucial for pagination with continuation tokens to work correctly when querying events from dual sources.

crates/starknet-devnet-core/src/starknet/events.rs (1)

27-28: Type consistency improvement: usize to u64

Changing the parameter types from usize to u64 provides better type consistency across the codebase. This change:

  1. Ensures consistent behavior across different platforms and architectures
  2. Aligns with the refactored event querying that spans origin and local chains
  3. Provides fixed-width integer guarantees for pagination parameters

The change is appropriate and follows good API design practices.

crates/starknet-devnet-server/Cargo.toml (1)

38-42: Added required dependencies for forking functionality

The dependencies have been properly organized with the addition of essential crates for the forking functionality:

  • starknet-rs-providers: Provides the JSON-RPC client for interacting with the origin chain
  • url: Supports proper URL handling in the refactored OriginForwarder

These additions directly support the enhanced event querying feature that retrieves events from both forked origin and local chains.

crates/starknet-devnet-types/src/rpc/emitted_event.rs (1)

21-34: Implementation looks good.

Clean implementation of the From trait to convert from starknet_rs_core::types::EmittedEvent to the local EmittedEvent struct. The use of expect for the address conversion is appropriate since this operation should never fail if both types are properly designed.

crates/starknet-devnet-types/src/rpc/transactions.rs (2)

261-261: Type update from usize to u64 for better cross-platform consistency.

This change ensures a fixed-width integer type is used for pagination, which is preferable to the platform-dependent usize type. This aligns with the changes in other parts of the codebase.


272-279: Clean implementation of the conversion between event pagination types.

Good implementation of the From trait to convert EventsPage to EventsChunk, which will help with integrating events from both local and forked sources.

crates/starknet-devnet-core/src/starknet/mod.rs (1)

1170-1172: Type signature update from usize to u64 for pagination parameters.

This method signature change ensures consistent types are used for pagination parameters, aligning with the changes in the EventFilter struct and the events::get_events function it calls. This is a good practice for ensuring consistent behavior across different platforms.

crates/starknet-devnet-server/src/api/json_rpc/origin_forwarder.rs (3)

25-25: Well-designed addition of the Starknet client field.

The addition of a public starknet_client field allows for direct JSON-RPC communication with the origin chain, which is essential for the enhanced event retrieval functionality.


29-36: Constructor signature change improves type safety.

Changing the constructor to accept a url::Url instead of a string is a good improvement for type safety, although it's a breaking change for any code that directly instantiates this struct.


38-40: Useful accessor method.

Adding a getter for fork_block_number provides a clean API to access this important value.

crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs (8)

3-3: Added Provider trait import for interacting with origin chain.

This import is needed for the modified get_events functionality that now needs to query the origin Starknet provider.


27-27: New constant for prefixing origin continuation tokens.

Good naming convention for the prefix that helps distinguish continuation tokens coming from the origin chain versus the local devnet.


397-453: Well-structured helper method for determining block ranges across forked chains.

The split_block_range helper elegantly handles the complexity of splitting a requested block range between origin and local chains. The method properly handles different block ID types (number, hash, tag) and various range scenarios, with appropriate error handling.

Some observations:

  • Good early returns for edge cases
  • Detailed comments explaining the logic and range boundaries
  • Clear return type that provides all necessary information for the caller

455-456: Clippy allow directive is appropriate here.

The #[allow(clippy::expect_used)] annotation is justified as failure scenarios here would indicate implementation bugs rather than runtime errors.


458-460: Clean integration with the block range splitting logic.

The function properly uses the helper method to determine which blocks need to be queried from the origin chain versus the local chain.


461-505: Well-implemented origin chain event fetching logic.

This segment handles fetching events from the origin chain when applicable. The code:

  • Correctly determines when to use the origin chain based on range and continuation token
  • Properly strips/adds prefixes to continuation tokens to maintain origin context
  • Handles errors from the origin client appropriately
  • Ensures continuation token state is preserved for subsequent calls

506-535: Local event fetching logic with proper pagination.

This segment handles local event fetching when either there's no origin range or we've exhausted the origin events. The implementation:

  • Correctly parses the continuation token as a page number
  • Calculates the appropriate skip (offset) based on pages read
  • Properly handles error cases, particularly mapping NoBlock errors to BlockNotFound
  • Generates the next continuation token based on whether more events exist

537-537: Clean response wrapping.

The response is correctly wrapped in the StarknetResponse::Events variant to maintain API consistency.

tests/integration/test_get_events.rs (7)

5-7: Updated imports for enhanced event handling.

Added EmittedEvent to the imports from starknet_rs_core::types, which is necessary for the new helper function and test implementations.


12-12: Added constants for forking mainnet in tests.

Including MAINNET_URL and STRK_ERC20_CONTRACT_ADDRESS constants is essential for the new forking-related tests.


15-37: Well-implemented helper function for handling paginated event retrieval.

The get_events_follow_continuation_token function efficiently handles the complexity of paginated event retrieval by:

  • Following continuation tokens until all events are retrieved
  • Accumulating events from multiple pages
  • Properly handling the completion condition (absence of continuation token)
  • Maintaining the original filter parameters across calls

This helper significantly reduces code duplication in the test implementations and makes the test logic more readable.


205-205: Appropriate constant for fork block number.

Defining the fork block as a constant improves maintainability and ensures consistency across tests.


207-238: Comprehensive test for querying events from origin chain only.

This test validates that events can be correctly retrieved when the queried blocks are entirely on the origin chain. It:

  • Sets up a forked devnet at a specific block number
  • Verifies the fork was successful by checking the latest block number
  • Uses a chunk size that forces pagination to test continuation token handling
  • Queries events from a known contract with specific event keys
  • Verifies the correct number of events are retrieved from the origin chain

This test is essential for validating the origin-side event fetching logic.


240-280: Thorough test for querying events from local devnet only.

This test ensures events can be correctly retrieved when the queried blocks are entirely on the local devnet. It:

  • Sets up a forked devnet
  • Performs mint operations to generate events on the local chain
  • Queries events specifically from blocks after the fork point
  • Verifies the correct number of events are retrieved based on the minting operations
  • Includes a helpful comment explaining the expected event count

Good test coverage for the local-only event querying scenario.


282-324: Comprehensive test for cross-boundary event querying.

This test validates the most complex scenario where events are retrieved across both origin and local chains. It:

  • Sets up a forked devnet
  • Performs mint operations to generate events on the local chain
  • Queries events from a block range that spans both the origin and local chains
  • Verifies the total number of events matches the sum from both sources
  • Includes helpful comments explaining the expected event counts from each source

This test is crucial for validating the seamless pagination across chain boundaries, which is a key feature of the implementation.

@FabijanC FabijanC merged commit 76eeb8f into main May 21, 2025
2 checks passed
@FabijanC FabijanC deleted the fix-forked-events branch May 21, 2025 11:25
@coderabbitai coderabbitai bot mentioned this pull request Jul 3, 2025
11 tasks
@coderabbitai coderabbitai bot mentioned this pull request Oct 1, 2025
10 tasks
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.

starknet_getEvents not working on forked Devnet if to_block <= forking_block

1 participant