mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: abstract OpBeaconConsensus over primitives and chainspec (#14171)
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
use alloy_consensus::Header;
|
||||
use reth_optimism_chainspec::OpChainSpec;
|
||||
use reth_optimism_forks::OpHardfork;
|
||||
use reth_optimism_forks::OpHardforks;
|
||||
|
||||
/// Map the latest active hardfork at the given header to a revm
|
||||
/// [`SpecId`](revm_primitives::SpecId).
|
||||
pub fn revm_spec(chain_spec: &OpChainSpec, header: &Header) -> revm_primitives::SpecId {
|
||||
pub fn revm_spec(chain_spec: impl OpHardforks, header: &Header) -> revm_primitives::SpecId {
|
||||
revm_spec_by_timestamp_after_bedrock(chain_spec, header.timestamp)
|
||||
}
|
||||
|
||||
@ -15,22 +14,22 @@ pub fn revm_spec(chain_spec: &OpChainSpec, header: &Header) -> revm_primitives::
|
||||
/// This is only intended to be used after the Bedrock, when hardforks are activated by
|
||||
/// timestamp.
|
||||
pub fn revm_spec_by_timestamp_after_bedrock(
|
||||
chain_spec: &OpChainSpec,
|
||||
chain_spec: impl OpHardforks,
|
||||
timestamp: u64,
|
||||
) -> revm_primitives::SpecId {
|
||||
if chain_spec.fork(OpHardfork::Isthmus).active_at_timestamp(timestamp) {
|
||||
if chain_spec.is_isthmus_active_at_timestamp(timestamp) {
|
||||
revm_primitives::ISTHMUS
|
||||
} else if chain_spec.fork(OpHardfork::Holocene).active_at_timestamp(timestamp) {
|
||||
} else if chain_spec.is_holocene_active_at_timestamp(timestamp) {
|
||||
revm_primitives::HOLOCENE
|
||||
} else if chain_spec.fork(OpHardfork::Granite).active_at_timestamp(timestamp) {
|
||||
} else if chain_spec.is_granite_active_at_timestamp(timestamp) {
|
||||
revm_primitives::GRANITE
|
||||
} else if chain_spec.fork(OpHardfork::Fjord).active_at_timestamp(timestamp) {
|
||||
} else if chain_spec.is_fjord_active_at_timestamp(timestamp) {
|
||||
revm_primitives::FJORD
|
||||
} else if chain_spec.fork(OpHardfork::Ecotone).active_at_timestamp(timestamp) {
|
||||
} else if chain_spec.is_ecotone_active_at_timestamp(timestamp) {
|
||||
revm_primitives::ECOTONE
|
||||
} else if chain_spec.fork(OpHardfork::Canyon).active_at_timestamp(timestamp) {
|
||||
} else if chain_spec.is_canyon_active_at_timestamp(timestamp) {
|
||||
revm_primitives::CANYON
|
||||
} else if chain_spec.fork(OpHardfork::Regolith).active_at_timestamp(timestamp) {
|
||||
} else if chain_spec.is_regolith_active_at_timestamp(timestamp) {
|
||||
revm_primitives::REGOLITH
|
||||
} else {
|
||||
revm_primitives::BEDROCK
|
||||
@ -51,35 +50,35 @@ mod tests {
|
||||
f(cs).build()
|
||||
}
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.isthmus_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.isthmus_activated()), 0),
|
||||
revm_primitives::ISTHMUS
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.holocene_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.holocene_activated()), 0),
|
||||
revm_primitives::HOLOCENE
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.granite_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.granite_activated()), 0),
|
||||
revm_primitives::GRANITE
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.fjord_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.fjord_activated()), 0),
|
||||
revm_primitives::FJORD
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.ecotone_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.ecotone_activated()), 0),
|
||||
revm_primitives::ECOTONE
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.canyon_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.canyon_activated()), 0),
|
||||
revm_primitives::CANYON
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.bedrock_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.bedrock_activated()), 0),
|
||||
revm_primitives::BEDROCK
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec_by_timestamp_after_bedrock(&op_cs(|cs| cs.regolith_activated()), 0),
|
||||
revm_spec_by_timestamp_after_bedrock(op_cs(|cs| cs.regolith_activated()), 0),
|
||||
revm_primitives::REGOLITH
|
||||
);
|
||||
}
|
||||
@ -92,35 +91,35 @@ mod tests {
|
||||
f(cs).build()
|
||||
}
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.isthmus_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.isthmus_activated()), &Default::default()),
|
||||
revm_primitives::ISTHMUS
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.holocene_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.holocene_activated()), &Default::default()),
|
||||
revm_primitives::HOLOCENE
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.granite_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.granite_activated()), &Default::default()),
|
||||
revm_primitives::GRANITE
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.fjord_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.fjord_activated()), &Default::default()),
|
||||
revm_primitives::FJORD
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.ecotone_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.ecotone_activated()), &Default::default()),
|
||||
revm_primitives::ECOTONE
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.canyon_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.canyon_activated()), &Default::default()),
|
||||
revm_primitives::CANYON
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.bedrock_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.bedrock_activated()), &Default::default()),
|
||||
revm_primitives::BEDROCK
|
||||
);
|
||||
assert_eq!(
|
||||
revm_spec(&op_cs(|cs| cs.regolith_activated()), &Default::default()),
|
||||
revm_spec(op_cs(|cs| cs.regolith_activated()), &Default::default()),
|
||||
revm_primitives::REGOLITH
|
||||
);
|
||||
}
|
||||
|
||||
@ -304,7 +304,7 @@ where
|
||||
receipts: &[N::Receipt],
|
||||
_requests: &Requests,
|
||||
) -> Result<(), ConsensusError> {
|
||||
validate_block_post_execution(block.header(), &self.chain_spec.clone(), receipts)
|
||||
validate_block_post_execution(block.header(), self.chain_spec.clone(), receipts)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -20,6 +20,8 @@ use op_alloy_consensus::EIP1559ParamError;
|
||||
use reth_chainspec::EthChainSpec;
|
||||
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, Database, Evm, NextBlockEnvAttributes};
|
||||
use reth_optimism_chainspec::OpChainSpec;
|
||||
use reth_optimism_consensus::next_block_base_fee;
|
||||
use reth_optimism_forks::OpHardforks;
|
||||
use reth_optimism_primitives::OpTransactionSigned;
|
||||
use reth_primitives_traits::FillTxEnv;
|
||||
use reth_revm::{
|
||||
@ -126,24 +128,30 @@ impl<EXT, DB: Database> Evm for OpEvm<'_, EXT, DB> {
|
||||
}
|
||||
|
||||
/// Optimism-related EVM configuration.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OpEvmConfig {
|
||||
chain_spec: Arc<OpChainSpec>,
|
||||
#[derive(Debug)]
|
||||
pub struct OpEvmConfig<ChainSpec = OpChainSpec> {
|
||||
chain_spec: Arc<ChainSpec>,
|
||||
}
|
||||
|
||||
impl OpEvmConfig {
|
||||
impl<ChainSpec> Clone for OpEvmConfig<ChainSpec> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { chain_spec: self.chain_spec.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<ChainSpec> OpEvmConfig<ChainSpec> {
|
||||
/// Creates a new [`OpEvmConfig`] with the given chain spec.
|
||||
pub const fn new(chain_spec: Arc<OpChainSpec>) -> Self {
|
||||
pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
|
||||
Self { chain_spec }
|
||||
}
|
||||
|
||||
/// Returns the chain spec associated with this configuration.
|
||||
pub const fn chain_spec(&self) -> &Arc<OpChainSpec> {
|
||||
pub const fn chain_spec(&self) -> &Arc<ChainSpec> {
|
||||
&self.chain_spec
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigureEvmEnv for OpEvmConfig {
|
||||
impl<ChainSpec: EthChainSpec + OpHardforks + 'static> ConfigureEvmEnv for OpEvmConfig<ChainSpec> {
|
||||
type Header = Header;
|
||||
type Transaction = OpTransactionSigned;
|
||||
type Error = EIP1559ParamError;
|
||||
@ -208,7 +216,11 @@ impl ConfigureEvmEnv for OpEvmConfig {
|
||||
prevrandao: Some(attributes.prev_randao),
|
||||
gas_limit: U256::from(attributes.gas_limit),
|
||||
// calculate basefee based on parent block's gas usage
|
||||
basefee: self.chain_spec.next_block_base_fee(parent, attributes.timestamp)?,
|
||||
basefee: U256::from(next_block_base_fee(
|
||||
&self.chain_spec,
|
||||
parent,
|
||||
attributes.timestamp,
|
||||
)?),
|
||||
// calculate excess gas based on parent block's blob gas usage
|
||||
blob_excess_gas_and_price,
|
||||
};
|
||||
@ -225,7 +237,7 @@ impl ConfigureEvmEnv for OpEvmConfig {
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigureEvm for OpEvmConfig {
|
||||
impl<ChainSpec: EthChainSpec + OpHardforks + 'static> ConfigureEvm for OpEvmConfig<ChainSpec> {
|
||||
type Evm<'a, DB: Database + 'a, I: 'a> = OpEvm<'a, I, DB>;
|
||||
type EvmError<DBError: core::error::Error + Send + Sync + 'static> = EVMError<DBError>;
|
||||
type HaltReason = HaltReason;
|
||||
|
||||
Reference in New Issue
Block a user