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(config): prints error when accessing invalid etherscan config #9951

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

andelf
Copy link

@andelf andelf commented Feb 25, 2025

Motivation

If any [etherscan] config entry has a wrong name, then the whole [etherscan] becomes unreachable.

This is difficult to debug since the only error message is "Error: Invalid API Key".
Actually user has already provided a correct key.

To reproduce, add the following to ~/.foundry/foundry.toml:

[etherscan]

eth = { key = "xx" }
bsc = { key = "xx" }
arbi = { key = "xxxx" } # NOTE: the name "arbi" is an invalid chain name. This entry invalidates the whole `[etherscan]` section.

Then all etherscan operations becomes "Invalid API Key" errors.

Solution

  • Skip invalid entry in ResolvedEtherscanConfig and prints error log. -- the config unit tests depend on this behaviour
  • print an error message to denote that [etherscan] section is invalid

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

match config {
Ok(c) if c.chain == Some(chain) => return Some(Ok(c)),
Err(e) => return Some(Err(e)),
Err(e) => {
warn!("invalid etherscan config for {:?}: {:?}", name, e);
Copy link
Member

Choose a reason for hiding this comment

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

Hi @andelf thanks for your PR!

Would you mind changing warn! to let _ = sh_warn!(..)

This will make it so the warning is outputted to the user's terminal rather than to the tracing target for internal debugging

Would you mind adding a unit test that validates the change?

Thanks!

Copy link
Author

@andelf andelf Feb 25, 2025

Choose a reason for hiding this comment

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

It's deeply coupled with other config testings. So I guess it would be better to refuse invalid config and output errors.

Copy link
Author

Choose a reason for hiding this comment

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

comments addressed. PTAL.

@andelf andelf force-pushed the fix/skip-illegal-etherscan-configs branch from 982c0dc to c2e14b3 Compare February 25, 2025 16:02
@andelf andelf changed the title fix(config): skip invalid etherscan config name entry with error log fix(config): prints error when accessing invalid etherscan config Feb 25, 2025
@zerosnacks
Copy link
Member

Perhaps I'm missing something but running the example case in your description with the current version of Foundry results in:

Error: At least one of `url` or `chain` must be present for Etherscan config with unknown alias `arbi`

or alternatively if the key is incorrect or missing

Error: failed to extract foundry config:
foundry config error: missing field `key` for setting `etherscan.arbi`

https://book.getfoundry.sh/reference/config/etherscan specifically states the ability to specify custom chains (even if incorrect) and will throw this error

Would you mind giving a brief example of the issue that produces the reported error?

@andelf
Copy link
Author

andelf commented Feb 26, 2025

@zerosnacks It's counterintuitive that another invalid name in [etherscan] will disable all the other "key" settings.
Previously, cast source will fail silently printing a message saying "Error: Invalid API Key". Actually no key is used.

conditions:

  • Anther name with higher ordering: "arb" is greater than something like "bsc": BTreeMap is used to store the values.
  • cast source uses a .ok() after an Error type with chaining-call style.(where this PR added the error message).

@@ -127,12 +127,10 @@ impl StorageArgs {
}
}

if !self.etherscan.has_key() {
Copy link
Author

Choose a reason for hiding this comment

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

This check does not match the later api_key assignment.
In my case, this check fails, but config.get_etherscan_api_key works.

@andelf andelf force-pushed the fix/skip-illegal-etherscan-configs branch from bdff14b to 544d035 Compare March 4, 2025 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants