Skip to content

Commit

Permalink
chore: move revm_spec methods from reth-primitives to chain speci…
Browse files Browse the repository at this point in the history
…fic crates (#9152)
  • Loading branch information
joshieDo authored Jun 27, 2024
1 parent 9fd2cf0 commit d8e6d01
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 165 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ethereum/engine-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true
[dependencies]
# reth
reth-chainspec.workspace = true
reth-evm-ethereum.workspace = true
reth-primitives.workspace = true
reth-engine-primitives.workspace = true
reth-payload-primitives.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions crates/ethereum/engine-primitives/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
use alloy_rlp::Encodable;
use reth_chainspec::ChainSpec;
use reth_evm_ethereum::revm_spec_by_timestamp_after_merge;
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes};
use reth_primitives::{
constants::EIP1559_INITIAL_BASE_FEE, revm::config::revm_spec_by_timestamp_after_merge, Address,
BlobTransactionSidecar, EthereumHardfork, Header, SealedBlock, Withdrawals, B256, U256,
constants::EIP1559_INITIAL_BASE_FEE, Address, BlobTransactionSidecar, EthereumHardfork, Header,
SealedBlock, Withdrawals, B256, U256,
};
use reth_rpc_types::engine::{
ExecutionPayloadEnvelopeV2, ExecutionPayloadEnvelopeV3, ExecutionPayloadEnvelopeV4,
Expand Down
1 change: 1 addition & 0 deletions crates/ethereum/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true
[dependencies]
# Reth
reth-chainspec.workspace = true
reth-ethereum-forks.workspace = true
reth-evm.workspace = true
reth-primitives.workspace = true
reth-revm.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "optimism")]
use reth_chainspec::OptimismHardfork;
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_ethereum_forks::{EthereumHardfork, Head};

