Skip to content

Commit

Permalink
refactor(levm): replace specId with Fork (#1762)
Browse files Browse the repository at this point in the history
**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

Ref #1741

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->
Replaces specId with Fork
- First change is in file `crates/vm/levm/src/environment.rs`
- - Changes in other relevant files to accept the changes and fix
cascading errors

<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes [#1741](#1741)

---------

Co-authored-by: Tomas Fabrizio Orsi <[email protected]>
Co-authored-by: Federico Borello <[email protected]>
Co-authored-by: fborello-lambda <[email protected]>
Co-authored-by: Leandro Ferrigno <[email protected]>
  • Loading branch information
5 people authored Jan 28, 2025
1 parent dcc90bf commit 69b0e30
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 192 deletions.
103 changes: 51 additions & 52 deletions cmd/ef_tests/levm/report.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::runner::{EFTestRunnerError, InternalError};
use colored::Colorize;
use ethrex_core::{Address, H256};
use ethrex_core::{types::Fork, Address, H256};
use ethrex_levm::{
errors::{TransactionReport, TxResult, VMError},
Account, StorageSlot,
};
use ethrex_storage::{error::StoreError, AccountUpdate};
use ethrex_vm::SpecId;
use itertools::Itertools;
use revm::primitives::{EVMError, ExecutionResult as RevmExecutionResult};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -133,21 +132,21 @@ pub fn summary_for_slack(reports: &[EFTestReport]) -> String {
}}
]
}}"#,
fork_summary_for_slack(reports, SpecId::PRAGUE),
fork_summary_for_slack(reports, SpecId::CANCUN),
fork_summary_for_slack(reports, SpecId::SHANGHAI),
fork_summary_for_slack(reports, SpecId::HOMESTEAD),
fork_summary_for_slack(reports, SpecId::ISTANBUL),
fork_summary_for_slack(reports, SpecId::LONDON),
fork_summary_for_slack(reports, SpecId::BYZANTIUM),
fork_summary_for_slack(reports, SpecId::BERLIN),
fork_summary_for_slack(reports, SpecId::CONSTANTINOPLE),
fork_summary_for_slack(reports, SpecId::MERGE),
fork_summary_for_slack(reports, SpecId::FRONTIER),
fork_summary_for_slack(reports, Fork::Prague),
fork_summary_for_slack(reports, Fork::Cancun),
fork_summary_for_slack(reports, Fork::Shanghai),
fork_summary_for_slack(reports, Fork::Byzantium),
fork_summary_for_slack(reports, Fork::Berlin),
fork_summary_for_slack(reports, Fork::Constantinople),
fork_summary_for_slack(reports, Fork::Paris),
fork_summary_for_slack(reports, Fork::Homestead),
fork_summary_for_slack(reports, Fork::Istanbul),
fork_summary_for_slack(reports, Fork::London),
fork_summary_for_slack(reports, Fork::Frontier),
)
}

