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

feat(forge): --disable-block-gas-limit flag #7287

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/common/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ pub struct EnvArgs {
#[arg(long, value_name = "MEMORY_LIMIT")]
#[serde(skip_serializing_if = "Option::is_none")]
pub memory_limit: Option<u64>,

/// Whether to disable the block gas limit checks.
#[arg(long, visible_alias = "no-gas-limit")]
pub disable_block_gas_limit: bool,
}

impl EvmArgs {
Expand Down
4 changes: 4 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ pub struct Config {
/// Useful for more correct gas accounting and EVM behavior in general.
pub isolate: bool,

/// Whether to disable the block gas limit.
pub disable_block_gas_limit: bool,

/// Address labels
pub labels: HashMap<Address, String>,

Expand Down Expand Up @@ -1889,6 +1892,7 @@ impl Default for Config {
block_difficulty: 0,
block_prevrandao: Default::default(),
block_gas_limit: None,
disable_block_gas_limit: false,
memory_limit: 1 << 27, // 2**27 = 128MiB = 134_217_728 bytes
eth_rpc_url: None,
eth_rpc_jwt: None,
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/core/src/fork/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub async fn environment<P: TempProvider>(
override_chain_id: Option<u64>,
pin_block: Option<u64>,
origin: Address,
disable_block_gas_limit: bool,
) -> eyre::Result<(Env, Block)> {
let block_number = if let Some(pin_block) = pin_block {
pin_block
Expand Down Expand Up @@ -55,6 +56,7 @@ pub async fn environment<P: TempProvider>(
// If EIP-3607 is enabled it can cause issues during fuzz/invariant tests if the caller
// is a contract. So we disable the check by default.
cfg.disable_eip3607 = true;
cfg.disable_block_gas_limit = disable_block_gas_limit;

let mut env = Env {
cfg,
Expand Down
5 changes: 5 additions & 0 deletions crates/evm/core/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub struct EvmOpts {

/// Whether to enable isolation of calls.
pub isolate: bool,

/// Whether to disable block gas limit checks.
pub disable_block_gas_limit: bool,
}

impl EvmOpts {
Expand Down Expand Up @@ -96,6 +99,7 @@ impl EvmOpts {
self.env.chain_id,
self.fork_block_number,
self.sender,
self.disable_block_gas_limit,
)
.await
.wrap_err_with(|| {
Expand All @@ -114,6 +118,7 @@ impl EvmOpts {
// If EIP-3607 is enabled it can cause issues during fuzz/invariant tests if the
// caller is a contract. So we disable the check by default.
cfg.disable_eip3607 = true;
cfg.disable_block_gas_limit = self.disable_block_gas_limit;

revm::primitives::Env {
block: BlockEnv {
Expand Down
10 changes: 7 additions & 3 deletions crates/evm/evm/src/inspectors/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,13 @@ impl InspectorStack {
data.env.tx.value = value;
data.env.tx.nonce = Some(nonce);
// Add 21000 to the gas limit to account for the base cost of transaction.
// We might have modified block gas limit earlier and revm will reject tx with gas limit >
// block gas limit, so we adjust.
data.env.tx.gas_limit = std::cmp::min(gas_limit + 21000, data.env.block.gas_limit.to());
data.env.tx.gas_limit = gas_limit + 21000;
// If we haven't disabled gas limit checks, ensure that transaction gas limit will not
// exceed block gas limit.
if !data.env.cfg.disable_block_gas_limit {
data.env.tx.gas_limit =
std::cmp::min(data.env.tx.gas_limit, data.env.block.gas_limit.to());
}
data.env.tx.gas_price = U256::ZERO;

self.inner_context_data = Some(InnerContextData {
Expand Down
1 change: 1 addition & 0 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
block_difficulty: 10,
block_prevrandao: B256::random(),
block_gas_limit: Some(100u64.into()),
disable_block_gas_limit: false,
memory_limit: 1 << 27,
eth_rpc_url: Some("localhost".to_string()),
eth_rpc_jwt: None,
Expand Down
36 changes: 36 additions & 0 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,39 @@ contract TransientTest is Test {

cmd.args(["test", "-vvvv", "--isolate", "--evm-version", "cancun"]).assert_success();
});

forgetest_init!(can_disable_block_gas_limit, |prj, cmd| {
prj.wipe_contracts();

let endpoint = rpc::next_http_archive_rpc_endpoint();

prj.add_test(
"Contract.t.sol",
&r#"pragma solidity 0.8.24;
import {Test} from "forge-std/Test.sol";

contract C is Test {}

contract GasWaster {
function waste() public {
for (uint256 i = 0; i < 100; i++) {
new C();
}
}
}

contract GasLimitTest is Test {
function test() public {
vm.createSelectFork("<rpc>");

GasWaster waster = new GasWaster();
waster.waste();
}
}
"#
.replace("<rpc>", &endpoint),
)
.unwrap();

cmd.args(["test", "-vvvv", "--isolate", "--disable-block-gas-limit"]).assert_success();
});
Loading