Skip to content

Commit

Permalink
fix(anvil): remove override for block.basefee when building transacti…
Browse files Browse the repository at this point in the history
…on env (#8517)

* remove override for block.basefee

* disable basefee check for env

* add tests

* fix
  • Loading branch information
jakim929 authored Jul 25, 2024
1 parent 4e4f35c commit 4845380
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
9 changes: 6 additions & 3 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,9 +1152,12 @@ impl Backend {
// impls and providers <https://github.com/foundry-rs/foundry/issues/4388>
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)
Expand Down
65 changes: 65 additions & 0 deletions crates/anvil/tests/it/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4845380

Please # to comment.