forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
144 lines (125 loc) · 4.16 KB
/
lib.rs
File metadata and controls
144 lines (125 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! # foundry-cheatcodes
//!
//! Foundry cheatcodes implementations.
#![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_spec as spec;
#[macro_use]
extern crate tracing;
use alloy_primitives::Address;
use foundry_evm_core::backend::DatabaseExt;
use revm::{ContextPrecompiles, InnerEvmContext};
pub use config::CheatsConfig;
pub use error::{Error, ErrorKind, Result};
pub use inspector::{BroadcastableTransaction, BroadcastableTransactions, Cheatcodes, Context};
pub use spec::{CheatcodeDef, Vm};
#[macro_use]
mod error;
mod base64;
mod config;
mod env;
mod evm;
mod fs;
mod inspector;
mod json;
mod script;
mod string;
mod test;
mod toml;
mod utils;
pub use env::set_execution_context;
pub use script::ScriptWallets;
pub use test::expect::ExpectedCallTracker;
pub use Vm::ForgeContext;
/// Cheatcode implementation.
pub(crate) trait Cheatcode: CheatcodeDef + DynCheatcode {
/// Applies this cheatcode to the given state.
///
/// Implement this function if you don't need access to the EVM data.
fn apply(&self, state: &mut Cheatcodes) -> Result {
let _ = state;
unimplemented!("{}", Self::CHEATCODE.func.id)
}
/// Applies this cheatcode to the given context.
///
/// Implement this function if you need access to the EVM data.
#[inline(always)]
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
self.apply(ccx.state)
}
#[inline]
fn apply_traced<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let _span = trace_span_and_call(self);
let result = self.apply_full(ccx);
trace_return(&result);
return result;
// Separate and non-generic functions to avoid inline and monomorphization bloat.
#[inline(never)]
fn trace_span_and_call(cheat: &dyn DynCheatcode) -> tracing::span::EnteredSpan {
let span = debug_span!(target: "cheatcodes", "apply");
if !span.is_disabled() {
if enabled!(tracing::Level::TRACE) {
span.record("cheat", tracing::field::debug(cheat.as_debug()));
} else {
span.record("id", cheat.cheatcode().func.id);
}
}
let entered = span.entered();
trace!(target: "cheatcodes", "applying");
entered
}
#[inline(never)]
fn trace_return(result: &Result) {
trace!(
target: "cheatcodes",
return = match result {
Ok(b) => hex::encode(b),
Err(e) => e.to_string(),
}
);
}
}
}
pub(crate) trait DynCheatcode {
fn cheatcode(&self) -> &'static foundry_cheatcodes_spec::Cheatcode<'static>;
fn as_debug(&self) -> &dyn std::fmt::Debug;
}
impl<T: Cheatcode> DynCheatcode for T {
fn cheatcode(&self) -> &'static foundry_cheatcodes_spec::Cheatcode<'static> {
T::CHEATCODE
}
fn as_debug(&self) -> &dyn std::fmt::Debug {
self
}
}
/// The cheatcode context, used in [`Cheatcode`].
pub(crate) struct CheatsCtxt<'cheats, 'evm, DB: DatabaseExt> {
/// The cheatcodes inspector state.
pub(crate) state: &'cheats mut Cheatcodes,
/// The EVM data.
pub(crate) ecx: &'evm mut InnerEvmContext<DB>,
/// The precompiles context.
pub(crate) precompiles: &'evm mut ContextPrecompiles<DB>,
/// The original `msg.sender`.
pub(crate) caller: Address,
}
impl<'cheats, 'evm, DB: DatabaseExt> std::ops::Deref for CheatsCtxt<'cheats, 'evm, DB> {
type Target = InnerEvmContext<DB>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
self.ecx
}
}
impl<'cheats, 'evm, DB: DatabaseExt> std::ops::DerefMut for CheatsCtxt<'cheats, 'evm, DB> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self.ecx
}
}
impl<'cheats, 'evm, DB: DatabaseExt> CheatsCtxt<'cheats, 'evm, DB> {
#[inline]
pub(crate) fn is_precompile(&self, address: &Address) -> bool {
self.precompiles.contains_key(address)
}
}