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): make conditional balance check free in gas cost #2114

Merged
merged 2 commits into from
Nov 18, 2024

Conversation

k-yang
Copy link
Member

@k-yang k-yang commented Nov 18, 2024

Purpose / Abstract

We recently experienced another consensus failure on devnet-3. It happened again with the gas_used value in the last_results block. It's likely due to parallel calls to ApplyEvmMsg thrashing the StateDB pointer on the NibiruBankKeeper.

In order to ensure that all nodes calculate the same gas_used value, we make the conditional path cost zero gas, which shouldn't be a major issue because there's only a tiny db read in that path.

Summary by CodeRabbit

  • New Features

    • Implemented WASM light clients on IBC.
    • Added fun token creation fee validation.
    • Introduced validation for WASM multi-message execution.
    • Provided support and template for Nibiru EVM development.
  • Bug Fixes

    • Improved ERC20 metadata handling during FunToken creation.
    • Enhanced gas fee calculations and consumption handling.
    • Fixed state consistency in precompile execution.
    • Resolved issues with ERC20 transfers returning false success values.
  • Documentation

    • Updated CHANGELOG.md to reflect recent changes and improvements.

@k-yang k-yang requested a review from a team as a code owner November 18, 2024 16:35
Copy link
Contributor

coderabbitai bot commented Nov 18, 2024

Walkthrough

The pull request updates the CHANGELOG.md to document recent changes in the Nibiru EVM project, including new features, bug fixes, and improvements. Key updates include the implementation of WASM light clients, enhancements to ERC20 metadata handling, and adjustments to gas fee calculations. Additionally, modifications to the NibiruBankKeeper structure in bank_extension.go improve gas cost handling during state synchronization while retaining existing functionality for coin management methods.

Changes

File Path Change Summary
CHANGELOG.md Updated to reflect new features, bug fixes, and improvements, including WASM light clients, ERC20 metadata handling, gas fee calculations, and state consistency fixes.
x/evm/keeper/bank_extension.go Modified SyncStateDBWithAccount to handle gas configuration, added logic for gas cost management, and retained core functionality of coin management methods.

Possibly related PRs

Suggested reviewers

  • Unique-Divine
  • onikonychev

Poem

In the land of code where bunnies hop,
Changes abound, we can’t let them stop!
With gas costs tamed and tokens that cheer,
Our Nibiru EVM shines bright, oh dear!
Hopping through features, fixes in sight,
Let’s celebrate progress, it feels so right! 🐇✨


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

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.68%. Comparing base (04ccbcf) to head (ca60566).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2114   +/-   ##
=======================================
  Coverage   64.67%   64.68%           
=======================================
  Files         273      273           
  Lines       21605    21610    +5     
=======================================
+ Hits        13973    13978    +5     
  Misses       6655     6655           
  Partials      977      977           
Files with missing lines Coverage Δ
x/evm/keeper/bank_extension.go 86.36% <100.00%> (+0.82%) ⬆️
---- 🚨 Try these New Features:

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 (1)
CHANGELOG.md (1)

109-109: Consider improving the changelog entry format and detail.

While the entry correctly captures the change, consider:

  1. Indenting the line to match other entries in the section
  2. Adding more context about the impact, e.g.:
-- [#2114](https://github.com/NibiruChain/nibiru/pull/2114) - fix(evm): make gas cost zero in conditional bank keeper flow
+  - [#2114](https://github.com/NibiruChain/nibiru/pull/2114) - fix(evm): make gas cost zero in conditional bank keeper flow to improve consensus reliability by eliminating gas calculation discrepancies
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 04ccbcf and ca60566.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • x/evm/keeper/bank_extension.go (2 hunks)
🔇 Additional comments (5)
x/evm/keeper/bank_extension.go (4)

4-4: LGTM: Import addition aligns with PR objectives

The addition of storetypes import is necessary for implementing zero-cost gas configuration.


89-92: LGTM: Proper gas config restoration pattern

The implementation correctly caches and restores the original gas configuration using defer, ensuring cleanup even in error cases.


94-95: Verify thread safety of context modifications

While making the balance check free in gas cost addresses the consensus failure, we should verify that modifying the context's gas config doesn't introduce race conditions during parallel execution of ApplyEvmMsg.

#!/bin/bash
# Search for other places where KVGasConfig is modified to ensure no conflicts
rg -A 3 "WithKVGasConfig|KVGasConfig\(\)" --type go

# Look for parallel execution patterns
ast-grep --pattern 'go func() { $$$ ApplyEvmMsg $$$ }'

88-95: Architecture Impact: Verify gas accounting across transaction types

The zero-cost balance check affects all coin management operations that involve EVM balances. While this change resolves the immediate consensus issue, we should verify that:

  1. Gas accounting remains accurate for complex transactions involving multiple coin operations
  2. The total gas used still properly reflects the computational cost of transactions
✅ Verification successful

Based on the search results, I can now provide a final response:

Gas accounting is correctly handled for EVM balance operations

The zero-cost balance check in SyncStateDBWithAccount is safe because:

  1. It's only triggered for EVM denom balance changes, controlled by findEtherBalanceChangeFromCoins
  2. The gas cost elimination is properly scoped within a deferred context restoration
  3. The overall transaction gas accounting is maintained through ApplyEvmMsg which:
    • Tracks intrinsic gas costs
    • Handles gas refunds according to EIP-3529
    • Properly syncs gas usage with the block gas meter
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for gas accounting patterns in transaction handling
rg -A 5 "GasMeter|ConsumeGas|gasUsed" --type go

# Look for transaction types that might be affected
ast-grep --pattern 'type $_Tx struct { $$$ }'

Length of output: 46838


Script:

#!/bin/bash
# Search for gas accounting in EVM transactions and state sync
rg -A 5 "SyncStateDBWithAccount|ApplyEvmMsg|ApplyMessage" --type go

# Look for gas consumption in bank operations
rg -A 5 "MintCoins|BurnCoins|SendCoins.*SyncStateDBWithAccount" --type go

Length of output: 24427

CHANGELOG.md (1)

109-109: LGTM! Changelog entry accurately reflects the PR purpose.

The changelog entry "fix(evm): make gas cost zero in conditional bank keeper flow" properly documents the change to make conditional balance check free in gas cost, which aligns with the PR objectives to address consensus failures related to gas calculations.

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.

2 participants