fn fork_summary_for_slack(reports: &[EFTestReport], fork: SpecId) -> String {
fn fork_summary_for_slack(reports: &[EFTestReport], fork: Fork) -> String {
let fork_str: &str = fork.into();
let (fork_tests, fork_passed_tests, fork_success_percentage) = fork_statistics(reports, fork);
format!(r#"*{fork_str}:* {fork_passed_tests}/{fork_tests} ({fork_success_percentage:.2}%)"#)
Expand All @@ -173,21 +172,21 @@ pub fn summary_for_github(reports: &[EFTestReport]) -> String {
let success_percentage = (total_passed as f64 / total_run as f64) * 100.0;
format!(
r#"Summary: {total_passed}/{total_run} ({success_percentage:.2}%)\n\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n"#,
fork_summary_for_github(reports, SpecId::PRAGUE),
fork_summary_for_github(reports, SpecId::CANCUN),
fork_summary_for_github(reports, SpecId::SHANGHAI),
fork_summary_for_github(reports, SpecId::HOMESTEAD),
fork_summary_for_github(reports, SpecId::ISTANBUL),
fork_summary_for_github(reports, SpecId::LONDON),
fork_summary_for_github(reports, SpecId::BYZANTIUM),
fork_summary_for_github(reports, SpecId::BERLIN),
fork_summary_for_github(reports, SpecId::CONSTANTINOPLE),
fork_summary_for_github(reports, SpecId::MERGE),
fork_summary_for_github(reports, SpecId::FRONTIER),
fork_summary_for_github(reports, Fork::Prague),
fork_summary_for_github(reports, Fork::Cancun),
fork_summary_for_github(reports, Fork::Shanghai),
fork_summary_for_github(reports, Fork::Byzantium),
fork_summary_for_github(reports, Fork::Berlin),
fork_summary_for_github(reports, Fork::Constantinople),
fork_summary_for_github(reports, Fork::Paris),
fork_summary_for_github(reports, Fork::Homestead),
fork_summary_for_github(reports, Fork::Istanbul),
fork_summary_for_github(reports, Fork::London),
fork_summary_for_github(reports, Fork::Frontier),
)
}

fn fork_summary_for_github(reports: &[EFTestReport], fork: SpecId) -> String {
fn fork_summary_for_github(reports: &[EFTestReport], fork: Fork) -> String {
let fork_str: &str = fork.into();
let (fork_tests, fork_passed_tests, fork_success_percentage) = fork_statistics(reports, fork);
format!("{fork_str}: {fork_passed_tests}/{fork_tests} ({fork_success_percentage:.2}%)")
Expand Down Expand Up @@ -221,22 +220,22 @@ pub fn summary_for_shell(reports: &[EFTestReport]) -> String {
} else {
format!("{}", total_passed).red()
},
fork_summary_shell(reports, SpecId::PRAGUE),
fork_summary_shell(reports, SpecId::CANCUN),
fork_summary_shell(reports, SpecId::SHANGHAI),
fork_summary_shell(reports, SpecId::MERGE),
fork_summary_shell(reports, SpecId::LONDON),
fork_summary_shell(reports, SpecId::BERLIN),
fork_summary_shell(reports, SpecId::ISTANBUL),
fork_summary_shell(reports, SpecId::CONSTANTINOPLE),
fork_summary_shell(reports, SpecId::BYZANTIUM),
fork_summary_shell(reports, SpecId::HOMESTEAD),
fork_summary_shell(reports, SpecId::FRONTIER),
fork_summary_shell(reports, Fork::Prague),
fork_summary_shell(reports, Fork::Cancun),
fork_summary_shell(reports, Fork::Shanghai),
fork_summary_shell(reports, Fork::Paris),
fork_summary_shell(reports, Fork::London),
fork_summary_shell(reports, Fork::Berlin),
fork_summary_shell(reports, Fork::Istanbul),
fork_summary_shell(reports, Fork::Constantinople),
fork_summary_shell(reports, Fork::Byzantium),
fork_summary_shell(reports, Fork::Homestead),
fork_summary_shell(reports, Fork::Frontier),
test_dir_summary_for_shell(reports),
)
}

fn fork_summary_shell(reports: &[EFTestReport], fork: SpecId) -> String {
fn fork_summary_shell(reports: &[EFTestReport], fork: Fork) -> String {
let fork_str: &str = fork.into();
let (fork_tests, fork_passed_tests, fork_success_percentage) = fork_statistics(reports, fork);
format!(
Expand All @@ -252,7 +251,7 @@ fn fork_summary_shell(reports: &[EFTestReport], fork: SpecId) -> String {
)
}

fn fork_statistics(reports: &[EFTestReport], fork: SpecId) -> (usize, usize, f64) {
fn fork_statistics(reports: &[EFTestReport], fork: Fork) -> (usize, usize, f64) {
let fork_tests = reports.iter().filter(|report| report.fork == fork).count();
let fork_passed_tests = reports
.iter()
Expand Down Expand Up @@ -312,17 +311,17 @@ impl Display for EFTestsReport {
let total_run = self.0.len();
writeln!(f, "Summary: {total_passed}/{total_run}",)?;
writeln!(f)?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::PRAGUE))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::CANCUN))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::SHANGHAI))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::HOMESTEAD))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::ISTANBUL))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::LONDON))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::BYZANTIUM))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::BERLIN))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::CONSTANTINOPLE))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::MERGE))?;
writeln!(f, "{}", fork_summary_shell(&self.0, SpecId::FRONTIER))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Prague))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Cancun))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Shanghai))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Byzantium))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Berlin))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Constantinople))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Paris))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Homestead))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Istanbul))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::London))?;
writeln!(f, "{}", fork_summary_shell(&self.0, Fork::Frontier))?;
writeln!(f)?;
writeln!(f, "Failed tests:")?;
writeln!(f)?;
Expand Down Expand Up @@ -397,14 +396,14 @@ pub struct EFTestReport {
pub name: String,
pub dir: String,
pub test_hash: H256,
pub fork: SpecId,
pub fork: Fork,
pub skipped: bool,
pub failed_vectors: HashMap<TestVector, EFTestRunnerError>,
pub re_run_report: Option<TestReRunReport>,
}

