-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
evm.rs
335 lines (287 loc) · 11.7 KB
/
evm.rs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! cli arguments for configuring the evm settings
use alloy_primitives::{Address, B256, U256};
use clap::{ArgAction, Parser};
use eyre::ContextCompat;
use foundry_config::{
figment::{
self,
error::Kind::InvalidType,
value::{Dict, Map, Value},
Metadata, Profile, Provider,
},
Chain, Config,
};
use rustc_hash::FxHashMap;
use serde::Serialize;
/// Map keyed by breakpoints char to their location (contract address, pc)
pub type Breakpoints = FxHashMap<char, (Address, usize)>;
/// `EvmArgs` and `EnvArgs` take the highest precedence in the Config/Figment hierarchy.
/// All vars are opt-in, their default values are expected to be set by the
/// [`foundry_config::Config`], and are always present ([`foundry_config::Config::default`])
///
/// Both have corresponding types in the `evm_adapters` crate which have mandatory fields.
/// The expected workflow is
/// 1. load the [`foundry_config::Config`]
/// 2. merge with `EvmArgs` into a `figment::Figment`
/// 3. extract `evm_adapters::Opts` from the merged `Figment`
///
/// # Example
///
/// ```ignore
/// use foundry_config::Config;
/// use forge::executor::opts::EvmOpts;
/// use foundry_common::evm::EvmArgs;
/// # fn t(args: EvmArgs) {
/// let figment = Config::figment_with_root(".").merge(args);
/// let opts = figment.extract::<EvmOpts>().unwrap();
/// # }
/// ```
#[derive(Clone, Debug, Default, Serialize, Parser)]
#[command(next_help_heading = "EVM options", about = None, long_about = None)] // override doc
pub struct EvmArgs {
/// Fetch state over a remote endpoint instead of starting from an empty state.
///
/// If you want to fetch state from a specific block number, see --fork-block-number.
#[arg(long, short, visible_alias = "rpc-url", value_name = "URL")]
#[serde(rename = "eth_rpc_url", skip_serializing_if = "Option::is_none")]
pub fork_url: Option<String>,
/// Fetch state from a specific block number over a remote endpoint.
///
/// See --fork-url.
#[arg(long, requires = "fork_url", value_name = "BLOCK")]
#[serde(skip_serializing_if = "Option::is_none")]
pub fork_block_number: Option<u64>,
/// Number of retries.
///
/// See --fork-url.
#[arg(long, requires = "fork_url", value_name = "RETRIES")]
#[serde(skip_serializing_if = "Option::is_none")]
pub fork_retries: Option<u32>,
/// Initial retry backoff on encountering errors.
///
/// See --fork-url.
#[arg(long, requires = "fork_url", value_name = "BACKOFF")]
#[serde(skip_serializing_if = "Option::is_none")]
pub fork_retry_backoff: Option<u64>,
/// Explicitly disables the use of RPC caching.
///
/// All storage slots are read entirely from the endpoint.
///
/// This flag overrides the project's configuration file.
///
/// See --fork-url.
#[arg(long)]
#[serde(skip)]
pub no_storage_caching: bool,
/// The initial balance of deployed test contracts.
#[arg(long, value_name = "BALANCE")]
#[serde(skip_serializing_if = "Option::is_none")]
pub initial_balance: Option<U256>,
/// The address which will be executing tests.
#[arg(long, value_name = "ADDRESS")]
#[serde(skip_serializing_if = "Option::is_none")]
pub sender: Option<Address>,
/// Enable the FFI cheatcode.
#[arg(long)]
#[serde(skip)]
pub ffi: bool,
/// Use the create 2 factory in all cases including tests and non-broadcasting scripts.
#[arg(long)]
#[serde(skip)]
pub always_use_create_2_factory: bool,
/// Verbosity of the EVM.
///
/// Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
///
/// Verbosity levels:
/// - 2: Print logs for all tests
/// - 3: Print execution traces for failing tests
/// - 4: Print execution traces for all tests, and setup traces for failing tests
/// - 5: Print execution and setup traces for all tests
#[arg(long, short, verbatim_doc_comment, action = ArgAction::Count)]
#[serde(skip)]
pub verbosity: u8,
/// Sets the number of assumed available compute units per second for this provider
///
/// default value: 330
///
/// See also --fork-url and https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second
#[arg(long, alias = "cups", value_name = "CUPS", help_heading = "Fork config")]
pub compute_units_per_second: Option<u64>,
/// Disables rate limiting for this node's provider.
///
/// See also --fork-url and https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second
#[arg(
long,
value_name = "NO_RATE_LIMITS",
help_heading = "Fork config",
visible_alias = "no-rate-limit"
)]
#[serde(skip)]
pub no_rpc_rate_limit: bool,
/// All ethereum environment related arguments
#[command(flatten)]
#[serde(flatten)]
pub env: EnvArgs,
/// Whether to enable isolation of calls.
/// In isolation mode all top-level calls are executed as a separate transaction in a separate
/// EVM context, enabling more precise gas accounting and transaction state changes.
#[arg(long)]
#[serde(skip)]
pub isolate: bool,
}
// Make this set of options a `figment::Provider` so that it can be merged into the `Config`
impl Provider for EvmArgs {
fn metadata(&self) -> Metadata {
Metadata::named("Evm Opts Provider")
}
fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
let value = Value::serialize(self)?;
let error = InvalidType(value.to_actual(), "map".into());
let mut dict = value.into_dict().ok_or(error)?;
if self.verbosity > 0 {
// need to merge that manually otherwise `from_occurrences` does not work
dict.insert("verbosity".to_string(), self.verbosity.into());
}
if self.ffi {
dict.insert("ffi".to_string(), self.ffi.into());
}
if self.isolate {
dict.insert("isolate".to_string(), self.isolate.into());
}
if self.always_use_create_2_factory {
dict.insert(
"always_use_create_2_factory".to_string(),
self.always_use_create_2_factory.into(),
);
}
if self.no_storage_caching {
dict.insert("no_storage_caching".to_string(), self.no_storage_caching.into());
}
if self.no_rpc_rate_limit {
dict.insert("no_rpc_rate_limit".to_string(), self.no_rpc_rate_limit.into());
}
if let Some(fork_url) = &self.fork_url {
dict.insert("eth_rpc_url".to_string(), fork_url.clone().into());
}
Ok(Map::from([(Config::selected_profile(), dict)]))
}
}
/// Configures the executor environment during tests.
#[derive(Clone, Debug, Default, Serialize, Parser)]
#[command(next_help_heading = "Executor environment config")]
pub struct EnvArgs {
/// The block gas limit.
#[arg(long, value_name = "GAS_LIMIT")]
#[serde(skip_serializing_if = "Option::is_none")]
pub gas_limit: Option<u64>,
/// EIP-170: Contract code size limit in bytes. Useful to increase this because of tests. By
/// default, it is 0x6000 (~25kb).
#[arg(long, value_name = "CODE_SIZE")]
#[serde(skip_serializing_if = "Option::is_none")]
pub code_size_limit: Option<usize>,
/// The chain name or EIP-155 chain ID.
#[arg(long, visible_alias = "chain-id", value_name = "CHAIN")]
#[serde(rename = "chain_id", skip_serializing_if = "Option::is_none", serialize_with = "id")]
pub chain: Option<Chain>,
/// The gas price.
#[arg(long, value_name = "GAS_PRICE")]
#[serde(skip_serializing_if = "Option::is_none")]
pub gas_price: Option<u64>,
/// The base fee in a block.
#[arg(long, visible_alias = "base-fee", value_name = "FEE")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_base_fee_per_gas: Option<u64>,
/// The transaction origin.
#[arg(long, value_name = "ADDRESS")]
#[serde(skip_serializing_if = "Option::is_none")]
pub tx_origin: Option<Address>,
/// The coinbase of the block.
#[arg(long, value_name = "ADDRESS")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_coinbase: Option<Address>,
/// The timestamp of the block.
#[arg(long, value_name = "TIMESTAMP")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_timestamp: Option<u64>,
/// The block number.
#[arg(long, value_name = "BLOCK")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_number: Option<u64>,
/// The block difficulty.
#[arg(long, value_name = "DIFFICULTY")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_difficulty: Option<u64>,
/// The block prevrandao value. NOTE: Before merge this field was mix_hash.
#[arg(long, value_name = "PREVRANDAO")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_prevrandao: Option<B256>,
/// The block gas limit.
#[arg(long, value_name = "GAS_LIMIT")]
#[serde(skip_serializing_if = "Option::is_none")]
pub block_gas_limit: Option<u64>,
/// The memory limit per EVM execution in bytes.
/// If this limit is exceeded, a `MemoryLimitOOG` result is thrown.
///
/// The default is 128MiB.
#[arg(long, value_name = "MEMORY_LIMIT")]
#[serde(skip_serializing_if = "Option::is_none")]
pub memory_limit: Option<u64>,
/// Whether to disable the block gas limit checks.
#[arg(long, visible_alias = "no-gas-limit")]
pub disable_block_gas_limit: bool,
}
impl EvmArgs {
/// Ensures that fork url exists and returns its reference.
pub fn ensure_fork_url(&self) -> eyre::Result<&String> {
self.fork_url.as_ref().wrap_err("Missing `--fork-url` field.")
}
}
/// We have to serialize chain IDs and not names because when extracting an EVM `Env`, it expects
/// `chain_id` to be `u64`.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn id<S: serde::Serializer>(chain: &Option<Chain>, s: S) -> Result<S::Ok, S::Error> {
if let Some(chain) = chain {
s.serialize_u64(chain.id())
} else {
// skip_serializing_if = "Option::is_none" should prevent this branch from being taken
unreachable!()
}
}
#[cfg(test)]
mod tests {
use super::*;
use foundry_config::NamedChain;
#[test]
fn can_parse_chain_id() {
let args = EvmArgs {
env: EnvArgs { chain: Some(NamedChain::Mainnet.into()), ..Default::default() },
..Default::default()
};
let config = Config::from_provider(Config::figment().merge(args));
assert_eq!(config.chain, Some(NamedChain::Mainnet.into()));
let env = EnvArgs::parse_from(["foundry-common", "--chain-id", "goerli"]);
assert_eq!(env.chain, Some(NamedChain::Goerli.into()));
}
#[test]
fn test_memory_limit() {
let args = EvmArgs {
env: EnvArgs { chain: Some(NamedChain::Mainnet.into()), ..Default::default() },
..Default::default()
};
let config = Config::from_provider(Config::figment().merge(args));
assert_eq!(config.memory_limit, Config::default().memory_limit);
let env = EnvArgs::parse_from(["foundry-common", "--memory-limit", "100"]);
assert_eq!(env.memory_limit, Some(100));
}
#[test]
fn test_chain_id() {
let env = EnvArgs::parse_from(["foundry-common", "--chain-id", "1"]);
assert_eq!(env.chain, Some(Chain::mainnet()));
let env = EnvArgs::parse_from(["foundry-common", "--chain-id", "mainnet"]);
assert_eq!(env.chain, Some(Chain::mainnet()));
let args = EvmArgs { env, ..Default::default() };
let config = Config::from_provider(Config::figment().merge(args));
assert_eq!(config.chain, Some(Chain::mainnet()));
}
}