Skip to content

Commit

Permalink
Incorrect Default EVM Version for Solidity Compiler 0.4.21-0.5.4 (#189)
Browse files Browse the repository at this point in the history
The default EVM version for Solidity compilers ranging from 0.4.21 to
0.5.4 is incorrectly implemented. Specifically, although the
`Constantinople` hard fork had been introduced during this period, the
default EVM version remains as `Byzantium`. After version 0.5.5, the
default EVM version shifts to `Petersburg`.

That is, `Constantinople` is never used as the default EVM version for
Solidity compilers.

This can be confirmed by running `solc --help`, with the output
indicating the default EVM version as follows:

```bash
$ solc --help
solc, the Solidity commandline compiler.

...

Allowed options:
  --help               Show help message and exit.
  --version            Show version and exit.
  --license            Show licensing information and exit.
  --evm-version version
                       Select desired EVM version. Either homestead,
                       tangerineWhistle, spuriousDragon, byzantium (default) or
                       constantinople.
  ...

$ solc --version
solc, the Solidity commandline interface
Version: 0.4.24+commit.e67f0147.Linux.g++
```
  • Loading branch information
ZhangZhuoSJTU authored Aug 26, 2024
1 parent d7a4b87 commit f2b260f
Showing 1 changed file with 58 additions and 0 deletions.
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

0 comments on commit f2b260f

Please sign in to comment.