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(katana): global compiled class cache #3004

Merged
merged 13 commits into from
Feb 11, 2025
Merged

Conversation

kariy
Copy link
Member

@kariy kariy commented Feb 10, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a public error handling interface to deliver clearer error messages.
    • Updated dependency management to simplify feature configurations.
    • Added new error variants to enhance error reporting capabilities.
  • Refactor/Chores

    • Streamlined state management and caching mechanisms for improved efficiency.
    • Revised contract class handling and state proof methods for enhanced internal consistency.
    • Simplified the NoopStateProvider by removing unused methods and dependencies.
  • Bug Fixes

    • Corrected error message text for better clarity.
  • Tests

    • Expanded test coverage to support new contract class provider extensions.

@kariy kariy marked this pull request as ready for review February 11, 2025 03:31
Copy link

coderabbitai bot commented Feb 11, 2025

Walkthrough

Ohayo, sensei! This pull request refactors various aspects of Katana’s state management and contract class handling. It removes the dependency on katana-trie in the executor package and updates state initialization and caching in the blockifier module. Several methods related to state proofs, state roots, and compiled class retrieval have been removed or migrated to new traits. Additionally, changes in import paths, error handling, and test enhancements further streamline the codebase for improved modularity and clarity.

Changes

File(s) Summary
crates/katana/executor/Cargo.toml, crates/katana/node/Cargo.toml Removed dependency katana-trie from executor; added katana-provider in node and removed it from the full-node feature.
crates/katana/executor/src/implementation/blockifier/mod.rs, crates/katana/executor/src/implementation/blockifier/state.rs Made the state module public; introduced COMPILED_CLASS_CACHE; refactored the CachedState and StateProviderDb structures with updated initialization and state access patterns.
crates/katana/executor/benches/execution.rs, crates/katana/executor/src/abstraction/mod.rs, crates/katana/executor/src/implementation/blockifier/utils.rs, crates/katana/pool/src/validation/stateful.rs Updated import paths and constructor parameters for StateProviderDb (adding a default argument and cache cloning); revised type aliases and removed redundant state provider implementations; added a new to_class function and updated class conversion logic.
crates/katana/executor/src/implementation/noop.rs, crates/katana/executor/tests/executor.rs Removed several methods from the NoopStateProvider trait implementations (state proofs and roots); added an import for ContractClassProviderExt in tests.
Multiple files under crates/katana/storage/provider/src/ and crates/katana/storage/provider/tests/ (including providers in db, fork, in_memory, and traits in contract.rs and state.rs) Eliminated the compiled_class methods from various provider implementations; added default error-returning implementations for state roots and proofs; removed the auto_impl attribute from ContractClassProvider and introduced the new ContractClassProviderExt trait; updated error variants and corrected error messages.

Sequence Diagram(s)

sequenceDiagram
    participant VM as StarknetVMProcessor
    participant CS as CachedState
    participant SP as StateProviderDb
    participant Cache as COMPILED_CLASS_CACHE

    VM->>CS: Call simulate_with(...)
    CS->>SP: Initialize new StateProviderDb (with state & defaults)
    SP-->>CS: Return processed state
    CS->>Cache: Retrieve compiled class if needed
    Cache-->>CS: Return contract class info
    CS-->>VM: Return updated state
Loading

Possibly related PRs


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c6d1d2a and 52c0b83.

📒 Files selected for processing (2)
  • crates/katana/storage/provider/src/providers/db/mod.rs (0 hunks)
  • crates/katana/storage/provider/src/traits/contract.rs (3 hunks)
💤 Files with no reviewable changes (1)
  • crates/katana/storage/provider/src/providers/db/mod.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/katana/storage/provider/src/traits/contract.rs

🪧 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

🔭 Outside diff range comments (1)
crates/katana/executor/src/implementation/blockifier/utils.rs (1)

584-611: 🛠️ Refactor suggestion

Class conversion logic needs more robust error handling.

The TODO comment indicates uncertainty about error handling in class conversions. Consider implementing a custom error type for better error handling and documentation.

