From 4845380050c55929c0f9309e20552e1412c4a0d2 Mon Sep 17 00:00:00 2001 From: James Kim Date: Thu, 25 Jul 2024 11:41:16 -0400 Subject: [PATCH] fix(anvil): remove override for block.basefee when building transaction env (#8517) * remove override for block.basefee * disable basefee check for env * add tests * fix --- crates/anvil/src/eth/backend/mem/mod.rs | 9 ++-- crates/anvil/tests/it/api.rs | 65 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 429cefcae834..804ef74f41fa 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -1152,9 +1152,12 @@ impl Backend { // impls and providers env.cfg.disable_block_gas_limit = true; - if let Some(base) = max_fee_per_gas { - env.block.basefee = U256::from(base); - } + // The basefee should be ignored for calls against state for + // - eth_call + // - eth_estimateGas + // - eth_createAccessList + // - tracing + env.cfg.disable_base_fee = true; let gas_price = gas_price.or(max_fee_per_gas).unwrap_or_else(|| { self.fees().raw_gas_price().saturating_add(MIN_SUGGESTED_PRIORITY_FEE) diff --git a/crates/anvil/tests/it/api.rs b/crates/anvil/tests/it/api.rs index 13f8200ef1fc..9e640beb908c 100644 --- a/crates/anvil/tests/it/api.rs +++ b/crates/anvil/tests/it/api.rs @@ -161,6 +161,39 @@ async fn can_get_pending_block() { assert_eq!(block.transactions.len(), 1); } +#[tokio::test(flavor = "multi_thread")] +async fn can_estimate_gas_with_undersized_max_fee_per_gas() { + let (api, handle) = spawn(NodeConfig::test()).await; + let wallet = handle.dev_wallets().next().unwrap(); + let signer: EthereumWallet = wallet.clone().into(); + + let provider = http_provider_with_signer(&handle.http_endpoint(), signer); + + api.anvil_set_auto_mine(true).await.unwrap(); + + let init_value = "toto".to_string(); + + let simple_storage_contract = + SimpleStorage::deploy(&provider, init_value.clone()).await.unwrap(); + + let undersized_max_fee_per_gas = 1_u128; + + let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap(); + let latest_block_base_fee_per_gas = latest_block.header.base_fee_per_gas.unwrap(); + + assert!(undersized_max_fee_per_gas < latest_block_base_fee_per_gas); + + let estimated_gas = simple_storage_contract + .setValue("new_value".to_string()) + .max_fee_per_gas(undersized_max_fee_per_gas) + .from(wallet.address()) + .estimate_gas() + .await + .unwrap(); + + assert!(estimated_gas > 0); +} + #[tokio::test(flavor = "multi_thread")] async fn can_call_on_pending_block() { let (api, handle) = spawn(NodeConfig::test()).await; @@ -239,6 +272,38 @@ async fn can_call_on_pending_block() { } } +#[tokio::test(flavor = "multi_thread")] +async fn can_call_with_undersized_max_fee_per_gas() { + let (api, handle) = spawn(NodeConfig::test()).await; + let wallet = handle.dev_wallets().next().unwrap(); + let signer: EthereumWallet = wallet.clone().into(); + + let provider = http_provider_with_signer(&handle.http_endpoint(), signer); + + api.anvil_set_auto_mine(true).await.unwrap(); + + let init_value = "toto".to_string(); + + let simple_storage_contract = + SimpleStorage::deploy(&provider, init_value.clone()).await.unwrap(); + + let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap(); + let latest_block_base_fee_per_gas = latest_block.header.base_fee_per_gas.unwrap(); + let undersized_max_fee_per_gas = 1_u128; + + assert!(undersized_max_fee_per_gas < latest_block_base_fee_per_gas); + + let last_sender = simple_storage_contract + .lastSender() + .max_fee_per_gas(undersized_max_fee_per_gas) + .from(wallet.address()) + .call() + .await + .unwrap() + ._0; + assert_eq!(last_sender, Address::ZERO); +} + #[tokio::test(flavor = "multi_thread")] async fn can_call_with_state_override() { let (api, handle) = spawn(NodeConfig::test()).await;