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): ensure stateDB is nil after tx execution #2173

Merged
merged 4 commits into from
Jan 21, 2025

Conversation

k-yang
Copy link
Member

@k-yang k-yang commented Jan 21, 2025

Purpose / Abstract

Use the defer pattern to ensure that StateDB is nil after tx execution, by coupling the nil-clearing operation next to StateDB creation.

Copy link
Contributor

coderabbitai bot commented Jan 21, 2025

📝 Walkthrough

Walkthrough

This pull request addresses state database management in the Nibiru EVM by introducing a consistent approach to clearing the StateDB between calls. The changes span multiple files in the x/evm package, focusing on adding deferred functions to reset the StateDB to nil after transaction processing. This ensures that the state database does not retain residual data from previous operations, potentially preventing inconsistencies in subsequent transactions across various methods like EthereumTx, CreateFunToken, and precompiled contract executions.

Changes

File Change Summary
CHANGELOG.md Added entry fix(evm): clear StateDB between calls referencing PR #2173
x/evm/keeper/funtoken_from_coin.go Added deferred function to reset k.Bank.StateDB to nil after method execution
x/evm/keeper/funtoken_from_erc20.go Added deferred function to reset k.Bank.StateDB to nil after function execution
x/evm/keeper/msg_server.go Added deferred reset of k.Bank.StateDB in multiple methods
x/evm/precompile/funtoken.go Removed direct assignment of StateDB from startResult
x/evm/precompile/wasm.go Removed line setting StateDB for Bank
x/evm/embeds/artifacts/contracts/TestPrecompileSendToBankThenERC20Transfer.sol/TestPrecompileSendToBankThenERC20Transfer.json Introduced new JSON artifact for TestPrecompileSendToBankThenERC20Transfer contract
x/evm/embeds/contracts/TestPrecompileSendToBankThenERC20Transfer.sol Added new contract TestPrecompileSendToBankThenERC20Transfer with functions and constructor
x/evm/embeds/embeds.go Added embedded JSON variable and smart contract instance for TestPrecompileSendToBankThenERC20Transfer
x/evm/keeper/funtoken_from_coin_test.go Added new test TestPrecompileSendToBankThenErc20Transfer and modified existing tests for state validation

Possibly related PRs

Suggested reviewers

  • Unique-Divine

Poem

🐰 StateDB Dancing, Clean and Bright
Clearing memories with deferred might
No traces left from calls before
Each transaction starts pure at its core
A rabbit's magic, reset so light! 🧹✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1bfdf06 and 89b8c6f.

