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): model reader without call to world #3052

Merged
merged 4 commits into from
Feb 21, 2025

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Feb 19, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a streamlined option for accessing model data with simplified parameter handling.
    • Added a method for setting the block in the model reader.
  • Refactor
    • Consolidated legacy initialization methods to create a more consistent and maintainable model retrieval process.
    • Transitioned to using ModelRPCReader for model data access across various processors, enhancing the overall workflow.
  • Bug Fixes
    • Corrected a minor typographical error in model configuration to ensure reliable behavior.

Copy link

coderabbitai bot commented Feb 19, 2025

Ohayo, sensei! Here’s the detailed overview of the pull request:

Walkthrough

This pull request refactors the initialization process for the ModelRPCReader struct. A new method new has been implemented to centralize instance creation using a contract address and class hash derived from the world resource. Existing methods are updated—new_with_block is renamed to new_from_world and calls the new new method; additionally, set_block is introduced. In the WorldContractReader, instantiation now consistently uses new_from_world, and the block-specific model reader method has been removed. The RegisterModelProcessor, RegisterEventProcessor, UpgradeEventProcessor, and UpgradeModelProcessor have been updated to utilize the new ModelRPCReader.

Changes

File(s) Change Summary
crates/dojo/.../model.rs Added new and set_block methods in ModelRPCReader; renamed new_with_block to new_from_world; updated new_from_world to use world.block_id directly.
crates/dojo/.../world.rs Updated model_reader_with_tag and model_reader methods to use ModelRPCReader::new_from_world; removed model_reader_with_block.
crates/torii/.../register_model.rs, crates/torii/.../register_event.rs, crates/torii/.../upgrade_event.rs, crates/torii/.../upgrade_model.rs Replaced previous model retrieval logic with instantiation of ModelRPCReader using new parameters; adjusted block setting logic based on strict_model_reader configuration.

Possibly related PRs

  • feat: batch entities #2633: The changes in the main PR are related to the modifications in the ModelRPCReader struct and its methods, which are directly utilized in the WorldContractReader methods updated in the retrieved PR.
  • refactor(torii-core): upgrade model silent if not in namespace #2688: The changes in the main PR are related to the modifications in the ModelRPCReader instantiation, which is directly referenced in the retrieved PR's updates to the UpgradeEventProcessor and other processors that now utilize ModelRPCReader::new_from_world.
  • feat(torii-indexer): add option for strict model reader block #2954: The changes in the main PR, which involve the introduction and modification of methods in the ModelRPCReader struct, are directly related to the changes in the retrieved PR, as both involve updates to how ModelRPCReader instances are created and utilized in the context of model reading, specifically with respect to the strict_model_reader configuration.

Suggested reviewers

  • glihm

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5d0317d and 4c70c5e.

📒 Files selected for processing (5)
  • crates/dojo/world/src/contracts/model.rs (3 hunks)
  • crates/torii/indexer/src/processors/register_event.rs (2 hunks)
  • crates/torii/indexer/src/processors/register_model.rs (2 hunks)
  • crates/torii/indexer/src/processors/upgrade_event.rs (2 hunks)
  • crates/torii/indexer/src/processors/upgrade_model.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • crates/torii/indexer/src/processors/upgrade_model.rs
  • crates/torii/indexer/src/processors/register_model.rs
  • crates/torii/indexer/src/processors/register_event.rs
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: docs
  • GitHub Check: build
  • GitHub Check: ensure-wasm
🔇 Additional comments (5)
crates/dojo/world/src/contracts/model.rs (3)

85-104: Ohayo! Nice encapsulation of instance creation logic, sensei!

The new new method provides a clean way to create ModelRPCReader instances directly with all necessary parameters.


106-129: Excellent refactoring, sensei!

The method now has:

  • Better naming that reflects its purpose
  • Consistent block ID handling using world's state
  • Clear error handling for invalid addresses

158-160: Clean and focused implementation, sensei!

The set_block method provides a clear way to update the block context for model reading.

crates/torii/indexer/src/processors/upgrade_event.rs (2)

6-6: Clean import addition, sensei!

The import of ModelRPCReader aligns with the refactored implementation.


91-96: Excellent use of the new ModelRPCReader API, sensei!

The initialization is now:

  • More direct using the new new method
  • Cleaner with conditional block setting
  • More maintainable with centralized instance creation

🪧 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e9f7e18 and 39769c2.

📒 Files selected for processing (3)
  • crates/dojo/world/src/contracts/model.rs (2 hunks)
  • crates/dojo/world/src/contracts/world.rs (1 hunks)
  • crates/torii/indexer/src/processors/register_model.rs (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: ci
crates/dojo/world/src/contracts/world.rs

[error] 38-39: missing parameters for function definition. Help: add a parameter list.


[error] 39-39: expected one of ->, <, where, or {, found }.

crates/torii/indexer/src/processors/register_model.rs

[error] 80-80: expected ;, found world. Help: add ; here.

🔇 Additional comments (5)
crates/dojo/world/src/contracts/world.rs (2)

27-27: Ohayo! LGTM on the model_reader_with_tag refactor!

The change to use new_from_world aligns well with the centralized instance creation pattern.


35-35: Ohayo! LGTM on the model_reader refactor!

The change to use new_from_world maintains consistency with the new pattern.

crates/dojo/world/src/contracts/model.rs (3)

85-104: Ohayo! The new ModelRPCReader::new implementation looks great!

Clean implementation with proper parameter handling and error management.


106-112: Ohayo! The new_from_world convenience method is well designed!

Good use of delegation to maintain DRY principles.


114-138: Ohayo! The new_from_world_with_block implementation is solid!

Excellent error handling and validation, with clean delegation to the new centralized constructor.

Comment on lines 38 to 39
pub async fn model_reader_with
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ohayo sensei! There's a syntax error in the incomplete function definition.

The function model_reader_with is incomplete and will cause compilation errors. This appears to be an unintended fragment that should be removed.

Apply this diff to fix the issue:

-    pub async fn model_reader_with
-}
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub async fn model_reader_with
}
}
🧰 Tools
🪛 GitHub Actions: ci

[error] 38-39: missing parameters for function definition. Help: add a parameter list.


[error] 39-39: expected one of ->, <, where, or {, found }.

@@ -77,7 +77,7 @@ where
return Ok(());
}

let model = if config.strict_model_reader {
let model = if config.strict_model_reader {s
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ohayo sensei! Found a typo in the conditional statement.

There's an extra 's' character after the opening brace that's causing a syntax error.

Apply this diff to fix the issue:

-        let model = if config.strict_model_reader {s
+        let model = if config.strict_model_reader {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let model = if config.strict_model_reader {s
let model = if config.strict_model_reader {
🧰 Tools
🪛 GitHub Actions: ci

[error] 80-80: expected ;, found world. Help: add ; here.

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

🧹 Nitpick comments (4)
crates/torii/indexer/src/processors/register_model.rs (1)

80-83: Ohayo sensei! Please address the pipeline's formatting concern.

The pipeline flagged a readability warning for this line. Splitting it will improve maintainability and adhere to the style guidelines.

-        let mut model = ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world).await?;
+        let mut model = ModelRPCReader::new(
+            &namespace,
+            &name,
+            event.address.0,
+            event.class_hash.0,
+            world
+        ).await?;
crates/torii/indexer/src/processors/upgrade_event.rs (1)

91-94: Ohayo sensei! Consider splitting the instantiation for readability.

Similar to the pipeline’s recommendation, a multiline approach for ModelRPCReader::new can improve legibility.

-        let mut model = ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world).await?;
+        let mut model = ModelRPCReader::new(
+            &namespace,
+            &name,
+            event.address.0,
+            event.class_hash.0,
+            world
+        ).await?;
crates/torii/indexer/src/processors/register_event.rs (1)

82-85: Ohayo sensei! Split the new call for better readability.

Addressing the pipeline’s formatting warning will keep things consistent and easier on the eyes.

-        let mut model = ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world).await?;
+        let mut model = ModelRPCReader::new(
+            &namespace,
+            &name,
+            event.address.0,
+            event.class_hash.0,
+            world
+        ).await?;
crates/torii/indexer/src/processors/upgrade_model.rs (1)

86-86: Let's improve the code formatting, sensei!

The pipeline detected a formatting issue. Consider splitting the long line for better readability.

-        let mut model = ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world).await?;
+        let mut model = ModelRPCReader::new(
+            &namespace,
+            &name,
+            event.address.0,
+            event.class_hash.0,
+            world,
+        )
+        .await?;
🧰 Tools
🪛 GitHub Actions: ci

[warning] 86-86: Formatting issue detected: Line split for 'let mut model = ModelRPCReader::new(...)' for better readability.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 39769c2 and 84524ee.

📒 Files selected for processing (6)
  • crates/dojo/world/src/contracts/model.rs (3 hunks)
  • crates/dojo/world/src/contracts/world.rs (1 hunks)
  • crates/torii/indexer/src/processors/register_event.rs (2 hunks)
  • crates/torii/indexer/src/processors/register_model.rs (2 hunks)
  • crates/torii/indexer/src/processors/upgrade_event.rs (2 hunks)
  • crates/torii/indexer/src/processors/upgrade_model.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/dojo/world/src/contracts/world.rs
🧰 Additional context used
🪛 GitHub Actions: ci
crates/torii/indexer/src/processors/register_model.rs

[warning] 77-77: Formatting issue detected: Line split for 'let mut model = ModelRPCReader::new(...)' for better readability.

crates/torii/indexer/src/processors/register_event.rs

[warning] 79-79: Formatting issue detected: Line split for 'let mut model = ModelRPCReader::new(...)' for better readability.

crates/torii/indexer/src/processors/upgrade_event.rs

[warning] 88-88: Formatting issue detected: Line split for 'let mut model = ModelRPCReader::new(...)' for better readability.

crates/torii/indexer/src/processors/upgrade_model.rs

[warning] 86-86: Formatting issue detected: Line split for 'let mut model = ModelRPCReader::new(...)' for better readability.

🔇 Additional comments (8)
crates/torii/indexer/src/processors/register_model.rs (1)

6-6: Ohayo sensei! Import looks good.

No issues here. The addition of ModelRPCReader seems consistent with the overall refactoring approach.

crates/torii/indexer/src/processors/upgrade_event.rs (1)

6-6: Ohayo sensei! Great import usage.

Bringing in ModelRPCReader aligns with the new approach in other files. No issues here.

crates/torii/indexer/src/processors/register_event.rs (1)

6-6: Ohayo sensei! The new import is consistent with the refactor.

No concerns here; it matches the revised model-reading strategy.

crates/torii/indexer/src/processors/upgrade_model.rs (2)

6-6: Ohayo! Nice import organization, sensei!

The addition of ModelRPCReader alongside ModelReader shows good foresight in maintaining both interfaces during the refactor.


89-92: Clean refactor using ModelRPCReader::new, sensei!

The direct instantiation of ModelRPCReader with event address and class hash simplifies the code and removes the dependency on world for these values.

crates/dojo/world/src/contracts/model.rs (3)

85-104: Ohayo! Nice constructor implementation, sensei!

The new new method provides a clean way to directly instantiate ModelRPCReader with address and class hash, reducing coupling with the world contract.


106-129: Clean refactor of world-based initialization, sensei!

The renamed new_from_world method now:

  • Uses a more descriptive name
  • Reuses the new constructor
  • Properly handles the zero address case

158-160: Nice extraction of block setting functionality, sensei!

The new set_block method provides a clean way to update the block ID after initialization, improving the API's flexibility.

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

🧹 Nitpick comments (2)
crates/torii/indexer/src/processors/register_event.rs (2)

82-84: Ohayo sensei! Consider improving error handling for ModelRPCReader initialization.

The .await? propagates errors directly. Consider adding context to these errors to make debugging easier.

-        let mut model =
-            ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world)
-                .await?;
+        let mut model = ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world)
+            .await
+            .map_err(|e| anyhow::anyhow!("Failed to initialize ModelRPCReader: {}", e))?;

