Skip to content

Commit

Permalink
snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Dec 20, 2024
1 parent bbe27b9 commit 0d9e658
Show file tree
Hide file tree
Showing 25 changed files with 516 additions and 492 deletions.
11 changes: 2 additions & 9 deletions crates/cast/bin/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,8 @@ impl CallArgs {
env.cfg.disable_block_gas_limit = true;
env.block.gas_limit = U256::MAX;

let mut executor = TracingExecutor::new(
env,
fork,
evm_version,
debug,
decode_internal,
alphanet,
strategy,
);
let mut executor =
TracingExecutor::new(env, fork, evm_version, debug, decode_internal, alphanet);

let value = tx.value.unwrap_or_default();
let input = tx.inner.input.into_input().unwrap_or_default();
Expand Down
1 change: 0 additions & 1 deletion crates/cast/bin/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ impl RunArgs {
self.debug,
self.decode_internal,
alphanet,
strategy,
);
let mut env =
EnvWithHandlerCfg::new_with_spec_id(Box::new(env.clone()), executor.spec_id());
Expand Down
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub trait CheatcodeInspectorStrategyExt: CheatcodeInspectorStrategy {
}

#[derive(Debug, Default, Clone)]
pub struct EvmCheatcodeInspectorStrategy {}
pub struct EvmCheatcodeInspectorStrategy;

impl CheatcodeInspectorStrategy for EvmCheatcodeInspectorStrategy {
fn name(&self) -> &'static str {
Expand Down
13 changes: 8 additions & 5 deletions crates/chisel/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use eyre::{Result, WrapErr};
use foundry_cli::utils;
use foundry_compilers::Artifact;
use foundry_evm::{
backend::Backend, decode::decode_console_logs, executors::ExecutorBuilder,
inspectors::CheatsConfig, traces::TraceMode,
backend::Backend,
decode::decode_console_logs,
executors::ExecutorBuilder,
inspectors::{cheatcodes::strategy::EvmCheatcodeInspectorStrategy, CheatsConfig},
traces::TraceMode,
};
use solang_parser::pt::{self, CodeLocation};
use std::str::FromStr;
Expand Down Expand Up @@ -323,7 +326,7 @@ impl SessionSource {
Some(backend) => backend,
None => {
let fork = self.config.evm_opts.get_fork(&self.config.foundry_config, env.clone());
let backend = Backend::spawn(fork, strategy.new_backend_strategy());
let backend = Backend::spawn(fork, ());
self.config.backend = Some(backend.clone());
backend
}
Expand All @@ -339,15 +342,15 @@ impl SessionSource {
None,
None,
Some(self.solc.version.clone()),
strategy.new_cheatcode_inspector_strategy(),
Box::new(EvmCheatcodeInspectorStrategy),
)
.into(),
)
})
.gas_limit(self.config.evm_opts.gas_limit())
.spec(self.config.foundry_config.evm_spec_id())
.legacy_assertions(self.config.foundry_config.legacy_assertions)
.build(env, backend, strategy);
.build(env, backend, ());

