Skip to content

Commit e49efc2

Browse files
committed
Merge branch 'main' into upstream-59f354c
2 parents 778786c + 2a052c6 commit e49efc2

File tree

8 files changed

+274
-518
lines changed

8 files changed

+274
-518
lines changed

crates/cheatcodes/src/evm.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
};
88
use alloy_consensus::TxEnvelope;
99
use alloy_genesis::{Genesis, GenesisAccount};
10-
use alloy_primitives::{Address, B256, U256};
10+
use alloy_primitives::{Address, Bytes, B256, U256};
1111
use alloy_rlp::Decodable;
1212
use alloy_sol_types::SolValue;
1313
use foundry_common::fs::{read_json_file, write_json_file};
@@ -18,13 +18,12 @@ use foundry_evm_core::{
1818
};
1919
use foundry_evm_traces::StackSnapshotType;
2020
use rand::Rng;
21-
use revm::primitives::{Account, SpecId};
21+
use revm::primitives::{Account, Bytecode, SpecId, KECCAK_EMPTY};
2222
use std::{
2323
collections::{btree_map::Entry, BTreeMap},
2424
fmt::Display,
2525
path::Path,
2626
};
27-
2827
mod record_debug_step;
2928
use record_debug_step::{convert_call_trace_to_debug_step, flatten_call_trace};
3029
use serde::Serialize;
@@ -133,8 +132,7 @@ impl Cheatcode for addrCall {
133132
impl Cheatcode for getNonce_0Call {
134133
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
135134
let Self { account } = self;
136-
137-
ccx.state.strategy.runner.clone().cheatcode_get_nonce(ccx, *account)
135+
get_nonce(ccx, account)
138136
}
139137
}
140138

@@ -420,7 +418,8 @@ impl Cheatcode for getBlobhashesCall {
420418
impl Cheatcode for rollCall {
421419
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
422420
let Self { newHeight } = self;
423-
ccx.state.strategy.runner.clone().cheatcode_roll(ccx, *newHeight)
421+
ccx.ecx.env.block.number = *newHeight;
422+
Ok(Default::default())
424423
}
425424
}
426425

@@ -442,7 +441,8 @@ impl Cheatcode for txGasPriceCall {
442441
impl Cheatcode for warpCall {
443442
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
444443
let Self { newTimestamp } = self;
445-
ccx.state.strategy.runner.clone().cheatcode_warp(ccx, *newTimestamp)
444+
ccx.ecx.env.block.timestamp = *newTimestamp;
445+
Ok(Default::default())
446446
}
447447
}
448448

@@ -477,38 +477,65 @@ 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.state.strategy.runner.clone().cheatcode_deal(ccx, address, new_balance)
480+
let account = journaled_account(ccx.ecx, address)?;
481+
let old_balance = std::mem::replace(&mut account.info.balance, new_balance);
482+
let record = DealRecord { address, old_balance, new_balance };
483+
ccx.state.eth_deals.push(record);
484+
Ok(Default::default())
481485
}
482486
}
483487

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

488-
ccx.state.strategy.runner.clone().cheatcode_etch(ccx, *target, newRuntimeBytecode)
492+
ensure_not_precompile!(&target, ccx);
493+
ccx.ecx.load_account(*target)?;
494+
let bytecode = Bytecode::new_raw(Bytes::copy_from_slice(newRuntimeBytecode));
495+
ccx.ecx.journaled_state.set_code(*target, bytecode);
496+
Ok(Default::default())
489497
}
490498
}
491499

492500
impl Cheatcode for resetNonceCall {
493501
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
494502
let Self { account } = self;
495-
ccx.state.strategy.runner.clone().cheatcode_reset_nonce(ccx, *account)
503+
let account = journaled_account(ccx.ecx, *account)?;
504+
// Per EIP-161, EOA nonces start at 0, but contract nonces
505+
// start at 1. Comparing by code_hash instead of code
506+
// to avoid hitting the case where account's code is None.
507+
let empty = account.info.code_hash == KECCAK_EMPTY;
508+
let nonce = if empty { 0 } else { 1 };
509+
account.info.nonce = nonce;
510+
debug!(target: "cheatcodes", nonce, "reset");
511+
Ok(Default::default())
496512
}
497513
}
498514

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

