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

refactor(torii-grpc): event subscription with multiple clauses #2555

Merged
merged 2 commits into from
Oct 22, 2024

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Oct 18, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced subscription capabilities to allow handling of multiple entity keys simultaneously.
    • Introduced new functions to improve logic for checking subscriber interest in entities.
  • Bug Fixes

    • Simplified logic for processing entity updates and event matching, improving performance and readability.

Copy link

coderabbitai bot commented Oct 18, 2024

Walkthrough

Ohayo, sensei! This pull request introduces changes to the SubscribeEventsRequest message, allowing it to handle multiple entity keys by modifying the keys field to a repeated type. Corresponding updates are made in the subscribe_events method of the DojoWorld struct, which now accepts a vector of entity keys. Additionally, new functions match_entity_keys and match_keys are added to streamline the logic for processing subscriptions and events, enhancing the overall subscription handling capabilities.

Changes

File Path Change Summary
crates/torii/grpc/proto/world.proto Updated SubscribeEventsRequest to use repeated types.EntityKeysClause keys.
crates/torii/grpc/src/server/mod.rs Changed subscribe_events method to accept Vec<proto::types::EntityKeysClause>.
crates/torii/grpc/src/server/subscriptions/entity.rs Introduced match_entity_keys function; removed PatternMatching import.
crates/torii/grpc/src/server/subscriptions/event.rs Updated EventSubscriber to use Vec<EntityKeysClause>; modified add_subscriber method.
crates/torii/grpc/src/server/subscriptions/event_message.rs Added match_entity_keys function; adjusted import for EntityKeysClause.
crates/torii/grpc/src/server/subscriptions/mod.rs Added pub(crate) fn match_entity_keys and pub(crate) fn match_keys functions.
crates/torii/client/src/client/mod.rs Updated Client struct to use Vec<EntityKeysClause> in on_starknet_event method.

Possibly related PRs

  • refactor-opt(torii-grpc): subscriptions no db fetch #2455: The changes in this PR involve refactoring the subscription handling to eliminate database fetching, which aligns with the main PR's updates to subscription message handling by allowing multiple entity keys.
  • refactor: subscription updates ordered #2507: This PR focuses on enhancing the subscription updates process, which is directly related to the main PR's modifications to the SubscribeEventsRequest and the handling of multiple entity keys in subscriptions.

Suggested reviewers

  • glihm

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

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

🧹 Outside diff range and nitpick comments (2)
crates/torii/grpc/proto/world.proto (1)

111-111: Ohayo, sensei! This change looks great!

The modification to SubscribeEventsRequest to use repeated types.EntityKeysClause instead of a single types.KeysClause is a solid improvement. It allows for more flexible and efficient event subscriptions by supporting multiple entity keys in a single request.

This change aligns well with the PR objectives and is consistent with other parts of the file, such as the SubscribeEntitiesRequest message.

Consider updating the service documentation for the SubscribeEvents RPC to reflect this change in capability. For example:

// Subscribe to events for multiple entity keys
rpc SubscribeEvents (SubscribeEventsRequest) returns (stream SubscribeEventsResponse);

This will help users of the service understand the new functionality at a glance.

crates/torii/grpc/src/server/subscriptions/mod.rs (1)

74-74: Correct typos in comments: 'athis' should be 'at this'

There's a typo in the comments on lines 74 and 120: 'athis index' should be 'at this index'.

- // athis index
+ // at this index

Also applies to: 120-120

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3508fb9 and ba0510a.

📒 Files selected for processing (6)
  • crates/torii/grpc/proto/world.proto (1 hunks)
  • crates/torii/grpc/src/server/mod.rs (1 hunks)
  • crates/torii/grpc/src/server/subscriptions/entity.rs (2 hunks)
  • crates/torii/grpc/src/server/subscriptions/event.rs (3 hunks)
  • crates/torii/grpc/src/server/subscriptions/event_message.rs (2 hunks)
  • crates/torii/grpc/src/server/subscriptions/mod.rs (1 hunks)
🧰 Additional context used
🔇 Additional comments (12)
crates/torii/grpc/src/server/mod.rs (1)

837-841: LGTM! The changes enhance event subscription capabilities.

Ohayo, sensei! The modifications to the subscribe_events method look good. The new implementation now supports multiple entity keys, which should provide more flexibility in event subscriptions.

