#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use alloy_consensus::{Header, EMPTY_OMMER_ROOT_HASH};
use alloy_primitives::U256;
use reth_chainspec::{EthChainSpec, EthereumHardfork, EthereumHardforks};
use reth_consensus::{Consensus, ConsensusError, PostExecutionInput};
use reth_consensus_common::validation::{
validate_4844_header_standalone, validate_against_parent_4844,
validate_against_parent_eip1559_base_fee, validate_against_parent_hash_number,
validate_against_parent_timestamp, validate_block_pre_execution, validate_body_against_header,
validate_header_base_fee, validate_header_extradata, validate_header_gas,
};
use reth_primitives::{BlockBody, BlockWithSenders, SealedBlock, SealedHeader};
use reth_primitives_traits::constants::MINIMUM_GAS_LIMIT;
use std::{fmt::Debug, sync::Arc, time::SystemTime};
pub const GAS_LIMIT_BOUND_DIVISOR: u64 = 1024;
mod validation;
pub use validation::validate_block_post_execution;
#[derive(Debug, Clone)]
pub struct EthBeaconConsensus<ChainSpec> {
chain_spec: Arc<ChainSpec>,
}
impl<ChainSpec: EthChainSpec + EthereumHardforks> EthBeaconConsensus<ChainSpec> {
pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
Self { chain_spec }
}
fn validate_against_parent_gas_limit(
&self,
header: &SealedHeader,
parent: &SealedHeader,
) -> Result<(), ConsensusError> {
let parent_gas_limit =
if self.chain_spec.fork(EthereumHardfork::London).transitions_at_block(header.number) {
parent.gas_limit *
self.chain_spec
.base_fee_params_at_timestamp(header.timestamp)
.elasticity_multiplier as u64
} else {
parent.gas_limit
};
if header.gas_limit > parent_gas_limit {
if header.gas_limit - parent_gas_limit >= parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR {
return Err(ConsensusError::GasLimitInvalidIncrease {
parent_gas_limit,
child_gas_limit: header.gas_limit,
})
}
}
else if parent_gas_limit - header.gas_limit >= parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR
{
return Err(ConsensusError::GasLimitInvalidDecrease {
parent_gas_limit,
child_gas_limit: header.gas_limit,
})
}
else if header.gas_limit < MINIMUM_GAS_LIMIT {
return Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: header.gas_limit })
}
Ok(())
}
}
impl<ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug> Consensus
for EthBeaconConsensus<ChainSpec>
{
fn validate_header(&self, header: &SealedHeader) -> Result<(), ConsensusError> {
validate_header_gas(header)?;
validate_header_base_fee(header, &self.chain_spec)?;
if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) &&
header.withdrawals_root.is_none()
{
return Err(ConsensusError::WithdrawalsRootMissing)
} else if !self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) &&
header.withdrawals_root.is_some()
{
return Err(ConsensusError::WithdrawalsRootUnexpected)
}
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp) {
validate_4844_header_standalone(header)?;
} else if header.blob_gas_used.is_some() {
return Err(ConsensusError::BlobGasUsedUnexpected)
} else if header.excess_blob_gas.is_some() {
return Err(ConsensusError::ExcessBlobGasUnexpected)
} else if header.parent_beacon_block_root.is_some() {
return Err(ConsensusError::ParentBeaconBlockRootUnexpected)
}
if self.chain_spec.is_prague_active_at_timestamp(header.timestamp) {
if header.requests_hash.is_none() {
return Err(ConsensusError::RequestsHashMissing)
}
} else if header.requests_hash.is_some() {
return Err(ConsensusError::RequestsHashUnexpected)
}
Ok(())
}
fn validate_header_against_parent(
&self,
header: &SealedHeader,
parent: &SealedHeader,
) -> Result<(), ConsensusError> {
validate_against_parent_hash_number(header, parent)?;
validate_against_parent_timestamp(header, parent)?;
self.validate_against_parent_gas_limit(header, parent)?;
validate_against_parent_eip1559_base_fee(header, parent, &self.chain_spec)?;
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp) {
validate_against_parent_4844(header, parent)?;
}
Ok(())
}
fn validate_header_with_total_difficulty(
&self,
header: &Header,
total_difficulty: U256,
) -> Result<(), ConsensusError> {
let is_post_merge = self
.chain_spec
.fork(EthereumHardfork::Paris)
.active_at_ttd(total_difficulty, header.difficulty);
if is_post_merge {
if !header.is_zero_difficulty() {
return Err(ConsensusError::TheMergeDifficultyIsNotZero)
}
if !header.nonce.is_zero() {
return Err(ConsensusError::TheMergeNonceIsNotZero)
}
if header.ommers_hash != EMPTY_OMMER_ROOT_HASH {
return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty)
}
validate_header_extradata(header)?;
} else {
let present_timestamp =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
if header.exceeds_allowed_future_timestamp(present_timestamp) {
return Err(ConsensusError::TimestampIsInFuture {
timestamp: header.timestamp,
present_timestamp,
})
}
validate_header_extradata(header)?;
}
Ok(())
}
fn validate_body_against_header(
&self,
body: &BlockBody,
header: &SealedHeader,
) -> Result<(), ConsensusError> {
validate_body_against_header(body, header)
}
fn validate_block_pre_execution(&self, block: &SealedBlock) -> Result<(), ConsensusError> {
validate_block_pre_execution(block, &self.chain_spec)
}
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,
input: PostExecutionInput<'_>,
) -> Result<(), ConsensusError> {
validate_block_post_execution(block, &self.chain_spec, input.receipts, input.requests)
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::B256;
use reth_chainspec::{ChainSpec, ChainSpecBuilder};
use reth_primitives::proofs;
fn header_with_gas_limit(gas_limit: u64) -> SealedHeader {
let header = Header { gas_limit, ..Default::default() };
SealedHeader::new(header, B256::ZERO)
}
#[test]
fn test_valid_gas_limit_increase() {
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit((parent.gas_limit + 5) as u64);
assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Ok(())
);
}
#[test]
fn test_gas_limit_below_minimum() {
let parent = header_with_gas_limit(MINIMUM_GAS_LIMIT);
let child = header_with_gas_limit(MINIMUM_GAS_LIMIT - 1);
assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: child.gas_limit as u64 })
);
}
#[test]
fn test_invalid_gas_limit_increase_exceeding_limit() {
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(
parent.gas_limit + parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR + 1,
);
assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Err(ConsensusError::GasLimitInvalidIncrease {
parent_gas_limit: parent.gas_limit,
child_gas_limit: child.gas_limit,
})
);
}
#[test]
fn test_valid_gas_limit_decrease_within_limit() {
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(parent.gas_limit - 5);
assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Ok(())
);
}
#[test]
fn test_invalid_gas_limit_decrease_exceeding_limit() {
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(
parent.gas_limit - parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1,
);
assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Err(ConsensusError::GasLimitInvalidDecrease {
parent_gas_limit: parent.gas_limit,
child_gas_limit: child.gas_limit,
})
);
}
#[test]
fn shanghai_block_zero_withdrawals() {
let chain_spec = Arc::new(ChainSpecBuilder::mainnet().shanghai_activated().build());
let header = Header {
base_fee_per_gas: Some(1337),
withdrawals_root: Some(proofs::calculate_withdrawals_root(&[])),
..Default::default()
};
assert_eq!(
EthBeaconConsensus::new(chain_spec).validate_header(&SealedHeader::seal(header,)),
Ok(())
);
}
}