+#[derive(Debug, thiserror::Error)]
+pub enum ClassConversionError {
+    #[error("Invalid legacy class: {0}")]
+    InvalidLegacy(#[from] ProgramError),
+    #[error("Invalid sierra class: {0}")]
+    InvalidSierra(#[from] ProgramError),
}

-pub fn to_class_info(class: class::CompiledClass) -> Result<ClassInfo, ProgramError> {
+pub fn to_class_info(class: class::CompiledClass) -> Result<ClassInfo, ClassConversionError> {
🧹 Nitpick comments (5)
crates/katana/executor/src/abstraction/mod.rs (1)

10-10: Consider avoiding wildcard imports.
pub use crate::error::*; might inadvertently bring unwanted items into scope. If you prefer more explicit imports, consider selectively importing only the needed items.

crates/katana/executor/src/implementation/blockifier/state.rs (2)

33-45: Monitor potential lock contention.
Using a single Arc<Mutex<...>> for both cached state and declared classes is convenient, but multiple threads might compete for the same lock. Consider an RwLock or finer-grained locks if scalability becomes a concern.


123-124: Empty trait impl placeholders.
StateProofProvider and StateRootProvider are currently empty. If they’re intended for future functionality, consider adding a TODO or doc comment for clarity.

crates/katana/pool/src/validation/stateful.rs (1)

91-92: Consider adding error handling for state initialization.

The state initialization could fail if there are issues with the cache. Consider wrapping this in a Result to handle potential errors gracefully.

-        let cached_state =
-            CachedState::new(StateProviderDb::new(state, COMPILED_CLASS_CACHE.clone()));
+        let cached_state = CachedState::new(StateProviderDb::new(state, COMPILED_CLASS_CACHE.clone()))
+            .map_err(|e| Error::new("Failed to initialize cached state", e))?;
crates/katana/executor/src/implementation/blockifier/mod.rs (1)

170-170: Watch out for potential deadlock scenarios.

Multiple places in the code acquire locks on the state. Consider documenting the lock ordering to prevent potential deadlocks, especially in the simulate_with and execute_transactions methods.

Consider adding a comment documenting the lock acquisition order:

// Lock ordering: 
// 1. state.inner
// 2. cached_state
// Always acquire locks in this order to prevent deadlocks

Also applies to: 199-199, 214-214, 329-330

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9f744a0 and c6d1d2a.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (19)
  • crates/katana/executor/Cargo.toml (0 hunks)
  • crates/katana/executor/benches/execution.rs (2 hunks)
  • crates/katana/executor/src/abstraction/mod.rs (1 hunks)
  • crates/katana/executor/src/implementation/blockifier/mod.rs (7 hunks)
  • crates/katana/executor/src/implementation/blockifier/state.rs (3 hunks)
  • crates/katana/executor/src/implementation/blockifier/utils.rs (6 hunks)
  • crates/katana/executor/src/implementation/noop.rs (2 hunks)
  • crates/katana/executor/src/lib.rs (1 hunks)
  • crates/katana/executor/tests/executor.rs (1 hunks)
  • crates/katana/node/Cargo.toml (2 hunks)
  • crates/katana/pool/src/validation/stateful.rs (2 hunks)
  • crates/katana/storage/provider/src/error.rs (2 hunks)
  • crates/katana/storage/provider/src/providers/db/state.rs (0 hunks)
  • crates/katana/storage/provider/src/providers/fork/backend.rs (1 hunks)
  • crates/katana/storage/provider/src/providers/fork/state.rs (4 hunks)
  • crates/katana/storage/provider/src/providers/in_memory/state.rs (1 hunks)
  • crates/katana/storage/provider/src/traits/contract.rs (3 hunks)
  • crates/katana/storage/provider/src/traits/state.rs (3 hunks)
  • crates/katana/storage/provider/tests/class.rs (1 hunks)
💤 Files with no reviewable changes (2)
  • crates/katana/executor/Cargo.toml
  • crates/katana/storage/provider/src/providers/db/state.rs
✅ Files skipped from review due to trivial changes (2)
  • crates/katana/storage/provider/tests/class.rs
  • crates/katana/executor/src/lib.rs
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: docs
  • GitHub Check: build
  • GitHub Check: clippy
  • GitHub Check: ensure-wasm
🔇 Additional comments (29)
crates/katana/storage/provider/src/traits/contract.rs (2)

7-7: Ohayo! Nice simplification of the trait bounds, sensei!

Removing Send + Sync bounds from ContractClassProvider makes the trait more flexible and easier to implement for various types.


37-48: Excellent trait design with extension pattern!

The new ContractClassProviderExt trait elegantly separates the compiled class functionality into an extension trait with a default implementation. This approach:

  • Maintains backward compatibility
  • Allows for custom implementations when needed
  • Provides sensible default behavior
crates/katana/storage/provider/src/traits/state.rs (2)

26-28: Smart default implementations for state root methods!

The default implementations returning appropriate errors make it clear when functionality is not supported while reducing boilerplate in implementations.

Also applies to: 31-33, 36-39


98-116: Well-structured error handling for state proofs!

The default implementations for state proof methods follow a consistent pattern and provide clear error messages. This is a good example of the fail-fast principle.

crates/katana/executor/src/implementation/noop.rs (1)

168-169: Clean and concise noop implementations!

Taking advantage of the default implementations from the traits makes the noop provider much cleaner and easier to maintain.

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

99-103: Well-defined error variants for state operations!

The new error variants StateProofNotSupported and StateRootNotFound provide clear and specific error messages for these operations.


119-119: Arigato for fixing the typo, sensei!

Fixed typo in error message from "soemthing" to "Something".

crates/katana/storage/provider/src/providers/in_memory/state.rs (1)

141-154: Ohayo! The changes look good, sensei!

The removal of the compiled_class method from the ContractClassProvider implementation aligns with the PR's objective of implementing a global compiled class cache. The remaining functionality for handling class hashes and contract classes is preserved.

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

98-99: Ohayo! The changes look good, sensei!

The empty trait implementations for StateProofProvider and StateRootProvider clearly indicate that these features are not supported in forked mode. This is a clean way to handle unsupported functionality.

Also applies to: 138-139

crates/katana/executor/tests/executor.rs (1)

15-15: Ohayo! The changes look good, sensei!

The addition of the ContractClassProviderExt import shows that the test has been properly updated to use the new trait for accessing compiled class functionality.

crates/katana/storage/provider/src/providers/fork/backend.rs (1)

576-631: Ohayo! The changes look good, sensei!

The ContractClassProvider implementation has been cleanly updated to remove the compiled_class method while retaining the essential compiled_class_hash_of_class_hash functionality. The extensive test coverage ensures that request deduplication continues to work correctly.

crates/katana/node/Cargo.toml (2)

16-16: Ohayo! The katana-provider addition looks great, sensei!

The addition of katana-provider as a workspace dependency aligns well with the global compiled class cache objectives.


55-55: Verify the full-node feature functionality, sensei!

The removal of katana-provider from the full-node feature suggests it's now required in the base configuration. Please ensure this change doesn't affect the experimental full-node mode's behavior.

Run this script to check for any remaining references to katana-provider in full-node related code:

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Search for katana-provider usage in full-node related files
rg -l "katana.?provider" "src/full/"

Length of output: 86


Ohayo, sensei! It looks like the initial script didn't yield any results because the "src/full/" directory isn’t present. I went ahead and conducted a global search for any occurrences of "katana-provider" to verify its removal from the experimental full‑node configuration.

Please run this script to search the entire repository:

Once you verify that there are no stray references and that the removal is intentional, please also run your integration tests or manually exercise the experimental full-node mode to ensure its runtime behavior remains correct.


🏁 Script executed:

[run_scripts]
#!/bin/bash
# Search for any references to katana-provider in the repository
rg -l "katana-provider" .

Length of output: 591


Ohayo, sensei! The global search shows that katana-provider is still referenced broadly, but the specific removal from the full-node feature in crates/katana/node/Cargo.toml seems intentional. However, since the search in the src/full/ directory didn’t yield results (likely because that directory doesn't exist), we should verify the feature configuration in the Cargo.toml files.

Let's dive deeper into the crates/katana/node/Cargo.toml file to inspect the full-node feature definition and see if it’s still logically consistent despite removing the katana-provider. Could you please execute the following script to output the contents of the feature configuration in that file?

Once we confirm that the dependency on katana-provider has been intentionally removed and the related modules are correctly factored as base dependencies, we can be sure the experimental full-node mode remains intact.


🏁 Script executed:

[run_scripts]
#!/bin/bash
# Output the feature definitions from the crates/katana/node/Cargo.toml file
rg -A 10 "\[features\]" crates/katana/node/Cargo.toml

Length of output: 456


Ohayo, sensei! The feature configuration in crates/katana/node/Cargo.toml clearly shows that the experimental full-node feature now only depends on "dep:katana-feeder-gateway" and "dep:tokio", with no explicit reference to katana-provider. This confirms that the removal was intentional, and that the katana-provider should now be part of the base configuration for both full‑node and non‑full node modes.

I recommend running your integration tests or manual runtime verification on the full‑node mode to ensure everything behaves as expected.

crates/katana/executor/src/abstraction/mod.rs (2)

8-8: Ohayo sensei! No concerns with the new import.
This streamlined import for ContractAddress and Felt looks consistent with the rest of the codebase.


12-12: Explicit reference to ExecutorError is clean.
The updated type alias referencing crate::error::ExecutorError is clearer and ensures the error type is easy to trace.

crates/katana/executor/src/implementation/blockifier/state.rs (9)

2-2: Ohayo sensei! Good job refining the imports.
These newly added imports (e.g., Deref, BlockifierContractClass, etc.) neatly align with the updated trait implementations below.

Also applies to: 5-5, 11-11, 14-14, 18-18


22-25: Lifetime 'a usage looks fine.
Defining CachedState<'a> with an internal Arc<Mutex<...>> is valid for the lifetime you introduce. Just ensure external references don't outlive 'a.


28-31: Straightforward internal struct definition.
CachedStateInner<'a> properly bundles the cached state and declared classes. No immediate issues spotted.


48-72: Implementation for ContractClassProvider is solid.
Lock usage, valid checks, and fallback to the underlying cached_state are consistent. Error handling also seems straightforward.


75-121: Consistent StateProvider methods.
All logic for class hashes, nonces, and storage checks is clearly handled. The early return for non-deployed contracts is a good safeguard.


126-130: StateProviderDb struct is a clean wrapper.
Wrapping the provider and cache logic is a neat approach. The lifetime annotation 'a lines up with your design.


132-139: Deref implementation is straightforward.
No hidden complexities. This approach makes your Box<dyn StateProvider> usage more ergonomic.


140-147: Constructor for StateProviderDb is fine.
Accepting Box<dyn StateProvider + 'a> and a compiled class cache feels flexible and consistent.


149-222: StateReader implementation handles caching nicely.
Good use of trace! to log cache hits/misses. The fallback to your provider on cache misses is logical. Overall, the pattern is well-structured.

crates/katana/executor/benches/execution.rs (2)

6-7: Ohayo sensei! Updated imports match the new module structure.
Switching to katana_executor::implementation::blockifier::state::StateProviderDb is consistent with the recent refactor. No issues spotted.


49-49: Instantiating StateProviderDb with a default cache is clear.
CachedState::new(StateProviderDb::new(state, Default::default())) nicely leverages the default for your compiled class cache. This ensures a clean setup for benchmarks.

crates/katana/pool/src/validation/stateful.rs (1)

12-12: Ohayo sensei! New imports look good but let's verify the order.

The imports are logically grouped and follow Rust's convention of separating external and internal imports.

Also applies to: 16-17

crates/katana/executor/src/implementation/blockifier/mod.rs (1)

36-37: Global cache implementation looks solid!

The use of LazyLock with Arc provides thread-safe initialization and access to the cache. Good choice for a global compiled class cache.

crates/katana/executor/src/implementation/blockifier/utils.rs (1)

613-623: Clean implementation of class conversion!

The to_class function provides a clear separation between legacy and new class formats. Good pattern matching and error propagation.

Copy link

codecov bot commented Feb 11, 2025

Codecov Report

Attention: Patch coverage is 73.98374% with 32 lines in your changes missing coverage. Please review.

Project coverage is 56.41%. Comparing base (9f744a0) to head (52c0b83).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
crates/katana/storage/provider/src/traits/state.rs 0.00% 23 Missing ⚠️
...na/executor/src/implementation/blockifier/state.rs 87.83% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3004      +/-   ##
==========================================
- Coverage   56.99%   56.41%   -0.58%     
==========================================
  Files         431      431              
  Lines       57151    57162      +11     
==========================================
- Hits        32572    32247     -325     
- Misses      24579    24915     +336     

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

@kariy kariy merged commit da76156 into main Feb 11, 2025
14 of 15 checks passed
@kariy kariy deleted the katana/executor-refactor branch February 11, 2025 19:04
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