Skip to content

feat(torii): check all contracts deployment on startup - #3134

Merged
Larkooo merged 4 commits into
dojoengine:mainfrom
Larkooo:check-contracts-deployed
Mar 31, 2025
Merged

feat(torii): check all contracts deployment on startup#3134
Larkooo merged 4 commits into
dojoengine:mainfrom
Larkooo:check-contracts-deployed

Conversation

@Larkooo

@Larkooo Larkooo commented Mar 31, 2025

Copy link
Copy Markdown
Collaborator

#3122

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Introduced a pre-execution check that verifies all required contracts are deployed.
    • Provides clear, detailed error notifications when any contract is not found, improving operational reliability.

@coderabbitai

coderabbitai Bot commented Mar 31, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Ohayo, sensei! The changes introduce a new asynchronous function, verify_contracts_deployed, in the crates/torii/runner/src/lib.rs file. This function utilizes a JsonRpcClient to query the blockchain for specified contracts at the pending block tag and returns a list of contracts that are not yet deployed. Additionally, the run method of the Runner struct now calls this function after creating the client, returning an error if any contracts are undeployed. New imports for BlockId, BlockTag, and Provider have also been added, and an outdated provider instantiation has been removed.

Changes

File Change Summary
crates/torii/.../lib.rs - Added new async function verify_contracts_deployed to check contract deployment via JsonRpcClient.
- Modified Runner::run to call this function and handle errors.
- Updated imports for BlockId, BlockTag, and Provider; removed previous provider instantiation.

Sequence Diagram(s)

sequenceDiagram
    participant R as Runner
    participant J as JsonRpcClient
    participant V as verify_contracts_deployed
    participant BC as Blockchain

    R->>J: Instantiate JsonRpcClient
    R->>V: Call verify_contracts_deployed(contracts)
    V->>J: Query contract info at pending BlockTag
    J-->>V: Return contract data or error
    V-->>R: Return list of undeployed contracts
    alt Undeployed contracts found
        R-->>R: Error returned with undeployed contract details
    else All contracts deployed
        R->>R: Continue execution
    end
Loading

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 plan to trigger planning for file edits and PR creation.
  • @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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
crates/torii/runner/src/lib.rs (1)

354-370: Solid implementation of contract verification, but consider a few enhancements.

The function effectively checks if contracts are deployed by attempting to retrieve their class at the pending block tag. However, a few improvements could make this even better:

  1. Consider logging the specific errors to help diagnose deployment issues
  2. For a large number of contracts, parallel checks might be more efficient
async fn verify_contracts_deployed(
    provider: &JsonRpcClient<HttpTransport>,
    contracts: &[Contract],
) -> anyhow::Result<Vec<Contract>> {
    let mut undeployed = Vec::new();

    for contract in contracts {
-        match provider.get_class_at(BlockId::Tag(BlockTag::Pending), contract.address).await {
-            Ok(_) => continue,
-            Err(_) => {
-                undeployed.push(contract.clone());
-            }
-        }
+        match provider.get_class_at(BlockId::Tag(BlockTag::Pending), contract.address).await {
+            Ok(_) => continue,
+            Err(err) => {
+                tracing::debug!(
+                    target: crate::constants::LOG_TARGET,
+                    address = %contract.address,
+                    error = %err,
+                    "Contract not deployed"
+                );
+                undeployed.push(contract.clone());
+            }
+        }
    }

    Ok(undeployed)
}

For larger contract sets, you might consider using futures::future::join_all for parallel verification, sensei!

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ecf6889 and 4eface1.

📒 Files selected for processing (1)
  • crates/torii/runner/src/lib.rs (3 hunks)
🔇 Additional comments (2)
crates/torii/runner/src/lib.rs (2)

27-29: Ohayo! Nice addition of necessary imports for contract verification.

The new imports for BlockId, BlockTag, and Provider are necessary for the contract deployment verification functionality. Good choice, sensei!


98-108: Early verification of contract deployment - smart approach, sensei!

Moving the provider creation earlier and adding contract deployment verification before proceeding with other initialization steps is an excellent defensive programming practice. This will prevent runtime issues by failing fast if any contracts are not properly deployed.

@codecov

codecov Bot commented Mar 31, 2025

Copy link
Copy Markdown

Codecov Report

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

Project coverage is 55.72%. Comparing base (ecf6889) to head (a933e73).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/runner/src/lib.rs 0.00% 22 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3134      +/-   ##
==========================================
- Coverage   55.74%   55.72%   -0.03%     
==========================================
  Files         443      443              
  Lines       62750    62770      +20     
==========================================
- Hits        34983    34976       -7     
- Misses      27767    27794      +27     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
crates/torii/runner/src/lib.rs (1)

354-370: Clean and effective implementation, sensei!

The new verify_contracts_deployed function is well-structured and focused. It:

  1. Accepts a provider and contracts slice
  2. Queries each contract's class at the pending block
  3. Collects undeployed contracts into a vector
  4. Returns the result

A small suggestion to make error handling more informative:

-        match provider.get_class_at(BlockId::Tag(BlockTag::Pending), contract.address).await {
-            Ok(_) => continue,
-            Err(_) => {
+        match provider.get_class_at(BlockId::Tag(BlockTag::Pending), contract.address).await {
+            Ok(_) => continue,
+            Err(err) => {
+                tracing::debug!(
+                    target: LOG_TARGET,
+                    "Contract at address {} not deployed: {:?}",
+                    contract.address,
+                    err
+                );
                 undeployed.push(*contract);
             }
         }

This would log the specific error for each undeployed contract, which could help with troubleshooting.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 4eface1 and a933e73.

📒 Files selected for processing (1)
  • crates/torii/runner/src/lib.rs (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (2)
crates/torii/runner/src/lib.rs (2)

27-27: Ohayo, sensei! Nice work on the necessary imports.

The additions of BlockId, BlockTag from the starknet::core::types module and Provider from starknet::providers support the new contract deployment verification functionality. These imports are precisely what's needed for the new feature.

Also applies to: 29-29


98-108: Great improvement to startup validation, sensei!

The code now creates an Arc-wrapped JsonRpcClient and verifies all contracts are deployed before proceeding with the application setup. This is excellent defensive programming that will prevent runtime issues when contracts aren't available. The error message clearly indicates which contracts are missing, making debugging easier.

@Larkooo
Larkooo enabled auto-merge (squash) March 31, 2025 07:17

@glihm glihm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This may slow down a bit the startup based on the provider and number of contracts, but for sure better to know that the contracts are actually there before working on them.

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