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

Incorrect Default EVM Version for Solidity Compiler 0.4.21-0.5.4 #189

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all 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
58 changes: 58 additions & 0 deletions crates/artifacts/solc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,30 @@ pub enum EvmVersion {
}

impl EvmVersion {
/// Find the default EVM version for the given compiler version.
pub fn default_version_solc(version: &Version) -> Option<Self> {
// In most cases, Solc compilers use the highest EVM version available at the time.
let default = Self::default().normalize_version_solc(version)?;

// However, there are some exceptions where the default is lower than the highest available.
match default {
Self::Constantinople => {
// Actually, Constantinople is never used as the default EVM version by Solidity
// compilers.
Some(Self::Byzantium)
}
Self::Cancun if *version == Version::new(0, 8, 24) => {
// While Cancun is introduced at the time of releasing 0.8.24, it has not been
// supported by the mainnet. So, the default EVM version of Solc 0.8.24 remains as
// Shanghai.
//
// <https://soliditylang.org/blog/2024/01/26/solidity-0.8.24-release-announcement/>
Some(Self::Shanghai)
}
_ => Some(default),
}
}

/// Normalizes this EVM version by checking against the given Solc [`Version`].
pub fn normalize_version_solc(self, version: &Version) -> Option<Self> {
// The EVM version flag was only added in 0.4.21; we work our way backwards
Expand Down Expand Up @@ -1903,6 +1927,40 @@ mod tests {
}
}

#[test]
fn test_evm_version_default() {
for &(solc_version, expected) in &[
// Everything before 0.4.21 should always return None
("0.4.20", None),
// Byzantium clipping
("0.4.21", Some(EvmVersion::Byzantium)),
// Constantinople bug fix
("0.4.22", Some(EvmVersion::Byzantium)),
// Petersburg
("0.5.5", Some(EvmVersion::Petersburg)),
// Istanbul
("0.5.14", Some(EvmVersion::Istanbul)),
// Berlin
("0.8.5", Some(EvmVersion::Berlin)),
// London
("0.8.7", Some(EvmVersion::London)),
// Paris
("0.8.18", Some(EvmVersion::Paris)),
// Shanghai
("0.8.20", Some(EvmVersion::Shanghai)),
// Cancun
("0.8.24", Some(EvmVersion::Shanghai)),
("0.8.25", Some(EvmVersion::Cancun)),
] {
let version = Version::from_str(solc_version).unwrap();
assert_eq!(
EvmVersion::default_version_solc(&version),
expected,
"({version}, {expected:?})"
)
}
}

#[test]
fn test_evm_version_normalization() {
for &(solc_version, evm_version, expected) in &[
Expand Down