Header validator (#12648)

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
This commit is contained in:
0xriazaka.eth
2024-11-19 11:42:40 +01:00
committed by GitHub
parent b78f20f5cb
commit e924bdab37
8 changed files with 152 additions and 137 deletions

View File

@ -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.
@ -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>,
@ -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

View File

@ -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};
@ -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(())
}
@ -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,

View File

@ -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};
@ -47,38 +47,6 @@ impl TestConsensus {
}
impl<H, B> Consensus<H, B> for TestConsensus {
fn validate_header(&self, _header: &SealedHeader<H>) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_against_parent(
&self,
_header: &SealedHeader<H>,
_parent: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_with_total_difficulty(
&self,
_header: &H,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_body_against_header(
&self,
_body: &B,
@ -114,3 +82,37 @@ impl<H, B> Consensus<H, B> for TestConsensus {
}
}
}
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_header_against_parent(
&self,
_header: &SealedHeader<H>,
_parent: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_with_total_difficulty(
&self,
_header: &H,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
}