Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Dec 22, 2024
1 parent b83bf04 commit 16d9cfa
Show file tree
Hide file tree
Showing 28 changed files with 318 additions and 247 deletions.
13 changes: 5 additions & 8 deletions crates/cheatcodes/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::Result;
use crate::{
strategy::{new_evm_strategy, Strategy},
Vm::Rpc,
};
use crate::{strategy::CheatcodeInspectorStrategy, Vm::Rpc};
use alloy_primitives::{map::AddressHashMap, U256};
use foundry_common::{fs::normalize_path, ContractsByArtifact};
use foundry_compilers::{utils::canonicalize, ProjectPathsConfig};
Expand Down Expand Up @@ -57,7 +54,7 @@ pub struct CheatsConfig {
/// Version of the script/test contract which is currently running.
pub running_version: Option<Version>,
/// The behavior strategy.
pub strategy: Strategy,
pub strategy: CheatcodeInspectorStrategy,
/// Whether to enable legacy (non-reverting) assertions.
pub assertions_revert: bool,
/// Optional seed for the RNG algorithm.
Expand All @@ -73,7 +70,7 @@ impl CheatsConfig {
available_artifacts: Option<ContractsByArtifact>,
running_contract: Option<String>,
running_version: Option<Version>,
strategy: Strategy,
strategy: CheatcodeInspectorStrategy,
) -> Self {
let mut allowed_paths = vec![config.root.0.clone()];
allowed_paths.extend(config.libs.clone());
Expand Down Expand Up @@ -234,7 +231,7 @@ impl Default for CheatsConfig {
available_artifacts: Default::default(),
running_contract: Default::default(),
running_version: Default::default(),
strategy: new_evm_strategy(),
strategy: CheatcodeInspectorStrategy::new_evm(),
assertions_revert: true,
seed: None,
}
Expand All @@ -253,7 +250,7 @@ mod tests {
None,
None,
None,
new_evm_strategy(),
CheatcodeInspectorStrategy::new_evm(),
)
}

Expand Down
16 changes: 8 additions & 8 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Cheatcode for getNonce_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { account } = self;

ccx.state.strategy.inner.clone().cheatcode_get_nonce(ccx, *account)
ccx.state.strategy.runner.clone().cheatcode_get_nonce(ccx, *account)
}
}

Expand Down Expand Up @@ -350,7 +350,7 @@ impl Cheatcode for getBlobhashesCall {
impl Cheatcode for rollCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { newHeight } = self;
ccx.state.strategy.inner.clone().cheatcode_roll(ccx, *newHeight)
ccx.state.strategy.runner.clone().cheatcode_roll(ccx, *newHeight)
}
}

Expand All @@ -372,7 +372,7 @@ impl Cheatcode for txGasPriceCall {
impl Cheatcode for warpCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { newTimestamp } = self;
ccx.state.strategy.inner.clone().cheatcode_warp(ccx, *newTimestamp)
ccx.state.strategy.runner.clone().cheatcode_warp(ccx, *newTimestamp)
}
}

Expand Down Expand Up @@ -407,38 +407,38 @@ impl Cheatcode for dealCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { account: address, newBalance: new_balance } = *self;

ccx.state.strategy.inner.clone().cheatcode_deal(ccx, address, new_balance)
ccx.state.strategy.runner.clone().cheatcode_deal(ccx, address, new_balance)
}
}

impl Cheatcode for etchCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { target, newRuntimeBytecode } = self;

ccx.state.strategy.inner.clone().cheatcode_etch(ccx, *target, newRuntimeBytecode)
ccx.state.strategy.runner.clone().cheatcode_etch(ccx, *target, newRuntimeBytecode)
}
}

impl Cheatcode for resetNonceCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { account } = self;
ccx.state.strategy.inner.clone().cheatcode_reset_nonce(ccx, *account)
ccx.state.strategy.runner.clone().cheatcode_reset_nonce(ccx, *account)
}
}

impl Cheatcode for setNonceCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { account, newNonce } = *self;

ccx.state.strategy.inner.clone().cheatcode_set_nonce(ccx, account, newNonce)
ccx.state.strategy.runner.clone().cheatcode_set_nonce(ccx, account, newNonce)
}
}

impl Cheatcode for setNonceUnsafeCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { account, newNonce } = *self;