To ensure consistency across the codebase, let's verify the usage of this updated method:

✅ Verification successful

Ohayo, sensei! It looks like the previous shell scripts had some hiccups. Let's try running corrected scripts to ensure everything is in order:


Ohayo, sensei! 🎉 The verification is complete, and there are no remaining usages of the old subscribe_events method signature with KeysClause. All instances have been successfully updated to use Vec<proto::types::EntityKeysClause>.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining usage of the old method signature and verify the new usage.

# Test 1: Check for any remaining usage of the old method signature
echo "Checking for old method signature usage:"
rg --type rust 'subscribe_events.*KeysClause'

# Test 2: Verify the new method usage
echo "Verifying new method usage:"
rg --type rust 'subscribe_events.*Vec<proto::types::EntityKeysClause>'

Length of output: 276


Script:

#!/bin/bash
# Description: Comprehensive search for any usages of `subscribe_events` to ensure no old method signatures remain.

echo "Searching for all occurrences of `subscribe_events` in the codebase:"
rg --type rust 'subscribe_events'

echo "Searching specifically for any `subscribe_events` calls with `KeysClause` parameters:"
rg --type rust 'subscribe_events\s*\(.*KeysClause.*\)'

Length of output: 1154


Script:

#!/bin/bash
# Description: Comprehensive search for any usages of `subscribe_events` to ensure no old method signatures remain.

echo 'Searching for all occurrences of subscribe_events in the codebase:'
rg --type rust 'subscribe_events'

echo 'Searching specifically for any subscribe_events calls with KeysClause parameters:'
rg --type rust 'subscribe_events\s*\(.*KeysClause.*\)'

Length of output: 938

crates/torii/grpc/src/server/subscriptions/event.rs (5)

22-22: Ohayo sensei! Importing match_keys function

The addition of the match_keys import ensures that the function is available for use in event processing. This is essential for the new key matching logic.


25-25: Ohayo sensei! Importing EntityKeysClause

Adding EntityKeysClause from crate::types enables handling multiple entity key clauses in subscriptions. This expands the flexibility of event subscriptions.


32-32: Ohayo sensei! Good job updating keys to Vec<EntityKeysClause>

By changing keys to a vector of EntityKeysClause, you allow subscribers to handle multiple key clauses, enhancing the flexibility of the event subscription system.


45-45: Ohayo sensei! Great update to add_subscriber method signature

Modifying add_subscriber to accept a Vec<EntityKeysClause> aligns with the updated EventSubscriber structure and allows subscribers to subscribe to multiple key clauses.


112-112: Ohayo sensei! Nice refactoring with match_keys function

Using match_keys to encapsulate key matching logic simplifies process_event and improves code readability. This abstraction enhances maintainability.

crates/torii/grpc/src/server/subscriptions/event_message.rs (3)

21-21: Correctly imported match_entity_keys function

Ohayo, sensei! The import statement properly includes the match_entity_keys function, facilitating its use in this module.


24-24: Updated import for EntityKeysClause

Ohayo, sensei! The import statement for EntityKeysClause is correctly adjusted, reflecting changes in the codebase.


124-124: Simplified conditional logic with match_entity_keys function

Ohayo, sensei! Utilizing match_entity_keys to handle the matching logic streamlines the code, enhancing clarity and maintainability.

crates/torii/grpc/src/server/subscriptions/entity.rs (3)

22-22: Ohayo, sensei! The import of match_entity_keys enhances modularity.

Adding use super::match_entity_keys; correctly imports the function, promoting code reusability and cleaner code structure.


25-25: Ohayo, sensei! The addition of EntityKeysClause import is appropriate.

Importing EntityKeysClause ensures proper handling of subscription clauses based on entity keys.


132-132: Ohayo, sensei! Excellent refactoring using match_entity_keys.

Replacing the complex conditional logic with the match_entity_keys function simplifies the code and enhances readability and maintainability.

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.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ba0510a and 1d5064c.

📒 Files selected for processing (3)
  • crates/torii/client/src/client/mod.rs (2 hunks)
  • crates/torii/grpc/src/client.rs (2 hunks)
  • crates/torii/grpc/src/server/mod.rs (2 hunks)
🧰 Additional context used
🔇 Additional comments (5)
crates/torii/grpc/src/client.rs (1)

