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: out of gas on ZetaClient during onRevert #3144

Merged
merged 11 commits into from
Nov 13, 2024
Merged

Conversation

lumtis
Copy link
Member

@lumtis lumtis commented Nov 12, 2024

Description

Use 7M gas instead of 4M when processing a revert outbound.

To test the change, the onRevert hook on ZetaChain testDApp consume 300k more gas

To check the change is effective:
Revert back this line: https://github.com/zeta-chain/node/pull/3144/files#diff-503de1565f57cda7a6a71c86db258cf0b5eb1a346eb009991b2f2ea1d30df70eR50
The v2EthWithdrawAndRevertTest will fail because of the current issue (outbound not observed)

Summary by CodeRabbit

  • New Features

    • Introduced a new constructor and function to the TestDAppV2 contract to manage ZetaChain status.
    • Enhanced contract deployment logic with a new boolean parameter to indicate ZetaChain deployment.
    • Added checks to validate if the deployed contract is part of the ZetaChain.
  • Bug Fixes

    • Updated gas limit for PostVoteOutboundRevertGasLimit to accommodate potential high gas consumption.
  • Tests

    • Modified test cases to utilize the updated contract deployment function with the new boolean parameter.

@lumtis lumtis added the V2_TESTS Run make start-v2-test label Nov 12, 2024
Copy link
Contributor

coderabbitai bot commented Nov 12, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request primarily involve modifications to the deployment logic of the TestDAppV2 contract across multiple files. Key updates include the addition of a boolean parameter to the DeployTestDAppV2 function, which influences the deployment behavior regarding the ZetaChain context. New checks and constructors have been introduced to ascertain the contract's status on the ZetaChain. Additionally, the gas limit for outbound revert operations has been increased, reflecting the need for higher gas consumption during contract interactions.

Changes

File Path Change Summary
e2e/e2etests/test_deploy_contract.go Updated deployZEVMTestDApp and deployEVMTestDApp functions to include a boolean parameter in the call to DeployTestDAppV2.
e2e/runner/v2_setup_evm.go Modified SetupEVMV2 function to add a boolean parameter for DeployTestDAppV2 and implemented a check for ZetaChain status using IsZetaChain.
e2e/runner/v2_setup_zeta.go Updated SetZEVMContractsV2 method to include a boolean parameter for DeployTestDAppV2 and added a check for ZetaChain status.
pkg/contracts/testdappv2/TestDAppV2.abi Added a new constructor and function isZetaChain to the TestDAppV2 contract's ABI.
pkg/contracts/testdappv2/TestDAppV2.go Updated DeployTestDAppV2 method to include a boolean parameter and added methods for isZetaChain functionality.
pkg/contracts/testdappv2/TestDAppV2.json Updated ABI to include a new constructor and function isZetaChain.
pkg/contracts/testdappv2/TestDAppV2.sol Introduced a new state variable isZetaChain, modified the constructor, and added a consumeGas function to simulate gas usage.
x/fungible/keeper/v2_deposits_test.go Updated deployTestDAppV2 function to include a boolean parameter affecting contract deployment in tests.
zetaclient/zetacore/constant.go Updated PostVoteOutboundRevertGasLimit constant from 4_000_000 to 7_000_000 to accommodate higher gas consumption needs.

Possibly related issues

Possibly related PRs

Suggested labels

breaking:cli, PERFORMANCE_TESTS, UPGRADE_TESTS

Suggested reviewers

  • fbac
  • kingpinXD
  • ws4charlie
  • brewmaster012
  • swift1337
  • skosito

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.

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.

@lumtis lumtis linked an issue Nov 12, 2024 that may be closed by this pull request
@lumtis lumtis added the no-changelog Skip changelog CI check label Nov 12, 2024
Copy link

codecov bot commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 0% with 16 lines in your changes missing coverage. Please review.

Project coverage is 63.13%. Comparing base (edebb7a) to head (d09760f).
Report is 6 commits behind head on develop.

Files with missing lines Patch % Lines
pkg/contracts/testdappv2/TestDAppV2.go 0.00% 16 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3144      +/-   ##
===========================================
- Coverage    63.19%   63.13%   -0.06%     
===========================================
  Files          423      423              
  Lines        29887    29880       -7     
