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

bug: vm.txGasPrice cheatcode incompatible with --gas-report/--isolate option #7277

Open
2 tasks done
KirienzoEth opened this issue Feb 29, 2024 · 2 comments · May be fixed by #9941
Open
2 tasks done

bug: vm.txGasPrice cheatcode incompatible with --gas-report/--isolate option #7277

KirienzoEth opened this issue Feb 29, 2024 · 2 comments · May be fixed by #9941
Labels
A-cheatcodes Area: cheatcodes A-gas-snapshots Area: gas snapshotting/reporting C-forge Command: forge Cmd-forge-test Command: forge test P-normal Priority: normal T-bug Type: bug

Comments

@KirienzoEth
Copy link

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (2f432fb 2024-02-29T00:16:38.827810253Z)

What command(s) is the bug in?

forge test --gas-report

Operating System

Windows

Describe the bug

Example repo: https://github.com/KirienzoEth/foundry-gas-report-txgasprice-issue

The vm.txGasPrice cheatcode seems to be incompatible with the use of --gas-report in functions that use tx.gasprice.

The --gas-report option seem to be calling the contract that is tested with a new transaction (to measure the gas used I'm assuming), but as vm.txGasPrice only works in the current transaction (see foundry book), any function relying on tx.gasprice will fail.

@KirienzoEth KirienzoEth added the T-bug Type: bug label Feb 29, 2024
@KirienzoEth KirienzoEth changed the title txGasPrice cheatcode incompatible with -gas-report option txGasPrice cheatcode incompatible with --gas-report option Feb 29, 2024
@klkvr
Copy link
Member

klkvr commented Feb 29, 2024

It's happening because of this line:

ecx.env.tx.gas_price = U256::ZERO;

When removing it and running tests with --isolate as default, there is a falining test can_deploy_with_create2. It's failing because for scripts we are simply changing caller of CREATE2 deployment to DEFAULT_CREATE2_DEPLOYER if it should be broadcasted. That way, because of non-zero gas price in forking environment, revm expects create2 deployer to have enough funds to pay gas * gas_price

So we can either prefund create2 deployer along with caller when initializing test/script runner, or prefund it at apply_create2_deployer (https://github.com/foundry-rs/foundry/blob/master/crates/cheatcodes/src/inspector.rs#L1432) when it's actually being used

It is also worth noting that removing gas price overwriting might break some tests

Right now it is possible to do something like this:

vm.txGasPrice(1); // basically anything non-zero
vm.startPrank(ALICE); // prank address with zero balance
someContract.someMethod();

call to someContract will revert in isolation because revm expects ALICE to have enough funds to pay for gas

cc @mattsse @onbjerg

@zerosnacks zerosnacks added Cmd-forge-test Command: forge test C-forge Command: forge A-gas-snapshots Area: gas snapshotting/reporting A-cheatcodes Area: cheatcodes labels Jul 11, 2024
@zerosnacks zerosnacks changed the title txGasPrice cheatcode incompatible with --gas-report option bug: vm.txGasPrice cheatcode incompatible with --gas-report option Jul 11, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@grandizzy grandizzy added the P-normal Priority: normal label Oct 24, 2024
@grandizzy grandizzy changed the title bug: vm.txGasPrice cheatcode incompatible with --gas-report option bug: vm.txGasPrice cheatcode incompatible with --gas-report/--isolate option Oct 24, 2024
@github-project-automation github-project-automation bot moved this to Todo in Foundry Oct 24, 2024
@grandizzy
Copy link
Collaborator

confirming still reproducible with latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes A-gas-snapshots Area: gas snapshotting/reporting C-forge Command: forge Cmd-forge-test Command: forge test P-normal Priority: normal T-bug Type: bug
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants