Skip to content

Commit d3cb0fc

Browse files
committed
Merge branch 'main' into upstream-59f354c
2 parents 824e42d + ddf8b75 commit d3cb0fc

File tree

34 files changed

+1282
-646
lines changed

34 files changed

+1282
-646
lines changed

crates/cheatcodes/src/config.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use super::Result;
2-
use crate::{
3-
strategy::{CheatcodeInspectorStrategy, EvmCheatcodeInspectorStrategy},
4-
Vm::Rpc,
5-
};
2+
use crate::{strategy::CheatcodeInspectorStrategy, Vm::Rpc};
63
use alloy_primitives::{map::AddressHashMap, U256};
74
use foundry_common::{fs::normalize_path, ContractsByArtifact};
85
use foundry_compilers::{utils::canonicalize, ProjectPathsConfig};
@@ -57,7 +54,7 @@ pub struct CheatsConfig {
5754
/// Version of the script/test contract which is currently running.
5855
pub running_version: Option<Version>,
5956
/// The behavior strategy.
60-
pub strategy: Box<dyn CheatcodeInspectorStrategy>,
57+
pub strategy: CheatcodeInspectorStrategy,
6158
/// Whether to enable legacy (non-reverting) assertions.
6259
pub assertions_revert: bool,
6360
/// Optional seed for the RNG algorithm.
@@ -73,7 +70,7 @@ impl CheatsConfig {
7370
available_artifacts: Option<ContractsByArtifact>,
7471
running_contract: Option<String>,
7572
running_version: Option<Version>,
76-
strategy: Box<dyn CheatcodeInspectorStrategy>,
73+
strategy: CheatcodeInspectorStrategy,
7774
) -> Self {
7875
let mut allowed_paths = vec![config.root.clone()];
7976
allowed_paths.extend(config.libs.iter().cloned());
@@ -117,7 +114,7 @@ impl CheatsConfig {
117114
self.available_artifacts.clone(),
118115
self.running_contract.clone(),
119116
self.running_version.clone(),
120-
self.strategy.new_cloned(),
117+
self.strategy.clone(),
121118
)
122119
}
123120

@@ -246,7 +243,7 @@ impl Default for CheatsConfig {
246243
available_artifacts: Default::default(),
247244
running_contract: Default::default(),
248245
running_version: Default::default(),
249-
strategy: Box::new(EvmCheatcodeInspectorStrategy::default()),
246+
strategy: CheatcodeInspectorStrategy::new_evm(),
250247
assertions_revert: true,
251248
seed: None,
252249
}
@@ -265,7 +262,7 @@ mod tests {
265262
None,
266263
None,
267264
None,
268-
Box::new(EvmCheatcodeInspectorStrategy::default()),
265+
CheatcodeInspectorStrategy::new_evm(),
269266
)
270267
}
271268

crates/cheatcodes/src/evm.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Cheatcode for getNonce_0Call {
134134
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
135135
let Self { account } = self;
136136

137-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_get_nonce(ccx, *account))
137+
ccx.state.strategy.runner.clone().cheatcode_get_nonce(ccx, *account)
138138
}
139139
}
140140

@@ -420,7 +420,7 @@ impl Cheatcode for getBlobhashesCall {
420420
impl Cheatcode for rollCall {
421421
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
422422
let Self { newHeight } = self;
423-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_roll(ccx, *newHeight))
423+
ccx.state.strategy.runner.clone().cheatcode_roll(ccx, *newHeight)
424424
}
425425
}
426426

@@ -442,7 +442,7 @@ impl Cheatcode for txGasPriceCall {
442442
impl Cheatcode for warpCall {
443443
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
444444
let Self { newTimestamp } = self;
445-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_warp(ccx, *newTimestamp))
445+
ccx.state.strategy.runner.clone().cheatcode_warp(ccx, *newTimestamp)
446446
}
447447
}
448448

@@ -477,40 +477,38 @@ impl Cheatcode for dealCall {
477477
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
478478
let Self { account: address, newBalance: new_balance } = *self;
479479

480-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_deal(ccx, address, new_balance))
480+
ccx.state.strategy.runner.clone().cheatcode_deal(ccx, address, new_balance)
481481
}
482482
}
483483

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

488-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_etch(ccx, *target, newRuntimeBytecode))
488+
ccx.state.strategy.runner.clone().cheatcode_etch(ccx, *target, newRuntimeBytecode)
489489
}
490490
}
491491

492492
impl Cheatcode for resetNonceCall {
493493
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
494494
let Self { account } = self;
495-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_reset_nonce(ccx, *account))
495+
ccx.state.strategy.runner.clone().cheatcode_reset_nonce(ccx, *account)
496496
}
497497
}
498498

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

503-
ccx.with_strategy(|strategy, ccx| strategy.cheatcode_set_nonce(ccx, account, newNonce))
503+
ccx.state.strategy.runner.clone().cheatcode_set_nonce(ccx, account, newNonce)
504504
}
505505
}
506506

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

511-
ccx.with_strategy(|strategy, ccx| {
512-
strategy.cheatcode_set_nonce_unsafe(ccx, account, newNonce)
513-
})
511+
ccx.state.strategy.runner.clone().cheatcode_set_nonce_unsafe(ccx, account, newNonce)
514512
}
515513
}
516514

crates/cheatcodes/src/evm/fork.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ impl Cheatcode for selectForkCall {
125125
persist_caller(ccx);
126126
check_broadcast(ccx.state)?;
127127

128-
ccx.with_strategy(|strategy, ccx| strategy.zksync_select_fork_vm(ccx.ecx, *forkId));
128+
ccx.state.strategy.runner.zksync_select_fork_vm(
129+
ccx.state.strategy.context.as_mut(),
130+
ccx.ecx,
131+
*forkId,
132+
);
129133
ccx.ecx.db.select_fork(*forkId, &mut ccx.ecx.env, &mut ccx.ecx.journaled_state)?;
130134

131135
Ok(Default::default())
@@ -281,7 +285,11 @@ fn create_select_fork(ccx: &mut CheatsCtxt, url_or_alias: &str, block: Option<u6
281285

282286
let fork = create_fork_request(ccx, url_or_alias, block)?;
283287
let id = ccx.ecx.db.create_fork(fork)?;
284-
ccx.with_strategy(|strategy, ccx| strategy.zksync_select_fork_vm(ccx.ecx, id));
288+
ccx.state.strategy.runner.zksync_select_fork_vm(
289+
ccx.state.strategy.context.as_mut(),
290+
ccx.ecx,
291+
id,
292+
);
285293
ccx.ecx.db.select_fork(id, &mut ccx.ecx.env, &mut ccx.ecx.journaled_state)?;
286294
Ok(id.abi_encode())
287295
}

crates/cheatcodes/src/evm/mock.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ impl Cheatcode for clearMockedCallsCall {
1515
impl Cheatcode for mockCall_0Call {
1616
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
1717
let Self { callee, data, returnData } = self;
18-
ccx.with_strategy(|strategy, ccx| {
19-
strategy.cheatcode_mock_call(ccx, *callee, data, returnData)
20-
})
18+
ccx.state.strategy.runner.clone().cheatcode_mock_call(ccx, *callee, data, returnData)
2119
}
2220
}
2321

@@ -85,9 +83,7 @@ impl Cheatcode for mockCalls_1Call {
8583
impl Cheatcode for mockCallRevert_0Call {
8684
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
8785
let Self { callee, data, revertData } = self;
88-
ccx.with_strategy(|strategy, ccx| {
89-
strategy.cheatcode_mock_call_revert(ccx, *callee, data, revertData)
90-
})
86+
ccx.state.strategy.runner.clone().cheatcode_mock_call_revert(ccx, *callee, data, revertData)
9187
}
9288
}
9389

crates/cheatcodes/src/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl Cheatcode for getArtifactPathByDeployedCodeCall {
283283
impl Cheatcode for getCodeCall {
284284
fn apply(&self, state: &mut Cheatcodes) -> Result {
285285
let Self { artifactPath: path } = self;
286-
state.with_strategy(|strategy, state| strategy.get_artifact_code(state, path, false))
286+
state.strategy.runner.get_artifact_code(state, path, false)
287287
}
288288
}
289289

crates/cheatcodes/src/inspector.rs

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub use utils::CommonCreateInput;
6969

7070
pub type Ecx<'a, 'b, 'c> = &'a mut EvmContext<&'b mut (dyn DatabaseExt + 'c)>;
7171
pub type InnerEcx<'a, 'b, 'c> = &'a mut InnerEvmContext<&'b mut (dyn DatabaseExt + 'c)>;
72-
pub type Strategy<'a> = &'a mut dyn CheatcodeInspectorStrategy;
7372

