Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(torii-core): remove old check for StoreUpdateMember processor #2858

Merged
merged 10 commits into from
Jan 6, 2025

Conversation

glihm
Copy link
Collaborator

@glihm glihm commented Jan 2, 2025

Old checks on the StoreUpdateMember processor make the indexing fail.

  • adding tests for this use-case.
  • fix the Torii startup issue with update member non-existing table.
  • looking to fix an other index issues in the same context.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added ability to update player configuration name in the game.
    • Enhanced player configuration management with new methods.
  • Bug Fixes

    • Simplified event processing logic in core systems.
    • Improved error handling for event management.
  • Refactor

    • Updated dependency management in core package.
    • Streamlined SQL and processor modules.
    • Removed deprecated constants and methods.
  • Tests

    • Added new test for remote configuration updates.
    • Improved testing framework for player configurations.

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

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

Project coverage is 55.73%. Comparing base (38b3c2a) to head (4885ace).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/core/src/sql/test.rs 0.00% 105 Missing ⚠️
...s/torii/core/src/processors/store_update_member.rs 0.00% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2858      +/-   ##
==========================================
- Coverage   55.82%   55.73%   -0.09%     
==========================================
  Files         446      446              
  Lines       57725    57816      +91     
==========================================
- Hits        32227    32226       -1     
- Misses      25498    25590      +92     

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

@glihm
Copy link
Collaborator Author

glihm commented Jan 2, 2025

@Larkooo seems that we've an issue with the parallelization in some situations. I've updated the spawn-and-move example to have an update of member to add a test to this fix.

Here's the setup:

katana --dev
cargo run --bin sozo -r -- --manifest-path examples/spawn-and-move/Scarb.toml build
cargo run --bin sozo -r -- --manifest-path examples/spawn-and-move/Scarb.toml migrate
cargo run --bin sozo -r -- --manifest-path examples/spawn-and-move/Scarb.toml execute actions set_player_config -c str:"myname"
cargo run --bin sozo -r -- --manifest-path examples/spawn-and-move/Scarb.toml execute actions update_player_config_name -c str:"name2"

The error it yields:

2025-01-02T22:36:48.808720Z ERROR torii_core::engine: Processing parallelized event. event_name="StoreUpdateMember" error=error returned from database: (code: 1) no such table: ns-PlayerConfig task_id=9150862908850922780

The table is correctly created though when checking the DB manually. Could it be related to the batch of queries into the db's transaction?

Once Torii runs and displays error for the first update, the subsequent updates are ok since the table exists.

Also, if Torii is running when the transactions are sent, it works well. The issue is observed when Torii is started, and the events are processed in batch.

@Larkooo Larkooo marked this pull request as ready for review January 3, 2025 04:40
Copy link

coderabbitai bot commented Jan 3, 2025

Walkthrough

Ohayo, sensei! The changes involve updates across multiple files in the Torii core and spawn-and-move example project. The modifications primarily focus on simplifying event processing, removing some constants, and adding a new method to update player configuration names. The changes streamline code structure and introduce more direct ways of handling player-related operations.

Changes

File Change Summary
crates/torii/core/Cargo.toml Removed num-traits.workspace dependency
crates/torii/core/src/processors/mod.rs Removed MODEL_INDEX and ENTITY_ID_INDEX constants
crates/torii/core/src/processors/store_update_member.rs Simplified event processing logic, updated validate and process methods to use WorldEvent
crates/torii/core/src/sql/mod.rs Removed does_entity_exist method from Sql struct
crates/torii/core/src/sql/test.rs Added test_load_from_remote_update test function
examples/spawn-and-move/manifest_dev.json Updated contract address, class hash, and added update_player_config_name function
examples/spawn-and-move/src/actions.cairo Added update_player_config_name method to IActions trait and ActionsImpl

Sequence Diagram

sequenceDiagram
    participant Player
    participant Actions
    participant PlayerConfig
    
    Player->>Actions: update_player_config_name(newName)
    Actions->>PlayerConfig: Directly update name
    PlayerConfig-->>Actions: Name updated
    Actions-->>Player: Confirmation
Loading

The sequence diagram illustrates the new method for updating a player's configuration name, showing a direct update process without intermediate steps.


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

🧹 Nitpick comments (8)
examples/spawn-and-move/src/actions.cairo (1)

