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-rpc): starknet rpc clean up #2205

Merged
merged 4 commits into from
Jul 23, 2024
Merged

refactor(katana-rpc): starknet rpc clean up #2205

merged 4 commits into from
Jul 23, 2024

Conversation

kariy
Copy link
Member

@kariy kariy commented Jul 23, 2024

Summary by CodeRabbit

  • New Features

    • Introduced new traits for transaction writing and tracing within the Starknet JSON-RPC API.
    • Added methods for simulating transactions and retrieving execution traces.
    • Implemented structured handling of transaction submissions for invoke, declare, and deploy account transactions.
  • Bug Fixes

    • Renamed methods for clarity and improved consistency in naming conventions.
  • Documentation

    • Added test scenarios to validate the Starknet API's transaction tracing capabilities.
  • Chores

    • Streamlined dependency management and cleaned up unused imports.

Copy link

coderabbitai bot commented Jul 23, 2024

Walkthrough

Ohayo, sensei! The recent changes enhance the Starknet API by introducing new features for transaction writing and tracing while improving the overall modularity of the code. Key modifications include the addition of new traits for transaction management and tracing, updates to existing methods for clarity, and a restructuring of the API server to support multiple functionalities concurrently, ultimately streamlining the integration process.

Changes

Files Change Summary
Cargo.toml Added default-features = false to katana-rpc-api; reformatted features list in alloy-provider.
crates/katana/rpc/rpc-api/src/starknet.rs Introduced StarknetWriteApi and StarknetTraceApi traits; renamed simulate_transactions to simulate for clarity; added trace_block method.
crates/katana/rpc/rpc/src/lib.rs Enhanced Starknet API server capabilities by integrating StarknetTraceApiServer and StarknetWriteApiServer.
crates/katana/rpc/rpc/src/starknet/mod.rs Implemented StarknetApi struct with methods for handling transactions and fee estimation, focusing on asynchronous operations and error management.
crates/katana/rpc/rpc/src/starknet/read.rs Removed several transaction-related methods; restructured API for a more streamlined execution model.
crates/katana/rpc/rpc/src/starknet/trace.rs Implemented StarknetTraceApiServer methods for transaction simulation and tracing, ensuring error handling and resource management.
crates/katana/rpc/rpc/src/starknet/write.rs Introduced StarknetWriteApiServer methods to handle invoke, declare, and deploy account transactions, validating input and managing the transaction pool.
examples/rpc/starknet/starknet_trace.hurl Created test scenarios for starknet_traceTransaction and starknet_traceBlockTransactions, validating response structures for unsupported methods.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API as StarknetAPI
    participant Trace as StarknetTraceApi
    participant Write as StarknetWriteApi

    Client->>API: Request transaction simulation
    API->>Write: add_invoke_transaction
    Write-->>API: Return transaction hash
    API->>Trace: simulate
    Trace-->>API: Return simulation results
    API-->>Client: Return final results
Loading
sequenceDiagram
    participant Client
    participant API as StarknetAPI
    participant Trace as StarknetTraceApi

    Client->>API: Request trace for transaction
    API->>Trace: trace
    Trace-->>API: Return trace results
    API-->>Client: Return trace results
Loading

Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a1c80c5 and a046c24.

Files selected for processing (8)
  • Cargo.toml (2 hunks)
  • crates/katana/rpc/rpc-api/src/starknet.rs (4 hunks)
  • crates/katana/rpc/rpc/src/lib.rs (2 hunks)
  • crates/katana/rpc/rpc/src/starknet/mod.rs (1 hunks)
  • crates/katana/rpc/rpc/src/starknet/read.rs (5 hunks)
  • crates/katana/rpc/rpc/src/starknet/trace.rs (1 hunks)
  • crates/katana/rpc/rpc/src/starknet/write.rs (1 hunks)
  • examples/rpc/starknet/starknet_trace.hurl (1 hunks)
Additional comments not posted (22)
examples/rpc/starknet/starknet_trace.hurl (2)

1-16: Ohayo, sensei! The starknet_traceTransaction section looks solid.

The POST request and assertions correctly test for the unsupported method error.


18-33: Ohayo, sensei! The starknet_traceBlockTransactions section is well-structured.

The POST request and assertions correctly test for the unsupported method error.

crates/katana/rpc/rpc/src/starknet/write.rs (3)

15-35: Ohayo, sensei! The add_invoke_transaction function is well-implemented.

The function correctly handles unsupported transaction versions and adds the transaction to the pool.


37-61: Ohayo, sensei! The add_declare_transaction function is well-structured.

The function correctly handles unsupported transaction versions, invalid contract classes, and adds the transaction to the pool.


63-85: Ohayo, sensei! The add_deploy_account_transaction function is well-implemented.

The function correctly handles unsupported transaction versions and adds the transaction to the pool.

crates/katana/rpc/rpc/src/starknet/mod.rs (5)

33-38: Ohayo, sensei! The new method is well-implemented.

The method correctly initializes a new StarknetApi instance with a sequencer and a blocking task pool.


40-47: Ohayo, sensei! The on_cpu_blocking_task method is well-structured.

The method correctly handles CPU blocking tasks using the blocking task pool.


49-56: Ohayo, sensei! The on_io_blocking_task method is well-implemented.

The method correctly handles IO blocking tasks using the Tokio task spawner.


58-98: Ohayo, sensei! The estimate_fee_with method is well-structured.

The method correctly estimates fees for a list of transactions and includes comprehensive error handling.


22-26: Ohayo, sensei! The Clone implementation is correct.

The method correctly clones the inner Arc.

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

27-27: Ohayo, sensei! Imports look good.

The addition of StarknetTraceApiServer and StarknetWriteApiServer is necessary for the new functionality.

Cargo.toml (2)

87-87: Ohayo, sensei! Dependency update looks good.

The addition of default-features = false to katana-rpc-api is correctly implemented and necessary for the new configuration.


234-234: Ohayo, sensei! Reformatting looks good.

The reformatting of alloy-provider for consistency is correctly implemented.

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

Line range hint 31-179:
Ohayo, sensei! Renaming looks good.

The method simulate_transactions has been correctly renamed to simulate for better clarity.


Line range hint 181-205:
Ohayo, sensei! New trait looks good.

The new trait StarknetWriteApi and its methods for adding transactions are correctly defined and follow best practices.


207-227: Ohayo, sensei! New trait looks good.

The new trait StarknetTraceApi and its methods for tracing transactions are correctly defined and follow best practices.

crates/katana/rpc/rpc/src/starknet/trace.rs (3)

24-30: LGTM! Unsupported method correctly returns an error.

The trace function correctly returns an error for unsupported methods.


38-38: Consider handling potential panics in on_cpu_blocking_task.

The on_cpu_blocking_task method call should handle potential panics to ensure the application remains stable.


214-220: LGTM! Unsupported method correctly returns an error.

The trace_block function correctly returns an error for unsupported methods.

crates/katana/rpc/rpc/src/starknet/read.rs (3)

Line range hint 35-37:
LGTM! Chain ID retrieval is correct.

The chain_id function correctly retrieves the chain ID from the sequencer.


Line range hint 49-51:
LGTM! Block number retrieval is correct.

The block_number function correctly retrieves the current block number from the sequencer.


Line range hint 82-87:
LGTM! Block hash and number retrieval is correct.

The block_hash_and_number function correctly retrieves the block hash and number.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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: 3

Outside diff range, codebase verification and nitpick comments (16)
crates/katana/rpc/rpc/src/starknet/trace.rs (2)

74-75: Clarify the comment on transaction validation.

The comment on transaction validation could be clarified for better understanding.

- // If the node is run with transaction validation disabled, then we should not validate
- // even if the `SKIP_VALIDATE` flag is not set.
+ // If the node is run with transaction validation disabled, we should not validate transactions
+ // even if the `SKIP_VALIDATE` flag is not set.

78-79: Clarify the comment on fee charging.

The comment on fee charging could be clarified for better understanding.

- // If the node is run with fee charge disabled, then we should disable charing fees even
- // if the `SKIP_FEE_CHARGE` flag is not set.
+ // If the node is run with fee charge disabled, we should disable charging fees
+ // even if the `SKIP_FEE_CHARGE` flag is not set.
crates/katana/rpc/rpc/src/starknet/read.rs (14)

Line range hint 40-46:
Handle potential errors in nonce_at.

The nonce_at method call should handle potential errors more gracefully.

- let nonce = this
-     .inner
-     .sequencer
-     .nonce_at(block_id, contract_address.into())
-     .map_err(StarknetApiError::from)?
-     .ok_or(StarknetApiError::ContractNotFound)?;
+ let nonce = this
+     .inner
+     .sequencer
+     .nonce_at(block_id, contract_address.into())
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .ok_or(StarknetApiError::ContractNotFound)?;

Line range hint 54-60:
Handle potential errors in transaction.

The transaction method call should handle potential errors more gracefully.

- let tx = this
-     .inner
-     .sequencer
-     .transaction(&transaction_hash)
-     .map_err(StarknetApiError::from)?
-     .ok_or(StarknetApiError::TxnHashNotFound)?;
+ let tx = this
+     .inner
+     .sequencer
+     .transaction(&transaction_hash)
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .ok_or(StarknetApiError::TxnHashNotFound)?;

Line range hint 63-69:
Handle potential errors in block_tx_count.

The block_tx_count method call should handle potential errors more gracefully.

- let count = this
-     .inner
-     .sequencer
-     .block_tx_count(block_id)
-     .map_err(StarknetApiError::from)?
-     .ok_or(StarknetApiError::BlockNotFound)?;
+ let count = this
+     .inner
+     .sequencer
+     .block_tx_count(block_id)
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .ok_or(StarknetApiError::BlockNotFound)?;

Line range hint 72-79:
Handle potential errors in class_hash_at.

The class_hash_at method call should handle potential errors more gracefully.

- let class_hash = self
-     .on_io_blocking_task(move |this| {
-         this.inner
-             .sequencer
-             .class_hash_at(block_id, contract_address.into())
-             .map_err(StarknetApiError::from)?
-             .ok_or(StarknetApiError::ContractNotFound)
-     })
-     .await?;
+ let class_hash = self
+     .on_io_blocking_task(move |this| {
+         this.inner
+             .sequencer
+             .class_hash_at(block_id, contract_address.into())
+             .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+             .ok_or(StarknetApiError::ContractNotFound)
+     })
+     .await?;

Line range hint 90-92:
Handle potential errors in latest_hash.

The latest_hash method call should handle potential errors more gracefully.

- let latest_hash = provider.latest_hash().map_err(StarknetApiError::from)?;
+ let latest_hash = provider.latest_hash().map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?;

Line range hint 98-99:
Clarify the comment on transaction filtering.

The comment on transaction filtering could be clarified for better understanding.

- // TODO(kariy): create a method that can perform this filtering for us instead
- // of doing it manually.
+ // TODO(kariy): Create a method to perform this filtering automatically.

Line range hint 126-127:
Handle potential errors in convert_block_id.

The convert_block_id method call should handle potential errors more gracefully.

- let block_num = BlockIdReader::convert_block_id(provider, block_id)
-     .map_err(StarknetApiError::from)?
-     .map(BlockHashOrNumber::Num)
-     .ok_or(StarknetApiError::BlockNotFound)?;
+ let block_num = BlockIdReader::convert_block_id(provider, block_id)
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .map(BlockHashOrNumber::Num)
+     .ok_or(StarknetApiError::BlockNotFound)?;

Line range hint 129-131:
Handle potential errors in convert_block_id.

The convert_block_id method call should handle potential errors more gracefully.

- let block_num = BlockIdReader::convert_block_id(provider, block_id)
-     .map_err(StarknetApiError::from)?
-     .map(BlockHashOrNumber::Num)
-     .ok_or(StarknetApiError::BlockNotFound)?;
+ let block_num = BlockIdReader::convert_block_id(provider, block_id)
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .map(BlockHashOrNumber::Num)
+     .ok_or(StarknetApiError::BlockNotFound)?;

Line range hint 134-136:
Handle potential errors in latest_hash.

The latest_hash method call should handle potential errors more gracefully.

- let latest_hash = provider.latest_hash().map_err(StarknetApiError::from)?;
+ let latest_hash = provider.latest_hash().map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?;

Line range hint 142-143:
Clarify the comment on transaction filtering.

The comment on transaction filtering could be clarified for better understanding.

- // TODO(kariy): create a method that can perform this filtering for us instead
- // of doing it manually.
+ // TODO(kariy): Create a method to perform this filtering automatically.

Line range hint 170-171:
Handle potential errors in convert_block_id.

The convert_block_id method call should handle potential errors more gracefully.

- let block_num = BlockIdReader::convert_block_id(provider, block_id)
-     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
-     .map(BlockHashOrNumber::Num)
-     .ok_or(StarknetApiError::BlockNotFound)?;
+ let block_num = BlockIdReader::convert_block_id(provider, block_id)
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .map(BlockHashOrNumber::Num)
+     .ok_or(StarknetApiError::BlockNotFound)?;

Line range hint 176-178:
Handle potential errors in latest_hash.

The latest_hash method call should handle potential errors more gracefully.

- let latest_hash = provider.latest_hash().map_err(StarknetApiError::from)?;
+ let latest_hash = provider.latest_hash().map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?;

Line range hint 184-185:
Clarify the comment on transaction filtering.

The comment on transaction filtering could be clarified for better understanding.

- // TODO(kariy): create a method that can perform this filtering for us instead
- // of doing it manually.
+ // TODO(kariy): Create a method to perform this filtering automatically.

Line range hint 224-225:
Handle potential errors in convert_block_id.

The convert_block_id method call should handle potential errors more gracefully.

- let

</blockquote></details>

</blockquote></details>



<!-- This is an auto-generated comment by CodeRabbit for review status -->

Comment on lines +49 to +53
// TODO: merge these into a single logic.
let server = StarknetApi::new(sequencer.clone());
methods.merge(StarknetApiServer::into_rpc(server.clone()))?;
methods.merge(StarknetWriteApiServer::into_rpc(server.clone()))?;
methods.merge(StarknetTraceApiServer::into_rpc(server))?;
Copy link

Choose a reason for hiding this comment

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

Ohayo, sensei! The changes look good, but consider merging the logic.

The updates to handle StarknetApiServer, StarknetWriteApiServer, and StarknetTraceApiServer are correct. However, the TODO comment suggests merging these into a single logic block for better maintainability.

// TODO: Merge these into a single logic.
let server = StarknetApi::new(sequencer.clone());
methods.merge(StarknetApiServer::into_rpc(server.clone()))?;
methods.merge(StarknetWriteApiServer::into_rpc(server.clone()))?;
methods.merge(StarknetTraceApiServer::into_rpc(server))?;

Comment on lines +112 to +113
// TODO: compute the state diff
let state_diff = None;
Copy link

Choose a reason for hiding this comment

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

Implement state diff computation.

The TODO comment indicates that state diff computation is missing.

Do you want me to implement the state diff computation or open a GitHub issue to track this task?

Comment on lines +91 to +95
let state = sequencer.state(&block_id).map_err(StarknetApiError::from)?;
let env = sequencer
.block_env_at(block_id)
.map_err(StarknetApiError::from)?
.ok_or(StarknetApiError::BlockNotFound)?;
Copy link

Choose a reason for hiding this comment

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

Handle potential errors in block_env_at.

The block_env_at method call should handle potential errors more gracefully.

- let env = sequencer
-     .block_env_at(block_id)
-     .map_err(StarknetApiError::from)?
-     .ok_or(StarknetApiError::BlockNotFound)?;
+ let env = sequencer
+     .block_env_at(block_id)
+     .map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
+     .ok_or(StarknetApiError::BlockNotFound)?;
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
let state = sequencer.state(&block_id).map_err(StarknetApiError::from)?;
let env = sequencer
.block_env_at(block_id)
.map_err(StarknetApiError::from)?
.ok_or(StarknetApiError::BlockNotFound)?;
let state = sequencer.state(&block_id).map_err(StarknetApiError::from)?;
let env = sequencer
.block_env_at(block_id)
.map_err(|e| StarknetApiError::UnexpectedError { reason: e.to_string() })?
.ok_or(StarknetApiError::BlockNotFound)?;

Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 31.30990% with 215 lines in your changes missing coverage. Please review.

Project coverage is 68.18%. Comparing base (a1c80c5) to head (a046c24).

Files Patch % Lines
crates/katana/rpc/rpc/src/starknet/trace.rs 0.00% 190 Missing ⚠️
crates/katana/rpc/rpc/src/starknet/write.rs 66.66% 20 Missing ⚠️
crates/katana/rpc/rpc/src/starknet/mod.rs 91.22% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2205      +/-   ##
==========================================
- Coverage   68.20%   68.18%   -0.03%     
==========================================
  Files         336      339       +3     
  Lines       44056    44066      +10     
==========================================
- Hits        30050    30047       -3     
- Misses      14006    14019      +13     

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

@kariy kariy merged commit c87358e into main Jul 23, 2024
13 of 15 checks passed
@kariy kariy deleted the refactor-rpc-api branch July 23, 2024 20:48
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