7473
/// Helper trait for obtaining complete [revm::Inspector] instance from mutable reference to
7574
/// [Cheatcodes].
@@ -528,7 +527,7 @@ pub struct Cheatcodes {
528527
pub wallets: Option<Wallets>,
529528

530529
/// The behavior strategy.
531-
pub strategy: Option<Box<dyn CheatcodeInspectorStrategy>>,
530+
pub strategy: CheatcodeInspectorStrategy,
532531
}
533532

534533
impl Clone for Cheatcodes {
@@ -568,7 +567,7 @@ impl Clone for Cheatcodes {
568567
arbitrary_storage: self.arbitrary_storage.clone(),
569568
deprecated: self.deprecated.clone(),
570569
wallets: self.wallets.clone(),
571-
strategy: self.strategy.as_ref().map(|s| s.new_cloned()),
570+
strategy: self.strategy.clone(),
572571
}
573572
}
574573
}
@@ -588,7 +587,7 @@ impl Cheatcodes {
588587
Self {
589588
fs_commit: true,
590589
labels: config.labels.clone(),
591-
strategy: Some(config.strategy.clone()),
590+
strategy: config.strategy.clone(),
592591
config,
593592
block: Default::default(),
594593
active_delegation: Default::default(),
@@ -763,7 +762,8 @@ impl Cheatcodes {
763762
if ecx_inner.journaled_state.depth() == broadcast.depth {
764763
input.set_caller(broadcast.new_origin);
765764

766-
self.strategy.as_mut().unwrap().record_broadcastable_create_transactions(
765+
self.strategy.runner.record_broadcastable_create_transactions(
766+
self.strategy.context.as_mut(),
767767
self.config.clone(),
768768
&input,
769769
ecx_inner,
@@ -801,9 +801,9 @@ impl Cheatcodes {
801801
}]);
802802
}
803803

804-
if let Some(result) = self.with_strategy(|strategy, cheatcodes| {
805-
strategy.zksync_try_create(cheatcodes, ecx, &input, executor)
806-
}) {
804+
if let Some(result) =
805+
self.strategy.runner.clone().zksync_try_create(self, ecx, &input, executor)
806+
{
807807
return Some(result);
808808
}
809809

@@ -924,10 +924,7 @@ where {
924924
}
925925
}
926926

927-
self.strategy
928-
.as_mut()
929-
.expect("failed acquiring strategy")
930-
.zksync_record_create_address(&outcome);
927+
self.strategy.runner.zksync_record_create_address(self.strategy.context.as_mut(), &outcome);
931928

932929
outcome
933930
}
@@ -970,10 +967,12 @@ where {
970967
let prev = account.info.nonce;
971968
let nonce = prev.saturating_sub(1);
972969
account.info.nonce = nonce;
973-
self.strategy
974-
.as_mut()
975-
.expect("failed acquiring strategy")
976-
.zksync_sync_nonce(sender, nonce, ecx);
970+
self.strategy.runner.zksync_sync_nonce(
971+
self.strategy.context.as_mut(),
972+
sender,
973+
nonce,
974+
ecx,
975+
);
977976

978977
trace!(target: "cheatcodes", %sender, nonce, prev, "corrected nonce");
979978
}
@@ -1007,10 +1006,7 @@ where {
10071006
return None;
10081007
}
10091008

1010-
self.strategy
1011-
.as_mut()
1012-
.expect("failed acquiring strategy")
1013-
.zksync_set_deployer_call_input(call);
1009+
self.strategy.runner.zksync_set_deployer_call_input(self.strategy.context.as_mut(), call);
10141010

10151011
// Handle expected calls
10161012

@@ -1145,17 +1141,15 @@ where {
11451141
})
11461142
}
11471143