===========================================
- Hits         18886    18865      -21     
- Misses       10163    10177      +14     
  Partials       838      838              
Files with missing lines Coverage Δ
pkg/contracts/testdappv2/TestDAppV2.go 0.00% <0.00%> (ø)

... and 2 files with indirect coverage changes

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

🧹 Outside diff range and nitpick comments (10)
zetaclient/zetacore/constant.go (1)

49-50: Enhance documentation with gas consumption breakdown

Consider adding a more detailed comment explaining:

  1. The breakdown of gas consumption for different operations during onRevert
  2. Why 7M gas is necessary (specific operations and their costs)
  3. Any chain-specific considerations
-	// The value is set to 7M because in case of onRevert call, it might consume lot of gas
+	// Gas limit breakdown for onRevert operations:
+	// - Base operation: ~4M gas
+	// - Additional operations in testDApp: ~300K gas
+	// - Buffer for complex operations: ~2.7M gas
+	// Total: 7M gas to ensure sufficient capacity for all onRevert scenarios
e2e/e2etests/test_deploy_contract.go (3)

48-48: Consider adding a named parameter for clarity.

The boolean parameter's purpose isn't immediately clear from the context. Consider using a named parameter for better code readability.

-    true,
+    isZetaChain: true,

68-68: Consider adding a named parameter for clarity.

Similar to the ZEVM deployment, the boolean parameter's purpose should be made explicit.

-    false,
+    isZetaChain: false,

47-49: Document the significance of the isZetaChain parameter.

The new boolean parameter appears to be crucial for determining the deployment context, but its significance and impact on gas consumption (particularly for onRevert operations) isn't documented. Consider adding comments explaining:

  1. The purpose of this parameter
  2. Its relationship to the gas consumption fix
  3. The expected behavior differences between ZetaChain and non-ZetaChain deployments

Add documentation above both deployment functions:

// deployZEVMTestDApp deploys the TestDAppV2 contract on ZetaChain
// with ZetaChain-specific configurations enabled, including adjusted
// gas limits for operations like onRevert.
// deployEVMTestDApp deploys the TestDAppV2 contract on Ethereum
// with standard EVM configurations, using default gas limits
// for contract operations.

Also applies to: 67-69

e2e/runner/v2_setup_evm.go (1)

119-122: Consider improving the verification organization and documentation.

While the verification logic is correct, consider these improvements:

  1. Add a more descriptive comment explaining why this check is crucial (e.g., "Verify contract is deployed in non-ZetaChain context").
  2. Consider moving this verification block closer to the TestDAppV2 deployment check for better code organization.
 	ensureTxReceipt(txTestDAppV2, "TestDAppV2 deployment failed")
+
+	// Verify contract is deployed in non-ZetaChain context
+	isZetaChain, err := r.TestDAppV2EVM.IsZetaChain(&bind.CallOpts{})
+	require.NoError(r, err)
+	require.False(r, isZetaChain)

 	// whitelist the ERC20
 	txWhitelist, err := r.ERC20CustodyV2.Whitelist(r.EVMAuth, r.ERC20Addr)
pkg/contracts/testdappv2/TestDAppV2.abi (1)

157-169: LGTM: Chain context identification function.

The isZetaChain view function provides a clean way to determine the chain context, which is essential for proper gas management and cross-chain operations.

Consider documenting the implications of this chain identification mechanism in the contract's documentation, particularly how it affects gas consumption patterns across different chains.

x/fungible/keeper/v2_deposits_test.go (1)

Line range hint 1-300: Consider adding test cases for gas consumption scenarios.

While the existing test cases cover various deposit scenarios, they don't explicitly test the gas consumption during onRevert operations. Consider adding test cases that:

  1. Verify the behavior with different gas limits
  2. Test the onRevert hook with the increased gas limit
  3. Validate the behavior when gas consumption exceeds the previous 4M limit but is within the new 7M limit

Would you like me to help draft these additional test cases?

pkg/contracts/testdappv2/TestDAppV2.sol (2)

15-15: Adjust Modifier Order for Consistency with Solidity Style Guide

Consider reordering the modifiers for isZetaChain to enhance readability and adhere to Solidity conventions:

- bool immutable public isZetaChain;
+ bool public immutable isZetaChain;

147-161: Refine Gas Consumption Logic in consumeGas()

While the current implementation achieves the goal of consuming gas, consider refining the method to account for variations in gas costs and to optimize performance.

Consider using a dynamic gas consumption loop:

function consumeGas() internal {
    uint256 startGas = gasleft();
    while (startGas - gasleft() < 500000) {
        // Empty loop to consume gas
    }
}

This approach consumes approximately 500,000 gas without relying on specific gas costs for storage operations, making it more robust against changes in gas pricing.

pkg/contracts/testdappv2/TestDAppV2.go (1)

Some invocations of DeployTestDAppV2 are missing the new isZetaChain_ parameter. Please update all function calls to include this parameter to prevent build failures.

  • e2e/e2etests/test_deploy_contract.go: Multiple invocations of DeployTestDAppV2 without the isZetaChain_ parameter.
🔗 Analysis chain

Line range hint 67-76: Update all calls to DeployTestDAppV2 with the new parameter

The function DeployTestDAppV2 now includes an additional parameter isZetaChain_. Ensure that all invocations of this function throughout the codebase are updated to include this parameter to prevent build failures.

Run the following script to identify all calls to DeployTestDAppV2 that may require updates:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all usages of 'DeployTestDAppV2' in Go files.

# Search for 'DeployTestDAppV2(' across the codebase.
rg --type go 'DeployTestDAppV2\('

Length of output: 659

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 67-67: pkg/contracts/testdappv2/TestDAppV2.go#L67
Added line #L67 was not covered by tests

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between edebb7a and d09760f.

⛔ Files ignored due to path filters (1)
  • pkg/contracts/testdappv2/TestDAppV2.bin is excluded by !**/*.bin
📒 Files selected for processing (9)
  • e2e/e2etests/test_deploy_contract.go (2 hunks)
  • e2e/runner/v2_setup_evm.go (3 hunks)
  • e2e/runner/v2_setup_zeta.go (3 hunks)
  • pkg/contracts/testdappv2/TestDAppV2.abi (2 hunks)
  • pkg/contracts/testdappv2/TestDAppV2.go (4 hunks)
  • pkg/contracts/testdappv2/TestDAppV2.json (3 hunks)
  • pkg/contracts/testdappv2/TestDAppV2.sol (4 hunks)
  • x/fungible/keeper/v2_deposits_test.go (1 hunks)
  • zetaclient/zetacore/constant.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
e2e/e2etests/test_deploy_contract.go (1)

Pattern **/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

e2e/runner/v2_setup_evm.go (1)

Pattern **/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

e2e/runner/v2_setup_zeta.go (1)

Pattern **/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

pkg/contracts/testdappv2/TestDAppV2.go (1)

Pattern **/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

x/fungible/keeper/v2_deposits_test.go (1)

Pattern **/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

zetaclient/zetacore/constant.go (1)

Pattern **/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

🪛 GitHub Check: codecov/patch
pkg/contracts/testdappv2/TestDAppV2.go

[warning] 67-67: pkg/contracts/testdappv2/TestDAppV2.go#L67
Added line #L67 was not covered by tests


[warning] 76-76: pkg/contracts/testdappv2/TestDAppV2.go#L76
Added line #L76 was not covered by tests


[warning] 414-420: pkg/contracts/testdappv2/TestDAppV2.go#L414-L420
Added lines #L414 - L420 were not covered by tests


[warning] 422-424: pkg/contracts/testdappv2/TestDAppV2.go#L422-L424
Added lines #L422 - L424 were not covered by tests


[warning] 431-432: pkg/contracts/testdappv2/TestDAppV2.go#L431-L432
Added lines #L431 - L432 were not covered by tests


[warning] 438-439: pkg/contracts/testdappv2/TestDAppV2.go#L438-L439
Added lines #L438 - L439 were not covered by tests

🔇 Additional comments (14)
zetaclient/zetacore/constant.go (1)

49-50: Verify the increased gas limit value

While increasing the gas limit addresses the immediate out-of-gas issue, the jump from 4M to 7M gas seems disproportionate to the stated 300K additional gas consumption. Let's verify this value.

e2e/runner/v2_setup_zeta.go (2)

6-6: LGTM: Required import for contract interaction.

The addition of the bind package is necessary for the new contract verification functionality.


