Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return error codes in binary merkle tree #279

Merged
merged 5 commits into from
Jan 23, 2024

Conversation

rach-id
Copy link
Member

@rach-id rach-id commented Jan 12, 2024

Overview

Binary merkle tree library not reverting and returning error codes instead.

Checklist

  • New and updated code has appropriate documentation
  • New and updated code has new and/or updated testing
  • Required CI checks are passing
  • Visual proof for any user facing features like CLI or documentation updates
  • Linked issues closed with keywords

Summary by CodeRabbit

  • Refactor
    • Improved error handling in the verification process of binary merkle trees.
    • Enhanced control flow with the introduction of error codes for merkle tree operations.

@rach-id rach-id added enhancement New feature or request contracts labels Jan 12, 2024
@rach-id rach-id self-assigned this Jan 12, 2024
Copy link

coderabbitai bot commented Jan 12, 2024

Warning

Rate Limit Exceeded

@sweexordious has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 37 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between b56b658 and 3420835.

Walkthrough

The recent updates across several Solidity files involve enhancing error handling in Merkle tree verification. The BinaryMerkleTree now returns error codes, and the calling contracts adjust to handle these codes. Changes streamline tuple assignment to ignore unneeded values and assert the absence of errors for cleaner, more robust contract functions.

Changes

File Path Change Summary
.../Blobstream.sol, .../verifier/DAVerifier.sol Modified to use tuple assignment for ignoring second return values and to handle new error codes from BinaryMerkleTree.verify.
.../tree/binary/BinaryMerkleTree.sol Added ErrorCodes enum, updated verify and computeRootHash to return error codes.
.../tree/binary/test/BinaryMerkleTree.t.sol, .../verifier/test/DAVerifier.t.sol Adjusted function calls to handle error codes from BinaryMerkleTree.verify and added assertions for these error codes.

🐰🌟
To the code, we hop and leap,
With error checks, our contracts keep.
No more silent failures in the deep,
Our Merkle roots are strong, not weak. 🌱

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.
  • 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 from git 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

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 20b4d30 and 82d67c3.
Files selected for processing (5)
  • src/Blobstream.sol (1 hunks)
  • src/lib/tree/binary/BinaryMerkleTree.sol (3 hunks)
  • src/lib/tree/binary/test/BinaryMerkleTree.t.sol (17 hunks)
  • src/lib/verifier/DAVerifier.sol (2 hunks)
  • src/lib/verifier/test/DAVerifier.t.sol (1 hunks)
Additional comments: 9
src/lib/tree/binary/BinaryMerkleTree.sol (4)
  • 15-27: The addition of the ErrorCodes enum provides a clear and descriptive way to handle errors within the BinaryMerkleTree library. This is a good practice as it improves the readability and maintainability of the code by providing meaningful error messages instead of generic failures.

  • 8-57: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [39-76]

The verify function has been refactored to return a tuple containing a boolean and an ErrorCodes value. This change allows the function to communicate specific error conditions back to the caller, which is a significant improvement over the previous implementation that only returned a boolean. It's important to ensure that all callers of this function are updated to handle the new return type.

  • 85-115: The computeRootHash function has been updated to return a tuple (bytes32, ErrorCodes), which is consistent with the changes made to the verify function. This ensures that error handling is uniform across the library. It's crucial that all internal calls to computeRootHash properly handle the new return type.

  • 82-118: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [117-130]

The slice function uses revert with a specific error message for handling invalid ranges, which is appropriate for an internal utility function. This approach is consistent with Solidity's error handling patterns.

src/lib/verifier/DAVerifier.sol (1)
  • 155-156: The code destructures the tuple returned by BinaryMerkleTree.verify to use the boolean value for validation. It's important to ensure that the error code is not ignored and is handled appropriately, especially if it can provide additional context for a failed verification.
Verification successful

The output from the rg command provides context around the usage of the BinaryMerkleTree.ErrorCodes error in the BinaryMerkleTree.t.sol test file. It shows that the error code is being checked against the NoError value using assertEq, and the boolean isValid is being checked with assertTrue or assertTrue(!isValid) depending on the expected outcome of the verification.

This indicates that the error codes are indeed being handled and tested for correctness in various scenarios within the test cases. The tests are designed to ensure that the correct error code is returned for different verification scenarios, which aligns with the PR's objective to enhance error handling.

Based on the provided context, it can be concluded that the error codes are being properly handled in the test cases where they are captured.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the error code from BinaryMerkleTree.verify is handled properly in all calls within the codebase.
ast-grep --lang solidity --pattern $'BinaryMerkleTree.verify($_, $_, $_) {
  (bool valid, $_) = $$$;
  $$$
}'

Length of output: 227



Script:

