Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ tokio = { version = "1", features = ["sync"] }
reth-interfaces = { path = "../interfaces", features = ["test-utils"] }
reth-provider = { path = "../storage/provider", features = ["test-utils"] }
assert_matches = "1.5.0"

[features]
optimism = []
8 changes: 8 additions & 0 deletions crates/consensus/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::{

use reth_primitives::constants;

#[cfg(feature = "optimism")]
use reth_primitives::TxDeposit;

/// Validate header standalone
pub fn validate_header_standalone(
header: &SealedHeader,
Expand Down Expand Up @@ -69,6 +72,11 @@ pub fn validate_transaction_regarding_header(
base_fee: Option<u64>,
) -> Result<(), Error> {
let chain_id = match transaction {
#[cfg(feature = "optimism")]
Transaction::Deposit(TxDeposit { .. }) => {
// TODO: get the chain id
None
}
Transaction::Legacy(TxLegacy { chain_id, .. }) => {
// EIP-155: Simple replay attack protection: https://eips.ethereum.org/EIPS/eip-155
if chain_spec.fork(Hardfork::SpuriousDragon).active_at_block(at_block_number) &&
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pprof = { version = "0.11", features = ["flamegraph", "frame-pointer", "criterio

[features]
default = []
optimism = []
arbitrary = [
"revm-primitives/arbitrary",
"dep:arbitrary",
Expand Down
44 changes: 44 additions & 0 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub static MAINNET: Lazy<ChainSpec> = Lazy::new(|| ChainSpec {
},
),
]),
#[cfg(feature = "optimism")]
optimism: None,
});

/// The Goerli spec
Expand All @@ -60,6 +62,8 @@ pub static GOERLI: Lazy<ChainSpec> = Lazy::new(|| ChainSpec {
ForkCondition::TTD { fork_block: None, total_difficulty: U256::from(10_790_000) },
),
]),
#[cfg(feature = "optimism")]
optimism: None,
});

/// The Sepolia spec
Expand Down Expand Up @@ -92,6 +96,8 @@ pub static SEPOLIA: Lazy<ChainSpec> = Lazy::new(|| ChainSpec {
),
(Hardfork::Shanghai, ForkCondition::Timestamp(1677557088)),
]),
#[cfg(feature = "optimism")]
optimism: None,
});

/// An Ethereum chain specification.
Expand All @@ -118,6 +124,20 @@ pub struct ChainSpec {

/// The active hard forks and their activation conditions
pub hardforks: BTreeMap<Hardfork, ForkCondition>,

/// Optimism configuration
#[cfg(feature = "optimism")]
pub optimism: Option<OptimismConfig>,
}

/// Optimism configuration.
#[cfg(feature = "optimism")]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OptimismConfig {
/// Elasticity multiplier as defined in [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)
pub eip_1559_elasticity: u64,
/// Base fee max change denominator as defined in [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)
pub eip_1559_denominator: u64,
}

impl ChainSpec {
Expand Down Expand Up @@ -293,6 +313,8 @@ impl From<EthersGenesis> for ChainSpec {
genesis: genesis_block,
genesis_hash: None,
hardforks,
#[cfg(feature = "optimism")]
optimism: None,
}
}
}
Expand Down Expand Up @@ -334,6 +356,8 @@ pub struct ChainSpecBuilder {
chain: Option<Chain>,
genesis: Option<Genesis>,
hardforks: BTreeMap<Hardfork, ForkCondition>,
#[cfg(feature = "optimism")]
optimism: Option<OptimismConfig>,
}

impl ChainSpecBuilder {
Expand All @@ -343,6 +367,8 @@ impl ChainSpecBuilder {
chain: Some(MAINNET.chain),
genesis: Some(MAINNET.genesis.clone()),
hardforks: MAINNET.hardforks.clone(),
#[cfg(feature = "optimism")]
optimism: None,
}
}

