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

fix(torii-core): correct parallelized task hash #2860

Merged
merged 1 commit into from
Jan 3, 2025

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Jan 3, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved task identifier generation for specific event types to enhance event processing accuracy.

Copy link

coderabbitai bot commented Jan 3, 2025

Walkthrough

Ohayo, sensei! The changes in the crates/torii/core/src/engine.rs file focus on refining the task identifier generation logic within the process_event method of the Engine struct. The modification involves updating the hashing mechanism for specific event types like StoreSetRecord, StoreUpdateRecord, StoreUpdateMember, and StoreDelRecord. Instead of using the first key for generating the task identifier, the method now uses the second and third keys, which represent the model selector and entity ID.

Changes

File Change Summary
crates/torii/core/src/engine.rs Modified task identifier generation logic for specific event types, using model selector and entity ID keys for hashing

The changes are surgical and precise, focusing on improving the accuracy of event processing by deriving task identifiers from more contextually relevant event data. The method's overall structure remains intact, with only the internal hashing logic being refined.

No public API changes or method signature modifications were introduced in this update.


🪧 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: 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 38f68db and 6f917e2.

📒 Files selected for processing (1)
  • crates/torii/core/src/engine.rs (1 hunks)

Comment on lines +862 to +865
// model selector
event.keys[1].hash(&mut hasher);
// entity id
event.keys[2].hash(&mut hasher);
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Add boundary check for event keys to prevent panic

Ohayo, sensei! After analyzing the codebase, I can confirm that there's no boundary check for the event keys in the hashing logic. While some event processors do validate the length of event keys (like ERC20 and ERC721 processors), the specific code in question that uses event.keys[1] and event.keys[2] for hashing doesn't have such validation.

The suggested fix in the review comment is appropriate:

if event.keys.len() < 3 {
    warn!(target: LOG_TARGET, "Event does not have enough keys (>=3) for hashing required for parallelized tasks. Skipping...");
    return Ok(());
}

Key findings from the codebase:

  • ERC20 transfer validates: event.keys.len() == 3
  • ERC721 transfer validates: event.keys.len() == 5
  • Messaging service validates: event.keys.len() != 4
  • But no validation in the hashing logic that uses indices 1 and 2
🔗 Analysis chain

Ensure event keys have sufficient length to avoid out-of-bounds errors.
Ohayo, sensei! The new hashing logic references event.keys[1] and event.keys[2], but there's no check on the number of keys within event.keys. If the event does not contain at least three keys, it will panic.

As a possible fix, you might add a boundary check before hashing:

                 let mut hasher = DefaultHasher::new();
+                // Ensure we have enough keys for hashing
+                if event.keys.len() < 3 {
+                    warn!(target: LOG_TARGET, "Event does not have enough keys (>=3) for hashing required for parallelized tasks. Skipping...");
+                    return Ok(());
+                }
                 // model selector
                 event.keys[1].hash(&mut hasher);

                 // entity id
                 event.keys[2].hash(&mut hasher);
                 hasher.finish()

To verify whether there are any existing checks to ensure a sufficient number of keys for these event types, consider running:


Let me check for any validation of event keys before they are accessed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# ohayo sensei! Let's check if there are boundary checks for events with event.keys indexing
rg -A 5 "event.keys\[" 

Length of output: 25064


Script:

#!/bin/bash
# Let's check for any validation of event keys length
rg -A 5 "event\.keys\.len" 

Length of output: 3706

Copy link

codecov bot commented Jan 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.83%. Comparing base (38f68db) to head (6f917e2).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2860      +/-   ##
==========================================
+ Coverage   55.82%   55.83%   +0.01%     
==========================================
  Files         446      446              
  Lines       57728    57730       +2     
==========================================
+ Hits        32225    32234       +9     
+ Misses      25503    25496       -7     

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

Copy link
Collaborator

@glihm glihm left a comment

Choose a reason for hiding this comment

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

Nice catch, this way all events related to the same entity, including the updates, will be processed in order.

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