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

fix(evm-precompiles): add assertNumArgs validation #2060

Merged
merged 2 commits into from
Oct 3, 2024
Merged

Conversation

Unique-Divine
Copy link
Member

@Unique-Divine Unique-Divine commented Oct 2, 2024

Purpose / Abstract

The logic for method.Inputs.Unpack does not validate the number of arguments the same way that method.Inputs.Pack does, so this validation check should still be present.

Note that it's very difficult to actually hit these error blocks in the tests because we always ABI Pack before Unpacking , which performs this validation. I think clients do this before broadcasting transactions too. The error blocks may be impossible to reach, but I'm not certain, so it's better to err on the side of more defensive programming.

Summary by CodeRabbit

  • New Features
    • Enhanced Ethereum compatibility with new features in the Nibiru EVM, including typed events for contract interactions and an in-memory EventBus.
    • Added methods for managing fungible token mappings and inflation parameters.
  • Bug Fixes
    • Resolved issues related to keyring import functions and FunToken state marshalling.
  • Documentation
    • Updated CHANGELOG.md to reflect the latest changes and improvements.
  • Refactor
    • Improved argument validation across various methods to enhance error handling and robustness.

@Unique-Divine Unique-Divine requested a review from a team as a code owner October 2, 2024 14:45
Copy link
Contributor

coderabbitai bot commented Oct 2, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces extensive updates to the Nibiru project's changelog, detailing changes across multiple versions. Key modifications include breaking changes to the state machine, enhancements to Ethereum compatibility, and various bug fixes, particularly in the EVM context. Additionally, new methods and validation checks have been added to improve error handling and argument validation in the precompile package. The updates aim to enhance both user-facing features and internal code quality.

Changes

File Path Change Summary
CHANGELOG.md Comprehensive update detailing changes across versions, including features, improvements, bug fixes, and breaking changes.
x/evm/precompile/errors.go Added assertNumArgs function for argument count validation.
x/evm/precompile/funtoken.go Updated decomposeBankSendArgs method to include argument count validation; improved comments.
x/evm/precompile/wasm.go Updated queryRaw method to include argument validation using assertNumArgs.
x/evm/precompile/wasm_parse.go Added argument count validation to parseInstantiateArgs, parseExecuteArgs, parseQueryArgs, and parseExecuteMultiArgs methods.

Possibly related PRs

Poem

🐰 In the land of code where rabbits play,
New features hop in, brightening the day.
With checks for arguments, all snug and tight,
Nibiru's journey is a joyful flight!
Bugs are squashed, and functions refined,
A happy project, with progress aligned! 🌟


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

🧹 Outside diff range and nitpick comments (8)
x/evm/precompile/errors.go (1)

46-55: LGTM! Consider adding an example usage in the comment.

The assertNumArgs function is well-implemented and provides a valuable addition to the error handling capabilities of the precompile package. The logic is correct, and the error message is clear and informative.

Consider enhancing the function comment by adding an example usage. This can help other developers quickly understand how to use the function. For instance:

 // assertNumArgs checks if the number of provided arguments matches the expected
 // count. If lenArgs does not equal wantArgsLen, it returns an error describing
 // the mismatch between expected and actual argument counts.
+//
+// Example usage:
+//   if err := assertNumArgs(len(args), 2); err != nil {
+//       return err
+//   }
 func assertNumArgs(lenArgs, wantArgsLen int) error {
x/evm/precompile/wasm_parse.go (4)

87-90: LGTM! Consider adding a comment for clarity.

The addition of assertNumArgs validation is a good improvement, aligning with the PR objectives. It ensures that the correct number of arguments is provided before proceeding with parsing.

Consider adding a brief comment explaining why 5 arguments are expected, for better code readability:

// Expect 5 arguments: admin, codeID, msgArgs, label, and funds
if e := assertNumArgs(len(args), 5); e != nil {
    err = e
    return
}

146-149: LGTM! Consider adding a comment for clarity.

The addition of assertNumArgs validation is a good improvement, aligning with the PR objectives. It ensures that the correct number of arguments is provided before proceeding with parsing.

Consider adding a brief comment explaining why 3 arguments are expected, for better code readability:

// Expect 3 arguments: contractAddr, msgArgs, and funds
if e := assertNumArgs(len(args), 3); e != nil {
    err = e
    return
}

192-195: LGTM! Consider adding a comment for clarity.

The addition of assertNumArgs validation is a good improvement, aligning with the PR objectives. It ensures that the correct number of arguments is provided before proceeding with parsing.

Consider adding a brief comment explaining why 2 arguments are expected, for better code readability:

// Expect 2 arguments: wasmContract and req
if e := assertNumArgs(len(args), 2); e != nil {
    err = e
    return
}

226-229: LGTM! Consider adding a comment for clarity.

The addition of assertNumArgs validation is a good improvement, aligning with the PR objectives. It ensures that the correct number of arguments is provided before proceeding with parsing.

Consider adding a brief comment explaining why 1 argument is expected, for better code readability:

// Expect 1 argument: an array of execution messages
if e := assertNumArgs(len(args), 1); e != nil {
    err = e
    return
}
x/evm/precompile/funtoken.go (1)

208-211: Approve changes with a minor suggestion for improvement

The addition of the assertNumArgs validation is a good improvement that aligns with the PR objective. It enhances the robustness of the code by ensuring the correct number of arguments before proceeding with type assertions.

Consider wrapping the error returned by assertNumArgs with additional context about the function:

if e := assertNumArgs(len(args), 3); e != nil {
    err = fmt.Errorf("decomposeBankSendArgs: %w", e)
    return
}

This will provide more informative error messages, making debugging easier.

x/evm/precompile/wasm.go (1)

358-361: Approve the addition of argument validation with a suggestion for improvement.

The addition of assertNumArgs validation is a good improvement to the function's robustness. It ensures that the correct number of arguments is provided before processing, which can prevent potential errors or unexpected behavior due to incorrect input.

However, to improve error clarity, consider adding more context to the error message:

-	if e := assertNumArgs(len(args), 2); e != nil {
+	if e := assertNumArgs(len(args), 2); e != nil {
+		e = fmt.Errorf("queryRaw: %w", e)
 		err = e
 		return
 	}

This change will make it clearer which function encountered the argument count mismatch, aiding in debugging and error handling.

CHANGELOG.md (1)

127-127: Consider adding more context to the new feature.

The changelog entry for PR #2060 is quite brief. Consider adding a short description of what the assertNumArgs validation does and why it's important. This would provide more context for users and developers.

-- [#2060](https://github.com/NibiruChain/nibiru/pull/2060) - fix(evm-precompiles): add assertNumArgs validation
+- [#2060](https://github.com/NibiruChain/nibiru/pull/2060) - fix(evm-precompiles): add assertNumArgs validation to ensure correct number of arguments are provided to precompile functions, improving robustness and error handling
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fa62d35 and baac2e4.

📒 Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • x/evm/precompile/errors.go (1 hunks)
  • x/evm/precompile/funtoken.go (1 hunks)
  • x/evm/precompile/wasm.go (1 hunks)
  • x/evm/precompile/wasm_parse.go (4 hunks)
🔇 Additional comments (14)
x/evm/precompile/errors.go (1)

46-55: Verify usage of assertNumArgs in the codebase

The new assertNumArgs function looks good and enhances the error handling capabilities of the package. To ensure it's being utilized as intended, let's verify its usage across the codebase.

Run the following script to check for usage of assertNumArgs:

If no usage is found, consider adding TODO comments in relevant places where assertNumArgs should be used, or update the PR description to clarify the intended usage of this new function.

✅ Verification successful

Usage of assertNumArgs verified

The assertNumArgs function is correctly utilized in the following files:

  • x/evm/precompile/wasm_parse.go
  • x/evm/precompile/wasm.go
  • x/evm/precompile/funtoken.go
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of assertNumArgs function in the codebase

# Search for function calls to assertNumArgs
echo "Searching for assertNumArgs usage:"
rg --type go "assertNumArgs\(" ./x/evm

# If no results, check if the function is at least imported in other files
if [ $? -ne 0 ]; then
    echo "No direct usage found. Checking for imports of the errors.go file:"
    rg --type go 'import\s+\([^)]*"[^"]*x/evm/precompile"[^)]*\)' ./x/evm
fi

Length of output: 703

x/evm/precompile/wasm_parse.go (1)

Line range hint 87-229: Overall changes look good. Verify assertNumArgs implementation.

The consistent addition of assertNumArgs validation across multiple functions improves the robustness of the code by validating input early. This aligns well with the PR objectives.

Please ensure that the assertNumArgs function is properly implemented and tested. Run the following script to verify its implementation:

x/evm/precompile/funtoken.go (1)

Line range hint 1-238: Overall impact of changes is positive and focused

The changes in this file are minimal and well-focused on improving input validation for the decomposeBankSendArgs method. This enhancement aligns with the PR objective and improves the robustness of the FunToken precompile contract without affecting its core logic.

The localized nature of the change minimizes the risk of introducing bugs while effectively addressing the intended improvement in argument validation.

x/evm/precompile/wasm.go (1)

Line range hint 1-384: Summary of changes to x/evm/precompile/wasm.go

The changes in this file are focused on improving the queryRaw function by adding argument validation. This aligns with the PR objective of adding assertNumArgs validation to EVM precompiles. The modification enhances the robustness of the function by ensuring the correct number of arguments is provided before processing.

The changes are minimal and do not affect the overall structure or logic of the file. They represent a good improvement in error handling and input validation for the queryRaw function.

CHANGELOG.md (10)

Line range hint 130-132: LGTM: Clear and concise release notes for v1.5.0.

The release notes for v1.5.0 are clear and concise, providing the key information about enabling IBC CosmWasm smart contracts.


Line range hint 139-143: LGTM: Comprehensive release notes for v1.4.0.

The release notes for v1.4.0 clearly state the main changes: PebbleDB support and increased wasm contract size limit. The breaking change is also clearly indicated.


Line range hint 150-159: LGTM: Well-structured release notes for v1.3.0.

The release notes for v1.3.0 are well-structured, clearly separating features, bug fixes, and improvements. The addition of interchain accounts is highlighted as the main feature.


Line range hint 166-170: LGTM: Concise release notes for v1.2.0.

The release notes for v1.2.0 are brief but clear, focusing on the addition of a burn method to the x/inflation module.


Line range hint 177-216: LGTM: Comprehensive release notes for v1.1.0.

The release notes for v1.1.0 are detailed and well-organized, clearly separating state machine breaking changes, bug fixes, features, and improvements. The focus on inflation-related changes is evident.


Line range hint 223-231: LGTM: Clear release notes for v1.0.3.

The release notes for v1.0.3 are concise and focused, highlighting a fix for IBC transactions from wasm contracts and the addition of a new CLI command.


Line range hint 238-240: LGTM: Concise release notes for v1.0.2.

The release notes for v1.0.2 are brief but clear, focusing on the dependency update for cosmos-sdk.


Line range hint 247-249: LGTM: Clear release notes for v1.0.1.

The release notes for v1.0.1 are concise, focusing on the dependency update for librocksdb.


Line range hint 256-269: LGTM: Comprehensive release notes for v1.0.0.

The release notes for v1.0.0 provide a good overview of the major release, highlighting its significance for the mainnet genesis and listing the key modules included.


Line range hint 1-1191: Overall, the CHANGELOG.md is well-maintained and informative.

The changelog follows best practices by adhering to the Keep a Changelog format and Semantic Versioning. It provides comprehensive information about changes across multiple versions, making it easy for users and developers to understand the project's evolution. The clear categorization of changes (Features, Improvements, Bug Fixes, etc.) and the use of links to pull requests enhance its usefulness.

While most entries are clear and informative, some could benefit from additional context or explanation, particularly for more technical changes. Consider adding brief descriptions of the impact or importance of changes where it's not immediately obvious.

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 9.09091% with 20 lines in your changes missing coverage. Please review.

Project coverage is 65.06%. Comparing base (629aea3) to head (6e27878).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
x/evm/precompile/wasm_parse.go 0.00% 8 Missing and 4 partials ⚠️
x/evm/precompile/funtoken.go 0.00% 2 Missing and 1 partial ⚠️
x/evm/precompile/wasm.go 0.00% 2 Missing and 1 partial ⚠️
x/evm/precompile/errors.go 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2060      +/-   ##
==========================================
- Coverage   65.13%   65.06%   -0.08%     
==========================================
  Files         269      269              
  Lines       17007    17029      +22     
==========================================
+ Hits        11078    11080       +2     
- Misses       5000     5013      +13     
- Partials      929      936       +7     
Files with missing lines Coverage Δ
x/evm/precompile/errors.go 61.90% <50.00%> (-2.81%) ⬇️
x/evm/precompile/funtoken.go 42.22% <0.00%> (-1.46%) ⬇️
x/evm/precompile/wasm.go 55.47% <0.00%> (-1.25%) ⬇️
x/evm/precompile/wasm_parse.go 53.19% <0.00%> (-4.95%) ⬇️

@Unique-Divine Unique-Divine merged commit 1588744 into main Oct 3, 2024
13 checks passed
@Unique-Divine Unique-Divine deleted the ud/evm-wasm branch October 3, 2024 12:32
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