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

Crasher on validation of args for eth_estimateGas #527

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Sep 6, 2024

Closes: #525

Description

Add nil check for transaction Value field.


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • New Features

    • Enhanced validation logic to prevent transactions from being sent to invalid addresses, improving overall transaction safety.
  • Bug Fixes

    • Added safeguards against nil values in transaction arguments to prevent potential runtime errors.
  • Tests

    • Introduced new test cases to validate handling of transactions with nil values, ensuring robust error messaging and validation coverage.

@m-Peter m-Peter added this to the Flow-EVM-M2 milestone Sep 6, 2024
@m-Peter m-Peter self-assigned this Sep 6, 2024
Copy link
Contributor

coderabbitai bot commented Sep 6, 2024

Walkthrough

The changes enhance the validation logic in the TransactionArgs struct's Validate method to prevent nil pointer dereferences. A new test case is added to check the handling of a nil To field, ensuring that transactions attempting to create contracts with empty code are correctly deemed invalid. This improves the robustness and reliability of the transaction validation process.

Changes

Files Change Summary
api/models.go, api/models_test.go, models/transaction_test.go Enhanced validation logic in TransactionArgs.Validate to check for nil values before comparisons and added test cases for scenarios with nil To field.

Assessment against linked issues

Objective Addressed Explanation
Crasher on validation of args (#525)

🐰 "In the code where errors did creep,
A safeguard now helps us leap!
With checks for nil, we dance with glee,
Validations strong, as strong can be!
Hooray for tests that catch the flaws,
A happy rabbit gives you applause!" 🐇✨


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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 04ba8f7 and cb9f8a0.

Files selected for processing (3)
  • api/models.go (1 hunks)
  • api/models_test.go (1 hunks)
  • models/transaction_test.go (1 hunks)

Comment on lines +83 to +94
"create empty contract (nil value)": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: nil,
Value: nil,
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &hexutil.Bytes{},
},
valid: false,
errMsg: "transaction will create a contract with empty code",
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Test case for nil value handling is well-implemented.

The test case "create empty contract (nil value)" is correctly set up to validate the handling of transactions with nil values for the To field. This aligns with the PR objectives to enhance stability by preventing nil pointer dereferences.

Consider adding more test cases to cover different scenarios where other fields might also be nil to ensure comprehensive validation coverage.

Would you like me to help draft additional test cases?

@@ -63,7 +63,7 @@ func (txArgs TransactionArgs) Validate() error {
// e.g. https://github.com/onflow/go-ethereum/issues/16106.
if txDataLen == 0 {
// Prevent sending ether into black hole (show stopper)
if txArgs.Value.ToInt().Cmp(big.NewInt(0)) > 0 {
if txArgs.Value != nil && txArgs.Value.ToInt().Cmp(big.NewInt(0)) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Critical nil check added to prevent runtime errors.

The addition of a nil check before comparing the transaction value is a crucial improvement. This change effectively prevents a potential nil pointer dereference, enhancing the robustness of the validation logic.

Consider adding a specific unit test to verify this new validation logic under various scenarios where Value might be nil or not nil.

Would you like me to help draft this unit test?

Comment on lines +334 to +347
"create empty contract (nil value)": {
tx: gethTypes.NewTx(
&gethTypes.LegacyTx{
Nonce: 1,
To: nil,
Value: nil,
Gas: 53_000,
GasPrice: big.NewInt(0),
Data: []byte{},
},
),
valid: false,
errMsg: "transaction will create a contract with empty code",
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Test case for nil value handling is well-implemented.

The test case "create empty contract (nil value)" is correctly set up to validate the handling of transactions with nil values for the To field. This aligns with the PR objectives to enhance stability by preventing nil pointer dereferences.

Consider adding more test cases to cover different scenarios where other fields might also be nil to ensure comprehensive validation coverage.

Would you like me to help draft additional test cases?

Copy link
Contributor

@sideninja sideninja left a comment

Choose a reason for hiding this comment

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

Nice work

@sideninja sideninja merged commit 5d58ddb into main Sep 6, 2024
2 checks passed
@m-Peter m-Peter deleted the fix-estimate-gas-crasher branch September 6, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Crasher on validation of args
2 participants