chore: refactor BlockExecutionError into validation and internal errors (#9911)

This commit is contained in:
Dan Cline
2024-07-30 14:30:30 -04:00
committed by GitHub
parent dec0c44035
commit 9586b9a4f5
6 changed files with 72 additions and 33 deletions

View File

@ -116,15 +116,67 @@ pub enum BlockValidationError {
/// `BlockExecutor` Errors
#[derive(thiserror_no_std::Error, Debug)]
pub enum BlockExecutionError {
/// Validation error, transparently wrapping `BlockValidationError`
/// Validation error, transparently wrapping [`BlockValidationError`]
#[error(transparent)]
Validation(#[from] BlockValidationError),
/// Pruning error, transparently wrapping `PruneSegmentError`
#[error(transparent)]
Pruning(#[from] PruneSegmentError),
/// Consensus error, transparently wrapping `ConsensusError`
/// Consensus error, transparently wrapping [`ConsensusError`]
#[error(transparent)]
Consensus(#[from] ConsensusError),
/// Internal, i.e. non consensus or validation related Block Executor Errors
#[error(transparent)]
Internal(#[from] InternalBlockExecutionError),
}
impl From<ProviderError> for BlockExecutionError {
fn from(value: ProviderError) -> Self {
InternalBlockExecutionError::from(value).into()
}
}
impl From<PruneSegmentError> for BlockExecutionError {
fn from(value: PruneSegmentError) -> Self {
InternalBlockExecutionError::from(value).into()
}
}
impl BlockExecutionError {
/// Create a new [`BlockExecutionError::Internal`] variant, containing a
/// [`InternalBlockExecutionError::Other`] error.
#[cfg(feature = "std")]
pub fn other<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Internal(InternalBlockExecutionError::other(error))
}
/// Create a new [`BlockExecutionError::Internal`] variant, containing a
/// [`InternalBlockExecutionError::Other`] error with the given message.
#[cfg(feature = "std")]
pub fn msg(msg: impl std::fmt::Display) -> Self {
Self::Internal(InternalBlockExecutionError::msg(msg))
}
/// Returns the inner `BlockValidationError` if the error is a validation error.
pub const fn as_validation(&self) -> Option<&BlockValidationError> {
match self {
Self::Validation(err) => Some(err),
_ => None,
}
}
/// Returns `true` if the error is a state root error.
pub const fn is_state_root_error(&self) -> bool {
matches!(self, Self::Validation(BlockValidationError::StateRoot(_)))
}
}
/// Internal (i.e., not validation or consensus related) `BlockExecutor` Errors
#[derive(thiserror_no_std::Error, Debug)]
pub enum InternalBlockExecutionError {
/// Pruning error, transparently wrapping [`PruneSegmentError`]
#[error(transparent)]
Pruning(#[from] PruneSegmentError),
/// Error when appending chain on fork is not possible
#[error(
"appending chain on fork (other_chain_fork:?) is not possible as the tip is {chain_tip:?}"
@ -144,8 +196,8 @@ pub enum BlockExecutionError {
Other(Box<dyn std::error::Error + Send + Sync>),
}
impl BlockExecutionError {
/// Create a new `BlockExecutionError::Other` variant.
impl InternalBlockExecutionError {
/// Create a new [`InternalBlockExecutionError::Other`] variant.
#[cfg(feature = "std")]
pub fn other<E>(error: E) -> Self
where
@ -154,22 +206,9 @@ impl BlockExecutionError {
Self::Other(Box::new(error))
}
/// Create a new [`BlockExecutionError::Other`] from a given message.
/// Create a new [`InternalBlockExecutionError::Other`] from a given message.
#[cfg(feature = "std")]
pub fn msg(msg: impl std::fmt::Display) -> Self {
Self::Other(msg.to_string().into())
}
/// Returns the inner `BlockValidationError` if the error is a validation error.
pub const fn as_validation(&self) -> Option<&BlockValidationError> {
match self {
Self::Validation(err) => Some(err),
_ => None,
}
}
/// Returns `true` if the error is a state root error.
pub const fn is_state_root_error(&self) -> bool {
matches!(self, Self::Validation(BlockValidationError::StateRoot(_)))
}
}

View File

@ -1,7 +1,7 @@
//! Contains [Chain], a chain of blocks and their final state.
use crate::ExecutionOutcome;
use reth_execution_errors::BlockExecutionError;
use reth_execution_errors::{BlockExecutionError, InternalBlockExecutionError};
use reth_primitives::{
Address, BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlock,
SealedBlockWithSenders, SealedHeader, TransactionSigned, TransactionSignedEcRecovered, TxHash,
@ -261,10 +261,11 @@ impl Chain {
let chain_tip = self.tip();
let other_fork_block = other.fork_block();
if chain_tip.hash() != other_fork_block.hash {
return Err(BlockExecutionError::AppendChainDoesntConnect {
return Err(InternalBlockExecutionError::AppendChainDoesntConnect {
chain_tip: Box::new(chain_tip.num_hash()),
other_chain_fork: Box::new(other_fork_block),
})
}
.into())
}
// Insert blocks from other chain