135-144: Ohayo sensei! Ensure data validation before writing to the model.
While directly overwriting name is straightforward, it might be helpful to:

  1. Validate the ByteArray (e.g., ensure it isn’t empty or overly long).
  2. Provide error handling or revert logic in case invalid data is provided.

Consider adding short, inlined checks to avoid unexpected states in your model.

crates/torii/core/src/processors/store_update_member.rs (2)

42-54: Consider more graceful handling instead of panicking

Ohayo sensei, this block currently panics if the event doesn't match the expected structure. While it might be unlikely in practice, panicking here could bring down the entire processor under unexpected circumstances. A more robust alternative might involve logging an error and returning early:

 let event = match WorldEvent::try_from(event).unwrap_or_else(|_| {
-    panic!(
-        "Expected {} event to be well formed.",
-        <StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self)
-    )
+    tracing::error!(
+        target: LOG_TARGET,
+        "Invalid event structure for: {}",
+        <StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self)
+    );
+    return;
 })
 {
     WorldEvent::StoreUpdateMember(e) => e,
     _ => {
         unreachable!()
     }
 };

97-99: Consider concurrency implications or an optional transaction

Ohayo sensei, while storing entities is straightforward, if you anticipate concurrent insertions or updates, wrapping this in a transaction or verifying concurrency handling may help prevent data races or partial writes.

crates/torii/core/src/processors/store_update_record.rs (1)

27-27: Add meaningful validation logic

Ohayo sensei, this method unconditionally returns true, making the validation step effectively moot. If you intend to verify the structure of event, consider adding checks here or removing the method entirely if it's no longer needed.

crates/torii/core/src/sql/test.rs (4)

212-215: Initialize the testing environment earlier to avoid race conditions.

Creating a new workspace and building config right before database initialization can occasionally race with asynchronous tasks. You might want to verify or log that the tasks have completed before proceeding.


249-256: Use meaningful data in the ByteArray to test real-world scenarios.

Empty ByteArrays might not reflect actual usage patterns. Try setting initial config fields to something more representative of real data to further validate the system's correctness and resilience.


263-275: Validate concurrency for large updates.

Updating the player's name works for a single short string “mimi,” but concurrency or bigger updates might expose issues. Consider concurrency tests or stress tests with random/wide updates to ensure system stability.


303-315: Ohayo sensei! Ensure table name references remain consistent.

Here, [ns-PlayerConfig] is queried based on a dynamic table name. If future refactors rename or remove the “ns” namespace, the query could become outdated. Adding a mechanism to fetch the model name from a config or schema registry will help maintain consistency.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3ccea4b and 3943c34.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • examples/spawn-and-move/Scarb.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • crates/torii/core/Cargo.toml (0 hunks)
  • crates/torii/core/src/processors/mod.rs (0 hunks)
  • crates/torii/core/src/processors/store_update_member.rs (3 hunks)
  • crates/torii/core/src/processors/store_update_record.rs (1 hunks)
  • crates/torii/core/src/sql/mod.rs (0 hunks)
  • crates/torii/core/src/sql/test.rs (2 hunks)
  • examples/spawn-and-move/manifest_dev.json (3 hunks)
  • examples/spawn-and-move/src/actions.cairo (3 hunks)
💤 Files with no reviewable changes (3)
  • crates/torii/core/Cargo.toml
  • crates/torii/core/src/processors/mod.rs
  • crates/torii/core/src/sql/mod.rs
🔇 Additional comments (11)
examples/spawn-and-move/src/actions.cairo (2)

8-8: Ohayo sensei! Great addition of the update_player_config_name function.
By exposing a direct method to update the player configuration name, you simplify usage for the caller. This addition looks consistent with the IActions trait’s responsibilities.


26-26: Ohayo sensei! Nice import for Model usage.
Importing the Model trait is crucial for direct member writes. This is a neat addition that helps keep your code more concise and organized.

examples/spawn-and-move/manifest_dev.json (3)

1255-1256: Ohayo sensei! Double-check contract redeployment references.
Updating the contract address and class hash typically requires re-deployment or upgrade steps. Ensure references in other config files and scripts remain valid post-update.


1402-1413: Ohayo sensei! The new manifest entry for update_player_config_name matches your Cairo code changes.
This addition to the interface allows external calls to update a player’s configuration name. Everything looks consistent and well-mapped.


