Skip to content

Commit

Permalink
feat(anvil): add --disable-block-gas-limit (#4324)
Browse files Browse the repository at this point in the history
* feat(anvil): add --disable-block-gas-limit

* extend backwards compat serialize
  • Loading branch information
mattsse authored Feb 10, 2023
1 parent 249538f commit 25fcfef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
21 changes: 21 additions & 0 deletions anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ impl NodeArgs {

NodeConfig::default()
.with_gas_limit(self.evm_opts.gas_limit)
.disable_block_gas_limit(self.evm_opts.disable_block_gas_limit)
.with_gas_price(self.evm_opts.gas_price)
.with_hardfork(self.hardfork)
.with_blocktime(self.block_time.map(Duration::from_secs))
Expand Down Expand Up @@ -427,6 +428,16 @@ pub struct AnvilEvmArgs {
#[clap(long, value_name = "GAS_LIMIT", help_heading = "Environment config")]
pub gas_limit: Option<u64>,

/// Disable the `call.gas_limit <= block.gas_limit` constraint.
#[clap(
long,
value_name = "DISABLE_GAS_LIMIT",
help_heading = "Environment config",
alias = "disable-gas-limit",
conflicts_with = "gas_limit"
)]
pub disable_block_gas_limit: bool,

/// EIP-170: Contract code size limit in bytes. Useful to increase this because of tests. By
/// default, it is 0x6000 (~25kb).
#[clap(long, value_name = "CODE_SIZE", help_heading = "Environment config")]
Expand Down Expand Up @@ -654,4 +665,14 @@ mod tests {
let args: NodeArgs = NodeArgs::parse_from(["anvil", "--prune-history", "100"]);
assert_eq!(args.prune_history, Some(Some(100)));
}

#[test]
fn can_parse_disable_block_gas_limit() {
let args: NodeArgs = NodeArgs::parse_from(["anvil", "--disable-block-gas-limit"]);
assert!(args.evm_opts.disable_block_gas_limit);

let args =
NodeArgs::try_parse_from(["anvil", "--disable-block-gas-limit", "--gas-limit", "100"]);
assert!(args.is_err());
}
}
13 changes: 13 additions & 0 deletions anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub struct NodeConfig {
pub chain_id: Option<u64>,
/// Default gas limit for all txs
pub gas_limit: U256,
/// If set to `true`, disables the block gas limit
pub disable_block_gas_limit: bool,
/// Default gas price for all txs
pub gas_price: Option<U256>,
/// Default base fee
Expand Down Expand Up @@ -339,6 +341,7 @@ impl Default for NodeConfig {
Self {
chain_id: None,
gas_limit: U256::from(30_000_000),
disable_block_gas_limit: false,
gas_price: None,
hardfork: None,
signer_accounts: genesis_accounts.clone(),
Expand Down Expand Up @@ -446,6 +449,15 @@ impl NodeConfig {
self
}

/// Disable block gas limit check
///
/// If set to `true` block gas limit will not be enforced
#[must_use]
pub fn disable_block_gas_limit(mut self, disable_block_gas_limit: bool) -> Self {
self.disable_block_gas_limit = disable_block_gas_limit;
self
}

/// Sets the gas price
#[must_use]
pub fn with_gas_price<U: Into<U256>>(mut self, gas_price: Option<U>) -> Self {
Expand Down Expand Up @@ -747,6 +759,7 @@ impl NodeConfig {
// 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.
disable_eip3607: true,
disable_block_gas_limit: self.disable_block_gas_limit,
..Default::default()
},
block: BlockEnv {
Expand Down
20 changes: 20 additions & 0 deletions anvil/tests/it/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,23 @@ async fn test_reject_gas_too_low() {
let err = resp.unwrap_err().to_string();
assert!(err.contains("intrinsic gas too low"));
}

// <https://github.com/foundry-rs/foundry/issues/3783>
#[tokio::test(flavor = "multi_thread")]
async fn can_call_with_high_gas_limit() {
let (_api, handle) =
spawn(NodeConfig::test().with_gas_limit(Some(U256::from(100_000_000)))).await;
let provider = handle.http_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));

let greeter_contract = Greeter::deploy(Arc::clone(&client), "Hello World!".to_string())
.unwrap()
.send()
.await
.unwrap();

let greeting = greeter_contract.greet().gas(60_000_000u64).call().await.unwrap();
assert_eq!("Hello World!", greeting);
}
3 changes: 2 additions & 1 deletion evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ revm = { version = "2.3", default-features = false, features = [
"k256",
"with-serde",
"memory_limit",
"optional_eip3607"
"optional_eip3607",
"optional_block_gas_limit"
] }

# Fuzzer
Expand Down
7 changes: 7 additions & 0 deletions evm/src/executor/fork/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ impl<'de> Deserialize<'de> for BlockchainDbMeta {
if !obj.contains_key(key) {
obj.insert(key.to_string(), true.into());
}
// additional field `disable_block_gas_limit` enabled by the
// `optional_block_gas_limit` feature
let key = "disable_block_gas_limit";
if !obj.contains_key(key) {
// keep default value
obj.insert(key.to_string(), false.into());
}
}

let cfg_env: revm::CfgEnv =
Expand Down

0 comments on commit 25fcfef

Please sign in to comment.