Skip to content

Commit

Permalink
chore: extract blockchaintree types to blockchain-tree-api crate (#8393)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored May 27, 2024
1 parent 2d33c17 commit 4dd2ad9
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 78 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"bin/reth/",
"crates/blockchain-tree/",
"crates/blockchain-tree-api/",
"crates/cli/runner/",
"crates/config/",
"crates/consensus/auto-seal/",
Expand Down Expand Up @@ -216,6 +217,7 @@ reth-auto-seal-consensus = { path = "crates/consensus/auto-seal" }
reth-basic-payload-builder = { path = "crates/payload/basic" }
reth-beacon-consensus = { path = "crates/consensus/beacon" }
reth-blockchain-tree = { path = "crates/blockchain-tree" }
reth-blockchain-tree-api = { path = "crates/blockchain-tree-api" }
reth-cli-runner = { path = "crates/cli/runner" }
reth-codecs = { path = "crates/storage/codecs" }
reth-codecs-derive = { path = "crates/storage/codecs/derive" }
Expand Down
20 changes: 20 additions & 0 deletions crates/blockchain-tree-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "reth-blockchain-tree-api"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[lints]
workspace = true

[dependencies]
reth-consensus.workspace = true
reth-execution-errors.workspace = true
reth-primitives.workspace = true
reth-storage-errors.workspace = true

# misc
thiserror.workspace = true
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Error handling for the blockchain tree
use crate::RethError;
use reth_consensus::ConsensusError;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::{BlockHash, BlockNumber, SealedBlock};
use reth_storage_errors::provider::ProviderError;
pub use reth_storage_errors::provider::ProviderError;

/// Various error cases that can occur when a block violates tree assumptions.
#[derive(Debug, Clone, Copy, thiserror::Error, Eq, PartialEq)]
Expand Down Expand Up @@ -133,11 +132,6 @@ impl InsertBlockError {
Self::new(block, InsertBlockErrorKind::Execution(error))
}

/// Create a new InsertBlockError from a RethError and block.
pub fn from_reth_error(error: RethError, block: SealedBlock) -> Self {
Self::new(block, error.into())
}

/// Consumes the error and returns the block that resulted in the error
#[inline]
pub fn into_block(self) -> SealedBlock {
Expand Down Expand Up @@ -383,18 +377,3 @@ impl InsertBlockErrorKind {
}
}
}

// This is a convenience impl to convert from crate::Error to InsertBlockErrorKind
impl From<RethError> for InsertBlockErrorKind {
fn from(err: RethError) -> Self {
match err {
RethError::Execution(err) => InsertBlockErrorKind::Execution(err),
RethError::Consensus(err) => InsertBlockErrorKind::Consensus(err),
RethError::Database(err) => InsertBlockErrorKind::Internal(Box::new(err)),
RethError::Provider(err) => InsertBlockErrorKind::Internal(Box::new(err)),
RethError::Network(err) => InsertBlockErrorKind::Internal(Box::new(err)),
RethError::Custom(err) => InsertBlockErrorKind::Internal(err.into()),
RethError::Canonical(err) => InsertBlockErrorKind::Canonical(err),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use crate::{blockchain_tree::error::InsertBlockError, provider::ProviderError, RethResult};
//! Interfaces and types for interacting with the blockchain tree.
#![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 self::error::CanonicalError;
use crate::error::InsertBlockError;
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader,
};
use reth_storage_errors::provider::ProviderError;
use std::collections::{BTreeMap, HashSet};

use self::error::CanonicalError;

pub mod error;

/// * [BlockchainTreeEngine::insert_block]: Connect block to chain, execute it and if valid insert
Expand Down Expand Up @@ -76,21 +85,21 @@ pub trait BlockchainTreeEngine: BlockchainTreeViewer + Send + Sync {
fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self,
last_finalized_block: BlockNumber,
) -> RethResult<()>;
) -> Result<(), CanonicalError>;

/// Update all block hashes. iterate over present and new list of canonical hashes and compare
/// them. Remove all mismatches, disconnect them, removes all chains and clears all buffered
/// blocks before the tip.
fn update_block_hashes_and_clear_buffered(
&self,
) -> RethResult<BTreeMap<BlockNumber, BlockHash>>;
) -> Result<BTreeMap<BlockNumber, BlockHash>, CanonicalError>;

/// Reads the last `N` canonical hashes from the database and updates the block indices of the
/// tree by attempting to connect the buffered blocks to canonical hashes.
///
/// `N` is the maximum of `max_reorg_depth` and the number of block hashes needed to satisfy the
/// `BLOCKHASH` opcode in the EVM.
fn connect_buffered_blocks_to_canonical_hashes(&self) -> RethResult<()>;
fn connect_buffered_blocks_to_canonical_hashes(&self) -> Result<(), CanonicalError>;

/// Make a block and its parent chain part of the canonical chain by committing it to the
/// database.
Expand Down
2 changes: 1 addition & 1 deletion crates/blockchain-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ workspace = true

