Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header validator #12648

Merged
merged 4 commits into from
Nov 19, 2024
Merged
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
73 changes: 39 additions & 34 deletions crates/consensus/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,42 @@ impl<'a> PostExecutionInput<'a> {

/// Consensus is a protocol that chooses canonical chain.
#[auto_impl::auto_impl(&, Arc)]
pub trait Consensus<H = Header, B = BlockBody>: Debug + Send + Sync {
pub trait Consensus<H = Header, B = BlockBody>: HeaderValidator<H> + Debug + Send + Sync {
/// Ensures that body field values match the header.
fn validate_body_against_header(
&self,
body: &B,
header: &SealedHeader<H>,
) -> Result<(), ConsensusError>;

/// Validate a block disregarding world state, i.e. things that can be checked before sender
/// recovery and execution.
///
/// See the Yellow Paper sections 4.3.2 "Holistic Validity", 4.3.4 "Block Header Validity", and
/// 11.1 "Ommer Validation".
///
/// **This should not be called for the genesis block**.
///
/// Note: validating blocks does not include other validations of the Consensus
fn validate_block_pre_execution(&self, block: &SealedBlock<H, B>)
-> Result<(), ConsensusError>;

/// Validate a block considering world state, i.e. things that can not be checked before
/// execution.
///
/// See the Yellow Paper sections 4.3.2 "Holistic Validity".
///
/// Note: validating blocks does not include other validations of the Consensus
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,
input: PostExecutionInput<'_>,
) -> Result<(), ConsensusError>;
}

/// HeaderValidator is a protocol that validates headers and their relationships.
#[auto_impl::auto_impl(&, Arc)]
pub trait HeaderValidator<H = Header>: Debug + Send + Sync {
/// Validate if header is correct and follows consensus specification.
///
/// This is called on standalone header to check if all hashes are correct.
Expand All @@ -60,7 +95,8 @@ pub trait Consensus<H = Header, B = BlockBody>: Debug + Send + Sync {
///
/// **This should not be called for the genesis block**.
///
/// Note: Validating header against its parent does not include other Consensus validations.
/// Note: Validating header against its parent does not include other HeaderValidator
/// validations.
fn validate_header_against_parent(
&self,
header: &SealedHeader<H>,
Expand Down Expand Up @@ -99,43 +135,12 @@ pub trait Consensus<H = Header, B = BlockBody>: Debug + Send + Sync {
///
/// Some consensus engines may want to do additional checks here.
///
/// Note: validating headers with TD does not include other Consensus validation.
/// Note: validating headers with TD does not include other HeaderValidator validation.
fn validate_header_with_total_difficulty(
&self,
header: &H,
total_difficulty: U256,
) -> Result<(), ConsensusError>;

/// Ensures that body field values match the header.
fn validate_body_against_header(
&self,
body: &B,
header: &SealedHeader<H>,
) -> Result<(), ConsensusError>;

/// Validate a block disregarding world state, i.e. things that can be checked before sender
/// recovery and execution.
///
/// See the Yellow Paper sections 4.3.2 "Holistic Validity", 4.3.4 "Block Header Validity", and
/// 11.1 "Ommer Validation".
///
/// **This should not be called for the genesis block**.
///
/// Note: validating blocks does not include other validations of the Consensus
fn validate_block_pre_execution(&self, block: &SealedBlock<H, B>)
-> Result<(), ConsensusError>;

/// Validate a block considering world state, i.e. things that can not be checked before
/// execution.
///
/// See the Yellow Paper sections 4.3.2 "Holistic Validity".
///
/// Note: validating blocks does not include other validations of the Consensus
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,
input: PostExecutionInput<'_>,
) -> Result<(), ConsensusError>;
}

/// Consensus Errors
Expand Down
6 changes: 4 additions & 2 deletions crates/consensus/consensus/src/noop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Consensus, ConsensusError, PostExecutionInput};
use crate::{Consensus, ConsensusError, HeaderValidator, PostExecutionInput};
use alloy_primitives::U256;
use reth_primitives::{BlockWithSenders, SealedBlock, SealedHeader};

Expand All @@ -7,7 +7,7 @@ use reth_primitives::{BlockWithSenders, SealedBlock, SealedHeader};
#[non_exhaustive]
pub struct NoopConsensus;

impl<H, B> Consensus<H, B> for NoopConsensus {
impl<H> HeaderValidator<H> for NoopConsensus {
fn validate_header(&self, _header: &SealedHeader<H>) -> Result<(), ConsensusError> {
Ok(())
}
Expand All @@ -27,7 +27,9 @@ impl<H, B> Consensus<H, B> for NoopConsensus {
) -> Result<(), ConsensusError> {
Ok(())
}
}

impl<H, B> Consensus<H, B> for NoopConsensus {
fn validate_body_against_header(
&self,
_body: &B,
Expand Down
42 changes: 22 additions & 20 deletions crates/consensus/consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Consensus, ConsensusError, PostExecutionInput};
use crate::{Consensus, ConsensusError, HeaderValidator, PostExecutionInput};
use alloy_primitives::U256;
use core::sync::atomic::{AtomicBool, Ordering};
use reth_primitives::{BlockWithSenders, SealedBlock, SealedHeader};
Expand Down Expand Up @@ -47,18 +47,21 @@ impl TestConsensus {
}

impl<H, B> Consensus<H, B> for TestConsensus {
fn validate_header(&self, _header: &SealedHeader<H>) -> Result<(), ConsensusError> {
if self.fail_validation() {
fn validate_body_against_header(
&self,
_body: &B,
_header: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
if self.fail_body_against_header() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}

fn validate_header_against_parent(
fn validate_block_pre_execution(
&self,
_header: &SealedHeader<H>,
_parent: &SealedHeader<H>,
_block: &SealedBlock<H, B>,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
Expand All @@ -67,33 +70,32 @@ impl<H, B> Consensus<H, B> for TestConsensus {
}
}

fn validate_header_with_total_difficulty(
fn validate_block_post_execution(
&self,
_header: &H,
_total_difficulty: U256,
_block: &BlockWithSenders,
_input: PostExecutionInput<'_>,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
}

fn validate_body_against_header(
&self,
_body: &B,
_header: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
if self.fail_body_against_header() {
impl<H> HeaderValidator<H> for TestConsensus {
fn validate_header(&self, _header: &SealedHeader<H>) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}

fn validate_block_pre_execution(
fn validate_header_against_parent(
&self,
_block: &SealedBlock<H, B>,
_header: &SealedHeader<H>,
_parent: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
Expand All @@ -102,10 +104,10 @@ impl<H, B> Consensus<H, B> for TestConsensus {
}
}

fn validate_block_post_execution(
fn validate_header_with_total_difficulty(
&self,
_block: &BlockWithSenders,
_input: PostExecutionInput<'_>,
_header: &H,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
Expand Down
46 changes: 25 additions & 21 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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::{Consensus, ConsensusError, HeaderValidator, 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,
Expand Down Expand Up @@ -92,6 +92,30 @@ impl<ChainSpec: EthChainSpec + EthereumHardforks> EthBeaconConsensus<ChainSpec>

impl<ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug> Consensus
for EthBeaconConsensus<ChainSpec>
{
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)
}
}

impl<ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug> HeaderValidator
for EthBeaconConsensus<ChainSpec>
{
fn validate_header(&self, header: &SealedHeader) -> Result<(), ConsensusError> {
validate_header_gas(header)?;
Expand Down Expand Up @@ -210,26 +234,6 @@ impl<ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug> Consensu

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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/net/downloaders/src/headers/reverse_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::{stream::Stream, FutureExt};
use futures_util::{stream::FuturesUnordered, StreamExt};
use rayon::prelude::*;
use reth_config::config::HeadersConfig;
use reth_consensus::Consensus;
use reth_consensus::{Consensus, HeaderValidator};
use reth_network_p2p::{
error::{DownloadError, DownloadResult, PeerRequestResult},
headers::{
Expand Down
4 changes: 2 additions & 2 deletions crates/net/p2p/src/headers/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy_consensus::BlockHeader;
use alloy_eips::BlockHashOrNumber;
use alloy_primitives::B256;
use futures::Stream;
use reth_consensus::Consensus;
use reth_consensus::HeaderValidator;
use reth_primitives::SealedHeader;
use reth_primitives_traits::BlockWithParent;
/// A downloader capable of fetching and yielding block headers.
Expand Down Expand Up @@ -83,7 +83,7 @@ impl SyncTarget {
///
/// Returns Ok(false) if the
pub fn validate_header_download<H: BlockHeader>(
consensus: &dyn Consensus<H>,
consensus: &dyn HeaderValidator<H>,
header: &SealedHeader<H>,
parent: &SealedHeader<H>,
) -> DownloadResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/net/p2p/src/test_utils/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Stream for TestDownload {

let empty: SealedHeader = SealedHeader::default();
if let Err(error) =
Consensus::<_>::validate_header_against_parent(&this.consensus, &empty, &empty)
<dyn Consensus<_>>::validate_header_against_parent(&this.consensus, &empty, &empty)
{
this.done = true;
return Poll::Ready(Some(Err(DownloadError::HeaderValidation {
Expand Down
Loading
Loading