diff --git a/Cargo.lock b/Cargo.lock index 099fe08d5..9aaedd530 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6525,6 +6525,7 @@ dependencies = [ "linked_hash_set", "metrics", "parking_lot 0.12.3", + "reth-blockchain-tree-api", "reth-consensus", "reth-db", "reth-evm", @@ -6544,6 +6545,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "reth-blockchain-tree-api" +version = "0.2.0-beta.7" +dependencies = [ + "reth-consensus", + "reth-execution-errors", + "reth-primitives", + "reth-storage-errors", + "thiserror", +] + [[package]] name = "reth-cli-runner" version = "0.2.0-beta.7" @@ -7056,12 +7068,12 @@ dependencies = [ name = "reth-interfaces" version = "0.2.0-beta.7" dependencies = [ + "reth-blockchain-tree-api", "reth-consensus", "reth-execution-errors", "reth-fs-util", "reth-network-api", "reth-network-p2p", - "reth-primitives", "reth-storage-errors", "thiserror", ] @@ -7627,6 +7639,7 @@ dependencies = [ "pin-project", "rand 0.8.5", "rayon", + "reth-blockchain-tree-api", "reth-codecs", "reth-db", "reth-evm", diff --git a/Cargo.toml b/Cargo.toml index 6f812253e..a61d1ca4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "bin/reth/", "crates/blockchain-tree/", + "crates/blockchain-tree-api/", "crates/cli/runner/", "crates/config/", "crates/consensus/auto-seal/", @@ -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" } diff --git a/crates/blockchain-tree-api/Cargo.toml b/crates/blockchain-tree-api/Cargo.toml new file mode 100644 index 000000000..69616209d --- /dev/null +++ b/crates/blockchain-tree-api/Cargo.toml @@ -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 diff --git a/crates/interfaces/src/blockchain_tree/error.rs b/crates/blockchain-tree-api/src/error.rs similarity index 93% rename from crates/interfaces/src/blockchain_tree/error.rs rename to crates/blockchain-tree-api/src/error.rs index 122b85743..c48a97676 100644 --- a/crates/interfaces/src/blockchain_tree/error.rs +++ b/crates/blockchain-tree-api/src/error.rs @@ -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)] @@ -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 { @@ -383,18 +377,3 @@ impl InsertBlockErrorKind { } } } - -// This is a convenience impl to convert from crate::Error to InsertBlockErrorKind -impl From 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), - } - } -} diff --git a/crates/interfaces/src/blockchain_tree/mod.rs b/crates/blockchain-tree-api/src/lib.rs similarity index 95% rename from crates/interfaces/src/blockchain_tree/mod.rs rename to crates/blockchain-tree-api/src/lib.rs index 0c1a9553d..113e951e6 100644 --- a/crates/interfaces/src/blockchain_tree/mod.rs +++ b/crates/blockchain-tree-api/src/lib.rs @@ -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 @@ -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>; + ) -> Result, 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. diff --git a/crates/blockchain-tree/Cargo.toml b/crates/blockchain-tree/Cargo.toml index 1b8a53394..58ee1cda5 100644 --- a/crates/blockchain-tree/Cargo.toml +++ b/crates/blockchain-tree/Cargo.toml @@ -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 diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index c031a5749..dc256eedf 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -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, diff --git a/crates/blockchain-tree/src/chain.rs b/crates/blockchain-tree/src/chain.rs index e73b17576..e8e40cb41 100644 --- a/crates/blockchain-tree/src/chain.rs +++ b/crates/blockchain-tree/src/chain.rs @@ -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, diff --git a/crates/blockchain-tree/src/lib.rs b/crates/blockchain-tree/src/lib.rs index 1ae44b85a..6f5717abd 100644 --- a/crates/blockchain-tree/src/lib.rs +++ b/crates/blockchain-tree/src/lib.rs @@ -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; diff --git a/crates/blockchain-tree/src/noop.rs b/crates/blockchain-tree/src/noop.rs index 18423d3bb..f4d27272f 100644 --- a/crates/blockchain-tree/src/noop.rs +++ b/crates/blockchain-tree/src/noop.rs @@ -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, @@ -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(()) } @@ -71,7 +68,7 @@ impl BlockchainTreeEngine for NoopBlockchainTree { fn update_block_hashes_and_clear_buffered( &self, - ) -> RethResult> { + ) -> Result, CanonicalError> { Ok(BTreeMap::new()) } } diff --git a/crates/blockchain-tree/src/shareable.rs b/crates/blockchain-tree/src/shareable.rs index 624dfd0e3..52a98e84d 100644 --- a/crates/blockchain-tree/src/shareable.rs +++ b/crates/blockchain-tree/src/shareable.rs @@ -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, @@ -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 = @@ -85,14 +82,14 @@ where fn update_block_hashes_and_clear_buffered( &self, - ) -> RethResult> { + ) -> Result, 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(); diff --git a/crates/consensus/beacon/src/engine/mod.rs b/crates/consensus/beacon/src/engine/mod.rs index 5f7f58390..c1ef62287 100644 --- a/crates/consensus/beacon/src/engine/mod.rs +++ b/crates/consensus/beacon/src/engine/mod.rs @@ -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()) } } } diff --git a/crates/interfaces/Cargo.toml b/crates/interfaces/Cargo.toml index 1d7483691..a5c01ecb9 100644 --- a/crates/interfaces/Cargo.toml +++ b/crates/interfaces/Cargo.toml @@ -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 diff --git a/crates/interfaces/src/lib.rs b/crates/interfaces/src/lib.rs index 461413a1e..651283bb8 100644 --- a/crates/interfaces/src/lib.rs +++ b/crates/interfaces/src/lib.rs @@ -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")] diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml index d9a555161..c9eb5f7e3 100644 --- a/crates/storage/provider/Cargo.toml +++ b/crates/storage/provider/Cargo.toml @@ -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 diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index f9969a950..d6a7d34c8 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -7,20 +7,16 @@ use crate::{ StateProviderBox, StateProviderFactory, StaticFileProviderFactory, TransactionVariant, TransactionsProvider, TreeViewer, WithdrawalsProvider, }; +use reth_blockchain_tree_api::{ + error::{CanonicalError, InsertBlockError}, + BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome, + InsertPayloadOk, +}; use reth_db::{ database::Database, models::{AccountBeforeTx, StoredBlockBodyIndices}, }; use reth_evm::ConfigureEvmEnv; -use reth_interfaces::{ - blockchain_tree::{ - error::{CanonicalError, InsertBlockError}, - BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome, - InsertPayloadOk, - }, - provider::ProviderResult, - RethResult, -}; use reth_primitives::{ stage::{StageCheckpoint, StageId}, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumber, @@ -29,6 +25,7 @@ use reth_primitives::{ TransactionSigned, TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256, U256, }; +use reth_storage_errors::provider::ProviderResult; use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg}; use std::{ collections::{BTreeMap, HashSet}, @@ -669,18 +666,20 @@ where self.tree.finalize_block(finalized_block) } - fn update_block_hashes_and_clear_buffered(&self) -> RethResult> { - self.tree.update_block_hashes_and_clear_buffered() - } - fn connect_buffered_blocks_to_canonical_hashes_and_finalize( &self, last_finalized_block: BlockNumber, - ) -> RethResult<()> { + ) -> Result<(), CanonicalError> { self.tree.connect_buffered_blocks_to_canonical_hashes_and_finalize(last_finalized_block) } - fn connect_buffered_blocks_to_canonical_hashes(&self) -> RethResult<()> { + fn update_block_hashes_and_clear_buffered( + &self, + ) -> Result, CanonicalError> { + self.tree.update_block_hashes_and_clear_buffered() + } + + fn connect_buffered_blocks_to_canonical_hashes(&self) -> Result<(), CanonicalError> { self.tree.connect_buffered_blocks_to_canonical_hashes() }