Skip to content

Commit cb8f3bf

Browse files
authored
feat: use alloy-evm apply overrides (foundry-rs#11071)
1 parent 8439227 commit cb8f3bf

File tree

4 files changed

+10
-56
lines changed

4 files changed

+10
-56
lines changed

crates/anvil/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ foundry-evm.workspace = true
3030
foundry-evm-core.workspace = true
3131

3232
# alloy
33-
alloy-evm.workspace = true
33+
alloy-evm = { workspace = true, features = ["call-util"] }
3434
alloy-op-evm.workspace = true
3535
alloy-primitives = { workspace = true, features = ["serde"] }
3636
alloy-consensus = { workspace = true, features = ["k256", "kzg"] }

crates/anvil/src/eth/api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use alloy_consensus::{
3535
};
3636
use alloy_dyn_abi::TypedData;
3737
use alloy_eips::eip2718::Encodable2718;
38+
use alloy_evm::overrides::OverrideBlockHashes;
3839
use alloy_network::{
3940
AnyRpcBlock, AnyRpcTransaction, BlockResponse, Ethereum, NetworkWallet, TransactionBuilder,
4041
TransactionResponse, eip2718::Decodable2718,
@@ -2965,7 +2966,7 @@ impl EthApi {
29652966
)?;
29662967
}
29672968
if let Some(block_overrides) = overrides.block {
2968-
state::apply_block_overrides(*block_overrides, &mut cache_db, &mut block);
2969+
cache_db.apply_block_overrides(*block_overrides, &mut block);
29692970
}
29702971
this.do_estimate_gas_with_state(request, &cache_db as &dyn DatabaseRef, block)
29712972
})

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ use alloy_consensus::{
4141
transaction::Recovered,
4242
};
4343
use alloy_eips::{eip1559::BaseFeeParams, eip4844::kzg_to_versioned_hash, eip7840::BlobParams};
44-
use alloy_evm::{Database, Evm, eth::EthEvmContext, precompiles::PrecompilesMap};
44+
use alloy_evm::{
45+
Database, Evm, eth::EthEvmContext, overrides::OverrideBlockHashes, precompiles::PrecompilesMap,
46+
};
4547
use alloy_network::{
4648
AnyHeader, AnyRpcBlock, AnyRpcHeader, AnyRpcTransaction, AnyTxEnvelope, AnyTxType,
4749
EthereumWallet, UnknownTxEnvelope, UnknownTypedTransaction,
@@ -1482,7 +1484,7 @@ impl Backend {
14821484
state::apply_state_overrides(state_overrides.into_iter().collect(), &mut cache_db)?;
14831485
}
14841486
if let Some(block_overrides) = overrides.block {
1485-
state::apply_block_overrides(*block_overrides, &mut cache_db, &mut block);
1487+
cache_db.apply_block_overrides(*block_overrides, &mut block);
14861488
}
14871489
self.call_with_state(&cache_db as &dyn DatabaseRef, request, fee_details, block)
14881490
}?;
@@ -1661,7 +1663,7 @@ impl Backend {
16611663
state::apply_state_overrides(state_overrides, &mut cache_db)?;
16621664
}
16631665
if let Some(block_overrides) = block_overrides {
1664-
state::apply_block_overrides(block_overrides, &mut cache_db, &mut block_env);
1666+
cache_db.apply_block_overrides(block_overrides, &mut block_env);
16651667
}
16661668

16671669
// execute all calls in that block
@@ -1892,7 +1894,7 @@ impl Backend {
18921894
state::apply_state_overrides(state_overrides, &mut cache_db)?;
18931895
}
18941896
if let Some(block_overrides) = block_overrides {
1895-
state::apply_block_overrides(block_overrides, &mut cache_db, &mut block);
1897+
cache_db.apply_block_overrides(block_overrides, &mut block);
18961898
}
18971899

18981900
if let Some(tracer) = tracer {

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

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
use crate::eth::error::BlockchainError;
44
use alloy_primitives::{Address, B256, U256, keccak256, map::HashMap};
55
use alloy_rlp::Encodable;
6-
use alloy_rpc_types::{BlockOverrides, state::StateOverride};
6+
use alloy_rpc_types::state::StateOverride;
77
use alloy_trie::{HashBuilder, Nibbles};
88
use foundry_evm::backend::DatabaseError;
99
use revm::{
1010
bytecode::Bytecode,
11-
context::BlockEnv,
1211
database::{CacheDB, DatabaseRef, DbAccount},
1312
state::AccountInfo,
1413
};
@@ -121,51 +120,3 @@ where
121120
}
122121
Ok(())
123122
}
124-
125-
/// Applies the given block overrides to the env and updates overridden block hashes in the db.
126-
pub fn apply_block_overrides<DB>(
127-
overrides: BlockOverrides,
128-
cache_db: &mut CacheDB<DB>,
129-
env: &mut BlockEnv,
130-
) {
131-
let BlockOverrides {
132-
number,
133-
difficulty,
134-
time,
135-
gas_limit,
136-
coinbase,
137-
random,
138-
base_fee,
139-
block_hash,
140-
} = overrides;
141-
142-
if let Some(block_hashes) = block_hash {
143-
// override block hashes
144-
cache_db
145-
.cache
146-
.block_hashes
147-
.extend(block_hashes.into_iter().map(|(num, hash)| (U256::from(num), hash)))
148-
}
149-
150-
if let Some(number) = number {
151-
env.number = number.saturating_to();
152-
}
153-
if let Some(difficulty) = difficulty {
154-
env.difficulty = difficulty;
155-
}
156-
if let Some(time) = time {
157-
env.timestamp = U256::from(time);
158-
}
159-
if let Some(gas_limit) = gas_limit {
160-
env.gas_limit = gas_limit;
161-
}
162-
if let Some(coinbase) = coinbase {
163-
env.beneficiary = coinbase;
164-
}
165-
if let Some(random) = random {
166-
env.prevrandao = Some(random);
167-
}
168-
if let Some(base_fee) = base_fee {
169-
env.basefee = base_fee.saturating_to();
170-
}
171-
}

0 commit comments

Comments
 (0)