#!/bin/bash
# Search for the usage of BinaryMerkleTree.verify in the codebase using ripgrep
rg "BinaryMerkleTree.verify"

Length of output: 2966



Script:

#!/bin/bash
# Check the handling of the error code in the test files where it is captured
rg "BinaryMerkleTree.ErrorCodes error" src/lib/tree/binary/test/

Length of output: 2042



Script:

#!/bin/bash
# Extract the relevant test cases from BinaryMerkleTree.t.sol to see how the error code is being used
rg -C 5 "BinaryMerkleTree.ErrorCodes error" src/lib/tree/binary/test/BinaryMerkleTree.t.sol

Length of output: 6174

src/Blobstream.sol (1)
  • 362-362: The isProofValid variable is now assigned using tuple assignment to ignore the second return value of the BinaryMerkleTree.verify function. This change is consistent with the new return type of verify. However, it's important to ensure that the ignored error code is not needed for additional context in case of a failed verification.
Verification successful

The review comment is consistent with the codebase. The Blobstream.sol and DAVerifier.sol contracts have been updated to ignore the second value of the tuple returned by BinaryMerkleTree.verify, which aligns with the new error handling strategy. The test files capture the error codes for assertions, indicating that the error codes are used where necessary, such as in testing, but are ignored in the main contract logic where only the boolean validity check is required.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the ignored error code from BinaryMerkleTree.verify is not needed for additional context.
ast-grep --lang solidity --pattern $'BinaryMerkleTree.verify($_, $_, $_) {
  (bool isProofValid,) = $$$;
  $$$
}'

Length of output: 231



Script:

#!/bin/bash
# Search for the usage of `BinaryMerkleTree.verify` and how its return values are handled.
rg "BinaryMerkleTree.verify" --multiline-dotall

Length of output: 2985

src/lib/tree/binary/test/BinaryMerkleTree.t.sol (2)
  • 54-54: The test testVerifyNone has been updated to handle the new tuple return type from BinaryMerkleTree.verify. It's good to see that the tests are being updated to reflect the changes in the library's interface.

  • 65-66: The test testVerifyOneLeafEmpty now includes an assertion to check the error code, which is a good practice to ensure that the correct error code is returned for the given scenario.

src/lib/verifier/test/DAVerifier.t.sol (1)
  • 170-172: The updated testComputeSquareSizeFromRowProof function now captures the error code from the BinaryMerkleTree.verify function and asserts that it equals NoError. This change aligns with the PR's objective to enhance error handling by using specific error codes instead of reverting. The test ensures that the verify function is returning the expected NoError code, which is a good practice for confirming the correct behavior of the updated error handling mechanism.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 82d67c3 and 8d0dec8.
Files selected for processing (1)
  • src/lib/tree/binary/BinaryMerkleTree.sol (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/lib/tree/binary/BinaryMerkleTree.sol

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8d0dec8 and b56b658.
Files selected for processing (2)
  • src/lib/tree/binary/BinaryMerkleTree.sol (3 hunks)
  • wrappers/Blobstream.sol/wrapper.go (1 hunks)
Files not summarized due to errors (1)
  • wrappers/Blobstream.sol/wrapper.go: Error: Message exceeds token limit
Files skipped from review as they are similar to previous changes (1)
  • src/lib/tree/binary/BinaryMerkleTree.sol
Additional comments: 7
wrappers/Blobstream.sol/wrapper.go (7)
  • 61-61: The WrappersMetaData variable has been updated with a new ABI and binary. Ensure that the ABI changes are consistent with the intended contract functionality changes and that the binary corresponds to the compiled result of the updated contract.

  • 61-61: The WrappersMetaData ABI string is quite long. It's important to ensure that the ABI matches the Solidity contract's functions, events, and errors. This can be done by comparing the ABI string with the contract's Solidity source code.

  • 61-61: The DeployWrappers function should be checked to ensure that it correctly deploys the contract with the updated ABI and binary. This includes verifying that the parsed ABI is not nil and that the deployment transaction is created without errors.

Verification successful

The provided output from the shell script execution shows the DeployWrappers function within wrappers/ERC1967Proxy.sol/wrapper.go. The function takes four parameters and returns four values. It first retrieves the ABI using WrappersMetaData.GetAbi() and checks for errors. If the ABI is nil, it returns an error. Then, it proceeds to deploy the contract using bind.DeployContract with the provided parameters and the binary data from WrappersBin. If there are no errors during deployment, it returns the address of the deployed contract, the transaction object, a Wrappers instance, and a nil error.

Based on this information, the DeployWrappers function appears to be correctly set up to deploy the contract with the updated ABI and binary, as it checks for a non-nil ABI and handles errors appropriately during the deployment process.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the DeployWrappers function correctly deploys the contract.
ast-grep --lang go --pattern $'func DeployWrappers($_, $_, $_, $_) $_ {
  $$$
}'

Length of output: 1464

* 61-61: Ensure that the types `WrappersCaller`, `WrappersTransactor`, and `WrappersFilterer` are correctly set up for contract interaction, including the correct contract address and backend interfaces.
Verification successful

The types WrappersCaller, WrappersTransactor, and WrappersFilterer in wrappers/Blobstream.sol/wrapper.go are set up with a *bind.BoundContract field, which is a generic contract wrapper for low-level calls. This setup is typical for Go Ethereum contract interaction, where a BoundContract is used to interact with smart contracts on the Ethereum blockchain.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the types are set up correctly for contract interaction.
ast-grep --lang go --pattern $'type WrappersCaller struct {
  $$$
}'
ast-grep --lang go --pattern $'type WrappersTransactor struct {
  $$$
}'
ast-grep --lang go --pattern $'type WrappersFilterer struct {
  $$$
}'

Length of output: 1649

* 61-61: Check that the session types `WrappersSession`, `WrappersCallerSession`, and `WrappersTransactorSession` are correctly set up with pre-set options for contract interaction.
Verification successful

The session types WrappersSession, WrappersCallerSession, and WrappersTransactorSession in wrappers/Blobstream.sol/wrapper.go are set up as follows:

  • WrappersSession includes a Contract field for the generic contract binding, CallOpts for call options, and TransactOpts for transaction authorization options.
  • WrappersCallerSession includes a Contract field for the generic contract caller binding and CallOpts for call options.
  • WrappersTransactorSession includes a Contract field for the generic contract transactor binding and TransactOpts for transaction authorization options.

These session types are correctly set up to pre-set call and transact options for contract interaction.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the session types are correctly set up.
ast-grep --lang go --pattern $'type WrappersSession struct {
  $$$
}'
ast-grep --lang go --pattern $'type WrappersCallerSession struct {
  $$$
}'
ast-grep --lang go --pattern $'type WrappersTransactorSession struct {
  $$$
}'

Length of output: 2733

* 61-61: Verify that the raw types `WrappersRaw`, `WrappersCallerRaw`, and `WrappersTransactorRaw` provide the correct low-level access to the contract's methods.
Verification successful

The provided script output confirms the existence of the WrappersRaw, WrappersCallerRaw, and WrappersTransactorRaw structs within the wrapper.go files for both ERC1967Proxy.sol and Blobstream.sol. Each of these structs contains a field named Contract which is a pointer to another struct (Wrappers, WrappersCaller, and WrappersTransactor respectively). These fields are intended to provide access to the raw methods of the contract.

Based on this information, it can be concluded that the raw types are indeed present and are structured to provide low-level access to the contract's methods as intended.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the raw types provide correct low-level access.
ast-grep --lang go --pattern $'type WrappersRaw struct {
  $$$
}'
ast-grep --lang go --pattern $'type WrappersCallerRaw struct {
  $$$
}'
ast-grep --lang go --pattern $'type WrappersTransactorRaw struct {
  $$$
}'

Length of output: 1708

* 61-61: Ensure that the methods for deploying and interacting with the contract, such as `DeployWrappers`, `Call`, `Transfer`, `Transact`, and event filters like `FilterDataRootTupleRootEvent`, are correctly implemented and bound to the contract's functions and events.
Verification successful

The verification scripts have successfully identified the implementations of the DeployWrappers, Call, Transfer, Transact, and FilterDataRootTupleRootEvent functions in the Go wrapper files for the smart contracts. The presence of these functions in the wrapper.go files for both ERC1967Proxy.sol and Blobstream.sol indicates that the methods for deploying and interacting with the contracts, as well as the event filters, are indeed implemented as expected.

Based on the output, we can conclude that the methods and event filters are correctly implemented and bound to the contract's functions and events in the Go wrapper files.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the methods are correctly implemented.
ast-grep --lang go --pattern $'func DeployWrappers($_, $_, $_, $_) $_ {
  $$$
}'
ast-grep --lang go --pattern $'func ($_ *WrappersRaw) Call($_, $_, $_, $_) $_ {
  $$$
}'
ast-grep --lang go --pattern $'func ($_ *WrappersRaw) Transfer($_) $_ {
  $$$
}'
ast-grep --lang go --pattern $'func ($_ *WrappersRaw) Transact($_, $_, $_) $_ {
  $$$
}'
# Verify that the event filters are correctly implemented.
ast-grep --lang go --pattern $'func ($_ *WrappersFilterer) FilterDataRootTupleRootEvent($_, $_) $_ {
  $$$
}'

Length of output: 4874

@rach-id rach-id merged commit 8bbbfc5 into celestiaorg:master Jan 23, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants