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(storev2): change error namespace #19698

Merged
merged 2 commits into from
Mar 8, 2024

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Mar 8, 2024

Description

Change error namespace from store/v2 to prohibit double error registration.
Moves the error in an error package, to have all errors at a same place instead of in random files for avoiding cycling deps


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Refactor
    • Improved error handling across various database implementations for enhanced stability and consistency.
  • Bug Fixes
    • Fixed an issue related to invalid proof errors by introducing a new error code.

Copy link
Contributor

coderabbitai bot commented Mar 8, 2024

Walkthrough

Walkthrough

The recent update involves a significant restructuring of error handling mechanisms across various components within the system. This includes standardizing error references, introducing new error packages, updating error codes, and enhancing error messages for improved clarity and consistency throughout the system.

Changes

Files Change Summary
.../goleveldb.go, .../memdb.go, .../prefixdb.go Refactored error handling to utilize the store/v2 errors package.
store/errors/errors.go Updated StoreCodespace constant and introduced ErrInvalidProof with code 2.
.../snapshots/chunk.go, .../manager.go, .../store.go Updated error handling and imports to use cosmossdk.io/store/v2/errors.
.../pebbledb/db.go, .../rocksdb/db.go, .../sqlite/db.go Transitioned to using storeerrors package for error references.
store/proof/proof.go Enhanced error handling, removed ErrInvalidProof, and updated error messages.

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>.
    • Generate unit-tests 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 0a98bfb and 92e4fec.
Files selected for processing (10)
  • store/db/goleveldb.go (12 hunks)
  • store/db/memdb.go (11 hunks)
  • store/db/prefixdb.go (8 hunks)
  • store/errors/errors.go (2 hunks)
  • store/snapshots/chunk.go (3 hunks)
  • store/snapshots/manager.go (10 hunks)
  • store/snapshots/store.go (5 hunks)
  • store/storage/pebbledb/db.go (5 hunks)
  • store/storage/rocksdb/db.go (4 hunks)
  • store/storage/sqlite/db.go (3 hunks)
Additional comments: 68
store/errors/errors.go (2)
  • 10-10: The update to the StoreCodespace constant to "storev2" is appropriate and aligns with the PR's objective to refactor the error namespace within the store/v2 module.
  • 13-14: The addition of ErrInvalidProof with code 2 is correctly implemented and well-documented. This enhances error specificity and handling within the module.
store/snapshots/chunk.go (3)
  • 8-8: The update to the import path for errors to cosmossdk.io/store/v2/errors is correct, ensuring that the new error namespace is utilized throughout the file.
  • 75-75: The error wrapping in ChunkWriter using storeerrors.ErrLogic for attempting to write to a closed writer is appropriately handled and clearly communicates the error condition.
  • 177-177: The error wrapping in ValidRestoreHeight using storeerrors.ErrLogic for an invalid height (0) is correctly implemented, providing a clear and specific error message.
store/storage/rocksdb/db.go (3)
  • 17-17: The update to import cosmossdk.io/store/v2/errors for error handling is correct, ensuring that the new error namespace is utilized throughout the file.
  • 103-103: The use of errors.ErrVersionPruned in getSlice for handling attempts to access pruned versions is appropriately implemented, providing a clear and specific error message.
  • 170-170: The error handling in iterators using errors.ErrKeyEmpty and errors.ErrStartAfterEnd is correctly implemented, providing clear messages for invalid iterator conditions.

Also applies to: 174-174, 186-186, 190-190

store/db/prefixdb.go (2)
  • 10-10: The update to import cosmossdk.io/store/v2/errors for error handling is correct, ensuring that the new error namespace is utilized throughout the file.
  • 33-33: The replacement of error references with those from the cosmossdk.io/store/v2/errors package across various methods in the PrefixDB struct is correctly implemented, providing clear and specific error messages for invalid conditions.

Also applies to: 47-47, 61-61, 82-82, 281-281, 284-284, 293-293, 317-317

store/storage/sqlite/db.go (3)
  • 15-15: The update to import storeerrors from cosmossdk.io/store/v2/errors for error handling is correct, ensuring that the new error namespace is utilized throughout the file.
  • 142-142: The use of storeerrors.ErrVersionPruned in Get for handling attempts to access pruned versions is appropriately implemented, providing a clear and specific error message.
  • 221-221: The error handling in iterators using storeerrors.ErrKeyEmpty and storeerrors.ErrStartAfterEnd is correctly implemented, providing clear messages for invalid iterator conditions.

Also applies to: 225-225, 233-233, 237-237

store/db/goleveldb.go (12)
  • 10-10: The import alias dberrors for the package github.com/syndtr/goleveldb/leveldb/errors is correctly introduced to distinguish it from the newly imported cosmossdk.io/store/v2/errors package. This change improves readability and avoids potential naming conflicts.
  • 18-18: The import of the new error package cosmossdk.io/store/v2/errors is correctly added to use the updated error handling mechanism within the Cosmos SDK's store/v2 module. This aligns with the PR's objective to refactor the error namespace.
  • 58-58: The use of errors.ErrKeyEmpty from the cosmossdk.io/store/v2/errors package for handling empty keys is appropriate and aligns with the Cosmos SDK's error handling conventions. This ensures consistency across the SDK.
  • 62-62: The comparison err == dberrors.ErrNotFound correctly utilizes the dberrors alias to distinguish between the leveldb error and the Cosmos SDK's store errors. This ensures that the specific leveldb not found error is correctly handled.
  • 82-82: The use of errors.ErrKeyEmpty for handling empty keys in the Set method is consistent with the SDK's error handling strategy. This change ensures that the error handling is uniform across different database implementations.
  • 85-85: The introduction of errors.ErrValueNil for handling nil values in the Set method is a good practice. It ensures that the method's preconditions are explicitly checked, improving the robustness of the database interface.
  • 96-96: Repeating the use of errors.ErrKeyEmpty in the SetSync method for empty key validation maintains consistency in error handling across synchronous and asynchronous write operations.
  • 99-99: The use of errors.ErrValueNil in the SetSync method for nil value checks is consistent with the Set method, ensuring uniform error handling for both synchronous and asynchronous writes.
  • 110-110: Applying errors.ErrKeyEmpty in the Delete method for empty key validation aligns with the error handling strategy used in other methods, ensuring a consistent approach across the database interface.
  • 121-121: The use of errors.ErrKeyEmpty in the DeleteSync method for empty key checks is appropriate and consistent with the Delete method, maintaining uniform error handling for deletion operations.
  • 199-199: The introduction of errors.ErrKeyEmpty for handling empty keys in the Iterator method is consistent with the error handling strategy used in other methods, ensuring uniformity across the database interface.
  • 208-208: The use of errors.ErrKeyEmpty in the ReverseIterator method for empty key validation is appropriate and maintains consistency in error handling across iterator operations.
store/storage/pebbledb/db.go (8)
  • 15-15: The introduction of the storeerrors alias for the cosmossdk.io/store/v2/errors package is correctly done to distinguish it from other error packages. This change is necessary for using the updated error handling mechanism within the Cosmos SDK's store/v2 module.
  • 152-152: The use of storeerrors.ErrVersionPruned for handling attempts to access pruned versions is appropriate and aligns with the Cosmos SDK's error handling conventions. This ensures that the error is descriptive and specific to the version pruning functionality.
  • 157-157: The check for storeerrors.ErrRecordNotFound using errors.Is is correctly implemented. This approach is idiomatic in Go for handling wrapped errors and ensures that the specific record not found error is correctly identified and handled.
  • 272-272: The use of storeerrors.ErrKeyEmpty for handling empty keys in the Iterator method is consistent with the SDK's error handling strategy. This change ensures that the error handling is uniform across different database implementations.
  • 276-276: The introduction of storeerrors.ErrStartAfterEnd for handling cases where the start key is after the end key in range queries is a good practice. It ensures that the method's preconditions are explicitly checked, improving the robustness of the database interface.
  • 296-296: Repeating the use of storeerrors.ErrKeyEmpty in the ReverseIterator method for empty key validation maintains consistency in error handling across iterator operations.
  • 300-300: The use of storeerrors.ErrStartAfterEnd in the ReverseIterator method for handling invalid range queries is appropriate and consistent with the Iterator method, ensuring uniform error handling for both forward and reverse iterations.
  • 382-382: The check for storeerrors.ErrRecordNotFound in the getMVCCSlice function is correctly implemented. This ensures that attempts to access non-existent records are handled with a specific and descriptive error.
store/db/memdb.go (15)
  • 13-13: The import of the cosmossdk.io/store/v2/errors package is correctly added to use the updated error handling mechanism within the Cosmos SDK's store/v2 module. This aligns with the PR's objective to refactor the error namespace.
  • 68-68: The use of errors.ErrKeyEmpty from the cosmossdk.io/store/v2/errors package for handling empty keys in the Get method is appropriate and aligns with the Cosmos SDK's error handling conventions. This ensures consistency across the SDK.
  • 83-83: The introduction of errors.ErrKeyEmpty for handling empty keys in the Has method is consistent with the SDK's error handling strategy. This change ensures that the error handling is uniform across different database implementations.
  • 94-94: The use of errors.ErrKeyEmpty for handling empty keys in the Set method is consistent with the error handling strategy used in other methods, ensuring a consistent approach across the database interface.
  • 97-97: The introduction of errors.ErrValueNil for handling nil values in the Set method is a good practice. It ensures that the method's preconditions are explicitly checked, improving the robustness of the database interface.
  • 119-119: Applying errors.ErrKeyEmpty in the Delete method for empty key validation aligns with the error handling strategy used in other methods, ensuring a consistent approach across the database interface.
  • 185-185: The use of errors.ErrKeyEmpty for handling empty keys in the Iterator method is appropriate and maintains consistency in error handling across iterator operations.
  • 194-194: Repeating the use of errors.ErrKeyEmpty in the ReverseIterator method for empty key validation maintains consistency in error handling across synchronous and asynchronous write operations.
  • 202-202: The introduction of errors.ErrKeyEmpty for handling empty keys in the IteratorNoMtx method is consistent with the SDK's error handling strategy. This change ensures that the error handling is uniform across different database implementations.
  • 210-210: The use of errors.ErrKeyEmpty in the ReverseIteratorNoMtx method for empty key checks is appropriate and consistent with the IteratorNoMtx method, maintaining uniform error handling for iterator operations without mutex locking.
  • 399-399: The use of errors.ErrKeyEmpty in the memDBBatch Set method for handling empty keys is appropriate and aligns with the Cosmos SDK's error handling conventions. This ensures consistency across the SDK.
  • 402-402: The introduction of errors.ErrValueNil for handling nil values in the memDBBatch Set method is a good practice. It ensures that the method's preconditions are explicitly checked, improving the robustness of the database interface.
  • 415-415: Applying errors.ErrKeyEmpty in the memDBBatch Delete method for empty key validation aligns with the error handling strategy used in other methods, ensuring a consistent approach across the database interface.
  • 428-428: The check for errors.ErrBatchClosed in the memDBBatch Write method ensures that attempts to write a closed batch are handled with a specific and descriptive error. This is a good practice for robust error handling.
  • 463-463: The check for errors.ErrBatchClosed in the memDBBatch GetByteSize method is correctly implemented. This ensures that attempts to get the byte size of a closed batch are handled with a specific and descriptive error.
store/snapshots/store.go (7)
  • 21-21: The import alias storeerrors for cosmossdk.io/store/v2/errors is correctly introduced to handle error namespacing. This change aligns with the PR's objective to refactor error handling within the store/v2 module.
  • 41-41: The usage of storeerrors.ErrLogic for error handling when the snapshot directory is not provided is appropriate and improves the clarity of the error's origin.
  • 64-64: Correctly utilizing storeerrors.ErrConflict to indicate a conflict error when a snapshot is currently being saved. This change enhances error specificity and maintainability.
  • 252-252: The use of storeerrors.ErrLogic for validating the snapshot height is a good practice, ensuring that the error is descriptive and originates from a consistent namespace.
  • 260-260: Appropriately using storeerrors.ErrConflict to handle the error when a snapshot for a specific height is already being saved, maintaining consistency in error handling.
  • 403-403: The application of storeerrors.ErrLogic for validating the length of a snapshot key is correct and improves the error's descriptiveness.
  • 406-406: Utilizing storeerrors.ErrLogic to indicate an invalid snapshot key prefix is appropriate, ensuring that the error message is clear and originates from a consistent error namespace.
