From eab4717cc4bdef38834aaa6895b7e38f436e1cb4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 1 Jun 2024 06:58:56 +0200 Subject: [PATCH] chore: Arc ContractsByArtifact internally --- crates/cheatcodes/src/config.rs | 5 ++-- crates/common/src/contracts.rs | 39 ++++++++++++-------------------- crates/forge/src/multi_runner.rs | 2 +- crates/forge/src/result.rs | 8 +++---- crates/forge/src/runner.rs | 3 +-- crates/script/src/build.rs | 3 +-- crates/script/src/execute.rs | 4 ++-- crates/script/src/lib.rs | 4 ++-- 8 files changed, 26 insertions(+), 42 deletions(-) diff --git a/crates/cheatcodes/src/config.rs b/crates/cheatcodes/src/config.rs index 66aef3cff735..54ba1dad1d61 100644 --- a/crates/cheatcodes/src/config.rs +++ b/crates/cheatcodes/src/config.rs @@ -12,7 +12,6 @@ use semver::Version; use std::{ collections::HashMap, path::{Path, PathBuf}, - sync::Arc, time::Duration, }; @@ -50,7 +49,7 @@ pub struct CheatsConfig { /// Artifacts which are guaranteed to be fresh (either recompiled or cached). /// If Some, `vm.getDeployedCode` invocations are validated to be in scope of this list. /// If None, no validation is performed. - pub available_artifacts: Option>, + pub available_artifacts: Option, /// Version of the script/test contract which is currently running. pub running_version: Option, } @@ -60,7 +59,7 @@ impl CheatsConfig { pub fn new( config: &Config, evm_opts: EvmOpts, - available_artifacts: Option>, + available_artifacts: Option, script_wallets: Option, running_version: Option, ) -> Self { diff --git a/crates/common/src/contracts.rs b/crates/common/src/contracts.rs index e523c3813c58..c9f527a5ad16 100644 --- a/crates/common/src/contracts.rs +++ b/crates/common/src/contracts.rs @@ -10,11 +10,7 @@ use foundry_compilers::{ }, ArtifactId, }; -use std::{ - collections::BTreeMap, - ops::{Deref, DerefMut}, - str::FromStr, -}; +use std::{collections::BTreeMap, ops::Deref, str::FromStr, sync::Arc}; /// Libraries' runtime code always starts with the following instruction: /// `PUSH20 0x0000000000000000000000000000000000000000` @@ -24,7 +20,7 @@ const CALL_PROTECTION_BYTECODE_PREFIX: [u8; 21] = hex!("730000000000000000000000000000000000000000"); /// Container for commonly used contract data. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct ContractData { /// Contract name. pub name: String, @@ -52,7 +48,7 @@ type ArtifactWithContractRef<'a> = (&'a ArtifactId, &'a ContractData); /// Wrapper type that maps an artifact to a contract ABI and bytecode. #[derive(Clone, Default, Debug)] -pub struct ContractsByArtifact(pub BTreeMap); +pub struct ContractsByArtifact(Arc>); impl ContractsByArtifact { /// Creates a new instance by collecting all artifacts with present bytecode from an iterator. @@ -60,18 +56,17 @@ impl ContractsByArtifact { /// It is recommended to use this method with an output of /// [foundry_linking::Linker::get_linked_artifacts]. pub fn new(artifacts: impl IntoIterator) -> Self { - Self( - artifacts - .into_iter() - .filter_map(|(id, artifact)| { - let name = id.name.clone(); - - let CompactContractBytecode { abi, bytecode, deployed_bytecode } = artifact; - - Some((id, ContractData { name, abi: abi?, bytecode, deployed_bytecode })) - }) - .collect(), - ) + let map = artifacts + .into_iter() + .filter_map(|(id, artifact)| { + let name = id.name.clone(); + + let CompactContractBytecode { abi, bytecode, deployed_bytecode } = artifact; + + Some((id, ContractData { name, abi: abi?, bytecode, deployed_bytecode })) + }) + .collect(); + Self(Arc::new(map)) } /// Finds a contract which has a similar bytecode as `code`. @@ -232,12 +227,6 @@ impl Deref for ContractsByArtifact { } } -impl DerefMut for ContractsByArtifact { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - /// Wrapper type that maps an address to a contract identifier and contract ABI. pub type ContractsByAddress = BTreeMap; diff --git a/crates/forge/src/multi_runner.rs b/crates/forge/src/multi_runner.rs index 3653a0e3f064..d910215e9c26 100644 --- a/crates/forge/src/multi_runner.rs +++ b/crates/forge/src/multi_runner.rs @@ -186,7 +186,7 @@ impl MultiContractRunner { self.output.artifact_ids().collect(), ); let linked_contracts = linker.get_linked_artifacts(&contract.libraries).unwrap_or_default(); - let known_contracts = Arc::new(ContractsByArtifact::new(linked_contracts)); + let known_contracts = ContractsByArtifact::new(linked_contracts); let cheats_config = CheatsConfig::new( &self.config, diff --git a/crates/forge/src/result.rs b/crates/forge/src/result.rs index 2dd5508e85d6..e4c0518e238b 100644 --- a/crates/forge/src/result.rs +++ b/crates/forge/src/result.rs @@ -1,5 +1,6 @@ //! Test outcomes. +use crate::gas_report::GasReport; use alloy_primitives::{Address, Log}; use foundry_common::{ evm::Breakpoints, get_contract_name, get_file_name, shell, ContractsByArtifact, @@ -16,13 +17,10 @@ use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, HashMap}, fmt::{self, Write}, - sync::Arc, time::Duration, }; use yansi::Paint; -use crate::gas_report::GasReport; - /// The aggregated result of a test run. #[derive(Clone, Debug)] pub struct TestOutcome { @@ -202,7 +200,7 @@ pub struct SuiteResult { pub libraries: Libraries, /// Contracts linked with correct libraries. #[serde(skip)] - pub known_contracts: Arc, + pub known_contracts: ContractsByArtifact, } impl SuiteResult { @@ -211,7 +209,7 @@ impl SuiteResult { test_results: BTreeMap, warnings: Vec, libraries: Libraries, - known_contracts: Arc, + known_contracts: ContractsByArtifact, ) -> Self { Self { duration, test_results, warnings, libraries, known_contracts } } diff --git a/crates/forge/src/runner.rs b/crates/forge/src/runner.rs index 33a2ce64ad54..ea6b6efe70e3 100644 --- a/crates/forge/src/runner.rs +++ b/crates/forge/src/runner.rs @@ -40,7 +40,6 @@ use std::{ borrow::Cow, cmp::min, collections::{BTreeMap, HashMap}, - sync::Arc, time::Instant, }; @@ -258,7 +257,7 @@ impl<'a> ContractRunner<'a> { mut self, filter: &dyn TestFilter, test_options: &TestOptions, - known_contracts: Arc, + known_contracts: ContractsByArtifact, handle: &tokio::runtime::Handle, ) -> SuiteResult { info!("starting tests"); diff --git a/crates/script/src/build.rs b/crates/script/src/build.rs index 3bf9e154bf93..a2462e4b2d85 100644 --- a/crates/script/src/build.rs +++ b/crates/script/src/build.rs @@ -146,10 +146,9 @@ impl LinkedBuildData { } /// Fetches target bytecode from linked contracts. - pub fn get_target_contract(&self) -> Result { + pub fn get_target_contract(&self) -> Result<&ContractData> { self.known_contracts .get(&self.build_data.target) - .cloned() .ok_or_eyre("target not found in linked artifacts") } } diff --git a/crates/script/src/execute.rs b/crates/script/src/execute.rs index afba19f22f06..2b57468e276b 100644 --- a/crates/script/src/execute.rs +++ b/crates/script/src/execute.rs @@ -73,13 +73,13 @@ impl LinkedState { args, script_config, script_wallets, - build_data, execution_data: ExecutionData { func, calldata, bytecode: bytecode.clone(), - abi: target_contract.abi, + abi: target_contract.abi.clone(), }, + build_data, }) } } diff --git a/crates/script/src/lib.rs b/crates/script/src/lib.rs index 88ab0782a851..39059ed212cc 100644 --- a/crates/script/src/lib.rs +++ b/crates/script/src/lib.rs @@ -44,7 +44,7 @@ use foundry_evm::{ }; use foundry_wallets::MultiWalletOpts; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, sync::Arc}; +use std::collections::HashMap; use yansi::Paint; mod broadcast; @@ -591,7 +591,7 @@ impl ScriptConfig { CheatsConfig::new( &self.config, self.evm_opts.clone(), - Some(Arc::new(known_contracts)), + Some(known_contracts), Some(script_wallets), Some(target.version), )