diff --git a/crates/anvil/src/eth/api.rs b/crates/anvil/src/eth/api.rs index 4d9fbcb2f..04e4ee8fa 100644 --- a/crates/anvil/src/eth/api.rs +++ b/crates/anvil/src/eth/api.rs @@ -1702,16 +1702,7 @@ impl EthApi { node_info!("eth_feeHistory"); // max number of blocks in the requested range - let current = self.backend.best_number(); - let slots_in_an_epoch = 32u64; - - let number = match newest_block { - BlockNumber::Latest | BlockNumber::Pending => current, - BlockNumber::Earliest => 0, - BlockNumber::Number(n) => n, - BlockNumber::Safe => current.saturating_sub(slots_in_an_epoch), - BlockNumber::Finalized => current.saturating_sub(slots_in_an_epoch * 2), - }; + let number = self.backend.convert_block_number(Some(newest_block)); // check if the number predates the fork, if in fork mode if let Some(fork) = self.get_fork() { diff --git a/crates/anvil/tests/it/gas.rs b/crates/anvil/tests/it/gas.rs index cd2e6bb08..162b99b2f 100644 --- a/crates/anvil/tests/it/gas.rs +++ b/crates/anvil/tests/it/gas.rs @@ -4,7 +4,7 @@ use crate::utils::http_provider_with_signer; use alloy_network::{EthereumWallet, TransactionBuilder}; use alloy_primitives::{Address, U64, U256, uint}; use alloy_provider::Provider; -use alloy_rpc_types::{BlockId, TransactionRequest}; +use alloy_rpc_types::{BlockId, BlockNumberOrTag, TransactionRequest}; use alloy_serde::WithOtherFields; use anvil::{NodeConfig, eth::fees::INITIAL_BASE_FEE, spawn}; @@ -233,6 +233,25 @@ async fn test_can_use_fee_history() { } } +#[tokio::test(flavor = "multi_thread")] +async fn test_fee_history_safe_finalized_use_slots_in_an_epoch() { + let (api, handle) = spawn(NodeConfig::test().with_slots_in_an_epoch(3)).await; + let provider = handle.http_provider(); + + // (blocks mined, expected safe, expected finalized); finalized saturates at genesis first + for (mined, safe, finalized) in [(4, 1, 0), (10, 7, 4)] { + while api.block_number().unwrap() < U256::from(mined) { + api.mine_one().await; + } + for (tag, expected) in + [(BlockNumberOrTag::Safe, safe), (BlockNumberOrTag::Finalized, finalized)] + { + let history = provider.get_fee_history(1, tag, &[]).await.unwrap(); + assert_eq!(history.oldest_block, expected, "{tag} at block {mined}"); + } + } +} + #[tokio::test(flavor = "multi_thread")] async fn test_estimate_gas_empty_data() { let (api, handle) = spawn(NodeConfig::test()).await;