Skip to content

Commit a4c0904

Browse files
authored
chore(cheatcodes): avoid unnecessary lookup in store (#11163)
1 parent c7c9655 commit a4c0904

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

crates/cheatcodes/src/error.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::Vm;
2-
use alloy_primitives::{Address, Bytes, hex};
2+
use alloy_primitives::{Bytes, hex};
33
use alloy_signer::Error as SignerError;
44
use alloy_signer_local::LocalSignerError;
55
use alloy_sol_types::SolError;
@@ -65,19 +65,6 @@ macro_rules! ensure {
6565
};
6666
}
6767

68-
macro_rules! ensure_not_precompile {
69-
($address:expr, $ctxt:expr) => {
70-
if $ctxt.is_precompile($address) {
71-
return Err($crate::error::precompile_error($address));
72-
}
73-
};
74-
}
75-
76-
#[cold]
77-
pub(crate) fn precompile_error(address: &Address) -> Error {
78-
fmt_err!("cannot use precompile {address} as an argument")
79-
}
80-
8168
/// Error thrown by cheatcodes.
8269
// This uses a custom repr to minimize the size of the error.
8370
// The repr is basically `enum { Cow<'static, str>, Cow<'static, [u8]> }`

crates/cheatcodes/src/evm.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Cheatcode for getNonce_1Call {
184184
impl Cheatcode for loadCall {
185185
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
186186
let Self { target, slot } = *self;
187-
ensure_not_precompile!(&target, ccx);
187+
ccx.ensure_not_precompile(&target)?;
188188
ccx.ecx.journaled_state.load_account(target)?;
189189
let mut val = ccx.ecx.journaled_state.sload(target, slot.into())?;
190190

@@ -530,7 +530,7 @@ impl Cheatcode for dealCall {
530530
impl Cheatcode for etchCall {
531531
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
532532
let Self { target, newRuntimeBytecode } = self;
533-
ensure_not_precompile!(target, ccx);
533+
ccx.ensure_not_precompile(target)?;
534534
ccx.ecx.journaled_state.load_account(*target)?;
535535
let bytecode = Bytecode::new_raw_checked(newRuntimeBytecode.clone())
536536
.map_err(|e| fmt_err!("failed to create bytecode: {e}"))?;
@@ -582,9 +582,8 @@ impl Cheatcode for setNonceUnsafeCall {
582582
impl Cheatcode for storeCall {
583583
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
584584
let Self { target, slot, value } = *self;
585-
ensure_not_precompile!(&target, ccx);
586-
// ensure the account is touched
587-
let _ = journaled_account(ccx.ecx, target)?;
585+
ccx.ensure_not_precompile(&target)?;
586+
ensure_loaded_account(ccx.ecx, target)?;
588587
ccx.ecx.journaled_state.sstore(target, slot.into(), value.into())?;
589588
Ok(Default::default())
590589
}
@@ -1155,9 +1154,14 @@ pub(super) fn journaled_account<'a>(
11551154
ecx: Ecx<'a, '_, '_>,
11561155
addr: Address,
11571156
) -> Result<&'a mut Account> {
1157+
ensure_loaded_account(ecx, addr)?;
1158+
Ok(ecx.journaled_state.state.get_mut(&addr).expect("account is loaded"))
1159+
}
1160+
1161+
pub(super) fn ensure_loaded_account(ecx: Ecx, addr: Address) -> Result<()> {
11581162
ecx.journaled_state.load_account(addr)?;
11591163
ecx.journaled_state.touch(addr);
1160-
Ok(ecx.journaled_state.state.get_mut(&addr).expect("account is loaded"))
1164+
Ok(())
11611165
}
11621166

11631167
/// Consumes recorded account accesses and returns them as an abi encoded

crates/cheatcodes/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,18 @@ impl std::ops::DerefMut for CheatsCtxt<'_, '_, '_, '_> {
162162
}
163163

164164
impl CheatsCtxt<'_, '_, '_, '_> {
165+
#[inline]
166+
pub(crate) fn ensure_not_precompile(&self, address: &Address) -> Result<()> {
167+
if self.is_precompile(address) { Err(precompile_error(address)) } else { Ok(()) }
168+
}
169+
165170
#[inline]
166171
pub(crate) fn is_precompile(&self, address: &Address) -> bool {
167172
self.ecx.journaled_state.inner.precompiles.contains(address)
168173
}
169174
}
175+
176+
#[cold]
177+
fn precompile_error(address: &Address) -> Error {
178+
fmt_err!("cannot use precompile {address} as an argument")
179+
}

0 commit comments

Comments
 (0)