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
142 lines (114 loc) · 3.52 KB
/
lib.rs
File metadata and controls
142 lines (114 loc) · 3.52 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
//! # foundry-cheatcodes
//!
//! Foundry cheatcodes implementations.
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![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, CheatcodesExecutor, Context,
};
pub use spec::{CheatcodeDef, Vm};
pub use Vm::ForgeContext;
#[macro_use]
mod error;
mod base64;
mod config;
mod crypto;
mod env;
pub use env::set_execution_context;
mod evm;
mod fs;
mod inspector;
mod json;
mod script;
pub use script::{ScriptWallets, ScriptWalletsInner};
mod string;
mod test;
pub use test::expect::ExpectedCallTracker;
mod toml;
mod utils;
/// 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_stateful<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
self.apply(ccx.state)
}
/// Applies this cheatcode to the given context and executor.
///
/// Implement this function if you need access to the executor.
#[inline(always)]
fn apply_full<DB: DatabaseExt, E: CheatcodesExecutor>(
&self,
ccx: &mut CheatsCtxt<DB>,
_executor: &mut E,
) -> Result {
self.apply_stateful(ccx)
}
}
pub(crate) trait DynCheatcode {
fn name(&self) -> &'static str;
fn id(&self) -> &'static str;
fn as_debug(&self) -> &dyn std::fmt::Debug;
}
impl<T: Cheatcode> DynCheatcode for T {
fn name(&self) -> &'static str {
T::CHEATCODE.func.signature.split('(').next().unwrap()
}
fn id(&self) -> &'static str {
T::CHEATCODE.func.id
}
fn as_debug(&self) -> &dyn std::fmt::Debug {
self
}
}
/// The cheatcode context, used in `Cheatcode`.
pub 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,
/// Gas limit of the current cheatcode call.
pub(crate) gas_limit: u64,
}
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(address)
}
}