ccx.state.strategy.inner.clone().cheatcode_set_nonce_unsafe(ccx, account, newNonce)
ccx.state.strategy.runner.clone().cheatcode_set_nonce_unsafe(ccx, account, newNonce)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Cheatcode for selectForkCall {
persist_caller(ccx);
check_broadcast(ccx.state)?;

ccx.state.strategy.inner.zksync_select_fork_vm(
ccx.state.strategy.runner.zksync_select_fork_vm(
ccx.state.strategy.context.as_mut(),
ccx.ecx,
*forkId,
Expand Down Expand Up @@ -285,7 +285,7 @@ fn create_select_fork(ccx: &mut CheatsCtxt, url_or_alias: &str, block: Option<u6

let fork = create_fork_request(ccx, url_or_alias, block)?;
let id = ccx.ecx.db.create_fork(fork)?;
ccx.state.strategy.inner.zksync_select_fork_vm(
ccx.state.strategy.runner.zksync_select_fork_vm(
ccx.state.strategy.context.as_mut(),
ccx.ecx,
id,
Expand Down
4 changes: 2 additions & 2 deletions crates/cheatcodes/src/evm/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Cheatcode for clearMockedCallsCall {
impl Cheatcode for mockCall_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { callee, data, returnData } = self;
ccx.state.strategy.inner.clone().cheatcode_mock_call(ccx, *callee, data, returnData)
ccx.state.strategy.runner.clone().cheatcode_mock_call(ccx, *callee, data, returnData)
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ impl Cheatcode for mockCalls_1Call {
impl Cheatcode for mockCallRevert_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { callee, data, revertData } = self;
ccx.state.strategy.inner.clone().cheatcode_mock_call_revert(ccx, *callee, data, revertData)
ccx.state.strategy.runner.clone().cheatcode_mock_call_revert(ccx, *callee, data, revertData)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl Cheatcode for getArtifactPathByDeployedCodeCall {
impl Cheatcode for getCodeCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { artifactPath: path } = self;
state.strategy.inner.get_artifact_code(state, path, false)
state.strategy.runner.get_artifact_code(state, path, false)
}
}

Expand Down
23 changes: 12 additions & 11 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
DealRecord, GasRecord,
},
script::{Broadcast, Wallets},
strategy::Strategy,
strategy::CheatcodeInspectorStrategy,
test::{
assume::AssumeNoRevert,
expect::{self, ExpectedEmit, ExpectedRevert, ExpectedRevertKind},
Expand Down Expand Up @@ -527,7 +527,7 @@ pub struct Cheatcodes {
pub wallets: Option<Wallets>,

/// The behavior strategy.
pub strategy: Strategy,
pub strategy: CheatcodeInspectorStrategy,
}

impl Clone for Cheatcodes {
Expand Down Expand Up @@ -762,7 +762,7 @@ impl Cheatcodes {
if ecx_inner.journaled_state.depth() == broadcast.depth {
input.set_caller(broadcast.new_origin);

self.strategy.inner.record_broadcastable_create_transactions(
self.strategy.runner.record_broadcastable_create_transactions(
self.strategy.context.as_mut(),
self.config.clone(),
&input,
Expand Down Expand Up @@ -802,7 +802,7 @@ impl Cheatcodes {
}

if let Some(result) =
self.strategy.inner.clone().zksync_try_create(self, ecx, &input, executor)
self.strategy.runner.clone().zksync_try_create(self, ecx, &input, executor)
{
return Some(result);
}
Expand Down Expand Up @@ -917,7 +917,7 @@ where {
}
}

self.strategy.inner.zksync_record_create_address(self.strategy.context.as_mut(), &outcome);
self.strategy.runner.zksync_record_create_address(self.strategy.context.as_mut(), &outcome);

outcome
}
Expand Down Expand Up @@ -960,7 +960,7 @@ where {
let prev = account.info.nonce;
let nonce = prev.saturating_sub(1);
account.info.nonce = nonce;
self.strategy.inner.zksync_sync_nonce(
self.strategy.runner.zksync_sync_nonce(
self.strategy.context.as_mut(),
sender,
nonce,
Expand Down Expand Up @@ -999,7 +999,7 @@ where {
return None;
}

self.strategy.inner.zksync_set_deployer_call_input(self.strategy.context.as_mut(), call);
self.strategy.runner.zksync_set_deployer_call_input(self.strategy.context.as_mut(), call);

// Handle expected calls

Expand Down Expand Up @@ -1133,7 +1133,7 @@ where {
})
}

self.strategy.inner.record_broadcastable_call_transactions(
self.strategy.runner.record_broadcastable_call_transactions(
self.strategy.context.as_mut(),
self.config.clone(),
call,
Expand Down Expand Up @@ -1214,7 +1214,8 @@ where {
}]);
}

if let Some(result) = self.strategy.inner.clone().zksync_try_call(self, ecx, call, executor)
if let Some(result) =
self.strategy.runner.clone().zksync_try_call(self, ecx, call, executor)
{
return Some(result);
}
Expand Down Expand Up @@ -1276,7 +1277,7 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
self.gas_metering.paused_frames.push(interpreter.gas);
}

self.strategy.inner.post_initialize_interp(
self.strategy.runner.post_initialize_interp(
self.strategy.context.as_mut(),
interpreter,
ecx,
Expand Down Expand Up @@ -1325,7 +1326,7 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {

#[inline]
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: Ecx) {
if self.strategy.inner.pre_step_end(self.strategy.context.as_mut(), interpreter, ecx) {
if self.strategy.runner.pre_step_end(self.strategy.context.as_mut(), interpreter, ecx) {
return;
}

Expand Down
46 changes: 30 additions & 16 deletions crates/cheatcodes/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ use crate::{
CheatsConfig, CheatsCtxt, Result,
};

/// Represents the context for [CheatcodeInspectorStrategy].
pub trait CheatcodeInspectorStrategyContext: Debug + Send + Sync + Any {
/// Clone the strategy context.
fn new_cloned(&self) -> Box<dyn CheatcodeInspectorStrategyContext>;
fn as_any_mut(&mut self) -> &mut dyn Any;
/// Alias as immutable reference of [Any].
fn as_any_ref(&self) -> &dyn Any;
/// Alias as mutable reference of [Any].
fn as_any_mut(&mut self) -> &mut dyn Any;
}

impl CheatcodeInspectorStrategyContext for () {
fn new_cloned(&self) -> Box<dyn CheatcodeInspectorStrategyContext> {
Box::new(*self)
Box::new(())
}

fn as_any_mut(&mut self) -> &mut dyn Any {
Expand All @@ -38,26 +42,36 @@ impl CheatcodeInspectorStrategyContext for () {
}
}

/// Represents the strategy.
#[derive(Debug)]
pub struct Strategy {
pub inner: Box<dyn CheatcodeInspectorStrategy>,
pub struct CheatcodeInspectorStrategy {
/// Strategy runner.
pub runner: Box<dyn CheatcodeInspectorStrategyRunner>,
/// Strategy context.
pub context: Box<dyn CheatcodeInspectorStrategyContext>,
}

pub fn new_evm_strategy() -> Strategy {
Strategy { inner: Box::new(EvmCheatcodeInspectorStrategy::default()), context: Box::new(()) }
impl CheatcodeInspectorStrategy {
pub fn new_evm() -> Self {
Self {
runner: Box::new(EvmCheatcodeInspectorStrategyRunner::default()),
context: Box::new(()),
}
}
}

impl Clone for Strategy {
impl Clone for CheatcodeInspectorStrategy {
fn clone(&self) -> Self {
Self { inner: self.inner.new_cloned(), context: self.context.new_cloned() }
Self { runner: self.runner.new_cloned(), context: self.context.new_cloned() }
}
}

pub trait CheatcodeInspectorStrategy: Debug + Send + Sync + CheatcodeInspectorStrategyExt {
pub trait CheatcodeInspectorStrategyRunner:
Debug + Send + Sync + CheatcodeInspectorStrategyExt
{
fn name(&self) -> &'static str;

fn new_cloned(&self) -> Box<dyn CheatcodeInspectorStrategy>;
fn new_cloned(&self) -> Box<dyn CheatcodeInspectorStrategyRunner>;

/// Get nonce.
fn get_nonce(&self, ccx: &mut CheatsCtxt, address: Address) -> Result<u64> {
Expand Down Expand Up @@ -328,14 +342,14 @@ pub trait CheatcodeInspectorStrategyExt {
}

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

impl CheatcodeInspectorStrategy for EvmCheatcodeInspectorStrategy {
impl CheatcodeInspectorStrategyRunner for EvmCheatcodeInspectorStrategyRunner {
fn name(&self) -> &'static str {
"evm"
}

fn new_cloned(&self) -> Box<dyn CheatcodeInspectorStrategy> {
fn new_cloned(&self) -> Box<dyn CheatcodeInspectorStrategyRunner> {
Box::new(self.clone())
}

Expand Down Expand Up @@ -411,13 +425,13 @@ impl CheatcodeInspectorStrategy for EvmCheatcodeInspectorStrategy {
}
}

impl CheatcodeInspectorStrategyExt for EvmCheatcodeInspectorStrategy {}
impl CheatcodeInspectorStrategyExt for EvmCheatcodeInspectorStrategyRunner {}

impl Clone for Box<dyn CheatcodeInspectorStrategy> {
impl Clone for Box<dyn CheatcodeInspectorStrategyRunner> {
fn clone(&self) -> Self {
self.new_cloned()
}
}

struct _ObjectSafe0(dyn CheatcodeInspectorStrategy);
struct _ObjectSafe0(dyn CheatcodeInspectorStrategyRunner);
struct _ObjectSafe1(dyn CheatcodeInspectorStrategyExt);
Loading

0 comments on commit 16d9cfa

Please sign in to comment.