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-pool): invalidate declare tx if the class already exists #2564

Merged
merged 3 commits into from
Oct 21, 2024

Conversation

kariy
Copy link
Member

@kariy kariy commented Oct 21, 2024

add a static check in the pool validator to check if the class that a Declare tx is trying to declare has already existed (ie already been declared). this is align with the spec

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new error variant for handling attempts to declare an already existing class.
    • Enhanced transaction validation process with checks for class existence and refined nonce handling.
    • Added a new macro for asserting Starknet errors in tests.
  • Bug Fixes

    • Improved error handling for class declaration errors in the API.
  • Tests

    • Added a new test case for declaring an already existing class, ensuring proper error handling.

Copy link

coderabbitai bot commented Oct 21, 2024

Walkthrough

Ohayo, sensei! This pull request introduces several changes to the katana codebase, primarily focusing on error handling in transaction validation. A new variant, ClassAlreadyDeclared, is added to the InvalidTransactionError enum to manage errors when declaring an already existing class. Additionally, a new method for creating Error instances is introduced, and the validate method in TxValidator is enhanced to include checks for existing classes. Corresponding changes are made in the StarknetApiError enum, and new tests are added to ensure proper error handling.

Changes

File Path Change Summary
crates/katana/pool/src/validation/error.rs - New enum variant added: ClassAlreadyDeclared { class_hash: ClassHash } in InvalidTransactionError.
crates/katana/pool/src/validation/mod.rs - Method added: pub fn new(hash: TxHash, error: Box<dyn std::error::Error>) -> Self in Error.
crates/katana/pool/src/validation/stateful.rs - Logic modified in validate method of TxValidator to check if a class already exists.
crates/katana/rpc/rpc-types/src/error/starknet.rs - Enum variant added: ClassAlreadyDeclared in StarknetApiError.
- Updated From<Box<InvalidTransactionError>> to include new match arm for ClassAlreadyDeclared.
crates/katana/rpc/rpc/tests/starknet.rs - Macro added: macro_rules! assert_starknet_err.
- New test function added: async fn declaring_already_existing_class().

Possibly related PRs


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8fc30ed and ccdb668.

📒 Files selected for processing (3)
  • crates/katana/pool/src/validation/error.rs (1 hunks)
  • crates/katana/pool/src/validation/stateful.rs (2 hunks)
  • crates/katana/rpc/rpc-types/src/error/starknet.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/katana/pool/src/validation/error.rs
  • crates/katana/rpc/rpc-types/src/error/starknet.rs
🧰 Additional context used
🔇 Additional comments (2)
crates/katana/pool/src/validation/stateful.rs (2)

105-116: Ohayo, sensei! Excellent implementation of class existence check.

The added code block effectively implements the static check for already declared classes, aligning perfectly with the PR objective. This enhancement improves the integrity of the transaction process by preventing duplicate class declarations.


141-142: Ohayo again, sensei! Nice touch on flexible validation control.

The changes to the account validation logic, including the new skip_validate and skip_fee_transfer parameters, enhance the flexibility of the validation process. This aligns with the mentioned improvement for handling certain invoke transactions.

Could you please verify the implementation of the skip_validate logic? It's not visible in the provided code segment.

✅ Verification successful

Ohayo again, sensei! The implementation of skip_validate has been successfully verified.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of skip_validate logic

# Test: Search for skip_validate usage
rg -A 10 'skip_validate'

Length of output: 13895


🪧 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 (4)
crates/katana/pool/src/validation/mod.rs (1)

32-36: Ohayo, sensei! Great clarification on error handling.

The updated documentation for the validate method provides clear guidance on how to handle different types of errors. This aligns well with the PR objectives and improves the overall validation process.

Consider adding an example to illustrate the difference between an unexpected error and an invalid transaction. This could further enhance the clarity of the documentation.

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

66-70: Ohayo, sensei! The new error variant looks sharp!

The ClassAlreadyExists variant is a great addition to handle the case of declaring an already existing class. The error message is clear, and the use of the #[error] attribute is consistent with other variants.

Consider adding a brief explanation of when this error might occur in the documentation comment, similar to other variants. For example:

/// Error when a Declare transaction attempts to declare a class that has already been
/// declared. This ensures that each class is declared only once in the system.
#[error("Class with hash {class_hash:#x} already exists.")]
ClassAlreadyExists { class_hash: ClassHash },
crates/katana/rpc/rpc/tests/starknet.rs (1)

153-187: Ohayo again, sensei! This test function is looking sharp!