207-209: Ohayo, sensei! Great improvement to the subscribe_events method!

The change from Option<KeysClause> to Vec<EntityKeysClause> enhances the flexibility of event subscriptions. This modification allows for multiple entity key clauses, which is a significant improvement in functionality.

The implementation correctly handles the new input type, mapping it to the expected format for the gRPC request. The rest of the method remains consistent with the existing error handling and stream processing logic, which is excellent.

crates/torii/client/src/client/mod.rs (2)

16-16: Ohayo, sensei! Updated import includes EntityKeysClause.

Including EntityKeysClause in the import statement aligns with its usage in the updated method signatures.


162-162: Update in on_starknet_event method signature requires attention.

The method now accepts keys: Vec<EntityKeysClause> instead of keys: Option<KeysClause>. Please ensure all calls to on_starknet_event are updated accordingly to prevent any mismatches.

To verify all usages are updated, you can run:

✅ Verification successful

Ohayo, sensei! To ensure all calls to on_starknet_event are correctly updated, please run the following script:


Ohayo, sensei! All calls to on_starknet_event have been correctly updated. No mismatches found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all calls to `on_starknet_event` and check their arguments.

# Test: Search for the method usage. Expect: Calls passing `Vec<EntityKeysClause>`.

rg --type rust -A 2 'on_starknet_event\('

Length of output: 245


Script:

#!/bin/bash
# Description: Verify all calls to `on_starknet_event` pass `Vec<EntityKeysClause>`.

# Search for all call sites and display the line with the call and the line(s) with arguments.
rg --type rust 'on_starknet_event\(' -A 2

Length of output: 245

crates/torii/grpc/src/server/mod.rs (2)

1193-1193: Confirm correct handling in subscribe_events request handler

The request handler correctly extracts the keys from the SubscribeEventsRequest and passes them to the updated subscribe_events method.


837-841: Verify that all usages of subscribe_events are updated

Ohayo, sensei! Since the signature of subscribe_events has changed, please ensure that all calls to this method across the codebase have been updated to match the new signature accepting a vector of EntityKeysClause.

Run the following script to locate all usages of subscribe_events:

✅ Verification successful

Further verification required for subscribe_events usage

Ohayo, sensei! The initial search only identified the definitions of subscribe_events. To ensure that all calls to this method across the codebase have been updated to use the new signature with Vec<proto::types::EntityKeysClause>, please run the following script:


Verification successful! All usages of subscribe_events have been updated appropriately.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all calls to `subscribe_events` in the Rust codebase.

# Expected: All calls should use the updated signature with `Vec<proto::types::EntityKeysClause>`.

rg --type rust --word-regexp 'subscribe_events\(' -A 3

Length of output: 976


Script:

#!/bin/bash
# Description: Find all calls to `subscribe_events` in the Rust codebase, excluding function definitions.

rg --type rust --word-regexp 'subscribe_events\(' -A 3 | grep -v 'fn subscribe_events'

Length of output: 807

crates/torii/grpc/src/server/mod.rs Show resolved Hide resolved
Copy link

codecov bot commented Oct 18, 2024

Codecov Report

Attention: Patch coverage is 0% with 105 lines in your changes missing coverage. Please review.

Project coverage is 69.59%. Comparing base (3508fb9) to head (1d5064c).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/grpc/src/server/subscriptions/mod.rs 0.00% 93 Missing ⚠️
crates/torii/grpc/src/server/mod.rs 0.00% 5 Missing ⚠️
crates/torii/grpc/src/client.rs 0.00% 2 Missing ⚠️
...rates/torii/grpc/src/server/subscriptions/event.rs 0.00% 2 Missing ⚠️
crates/torii/client/src/client/mod.rs 0.00% 1 Missing ⚠️
...ates/torii/grpc/src/server/subscriptions/entity.rs 0.00% 1 Missing ⚠️
...rii/grpc/src/server/subscriptions/event_message.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2555   +/-   ##
=======================================
  Coverage   69.59%   69.59%           
=======================================
  Files         388      389    +1     
  Lines       50018    50015    -3     
=======================================
  Hits        34809    34809           
+ Misses      15209    15206    -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Larkooo Larkooo merged commit 33fd66c into dojoengine:main Oct 22, 2024
14 of 15 checks passed
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.

2 participants