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

refactor(katana): simplify genesis class #2948

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

kariy
Copy link
Member

@kariy kariy commented Jan 24, 2025

Simplify how we handle contract classes in the genesis. We removed the responsibility of computing the compiled class hash from the genesis and delegate it fully to the provider level. This will avoid having mismatched compiled class hash in the case where the genesis type is hand-crafted - essentially reducing places where invalid things could potentially happen :)

Copy link

coderabbitai bot commented Jan 24, 2025

Pull Request Analysis

Ohayo, sensei! Let's dive into the details of this intriguing code transformation!

Walkthrough

This pull request introduces a significant refactoring of class handling across multiple Katana crates. The primary focus is on simplifying the representation of contract classes by removing the GenesisClass abstraction. The changes streamline how classes are processed, stored, and managed in the genesis configuration, removing unnecessary wrapper structures and directly using Arc<ContractClass> instead of the previous more complex class representation.

Changes

File Change Summary
crates/katana/chain-spec/src/lib.rs - Simplified class legacy check
- Modified state updates processing
crates/katana/controller/src/lib.rs - Removed class_hash from GenesisClassJson
crates/katana/primitives/src/genesis/json.rs - Removed class_hash and name fields
- Simplified class hash handling
- Updated error management
crates/katana/primitives/src/genesis/mod.rs - Removed GenesisClass struct
- Updated Genesis struct to use Arc<ContractClass> directly
crates/katana/storage/provider/src/test_utils.rs - Removed GenesisClass import
- Simplified class initialization

Possibly related PRs

The changes reflect a trend towards simplification and more direct data representation in the Katana project's codebase.


🪧 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

@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 (3)
crates/katana/primitives/src/genesis/json.rs (2)

298-318: Ohayo sensei, fallback logic for contract parsing
The code attempts to parse a Sierra contract and, if that fails, parses a legacy contract. This dual parsing is effective but might induce a performance overhead. Consider optimizing detection if necessary.


458-460: Ohayo sensei, serializing class artifacts
Iterating over classes to serialize them ensures consistent storage of both Sierra and legacy contracts. The approach appears solid for typical use cases.

crates/katana/chain-spec/src/lib.rs (1)

130-131: Ohayo sensei! Avoid multiple unwrap calls here.
Chained unwrap calls can lead to runtime panics if the class fails to compile. Consider handling errors more gracefully.

Here’s a potential refactor:

- let casm_hash = class.as_ref().clone().compile().unwrap().class_hash().unwrap();
+ let compiled = class.as_ref().clone().compile()?;
+ let casm_hash = compiled.class_hash()?;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8c484fd and 31245bc.

📒 Files selected for processing (5)
  • crates/katana/chain-spec/src/lib.rs (2 hunks)
  • crates/katana/controller/src/lib.rs (1 hunks)
  • crates/katana/primitives/src/genesis/json.rs (10 hunks)
  • crates/katana/primitives/src/genesis/mod.rs (3 hunks)
  • crates/katana/storage/provider/src/test_utils.rs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: ensure-wasm
  • GitHub Check: docs
  • GitHub Check: clippy
  • GitHub Check: build
🔇 Additional comments (22)
crates/katana/primitives/src/genesis/json.rs (12)

20-20: Ohayo sensei, nice import!
This enables better error handling for class hashing and JSON parsing. No concerns here.


27-27: Ohayo sensei, consistent usage of constants
Referencing DEFAULT_ACCOUNT_CLASS and DEFAULT_ACCOUNT_CLASS_HASH ensures uniformity across the code.


30-30: Ohayo sensei, good import for classes
Pulling in ContractClass and SierraContractClass aligns with the refactored design.


32-32: Ohayo sensei, parse_deprecated_compiled_class
Including this utility cleanly supports legacy contract formats.


278-278: Ohayo sensei, simpler data structure for classes
Using Arc<ContractClass> directly streamlines class management by removing extra abstractions.


281-284: Ohayo sensei, conditional controller insertion
Conditionally inserting the controller class is orderly. Ensure to confirm no unexpected side effects if the feature is disabled.


287-287: Ohayo sensei, destructuring of GenesisClassJson
Neat approach that eliminates the now-unused class_hash field. It’s more concise.


335-335: Ohayo sensei, final class insertion
Directly storing the computed class hash and Arc<ContractClass> is clear and efficient.


366-366: Ohayo sensei, default account class insertion
Gracefully inserts the default account class when none is specified. Well-handled fallback.


754-758: Ohayo sensei, enumerating expected default classes in tests
Including fee token, universal deployer, and account class checks ensures robust coverage.


879-879: Ohayo sensei, verifying actual vs. expected classes
An assert like this is crucial for confirming correctness of the loaded classes. Good thoroughness.


933-935: Ohayo sensei, validating default/controller classes
Ensures that both the default account class and the controller class (if enabled) match the expected configuration.

Also applies to: 972-972

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

12-12: Ohayo sensei, streamlined imports
Dropping GenesisClass in favor of direct references to Genesis aligns with the simplified architecture.


53-53: Ohayo sensei, direct Arc usage
Wrapping the parsed class in an Arc is consistent with the rest of the changes removing GenesisClass. Nicely done.

crates/katana/primitives/src/genesis/mod.rs (4)

16-16: Ohayo sensei, importing multiple default constants
Centralizing these constants in a single location is beneficial for code clarity and maintainability.


20-20: Ohayo sensei, shifting from GenesisClass
Using ContractClass directly indicates a more cohesive design.


40-40: Ohayo sensei, adopting Arc
This approach cuts down on unnecessary wrappers and keeps the code simpler overall.


85-85: Ohayo sensei, default classes inlined
Inserting the default, legacy, and optional controller classes directly improves readability. This is a clean improvement.

Also applies to: 87-87, 89-89, 91-91

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

118-119: Ohayo sensei! Removal of the class_hash field looks neat.
This aligns perfectly with storing classes directly instead of referencing the hash.

crates/katana/chain-spec/src/lib.rs (3)

127-127: Ohayo sensei! Simplified check for legacy classes.
Switching from class.class.is_legacy() to class.is_legacy() is more direct and clear.


134-134: Ohayo sensei! Direct insertion into states.classes is concise.
This approach reflects the new architecture of storing classes directly in Genesis, which simplifies management.


327-331: Ohayo sensei! Using the new direct approach for default classes.
Replacing references to GenesisClass with .into() to store classes directly keeps consistency across the codebase.

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