1600-1600: Ohayo sensei! Good job adding the system method.
Listing update_player_config_name as a system function ensures the method is externally accessible as intended.

crates/torii/core/src/processors/store_update_member.rs (5)

4-4: Ohayo sensei, this import of WorldEvent from abigen looks clean! No compatibility issues detected.


9-9: Ohayo sensei, the addition of use tracing::info; is consistent with the rest of the tracing logs.


56-58: Ohayo sensei, extracting model_selector, entity_id, and member_selector in these lines is straightforward and consistent with the new event structure.


62-62: Ohayo sensei, silently ignoring missing models is aligned with an opt-in indexing strategy. This helps ignore events for untracked models without causing errors.


94-94: Ohayo sensei, copying values into a mutable vector is a typical approach for deserialization here. No issues noted.

crates/torii/core/src/sql/test.rs (1)

208-209: Ohayo sensei! Consider investigating the root cause of flakiness.

Marking the test as #[ignore] due to flakiness may conceal underlying concurrency or timing issues. If possible, gather logs and investigate whether the processor is fully caught up or if the table creation is delayed.

Do you want guidance on adding logs or extra synchronization steps to diagnose the cause of flakiness?

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
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3943c34 and 86b885e.

📒 Files selected for processing (1)
  • crates/torii/core/src/processors/store_update_member.rs (4 hunks)
🔇 Additional comments (4)
crates/torii/core/src/processors/store_update_member.rs (4)

4-4: Ohayo! Nice improvement using WorldEvent!

The switch to using the generated WorldEvent type is a good structural improvement that makes the code more type-safe and maintainable.


28-30: Ohayo! Validation simplification looks good, sensei!

The simplification of the validate method aligns with other processors since validation is now handled by auto-generated deserialization.


94-99: Clean and consistent member update implementation!

The member update logic is now more direct and uses model_selector consistently. Good improvement in code clarity!


Line range hint 62-70: Verify table existence handling in parallel scenarios.

The silent handling of missing models could be related to the table existence issues mentioned in the PR objectives, particularly during parallel processing.

Let's verify if this is related to the reported "no such table" errors:

Comment on lines +42 to +54
// Torii version is coupled to the world version, so we can expect the event to be well
// formed.
let event = match WorldEvent::try_from(event).unwrap_or_else(|_| {
panic!(
"Expected {} event to be well formed.",
<StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self)
)
}) {
WorldEvent::StoreUpdateMember(e) => e,
_ => {
unreachable!()
}
};
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider graceful error handling for parallel processing, sensei!

The panic on malformed events could be problematic in a parallel processing context. While the comment suggests that Torii version is coupled to the world version, this assumption might not hold during parallel processing or upgrades.

Consider this more resilient approach:

-        let event = match WorldEvent::try_from(event).unwrap_or_else(|_| {
-            panic!(
-                "Expected {} event to be well formed.",
-                <StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self)
-            )
-        }) {
+        let event = match WorldEvent::try_from(event) {
+            Ok(event) => match event {
+                WorldEvent::StoreUpdateMember(e) => e,
+                _ => return Err(Error::msg("Unexpected event type")),
+            },
+            Err(e) => return Err(Error::msg(format!(
+                "Malformed {} event: {}", 
+                <StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self),
+                e
+            ))),
+        };
📝 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
// Torii version is coupled to the world version, so we can expect the event to be well
// formed.
let event = match WorldEvent::try_from(event).unwrap_or_else(|_| {
panic!(
"Expected {} event to be well formed.",
<StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self)
)
}) {
WorldEvent::StoreUpdateMember(e) => e,
_ => {
unreachable!()
}
};
// Torii version is coupled to the world version, so we can expect the event to be well
// formed.
let event = match WorldEvent::try_from(event) {
Ok(event) => match event {
WorldEvent::StoreUpdateMember(e) => e,
_ => return Err(Error::msg("Unexpected event type")),
},
Err(e) => return Err(Error::msg(format!(
"Malformed {} event: {}",
<StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self),
e
))),
};

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 (1)
bin/sozo/tests/test_data/policies.json (1)

86-133: Consider documenting the purpose of each contract address, sensei! 📝

While the permissions are correctly configured, it would be helpful to add comments or documentation explaining the purpose of each contract address and its associated permissions. This would make future maintenance easier.

Consider adding a comment block at the top of the file like this:

+ /*
+  * Contract Addresses:
+  * 0x50b1... - StoreUpdateMember processor
+  * 0x6bfba... - Player management system
+  * ...
+  */
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 86b885e and 4885ace.

⛔ Files ignored due to path filters (5)
  • Cargo.lock is excluded by !**/*.lock
  • crates/torii/types-test/Scarb.lock is excluded by !**/*.lock
  • examples/simple/Scarb.lock is excluded by !**/*.lock
  • spawn-and-move-db.tar.gz is excluded by !**/*.gz
  • types-test-db.tar.gz is excluded by !**/*.gz
📒 Files selected for processing (2)
  • Cargo.toml (1 hunks)
  • bin/sozo/tests/test_data/policies.json (1 hunks)
🔇 Additional comments (4)
Cargo.toml (1)

76-77: Ohayo sensei! Let's verify the cainome dependency versions.

The update from v0.4.10 to v0.4.11 for both cainome dependencies looks good, but let's ensure we're using the latest stable version.

Would you mind adding a comment explaining why these specific versions were chosen? This helps future maintainers understand the rationale behind version updates.

 [workspace.dependencies]
-cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.11", features = [ "abigen-rs" ] }
-cainome-cairo-serde = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.11" }
+# Updated to v0.4.11 to fix StoreUpdateMember processor indexing issues
+cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.11", features = [ "abigen-rs" ] }
+cainome-cairo-serde = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.11" }
✅ Verification successful

Ohayo sensei! The cainome dependencies are already at the latest version!

Based on the GitHub tags, v0.4.11 is indeed the most recent version available in the cartridge-gg/cainome repository. The update from v0.4.10 to v0.4.11 is spot on!

The suggestion to add a comment explaining the version update rationale still stands as a nitpick, but the version choice itself is perfect.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check latest tags from the cainome repository
gh api \
  -H "Accept: application/vnd.github+json" \
  repos/cartridge-gg/cainome/tags \
  --jq '.[].name' | head -n 5

Length of output: 146

bin/sozo/tests/test_data/policies.json (3)

86-93: Ohayo! New upgrade permissions look good, sensei! 🎯

The addition of upgrade permissions for the new contract addresses aligns with the PR objectives of fixing the StoreUpdateMember processor and addressing indexing failures.


126-133: Additional upgrade permissions are properly configured, sensei! ✨

The upgrade permissions for the remaining addresses look good and follow the same pattern as the earlier upgrade permissions.


94-125: Verify player-related permissions, sensei! 🔍

The new permissions for address 0x6bfba7 include a comprehensive set of player-related operations, including the new update_player_config_name method. This aligns with the changes mentioned in the AI summary regarding player configuration management.

However, let's verify that all required permissions are properly configured to prevent any startup issues with Torii.

✅ Verification successful

All player permissions are properly configured, sensei! ✨

Ohayo! I've verified the player-related permissions and found everything in perfect order:

  • No duplicate permissions were found that could cause conflicts
  • All essential player methods are present, including:
    • Core actions: spawn, move, enter_dungeon
    • Configuration management: set_player_config, update_player_config_name, reset_player_config
    • Profile and models: set_player_server_profile, set_models
    • Character progression: upgrade

The permissions are well-structured and include the new update_player_config_name method along with all other necessary player-related operations. This setup should work smoothly with Torii.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all required player-related methods are properly configured
# and that there are no duplicate permissions that might cause conflicts

# Check for any duplicate method permissions for the same target
jq '[.[] | {target: .target, method: .method}] | group_by({target: .target, method: .method}) | map(select(length > 1))' bin/sozo/tests/test_data/policies.json

# List all player-related methods to ensure completeness
jq '.[] | select(.target == "0x6bfba78b8f4f42da469860a95291c2fad959c91ea747e2062de763ff4e62c4a") | .method' bin/sozo/tests/test_data/policies.json

Length of output: 465

@glihm glihm changed the title fix: remove old check for StoreUpdateMember processor fix(torii-core): remove old check for StoreUpdateMember processor Jan 3, 2025
@glihm glihm merged commit d9e93c4 into main Jan 6, 2025
14 of 16 checks passed
@glihm glihm deleted the fix/torii-member-update branch January 6, 2025 02:47
@coderabbitai coderabbitai bot mentioned this pull request Jan 8, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants