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

feature: add properties for JSON REST responses #204

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jns-ps
Copy link
Contributor

@jns-ps jns-ps commented Jan 13, 2025

Adds properties to SignedData and Commitment in JSON, which simplifies client-side REST API interaction.

Merge after #198

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced CommitmentResponse struct for more structured API responses
  • Refactor

    • Updated SignedData struct to use named fields for improved code clarity
    • Modified data signature handling in account operations
  • Documentation

    • Updated OpenAPI schema to include new CommitmentResponse type
  • Tests

    • Adjusted test assertions to align with new struct field access

Copy link
Contributor

coderabbitai bot commented Jan 13, 2025

Walkthrough

The pull request introduces structural changes to the SignedData struct in the account.rs file, transforming it from a tuple struct to a regular struct with named fields. This modification affects how signed data is processed and validated across multiple components. In the webserver.rs, a new CommitmentResponse struct is added to provide a more structured API response. The test file tests.rs is updated to accommodate these changes, specifically adjusting how signed data is accessed and validated.

Changes

File Change Summary
crates/common/src/account.rs - Converted SignedData from tuple struct to named struct
- Updated process_operation to use named field syntax
- Enhanced validate_operation to include SetData case
crates/node_types/prover/src/webserver.rs - Added new CommitmentResponse struct
- Modified get_commitment to return CommitmentResponse
- Updated ApiDoc to include new response type
crates/tree/src/tests.rs - Updated assertions in test_data_ops to use new struct field access

Sequence Diagram

sequenceDiagram
    participant Client
    participant WebServer
    participant Prover
    
    Client->>WebServer: Request Commitment
    WebServer->>Prover: Retrieve Commitment
    Prover-->>WebServer: Return Commitment
    WebServer->>WebServer: Wrap in CommitmentResponse
    WebServer-->>Client: Return CommitmentResponse
Loading

Possibly related PRs

Suggested reviewers

  • distractedm1nd
  • sebasti810

Poem

🐰 A Rabbit's Ode to Struct Refactoring 🐰

Tuple fields, once compact and tight,
Now named with clarity so bright
Commitment wrapped in response new
Code dancing with a structure true
Refactoring, our rabbit's delight! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 75cc40b and 6aef596.

📒 Files selected for processing (3)
  • crates/common/src/account.rs (2 hunks)
  • crates/node_types/prover/src/webserver.rs (2 hunks)
  • crates/tree/src/tests.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • crates/node_types/prover/src/webserver.rs
  • crates/tree/src/tests.rs
  • crates/common/src/account.rs
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: unused dependencies
  • GitHub Check: build-and-push-image

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.

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
Contributor

@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: 2

🧹 Nitpick comments (3)
crates/common/src/operation.rs (1)

32-37: Consider adding documentation for data size limits.

The SetData operation looks good, but it would be helpful to document any size limits for the data field, similar to how they will be enforced in validate_basic.

     /// Set arbitrary signed data to an existing account. Replaces all existing data.
+    /// Note: The data size must be less than the maximum allowed size (TBD).
     SetData {
         #[serde(with = "raw_or_b64")]
         data: Vec<u8>,
         data_signature: SignatureBundle,
     },
crates/node_types/prover/src/webserver.rs (1)

79-82: Add OpenAPI documentation for the response struct.

The CommitmentResponse struct should include OpenAPI documentation to describe its purpose and usage.

 #[derive(Serialize, Deserialize, ToSchema)]
+/// Response containing the current commitment (tree root) of the IndexedMerkleTree.
 pub struct CommitmentResponse {
+    /// The commitment value representing the tree root.
     commitment: Digest,
 }
crates/common/src/transaction_builder.rs (1)

400-410: Consider extracting common logic between add_ and set_ methods.**

The implementation of set_randomly_signed_data follows the same pattern as add_randomly_signed_data. Consider extracting the common logic into a private helper method to reduce code duplication.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 369bc66 and 75cc40b.

📒 Files selected for processing (7)
  • crates/common/src/account.rs (3 hunks)
  • crates/common/src/operation.rs (3 hunks)
  • crates/common/src/transaction_builder.rs (1 hunks)
  • crates/node_types/prover/src/prover/mod.rs (1 hunks)
  • crates/node_types/prover/src/webserver.rs (2 hunks)
  • crates/tree/src/snarkable_tree.rs (1 hunks)
  • crates/tree/src/tests.rs (3 hunks)
🧰 Additional context used
📓 Learnings (2)
crates/common/src/operation.rs (1)
Learnt from: distractedm1nd
PR: deltadevsde/prism#141
File: crates/common/src/operation.rs:311-313
Timestamp: 2024-11-12T11:47:59.930Z
Learning: When implementing `AddSignedData` in `crates/common/src/operation.rs`, ensure that signatures cover the entire operation (including metadata like account ID) to prevent replay attacks or unauthorized data modifications. Including a `value_signature: Option<(VerifyingKey, Signature)>` allows for externally signed data while ensuring proper validation.
crates/common/src/account.rs (1)
Learnt from: distractedm1nd
PR: deltadevsde/prism#141
File: crates/common/src/operation.rs:311-313
Timestamp: 2024-11-12T11:47:59.930Z
Learning: When implementing `AddSignedData` in `crates/common/src/operation.rs`, ensure that signatures cover the entire operation (including metadata like account ID) to prevent replay attacks or unauthorized data modifications. Including a `value_signature: Option<(VerifyingKey, Signature)>` allows for externally signed data while ensuring proper validation.
🪛 GitHub Check: unit-test
crates/tree/src/tests.rs

[failure] 202-202:
no field 1 on type SignedData


[failure] 203-203:
no field 1 on type SignedData


[failure] 224-224:
no field 1 on type SignedData

🪛 GitHub Check: clippy
crates/tree/src/tests.rs

[failure] 202-202:
no field 1 on type prism_common::account::SignedData


[failure] 203-203:
no field 1 on type prism_common::account::SignedData


[failure] 224-224:
no field 1 on type prism_common::account::SignedData

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: unused dependencies
  • GitHub Check: integration-test
  • GitHub Check: build-and-push-image
🔇 Additional comments (10)
crates/common/src/operation.rs (2)

78-78: LGTM!

The get_public_key method correctly handles both data operations consistently.


Line range hint 103-108: Define a proper maximum data size.

The TODO comment indicates that a proper maximum data size needs to be determined. This is important for preventing potential DoS attacks through large data submissions.

Consider factors like:

  • Network bandwidth limitations
  • Storage capacity
  • Processing overhead
  • Typical use case requirements
crates/common/src/account.rs (3)

12-16: LGTM! Improved struct readability.

The change from tuple struct to named fields makes the code more maintainable and self-documenting.


138-141: LGTM! Consistent signature validation.

The signature validation for SetData follows the same secure pattern as AddData, ensuring proper verification of externally signed data.


181-188: LGTM! Clear data replacement logic.

The implementation correctly replaces all existing data with the new entry.

crates/node_types/prover/src/webserver.rs (1)

240-240: LGTM! Consistent response format.

The endpoint now returns a properly structured response object.

crates/tree/src/snarkable_tree.rs (1)

92-95: LGTM! Consistent transaction processing.

The SetData operation is correctly integrated into the transaction processing flow, using the same update mechanism as other account modifications.

crates/common/src/transaction_builder.rs (1)

423-435: ⚠️ Potential issue

Replace unwrap() with proper error handling.

The expect call on line 408 could panic if key creation fails. Consider propagating the error using the Result type instead.

Apply this diff to improve error handling:

-    let value_signing_key =
-        SigningKey::new_with_algorithm(algorithm).expect("Failed to create value signing key");
+    let value_signing_key = SigningKey::new_with_algorithm(algorithm)
+        .map_err(|e| anyhow::anyhow!("Failed to create value signing key: {}", e))?;

Likely invalid or redundant comment.

crates/node_types/prover/src/prover/mod.rs (1)

494-497: LGTM!

The addition of SetData to the match arm follows the existing pattern and maintains consistent validation logic with other operations.

crates/tree/src/tests.rs (1)

162-245: LGTM! Comprehensive test coverage.

The test function thoroughly covers:

  • Data addition with different signing methods
  • Data content verification
  • SetData replacement behavior
  • Invalid signature handling
🧰 Tools
🪛 GitHub Check: unit-test

[failure] 202-202:
no field 1 on type SignedData


[failure] 203-203:
no field 1 on type SignedData


[failure] 224-224:
no field 1 on type SignedData

🪛 GitHub Check: clippy

[failure] 202-202:
no field 1 on type prism_common::account::SignedData


[failure] 203-203:
no field 1 on type prism_common::account::SignedData


[failure] 224-224:
no field 1 on type prism_common::account::SignedData

crates/tree/src/tests.rs Outdated Show resolved Hide resolved
crates/tree/src/tests.rs Outdated Show resolved Hide resolved
@jns-ps jns-ps force-pushed the unify-json-responses branch 2 times, most recently from 25a2a9c to 6a5f953 Compare January 13, 2025 16:15
@jns-ps jns-ps force-pushed the unify-json-responses branch from 6a5f953 to 6aef596 Compare January 14, 2025 13:30
@jns-ps jns-ps requested a review from distractedm1nd January 15, 2025 10:24
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