reth_blockchain_tree_api/
error.rsuse alloy_consensus::BlockHeader;
use alloy_primitives::{BlockHash, BlockNumber};
use reth_consensus::ConsensusError;
use reth_execution_errors::{
BlockExecutionError, BlockValidationError, InternalBlockExecutionError,
};
use reth_primitives::{SealedBlock, SealedBlockFor};
use reth_primitives_traits::{Block, BlockBody};
pub use reth_storage_errors::provider::ProviderError;
#[derive(Debug, Clone, Copy, thiserror::Error, Eq, PartialEq)]
pub enum BlockchainTreeError {
#[error("block number is lower than the last finalized block number #{last_finalized}")]
PendingBlockIsFinalized {
last_finalized: BlockNumber,
},
#[error("chainId can't be found in BlockchainTree with internal index {chain_id}")]
BlockSideChainIdConsistency {
chain_id: u64,
},
#[error("canonical chain header {block_hash} can't be found")]
CanonicalChain {
block_hash: BlockHash,
},
#[error("block number #{block_number} not found in blockchain tree chain")]
BlockNumberNotFoundInChain {
block_number: BlockNumber,
},
#[error("block hash {block_hash} not found in blockchain tree chain")]
BlockHashNotFoundInChain {
block_hash: BlockHash,
},
#[error("block with hash {block_hash} failed to buffer")]
BlockBufferingFailed {
block_hash: BlockHash,
},
#[error("genesis block has no parent")]
GenesisBlockHasNoParent,
}
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
pub enum CanonicalError {
#[error(transparent)]
Validation(#[from] BlockValidationError),
#[error(transparent)]
BlockchainTree(#[from] BlockchainTreeError),
#[error(transparent)]
Provider(#[from] ProviderError),
#[error("transaction error on revert: {0}")]
CanonicalRevert(String),
#[error("transaction error on commit: {0}")]
CanonicalCommit(String),
#[error("transaction error on revert: {0}")]
OptimisticTargetRevert(BlockNumber),
}
impl CanonicalError {
pub const fn is_fatal(&self) -> bool {
matches!(self, Self::CanonicalCommit(_) | Self::CanonicalRevert(_))
}
pub const fn is_block_hash_not_found(&self) -> bool {
matches!(self, Self::BlockchainTree(BlockchainTreeError::BlockHashNotFoundInChain { .. }))
}
pub const fn optimistic_revert_block_number(&self) -> Option<BlockNumber> {
match self {
Self::OptimisticTargetRevert(block_number) => Some(*block_number),
_ => None,
}
}
}
#[derive(thiserror::Error)]
#[error(transparent)]
pub struct InsertBlockError {
inner: Box<InsertBlockErrorData>,
}
impl InsertBlockError {
pub fn new(block: SealedBlock, kind: InsertBlockErrorKind) -> Self {
Self { inner: InsertBlockErrorData::boxed(block, kind) }
}
pub fn tree_error(error: BlockchainTreeError, block: SealedBlock) -> Self {
Self::new(block, InsertBlockErrorKind::Tree(error))
}
pub fn consensus_error(error: ConsensusError, block: SealedBlock) -> Self {
Self::new(block, InsertBlockErrorKind::Consensus(error))
}
pub fn sender_recovery_error(block: SealedBlock) -> Self {
Self::new(block, InsertBlockErrorKind::SenderRecovery)
}
pub fn execution_error(error: BlockExecutionError, block: SealedBlock) -> Self {
Self::new(block, InsertBlockErrorKind::Execution(error))
}
#[inline]
pub fn into_block(self) -> SealedBlock {
self.inner.block
}
#[inline]
pub const fn kind(&self) -> &InsertBlockErrorKind {
&self.inner.kind
}
#[inline]
pub const fn block(&self) -> &SealedBlock {
&self.inner.block
}
#[inline]
pub fn split(self) -> (SealedBlock, InsertBlockErrorKind) {
let inner = *self.inner;
(inner.block, inner.kind)
}
}
impl std::fmt::Debug for InsertBlockError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.inner, f)
}
}
#[derive(thiserror::Error, Debug)]
#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {kind}",
.block.hash(),
.block.number,
.block.parent_hash)]
struct InsertBlockErrorData {
block: SealedBlock,
#[source]
kind: InsertBlockErrorKind,
}
impl InsertBlockErrorData {
const fn new(block: SealedBlock, kind: InsertBlockErrorKind) -> Self {
Self { block, kind }
}
fn boxed(block: SealedBlock, kind: InsertBlockErrorKind) -> Box<Self> {
Box::new(Self::new(block, kind))
}
}
#[derive(thiserror::Error)]
#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {}",
.block.hash(),
.block.number(),
.block.parent_hash(),
.kind)]
struct InsertBlockErrorDataTwo<B: Block> {
block: SealedBlockFor<B>,
#[source]
kind: InsertBlockErrorKindTwo,
}
impl<B: Block> std::fmt::Debug for InsertBlockErrorDataTwo<B> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InsertBlockError")
.field("error", &self.kind)
.field("hash", &self.block.hash())
.field("number", &self.block.number())
.field("parent_hash", &self.block.parent_hash())
.field("num_txs", &self.block.body.transactions().len())
.finish_non_exhaustive()
}
}
impl<B: Block> InsertBlockErrorDataTwo<B> {
const fn new(block: SealedBlockFor<B>, kind: InsertBlockErrorKindTwo) -> Self {
Self { block, kind }
}
fn boxed(block: SealedBlockFor<B>, kind: InsertBlockErrorKindTwo) -> Box<Self> {
Box::new(Self::new(block, kind))
}
}
#[derive(thiserror::Error)]
#[error(transparent)]
pub struct InsertBlockErrorTwo<B: Block> {
inner: Box<InsertBlockErrorDataTwo<B>>,
}
impl<B: Block> InsertBlockErrorTwo<B> {
pub fn new(block: SealedBlockFor<B>, kind: InsertBlockErrorKindTwo) -> Self {
Self { inner: InsertBlockErrorDataTwo::boxed(block, kind) }
}
pub fn consensus_error(error: ConsensusError, block: SealedBlockFor<B>) -> Self {
Self::new(block, InsertBlockErrorKindTwo::Consensus(error))
}
pub fn sender_recovery_error(block: SealedBlockFor<B>) -> Self {
Self::new(block, InsertBlockErrorKindTwo::SenderRecovery)
}
pub fn execution_error(error: BlockExecutionError, block: SealedBlockFor<B>) -> Self {
Self::new(block, InsertBlockErrorKindTwo::Execution(error))
}
#[inline]
pub fn into_block(self) -> SealedBlockFor<B> {
self.inner.block
}
#[inline]
pub const fn kind(&self) -> &InsertBlockErrorKindTwo {
&self.inner.kind
}
#[inline]
pub const fn block(&self) -> &SealedBlockFor<B> {
&self.inner.block
}
#[inline]
pub fn split(self) -> (SealedBlockFor<B>, InsertBlockErrorKindTwo) {
let inner = *self.inner;
(inner.block, inner.kind)
}
}
impl<B: Block> std::fmt::Debug for InsertBlockErrorTwo<B> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.inner, f)
}
}
#[derive(Debug, thiserror::Error)]
pub enum InsertBlockErrorKindTwo {
#[error("failed to recover senders for block")]
SenderRecovery,
#[error(transparent)]
Consensus(#[from] ConsensusError),
#[error(transparent)]
Execution(#[from] BlockExecutionError),
#[error(transparent)]
Provider(#[from] ProviderError),
#[error(transparent)]
Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
}
impl InsertBlockErrorKindTwo {
pub fn ensure_validation_error(
self,
) -> Result<InsertBlockValidationError, InsertBlockFatalError> {
match self {
Self::SenderRecovery => Ok(InsertBlockValidationError::SenderRecovery),
Self::Consensus(err) => Ok(InsertBlockValidationError::Consensus(err)),
Self::Execution(err) => {
match err {
BlockExecutionError::Validation(err) => {
Ok(InsertBlockValidationError::Validation(err))
}
BlockExecutionError::Consensus(err) => {
Ok(InsertBlockValidationError::Consensus(err))
}
BlockExecutionError::Internal(error) => {
Err(InsertBlockFatalError::BlockExecutionError(error))
}
}
}
Self::Provider(err) => Err(InsertBlockFatalError::Provider(err)),
Self::Other(err) => Err(InternalBlockExecutionError::Other(err).into()),
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum InsertBlockFatalError {
#[error(transparent)]
Provider(#[from] ProviderError),
#[error(transparent)]
BlockExecutionError(#[from] InternalBlockExecutionError),
}
#[derive(Debug, thiserror::Error)]
pub enum InsertBlockValidationError {
#[error("failed to recover senders for block")]
SenderRecovery,
#[error(transparent)]
Consensus(#[from] ConsensusError),
#[error(transparent)]
Validation(#[from] BlockValidationError),
}
impl InsertBlockValidationError {
pub const fn is_block_pre_merge(&self) -> bool {
matches!(self, Self::Validation(BlockValidationError::BlockPreMerge { .. }))
}
}
#[derive(Debug, thiserror::Error)]
pub enum InsertBlockErrorKind {
#[error("failed to recover senders for block")]
SenderRecovery,
#[error(transparent)]
Consensus(#[from] ConsensusError),
#[error(transparent)]
Execution(#[from] BlockExecutionError),
#[error(transparent)]
Tree(#[from] BlockchainTreeError),
#[error(transparent)]
Provider(#[from] ProviderError),
#[error(transparent)]
Internal(#[from] Box<dyn core::error::Error + Send + Sync>),
#[error(transparent)]
Canonical(#[from] CanonicalError),
}
impl InsertBlockErrorKind {
pub const fn is_tree_error(&self) -> bool {
matches!(self, Self::Tree(_))
}
pub const fn is_consensus_error(&self) -> bool {
matches!(self, Self::Consensus(_))
}
pub const fn is_state_root_error(&self) -> bool {
match self {
Self::Execution(err) => {
matches!(
err,
BlockExecutionError::Validation(BlockValidationError::StateRoot { .. })
)
}
Self::Canonical(err) => {
matches!(
err,
CanonicalError::Validation(BlockValidationError::StateRoot { .. }) |
CanonicalError::Provider(
ProviderError::StateRootMismatch(_) |
ProviderError::UnwindStateRootMismatch(_)
)
)
}
Self::Provider(err) => {
matches!(
err,
ProviderError::StateRootMismatch(_) | ProviderError::UnwindStateRootMismatch(_)
)
}
_ => false,
}
}
#[allow(clippy::match_same_arms)]
pub const fn is_invalid_block(&self) -> bool {
match self {
Self::SenderRecovery | Self::Consensus(_) => true,
Self::Execution(err) => {
match err {
BlockExecutionError::Validation(_) | BlockExecutionError::Consensus(_) => {
true
}
BlockExecutionError::Internal(_) => false,
}
}
Self::Tree(err) => {
match err {
BlockchainTreeError::PendingBlockIsFinalized { .. } => {
true
}
BlockchainTreeError::BlockSideChainIdConsistency { .. } |
BlockchainTreeError::CanonicalChain { .. } |
BlockchainTreeError::BlockNumberNotFoundInChain { .. } |
BlockchainTreeError::BlockHashNotFoundInChain { .. } |
BlockchainTreeError::BlockBufferingFailed { .. } |
BlockchainTreeError::GenesisBlockHasNoParent => false,
}
}
Self::Provider(_) | Self::Internal(_) => {
false
}
Self::Canonical(err) => match err {
CanonicalError::BlockchainTree(_) |
CanonicalError::CanonicalCommit(_) |
CanonicalError::CanonicalRevert(_) |
CanonicalError::OptimisticTargetRevert(_) |
CanonicalError::Provider(_) => false,
CanonicalError::Validation(_) => true,
},
}
}
pub const fn is_block_pre_merge(&self) -> bool {
matches!(
self,
Self::Execution(BlockExecutionError::Validation(
BlockValidationError::BlockPreMerge { .. }
))
)
}
pub const fn is_execution_error(&self) -> bool {
matches!(self, Self::Execution(_))
}
pub const fn is_internal(&self) -> bool {
matches!(self, Self::Internal(_))
}
pub const fn as_tree_error(&self) -> Option<BlockchainTreeError> {
match self {
Self::Tree(err) => Some(*err),
_ => None,
}
}
pub const fn as_consensus_error(&self) -> Option<&ConsensusError> {
match self {
Self::Consensus(err) => Some(err),
_ => None,
}
}
pub const fn as_execution_error(&self) -> Option<&BlockExecutionError> {
match self {
Self::Execution(err) => Some(err),
_ => None,
}
}
}