78-81: LGTM: Robust contract verification.

The added verification ensures that TestDAppV2 is correctly deployed as a ZetaChain contract. The error handling is comprehensive and follows Go best practices.

e2e/runner/v2_setup_evm.go (2)

7-7: LGTM: Import addition is appropriate.

The bind package import is correctly placed and necessary for the new contract interaction functionality.


105-105: LGTM: TestDAppV2 deployment modification is correct.

The deployment call has been properly updated to include the new isZetaChain parameter, with appropriate error handling.

pkg/contracts/testdappv2/TestDAppV2.abi (1)

2-12: LGTM: Constructor implementation with chain context.

The constructor addition with isZetaChain_ parameter is well-structured and follows ABI standards.

Let's verify the constructor usage across the codebase:

✅ Verification successful

LGTM: Constructor implementation with chain context.

The isZetaChain_ parameter is correctly passed in all instances of DeployTestDAppV2, ensuring consistent contract deployment across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for TestDAppV2 deployments to ensure the boolean parameter is properly passed
rg -A 3 "DeployTestDAppV2" 

Length of output: 1605

x/fungible/keeper/v2_deposits_test.go (1)

54-54: Verify the impact of the true parameter on gas consumption.

The change to pass true to DeployContract appears related to the PR's objective of addressing gas consumption issues. However, the test cases don't explicitly verify the gas consumption behavior during onRevert operations.

Let's verify the gas consumption implications:

✅ Verification successful

Verified: The true parameter enables gas consumption testing during onRevert

The change to pass true to DeployContract is intentional and aligns with the test's purpose of verifying gas consumption behavior during onRevert operations. The TestDAppV2 contract is specifically designed to consume gas during reverts when deployed on ZetaChain, helping validate the system's handling of gas-intensive revert scenarios.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for gas-related configurations and tests
rg -A 5 "gasLimit|onRevert" 

# Look for related test cases
ast-grep --pattern 'func Test$_.*Revert.*\(.*\) {
  $$$
}'

Length of output: 191163

pkg/contracts/testdappv2/TestDAppV2.json (3)

3-13: LGTM: Constructor properly handles ZetaChain context.

The constructor follows Solidity best practices with:

  • Appropriate parameter naming convention using underscore suffix
  • Proper state mutability modifier

158-170: LGTM: isZetaChain function implementation is correct.

The function follows best practices:

  • View modifier for gas optimization
  • Boolean return type matches the context check requirement

319-319: Verify bytecode size impact on deployment.

The contract bytecode is substantial. While this is expected given the contract's functionality, it's worth noting that:

  1. Large contract sizes can impact deployment costs
  2. Some networks have contract size limits
pkg/contracts/testdappv2/TestDAppV2.sol (3)

9-10: Initialization of storageArray for Gas Simulation is Appropriate

The addition of storageArray to simulate gas consumption aligns with the intended functionality.


47-49: Constructor Update Correctly Initializes isZetaChain

The constructor appropriately sets the isZetaChain immutable variable based on the deployment context.


127-129: Verify Additional Gas Consumption Does Not Exceed Block Gas Limit

The invocation of consumeGas() within onRevert serves the purpose of increasing gas consumption for testing. Ensure that this additional gas usage does not cause transactions to exceed the block gas limit, which could result in unintended reverts.

pkg/contracts/testdappv2/TestDAppV2.go (1)

54-55: Verify the accuracy of the updated ABI and binary data

The ABI and Bin fields in TestDAppV2MetaData have been updated to reflect the new constructor and methods. Please confirm that these changes correctly represent the latest contract modifications.

e2e/runner/v2_setup_zeta.go Show resolved Hide resolved
pkg/contracts/testdappv2/TestDAppV2.go Show resolved Hide resolved
@lumtis lumtis added this pull request to the merge queue Nov 13, 2024
Merged via the queue into develop with commit 5012d28 Nov 13, 2024
48 of 50 checks passed
@lumtis lumtis deleted the fix/gas-onrevert branch November 13, 2024 09:12
@CryptoFewka CryptoFewka mentioned this pull request Nov 13, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog Skip changelog CI check V2_TESTS Run make start-v2-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase gas limit used by ZetaClient to handle outbound revert
3 participants