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

feat: Allow complex calldata when initializing contracts #2625

Merged
merged 3 commits into from
Nov 4, 2024

Conversation

edisontim
Copy link
Contributor

@edisontim edisontim commented Nov 3, 2024

Closes #2587

Summary by CodeRabbit

  • New Features

    • Added new dependencies to enhance project functionality: hex-literal, num-bigint, and hex in the Cargo.toml files.
    • Introduced a new public module calldata_decoder to expand functionality.
  • Improvements

    • Updated the method for decoding initialization call arguments, streamlining the contract initialization process.
  • Refactor

    • Adjusted import paths for calldata_decoder to reflect a reorganization in the codebase.
    • Removed several unused dependencies from the workspace configuration to streamline dependency management.
  • Bug Fixes

    • Improved error handling in the decoding process for initialization arguments.
    • Enhanced visibility management by removing the snapbox module from tests.

Copy link

coderabbitai bot commented Nov 3, 2024

Walkthrough

Ohayo, sensei! This pull request introduces updates primarily focused on dependency management in the Cargo.toml files across different packages. New dependencies hex-literal and num-bigint are added to enhance functionality. Additionally, the import paths for the calldata_decoder module are updated to reflect a reorganization of the codebase. The public interface is adjusted by removing the calldata_decoder module from mod.rs and adding it in the config module. The initialize_contracts method in migration logic is also modified to improve argument processing.

Changes

File Change Summary
Cargo.toml Added dependencies: hex-literal = "0.4.1", num-bigint = "0.4.3" in workspace dependencies.
bin/sozo/src/commands/call.rs Updated import path for calldata_decoder from crate::commands::calldata_decoder to dojo_world::config::calldata_decoder.
bin/sozo/src/commands/execute.rs Updated import path for calldata_decoder from crate::commands::calldata_decoder to dojo_world::config::calldata_decoder.
bin/sozo/src/commands/mod.rs Removed public module declaration for calldata_decoder.
crates/dojo/world/Cargo.toml Added dependencies: hex, hex-literal, num-bigint marked with workspace = true.
crates/dojo/world/src/config/mod.rs Added new public module declaration for calldata_decoder.
crates/sozo/ops/src/migrate/mod.rs Updated logic in initialize_contracts to utilize decode_calldata from dojo_world::config::calldata_decoder.

Assessment against linked issues