The declaring_already_existing_class test function effectively covers the scenario of attempting to declare an already existing class. It's well-structured and uses our new assert_starknet_err! macro nicely.

One small suggestion to make it even better:

Consider adding a comment explaining why we're setting the max fee manually on line 183. It might help future readers understand the reasoning behind this step.

+ // We set max fee manually to avoid performing fee estimation as we just want to test that the
+ // pool validation will reject the tx.
let result = account.declare_v2(contract.into(), compiled_hash).max_fee(Felt::ONE).send().await;

Overall, great job implementing this test, sensei! It aligns perfectly with our PR objectives.

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

141-142: Ohayo, sensei! Consider combining comment lines for clarity

The comment is split across two lines. For improved readability, consider combining them into a single line.

Apply this diff:

-            // prepare a stateful validator and run the account validation logic (ie __validate__
-            // entrypoint)
+            // prepare a stateful validator and run the account validation logic (ie __validate__ entrypoint)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fac93b0 and 337c4aa.

📒 Files selected for processing (5)
  • crates/katana/pool/src/validation/error.rs (1 hunks)
  • crates/katana/pool/src/validation/mod.rs (1 hunks)
  • crates/katana/pool/src/validation/stateful.rs (2 hunks)
  • crates/katana/rpc/rpc-types/src/error/starknet.rs (1 hunks)
  • crates/katana/rpc/rpc/tests/starknet.rs (2 hunks)
🧰 Additional context used
🔇 Additional comments (7)
crates/katana/pool/src/validation/mod.rs (1)

19-23: Ohayo, sensei! LGTM: New new method for Error struct.

The addition of the new method to the Error struct provides a clean and convenient way to create new instances. It follows good Rust practices for struct initialization.

crates/katana/rpc/rpc-types/src/error/starknet.rs (3)

Line range hint 25-25: Ohayo, sensei! New error variant looks sharp!

The addition of ClassAlreadyDeclared to the StarknetApiError enum is spot-on. It aligns perfectly with our mission to invalidate declare transactions for existing classes. The error message is clear and concise, just like a well-sharpened katana!


185-185: Ohayo again, sensei! The From implementation is as smooth as silk!

The new match arm for InvalidTransactionError::ClassAlreadyExists is a perfect addition to our error-handling dojo. It seamlessly integrates with the existing implementation, maintaining our code's harmony. Great job on keeping our error conversions consistent and clear!


Line range hint 25-25: Ohayo once more, sensei! Your code changes are as harmonious as a zen garden!

The additions to StarknetApiError and its From implementation are perfectly aligned with our mission to invalidate declare transactions for existing classes. These changes enhance our error handling capabilities while maintaining the existing structure and style. Excellent work on improving our katana's edge!

Is there anything else you'd like me to review or explain further, sensei?

Also applies to: 185-185

crates/katana/rpc/rpc/tests/starknet.rs (2)

37-42: Ohayo, sensei! LGTM for this macro implementation.

The assert_starknet_err! macro looks well-crafted and will simplify error assertions in our tests. It's a nice addition to our testing toolkit.


Line range hint 37-187: Ohayo one last time, sensei! Let's wrap this up with a bow.

The changes in this file are spot-on for our PR objectives. Here's a quick summary:

  1. The new assert_starknet_err! macro (lines 37-42) simplifies our error assertions, making our tests cleaner and more readable.
  2. The declaring_already_existing_class test function (lines 153-187) effectively covers the scenario of attempting to declare an already existing class, which is exactly what we aimed for in this PR.

These additions improve our test coverage and make our error handling more robust. Great work, sensei! This implementation aligns perfectly with the StarkNet API documentation and enhances the integrity of our transaction process.

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

105-116: Ohayo, sensei! The static class existence check looks good!

The implementation correctly checks for the existence of the class and returns the appropriate error if the class already exists.

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.

wdyt about something similar to the RPC spec with declared terminology?

crates/katana/pool/src/validation/error.rs Outdated Show resolved Hide resolved
Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 69.76%. Comparing base (fac93b0) to head (ccdb668).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/katana/pool/src/validation/mod.rs 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2564      +/-   ##
==========================================
- Coverage   69.77%   69.76%   -0.01%     
==========================================
  Files         400      400              
  Lines       51171    51183      +12     
==========================================
+ Hits        35704    35709       +5     
- Misses      15467    15474       +7     

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

@kariy kariy merged commit 97b679b into main Oct 21, 2024
14 of 15 checks passed
@kariy kariy deleted the katana/rpc-fix branch October 21, 2024 15:09
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