From 77a9da5a649ece0c7009df6dc35bbb9d41ccedb9 Mon Sep 17 00:00:00 2001 From: alpharush <0xalpharush@protonmail.com> Date: Mon, 5 Feb 2024 22:24:03 -0600 Subject: [PATCH] fix(chisel): validate that EVM version is compatible with solc version --- crates/chisel/src/session_source.rs | 29 ++++++++++++----------------- crates/chisel/tests/cache.rs | 26 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/crates/chisel/src/session_source.rs b/crates/chisel/src/session_source.rs index 322598e36940..36fb5cf1c167 100644 --- a/crates/chisel/src/session_source.rs +++ b/crates/chisel/src/session_source.rs @@ -8,7 +8,7 @@ use eyre::Result; use forge_fmt::solang_ext::SafeUnwrap; use foundry_compilers::{ artifacts::{Source, Sources}, - CompilerInput, CompilerOutput, EvmVersion, Solc, + CompilerInput, CompilerOutput, Solc, }; use foundry_config::{Config, SolcReq}; use foundry_evm::{backend::Backend, opts::EvmOpts}; @@ -105,22 +105,17 @@ impl SessionSourceConfig { match solc_req { SolcReq::Version(version) => { - // We now need to verify if the solc version provided is supported by the evm - // version set. If not, we bail and ask the user to provide a newer version. - // 1. Do we need solc 0.8.18 or higher? - let evm_version = self.foundry_config.evm_version; - let needs_post_merge_solc = evm_version >= EvmVersion::Paris; - // 2. Check if the version provided is less than 0.8.18 and bail, - // or leave it as-is if we don't need a post merge solc version or the version we - // have is good enough. - let v = if needs_post_merge_solc && version < Version::new(0, 8, 18) { - eyre::bail!("solc {version} is not supported by the set evm version: {evm_version}. Please install and use a version of solc higher or equal to 0.8.18. -You can also set the solc version in your foundry.toml.") - } else { - version.to_string() - }; + // Validate that the requested evm version is supported by the solc version + let req_evm_version = self.foundry_config.evm_version; + if let Some(compat_evm_version) = req_evm_version.normalize_version(&version) { + if req_evm_version > compat_evm_version { + eyre::bail!( + "The set evm version, {req_evm_version}, is not supported by solc {version}. Upgrade to a newer solc version." + ); + } + } - let mut solc = Solc::find_svm_installed_version(&v)?; + let mut solc = Solc::find_svm_installed_version(version.to_string())?; if solc.is_none() { if self.foundry_config.offline { @@ -131,7 +126,7 @@ You can also set the solc version in your foundry.toml.") Paint::green(format!("Installing solidity version {version}...")) ); Solc::blocking_install(&version)?; - solc = Solc::find_svm_installed_version(&v)?; + solc = Solc::find_svm_installed_version(version.to_string())?; } solc.ok_or_else(|| eyre::eyre!("Failed to install {version}")) } diff --git a/crates/chisel/tests/cache.rs b/crates/chisel/tests/cache.rs index 9173e8bc791d..c6b9625ebd7c 100644 --- a/crates/chisel/tests/cache.rs +++ b/crates/chisel/tests/cache.rs @@ -1,6 +1,6 @@ use chisel::session::ChiselSession; use foundry_compilers::EvmVersion; -use foundry_config::Config; +use foundry_config::{Config, SolcReq}; use serial_test::serial; use std::path::Path; @@ -220,3 +220,27 @@ fn test_load_latest_cache() { assert_eq!(new_env.id.unwrap(), "1"); assert_eq!(new_env.session_source.to_repl_source(), env.session_source.to_repl_source()); } + +#[test] +#[serial] +fn test_solc_evm_configuration_mismatch() { + // Create and clear the cache directory + ChiselSession::create_cache_dir().unwrap(); + ChiselSession::clear_cache().unwrap(); + + // Force the solc version to be 0.8.13 which does not support Paris + let foundry_config = Config { + evm_version: EvmVersion::Paris, + solc: Some(SolcReq::Version("0.8.13".parse().expect("invalid semver"))), + ..Default::default() + }; + + // Create a new session that is expected to fail + let error = ChiselSession::new(chisel::session_source::SessionSourceConfig { + foundry_config, + ..Default::default() + }) + .unwrap_err(); + + assert_eq!(error.to_string(), "The set evm version, paris, is not supported by solc 0.8.13. Upgrade to a newer solc version."); +}