1148-
self.strategy
1149-
.as_mut()
1150-
.expect("failed acquiring strategy")
1151-
.record_broadcastable_call_transactions(
1152-
self.config.clone(),
1153-
call,
1154-
ecx_inner,
1155-
broadcast,
1156-
&mut self.broadcastable_transactions,
1157-
&mut self.active_delegation,
1158-
);
1144+
self.strategy.runner.record_broadcastable_call_transactions(
1145+
self.strategy.context.as_mut(),
1146+
self.config.clone(),
1147+
call,
1148+
ecx_inner,
1149+
broadcast,
1150+
&mut self.broadcastable_transactions,
1151+
&mut self.active_delegation,
1152+
);
11591153

11601154
let account =
11611155
ecx_inner.journaled_state.state().get_mut(&broadcast.new_origin).unwrap();
@@ -1228,9 +1222,9 @@ where {
12281222
}]);
12291223
}
12301224

1231-
if let Some(result) = self.with_strategy(|strategy, cheatcodes| {
1232-
strategy.zksync_try_call(cheatcodes, ecx, call, executor)
1233-
}) {
1225+
if let Some(result) =
1226+
self.strategy.runner.clone().zksync_try_call(self, ecx, call, executor)
1227+
{
12341228
return Some(result);
12351229
}
12361230

@@ -1272,17 +1266,6 @@ where {
12721266
None => false,
12731267
}
12741268
}
1275-
1276-
pub fn with_strategy<F, R>(&mut self, mut f: F) -> R
1277-
where
1278-
F: FnMut(Strategy, &mut Self) -> R,
1279-
{
1280-
let mut strategy = self.strategy.take();
1281-
let result = f(strategy.as_mut().expect("failed acquiring strategy").as_mut(), self);
1282-
self.strategy = strategy;
1283-
1284-
result
1285-
}
12861269
}
12871270

12881271
impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
@@ -1302,10 +1285,11 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
13021285
self.gas_metering.paused_frames.push(interpreter.gas);
13031286
}
13041287

1305-
self.strategy
1306-
.as_mut()
1307-
.expect("failed acquiring strategy")
1308-
.post_initialize_interp(interpreter, ecx);
1288+
self.strategy.runner.post_initialize_interp(
1289+
self.strategy.context.as_mut(),
1290+
interpreter,
1291+
ecx,
1292+
);
13091293
}
13101294

13111295
#[inline]
@@ -1350,8 +1334,7 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
13501334

13511335
#[inline]
13521336
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: Ecx) {
1353-
if self.strategy.as_mut().expect("failed acquiring strategy").pre_step_end(interpreter, ecx)
1354-
{
1337+
if self.strategy.runner.pre_step_end(self.strategy.context.as_mut(), interpreter, ecx) {
13551338
return;
13561339
}
13571340

crates/cheatcodes/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extern crate tracing;
1717

1818
use alloy_primitives::Address;
1919
use foundry_evm_core::backend::DatabaseExt;
20-
use inspector::Strategy;
2120
use revm::{ContextPrecompiles, InnerEvmContext};
2221
use spec::Status;
2322

@@ -175,15 +174,4 @@ impl CheatsCtxt<'_, '_, '_, '_> {
175174
pub(crate) fn is_precompile(&self, address: &Address) -> bool {
176175
self.precompiles.contains(address)
177176
}
178-
179-
pub(crate) fn with_strategy<F, R>(&mut self, mut f: F) -> R
180-
where
181-
F: FnMut(Strategy, &mut Self) -> R,
182-
{
183-
let mut strategy = self.state.strategy.take();
184-
let result = f(strategy.as_mut().expect("failed acquiring strategy").as_mut(), self);
185-
self.state.strategy = strategy;
186-
187-
result
188-
}
189177
}

0 commit comments

Comments
 (0)