@@ -7,7 +7,7 @@ use crate::{
77} ;
88use alloy_consensus:: TxEnvelope ;
99use alloy_genesis:: { Genesis , GenesisAccount } ;
10- use alloy_primitives:: { Address , B256 , U256 } ;
10+ use alloy_primitives:: { Address , Bytes , B256 , U256 } ;
1111use alloy_rlp:: Decodable ;
1212use alloy_sol_types:: SolValue ;
1313use foundry_common:: fs:: { read_json_file, write_json_file} ;
@@ -18,13 +18,12 @@ use foundry_evm_core::{
1818} ;
1919use foundry_evm_traces:: StackSnapshotType ;
2020use rand:: Rng ;
21- use revm:: primitives:: { Account , SpecId } ;
21+ use revm:: primitives:: { Account , Bytecode , SpecId , KECCAK_EMPTY } ;
2222use std:: {
2323 collections:: { btree_map:: Entry , BTreeMap } ,
2424 fmt:: Display ,
2525 path:: Path ,
2626} ;
27-
2827mod record_debug_step;
2928use record_debug_step:: { convert_call_trace_to_debug_step, flatten_call_trace} ;
3029use serde:: Serialize ;
@@ -133,8 +132,7 @@ impl Cheatcode for addrCall {
133132impl 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 {
420418impl 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 {
442441impl 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
484488impl 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
492500impl 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
499515impl 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
507532impl 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
0 commit comments