Skip to content

Commit

Permalink
refactor: adapt to CompilerContract trait type (#9423)
Browse files Browse the repository at this point in the history
* refactor: adapt to CompilerContract trait type

* chore: cargo fmt

* fix: specify MultiCompiler in MultiContractRunner::build

* bump

* fix

---------

Co-authored-by: Arsenii Kulikov <[email protected]>
  • Loading branch information
elfedy and klkvr authored Dec 4, 2024
1 parent 25c978a commit 3784cd8
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 42 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ foundry-linking = { path = "crates/linking" }

# solc & compilation utilities
foundry-block-explorers = { version = "0.9.0", default-features = false }
foundry-compilers = { version = "0.12.3", default-features = false }
foundry-compilers = { version = "0.12.5", default-features = false }
foundry-fork-db = "0.8.0"
solang-parser = "=0.3.3"
solar-ast = { version = "=0.1.0", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions crates/cast/bin/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use foundry_common::{
shell,
};
use foundry_compilers::{
artifacts::{ConfigurableContractArtifact, StorageLayout},
artifacts::{ConfigurableContractArtifact, Contract, StorageLayout},
compilers::{
solc::{Solc, SolcCompiler},
Compiler,
Expand Down Expand Up @@ -284,7 +284,7 @@ fn print_storage(layout: StorageLayout, values: Vec<StorageValue>, pretty: bool)
"{}",
serde_json::to_string_pretty(&serde_json::to_value(StorageReport { layout, values })?)?
)?;
return Ok(())
return Ok(());
}

let mut table = Table::new();
Expand Down Expand Up @@ -314,7 +314,7 @@ fn print_storage(layout: StorageLayout, values: Vec<StorageValue>, pretty: bool)
Ok(())
}

fn add_storage_layout_output<C: Compiler>(project: &mut Project<C>) {
fn add_storage_layout_output<C: Compiler<CompilerContract = Contract>>(project: &mut Project<C>) {
project.artifacts.additional_values.storage_layout = true;
project.update_output_selection(|selection| {
selection.0.values_mut().for_each(|contract_selection| {
Expand Down
19 changes: 14 additions & 5 deletions crates/common/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use comfy_table::{presets::ASCII_MARKDOWN, Attribute, Cell, CellAlignment, Color
use eyre::Result;
use foundry_block_explorers::contract::Metadata;
use foundry_compilers::{
artifacts::{remappings::Remapping, BytecodeObject, Source},
artifacts::{remappings::Remapping, BytecodeObject, Contract, Source},
compilers::{
solc::{Solc, SolcCompiler},
Compiler,
Expand Down Expand Up @@ -129,7 +129,10 @@ impl ProjectCompiler {
}

/// Compiles the project.
pub fn compile<C: Compiler>(mut self, project: &Project<C>) -> Result<ProjectCompileOutput<C>> {
pub fn compile<C: Compiler<CompilerContract = Contract>>(
mut self,
project: &Project<C>,
) -> Result<ProjectCompileOutput<C>> {
// TODO: Avoid process::exit
if !project.paths.has_input_files() && self.files.is_empty() {
sh_println!("Nothing to compile")?;
Expand Down Expand Up @@ -163,7 +166,10 @@ impl ProjectCompiler {
/// ProjectCompiler::new().compile_with(|| Ok(prj.compile()?)).unwrap();
/// ```
#[instrument(target = "forge::compile", skip_all)]
fn compile_with<C: Compiler, F>(self, f: F) -> Result<ProjectCompileOutput<C>>
fn compile_with<C: Compiler<CompilerContract = Contract>, F>(
self,
f: F,
) -> Result<ProjectCompileOutput<C>>
where
F: FnOnce() -> Result<ProjectCompileOutput<C>>,
{
Expand Down Expand Up @@ -202,7 +208,10 @@ impl ProjectCompiler {
}

/// If configured, this will print sizes or names
fn handle_output<C: Compiler>(&self, output: &ProjectCompileOutput<C>) {
fn handle_output<C: Compiler<CompilerContract = Contract>>(
&self,
output: &ProjectCompileOutput<C>,
) {
let print_names = self.print_names.unwrap_or(false);
let print_sizes = self.print_sizes.unwrap_or(false);

Expand Down Expand Up @@ -465,7 +474,7 @@ pub struct ContractInfo {
/// If `verify` and it's a standalone script, throw error. Only allowed for projects.
///
/// **Note:** this expects the `target_path` to be absolute
pub fn compile_target<C: Compiler>(
pub fn compile_target<C: Compiler<CompilerContract = Contract>>(
target_path: &Path,
project: &Project<C>,
quiet: bool,
Expand Down
15 changes: 9 additions & 6 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use foundry_compilers::{
error::SolcError,
multi::{MultiCompilerParsedSource, MultiCompilerRestrictions},
solc::{CliSettings, SolcSettings},
ConfigurableArtifacts, Graph, Project, ProjectPathsConfig, RestrictionsWithVersion,
VyperLanguage,
ArtifactOutput, ConfigurableArtifacts, Graph, Project, ProjectPathsConfig,
RestrictionsWithVersion, VyperLanguage,
};
use regex::Regex;
use revm_primitives::{map::AddressHashMap, FixedBytes, SpecId};
Expand Down Expand Up @@ -1021,7 +1021,10 @@ impl Config {
}

/// Cleans the project.
pub fn cleanup<C: Compiler>(&self, project: &Project<C>) -> Result<(), SolcError> {
pub fn cleanup<C: Compiler, T: ArtifactOutput<CompilerContract = C::CompilerContract>>(
&self,
project: &Project<C, T>,
) -> Result<(), SolcError> {
project.cleanup()?;

// Remove last test run failures file.
Expand Down Expand Up @@ -1090,7 +1093,7 @@ impl Config {
rx.recv().expect("sender dropped")
}
Err(RecvTimeoutError::Disconnected) => panic!("sender dropped"),
}
};
}
if let Some(ref solc) = self.solc {
let solc = match solc {
Expand Down Expand Up @@ -1291,11 +1294,11 @@ impl Config {
) -> Option<Result<Cow<'_, str>, UnresolvedEnvVarError>> {
let mut endpoints = self.rpc_endpoints.clone().resolved();
if let Some(endpoint) = endpoints.remove(maybe_alias) {
return Some(endpoint.map(Cow::Owned))
return Some(endpoint.map(Cow::Owned));
}

if let Ok(Some(endpoint)) = mesc::get_endpoint_by_query(maybe_alias, Some("foundry")) {
return Some(Ok(Cow::Owned(endpoint.url)))
return Some(Ok(Cow::Owned(endpoint.url)));
}

None
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/traces/src/debug/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use foundry_common::compact_to_contract;
use foundry_compilers::{
artifacts::{
sourcemap::{SourceElement, SourceMap},
Bytecode, ContractBytecodeSome, Libraries, Source,
Bytecode, Contract, ContractBytecodeSome, Libraries, Source,
},
multi::MultiCompilerLanguage,
Artifact, Compiler, ProjectCompileOutput,
Expand Down Expand Up @@ -137,7 +137,7 @@ impl ContractSources {
Ok(sources)
}

pub fn insert<C: Compiler>(
pub fn insert<C: Compiler<CompilerContract = Contract>>(
&mut self,
output: &ProjectCompileOutput<C>,
root: &Path,
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/bin/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl CloneArgs {
let remappings_txt_content =
config.remappings.iter().map(|r| r.to_string()).collect::<Vec<_>>().join("\n");
if fs::write(&remappings_txt, remappings_txt_content).is_err() {
return false
return false;
}

let profile = config.profile.as_str().as_str();
Expand Down Expand Up @@ -612,7 +612,7 @@ impl EtherscanClient for Client {
mod tests {
use super::*;
use alloy_primitives::hex;
use foundry_compilers::Artifact;
use foundry_compilers::CompilerContract;
use foundry_test_utils::rpc::next_mainnet_etherscan_api_key;
use std::collections::BTreeMap;

Expand All @@ -631,7 +631,7 @@ mod tests {
contracts.iter().for_each(|(name, contract)| {
if name == contract_name {
let compiled_creation_code =
contract.get_bytecode_object().expect("creation code not found");
contract.bin_ref().expect("creation code not found");
assert!(
hex::encode(compiled_creation_code.as_ref())
.starts_with(stripped_creation_code),
Expand Down
3 changes: 2 additions & 1 deletion crates/forge/bin/cmd/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use foundry_compilers::{
artifacts::{
sourcemap::SourceMap, CompactBytecode, CompactDeployedBytecode, SolcLanguage, Source,
},
compilers::multi::MultiCompiler,
Artifact, ArtifactId, Project, ProjectCompileOutput,
};
use foundry_config::{Config, SolcReq};
Expand Down Expand Up @@ -245,7 +246,7 @@ impl CoverageArgs {
.sender(evm_opts.sender)
.with_fork(evm_opts.get_fork(&config, env.clone()))
.set_coverage(true)
.build(&root, output, env, evm_opts)?;
.build::<MultiCompiler>(&root, output, env, evm_opts)?;

let known_contracts = runner.known_contracts.clone();

Expand Down
7 changes: 5 additions & 2 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use foundry_cli::{
use foundry_common::{compile::ProjectCompiler, evm::EvmArgs, fs, shell, TestFunctionExt};
use foundry_compilers::{
artifacts::output_selection::OutputSelection,
compilers::{multi::MultiCompilerLanguage, Language},
compilers::{
multi::{MultiCompiler, MultiCompilerLanguage},
Language,
},
utils::source_files_iter,
ProjectCompileOutput,
};
Expand Down Expand Up @@ -353,7 +356,7 @@ impl TestArgs {
.with_fork(evm_opts.get_fork(&config, env.clone()))
.enable_isolation(evm_opts.isolate)
.alphanet(evm_opts.alphanet)
.build(project_root, &output, env, evm_opts)?;
.build::<MultiCompiler>(project_root, &output, env, evm_opts)?;

let mut maybe_override_mt = |flag, maybe_regex: Option<&Option<Regex>>| {
if let Some(Some(regex)) = maybe_regex {
Expand Down
8 changes: 6 additions & 2 deletions crates/forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use alloy_json_abi::{Function, JsonAbi};
use alloy_primitives::{Address, Bytes, U256};
use eyre::Result;
use foundry_common::{get_contract_name, shell::verbosity, ContractsByArtifact, TestFunctionExt};
use foundry_compilers::{artifacts::Libraries, Artifact, ArtifactId, ProjectCompileOutput};
use foundry_compilers::{
artifacts::{Contract, Libraries},
compilers::Compiler,
Artifact, ArtifactId, ProjectCompileOutput,
};
use foundry_config::{Config, InlineConfig};
use foundry_evm::{
backend::Backend,
Expand Down Expand Up @@ -457,7 +461,7 @@ impl MultiContractRunnerBuilder {

/// Given an EVM, proceeds to return a runner which is able to execute all tests
/// against that evm
pub fn build(
pub fn build<C: Compiler<CompilerContract = Contract>>(
self,
root: &Path,
output: &ProjectCompileOutput,
Expand Down
7 changes: 4 additions & 3 deletions crates/forge/tests/it/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloy_primitives::U256;
use forge::{revm::primitives::SpecId, MultiContractRunner, MultiContractRunnerBuilder};
use foundry_compilers::{
artifacts::{EvmVersion, Libraries, Settings},
compilers::multi::MultiCompiler,
utils::RuntimeOrHandle,
Project, ProjectCompileOutput, SolcConfig, Vyper,
};
Expand Down Expand Up @@ -212,7 +213,7 @@ impl ForgeTestData {
builder
.enable_isolation(opts.isolate)
.sender(config.sender)
.build(root, &self.output, opts.local_evm_env(), opts)
.build::<MultiCompiler>(root, &self.output, opts.local_evm_env(), opts)
.unwrap()
}

Expand All @@ -221,7 +222,7 @@ impl ForgeTestData {
let mut opts = config_evm_opts(&self.config);
opts.verbosity = 5;
self.base_runner()
.build(self.project.root(), &self.output, opts.local_evm_env(), opts)
.build::<MultiCompiler>(self.project.root(), &self.output, opts.local_evm_env(), opts)
.unwrap()
}

Expand All @@ -237,7 +238,7 @@ impl ForgeTestData {

self.base_runner()
.with_fork(fork)
.build(self.project.root(), &self.output, env, opts)
.build::<MultiCompiler>(self.project.root(), &self.output, env, opts)
.unwrap()
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::init_tracing;
use eyre::{Result, WrapErr};
use foundry_compilers::{
artifacts::Contract,
cache::CompilerCache,
compilers::multi::MultiCompiler,
error::Result as SolcResult,
Expand Down Expand Up @@ -420,7 +421,9 @@ pub fn setup_cast_project(test: TestProject) -> (TestProject, TestCommand) {
///
/// Test projects are created from a global atomic counter to avoid duplicates.
#[derive(Clone, Debug)]
pub struct TestProject<T: ArtifactOutput = ConfigurableArtifacts> {
pub struct TestProject<
T: ArtifactOutput<CompilerContract = Contract> + Default = ConfigurableArtifacts,
> {
/// The directory in which this test executable is running.
exe_root: PathBuf,
/// The project in which the test should run.
Expand Down

0 comments on commit 3784cd8

Please sign in to comment.