503-
ccx.state.strategy.runner.clone().cheatcode_set_nonce(ccx, account, newNonce)
519+
let account = journaled_account(ccx.ecx, account)?;
520+
// nonce must increment only
521+
let current = account.info.nonce;
522+
ensure!(
523+
newNonce >= current,
524+
"new nonce ({newNonce}) must be strictly equal to or higher than the \
525+
account's current nonce ({current})"
526+
);
527+
account.info.nonce = newNonce;
528+
Ok(Default::default())
504529
}
505530
}
506531

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

511-
ccx.state.strategy.runner.clone().cheatcode_set_nonce_unsafe(ccx, account, newNonce)
536+
let account = journaled_account(ccx.ecx, account)?;
537+
account.info.nonce = newNonce;
538+
Ok(Default::default())
512539
}
513540
}
514541

crates/cheatcodes/src/evm/mock.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ 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.state.strategy.runner.clone().cheatcode_mock_call(ccx, *callee, data, returnData)
18+
let _ = make_acc_non_empty(callee, ccx.ecx)?;
19+
mock_call(ccx.state, callee, data, None, returnData, InstructionResult::Return);
20+
Ok(Default::default())
1921
}
2022
}
2123

@@ -83,7 +85,10 @@ impl Cheatcode for mockCalls_1Call {
8385
impl Cheatcode for mockCallRevert_0Call {
8486
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
8587
let Self { callee, data, revertData } = self;
86-
ccx.state.strategy.runner.clone().cheatcode_mock_call_revert(ccx, *callee, data, revertData)
88+
89+
let _ = make_acc_non_empty(callee, ccx.ecx)?;
90+
mock_call(ccx.state, callee, data, None, revertData, InstructionResult::Revert);
91+
Ok(Default::default())
8792
}
8893
}
8994

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.strategy.runner.get_artifact_code(state, path, false)
286+
Ok(get_artifact_code(state, path, false)?.abi_encode())
287287
}
288288
}
289289

crates/cheatcodes/src/inspector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,8 @@ fn apply_dispatch(
22852285
}
22862286

22872287
// Apply the cheatcode.
2288-
let mut result = cheat.dyn_apply(ccx, executor);
2288+
let runner = ccx.state.strategy.runner.new_cloned();
2289+
let mut result = runner.apply_full(cheat, ccx, executor);
22892290

22902291
// Format the error message to include the cheatcode name.
22912292
if let Err(e) = &mut result {

crates/cheatcodes/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ pub(crate) trait Cheatcode: CheatcodeDef + DynCheatcode {
9696
}
9797
}
9898

99-
pub(crate) trait DynCheatcode: 'static {
99+
pub trait DynCheatcode: 'static + std::any::Any {
100100
fn cheatcode(&self) -> &'static spec::Cheatcode<'static>;
101101

102102
fn as_debug(&self) -> &dyn std::fmt::Debug;
103103

104104
fn dyn_apply(&self, ccx: &mut CheatsCtxt, executor: &mut dyn CheatcodesExecutor) -> Result;
105+
106+
fn as_any(&self) -> &dyn std::any::Any;
105107
}
106108

107109
impl<T: Cheatcode> DynCheatcode for T {
@@ -119,6 +121,11 @@ impl<T: Cheatcode> DynCheatcode for T {
119121
fn dyn_apply(&self, ccx: &mut CheatsCtxt, executor: &mut dyn CheatcodesExecutor) -> Result {
120122
self.apply_full(ccx, executor)
121123
}
124+
125+
#[inline]
126+
fn as_any(&self) -> &dyn std::any::Any {
127+
self
128+
}
122129
}
123130

124131
impl dyn DynCheatcode {

0 commit comments

Comments
 (0)