impl EFTestReport {
pub fn new(name: String, dir: String, test_hash: H256, fork: SpecId) -> Self {
pub fn new(name: String, dir: String, test_hash: H256, fork: Fork) -> Self {
EFTestReport {
name,
dir,
Expand Down
2 changes: 1 addition & 1 deletion cmd/ef_tests/levm/runner/levm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn prepare_vm_for_tx(vector: &TestVector, test: &EFTest) -> Result<VM, EFTes
origin: tx.sender,
refunded_gas: 0,
gas_limit: tx.gas_limit,
spec_id: test.fork(),
fork: test.fork(),
block_number: test.env.current_number,
coinbase: test.env.current_coinbase,
timestamp: test.env.current_timestamp,
Expand Down
4 changes: 2 additions & 2 deletions cmd/ef_tests/levm/runner/revm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ethrex_levm::{
Account, StorageSlot,
};
use ethrex_storage::{error::StoreError, AccountUpdate};
use ethrex_vm::{db::StoreWrapper, EvmState, RevmAddress, RevmU256};
use ethrex_vm::{db::StoreWrapper, fork_to_spec_id, EvmState, RevmAddress, RevmU256};
use revm::{
db::State,
inspectors::TracerEip3155 as RevmTracerEip3155,
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn prepare_revm_for_tx<'state>(
.with_block_env(block_env)
.with_tx_env(tx_env)
.modify_cfg_env(|cfg| cfg.chain_id = chain_spec.chain_id)
.with_spec_id(test.fork())
.with_spec_id(fork_to_spec_id(test.fork()))
.with_external_context(
RevmTracerEip3155::new(Box::new(std::io::stderr())).without_summary(),
);
Expand Down
27 changes: 13 additions & 14 deletions cmd/ef_tests/levm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use crate::{
};
use bytes::Bytes;
use ethrex_core::{
types::{Genesis, GenesisAccount, TxKind},
types::{Fork, Genesis, GenesisAccount, TxKind},
Address, H256, U256,
};
use ethrex_vm::SpecId;
use serde::Deserialize;
use std::collections::HashMap;

Expand All @@ -33,21 +32,21 @@ pub struct EFTest {
}

impl EFTest {
pub fn fork(&self) -> SpecId {
pub fn fork(&self) -> Fork {
match &self.post {
EFTestPost::Prague(_) => SpecId::PRAGUE,
EFTestPost::Cancun(_) => SpecId::CANCUN,
EFTestPost::Shanghai(_) => SpecId::SHANGHAI,
EFTestPost::Homestead(_) => SpecId::HOMESTEAD,
EFTestPost::Istanbul(_) => SpecId::ISTANBUL,
EFTestPost::London(_) => SpecId::LONDON,
EFTestPost::Byzantium(_) => SpecId::BYZANTIUM,
EFTestPost::Berlin(_) => SpecId::BERLIN,
EFTestPost::Prague(_) => Fork::Prague,
EFTestPost::Cancun(_) => Fork::Cancun,
EFTestPost::Shanghai(_) => Fork::Shanghai,
EFTestPost::Homestead(_) => Fork::Homestead,
EFTestPost::Istanbul(_) => Fork::Istanbul,
EFTestPost::London(_) => Fork::London,
EFTestPost::Byzantium(_) => Fork::Byzantium,
EFTestPost::Berlin(_) => Fork::Berlin,
EFTestPost::Constantinople(_) | EFTestPost::ConstantinopleFix(_) => {
SpecId::CONSTANTINOPLE
Fork::Constantinople
}
EFTestPost::Paris(_) => SpecId::MERGE,
EFTestPost::Frontier(_) => SpecId::FRONTIER,
EFTestPost::Paris(_) => Fork::Paris,
EFTestPost::Frontier(_) => Fork::Frontier,
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,9 @@ fn apply_plain_transaction(
&context.payload.header,
store_wrapper.clone(),
block_cache,
spec_id(
&context.chain_config().map_err(ChainError::from)?,
context.payload.header.timestamp,
),
context
.chain_config()?
.fork(context.payload.header.timestamp),
)
.map_err(EvmError::from)?;

Expand Down
58 changes: 54 additions & 4 deletions crates/common/types/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,57 @@ pub struct ChainConfig {
pub terminal_total_difficulty_passed: bool,
}

#[derive(Debug, PartialEq, PartialOrd)]
#[repr(u8)]
#[derive(Debug, PartialEq, PartialOrd, Default, Clone, Copy, Serialize, Deserialize)]
pub enum Fork {
Paris = 0,
Shanghai = 1,
Cancun = 2,
Frontier = 0,
FrontierThawing = 1,
Homestead = 2,
DaoFork = 3,
Tangerine = 4,
SpuriousDragon = 5,
Byzantium = 6,
Constantinople = 7,
Petersburg = 8,
Istanbul = 9,
MuirGlacier = 10,
Berlin = 11,
London = 12,
ArrowGlacier = 13,
GrayGlacier = 14,
Paris = 15,
Shanghai = 16,
#[default]
Cancun = 17,
Prague = 18,
PragueEof = 19,
}

impl From<Fork> for &str {
fn from(fork: Fork) -> Self {
match fork {
Fork::Frontier => "Frontier",
Fork::FrontierThawing => "FrontierThawing",
Fork::Homestead => "Homestead",
Fork::DaoFork => "DaoFork",
Fork::Tangerine => "Tangerine",
Fork::SpuriousDragon => "SpuriousDragon",
Fork::Byzantium => "Byzantium",
Fork::Constantinople => "Constantinople",
Fork::Petersburg => "Petersburg",
Fork::Istanbul => "Istanbul",
Fork::MuirGlacier => "MuirGlacier",
Fork::Berlin => "Berlin",
Fork::London => "London",
Fork::ArrowGlacier => "ArrowGlacier",
Fork::GrayGlacier => "GrayGlacier",
Fork::Paris => "Paris",
Fork::Shanghai => "Shanghai",
Fork::Cancun => "Cancun",
Fork::Prague => "Prague",
Fork::PragueEof => "Prague EOF",
}
}
}

impl ChainConfig {
Expand Down Expand Up @@ -122,6 +168,10 @@ impl ChainConfig {
}
}

pub fn fork(&self, block_timestamp: u64) -> Fork {
self.get_fork(block_timestamp)
}

pub fn gather_forks(&self) -> (Vec<u64>, Vec<u64>) {
let block_number_based_forks: Vec<u64> = vec![
self.homestead_block,
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/levm/src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ethrex_core::{Address, H256, U256};
use ethrex_core::{types::Fork, Address, H256, U256};
pub use revm_primitives::SpecId;

use std::collections::HashMap;
Expand All @@ -12,7 +12,7 @@ pub struct Environment {
pub origin: Address,
pub refunded_gas: u64,
pub gas_limit: u64,
pub spec_id: SpecId,
pub fork: Fork,
pub block_number: U256,
pub coinbase: Address,
pub timestamp: U256,
Expand Down
Loading

0 comments on commit 69b0e30

Please sign in to comment.