Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forge): fix cache search for verification #7053

Merged
merged 6 commits into from
Feb 10, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions crates/forge/bin/cmd/verify/etherscan/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -212,15 +214,26 @@ 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) = 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()))?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.ok_or_eyre(format!("Cache entry not found for {}", path.display()))?
.ok_or_else(|| eyre::eyre!("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)))
}

Expand Down Expand Up @@ -350,7 +363,7 @@ impl EtherscanVerificationProvider {
let path = match args.contract.path.as_ref() {
Some(path) => project.root().join(path),
None => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while we're at it, let's also do if let some else for this

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>:<name>",
)?;
path.to_owned()
Expand Down Expand Up @@ -391,7 +404,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::<Vec<_>>();
Expand Down Expand Up @@ -423,7 +436,7 @@ impl EtherscanVerificationProvider {
/// return whatever was set in the [VerifyArgs] args.
fn constructor_args(&mut self, args: &VerifyArgs, project: &Project) -> Result<Option<String>> {
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 =
Expand Down Expand Up @@ -474,6 +487,7 @@ mod tests {
use clap::Parser;
use foundry_cli::utils::LoadConfig;
use foundry_common::fs;
use foundry_test_utils::forgetest_async;
use tempfile::tempdir;

#[test]
Expand Down Expand Up @@ -613,4 +627,22 @@ 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",
"src/Counter1.sol:Counter",
"--root",
&prj.root().to_string_lossy(),
]);

let mut etherscan = EtherscanVerificationProvider::default();
etherscan.preflight_check(args).await.unwrap();
});
}
Loading