Skip to content

Commit 3e6d3b8

Browse files
authored
feat: allow any config to be defined inline (#9430)
* feat: allow any config to be defined inline * com * rm duplicate * don't update everything * bump * bump
1 parent c4d81b9 commit 3e6d3b8

File tree

23 files changed

+679
-478
lines changed

23 files changed

+679
-478
lines changed

crates/cheatcodes/src/config.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl CheatsConfig {
6969
running_version: Option<Version>,
7070
) -> Self {
7171
let mut allowed_paths = vec![config.root.clone()];
72-
allowed_paths.extend(config.libs.clone());
73-
allowed_paths.extend(config.allow_paths.clone());
72+
allowed_paths.extend(config.libs.iter().cloned());
73+
allowed_paths.extend(config.allow_paths.iter().cloned());
7474

7575
let rpc_endpoints = config.rpc_endpoints.clone().resolved();
7676
trace!(?rpc_endpoints, "using resolved rpc endpoints");
@@ -101,6 +101,17 @@ impl CheatsConfig {
101101
}
102102
}
103103

104+
/// Returns a new `CheatsConfig` configured with the given `Config` and `EvmOpts`.
105+
pub fn clone_with(&self, config: &Config, evm_opts: EvmOpts) -> Self {
106+
Self::new(
107+
config,
108+
evm_opts,
109+
self.available_artifacts.clone(),
110+
self.running_contract.clone(),
111+
self.running_version.clone(),
112+
)
113+
}
114+
104115
/// Attempts to canonicalize (see [std::fs::canonicalize]) the path.
105116
///
106117
/// Canonicalization fails for non-existing paths, in which case we just normalize the path.

crates/cheatcodes/src/inspector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ pub struct Cheatcodes {
439439
/// Scripting based transactions
440440
pub broadcastable_transactions: BroadcastableTransactions,
441441

442-
/// Additional, user configurable context this Inspector has access to when inspecting a call
442+
/// Additional, user configurable context this Inspector has access to when inspecting a call.
443443
pub config: Arc<CheatsConfig>,
444444

445445
/// Test-scoped context holding data that needs to be reset every test run
@@ -540,7 +540,7 @@ impl Cheatcodes {
540540

541541
/// Returns the configured wallets if available, else creates a new instance.
542542
pub fn wallets(&mut self) -> &Wallets {
543-
self.wallets.get_or_insert(Wallets::new(MultiWallet::default(), None))
543+
self.wallets.get_or_insert_with(|| Wallets::new(MultiWallet::default(), None))
544544
}
545545

546546
/// Sets the unlocked wallets.

crates/chisel/src/executor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl SessionSource {
341341
)
342342
})
343343
.gas_limit(self.config.evm_opts.gas_limit())
344-
.spec(self.config.foundry_config.evm_spec_id())
344+
.spec_id(self.config.foundry_config.evm_spec_id())
345345
.legacy_assertions(self.config.foundry_config.legacy_assertions)
346346
.build(env, backend);
347347

crates/config/src/inline/mod.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use figment::{
44
value::{Dict, Map, Value},
55
Figment, Profile, Provider,
66
};
7+
use foundry_compilers::ProjectCompileOutput;
78
use itertools::Itertools;
89

910
mod natspec;
@@ -53,6 +54,20 @@ impl InlineConfig {
5354
Self::default()
5455
}
5556

57+
/// Tries to create a new instance by detecting inline configurations from the project compile
58+
/// output.
59+
pub fn new_parsed(output: &ProjectCompileOutput, config: &Config) -> eyre::Result<Self> {
60+
let natspecs: Vec<NatSpec> = NatSpec::parse(output, &config.root);
61+
let profiles = &config.profiles;
62+
let mut inline = Self::new();
63+
for natspec in &natspecs {
64+
inline.insert(natspec)?;
65+
// Validate after parsing as TOML.
66+
natspec.validate_profiles(profiles)?;
67+
}
68+
Ok(inline)
69+
}
70+
5671
/// Inserts a new [`NatSpec`] into the [`InlineConfig`].
5772
pub fn insert(&mut self, natspec: &NatSpec) -> Result<(), InlineConfigError> {
5873
let map = if let Some(function) = &natspec.function {
@@ -92,13 +107,16 @@ impl InlineConfig {
92107
Figment::from(base).merge(self.provide(contract, function))
93108
}
94109

95-
/// Returns `true` if a configuration is present at the given contract and function level.
96-
pub fn contains(&self, contract: &str, function: &str) -> bool {
97-
// Order swapped to avoid allocation in `get_function` since order doesn't matter here.
98-
self.get_contract(contract)
99-
.filter(|map| !map.is_empty())
100-
.or_else(|| self.get_function(contract, function))
101-
.is_some_and(|map| !map.is_empty())
110+
/// Returns `true` if a configuration is present at the given contract level.
111+
pub fn contains_contract(&self, contract: &str) -> bool {
112+
self.get_contract(contract).is_some_and(|map| !map.is_empty())
113+
}
114+
115+
/// Returns `true` if a configuration is present at the function level.
116+
///
117+
/// Does not include contract-level configurations.
118+
pub fn contains_function(&self, contract: &str, function: &str) -> bool {
119+
self.get_function(contract, function).is_some_and(|map| !map.is_empty())
102120
}
103121

104122
fn get_contract(&self, contract: &str) -> Option<&DataMap> {

crates/config/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ impl Config {
11251125
/// Returns the [SpecId] derived from the configured [EvmVersion]
11261126
#[inline]
11271127
pub fn evm_spec_id(&self) -> SpecId {
1128-
evm_spec_id(&self.evm_version, self.alphanet)
1128+
evm_spec_id(self.evm_version, self.alphanet)
11291129
}
11301130

11311131
/// Returns whether the compiler version should be auto-detected

crates/config/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl FromStr for Numeric {
259259

260260
/// Returns the [SpecId] derived from [EvmVersion]
261261
#[inline]
262-
pub fn evm_spec_id(evm_version: &EvmVersion, alphanet: bool) -> SpecId {
262+
pub fn evm_spec_id(evm_version: EvmVersion, alphanet: bool) -> SpecId {
263263
if alphanet {
264264
return SpecId::OSAKA;
265265
}

crates/evm/core/src/opts.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ impl EvmOpts {
113113
/// And the block that was used to configure the environment.
114114
pub async fn fork_evm_env(
115115
&self,
116-
fork_url: impl AsRef<str>,
116+
fork_url: &str,
117117
) -> eyre::Result<(revm::primitives::Env, AnyRpcBlock)> {
118-
let fork_url = fork_url.as_ref();
119118
let provider = ProviderBuilder::new(fork_url)
120119
.compute_units_per_second(self.get_compute_units_per_second())
121120
.build()?;

crates/evm/core/src/utils.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ pub fn apply_chain_and_block_specific_env_changes<N: Network>(
4646

4747
return;
4848
}
49-
NamedChain::Arbitrum |
50-
NamedChain::ArbitrumGoerli |
51-
NamedChain::ArbitrumNova |
52-
NamedChain::ArbitrumTestnet => {
49+
c if c.is_arbitrum() => {
5350
// on arbitrum `block.number` is the L1 block which is included in the
5451
// `l1BlockNumber` field
5552
if let Some(l1_block_number) = block

crates/evm/evm/src/executors/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl ExecutorBuilder {
5252

5353
/// Sets the EVM spec to use.
5454
#[inline]
55-
pub fn spec(mut self, spec: SpecId) -> Self {
55+
pub fn spec_id(mut self, spec: SpecId) -> Self {
5656
self.spec_id = spec;
5757
self
5858
}

crates/evm/evm/src/executors/mod.rs

+31-9
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ pub struct Executor {
8686
env: EnvWithHandlerCfg,
8787
/// The Revm inspector stack.
8888
inspector: InspectorStack,
89-
/// The gas limit for calls and deployments. This is different from the gas limit imposed by
90-
/// the passed in environment, as those limits are used by the EVM for certain opcodes like
91-
/// `gaslimit`.
89+
/// The gas limit for calls and deployments.
9290
gas_limit: u64,
9391
/// Whether `failed()` should be called on the test contract to determine if the test failed.
9492
legacy_assertions: bool,
@@ -166,6 +164,36 @@ impl Executor {
166164
self.env.spec_id()
167165
}
168166

167+
/// Sets the EVM spec ID.
168+
pub fn set_spec_id(&mut self, spec_id: SpecId) {
169+
self.env.handler_cfg.spec_id = spec_id;
170+
}
171+
172+
/// Returns the gas limit for calls and deployments.
173+
///
174+
/// This is different from the gas limit imposed by the passed in environment, as those limits
175+
/// are used by the EVM for certain opcodes like `gaslimit`.
176+
pub fn gas_limit(&self) -> u64 {
177+
self.gas_limit
178+
}
179+
180+
/// Sets the gas limit for calls and deployments.
181+
pub fn set_gas_limit(&mut self, gas_limit: u64) {
182+
self.gas_limit = gas_limit;
183+
}
184+
185+
/// Returns whether `failed()` should be called on the test contract to determine if the test
186+
/// failed.
187+
pub fn legacy_assertions(&self) -> bool {
188+
self.legacy_assertions
189+
}
190+
191+
/// Sets whether `failed()` should be called on the test contract to determine if the test
192+
/// failed.
193+
pub fn set_legacy_assertions(&mut self, legacy_assertions: bool) {
194+
self.legacy_assertions = legacy_assertions;
195+
}
196+
169197
/// Creates the default CREATE2 Contract Deployer for local tests and scripts.
170198
pub fn deploy_create2_deployer(&mut self) -> eyre::Result<()> {
171199
trace!("deploying local create2 deployer");
@@ -235,12 +263,6 @@ impl Executor {
235263
self
236264
}
237265

238-
#[inline]
239-
pub fn set_gas_limit(&mut self, gas_limit: u64) -> &mut Self {
240-
self.gas_limit = gas_limit;
241-
self
242-
}
243-
244266
#[inline]
245267
pub fn create2_deployer(&self) -> Address {
246268
self.inspector().create2_deployer()

crates/evm/evm/src/executors/trace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl TracingExecutor {
3232
.alphanet(alphanet)
3333
.create2_deployer(create2_deployer)
3434
})
35-
.spec(evm_spec_id(&version.unwrap_or_default(), alphanet))
35+
.spec_id(evm_spec_id(version.unwrap_or_default(), alphanet))
3636
.build(env, db),
3737
}
3838
}

crates/evm/traces/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ impl TraceMode {
349349
}
350350
}
351351

352-
pub fn with_verbosity(self, verbosiy: u8) -> Self {
353-
if verbosiy >= 3 {
352+
pub fn with_verbosity(self, verbosity: u8) -> Self {
353+
if verbosity >= 3 {
354354
std::cmp::max(self, Self::Call)
355355
} else {
356356
self

crates/forge/bin/cmd/coverage.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use forge::{
1111
},
1212
opts::EvmOpts,
1313
utils::IcPcMap,
14-
MultiContractRunnerBuilder, TestOptions,
14+
MultiContractRunnerBuilder,
1515
};
1616
use foundry_cli::utils::{LoadConfig, STATIC_FUZZ_SEED};
1717
use foundry_common::{compile::ProjectCompiler, fs};
@@ -233,7 +233,6 @@ impl CoverageArgs {
233233
.evm_spec(config.evm_spec_id())
234234
.sender(evm_opts.sender)
235235
.with_fork(evm_opts.get_fork(&config, env.clone()))
236-
.with_test_options(TestOptions::new(output, config.clone())?)
237236
.set_coverage(true)
238237
.build(&root, output, env, evm_opts)?;
239238

crates/forge/bin/cmd/test/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use forge::{
1414
identifier::SignaturesIdentifier,
1515
CallTraceDecoderBuilder, InternalTraceMode, TraceKind,
1616
},
17-
MultiContractRunner, MultiContractRunnerBuilder, TestFilter, TestOptions,
17+
MultiContractRunner, MultiContractRunnerBuilder, TestFilter,
1818
};
1919
use foundry_cli::{
2020
opts::{CoreBuildArgs, GlobalOpts},
@@ -317,9 +317,6 @@ impl TestArgs {
317317
}
318318
}
319319

320-
let config = Arc::new(config);
321-
let test_options = TestOptions::new(&output, config.clone())?;
322-
323320
let should_debug = self.debug.is_some();
324321
let should_draw = self.flamegraph || self.flamechart;
325322

@@ -346,14 +343,14 @@ impl TestArgs {
346343
};
347344

348345
// Prepare the test builder.
346+
let config = Arc::new(config);
349347
let runner = MultiContractRunnerBuilder::new(config.clone())
350348
.set_debug(should_debug)
351349
.set_decode_internal(decode_internal)
352350
.initial_balance(evm_opts.initial_balance)
353351
.evm_spec(config.evm_spec_id())
354352
.sender(evm_opts.sender)
355353
.with_fork(evm_opts.get_fork(&config, env.clone()))
356-
.with_test_options(test_options)
357354
.enable_isolation(evm_opts.isolate)
358355
.alphanet(evm_opts.alphanet)
359356
.build(project_root, &output, env, evm_opts)?;

0 commit comments

Comments
 (0)