Skip to content

Commit

Permalink
fix: normalize default evm version if solc configured (#7018)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 6, 2024
1 parent f957e82 commit 2b24a10
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,29 @@ impl Config {
self
}

/// Normalizes the evm version if a [SolcReq] is set
pub fn normalized_evm_version(mut self) -> Self {
self.normalize_evm_version();
self
}

/// Normalizes the evm version if a [SolcReq] is set to a valid version.
pub fn normalize_evm_version(&mut self) {
self.evm_version = self.get_normalized_evm_version();
}

/// Returns the normalized [EvmVersion] if a [SolcReq] is set to a valid version.
///
/// Otherwise it returns the configured [EvmVersion].
pub fn get_normalized_evm_version(&self) -> EvmVersion {
if let Some(SolcReq::Version(version)) = &self.solc {
if let Some(evm_version) = self.evm_version.normalize_version(version) {
return evm_version;
}
}
self.evm_version
}

/// Returns a sanitized version of the Config where are paths are set correctly and potential
/// duplicates are resolved
///
Expand Down Expand Up @@ -1552,10 +1575,30 @@ impl Config {
figment = figment.merge(provider);
figment
}

/// Check if any defaults need to be normalized.
///
/// This normalizes the default `evm_version` if a `solc` was provided in the config.
///
/// See also <https://github.com/foundry-rs/foundry/issues/7014>
fn normalize_defaults(&mut self, figment: Figment) -> Figment {
if let Ok(version) = figment.extract_inner::<Version>("solc") {
// check if evm_version is set
// TODO: add a warning if evm_version is provided but incompatible
if figment.find_value("evm_version").is_err() {
if let Some(version) = self.evm_version.normalize_version(&version) {
// normalize evm_version based on the provided solc version
self.evm_version = version;
}
}
}

figment
}
}

impl From<Config> for Figment {
fn from(c: Config) -> Figment {
fn from(mut c: Config) -> Figment {
let profile = Config::selected_profile();
let mut figment = Figment::default().merge(DappHardhatDirProvider(&c.__root.0));

Expand Down Expand Up @@ -1622,6 +1665,9 @@ impl From<Config> for Figment {
};
let merge = figment.merge(remappings);

// normalize defaults
let merge = c.normalize_defaults(merge);

Figment::from(c).merge(merge).select(profile)
}
}
Expand Down Expand Up @@ -3877,7 +3923,7 @@ mod tests {
#[test]
fn can_handle_deviating_dapp_aliases() {
figment::Jail::expect_with(|jail| {
let addr = Address::random();
let addr = Address::ZERO;
jail.set_env("DAPP_TEST_NUMBER", 1337);
jail.set_env("DAPP_TEST_ADDRESS", format!("{addr:?}"));
jail.set_env("DAPP_TEST_FUZZ_RUNS", 420);
Expand Down Expand Up @@ -4377,6 +4423,23 @@ mod tests {
});
}

#[test]
fn test_normalize_defaults() {
figment::Jail::expect_with(|jail| {
jail.create_file(
"foundry.toml",
r"
[default]
solc = '0.8.13'
",
)?;

let loaded = Config::load().sanitized();
assert_eq!(loaded.evm_version, EvmVersion::London);
Ok(())
});
}

// a test to print the config, mainly used to update the example config in the README
#[test]
#[ignore]
Expand Down

0 comments on commit 2b24a10

Please sign in to comment.