Skip to content

Commit

Permalink
fix: separate dispatch impl from defs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 3, 2023
1 parent 2ff8a99 commit c34b61d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
19 changes: 13 additions & 6 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,7 @@ impl Cheatcodes {
// but only if the backend is in forking mode
data.db.ensure_cheatcode_access_forking_mode(caller)?;

// TODO
let _ = decoded;
let _ = CheatsCtxt { state: self, data, caller };
// // apply the cheatcode to the current state
// decoded.apply(&mut CheatsCtxt { state: self, data, caller })
todo!()
apply_dispatch(&decoded, &mut CheatsCtxt { state: self, data, caller })
}

/// Determines the address of the contract and marks it as allowed
Expand Down Expand Up @@ -1110,3 +1105,15 @@ fn check_if_fixed_gas_limit<DB: DatabaseExt>(data: &EVMData<'_, DB>, call_gas_li
// gas too low" failure when simulated on chain
&& call_gas_limit > 2300
}

/// Dispatches the cheatcode call to the appropriate function.
fn apply_dispatch<DB: DatabaseExt>(calls: &Vm::VmCalls, ccx: &mut CheatsCtxt<DB>) -> Result {
macro_rules! match_ {
($($variant:ident),*) => {
match calls {
$(Vm::VmCalls::$variant(cheat) => crate::Cheatcode::apply_traced(cheat, ccx),)*
}
};
}
vm_calls!(match_)
}
48 changes: 25 additions & 23 deletions crates/cheatcodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#![warn(missing_docs, unreachable_pub, unused_crate_dependencies, rust_2018_idioms)]
#![allow(elided_lifetimes_in_paths)] // Cheats context uses 3 lifetimes

#[macro_use]
pub extern crate foundry_cheatcodes_defs as defs;
#[macro_use]
extern crate tracing;

Expand All @@ -13,7 +15,7 @@ use foundry_evm_core::backend::DatabaseExt;
use revm::EVMData;
use tracing::Level;

pub use foundry_cheatcodes_defs::{self as defs, CheatcodeDef, Vm};
pub use defs::{CheatcodeDef, Vm};

#[macro_use]
mod error;
Expand Down Expand Up @@ -61,31 +63,31 @@ pub(crate) trait Cheatcode: CheatcodeDef {
trace_call();
let result = self.apply_full(ccx);
trace_return(&result);
result
}
}

// Separate functions to avoid inline and monomorphization bloat.
fn trace_span<T: Cheatcode>(cheat: &T) -> tracing::Span {
if enabled!(Level::TRACE) {
trace_span!(target: "cheatcodes", "apply", cheat=?cheat)
} else {
debug_span!(target: "cheatcodes", "apply", id=%T::CHEATCODE.func.id)
}
}
return result;

// Separate functions to avoid inline and monomorphization bloat.
fn trace_span<T: Cheatcode>(cheat: &T) -> tracing::Span {
if enabled!(Level::TRACE) {
trace_span!(target: "cheatcodes", "apply", cheat=?cheat)
} else {
debug_span!(target: "cheatcodes", "apply", id=%T::CHEATCODE.func.id)
}
}

fn trace_call() {
trace!(target: "cheatcodes", "applying");
}
fn trace_call() {
trace!(target: "cheatcodes", "applying");
}

fn trace_return(result: &Result) {
trace!(
target: "cheatcodes",
return = match result {
Ok(b) => hex::encode(b),
Err(e) => e.to_string(),
fn trace_return(result: &Result) {
trace!(
target: "cheatcodes",
return = match result {
Ok(b) => hex::encode(b),
Err(e) => e.to_string(),
}
);
}
);
}
}

/// The cheatcode context, used in [`Cheatcode`].
Expand Down
19 changes: 8 additions & 11 deletions crates/macros/impl/src/cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn derive_calls_enum(name: &Ident, input: &syn::DataEnum) -> Result<TokenStream>
}

// keep original order for matching
let variants_names = input.variants.iter().map(|v| &v.ident);
let variant_names = input.variants.iter().map(|v| &v.ident);

let mut variants = input.variants.iter().collect::<Vec<_>>();
variants.sort_by(|a, b| a.ident.cmp(&b.ident));
Expand All @@ -110,16 +110,13 @@ fn derive_calls_enum(name: &Ident, input: &syn::DataEnum) -> Result<TokenStream>
/// All the cheatcodes in [this contract](self).
pub const CHEATCODES: &'static [&'static Cheatcode<'static>] = &[#(<#variant_tys as CheatcodeDef>::CHEATCODE,)*];

#[cfg(feature = "impls")]
impl #name {
pub(crate) fn apply<DB: foundry_evm_core::backend::DatabaseExt>(
&self,
ccx: &mut crate::impls::CheatsCtxt<DB>
) -> crate::impls::Result {
match self {
#(Self::#variants_names(c) => crate::impls::Cheatcode::apply_traced(c, ccx),)*
}
}
/// Internal macro to implement the `Cheatcode` trait for the Vm calls enum.
#[doc(hidden)]
#[macro_export]
macro_rules! vm_calls {
($mac:ident) => {
$mac!(#(#variant_names),*)
};
}
})
}
Expand Down

0 comments on commit c34b61d

Please sign in to comment.