Skip to content

Commit 4845380

Browse files
authored
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
1 parent 4e4f35c commit 4845380

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,9 +1152,12 @@ impl Backend {
11521152
// impls and providers <https://github.com/foundry-rs/foundry/issues/4388>
11531153
env.cfg.disable_block_gas_limit = true;
11541154

1155-
if let Some(base) = max_fee_per_gas {
1156-
env.block.basefee = U256::from(base);
1157-
}
1155+
// The basefee should be ignored for calls against state for
1156+
// - eth_call
1157+
// - eth_estimateGas
1158+
// - eth_createAccessList
1159+
// - tracing
1160+
env.cfg.disable_base_fee = true;
11581161

11591162
let gas_price = gas_price.or(max_fee_per_gas).unwrap_or_else(|| {
11601163
self.fees().raw_gas_price().saturating_add(MIN_SUGGESTED_PRIORITY_FEE)

crates/anvil/tests/it/api.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,39 @@ async fn can_get_pending_block() {
161161
assert_eq!(block.transactions.len(), 1);
162162
}
163163

164+
#[tokio::test(flavor = "multi_thread")]
165+
async fn can_estimate_gas_with_undersized_max_fee_per_gas() {
166+
let (api, handle) = spawn(NodeConfig::test()).await;
167+
let wallet = handle.dev_wallets().next().unwrap();
168+
let signer: EthereumWallet = wallet.clone().into();
169+
170+
let provider = http_provider_with_signer(&handle.http_endpoint(), signer);
171+
172+
api.anvil_set_auto_mine(true).await.unwrap();
173+
174+
let init_value = "toto".to_string();
175+
176+
let simple_storage_contract =
177+
SimpleStorage::deploy(&provider, init_value.clone()).await.unwrap();
178+
179+
let undersized_max_fee_per_gas = 1_u128;
180+
181+
let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap();
182+
let latest_block_base_fee_per_gas = latest_block.header.base_fee_per_gas.unwrap();
183+
184+
assert!(undersized_max_fee_per_gas < latest_block_base_fee_per_gas);
185+
186+
let estimated_gas = simple_storage_contract
187+
.setValue("new_value".to_string())
188+
.max_fee_per_gas(undersized_max_fee_per_gas)
189+
.from(wallet.address())
190+
.estimate_gas()
191+
.await
192+
.unwrap();
193+
194+
assert!(estimated_gas > 0);
195+
}
196+
164197
#[tokio::test(flavor = "multi_thread")]
165198
async fn can_call_on_pending_block() {
166199
let (api, handle) = spawn(NodeConfig::test()).await;
@@ -239,6 +272,38 @@ async fn can_call_on_pending_block() {
239272
}
240273
}
241274

275+
#[tokio::test(flavor = "multi_thread")]
276+
async fn can_call_with_undersized_max_fee_per_gas() {
277+
let (api, handle) = spawn(NodeConfig::test()).await;
278+
let wallet = handle.dev_wallets().next().unwrap();
279+
let signer: EthereumWallet = wallet.clone().into();
280+
281+
let provider = http_provider_with_signer(&handle.http_endpoint(), signer);
282+
283+
api.anvil_set_auto_mine(true).await.unwrap();
284+
285+
let init_value = "toto".to_string();
286+
287+
let simple_storage_contract =
288+
SimpleStorage::deploy(&provider, init_value.clone()).await.unwrap();
289+
290+
let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap();
291+
let latest_block_base_fee_per_gas = latest_block.header.base_fee_per_gas.unwrap();
292+
let undersized_max_fee_per_gas = 1_u128;
293+
294+
assert!(undersized_max_fee_per_gas < latest_block_base_fee_per_gas);
295+
296+
let last_sender = simple_storage_contract
297+
.lastSender()
298+
.max_fee_per_gas(undersized_max_fee_per_gas)
299+
.from(wallet.address())
300+
.call()
301+
.await
302+
.unwrap()
303+
._0;
304+
assert_eq!(last_sender, Address::ZERO);
305+
}
306+
242307
#[tokio::test(flavor = "multi_thread")]
243308
async fn can_call_with_state_override() {
244309
let (api, handle) = spawn(NodeConfig::test()).await;

0 commit comments

Comments
 (0)