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

hotfix(katana): missing erc20 class on custom genesis #2582

Merged
merged 1 commit into from
Oct 26, 2024

Conversation

kariy
Copy link
Member

@kariy kariy commented Oct 26, 2024

Summary by CodeRabbit

  • Refactor
    • Streamlined fee token handling by introducing a new method for adding default fee tokens.
    • Updated constants for legacy ERC20 class hash for consistency.
  • Bug Fixes
    • No changes to public interface or data management, ensuring stability in functionality.

Copy link

coderabbitai bot commented Oct 26, 2024

Walkthrough

Ohayo, sensei! This pull request refactors the fee token handling in the ChainSpec struct found in chain_spec.rs. It introduces a new method, add_default_fee_tokens, which consolidates the addition of ETH and STRK fee tokens into a single function. Additionally, constants related to the legacy ERC20 class hash have been updated for consistency. The existing method add_fee_token remains unchanged but is now utilized within the new method. Overall, the structure and public interface of ChainSpec remain intact.

Changes

File Path Change Summary
crates/katana/primitives/src/chain_spec.rs - Added method add_default_fee_tokens for streamlined fee token handling.
- Updated constants DEFAULT_LEGACY_ERC20_CLASS_HASH and DEFAULT_LEGACY_ERC20_CASM for consistency.

Possibly related PRs


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

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d081602 and ec26bc4.

📒 Files selected for processing (1)
  • crates/katana/primitives/src/chain_spec.rs (3 hunks)
🔇 Additional comments (2)
crates/katana/primitives/src/chain_spec.rs (2)

16-17: LGTM! Constants are properly organized.

Ohayo! The constants are well-organized and maintain clear separation between ERC20 and UDC-related constants.


110-110: LGTM! Clean refactoring of fee token initialization.

Ohayo sensei! The refactoring nicely consolidates the fee token initialization into a single function call, improving code organization and maintainability.

Comment on lines +148 to +176
fn add_default_fee_tokens(states: &mut StateUpdatesWithDeclaredClasses, genesis: &Genesis) {
// declare erc20 token contract
states
.declared_compiled_classes
.entry(DEFAULT_LEGACY_ERC20_CLASS_HASH)
.or_insert_with(|| DEFAULT_LEGACY_ERC20_CASM.clone());

// -- ETH
add_fee_token(
states,
"Ether",
"ETH",
18,
DEFAULT_ETH_FEE_TOKEN_ADDRESS,
DEFAULT_LEGACY_ERC20_CLASS_HASH,
&genesis.allocations,
);

// -- STRK
add_fee_token(
states,
"Starknet Token",
"STRK",
18,
DEFAULT_STRK_FEE_TOKEN_ADDRESS,
DEFAULT_LEGACY_ERC20_CLASS_HASH,
&genesis.allocations,
);
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding error handling for class declaration.

Ohayo sensei! While the implementation is solid, consider adding error handling for the ERC20 class declaration. If the class hash is already declared with a different compiled class hash, it could lead to inconsistencies.

Consider this approach:

 fn add_default_fee_tokens(states: &mut StateUpdatesWithDeclaredClasses, genesis: &Genesis) {
     // declare erc20 token contract
-    states
-        .declared_compiled_classes
-        .entry(DEFAULT_LEGACY_ERC20_CLASS_HASH)
-        .or_insert_with(|| DEFAULT_LEGACY_ERC20_CASM.clone());
+    if let Some(existing_casm) = states.declared_compiled_classes.get(&DEFAULT_LEGACY_ERC20_CLASS_HASH) {
+        if existing_casm != &DEFAULT_LEGACY_ERC20_CASM {
+            // Handle the case where the class is already declared with different bytecode
+            panic!("ERC20 class hash already declared with different bytecode");
+        }
+    } else {
+        states
+            .declared_compiled_classes
+            .insert(DEFAULT_LEGACY_ERC20_CLASS_HASH, DEFAULT_LEGACY_ERC20_CASM.clone());
+    }
📝 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
fn add_default_fee_tokens(states: &mut StateUpdatesWithDeclaredClasses, genesis: &Genesis) {
// declare erc20 token contract
states
.declared_compiled_classes
.entry(DEFAULT_LEGACY_ERC20_CLASS_HASH)
.or_insert_with(|| DEFAULT_LEGACY_ERC20_CASM.clone());
// -- ETH
add_fee_token(
states,
"Ether",
"ETH",
18,
DEFAULT_ETH_FEE_TOKEN_ADDRESS,
DEFAULT_LEGACY_ERC20_CLASS_HASH,
&genesis.allocations,
);
// -- STRK
add_fee_token(
states,
"Starknet Token",
"STRK",
18,
DEFAULT_STRK_FEE_TOKEN_ADDRESS,
DEFAULT_LEGACY_ERC20_CLASS_HASH,
&genesis.allocations,
);
}
fn add_default_fee_tokens(states: &mut StateUpdatesWithDeclaredClasses, genesis: &Genesis) {
// declare erc20 token contract
if let Some(existing_casm) = states.declared_compiled_classes.get(&DEFAULT_LEGACY_ERC20_CLASS_HASH) {
if existing_casm != &DEFAULT_LEGACY_ERC20_CASM {
// Handle the case where the class is already declared with different bytecode
panic!("ERC20 class hash already declared with different bytecode");
}
} else {
states
.declared_compiled_classes
.insert(DEFAULT_LEGACY_ERC20_CLASS_HASH, DEFAULT_LEGACY_ERC20_CASM.clone());
}
// -- ETH
add_fee_token(
states,
"Ether",
"ETH",
18,
DEFAULT_ETH_FEE_TOKEN_ADDRESS,
DEFAULT_LEGACY_ERC20_CLASS_HASH,
&genesis.allocations,
);
// -- STRK
add_fee_token(
states,
"Starknet Token",
"STRK",
18,
DEFAULT_STRK_FEE_TOKEN_ADDRESS,
DEFAULT_LEGACY_ERC20_CLASS_HASH,
&genesis.allocations,
);
}

Copy link

codecov bot commented Oct 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.62%. Comparing base (d081602) to head (ec26bc4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2582      +/-   ##
==========================================
- Coverage   69.63%   69.62%   -0.01%     
==========================================
  Files         401      401              
  Lines       50759    50768       +9     
==========================================
+ Hits        35344    35347       +3     
- Misses      15415    15421       +6     

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

@kariy kariy merged commit e54c4c2 into main Oct 26, 2024
15 checks passed
@kariy kariy deleted the katana/missing-erc20-on-custom-genesis branch October 26, 2024 02:52
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