Skip to content

fix(dojo-lang): ensure the dns returns the actual ClassHash. #2935

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

Merged
merged 2 commits into from
Jan 20, 2025

Conversation

glihm
Copy link
Collaborator

@glihm glihm commented Jan 20, 2025

Since this class hash is not used often, it wasn't detected before.
Added a test to avoid regression.

Previously it returned the namespace hash.

Summary by CodeRabbit

  • Tests

    • Added a new test function to validate DNS class hash functionality
    • Verifies correct class hash association for resources in the dojo namespace
  • Improvements

    • Updated DNS method to use syscall for retrieving contract class hash
    • Enhanced reliability of class hash retrieval mechanism

Copy link
Contributor

coderabbitai bot commented Jan 20, 2025

Walkthrough

Ohayo, sensei! The pull request introduces a new test function dns_valid_class_hash in the world test suite, which validates the DNS functionality for class hashes. Simultaneously, the dns method in the world storage implementation has been modified to use a syscall for retrieving the class hash, changing how the hash is obtained from a contract address.

Changes

File Change Summary
crates/dojo/.../world.cairo - Added new test function dns_valid_class_hash()
- Updated import statements to include bar
crates/dojo/.../storage.cairo - Modified dns method to use starknet::syscalls::get_class_hash_at_syscall()
- Replaced direct tuple extraction with syscall-based class hash retrieval

Sequence Diagram

sequenceDiagram
    participant Test as DNS Test
    participant World as World Storage
    participant Syscall as Starknet Syscall

    Test->>World: Call dns("bar")
    World->>Syscall: get_class_hash_at_syscall(contract_address)
    Syscall-->>World: Return class_hash
    World-->>Test: Return (contract_address, class_hash)
Loading

The sequence diagram illustrates the new flow of retrieving a class hash through a syscall when performing a DNS lookup in the world storage implementation.


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

@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)
crates/dojo/core-cairo-test/src/tests/world/world.cairo (1)

314-331: Consider adding more test cases, sensei!

To make the test suite more robust, consider adding:

  1. Test for non-existent contract names
  2. Test for multiple contracts in the same namespace
  3. Test for contracts after upgrade
 #[test]
 pub fn dns_valid_class_hash() {
     let namespace_def = NamespaceDef {
         namespace: "dojo",
         resources: [
             TestResource::Model(m_Foo::TEST_CLASS_HASH),
             TestResource::Contract(bar::TEST_CLASS_HASH),
+            TestResource::Contract(test_contract::TEST_CLASS_HASH),
         ]
             .span(),
     };
 
     let mut world = spawn_test_world([namespace_def].span());
     world.sync_perms_and_inits([].span());
 
     let (_, class_hash) = world.dns(@"bar").unwrap();
     assert!(class_hash == bar::TEST_CLASS_HASH.try_into().unwrap());
+
+    // Test non-existent contract
+    assert!(world.dns(@"nonexistent").is_none());
+
+    // Test another contract in same namespace
+    let (_, class_hash2) = world.dns(@"test_contract").unwrap();
+    assert!(class_hash2 == test_contract::TEST_CLASS_HASH.try_into().unwrap());
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0e6e207 and 863eb3c.

⛔ Files ignored due to path filters (2)
  • spawn-and-move-db.tar.gz is excluded by !**/*.gz
  • types-test-db.tar.gz is excluded by !**/*.gz
📒 Files selected for processing (2)
  • crates/dojo/core-cairo-test/src/tests/world/world.cairo (2 hunks)
  • crates/dojo/core/src/world/storage.cairo (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: fmt
🔇 Additional comments (2)
crates/dojo/core/src/world/storage.cairo (1)

45-50: Ohayo sensei! The DNS implementation now correctly retrieves the actual ClassHash.

The change to use get_class_hash_at_syscall instead of the stored class hash is a robust solution that ensures we always get the current class hash, even if the contract was upgraded.

crates/dojo/core-cairo-test/src/tests/world/world.cairo (1)

314-331: LGTM! The test validates the DNS fix.

The test effectively verifies that the DNS returns the correct class hash for a registered contract.

Copy link

codecov bot commented Jan 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.82%. Comparing base (0e6e207) to head (863eb3c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2935      +/-   ##
==========================================
+ Coverage   55.80%   55.82%   +0.01%     
==========================================
  Files         444      444              
  Lines       57329    57329              
==========================================
+ Hits        31994    32002       +8     
+ Misses      25335    25327       -8     

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

@glihm glihm merged commit fbb972b into main Jan 20, 2025
15 checks passed
@glihm glihm deleted the dojo-lang/fix/dns branch January 20, 2025 23:03
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.

1 participant