// Create a [ChiselRunner] with a default balance of [U256::MAX] and
// the sender [Address::zero].
Expand Down
14 changes: 10 additions & 4 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use foundry_common::{
shell,
};
use foundry_config::{Chain, Config};
use foundry_evm::executors::strategy::{EvmExecutorStrategy, ExecutorStrategyExt};
use foundry_evm::executors::strategy::EvmExecutorStrategy;
use foundry_strategy_zksync::ZksyncExecutorStrategy;
use serde::de::DeserializeOwned;
use std::{
Expand Down Expand Up @@ -93,13 +93,19 @@ pub fn get_provider(config: &Config) -> Result<RetryProvider> {
get_provider_builder(config)?.build()
}

pub fn get_executor_strategy(config: &Config) -> Box<dyn ExecutorStrategyExt> {
#[derive(Debug, Clone, Copy)]
pub enum ExecutorStrategy {
Evm,
Zksync,
}

pub fn get_executor_strategy(config: &Config) -> ExecutorStrategy {
if config.zksync.should_compile() {
info!("using zksync strategy");
Box::new(ZksyncExecutorStrategy::default())
ExecutorStrategy::Zksync
} else {
info!("using evm strategy");
Box::new(EvmExecutorStrategy::default())
ExecutorStrategy::Evm
}
}

Expand Down
49 changes: 33 additions & 16 deletions crates/evm/core/src/backend/cow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A wrapper around `Backend` that is clone-on-write used for fuzzing.
use super::{strategy::BackendStrategyExt, BackendError, ForkInfo};
use super::{strategy::BackendStrategy, BackendError, ForkInfo};
use crate::{
backend::{
diagnostic::RevertDiagnostic, Backend, DatabaseExt, LocalForkId, RevertStateSnapshotAction,
Expand Down Expand Up @@ -36,24 +36,24 @@ use std::{borrow::Cow, collections::BTreeMap};
/// which would add significant overhead for large fuzz sets even if the Database is not big after
/// setup.
#[derive(Clone, Debug)]
pub struct CowBackend<'a> {
pub struct CowBackend<'a, S: BackendStrategy> {
/// The underlying `Backend`.
///
/// No calls on the `CowBackend` will ever persistently modify the `backend`'s state.
pub backend: Cow<'a, Backend>,
pub backend: Cow<'a, Backend<S>>,
/// Keeps track of whether the backed is already initialized
pub is_initialized: bool,
/// The [SpecId] of the current backend.
pub spec_id: SpecId,
}

impl<'a> CowBackend<'a> {
impl<'a, S: BackendStrategy> CowBackend<'a, S> {
/// Creates a new `CowBackend` with the given `Backend`.
pub fn new(backend: &'a Backend) -> Self {
pub fn new(backend: &'a Backend<S>) -> Self {
Self { backend: Cow::Borrowed(backend), is_initialized: false, spec_id: SpecId::LATEST }
}

pub fn new_borrowed(backend: &'a Backend) -> Self {
pub fn new_borrowed(backend: &'a Backend<S>) -> Self {
Self { backend: Cow::Borrowed(backend), is_initialized: false, spec_id: SpecId::LATEST }
}

Expand All @@ -67,7 +67,7 @@ impl<'a> CowBackend<'a> {
/// Returns a mutable instance of the Backend.
///
/// If this is the first time this is called, the backed is cloned and initialized.
fn backend_mut(&mut self, env: &Env) -> &mut Backend {
fn backend_mut(&mut self, env: &Env) -> &mut Backend<S> {
if !self.is_initialized {
let backend = self.backend.to_mut();
let env = EnvWithHandlerCfg::new_with_spec_id(Box::new(env.clone()), self.spec_id);
Expand All @@ -79,23 +79,19 @@ impl<'a> CowBackend<'a> {
}

/// Returns a mutable instance of the Backend if it is initialized.
fn initialized_backend_mut(&mut self) -> Option<&mut Backend> {
fn initialized_backend_mut(&mut self) -> Option<&mut Backend<S>> {
if self.is_initialized {
return Some(self.backend.to_mut())
}
None
}
}

impl DatabaseExt for CowBackend<'_> {
impl<S: BackendStrategy> DatabaseExt for CowBackend<'_, S> {
fn get_fork_info(&mut self, id: LocalForkId) -> eyre::Result<ForkInfo> {
self.backend.to_mut().get_fork_info(id)
}

fn get_strategy(&mut self) -> &mut dyn BackendStrategyExt {
self.backend.to_mut().strategy.as_mut()
}

fn snapshot_state(&mut self, journaled_state: &JournaledState, env: &Env) -> U256 {
self.backend_mut(env).snapshot_state(journaled_state, env)
}
Expand Down Expand Up @@ -262,9 +258,30 @@ impl DatabaseExt for CowBackend<'_> {
fn get_test_contract_address(&self) -> Option<Address> {
self.backend.get_test_contract_address()
}

fn insert_account_info(&mut self, address: Address, account: AccountInfo) {
self.backend.to_mut().insert_account_info(address, account);
}

fn insert_account_storage(
&mut self,
address: Address,
slot: U256,
value: U256,
) -> Result<(), DatabaseError> {
self.backend.to_mut().insert_account_storage(address, slot, value)
}

fn replace_account_storage(
&mut self,
address: Address,
storage: Map<U256, U256>,
) -> Result<(), DatabaseError> {
self.backend.to_mut().replace_account_storage(address, storage)
}
}

impl DatabaseRef for CowBackend<'_> {
impl<S: BackendStrategy> DatabaseRef for CowBackend<'_, S> {
type Error = DatabaseError;

fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Expand All @@ -284,7 +301,7 @@ impl DatabaseRef for CowBackend<'_> {
}
}

impl Database for CowBackend<'_> {
impl<S: BackendStrategy> Database for CowBackend<'_, S> {
type Error = DatabaseError;

fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Expand All @@ -304,7 +321,7 @@ impl Database for CowBackend<'_> {
}
}

impl DatabaseCommit for CowBackend<'_> {
impl<S: BackendStrategy> DatabaseCommit for CowBackend<'_, S> {
fn commit(&mut self, changes: Map<Address, Account>) {
self.backend.to_mut().commit(changes)
}
Expand Down
Loading

0 comments on commit 0d9e658

Please sign in to comment.