Objective Addressed Explanation
Allow felt252 short strings on init_calldata (Issue #2587) The changes do not implement the requested feature.

Possibly related PRs

Suggested labels

sozo

Suggested reviewers

  • kariy
  • glihm

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f43867d and d00d56f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • bin/sozo/Cargo.toml (1 hunks)
  • bin/sozo/tests/utils/mod.rs (0 hunks)
  • bin/sozo/tests/utils/snapbox.rs (0 hunks)
💤 Files with no reviewable changes (2)
  • bin/sozo/tests/utils/mod.rs
  • bin/sozo/tests/utils/snapbox.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • bin/sozo/Cargo.toml

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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 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

🧹 Outside diff range and nitpick comments (2)
bin/sozo/src/commands/call.rs (1)

14-14: Ohayo sensei! Good architectural decision on the module reorganization!

Moving calldata_decoder to dojo_world::config improves the codebase organization by:

  1. Making the decoder more accessible to other crates
  2. Placing it in a more appropriate location given its configuration-related purpose
crates/sozo/ops/src/migrate/mod.rs (1)

171-172: Consider enhancing error context for better debugging.

Sensei, the current error handling using map_err(|_| MigrationError::InitCallArgs) discards valuable context about what failed during decoding. Consider preserving the original error details.

-                        decode_calldata(&args.join(","))
-                            .map_err(|_| MigrationError::InitCallArgs)?
+                        decode_calldata(&args.join(","))
+                            .map_err(|e| MigrationError::InitCallArgs(format!("Failed to decode calldata: {}", e)))?

This would require updating the MigrationError enum to include the error context:

pub enum MigrationError<E> {
    InitCallArgs(String),
    // ... other variants
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 32af19e and 6b2f241.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • Cargo.toml (2 hunks)
  • bin/sozo/src/commands/call.rs (1 hunks)
  • bin/sozo/src/commands/execute.rs (1 hunks)
  • bin/sozo/src/commands/mod.rs (0 hunks)
  • crates/dojo/world/Cargo.toml (1 hunks)
  • crates/dojo/world/src/config/mod.rs (1 hunks)
  • crates/sozo/ops/src/migrate/mod.rs (2 hunks)
💤 Files with no reviewable changes (1)
  • bin/sozo/src/commands/mod.rs
✅ Files skipped from review due to trivial changes (1)
  • bin/sozo/src/commands/execute.rs
🔇 Additional comments (8)
crates/dojo/world/src/config/mod.rs (1)

1-1: Ohayo sensei! LGTM - Clean module organization.

The addition of calldata_decoder as a public module aligns well with the PR objectives to support complex calldata types during initialization. The module organization follows Rust best practices.

Let's verify the module's integration:

✅ Verification successful

Ohayo sensei! LGTM - Module integration verified successfully

The calldata_decoder module is properly integrated and used across the codebase:

  • Located at expected path: crates/dojo/world/src/config/calldata_decoder.rs
  • Actively used in key command implementations:
    • bin/sozo/src/commands/execute.rs
    • bin/sozo/src/commands/call.rs
    • crates/sozo/ops/src/migrate/mod.rs
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the calldata_decoder module integration and usage

# Test 1: Check if the module file exists
fd --type f "calldata_decoder\.rs$" crates/dojo/world/src/config/

# Test 2: Verify the module's usage across the codebase
rg -l "use.*config::calldata_decoder" 

Length of output: 254

crates/dojo/world/Cargo.toml (1)

27-29: Ohayo! The new dependencies look good, sensei!

The addition of hex, hex-literal, and num-bigint aligns perfectly with the PR's objective of supporting felt252 short strings in calldata. These are essential for hex string processing and arbitrary-precision integer operations.

Let's verify these dependencies are being used appropriately:

✅ Verification successful

Ohayo! Dependencies are being used effectively, sensei!

The verification shows that the new dependencies are actively used in the codebase, particularly in calldata_decoder.rs where:

  • hex is used for decoding hex strings in U256 processing
  • hex-literal is used for constant definition (u128_max_plus_1)
  • num-bigint::BigUint is used for handling large numbers in U256 decoding

The dependencies are not just added but are properly integrated into the codebase's functionality, especially for handling complex calldata types as intended.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of new dependencies in the codebase
# Expected: Find usages in calldata processing related code

echo "Checking hex/hex-literal usage..."
rg -l 'use hex::|use hex_literal::|hex::decode|hex::encode'

echo "Checking num-bigint usage..."
rg -l 'use num_bigint::|BigInt|BigUint'

Length of output: 831


Script:

#!/bin/bash
# Let's check the actual usage in calldata_decoder.rs since it's the main file we're interested in
echo "Checking detailed usage in calldata_decoder.rs..."
rg -A 5 'use (hex|hex_literal|num_bigint)' crates/dojo/world/src/config/calldata_decoder.rs

echo "\nChecking implementations using these dependencies..."
rg -C 10 '(hex::|BigInt|BigUint)' crates/dojo/world/src/config/calldata_decoder.rs

Length of output: 3113

bin/sozo/src/commands/call.rs (1)

Line range hint 24-35: Ohayo sensei! Perfect implementation aligning with PR objectives!

The calldata handling implementation successfully:

  1. Supports felt252 short strings through the sstr prefix
  2. Provides a generic solution for various types (u256, str, int)
  3. Maintains consistency with sozo --calldata command style
Cargo.toml (3)

172-172: Ohayo sensei! The hex-literal dependency addition looks good!

The hex-literal crate is a perfect fit for handling felt252 short strings in calldata, which aligns well with the PR objectives.


182-182: The num-bigint dependency is well-chosen, sensei!

The num-bigint crate is essential for handling arbitrary-precision integers needed for felt252 operations. The version 0.4.3 is stable and maintained.


Line range hint 172-182: Verify dependency compatibility with existing crates

Let's ensure these new dependencies don't conflict with existing ones, particularly with the crypto and numeric libraries already in use.

crates/sozo/ops/src/migrate/mod.rs (2)

25-25: LGTM! Import addition aligns with feature objectives.

Ohayo! The new import for decode_calldata is well-placed and supports the PR's goal of enabling complex calldata types during contract initialization.


171-172: Implementation looks good, but let's verify the comma handling.

The new implementation using decode_calldata successfully enables complex calldata types. However, we should verify how it handles arguments that might contain commas themselves.

✅ Verification successful

Implementation is safe for comma handling, sensei! 🍜

After reviewing the decode_calldata implementation, I can confirm that commas in arguments are properly handled through type prefixes. The decoder supports:

  • String values with str: prefix (e.g., str:hello, world)
  • Short strings with sstr: prefix
  • Numbers with u256: or int: prefix
  • Default hex/decimal values

The test case test_str_decoder_long demonstrates that strings containing spaces and commas are correctly processed. The implementation is robust and handles complex calldata types safely.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential comma-containing arguments in init_call_args
# Look for string literals in init_call_args that might contain commas

rg -A 3 'init_call_args.*=.*".*,.*"' 

Length of output: 38


Script:

#!/bin/bash
# Let's search for any usage of init_call_args to understand the context better
rg -A 5 "init_call_args" 

# Also check for any tests or examples using decode_calldata
rg -A 5 "decode_calldata"

# Look for the implementation of decode_calldata to understand its behavior
ast-grep --pattern 'fn decode_calldata($$$) { $$$ }'

Length of output: 13481


Script:

#!/bin/bash
# Let's check the implementation of decode_calldata and its item delimiter
cat crates/dojo/world/src/config/calldata_decoder.rs

# Also check for any test cases that might involve commas
rg -A 10 "test_.*decoder.*" crates/dojo/world/src/config/calldata_decoder.rs

Length of output: 14152

Copy link

codecov bot commented Nov 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.89%. Comparing base (32af19e) to head (d00d56f).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2625      +/-   ##
==========================================
- Coverage   57.25%   56.89%   -0.36%     
==========================================
  Files         396      397       +1     
  Lines       49162    49464     +302     
==========================================
- Hits        28148    28143       -5     
- Misses      21014    21321     +307     

☔ 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.

Thanks @edisontim for tackling that!
Some comments that require changes, and then let's move on!

Comment on lines 168 to 170
// The injection of class hash and addresses is no longer supported since the
// world contains an internal DNS.
let args = if let Some(args) = init_call_args {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The comment can be updated where we can remove that "only felts are supported".
We can keep the comment related to the DNS, since injection of other contract addresses are not yet supported again.

Comment on lines +27 to +29
hex.workspace = true
hex-literal.workspace = true
num-bigint.workspace = true
Copy link
Collaborator

Choose a reason for hiding this comment

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

those weren't removed from the sozo/Cargo.toml.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, could you uncomment and add to the sozo/main.rs this?

#![warn(unused_crate_dependencies)]

This will ensure we removed unused crates. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I forgot to check afterwards what I had taken out!

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.

Thanks for the cleanup @edisontim! Good feature to have better init_calldata passed to contracts. :)

@glihm glihm merged commit 82d3a85 into dojoengine:main Nov 4, 2024
13 of 14 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.

feat: Allow felt252 short strings on init_calldata
2 participants