85-87: Add error handling for block setting.

The set_block call should handle potential errors and provide feedback.

         if config.strict_model_reader {
-            model.set_block(BlockId::Number(block_number)).await;
+            model.set_block(BlockId::Number(block_number))
+                .await
+                .map_err(|e| anyhow::anyhow!("Failed to set block for model reader: {}", e))?;
         }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 84524ee and 5d0317d.

📒 Files selected for processing (5)
  • crates/dojo/world/src/contracts/world.rs (1 hunks)
  • crates/torii/indexer/src/processors/register_event.rs (2 hunks)
  • crates/torii/indexer/src/processors/register_model.rs (2 hunks)
  • crates/torii/indexer/src/processors/upgrade_event.rs (2 hunks)
  • crates/torii/indexer/src/processors/upgrade_model.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/torii/indexer/src/processors/register_model.rs
  • crates/dojo/world/src/contracts/world.rs
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: docs
  • GitHub Check: ensure-wasm
  • GitHub Check: clippy
🔇 Additional comments (3)
crates/torii/indexer/src/processors/upgrade_model.rs (2)

89-91: Similar improvements needed for ModelRPCReader initialization.

The initialization pattern here matches register_event.rs and would benefit from the same error handling improvements.


92-94: Similar improvements needed for block setting.

The block setting pattern here matches register_event.rs and would benefit from the same error handling improvements.

crates/torii/indexer/src/processors/upgrade_event.rs (1)

91-96: Ohayo! Consider addressing the model vs event distinction.

The comment on line 70-71 mentions "Called model here by language, but it's an event. Torii rework will make clear distinction." This seems like a good opportunity to start making this distinction clearer.

Would you like me to help create an issue to track the model/event distinction refactoring? This could help improve code clarity and reduce confusion.

Also, the ModelRPCReader initialization and block setting patterns here match the other processors and would benefit from the same error handling improvements.

Copy link

codecov bot commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 48.93617% with 24 lines in your changes missing coverage. Please review.

Project coverage is 57.56%. Comparing base (e9f7e18) to head (4c70c5e).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
crates/dojo/world/src/contracts/model.rs 71.42% 6 Missing ⚠️
...ates/torii/indexer/src/processors/upgrade_event.rs 0.00% 6 Missing ⚠️
...ates/torii/indexer/src/processors/upgrade_model.rs 0.00% 6 Missing ⚠️
crates/dojo/world/src/contracts/world.rs 0.00% 2 Missing ⚠️
...tes/torii/indexer/src/processors/register_event.rs 66.66% 2 Missing ⚠️
...tes/torii/indexer/src/processors/register_model.rs 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3052      +/-   ##
==========================================
+ Coverage   56.88%   57.56%   +0.68%     
==========================================
  Files         439      439              
  Lines       59762    59445     -317     
==========================================
+ Hits        33995    34222     +227     
+ Misses      25767    25223     -544     

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

@glihm glihm merged commit 7facdb7 into dojoengine:main Feb 21, 2025
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