Expand Down Expand Up @@ -443,6 +469,20 @@ impl ChainSpecBuilder {
self
}

/// Enable Bedrock at genesis
#[cfg(feature = "optimism")]
pub fn bedrock_activated(mut self) -> Self {
self.hardforks.insert(Hardfork::Bedrock, ForkCondition::Block(0));
self
}

/// Enable Bedrock at genesis
#[cfg(feature = "optimism")]
pub fn regolith_activated(mut self) -> Self {
self.hardforks.insert(Hardfork::Regolith, ForkCondition::Timestamp(0));
self
}

/// Build the resulting [`ChainSpec`].
///
/// # Panics
Expand All @@ -455,6 +495,8 @@ impl ChainSpecBuilder {
genesis: self.genesis.expect("The genesis is required"),
genesis_hash: None,
hardforks: self.hardforks,
#[cfg(feature = "optimism")]
optimism: self.optimism,
}
}
}
Expand All @@ -465,6 +507,8 @@ impl From<&ChainSpec> for ChainSpecBuilder {
chain: Some(value.chain),
genesis: Some(value.genesis.clone()),
hardforks: value.hardforks.clone(),
#[cfg(feature = "optimism")]
optimism: value.optimism.clone(),
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions crates/primitives/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ pub enum Hardfork {
Paris,
/// Shanghai.
Shanghai,
/// Bedrock.
#[cfg(feature = "optimism")]
Bedrock,
/// Regolith
#[cfg(feature = "optimism")]
Regolith,
}

impl Hardfork {
Expand Down Expand Up @@ -82,6 +88,10 @@ impl FromStr for Hardfork {
"grayglacier" => Hardfork::GrayGlacier,
"paris" => Hardfork::Paris,
"shanghai" => Hardfork::Shanghai,
#[cfg(feature = "optimism")]
"bedrock" => Hardfork::Bedrock,
#[cfg(feature = "optimism")]
"regolith" => Hardfork::Regolith,
_ => return Err(format!("Unknown hardfork: {s}")),
};
Ok(hardfork)
Expand Down Expand Up @@ -146,6 +156,18 @@ mod tests {
assert_eq!(hardforks, expected_hardforks);
}

#[test]
#[cfg(feature = "optimism")]
fn check_op_hardfork_from_str() {
let hardfork_str = ["beDrOck", "rEgOlITH"];
let expected_hardforks = [Hardfork::Bedrock, Hardfork::Regolith];

let hardforks: Vec<Hardfork> =
hardfork_str.iter().map(|h| Hardfork::from_str(h).unwrap()).collect();

assert_eq!(hardforks, expected_hardforks);
}

#[test]
fn check_nonexistent_hardfork_from_str() {
assert!(Hardfork::from_str("not a hardfork").is_err());
Expand All @@ -158,6 +180,8 @@ mod tests {
genesis: Genesis::default(),
genesis_hash: None,
hardforks: BTreeMap::from([(Hardfork::Frontier, ForkCondition::Never)]),
#[cfg(feature = "optimism")]
optimism: None,
};

assert_eq!(Hardfork::Frontier.fork_id(&spec), None);
Expand All @@ -170,6 +194,8 @@ mod tests {
genesis: Genesis::default(),
genesis_hash: None,
hardforks: BTreeMap::from([(Hardfork::Shanghai, ForkCondition::Never)]),
#[cfg(feature = "optimism")]
optimism: None,
};

assert_eq!(Hardfork::Shanghai.fork_filter(&spec), None);
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub use transaction::{
Transaction, TransactionKind, TransactionSigned, TransactionSignedEcRecovered, TxEip1559,
TxEip2930, TxLegacy, TxType,
};
#[cfg(feature = "optimism")]
pub use transaction::{TxDeposit, DEPOSIT_TX_TYPE, DEPOSIT_VERSION};
pub use withdrawal::Withdrawal;

/// A block hash.
Expand Down
Loading