Expand All @@ -11,21 +9,6 @@ pub fn revm_spec_by_timestamp_after_merge(
chain_spec: &ChainSpec,
timestamp: u64,
) -> revm_primitives::SpecId {
#[cfg(feature = "optimism")]
if chain_spec.is_optimism() {
return if chain_spec.fork(OptimismHardfork::Fjord).active_at_timestamp(timestamp) {
revm_primitives::FJORD
} else if chain_spec.fork(OptimismHardfork::Ecotone).active_at_timestamp(timestamp) {
revm_primitives::ECOTONE
} else if chain_spec.fork(OptimismHardfork::Canyon).active_at_timestamp(timestamp) {
revm_primitives::CANYON
} else if chain_spec.fork(OptimismHardfork::Regolith).active_at_timestamp(timestamp) {
revm_primitives::REGOLITH
} else {
revm_primitives::BEDROCK
}
}

if chain_spec.is_prague_active_at_timestamp(timestamp) {
revm_primitives::PRAGUE
} else if chain_spec.is_cancun_active_at_timestamp(timestamp) {
Expand All @@ -38,47 +21,32 @@ pub fn revm_spec_by_timestamp_after_merge(
}

/// return `revm_spec` from spec configuration.
pub fn revm_spec(chain_spec: &ChainSpec, block: Head) -> revm_primitives::SpecId {
#[cfg(feature = "optimism")]
if chain_spec.is_optimism() {
if chain_spec.fork(OptimismHardfork::Fjord).active_at_head(&block) {
return revm_primitives::FJORD
} else if chain_spec.fork(OptimismHardfork::Ecotone).active_at_head(&block) {
return revm_primitives::ECOTONE
} else if chain_spec.fork(OptimismHardfork::Canyon).active_at_head(&block) {
return revm_primitives::CANYON
} else if chain_spec.fork(OptimismHardfork::Regolith).active_at_head(&block) {
return revm_primitives::REGOLITH
} else if chain_spec.fork(OptimismHardfork::Bedrock).active_at_head(&block) {
return revm_primitives::BEDROCK
}
}

if chain_spec.fork(EthereumHardfork::Prague).active_at_head(&block) {
pub fn revm_spec(chain_spec: &ChainSpec, block: &Head) -> revm_primitives::SpecId {
if chain_spec.fork(EthereumHardfork::Prague).active_at_head(block) {
revm_primitives::PRAGUE
} else if chain_spec.fork(EthereumHardfork::Cancun).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Cancun).active_at_head(block) {
revm_primitives::CANCUN
} else if chain_spec.fork(EthereumHardfork::Shanghai).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Shanghai).active_at_head(block) {
revm_primitives::SHANGHAI
} else if chain_spec.fork(EthereumHardfork::Paris).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Paris).active_at_head(block) {
revm_primitives::MERGE
} else if chain_spec.fork(EthereumHardfork::London).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::London).active_at_head(block) {
revm_primitives::LONDON
} else if chain_spec.fork(EthereumHardfork::Berlin).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Berlin).active_at_head(block) {
revm_primitives::BERLIN
} else if chain_spec.fork(EthereumHardfork::Istanbul).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Istanbul).active_at_head(block) {
revm_primitives::ISTANBUL
} else if chain_spec.fork(EthereumHardfork::Petersburg).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Petersburg).active_at_head(block) {
revm_primitives::PETERSBURG
} else if chain_spec.fork(EthereumHardfork::Byzantium).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Byzantium).active_at_head(block) {
revm_primitives::BYZANTIUM
} else if chain_spec.fork(EthereumHardfork::SpuriousDragon).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::SpuriousDragon).active_at_head(block) {
revm_primitives::SPURIOUS_DRAGON
} else if chain_spec.fork(EthereumHardfork::Tangerine).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Tangerine).active_at_head(block) {
revm_primitives::TANGERINE
} else if chain_spec.fork(EthereumHardfork::Homestead).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Homestead).active_at_head(block) {
revm_primitives::HOMESTEAD
} else if chain_spec.fork(EthereumHardfork::Frontier).active_at_head(&block) {
} else if chain_spec.fork(EthereumHardfork::Frontier).active_at_head(block) {
revm_primitives::FRONTIER
} else {
panic!(
Expand Down Expand Up @@ -114,137 +82,84 @@ mod tests {
revm_spec_by_timestamp_after_merge(&ChainSpecBuilder::mainnet().build(), 0),
revm_primitives::MERGE
);
#[cfg(feature = "optimism")]
{
#[inline(always)]
fn op_cs(f: impl FnOnce(ChainSpecBuilder) -> ChainSpecBuilder) -> ChainSpec {
let cs = ChainSpecBuilder::mainnet().chain(reth_chainspec::Chain::from_id(10));
f(cs).build()
}
assert_eq!(
revm_spec_by_timestamp_after_merge(&op_cs(|cs| cs.fjord_activated()), 0),
revm_primitives::FJORD
);
assert_eq!(
revm_spec_by_timestamp_after_merge(&op_cs(|cs| cs.ecotone_activated()), 0),
revm_primitives::ECOTONE
);
assert_eq!(
revm_spec_by_timestamp_after_merge(&op_cs(|cs| cs.canyon_activated()), 0),
revm_primitives::CANYON
);
assert_eq!(
revm_spec_by_timestamp_after_merge(&op_cs(|cs| cs.bedrock_activated()), 0),
revm_primitives::BEDROCK
);
assert_eq!(
revm_spec_by_timestamp_after_merge(&op_cs(|cs| cs.regolith_activated()), 0),
revm_primitives::REGOLITH
);
}
}

#[test]
fn test_to_revm_spec() {
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().cancun_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().cancun_activated().build(), &Head::default()),
revm_primitives::CANCUN
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().shanghai_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().shanghai_activated().build(), &Head::default()),
revm_primitives::SHANGHAI
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().paris_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().paris_activated().build(), &Head::default()),
revm_primitives::MERGE
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().london_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().london_activated().build(), &Head::default()),
revm_primitives::LONDON
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().berlin_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().berlin_activated().build(), &Head::default()),
revm_primitives::BERLIN
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().istanbul_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().istanbul_activated().build(), &Head::default()),
revm_primitives::ISTANBUL
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().petersburg_activated().build(), Head::default()),
revm_spec(
&ChainSpecBuilder::mainnet().petersburg_activated().build(),
&Head::default()
),
revm_primitives::PETERSBURG
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().byzantium_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().byzantium_activated().build(), &Head::default()),
revm_primitives::BYZANTIUM
);
assert_eq!(
revm_spec(
&ChainSpecBuilder::mainnet().spurious_dragon_activated().build(),
Head::default()
&Head::default()
),
revm_primitives::SPURIOUS_DRAGON
);
assert_eq!(
revm_spec(
&ChainSpecBuilder::mainnet().tangerine_whistle_activated().build(),
Head::default()
&Head::default()
),
revm_primitives::TANGERINE
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().homestead_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().homestead_activated().build(), &Head::default()),
revm_primitives::HOMESTEAD
);
assert_eq!(
revm_spec(&ChainSpecBuilder::mainnet().frontier_activated().build(), Head::default()),
revm_spec(&ChainSpecBuilder::mainnet().frontier_activated().build(), &Head::default()),
revm_primitives::FRONTIER
);
#[cfg(feature = "optimism")]
{
#[inline(always)]
fn op_cs(f: impl FnOnce(ChainSpecBuilder) -> ChainSpecBuilder) -> ChainSpec {
let cs = ChainSpecBuilder::mainnet().chain(reth_chainspec::Chain::from_id(10));
f(cs).build()
}
assert_eq!(
revm_spec(&op_cs(|cs| cs.fjord_activated()), Head::default()),
revm_primitives::FJORD
);
assert_eq!(
revm_spec(&op_cs(|cs| cs.ecotone_activated()), Head::default()),
revm_primitives::ECOTONE
);
assert_eq!(
revm_spec(&op_cs(|cs| cs.canyon_activated()), Head::default()),
revm_primitives::CANYON
);
assert_eq!(
revm_spec(&op_cs(|cs| cs.bedrock_activated()), Head::default()),
revm_primitives::BEDROCK
);
assert_eq!(
revm_spec(&op_cs(|cs| cs.regolith_activated()), Head::default()),
revm_primitives::REGOLITH
);
}
}

#[test]
fn test_eth_spec() {
assert_eq!(
revm_spec(&MAINNET, Head { timestamp: 1710338135, ..Default::default() }),
revm_spec(&MAINNET, &Head { timestamp: 1710338135, ..Default::default() }),
revm_primitives::CANCUN
);
assert_eq!(
revm_spec(&MAINNET, Head { timestamp: 1681338455, ..Default::default() }),
revm_spec(&MAINNET, &Head { timestamp: 1681338455, ..Default::default() }),
revm_primitives::SHANGHAI
);

assert_eq!(
revm_spec(
&MAINNET,
Head {
&Head {
total_difficulty: U256::from(58_750_000_000_000_000_000_010_u128),
difficulty: U256::from(10_u128),
..Default::default()
Expand All @@ -256,7 +171,7 @@ mod tests {
assert_eq!(
revm_spec(
&MAINNET,
Head {
&Head {
number: 15537394 - 10,
total_difficulty: U256::from(58_750_000_000_000_000_000_010_u128),
difficulty: U256::from(10_u128),
Expand All @@ -266,39 +181,39 @@ mod tests {
revm_primitives::MERGE
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 15537394 - 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 15537394 - 10, ..Default::default() }),
revm_primitives::LONDON
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 12244000 + 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 12244000 + 10, ..Default::default() }),
revm_primitives::BERLIN
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 12244000 - 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 12244000 - 10, ..Default::default() }),
revm_primitives::ISTANBUL
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 7280000 + 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 7280000 + 10, ..Default::default() }),
revm_primitives::PETERSBURG
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 7280000 - 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 7280000 - 10, ..Default::default() }),
revm_primitives::BYZANTIUM
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 2675000 + 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 2675000 + 10, ..Default::default() }),
revm_primitives::SPURIOUS_DRAGON
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 2675000 - 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 2675000 - 10, ..Default::default() }),
revm_primitives::TANGERINE
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 1150000 + 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 1150000 + 10, ..Default::default() }),
revm_primitives::HOMESTEAD
);
assert_eq!(
revm_spec(&MAINNET, Head { number: 1150000 - 10, ..Default::default() }),
revm_spec(&MAINNET, &Head { number: 1150000 - 10, ..Default::default() }),
revm_primitives::FRONTIER
);
}
Expand Down
Loading

0 comments on commit d8e6d01

Please sign in to comment.