Skip to content

Commit 31cba15

Browse files
authored
chore: use alloy-evm::apply_state_overrides (#11083)
* chore: use alloy-evm::apply_state_overrides * clippy
1 parent a616c7a commit 31cba15

File tree

4 files changed

+27
-67
lines changed

4 files changed

+27
-67
lines changed

crates/anvil/src/eth/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
backend::mem::{BlockRequest, DatabaseRef, State, state},
2+
backend::mem::{BlockRequest, DatabaseRef, State},
33
sign::build_typed_transaction,
44
};
55
use crate::{
@@ -35,7 +35,7 @@ use alloy_consensus::{
3535
};
3636
use alloy_dyn_abi::TypedData;
3737
use alloy_eips::eip2718::Encodable2718;
38-
use alloy_evm::overrides::OverrideBlockHashes;
38+
use alloy_evm::overrides::{OverrideBlockHashes, apply_state_overrides};
3939
use alloy_network::{
4040
AnyRpcBlock, AnyRpcTransaction, BlockResponse, Ethereum, NetworkWallet, TransactionBuilder,
4141
TransactionResponse, eip2718::Decodable2718,
@@ -2960,7 +2960,7 @@ impl EthApi {
29602960
.with_database_at(Some(block_request), |state, mut block| {
29612961
let mut cache_db = CacheDB::new(state);
29622962
if let Some(state_overrides) = overrides.state {
2963-
state::apply_state_overrides(
2963+
apply_state_overrides(
29642964
state_overrides.into_iter().collect(),
29652965
&mut cache_db,
29662966
)?;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ use alloy_consensus::{
4242
};
4343
use alloy_eips::{eip1559::BaseFeeParams, eip4844::kzg_to_versioned_hash, eip7840::BlobParams};
4444
use alloy_evm::{
45-
Database, Evm, eth::EthEvmContext, overrides::OverrideBlockHashes, precompiles::PrecompilesMap,
45+
Database, Evm,
46+
eth::EthEvmContext,
47+
overrides::{OverrideBlockHashes, apply_state_overrides},
48+
precompiles::PrecompilesMap,
4649
};
4750
use alloy_network::{
4851
AnyHeader, AnyRpcBlock, AnyRpcHeader, AnyRpcTransaction, AnyTxEnvelope, AnyTxType,
@@ -1481,7 +1484,7 @@ impl Backend {
14811484
let (exit, out, gas, state) = {
14821485
let mut cache_db = CacheDB::new(state);
14831486
if let Some(state_overrides) = overrides.state {
1484-
state::apply_state_overrides(state_overrides.into_iter().collect(), &mut cache_db)?;
1487+
apply_state_overrides(state_overrides.into_iter().collect(), &mut cache_db)?;
14851488
}
14861489
if let Some(block_overrides) = overrides.block {
14871490
cache_db.apply_block_overrides(*block_overrides, &mut block);
@@ -1660,7 +1663,7 @@ impl Backend {
16601663

16611664
// apply state overrides before executing the transactions
16621665
if let Some(state_overrides) = state_overrides {
1663-
state::apply_state_overrides(state_overrides, &mut cache_db)?;
1666+
apply_state_overrides(state_overrides, &mut cache_db)?;
16641667
}
16651668
if let Some(block_overrides) = block_overrides {
16661669
cache_db.apply_block_overrides(block_overrides, &mut block_env);
@@ -1891,7 +1894,7 @@ impl Backend {
18911894

18921895
let mut cache_db = CacheDB::new(state);
18931896
if let Some(state_overrides) = state_overrides {
1894-
state::apply_state_overrides(state_overrides, &mut cache_db)?;
1897+
apply_state_overrides(state_overrides, &mut cache_db)?;
18951898
}
18961899
if let Some(block_overrides) = block_overrides {
18971900
cache_db.apply_block_overrides(block_overrides, &mut block);
Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
//! Support for generating the state root for memdb storage
22
3-
use crate::eth::error::BlockchainError;
43
use alloy_primitives::{Address, B256, U256, keccak256, map::HashMap};
54
use alloy_rlp::Encodable;
6-
use alloy_rpc_types::state::StateOverride;
75
use alloy_trie::{HashBuilder, Nibbles};
8-
use foundry_evm::backend::DatabaseError;
9-
use revm::{
10-
bytecode::Bytecode,
11-
database::{CacheDB, DatabaseRef, DbAccount},
12-
state::AccountInfo,
13-
};
6+
use revm::{database::DbAccount, state::AccountInfo};
147

158
pub fn build_root(values: impl IntoIterator<Item = (Nibbles, Vec<u8>)>) -> B256 {
169
let mut builder = HashBuilder::default();
@@ -68,55 +61,3 @@ pub fn trie_account_rlp(info: &AccountInfo, storage: &HashMap<U256, U256>) -> Ve
6861

6962
out
7063
}
71-
72-
/// Applies the given state overrides to the given CacheDB
73-
pub fn apply_state_overrides<D>(
74-
overrides: StateOverride,
75-
cache_db: &mut CacheDB<D>,
76-
) -> Result<(), BlockchainError>
77-
where
78-
D: DatabaseRef<Error = DatabaseError>,
79-
{
80-
for (account, account_overrides) in &overrides {
81-
let mut account_info = cache_db.basic_ref(*account)?.unwrap_or_default();
82-
83-
if let Some(nonce) = account_overrides.nonce {
84-
account_info.nonce = nonce;
85-
}
86-
if let Some(code) = &account_overrides.code {
87-
account_info.code = Some(Bytecode::new_raw(code.to_vec().into()));
88-
}
89-
if let Some(balance) = account_overrides.balance {
90-
account_info.balance = balance;
91-
}
92-
93-
cache_db.insert_account_info(*account, account_info);
94-
95-
// We ensure that not both state and state_diff are set.
96-
// If state is set, we must mark the account as "NewlyCreated", so that the old storage
97-
// isn't read from
98-
match (&account_overrides.state, &account_overrides.state_diff) {
99-
(Some(_), Some(_)) => {
100-
return Err(BlockchainError::StateOverrideError(
101-
"state and state_diff can't be used together".to_string(),
102-
));
103-
}
104-
(None, None) => (),
105-
(Some(new_account_state), None) => {
106-
cache_db.replace_account_storage(
107-
*account,
108-
new_account_state
109-
.iter()
110-
.map(|(key, value)| ((*key).into(), (*value).into()))
111-
.collect(),
112-
)?;
113-
}
114-
(None, Some(account_state_diff)) => {
115-
for (key, value) in account_state_diff {
116-
cache_db.insert_account_storage(*account, (*key).into(), (*value).into())?;
117-
}
118-
}
119-
};
120-
}
121-
Ok(())
122-
}

crates/anvil/src/eth/error.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Aggregated error type for this module
22
33
use crate::eth::pool::transactions::PoolTransaction;
4+
use alloy_evm::overrides::StateOverrideError;
45
use alloy_primitives::{B256, Bytes, SignatureError};
56
use alloy_rpc_types::BlockNumberOrTag;
67
use alloy_signer::Error as SignerError;
@@ -190,6 +191,21 @@ impl From<WalletError> for BlockchainError {
190191
}
191192
}
192193

194+
impl<E> From<StateOverrideError<E>> for BlockchainError
195+
where
196+
E: Into<Self>,
197+
{
198+
fn from(value: StateOverrideError<E>) -> Self {
199+
match value {
200+
StateOverrideError::InvalidBytecode(err) => Self::StateOverrideError(err.to_string()),
201+
StateOverrideError::BothStateAndStateDiff(addr) => Self::StateOverrideError(format!(
202+
"state and state_diff can't be used together for account {addr}",
203+
)),
204+
StateOverrideError::Database(err) => err.into(),
205+
}
206+
}
207+
}
208+
193209
/// Errors that can occur in the transaction pool
194210
#[derive(Debug, thiserror::Error)]
195211
pub enum PoolError {

0 commit comments

Comments
 (0)