[dependencies]
# reth
reth-blockchain-tree-api.workspace = true
reth-primitives.workspace = true
reth-interfaces.workspace = true
reth-storage-errors.workspace = true
reth-execution-errors.workspace = true
reth-db.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::{
state::{BlockchainId, TreeState},
AppendableChain, BlockIndices, BlockchainTreeConfig, BundleStateData, TreeExternals,
};
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError, InsertBlockErrorKind},
BlockAttachment, BlockStatus, BlockValidationKind, CanonicalOutcome, InsertPayloadOk,
};
use reth_consensus::{Consensus, ConsensusError};
use reth_db::database::Database;
use reth_evm::execute::BlockExecutorProvider;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_interfaces::blockchain_tree::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError, InsertBlockErrorKind},
BlockAttachment, BlockStatus, BlockValidationKind, CanonicalOutcome, InsertPayloadOk,
};
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, ForkBlock, GotExpected, Hardfork, PruneModes, Receipt,
SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
Expand Down
8 changes: 4 additions & 4 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use super::externals::TreeExternals;
use crate::BundleStateDataRef;
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, InsertBlockErrorKind},
BlockAttachment, BlockValidationKind,
};
use reth_consensus::{Consensus, ConsensusError};
use reth_db::database::Database;
use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor};
use reth_execution_errors::BlockExecutionError;
use reth_interfaces::blockchain_tree::{
error::{BlockchainTreeError, InsertBlockErrorKind},
BlockAttachment, BlockValidationKind,
};
use reth_primitives::{
BlockHash, BlockNumber, ForkBlock, GotExpected, Receipts, SealedBlockWithSenders, SealedHeader,
U256,
Expand Down
3 changes: 3 additions & 0 deletions crates/blockchain-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

/// Re-export of the blockchain tree API.
pub use reth_blockchain_tree_api::*;

pub mod blockchain_tree;
pub use blockchain_tree::BlockchainTree;

Expand Down
19 changes: 8 additions & 11 deletions crates/blockchain-tree/src/noop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use reth_interfaces::{
blockchain_tree::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
},
provider::ProviderError,
RethResult,
use reth_blockchain_tree_api::{
self,
error::{BlockchainTreeError, CanonicalError, InsertBlockError, ProviderError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
};
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
Expand Down Expand Up @@ -57,11 +54,11 @@ impl BlockchainTreeEngine for NoopBlockchainTree {
fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self,
_last_finalized_block: BlockNumber,
) -> RethResult<()> {
) -> Result<(), CanonicalError> {
Ok(())
}

fn connect_buffered_blocks_to_canonical_hashes(&self) -> RethResult<()> {
fn connect_buffered_blocks_to_canonical_hashes(&self) -> Result<(), CanonicalError> {
Ok(())
}

Expand All @@ -71,7 +68,7 @@ impl BlockchainTreeEngine for NoopBlockchainTree {

fn update_block_hashes_and_clear_buffered(
&self,
) -> RethResult<BTreeMap<BlockNumber, BlockHash>> {
) -> Result<BTreeMap<BlockNumber, BlockHash>, CanonicalError> {
Ok(BTreeMap::new())
}
}
Expand Down
19 changes: 8 additions & 11 deletions crates/blockchain-tree/src/shareable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
use super::BlockchainTree;
use parking_lot::RwLock;
use reth_blockchain_tree_api::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
};
use reth_db::database::Database;
use reth_evm::execute::BlockExecutorProvider;
use reth_interfaces::{
blockchain_tree::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
},
RethResult,
};
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader,
Expand Down Expand Up @@ -74,7 +71,7 @@ where
fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self,
last_finalized_block: BlockNumber,
) -> RethResult<()> {
) -> Result<(), CanonicalError> {
trace!(target: "blockchain_tree", last_finalized_block, "Connecting buffered blocks to canonical hashes and finalizing the tree");
let mut tree = self.tree.write();
let res =
Expand All @@ -85,14 +82,14 @@ where

fn update_block_hashes_and_clear_buffered(
&self,
) -> RethResult<BTreeMap<BlockNumber, BlockHash>> {
) -> Result<BTreeMap<BlockNumber, BlockHash>, CanonicalError> {
let mut tree = self.tree.write();
let res = tree.update_block_hashes_and_clear_buffered();
tree.update_chains_metrics();
Ok(res?)
}

fn connect_buffered_blocks_to_canonical_hashes(&self) -> RethResult<()> {
fn connect_buffered_blocks_to_canonical_hashes(&self) -> Result<(), CanonicalError> {
trace!(target: "blockchain_tree", "Connecting buffered blocks to canonical hashes");
let mut tree = self.tree.write();
let res = tree.connect_buffered_blocks_to_canonical_hashes();
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ where
self.blockchain.connect_buffered_blocks_to_canonical_hashes()
{
error!(target: "consensus::engine", %error, "Error connecting buffered blocks to canonical hashes on hook result");
return Err(error.into())
return Err(RethError::Canonical(error).into())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/interfaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ repository.workspace = true
workspace = true

[dependencies]
reth-blockchain-tree-api.workspace = true
reth-consensus.workspace = true
reth-execution-errors.workspace = true
reth-fs-util.workspace = true
reth-network-api.workspace = true
reth-network-p2p.workspace = true
reth-primitives.workspace = true
reth-storage-errors.workspace = true

# misc
Expand Down
2 changes: 1 addition & 1 deletion crates/interfaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use reth_execution_errors::trie;
pub use reth_network_p2p::sync;

/// BlockchainTree related traits.
pub mod blockchain_tree;
pub use reth_blockchain_tree_api as blockchain_tree;

/// Common test helpers for mocking out Consensus, Downloaders and Header Clients.
#[cfg(feature = "test-utils")]
Expand Down
1 change: 1 addition & 0 deletions crates/storage/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true

[dependencies]
# reth
reth-blockchain-tree-api.workspace = true
reth-execution-errors.workspace = true
reth-primitives.workspace = true
reth-fs-util.workspace = true
Expand Down
Loading

0 comments on commit 4dd2ad9

Please sign in to comment.