store/snapshots/manager.go (13)
  • 17-17: The import alias storeerrors for cosmossdk.io/store/v2/errors is correctly introduced to handle error namespacing. This change aligns with the PR's objective to refactor error handling within the store/v2 module.
  • 121-121: Correctly using storeerrors.ErrLogic to indicate a logical error when attempting to begin a "none" operation. This enhances the clarity and maintainability of error handling.
  • 124-124: Appropriately utilizing storeerrors.ErrConflict to handle the error when an operation is already in progress, maintaining consistency in error handling across the module.
  • 170-170: The use of storeerrors.ErrLogic for handling a nil Snapshot Manager instance is appropriate, ensuring that the error is descriptive and originates from a consistent namespace.
  • 184-184: Correctly using storeerrors.ErrConflict to indicate a conflict error when a more recent snapshot already exists. This change enhances error specificity and maintainability.
  • 241-241: The application of storeerrors.ErrLogic for handling a nil Snapshot Manager instance during migration is correct, improving the error's descriptiveness.
  • 310-310: Utilizing storeerrors.ErrLogic to indicate a logical error when attempting to restore a snapshot at height 0 is appropriate, ensuring that the error message is clear and originates from a consistent error namespace.
  • 419-419: The use of storeerrors.ErrLogic for handling unknown snapshot items during restoration is correct, enhancing the clarity and maintainability of error handling.
  • 423-423: Appropriately using storeerrors.ErrLogic to indicate an error when an unknown extension snapshotter is encountered during restoration. This maintains consistency in error handling across the module.
  • 452-452: Correctly utilizing storeerrors.ErrLogic to handle the absence of a restore operation in progress, ensuring that the error is descriptive and originates from a consistent namespace.
  • 456-456: The application of storeerrors.ErrLogic for handling unexpected chunks during restoration is appropriate, improving the error's descriptiveness and maintainability.
  • 466-466: Utilizing storeerrors.ErrLogic to indicate that the restoration ended unexpectedly is correct, ensuring that the error message is clear and originates from a consistent error namespace.
  • 502-502: The use of storeerrors.ErrLogic for indicating that the restoration ended prematurely is appropriate, enhancing the clarity and maintainability of error handling.

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.

Review Status

Actionable comments generated: 1

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 92e4fec and e9f52de.
Files selected for processing (1)
  • store/proof/proof.go (3 hunks)
Additional comments: 4
store/proof/proof.go (4)
  • 9-9: The import alias storeerrors for cosmossdk.io/store/v2/errors is correctly defined and follows Go conventions for package aliasing. This change aligns with the PR's objective to refactor error handling within the store/v2 module.
  • 101-101: The error wrapping using storeerrors.ErrInvalidProof with errors.Wrapf is correctly implemented. However, consider the user's preference for not making error messages too generic. Ensure that the error message provides enough context without revealing sensitive information such as the key.

Would you like suggestions for making error messages detailed yet secure?

Verification successful

The error message in the provided code snippet appears to be appropriately detailed without revealing sensitive information, such as specific keys. It strikes a balance between providing context about the error and adhering to the user's preference for not making error messages too generic. This approach seems to align with ensuring error messages are secure yet informative.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Manual review recommended to ensure error messages are appropriately detailed without revealing sensitive information.
echo "Review error messages for sensitivity and appropriateness."

Length of output: 126

* 110-110: The use of `storeerrors.ErrInvalidProof` for indicating the absence of a key is appropriate. However, the error message includes the key, which might conflict with the user alexanderbez's preference for not revealing the key in error messages. Consider rephrasing the error message to avoid potential security concerns.

Consider removing or obfuscating the key in the error message to align with security best practices and user preferences.

Skipped due to learnings

User: alexanderbez
PR: #18633
File: store/proof.go:130-130
Timestamp: 2023-12-05T16:58:16.746Z
Learning: User alexanderbez has requested to ignore the suggestion to make the error message more generic to avoid potential sensitive information leakage.

* 120-120: The error handling for invalid argument lengths using `storeerrors.ErrInvalidProof` is correctly implemented. This is a good practice as it provides clear feedback to the caller about the nature of the error.

}

case 1:
// Args is length 1, verify existence of key with value args[0]
if !ics23.VerifyMembership(op.Spec, root, op.Proof, op.Key, args[0]) {
return nil, errors.Wrapf(ErrInvalidProof, "proof did not verify existence of key %s with given value %x", op.Key, args[0])
return nil, errors.Wrapf(storeerrors.ErrInvalidProof, "proof did not verify existence of key %s with given value %x", op.Key, args[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

While the error handling logic is correct, including both the key and value in the error message could potentially reveal sensitive information. This is especially relevant given the user's preference for not disclosing the key in error messages. Consider how to provide useful feedback in errors without compromising security.

Reevaluate the inclusion of key and value in error messages to ensure they do not inadvertently expose sensitive information.

@julienrbrt julienrbrt added this pull request to the merge queue Mar 8, 2024
Merged via the queue into main with commit f69dabe Mar 8, 2024
63 of 65 checks passed
@julienrbrt julienrbrt deleted the julien/store-error-collision branch March 8, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants