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

chore: implement From<BigUint> for address type #2488

Merged
merged 1 commit into from
Sep 30, 2024
Merged

chore: implement From<BigUint> for address type #2488

merged 1 commit into from
Sep 30, 2024

Conversation

kariy
Copy link
Member

@kariy kariy commented Sep 30, 2024

a little clean up on ContractAddress type

Summary by CodeRabbit

  • New Features

    • Introduced a method to create snapshots of the state in the ForkedStateDb.
    • Enhanced flexibility for constructing ContractAddress from BigUint.
  • Improvements

    • Simplified address handling in encoding and decoding processes.
    • Enhanced error handling for metadata decoding.
  • Bug Fixes

    • Updated logic to prioritize local state over remote state for nonce and storage retrieval.
  • Tests

    • Updated test cases to reflect changes in address handling and state retrieval logic.

Copy link

coderabbitai bot commented Sep 30, 2024

Walkthrough

Ohayo, sensei! The changes in this pull request primarily focus on improving the handling of ContractAddress across various components. Modifications include simplifying the construction of ContractAddress from BigUint, refining encoding and decoding processes, and enhancing state retrieval logic in the ForkedStateDb. Additionally, the introduction of the address! macro streamlines address creation for testing purposes, ensuring consistency and clarity throughout the codebase.

Changes

File Path Change Summary
crates/katana/controller/src/lib.rs Simplified ContractAddress construction in add_controller_account_inner and add_controller_account_json functions; clarified comments in get_contract_storage.
crates/katana/primitives/src/contract.rs Added From trait implementations for ContractAddress from BigUint, enhancing flexibility in address construction.
crates/katana/primitives/src/da/encoding.rs Updated decode_state_updates to simplify ContractAddress construction; enhanced error handling in Metadata struct's decode method; modified ContractUpdate encoding logic.
crates/katana/storage/provider/src/providers/fork/state.rs Introduced create_snapshot method in ForkedStateDb; refined logic for retrieving class hash, nonce, and storage values to prioritize local state.
crates/katana/storage/provider/src/test_utils.rs Changed ContractAddress instantiation method in create_genesis_for_testing function to use address! macro.

Possibly related PRs

Suggested labels

sozo


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

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 25092b8 and eede580.

📒 Files selected for processing (5)
  • crates/katana/controller/src/lib.rs (2 hunks)
  • crates/katana/primitives/src/contract.rs (2 hunks)
  • crates/katana/primitives/src/da/encoding.rs (3 hunks)
  • crates/katana/storage/provider/src/providers/fork/state.rs (2 hunks)
  • crates/katana/storage/provider/src/test_utils.rs (2 hunks)
🔇 Additional comments (10)
crates/katana/primitives/src/contract.rs (1)

3-3: Ohayo, sensei! The new import looks good!

The addition of BigUint from num_bigint is necessary for the new implementations. It's placed appropriately with other imports.

crates/katana/storage/provider/src/test_utils.rs (1)

5-5: Ohayo, sensei! LGTM on the new import.

The addition of use katana_primitives::address; is well-placed and likely supports the new address creation method. Nice work on keeping the imports clean and relevant!

crates/katana/controller/src/lib.rs (3)

50-50: Ohayo, sensei! Simplified ContractAddress construction looks great!

The change to use ContractAddress::from(user.contract_address) directly is a nice simplification. It removes unnecessary conversions and makes the code more readable. This aligns well with the PR's objective of implementing From<BigUint> for the ContractAddress type.


99-99: Ohayo again, sensei! Consistent simplification in add_controller_account_json!

Great job maintaining consistency! The simplification of ContractAddress construction here mirrors the change in add_controller_account_inner. This consistent approach enhances overall code readability and maintainability.


Line range hint 1-265: Ohayo, sensei! Overall assessment of changes

The modifications in this file successfully implement the From<BigUint> trait for the ContractAddress type, as outlined in the PR objectives. The changes simplify the construction of ContractAddress instances, improving code clarity and consistency across the codebase. No issues or concerns were identified during the review.

Great work on this clean-up, sensei! The changes are well-implemented and contribute to a more maintainable codebase.

crates/katana/primitives/src/da/encoding.rs (3)

142-142: Ohayo, sensei! This change looks great!

The simplification of ContractAddress construction from BigUint aligns perfectly with the PR objectives. It makes the code more readable and directly utilizes the newly implemented From<BigUint> trait for ContractAddress.


311-311: Ohayo again, sensei! These test improvements are dojo-level!

The introduction of the address! macro for creating ContractAddress instances in tests is a great clean-up. It enhances readability and provides a more consistent way to create addresses across the test suite. This change aligns well with the "little clean up" mentioned in the PR objectives.

Also applies to: 356-358


142-142: Ohayo once more, sensei! Let's wrap up this review with a final check.

The changes in this file align perfectly with the PR objectives, improving ContractAddress handling and enhancing test readability. These modifications contribute to overall code clarity and consistency without introducing any apparent risks or breaking changes.

To ensure everything is in order, it might be beneficial to run the full test suite and verify that these changes don't have any unexpected impacts on other parts of the codebase.

Also applies to: 311-311, 356-358

crates/katana/storage/provider/src/providers/fork/state.rs (2)

222-224: Ohayo, sensei! Good addition of necessary imports.

The inclusion of address, StateUpdates, StateUpdatesWithDeclaredClasses, and felt macros is appropriate for the enhancements in the test cases below.


233-236: Ohayo, sensei! The use of address! and felt! macros improves test clarity.

Using these macros simplifies the creation of test addresses and felts, enhancing readability and consistency in the test cases.

Comment on lines +54 to +64
impl From<&BigUint> for ContractAddress {
fn from(biguint: &BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}

impl From<BigUint> for ContractAddress {
fn from(biguint: BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ohayo, sensei! The new From implementations look great!

The added implementations for From<&BigUint> and From<BigUint> for ContractAddress are well-structured and align perfectly with the PR objectives. They provide a clean way to convert from BigUint to ContractAddress, which enhances the flexibility of the ContractAddress type.

A small optimization suggestion:

Consider reusing the From<&BigUint> implementation in the From<BigUint> implementation to reduce code duplication:

 impl From<BigUint> for ContractAddress {
     fn from(biguint: BigUint) -> Self {
-        Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
+        Self::from(&biguint)
     }
 }

This change would make the code more DRY and easier to maintain.

📝 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
impl From<&BigUint> for ContractAddress {
fn from(biguint: &BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}
impl From<BigUint> for ContractAddress {
fn from(biguint: BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}
impl From<&BigUint> for ContractAddress {
fn from(biguint: &BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}
impl From<BigUint> for ContractAddress {
fn from(biguint: BigUint) -> Self {
Self::from(&biguint)
}
}

@@ -49,7 +50,7 @@ fn initialize_test_provider<P: BlockWriter>(provider: &P) {
/// - An account with simple `__execute__` function, deployed at address `0x1`.
pub fn create_genesis_for_testing() -> Genesis {
let class_hash = felt!("0x111");
let address = ContractAddress::from(felt!("0x1"));
let address = address!("0x1");
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Ohayo sensei!

I've identified several instances where ContractAddress::from(felt!("...")) is still being used in the codebase. To ensure consistency and improve readability, please update these to utilize the new address! macro.

🔗 Analysis chain

Excellent use of the new address! macro, sensei!

The change from ContractAddress::from(felt!("0x1")) to address!("0x1") simplifies the code and improves readability. It's a great improvement!

To ensure consistency, let's check if this new macro is used throughout the codebase:

This will help us identify any remaining instances of the old method and confirm the adoption of the new macro across the project.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent usage of the address! macro

# Search for ContractAddress::from usage
echo "Checking for remaining ContractAddress::from usage:"
rg --type rust "ContractAddress::from\(felt!\(" -g '!test_utils.rs'

# Search for address! macro usage
echo "Checking for address! macro usage:"
rg --type rust "address!\(" -g '!test_utils.rs'

Length of output: 14172

Copy link

codecov bot commented Sep 30, 2024

Codecov Report

Attention: Patch coverage is 64.28571% with 5 lines in your changes missing coverage. Please review.

Project coverage is 68.96%. Comparing base (ee06e56) to head (eede580).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crates/katana/primitives/src/contract.rs 50.00% 3 Missing ⚠️
crates/katana/controller/src/lib.rs 50.00% 1 Missing ⚠️
crates/katana/storage/provider/src/test_utils.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2488      +/-   ##
==========================================
+ Coverage   68.62%   68.96%   +0.33%     
==========================================
  Files         372      372              
  Lines       48441    48526      +85     
==========================================
+ Hits        33244    33467     +223     
+ Misses      15197    15059     -138     

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

@kariy kariy merged commit afb86a9 into main Sep 30, 2024
14 of 15 checks passed
@kariy kariy deleted the katana/address branch September 30, 2024 23:07
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