From 3daacb7325c8678a8cde28b60dde6d13d03e0336 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Thu, 8 Feb 2024 23:38:22 +0300 Subject: [PATCH 1/5] fix --- crates/forge/bin/cmd/verify/etherscan/mod.rs | 53 +++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/crates/forge/bin/cmd/verify/etherscan/mod.rs b/crates/forge/bin/cmd/verify/etherscan/mod.rs index e973189036fb..60a749f0ef1c 100644 --- a/crates/forge/bin/cmd/verify/etherscan/mod.rs +++ b/crates/forge/bin/cmd/verify/etherscan/mod.rs @@ -1,7 +1,7 @@ use super::{provider::VerificationProvider, VerifyArgs, VerifyCheckArgs}; use crate::cmd::retry::RETRY_CHECK_ON_VERIFY; use alloy_json_abi::Function; -use eyre::{eyre, Context, Result}; +use eyre::{eyre, Context, OptionExt, Result}; use forge::hashbrown::HashSet; use foundry_block_explorers::{ errors::EtherscanError, @@ -11,7 +11,9 @@ use foundry_block_explorers::{ }; use foundry_cli::utils::{get_cached_entry_by_name, read_constructor_args_file, LoadConfig}; use foundry_common::{abi::encode_function_args, retry::Retry}; -use foundry_compilers::{artifacts::CompactContract, cache::CacheEntry, Project, Solc}; +use foundry_compilers::{ + artifacts::CompactContract, cache::CacheEntry, info::ContractInfo, Project, Solc, +}; use foundry_config::{Chain, Config, SolcReq}; use futures::FutureExt; use once_cell::sync::Lazy; @@ -212,15 +214,27 @@ impl EtherscanVerificationProvider { fn cache_entry( &mut self, project: &Project, - contract_name: &str, + contract: &ContractInfo, ) -> Result<&(PathBuf, CacheEntry, CompactContract)> { if let Some(ref entry) = self.cached_entry { return Ok(entry) } let cache = project.read_cache_file()?; - let (path, entry) = get_cached_entry_by_name(&cache, contract_name)?; - let contract: CompactContract = cache.read_artifact(path.clone(), contract_name)?; + let (path, entry) = match contract.path.as_ref() { + Some(path) => { + let path = project.root().join(path); + ( + path.clone(), + cache + .entry(&path) + .ok_or_eyre(format!("Cache entry not found for {}", path.display()))? + .to_owned(), + ) + } + None => get_cached_entry_by_name(&cache, &contract.name)?, + }; + let contract: CompactContract = cache.read_artifact(path.clone(), &contract.name)?; Ok(self.cached_entry.insert((path, entry, contract))) } @@ -350,7 +364,7 @@ impl EtherscanVerificationProvider { let path = match args.contract.path.as_ref() { Some(path) => project.root().join(path), None => { - let (path, _, _) = self.cache_entry(project, &args.contract.name).wrap_err( + let (path, _, _) = self.cache_entry(project, &args.contract).wrap_err( "If cache is disabled, contract info must be provided in the format :", )?; path.to_owned() @@ -391,7 +405,7 @@ impl EtherscanVerificationProvider { } } - let (_, entry, _) = self.cache_entry(project, &args.contract.name).wrap_err( + let (_, entry, _) = self.cache_entry(project, &args.contract).wrap_err( "If cache is disabled, compiler version must be either provided with `--compiler-version` option or set in foundry.toml" )?; let artifacts = entry.artifacts_versions().collect::>(); @@ -423,7 +437,7 @@ impl EtherscanVerificationProvider { /// return whatever was set in the [VerifyArgs] args. fn constructor_args(&mut self, args: &VerifyArgs, project: &Project) -> Result> { if let Some(ref constructor_args_path) = args.constructor_args_path { - let (_, _, contract) = self.cache_entry(project, &args.contract.name).wrap_err( + let (_, _, contract) = self.cache_entry(project, &args.contract).wrap_err( "Cache must be enabled in order to use the `--constructor-args-path` option", )?; let abi = @@ -471,9 +485,11 @@ async fn ensure_solc_build_metadata(version: Version) -> Result { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::Address; use clap::Parser; use foundry_cli::utils::LoadConfig; use foundry_common::fs; + use foundry_test_utils::{forgetest, forgetest_async}; use tempfile::tempdir; #[test] @@ -613,4 +629,25 @@ mod tests { "Cache must be enabled in order to use the `--constructor-args-path` option", ); } + + forgetest_async!( + respects_path_for_duplicate, + |prj, cmd| { + prj.add_source("Counter1", "contract Counter {}").unwrap(); + prj.add_source("Counter2", "contract Counter {}").unwrap(); + + cmd.args(["build", "--force"]).ensure_execute_success().unwrap(); + + let args = VerifyArgs::parse_from([ + "foundry-cli", + "0x0000000000000000000000000000000000000000", + &format!("src/Counter1.sol:Counter"), + "--root", + &prj.root().to_string_lossy() + ]); + + let mut etherscan = EtherscanVerificationProvider::default(); + etherscan.preflight_check(args).await.unwrap(); + } + ); } From 051e1929121b55d28f4387e6d472cdc224c04180 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Thu, 8 Feb 2024 23:40:52 +0300 Subject: [PATCH 2/5] fmt --- crates/forge/bin/cmd/verify/etherscan/mod.rs | 37 +++++++++----------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/crates/forge/bin/cmd/verify/etherscan/mod.rs b/crates/forge/bin/cmd/verify/etherscan/mod.rs index 60a749f0ef1c..728d3dff8e25 100644 --- a/crates/forge/bin/cmd/verify/etherscan/mod.rs +++ b/crates/forge/bin/cmd/verify/etherscan/mod.rs @@ -630,24 +630,21 @@ mod tests { ); } - forgetest_async!( - respects_path_for_duplicate, - |prj, cmd| { - prj.add_source("Counter1", "contract Counter {}").unwrap(); - prj.add_source("Counter2", "contract Counter {}").unwrap(); - - cmd.args(["build", "--force"]).ensure_execute_success().unwrap(); - - let args = VerifyArgs::parse_from([ - "foundry-cli", - "0x0000000000000000000000000000000000000000", - &format!("src/Counter1.sol:Counter"), - "--root", - &prj.root().to_string_lossy() - ]); - - let mut etherscan = EtherscanVerificationProvider::default(); - etherscan.preflight_check(args).await.unwrap(); - } - ); + forgetest_async!(respects_path_for_duplicate, |prj, cmd| { + prj.add_source("Counter1", "contract Counter {}").unwrap(); + prj.add_source("Counter2", "contract Counter {}").unwrap(); + + cmd.args(["build", "--force"]).ensure_execute_success().unwrap(); + + let args = VerifyArgs::parse_from([ + "foundry-cli", + "0x0000000000000000000000000000000000000000", + &format!("src/Counter1.sol:Counter"), + "--root", + &prj.root().to_string_lossy(), + ]); + + let mut etherscan = EtherscanVerificationProvider::default(); + etherscan.preflight_check(args).await.unwrap(); + }); } From aac80fb6c3a332614517e65bb89a2c61da2938d5 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Thu, 8 Feb 2024 23:42:31 +0300 Subject: [PATCH 3/5] clippy --- crates/forge/bin/cmd/verify/etherscan/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/forge/bin/cmd/verify/etherscan/mod.rs b/crates/forge/bin/cmd/verify/etherscan/mod.rs index 728d3dff8e25..ff55a339e782 100644 --- a/crates/forge/bin/cmd/verify/etherscan/mod.rs +++ b/crates/forge/bin/cmd/verify/etherscan/mod.rs @@ -485,11 +485,10 @@ async fn ensure_solc_build_metadata(version: Version) -> Result { #[cfg(test)] mod tests { use super::*; - use alloy_primitives::Address; use clap::Parser; use foundry_cli::utils::LoadConfig; use foundry_common::fs; - use foundry_test_utils::{forgetest, forgetest_async}; + use foundry_test_utils::forgetest_async; use tempfile::tempdir; #[test] @@ -639,7 +638,7 @@ mod tests { let args = VerifyArgs::parse_from([ "foundry-cli", "0x0000000000000000000000000000000000000000", - &format!("src/Counter1.sol:Counter"), + "src/Counter1.sol:Counter", "--root", &prj.root().to_string_lossy(), ]); From ecae00832a0c369274c3a7f5393044df8047e6ac Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Fri, 9 Feb 2024 00:49:01 +0300 Subject: [PATCH 4/5] if let some --- crates/forge/bin/cmd/verify/etherscan/mod.rs | 23 ++++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/crates/forge/bin/cmd/verify/etherscan/mod.rs b/crates/forge/bin/cmd/verify/etherscan/mod.rs index ff55a339e782..6fef761e3304 100644 --- a/crates/forge/bin/cmd/verify/etherscan/mod.rs +++ b/crates/forge/bin/cmd/verify/etherscan/mod.rs @@ -221,18 +221,17 @@ impl EtherscanVerificationProvider { } let cache = project.read_cache_file()?; - let (path, entry) = match contract.path.as_ref() { - Some(path) => { - let path = project.root().join(path); - ( - path.clone(), - cache - .entry(&path) - .ok_or_eyre(format!("Cache entry not found for {}", path.display()))? - .to_owned(), - ) - } - None => get_cached_entry_by_name(&cache, &contract.name)?, + let (path, entry) = if let Some(path) = contract.path.as_ref() { + let path = project.root().join(path); + ( + path.clone(), + cache + .entry(&path) + .ok_or_eyre(format!("Cache entry not found for {}", path.display()))? + .to_owned(), + ) + } else { + get_cached_entry_by_name(&cache, &contract.name)? }; let contract: CompactContract = cache.read_artifact(path.clone(), &contract.name)?; Ok(self.cached_entry.insert((path, entry, contract))) From 2799b23069142b022fbafa7873c5907ad47785d5 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Fri, 9 Feb 2024 01:04:51 +0300 Subject: [PATCH 5/5] fixes --- crates/forge/bin/cmd/verify/etherscan/mod.rs | 21 ++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/crates/forge/bin/cmd/verify/etherscan/mod.rs b/crates/forge/bin/cmd/verify/etherscan/mod.rs index 6fef761e3304..961b6db47d96 100644 --- a/crates/forge/bin/cmd/verify/etherscan/mod.rs +++ b/crates/forge/bin/cmd/verify/etherscan/mod.rs @@ -1,7 +1,7 @@ use super::{provider::VerificationProvider, VerifyArgs, VerifyCheckArgs}; use crate::cmd::retry::RETRY_CHECK_ON_VERIFY; use alloy_json_abi::Function; -use eyre::{eyre, Context, OptionExt, Result}; +use eyre::{eyre, Context, Result}; use forge::hashbrown::HashSet; use foundry_block_explorers::{ errors::EtherscanError, @@ -227,7 +227,9 @@ impl EtherscanVerificationProvider { path.clone(), cache .entry(&path) - .ok_or_eyre(format!("Cache entry not found for {}", path.display()))? + .ok_or_else(|| { + eyre::eyre!(format!("Cache entry not found for {}", path.display())) + })? .to_owned(), ) } else { @@ -360,14 +362,13 @@ impl EtherscanVerificationProvider { /// Get the target contract path. If it wasn't provided, attempt a lookup /// in cache. Validate the path indeed exists on disk. fn contract_path(&mut self, args: &VerifyArgs, project: &Project) -> Result { - let path = match args.contract.path.as_ref() { - Some(path) => project.root().join(path), - None => { - let (path, _, _) = self.cache_entry(project, &args.contract).wrap_err( - "If cache is disabled, contract info must be provided in the format :", - )?; - path.to_owned() - } + let path = if let Some(path) = args.contract.path.as_ref() { + project.root().join(path) + } else { + let (path, _, _) = self.cache_entry(project, &args.contract).wrap_err( + "If cache is disabled, contract info must be provided in the format :", + )?; + path.to_owned() }; // check that the provided contract is part of the source dir