Skip to content

Commit 664809e

Browse files
committed
wip
1 parent 0a2ad00 commit 664809e

21 files changed

+811
-371
lines changed

Cargo.lock

+393-82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,17 @@ foundry-linking = { path = "crates/linking" }
189189
# solc & compilation utilities
190190
foundry-block-explorers = { version = "0.11.0", default-features = false }
191191
foundry-compilers = { version = "0.13.3", default-features = false }
192-
foundry-fork-db = "0.12"
192+
foundry-fork-db = { git = "https://github.com/foundry-rs/foundry-fork-db", rev = "e60b89c" }
193193
solang-parser = "=0.3.3"
194194
solar-parse = { version = "=0.1.1", default-features = false }
195195

196196
## revm
197-
revm = { version = "19.4.0", default-features = false }
197+
revm = { version = "20.0.0-alpha.3", default-features = false }
198198
revm-primitives = { version = "15.1.0", default-features = false }
199199
revm-inspectors = { version = "0.16.0", features = ["serde"] }
200200

201201
## alloy
202+
alloy-evm = { path = "../evm/crates/evm" }
202203
alloy-consensus = { version = "0.12.1", default-features = false }
203204
alloy-contract = { version = "0.12.1", default-features = false }
204205
alloy-eips = { version = "0.12.1", default-features = false }
@@ -316,6 +317,8 @@ vergen = { version = "8", default-features = false }
316317
yansi = { version = "1.0", features = ["detect-tty", "detect-env"] }
317318

318319
[patch.crates-io]
320+
revm = { git = "https://github.com/bluealloy/revm", rev = "9e39df5" }
321+
revm-inspectors = { git = "https://github.com/paradigmxyz/revm-inspectors", rev = "2a365e3" }
319322
## alloy-core
320323
# alloy-dyn-abi = { path = "../../alloy-rs/core/crates/dyn-abi" }
321324
# alloy-json-abi = { path = "../../alloy-rs/core/crates/json-abi" }

crates/cheatcodes/src/inspector.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use foundry_evm_core::{
3333
abi::Vm::stopExpectSafeMemoryCall,
3434
backend::{DatabaseError, DatabaseExt, RevertDiagnostic},
3535
constants::{CHEATCODE_ADDRESS, HARDHAT_CONSOLE_ADDRESS, MAGIC_ASSUME},
36-
utils::new_evm_with_existing_context,
36+
utils::new_evm_with_context,
3737
InspectorExt,
3838
};
3939
use foundry_evm_traces::{TracingInspector, TracingInspectorConfig};
@@ -161,7 +161,7 @@ where
161161
l1_block_info,
162162
};
163163

164-
let mut evm = new_evm_with_existing_context(inner, &mut *inspector);
164+
let mut evm = new_evm_with_context(inner, &mut *inspector);
165165

166166
let res = f(&mut evm)?;
167167

@@ -1368,7 +1368,7 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
13681368
outcome.result.output = error.abi_encode().into();
13691369
outcome
13701370
}
1371-
}
1371+
};
13721372
} else {
13731373
// Call didn't revert, reset `assume_no_revert` state.
13741374
self.assume_no_revert = None;
@@ -1820,7 +1820,7 @@ impl Cheatcodes {
18201820
let (key, target_address) = if interpreter.current_opcode() == op::SLOAD {
18211821
(try_or_return!(interpreter.stack().peek(0)), interpreter.contract().target_address)
18221822
} else {
1823-
return
1823+
return;
18241824
};
18251825

18261826
let Ok(value) = ecx.sload(target_address, key) else {

crates/evm/core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ foundry-common.workspace = true
1919
foundry-config.workspace = true
2020
foundry-evm-abi.workspace = true
2121

22+
alloy-evm.workspace = true
2223
alloy-dyn-abi = { workspace = true, features = ["arbitrary", "eip712"] }
2324
alloy-genesis.workspace = true
2425
alloy-json-abi.workspace = true
@@ -43,7 +44,6 @@ revm = { workspace = true, features = [
4344
"optional_block_gas_limit",
4445
"optional_no_base_fee",
4546
"arbitrary",
46-
"optimism",
4747
"c-kzg",
4848
"blst",
4949
] }

crates/evm/core/src/backend/cow.rs

+35-36
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
//! A wrapper around `Backend` that is clone-on-write used for fuzzing.
22
3-
use super::BackendError;
3+
use super::{BackendError, JournaledState};
44
use crate::{
55
backend::{
66
diagnostic::RevertDiagnostic, Backend, DatabaseExt, LocalForkId, RevertStateSnapshotAction,
7-
},
8-
fork::{CreateFork, ForkId},
9-
InspectorExt,
7+
}, fork::{CreateFork, ForkId}, AsEnvMut, Env, EnvMut, InspectorExt
108
};
119
use alloy_genesis::GenesisAccount;
1210
use alloy_primitives::{Address, B256, U256};
1311
use alloy_rpc_types::TransactionRequest;
1412
use eyre::WrapErr;
1513
use foundry_fork_db::DatabaseError;
1614
use revm::{
17-
db::DatabaseRef,
18-
primitives::{
19-
Account, AccountInfo, Bytecode, Env, EnvWithHandlerCfg, HashMap as Map, ResultAndState,
20-
SpecId,
21-
},
22-
Database, DatabaseCommit, JournaledState,
15+
context::{result::ResultAndState, JournalInit},
16+
database::DatabaseRef,
17+
primitives::{hardfork::SpecId, HashMap as Map},
18+
state::{Account, AccountInfo, Bytecode},
19+
Database, DatabaseCommit, ExecuteEvm,
2320
};
2421
use std::{borrow::Cow, collections::BTreeMap};
2522

@@ -64,7 +61,7 @@ impl<'a> CowBackend<'a> {
6461
#[instrument(name = "inspect", level = "debug", skip_all)]
6562
pub fn inspect<I: InspectorExt>(
6663
&mut self,
67-
env: &mut EnvWithHandlerCfg,
64+
env: &mut Env,
6865
inspector: &mut I,
6966
) -> eyre::Result<ResultAndState> {
7067
// this is a new call to inspect with a new env, so even if we've cloned the backend
@@ -73,9 +70,9 @@ impl<'a> CowBackend<'a> {
7370
self.spec_id = env.handler_cfg.spec_id;
7471
let mut evm = crate::utils::new_evm_with_inspector(self, env.clone(), inspector);
7572

76-
let res = evm.transact().wrap_err("EVM error")?;
73+
let res = evm.replay().wrap_err("EVM error")?;
7774

78-
env.env = evm.context.evm.inner.env;
75+
*env = evm.data.ctx.as_env_mut().to_owned();
7976

8077
Ok(res)
8178
}
@@ -90,45 +87,47 @@ impl<'a> CowBackend<'a> {
9087
/// Returns a mutable instance of the Backend.
9188
///
9289
/// If this is the first time this is called, the backed is cloned and initialized.
93-
fn backend_mut(&mut self, env: &Env) -> &mut Backend {
90+
fn backend_mut(&mut self, env: EnvMut<'_>) -> &mut Backend {
91+
let env = env.as_env_mut().to_owned();
9492
if !self.is_initialized {
9593
let backend = self.backend.to_mut();
96-
let env = EnvWithHandlerCfg::new_with_spec_id(Box::new(env.clone()), self.spec_id);
94+
let mut env = env.to_owned();
95+
env.evm_env.spec = self.spec_id;
9796
backend.initialize(&env);
9897
self.is_initialized = true;
99-
return backend
98+
return backend;
10099
}
101100
self.backend.to_mut()
102101
}
103102

104103
/// Returns a mutable instance of the Backend if it is initialized.
105104
fn initialized_backend_mut(&mut self) -> Option<&mut Backend> {
106105
if self.is_initialized {
107-
return Some(self.backend.to_mut())
106+
return Some(self.backend.to_mut());
108107
}
109108
None
110109
}
111110
}
112111

113112
impl DatabaseExt for CowBackend<'_> {
114-
fn snapshot_state(&mut self, journaled_state: &JournaledState, env: &Env) -> U256 {
113+
fn snapshot_state(&mut self, journaled_state: &JournaledState<'_>, env: &Env) -> U256 {
115114
self.backend_mut(env).snapshot_state(journaled_state, env)
116115
}
117116

118117
fn revert_state(
119118
&mut self,
120119
id: U256,
121-
journaled_state: &JournaledState,
122-
current: &mut Env,
120+
journaled_state: &JournaledState<'_>,
121+
current: EnvMut<'_>,
123122
action: RevertStateSnapshotAction,
124-
) -> Option<JournaledState> {
123+
) -> Option<JournalInit> {
125124
self.backend_mut(current).revert_state(id, journaled_state, current, action)
126125
}
127126

128127
fn delete_state_snapshot(&mut self, id: U256) -> bool {
129128
// delete state snapshot requires a previous snapshot to be initialized
130129
if let Some(backend) = self.initialized_backend_mut() {
131-
return backend.delete_state_snapshot(id)
130+
return backend.delete_state_snapshot(id);
132131
}
133132
false
134133
}
@@ -154,38 +153,38 @@ impl DatabaseExt for CowBackend<'_> {
154153
fn select_fork(
155154
&mut self,
156155
id: LocalForkId,
157-
env: &mut Env,
158-
journaled_state: &mut JournaledState,
156+
env: EnvMut<'_>,
157+
journaled_state: &mut JournaledState<'_>,
159158
) -> eyre::Result<()> {
160-
self.backend_mut(env).select_fork(id, env, journaled_state)
159+
self.backend_mut(&mut env).select_fork(id, env, journaled_state)
161160
}
162161

163162
fn roll_fork(
164163
&mut self,
165164
id: Option<LocalForkId>,
166165
block_number: u64,
167-
env: &mut Env,
168-
journaled_state: &mut JournaledState,
166+
env: EnvMut<'_>,
167+
journaled_state: &mut JournaledState<'_>,
169168
) -> eyre::Result<()> {
170-
self.backend_mut(env).roll_fork(id, block_number, env, journaled_state)
169+
self.backend_mut(env.as_env_mut()).roll_fork(id, block_number, env, journaled_state)
171170
}
172171

173172
fn roll_fork_to_transaction(
174173
&mut self,
175174
id: Option<LocalForkId>,
176175
transaction: B256,
177-
env: &mut Env,
178-
journaled_state: &mut JournaledState,
176+
mut env: EnvMut<'_>,
177+
journaled_state: &mut JournaledState<'_>,
179178
) -> eyre::Result<()> {
180-
self.backend_mut(env).roll_fork_to_transaction(id, transaction, env, journaled_state)
179+
self.backend_mut(env.as_env_mut()).roll_fork_to_transaction(id, transaction, env, journaled_state)
181180
}
182181

183182
fn transact(
184183
&mut self,
185184
id: Option<LocalForkId>,
186185
transaction: B256,
187186
env: Env,
188-
journaled_state: &mut JournaledState,
187+
journaled_state: &mut JournaledState<'_>,
189188
inspector: &mut dyn InspectorExt,
190189
) -> eyre::Result<()> {
191190
self.backend_mut(&env).transact(id, transaction, env, journaled_state, inspector)
@@ -195,7 +194,7 @@ impl DatabaseExt for CowBackend<'_> {
195194
&mut self,
196195
transaction: &TransactionRequest,
197196
env: Env,
198-
journaled_state: &mut JournaledState,
197+
journaled_state: &mut JournaledState<'_>,
199198
inspector: &mut dyn InspectorExt,
200199
) -> eyre::Result<()> {
201200
self.backend_mut(&env).transact_from_tx(transaction, env, journaled_state, inspector)
@@ -220,15 +219,15 @@ impl DatabaseExt for CowBackend<'_> {
220219
fn diagnose_revert(
221220
&self,
222221
callee: Address,
223-
journaled_state: &JournaledState,
222+
journaled_state: &JournaledState<'_>,
224223
) -> Option<RevertDiagnostic> {
225224
self.backend.diagnose_revert(callee, journaled_state)
226225
}
227226

228227
fn load_allocs(
229228
&mut self,
230229
allocs: &BTreeMap<Address, GenesisAccount>,
231-
journaled_state: &mut JournaledState,
230+
journaled_state: &mut JournaledState<'_>,
232231
) -> Result<(), BackendError> {
233232
self.backend_mut(&Env::default()).load_allocs(allocs, journaled_state)
234233
}
@@ -237,7 +236,7 @@ impl DatabaseExt for CowBackend<'_> {
237236
&mut self,
238237
source: &GenesisAccount,
239238
target: &Address,
240-
journaled_state: &mut JournaledState,
239+
journaled_state: &mut JournaledState<'_>,
241240
) -> Result<(), BackendError> {
242241
self.backend_mut(&Env::default()).clone_account(source, target, journaled_state)
243242
}

crates/evm/core/src/backend/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloy_primitives::Address;
22
pub use foundry_fork_db::{DatabaseError, DatabaseResult};
3-
use revm::primitives::EVMError;
4-
use std::convert::Infallible;
3+
use revm::context::result::EVMError;
4+
use std::{convert::Infallible, fmt::Display};
55

66
pub type BackendResult<T> = Result<T, BackendError>;
77

@@ -53,8 +53,8 @@ impl From<Infallible> for BackendError {
5353
}
5454

5555
// Note: this is mostly necessary to use some revm internals that return an [EVMError]
56-
impl<T: Into<Self>> From<EVMError<T>> for BackendError {
57-
fn from(err: EVMError<T>) -> Self {
56+
impl<T: Into<Self>, TxError: Display> From<EVMError<T, TxError>> for BackendError {
57+
fn from(err: EVMError<T, TxError>) -> Self {
5858
match err {
5959
EVMError::Database(err) => err.into(),
6060
EVMError::Custom(err) => Self::msg(err),

crates/evm/core/src/backend/in_memory_db.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::state_snapshot::StateSnapshots;
44
use alloy_primitives::{Address, B256, U256};
55
use foundry_fork_db::DatabaseError;
66
use revm::{
7-
db::{CacheDB, DatabaseRef, EmptyDB},
8-
primitives::{Account, AccountInfo, Bytecode, HashMap as Map},
7+
database::{CacheDB, DatabaseRef, EmptyDB},
8+
primitives::HashMap as Map,
9+
state::{Account, AccountInfo, Bytecode},
910
Database, DatabaseCommit,
1011
};
1112

0 commit comments

Comments
 (0)