-
Notifications
You must be signed in to change notification settings - Fork 9
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(tree): Return membership-proof when getting tree values #136
Conversation
Before this, only a non-membership-proof was returned when the value could not be found in the tree. Now, a proof is returned alongside the value when it can be found.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThe changes introduce a new type, Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant WebServer
participant Sequencer
participant Tree
User->>WebServer: Request hashchain
WebServer->>Sequencer: Call get_hashchain
Sequencer->>Tree: Call get with key
Tree-->>Sequencer: Return HashchainResponse
Sequencer-->>WebServer: Return HashchainResponse
WebServer-->>User: Respond with hashchain
Possibly related PRs
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 8
🧹 Outside diff range and nitpick comments (5)
crates/prism/src/node_types/sequencer.rs (2)
Line range hint
365-372
: LGTM: Improvedget_hashchain
method signature and implementation.The change to return
Result<HashchainResponse>
simplifies the method and aligns with the newHashchainResponse
type. This improvement enhances code clarity and maintainability.Consider adding a brief comment explaining the
HashchainResponse
type for better documentation:pub async fn get_hashchain(&self, id: &String) -> Result<HashchainResponse> { + // Returns a HashchainResponse which can be either Found(Hashchain, Proof) or NotFound(NonMembershipProof) let tree = self.tree.read().await; let hashed_id = Digest::hash(id); let key_hash = KeyHash::with::<Hasher>(hashed_id); tree.get(key_hash) }
392-398
: LGTM: Enhanced error handling invalidate_and_queue_update
.The use of pattern matching with
HashchainResponse::Found
and thebail!
macro improves error handling and code robustness. This change aligns well with the PR objectives.Consider using
anyhow::ensure!
for a more idiomatic error handling:- let HashchainResponse::Found(mut hc, _) = hcr else { - bail!("Hashchain not found for id: {}", incoming_operation.id()) - }; + let (mut hc, _) = hcr.found().context(format!("Hashchain not found for id: {}", incoming_operation.id()))?;This change provides more context in the error message and uses the
?
operator for concise error propagation.crates/prism/src/webserver.rs (3)
56-57
: UpdateUserKeyResponse
documentation to reflect new structureThe
UserKeyResponse
struct has been modified to include an optionalhashchain
:pub struct UserKeyResponse { pub hashchain: Option<Hashchain>, pub proof: SparseMerkleProof<Hasher>, }Ensure that the API documentation and any related comments are updated to reflect that
hashchain
can beNone
. This helps clients understand the possible values and handle them appropriately.
162-178
: Consider unifying response status codes for better client experienceCurrently, the
Found
case returnsStatusCode::OK
, while theNotFound
case returns an error status code. Since both cases return aUserKeyResponse
, consider always returningStatusCode::OK
and using thehashchain
field beingSome
orNone
to indicate presence or absence. This approach can simplify client-side error handling.Apply this diff if you choose to return
StatusCode::OK
for both cases:HashchainResponse::NotFound(non_membership_proof) => ( - StatusCode::NOT_FOUND, + StatusCode::OK, Json(UserKeyResponse { hashchain: None, proof: non_membership_proof.proof, }), ) .into_response(),
149-159
: Handle potential panics when unwrapping errorsIn the error handling block,
get_hashchain_result.unwrap_err()
is used to retrieve the error message. Ifget_hashchain_result
isOk
, callingunwrap_err()
will cause a panic. However, since you're already in theErr
case, this is safe. Ensure that this pattern is consistently safe in all similar contexts.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
- crates/common/src/test_utils.rs (2 hunks)
- crates/common/src/tree.rs (12 hunks)
- crates/prism/src/node_types/sequencer.rs (3 hunks)
- crates/prism/src/webserver.rs (3 hunks)
🧰 Additional context used
🔇 Additional comments (8)
crates/prism/src/node_types/sequencer.rs (3)
1-1
: LGTM: Import changes enhance error handling and response management.The addition of
bail
andHashchainResponse
to the imports suggests improvements in error handling and hashchain response management. These changes align well with the PR objectives.Also applies to: 5-7
Line range hint
1-600
: Summary: Improved error handling and response managementThe changes in this file enhance error handling and introduce a new
HashchainResponse
type, improving the overall robustness and clarity of the code. The modifications align well with the PR objectives of including proof in the WS response.Key improvements:
- Addition of
bail!
for more concise error handling.- Introduction of
HashchainResponse
for better management of hashchain responses.- Updated method signatures and implementations to use the new
HashchainResponse
type.Suggestions for further improvement:
- Add brief comments explaining the
HashchainResponse
type for better documentation.- Consider using
anyhow::ensure!
for more idiomatic error handling in some cases.- Review and update test cases to cover the new behavior and error cases.
Overall, these changes represent a positive step towards more robust and maintainable code.
Line range hint
409-600
: Consider updating test cases to reflect recent changes.While the main code has been updated to use
HashchainResponse
and improved error handling, the test cases haven't been modified. It's important to ensure that the tests cover the new behavior and error cases.To verify the test coverage, you can run the following script:
Please review the output and consider adding or updating tests to cover the new
HashchainResponse
type and error handling mechanisms.crates/prism/src/webserver.rs (1)
162-169
: Ensure consistent handling of optionalhashchain
in responsesIn the
HashchainResponse::Found
case,hashchain
is wrapped inSome
, and in theNotFound
case, it'sNone
. Ensure that any clients consuming this API properly handle the optionalhashchain
field. This change might require updates to client-side code and documentation to preventNone
value misinterpretation.Run the following script to identify usages of
UserKeyResponse
in the codebase:✅ Verification successful
Consistent handling of
hashchain
inUserKeyResponse
verifiedAll instances of
UserKeyResponse
correctly handle the optionalhashchain
field asSome
orNone
.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Find all usages of UserKeyResponse to verify proper handling of optional hashchain # Expected: All usages handle hashchain being None or Some. rg --type rust 'UserKeyResponse' -A 5 -g '!webserver.rs'Length of output: 56
Script:
#!/bin/bash # Description: Verify that all usages of UserKeyResponse handle the optional hashchain field appropriately. # Use ast-grep to find all instances of UserKeyResponse and inspect the hashchain field handling ast-grep --lang rust --pattern 'UserKeyResponse { hashchain: $_, proof: $_ }' .Length of output: 630
crates/common/src/test_utils.rs (1)
4-4
: ImportingHashchainResponse
is appropriateThe addition of
HashchainResponse
to the import statement is necessary for handling the tree responses effectively.crates/common/src/tree.rs (3)
191-204
: Code looks good forMembershipProof
The implementation of the
MembershipProof
struct and itsverify
method appears correct and follows best practices.
692-696
: No issues found with non-membership proof verificationThe test for getting a non-existing key correctly handles the
NotFound
response and verifies the non-membership proof.
828-835
: Tests look good for verifying multiple insertsThe test
test_batch_writing
appropriately checks the insertion and retrieval of multiple accounts using the updatedHashchainResponse
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful, king 👑
When no value is found within /get-hashchain, there is still a valid non-membership-proof response. In order not to confuse clients that may think a non-success code means a failed request, we switch to a success code here. Clients have to parse the hashchain property to see if there is a valid value.
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (8)
crates/prism/src/webserver.rs (1)
149-159
: LGTM: Improved error handling, with a minor suggestionThe error handling has been improved:
- The use of
let Ok(...) = ... else
syntax simplifies the logic.- The status code change to
INTERNAL_SERVER_ERROR
is appropriate for server-side errors.However, consider making the error message more specific:
- "Couldn't get hashchain: {}", + "Failed to retrieve hashchain or non-inclusion proof: {}",This change would provide more context about the nature of the error.
crates/prism/src/node_types/sequencer.rs (3)
Line range hint
366-373
: LGTM: Improvedget_hashchain
method with simplified return type.The updated
get_hashchain
method now returnsResult<HashchainResponse>
, which aligns with the newHashchainResponse
type and simplifies error handling. The implementation is more concise and directly returns the result oftree.get(key_hash)
.Consider adding a brief comment explaining the purpose of
KeyHash::with::<Hasher>(hashed_id)
for better code documentation:pub async fn get_hashchain(&self, id: &String) -> Result<HashchainResponse> { let tree = self.tree.read().await; let hashed_id = Digest::hash(id); + // Create a KeyHash using the custom Hasher for consistent key representation let key_hash = KeyHash::with::<Hasher>(hashed_id); tree.get(key_hash) }
393-399
: LGTM: Improved error handling and pattern matching invalidate_and_queue_update
.The updated
validate_and_queue_update
method now uses pattern matching withHashchainResponse::Found
, which is more idiomatic and aligns with the newHashchainResponse
type. The error handling has been improved usingbail!
, enhancing readability and consistency with the added import.Consider using a more descriptive variable name for the hashchain response:
-let hc_response = self.get_hashchain(&incoming_operation.id()).await?; +let hashchain_response = self.get_hashchain(&incoming_operation.id()).await?; -let Found(mut hc, _) = hc_response else { +let Found(mut hashchain, _) = hashchain_response else { bail!("Hashchain not found for id: {}", incoming_operation.id()) }; -hc.perform_operation(incoming_operation.clone())?; +hashchain.perform_operation(incoming_operation.clone())?;This change improves code readability and aligns with the previous review comment suggesting the use of
hashchain_response
instead ofhcr
.
Line range hint
412-599
: Consider adding test cases for new error handling and pattern matching.While the existing test cases cover the main functionality of the
Sequencer
struct, it would be beneficial to add new test cases that specifically target the updated error handling and pattern matching in thevalidate_and_queue_update
method. This will ensure that the new changes are thoroughly tested.Consider adding the following test cases:
- Test the case where
get_hashchain
returns aNotFound
response.- Test the case where
perform_operation
fails and triggers thebail!
error.Example:
#[tokio::test] async fn test_validate_and_queue_update_hashchain_not_found() { let sequencer = create_test_sequencer().await; // Create an operation that requires an existing hashchain let signing_key = create_mock_signing_key(); let add_key_op = Operation::new_add_key( "non_existent@example.com".to_string(), signing_key.verifying_key(), &signing_key, 0, ).unwrap(); // Attempt to validate and queue the update let result = sequencer.clone().validate_and_queue_update(&add_key_op).await; // Assert that the operation failed due to hashchain not found assert!(result.is_err()); assert!(result.unwrap_err().to_string().contains("Hashchain not found")); }This will help ensure that the new error handling and pattern matching logic is working as expected.
crates/common/src/tree.rs (4)
24-25
: Consider using explicit imports instead of wildcard importWhile using
use HashchainResponse::*;
can make the code more concise, it's generally considered better practice to use explicit imports. This helps avoid potential naming conflicts and improves code readability by making it clear which items are being used from theHashchainResponse
enum.Consider replacing the wildcard import with explicit imports of the enum variants you're using, like this:
-use HashchainResponse::*; +use HashchainResponse::{Found, NotFound};This change will make the code more explicit about which parts of
HashchainResponse
are being used.
289-297
: LGTM: Well-designedHashchainResponse
enum with a minor suggestionThe new
HashchainResponse
enum is well-designed and clearly represents the possible outcomes of a hashchain lookup. The use ofMembershipProof
andNonMembershipProof
in the variants provides additional verification capabilities, which is a good practice.Consider adding a brief doc comment for the enum itself, explaining its purpose and when it's used. For example:
/// Represents the result of a hashchain lookup operation. /// This enum is returned by the `get` method of the `SnarkableTree` trait. #[derive(Debug)] pub enum HashchainResponse { // ... existing variants ... }This additional documentation will provide more context for developers using this enum.
382-394
: LGTM: Improved error handling with a minor optimization suggestionThe changes to the
process_operation
method improve error handling by using pattern matching onHashchainResponse
. This approach is more robust and aligns well with the newHashchainResponse
enum.Consider avoiding the unnecessary
clone()
of theoperation
:match self.get(key_hash)? { HashchainResponse::Found(mut current_chain, _) => { - let new_entry = current_chain.perform_operation(operation.clone())?; + let new_entry = current_chain.perform_operation(operation)?; debug!("updating hashchain for user id {}", id.clone()); - let proof = self.update(key_hash, new_entry.clone())?; + let proof = self.update(key_hash, new_entry)?; Ok(Proof::Update(proof)) } HashchainResponse::NotFound(_) => { bail!("Failed to get hashchain for ID {}", id) } }This change assumes that
perform_operation
andupdate
can work with references toOperation
andHashchainEntry
respectively. If they can't, please disregard this suggestion.
553-576
: LGTM with optimization suggestionThe changes to the
get
method look good. The new return typeHashchainResponse
simplifies the method signature and aligns well with the newHashchainResponse
enum. The logic for creatingMembershipProof
andNonMembershipProof
is correct.To optimize performance, consider retrieving the current root hash once at the beginning of the method instead of calling
self.get_current_root()?
multiple times:fn get(&self, key: KeyHash) -> Result<HashchainResponse> { + let root = self.get_current_root()?.into(); let (value, proof) = self.jmt.get_with_proof(key, self.epoch)?; match value { Some(serialized_value) => { let deserialized_value = Self::deserialize_value(&serialized_value)?; let membership_proof = MembershipProof { - root: self.get_current_root()?.into(), + root: root.clone(), proof, key, value: deserialized_value.clone(), }; Ok(HashchainResponse::Found( deserialized_value, membership_proof, )) } None => { let non_membership_proof = NonMembershipProof { - root: self.get_current_root()?.into(), + root, proof, key, }; Ok(HashchainResponse::NotFound(non_membership_proof)) } } }This change reduces the number of calls to
get_current_root()
from two to one, potentially improving performance, especially ifget_current_root()
is an expensive operation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
- crates/common/src/test_utils.rs (2 hunks)
- crates/common/src/tree.rs (13 hunks)
- crates/prism/src/node_types/sequencer.rs (3 hunks)
- crates/prism/src/webserver.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- crates/common/src/test_utils.rs
🧰 Additional context used
🔇 Additional comments (7)
crates/prism/src/webserver.rs (4)
14-19
: LGTM: Import changes reflect updated dependenciesThe new import of
SparseMerkleProof
and the updated imports fromprism_common
align with the changes in the code structure and types being used.
56-57
: LGTM:UserKeyResponse
struct updated to handle not found casesThe changes to the
UserKeyResponse
struct are well-considered:
Option<Hashchain>
allows for explicit representation of cases where a hashchain is not found.SparseMerkleProof<Hasher>
aligns with the new proof type being used.These changes improve the flexibility and expressiveness of the response structure.
161-176
: LGTM: Improved response handling for found and not found casesThe new structure for handling responses is well-implemented:
- Explicit handling of
HashchainResponse::Found
andHashchainResponse::NotFound
cases improves clarity.- Consistent use of
StatusCode::OK
for both cases is appropriate, as both return valid responses (either with a hashchain or a non-membership proof).- The response structure is consistent, using
Some(hashchain)
orNone
as appropriate.This implementation aligns well with the previous discussions and improves the overall response handling.
Line range hint
1-200
: Overall LGTM: Improved error handling and response structureThe changes in this file significantly improve the handling of hashchain retrieval and proof management:
- The
UserKeyResponse
struct now accommodates both found and not found scenarios.- Error handling in the
get_hashchain
function is more robust and uses appropriate status codes.- The response structure for both found and not found cases is consistent and informative.
- The changes align well with previous review comments and suggestions.
These improvements enhance the clarity, flexibility, and reliability of the webserver's hashchain-related operations.
crates/prism/src/node_types/sequencer.rs (2)
1-1
: LGTM: Import updates enhance error handling and align with tree structure changes.The addition of
bail
fromanyhow
and the updated imports fromprism_common::tree
reflect improvements in error handling and changes to the tree-related structures. These modifications are consistent with the overall changes in the file.Also applies to: 5-8
Line range hint
1-599
: Overall improvements in error handling and type consistency.The changes in this file primarily focus on enhancing error handling and aligning the code with the new
HashchainResponse
type. These modifications improve the overall robustness and readability of theSequencer
implementation. The core functionality remains intact while benefiting from more idiomatic Rust patterns and clearer error management.Key improvements:
- Updated imports to include
bail
and new tree-related types.- Simplified
get_hashchain
method with a more consistent return type.- Enhanced
validate_and_queue_update
with better pattern matching and error handling.These changes contribute to a more maintainable and error-resistant codebase. Consider addressing the minor suggestions in the previous comments and adding the proposed test cases to further strengthen the implementation.
crates/common/src/tree.rs (1)
192-206
: LGTM: Well-designedMembershipProof
struct and implementationThe new
MembershipProof
struct and its implementation look well-designed and correctly implemented. The struct contains all necessary fields for a membership proof, and theverify
method properly serializes the value and verifies the existence of the proof.
91bcfc6
to
c908596
Compare
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (5)
crates/prism/src/webserver.rs (2)
56-57
: LGTM:UserKeyResponse
struct changes are appropriate.The changes to the
UserKeyResponse
struct, including makinghashchain
optional and updating theproof
type, align with the new functionality.Consider updating the struct's documentation to reflect these changes, especially explaining when
hashchain
might beNone
and the implications of usingSparseMerkleProof<Hasher>
.
Line range hint
1-200
: Overall improvements to webserver functionality and error handling.The changes in this file significantly enhance the webserver's functionality and error handling:
- The
UserKeyResponse
struct now accommodates both found and not found scenarios.- Error handling in the
get_hashchain
function is more robust and informative.- The response structure for the
get_hashchain
endpoint is now consistent, returningStatusCode::OK
for both found and not found cases, which aligns with RESTful best practices.These improvements contribute to a more reliable and maintainable codebase. The changes effectively address the concerns raised in previous reviews and align with the stated PR objectives.
Consider adding unit tests to verify the new behavior of the
get_hashchain
function, especially focusing on the differentHashchainResponse
scenarios and error handling.crates/prism/src/node_types/sequencer.rs (3)
Line range hint
366-373
: LGTM: Simplifiedget_hashchain
method improves API consistency.The return type change to
Result<HashchainResponse>
simplifies the API and likely improves error handling. The implementation is now more concise, delegating the creation ofHashchainResponse
to the tree implementation.Consider adding a brief comment explaining the purpose of
KeyHash::with::<Hasher>(hashed_id)
for better code documentation.
393-399
: LGTM: Improved error handling invalidate_and_queue_update
.The use of pattern matching with
HashchainResponse::Found
and thebail!
macro enhances error handling and makes the code more robust. This change aligns well with Rust best practices.Consider using a more descriptive variable name than
hc
for better readability, e.g.,hashchain
.
Impact Detected in
webserver.rs
Due toget_hashchain
ChangesThe
get_hashchain
method is utilized incrates/prism/src/webserver.rs
for defining routes and handling asynchronous requests. Changes to its return type or behavior insequencer.rs
may affect the functionality within the webserver.
- Affected File:
crates/prism/src/webserver.rs
- Route definitions:
/get-hashchain
- Asynchronous calls handling
get_hashchain
resultsPlease review and update the
webserver.rs
implementations to ensure compatibility with the updatedHashchainResponse
.🔗 Analysis chain
Line range hint
366-399
: Verify impact ofHashchainResponse
changes on the wider system.The introduction of
HashchainResponse
and the related changes in error handling improve the robustness of theSequencer
. However, it's important to ensure that these changes are compatible with other parts of the system that interact with theSequencer
.Run the following script to check for potential impacts:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check for uses of old return types or methods that might be affected by the HashchainResponse changes. # Test: Search for potential uses of old return types or affected methods rg --type rust 'Result<Result<Hashchain, NonMembershipProof>>' -g '!crates/prism/src/node_types/sequencer.rs' rg --type rust 'get_hashchain' -g '!crates/prism/src/node_types/sequencer.rs'Length of output: 691
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
- crates/common/src/test_utils.rs (2 hunks)
- crates/common/src/tree.rs (13 hunks)
- crates/prism/src/node_types/sequencer.rs (3 hunks)
- crates/prism/src/webserver.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- crates/common/src/test_utils.rs
🧰 Additional context used
🔇 Additional comments (5)
crates/prism/src/webserver.rs (3)
14-19
: LGTM: Import changes are appropriate.The new imports for
SparseMerkleProof
andprism_common
types are correctly added and align with the changes made in the file.
149-159
: LGTM: Improved error handling inget_hashchain
.The changes to error handling are well-implemented:
- The use of
let Ok(...) = ... else
syntax improves readability.- The error message is more descriptive and accurate.
- Using
StatusCode::INTERNAL_SERVER_ERROR
is appropriate for server-side errors.These changes effectively address the concerns raised in previous review comments.
161-176
: LGTM: Consistent handling ofHashchainResponse
.The changes to handle
HashchainResponse
are well-implemented:
- Both
Found
andNotFound
cases returnStatusCode::OK
, addressing the previous discussion about returning success status codes.- The
NotFound
case correctly returns aUserKeyResponse
withhashchain: None
.- The implementation maintains a consistent response structure while distinguishing between found and not found cases.
These changes improve the API's consistency and align with best practices for RESTful endpoints.
crates/prism/src/node_types/sequencer.rs (2)
1-1
: LGTM: Import updates enhance error handling and align with module changes.The addition of
bail
to theanyhow
import and the updates toprism_common::tree
imports, particularlyHashchainResponse
, reflect improvements in error handling and alignment with changes in the tree module.Also applies to: 5-7
393-393
: Past suggestion implemented: Variable naming improved.The code now uses
hc_response
instead ofhcr
, as suggested in a previous review. This change enhances code readability.
That way if-let constructs for ensuring some HashchainResponse value are way more readable
c908596
to
ae4cce0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (7)
crates/prism/src/node_types/sequencer.rs (2)
Line range hint
366-373
: LGTM: Simplifiedget_hashchain
method.The updated method signature and implementation improve clarity by using
HashchainResponse
. This change aligns well with the new imports and simplifies error handling.Consider adding a brief comment explaining the
HashchainResponse
type for better documentation:pub async fn get_hashchain(&self, id: &String) -> Result<HashchainResponse> { + // HashchainResponse can be Found(Hashchain, MembershipProof) or NotFound(NonMembershipProof) let tree = self.tree.read().await; let hashed_id = Digest::hash(id); let key_hash = KeyHash::with::<Hasher>(hashed_id); tree.get(key_hash) }
393-399
: LGTM: Improved error handling invalidate_and_queue_update
.The use of pattern matching with
HashchainResponse::Found
and thebail!
macro enhances error handling and aligns with Rust best practices.Consider using a more descriptive error message:
- bail!("Hashchain not found for id: {}", incoming_operation.id()) + bail!("Cannot perform operation: Hashchain not found for id '{}'", incoming_operation.id())crates/common/src/tree.rs (5)
24-25
: Consider using explicit imports instead of wildcard importWhile using
use HashchainResponse::*;
can make the code more concise, it's generally considered better practice to use explicit imports. This helps prevent potential naming conflicts and makes the code more readable by clearly showing which variants are being used.Consider changing this to explicitly import the variants you need, like this:
-use HashchainResponse::*; +use HashchainResponse::{Found, NotFound};
192-206
: LGTM! Consider adding documentationThe
MembershipProof
struct and its implementation look good. Theverify
method correctly serializes the value and verifies its existence using the proof.Consider adding documentation comments to the struct and its method to improve code readability and maintainability. For example:
/// Represents a proof of membership for a hashchain in the Merkle tree #[derive(Debug, Clone, Serialize, Deserialize)] pub struct MembershipProof { // ... fields ... } impl MembershipProof { /// Verifies the membership proof /// /// # Returns /// - `Ok(())` if the proof is valid /// - `Err(_)` if the proof is invalid or an error occurred during verification pub fn verify(&self) -> Result<()> { // ... implementation ... } }
289-297
: LGTM! Consider enhancing documentationThe
HashchainResponse
enum is well-designed and provides a clear way to represent the result of fetching tree values. The variants and their associated data are appropriate for the use case.To further improve the documentation, consider adding a brief description of the enum itself, not just its variants. For example:
/// Represents the response when fetching tree values, encapsulating both successful retrievals and proofs of non-existence #[derive(Debug)] pub enum HashchainResponse { // ... existing variants and comments ... }
382-394
: Improve error handling and consider optimizationThe updated
process_operation
method correctly uses pattern matching with the newHashchainResponse
enum. However, there are a couple of points that could be improved:
- The error message in the
NotFound
case could be more descriptive.- The
clone()
ofoperation
on line 384 might be unnecessary ifperform_operation
can accept a reference.Consider applying these changes:
match self.get(key_hash)? { Found(mut current_chain, _) => { - let new_entry = current_chain.perform_operation(operation.clone())?; + let new_entry = current_chain.perform_operation(operation)?; debug!("updating hashchain for user id {}", id.clone()); let proof = self.update(key_hash, new_entry.clone())?; Ok(Proof::Update(proof)) } NotFound(_) => { - bail!("Failed to get hashchain for ID {}", id) + bail!("Hashchain not found for ID {}. Cannot perform operation.", id) } }Also, ensure that the
perform_operation
method ofHashchain
accepts a reference toOperation
if possible, to avoid unnecessary cloning.
553-572
: LGTM! Consider minor optimizationThe updated
get
method implementation looks good. It correctly handles both cases when retrieving the value and properly constructs theMembershipProof
andNonMembershipProof
objects. The method now returns the appropriate variants ofHashchainResponse
.Consider a minor optimization to avoid cloning the deserialized value:
match value { Some(serialized_value) => { let deserialized_value = Self::deserialize_value(&serialized_value)?; let membership_proof = MembershipProof { root, proof, key, - value: deserialized_value.clone(), + value: deserialized_value, }; - Ok(Found(deserialized_value, membership_proof)) + Ok(Found(membership_proof.value, membership_proof)) } None => { let non_membership_proof = NonMembershipProof { root, proof, key }; Ok(NotFound(non_membership_proof)) } }This change avoids an unnecessary clone of the deserialized value.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
- crates/common/src/test_utils.rs (2 hunks)
- crates/common/src/tree.rs (13 hunks)
- crates/prism/src/node_types/sequencer.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- crates/common/src/test_utils.rs
🧰 Additional context used
🔇 Additional comments (3)
crates/prism/src/node_types/sequencer.rs (2)
1-1
: LGTM: Import changes enhance error handling and response management.The addition of
bail
and the updated imports fromprism_common::tree
align with the changes in error handling and hashchain response management throughout the file.Also applies to: 5-7
Line range hint
1-599
: Overall: Solid improvements to error handling and response management.The changes in this file effectively introduce the
HashchainResponse
type and improve error handling throughout theSequencer
implementation. The use ofbail!
and pattern matching enhances the code's robustness and readability. The modifications are well-aligned with Rust best practices and the codebase's evolving architecture.crates/common/src/tree.rs (1)
303-303
: LGTM! Improved method signatureThe updated
get
method signature in theSnarkableTree
trait is a good improvement. ReturningResult<HashchainResponse>
simplifies the API and aligns well with the newHashchainResponse
enum. This change makes the code more readable and easier to use, while also providing a clear way to handle both successful retrievals and proofs of non-existence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK, lets see what CI says
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful
Introduces a new response structure for hashchain retrieval, that returns the corresponding membership-proofs along with the hashchain
Summary by CodeRabbit
New Features
MembershipProof
andHashchainResponse
types for improved data management.UserKeyResponse
to include an optional hashchain field and a new proof type.Bug Fixes
Documentation