⛔ Files ignored due to path filters (1)
  • x/evm/embeds/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • x/evm/embeds/artifacts/contracts/TestPrecompileSendToBankThenERC20Transfer.sol/TestPrecompileSendToBankThenERC20Transfer.json (1 hunks)
  • x/evm/embeds/contracts/TestPrecompileSendToBankThenERC20Transfer.sol (1 hunks)
  • x/evm/embeds/embeds.go (3 hunks)
  • x/evm/keeper/funtoken_from_coin_test.go (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • x/evm/embeds/artifacts/contracts/TestPrecompileSendToBankThenERC20Transfer.sol/TestPrecompileSendToBankThenERC20Transfer.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: integration-tests
🔇 Additional comments (9)
x/evm/embeds/contracts/TestPrecompileSendToBankThenERC20Transfer.sol (3)

1-6: LGTM! Imports and license are correctly specified.

The contract correctly imports OpenZeppelin's IERC20 interface and the local IFunToken interface.


7-14: LGTM! Contract state and constructor are well implemented.

The contract correctly:

  • Stores the ERC20 token address and recipient
  • Initializes state variables in the constructor

16-24: LGTM! Attack function implements the test scenario correctly.

The function:

  1. Retrieves the contract's entire balance
  2. Sends it to the recipient using sendToBank
  3. Attempts a transfer that should fail
    This effectively tests the state management during transaction execution.
x/evm/embeds/embeds.go (3)

48-49: LGTM! Embedded JSON variable follows the established pattern.

The new embedded JSON variable is correctly declared using the go:embed directive.


154-158: LGTM! Smart contract instance is properly declared.

The new smart contract instance follows the existing pattern and is correctly initialized.


177-177: LGTM! Contract is properly initialized.

The new contract is correctly loaded in the init function.

x/evm/keeper/funtoken_from_coin_test.go (3)

608-608: LGTM! Properly initialize EVM object.

The EVM object is correctly initialized before use.

Also applies to: 623-623


676-692: LGTM! Test documentation is clear and comprehensive.

The test documentation clearly outlines:

  • Initial state
  • Contract call sequence
  • Expected outcomes

693-772: LGTM! Test implementation is thorough and well-structured.

The test:

  1. Sets up initial state correctly
  2. Deploys and funds the test contract
  3. Executes the test scenario
  4. Verifies balances after execution
  5. Ensures proper error handling

The implementation aligns with the PR objective of ensuring proper state management.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@k-yang k-yang marked this pull request as ready for review January 21, 2025 19:52
@k-yang k-yang requested a review from a team as a code owner January 21, 2025 19:52
@k-yang k-yang added P: Urgent / High prio x: evm Relates to Nibiru EVM or the EVM Module labels Jan 21, 2025
@k-yang k-yang self-assigned this Jan 21, 2025
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

🧹 Nitpick comments (1)
CHANGELOG.md (1)

87-88: Consider enhancing the changelog entry.

While the entry correctly documents the change, it could be more informative by explaining why clearing the StateDB between calls is important (e.g., to prevent residual state from affecting subsequent transactions).

Consider expanding the entry to:

-- [#2173](https://github.com/NibiruChain/nibiru/pull/2173) - fix(evm): clear `StateDB` between calls
++ [#2173](https://github.com/NibiruChain/nibiru/pull/2173) - fix(evm): clear `StateDB` between calls to prevent residual state from affecting subsequent transactions
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dc91b74 and 1bfdf06.

📒 Files selected for processing (6)
  • CHANGELOG.md (1 hunks)
  • x/evm/keeper/funtoken_from_coin.go (1 hunks)
  • x/evm/keeper/funtoken_from_erc20.go (1 hunks)
  • x/evm/keeper/msg_server.go (3 hunks)
  • x/evm/precompile/funtoken.go (0 hunks)
  • x/evm/precompile/wasm.go (0 hunks)
💤 Files with no reviewable changes (2)
  • x/evm/precompile/funtoken.go
  • x/evm/precompile/wasm.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: integration-tests
🔇 Additional comments (5)
x/evm/keeper/funtoken_from_erc20.go (1)

135-137: LGTM! Good use of defer pattern.

The deferred function ensures that StateDB is properly reset to nil after execution, regardless of the execution path. This is a robust way to handle cleanup.

x/evm/keeper/msg_server.go (3)

63-65: LGTM! Consistent state management.

The deferred reset of StateDB ensures proper cleanup after Ethereum transaction execution.


548-554: LGTM! Proper state cleanup.

The pattern of checking for nil, initializing if needed, and deferring the reset is well implemented.


602-608: LGTM! Consistent implementation.

The same state management pattern is correctly applied here, maintaining consistency across the codebase.

x/evm/keeper/funtoken_from_coin.go (1)

101-103: LGTM! Consistent with other changes.

The deferred reset of StateDB follows the same pattern implemented across the codebase.

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.97%. Comparing base (dc91b74) to head (89b8c6f).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2173      +/-   ##
==========================================
+ Coverage   64.88%   64.97%   +0.08%     
==========================================
  Files         277      277              
  Lines       22301    22363      +62     
==========================================
+ Hits        14471    14530      +59     
+ Misses       6839     6838       -1     
- Partials      991      995       +4     
Files with missing lines Coverage Δ
x/evm/embeds/embeds.go 76.47% <100.00%> (+0.71%) ⬆️
x/evm/keeper/funtoken_from_coin.go 81.92% <100.00%> (+0.22%) ⬆️
x/evm/keeper/funtoken_from_erc20.go 80.48% <100.00%> (+0.15%) ⬆️
x/evm/keeper/msg_server.go 76.99% <100.00%> (-0.46%) ⬇️
x/evm/precompile/funtoken.go 52.97% <ø> (-0.28%) ⬇️
x/evm/precompile/wasm.go 63.49% <ø> (-0.95%) ⬇️

... and 1 file with indirect coverage changes

@Unique-Divine Unique-Divine enabled auto-merge (squash) January 21, 2025 21:07
@Unique-Divine Unique-Divine merged commit 0e3ab43 into main Jan 21, 2025
15 checks passed
@Unique-Divine Unique-Divine deleted the fix/evm/statedb-nil branch January 21, 2025 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P: Urgent / High prio x: evm Relates to Nibiru EVM or the EVM Module
Projects
Status: ✅ Completed
Development

Successfully merging this pull request may close these issues.

2 participants