chore: extract blockchaintree types to blockchain-tree-api crate (#8393)

This commit is contained in:
Matthias Seitz
2024-05-27 12:22:13 +02:00
committed by GitHub
parent 2d33c17bc0
commit 4dd2ad99f6
16 changed files with 98 additions and 78 deletions

15
Cargo.lock generated
View File

@ -6525,6 +6525,7 @@ dependencies = [
"linked_hash_set", "linked_hash_set",
"metrics", "metrics",
"parking_lot 0.12.3", "parking_lot 0.12.3",
"reth-blockchain-tree-api",
"reth-consensus", "reth-consensus",
"reth-db", "reth-db",
"reth-evm", "reth-evm",
@ -6544,6 +6545,17 @@ dependencies = [
"tracing", "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]] [[package]]
name = "reth-cli-runner" name = "reth-cli-runner"
version = "0.2.0-beta.7" version = "0.2.0-beta.7"
@ -7056,12 +7068,12 @@ dependencies = [
name = "reth-interfaces" name = "reth-interfaces"
version = "0.2.0-beta.7" version = "0.2.0-beta.7"
dependencies = [ dependencies = [
"reth-blockchain-tree-api",
"reth-consensus", "reth-consensus",
"reth-execution-errors", "reth-execution-errors",
"reth-fs-util", "reth-fs-util",
"reth-network-api", "reth-network-api",
"reth-network-p2p", "reth-network-p2p",
"reth-primitives",
"reth-storage-errors", "reth-storage-errors",
"thiserror", "thiserror",
] ]
@ -7627,6 +7639,7 @@ dependencies = [
"pin-project", "pin-project",
"rand 0.8.5", "rand 0.8.5",
"rayon", "rayon",
"reth-blockchain-tree-api",
"reth-codecs", "reth-codecs",
"reth-db", "reth-db",
"reth-evm", "reth-evm",

View File

@ -2,6 +2,7 @@
members = [ members = [
"bin/reth/", "bin/reth/",
"crates/blockchain-tree/", "crates/blockchain-tree/",
"crates/blockchain-tree-api/",
"crates/cli/runner/", "crates/cli/runner/",
"crates/config/", "crates/config/",
"crates/consensus/auto-seal/", "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-basic-payload-builder = { path = "crates/payload/basic" }
reth-beacon-consensus = { path = "crates/consensus/beacon" } reth-beacon-consensus = { path = "crates/consensus/beacon" }
reth-blockchain-tree = { path = "crates/blockchain-tree" } reth-blockchain-tree = { path = "crates/blockchain-tree" }
reth-blockchain-tree-api = { path = "crates/blockchain-tree-api" }
reth-cli-runner = { path = "crates/cli/runner" } reth-cli-runner = { path = "crates/cli/runner" }
reth-codecs = { path = "crates/storage/codecs" } reth-codecs = { path = "crates/storage/codecs" }
reth-codecs-derive = { path = "crates/storage/codecs/derive" } reth-codecs-derive = { path = "crates/storage/codecs/derive" }

View File

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

View File

@ -1,10 +1,9 @@
//! Error handling for the blockchain tree //! Error handling for the blockchain tree
use crate::RethError;
use reth_consensus::ConsensusError; use reth_consensus::ConsensusError;
use reth_execution_errors::{BlockExecutionError, BlockValidationError}; use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::{BlockHash, BlockNumber, SealedBlock}; 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. /// Various error cases that can occur when a block violates tree assumptions.
#[derive(Debug, Clone, Copy, thiserror::Error, Eq, PartialEq)] #[derive(Debug, Clone, Copy, thiserror::Error, Eq, PartialEq)]
@ -133,11 +132,6 @@ impl InsertBlockError {
Self::new(block, InsertBlockErrorKind::Execution(error)) 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 /// Consumes the error and returns the block that resulted in the error
#[inline] #[inline]
pub fn into_block(self) -> SealedBlock { 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<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),
}
}
}

View File

@ -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::{ use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders, BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader, SealedHeader,
}; };
use reth_storage_errors::provider::ProviderError;
use std::collections::{BTreeMap, HashSet}; use std::collections::{BTreeMap, HashSet};
use self::error::CanonicalError;
pub mod error; pub mod error;
/// * [BlockchainTreeEngine::insert_block]: Connect block to chain, execute it and if valid insert /// * [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( fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self, &self,
last_finalized_block: BlockNumber, last_finalized_block: BlockNumber,
) -> RethResult<()>; ) -> Result<(), CanonicalError>;
/// Update all block hashes. iterate over present and new list of canonical hashes and compare /// 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 /// them. Remove all mismatches, disconnect them, removes all chains and clears all buffered
/// blocks before the tip. /// blocks before the tip.
fn update_block_hashes_and_clear_buffered( fn update_block_hashes_and_clear_buffered(
&self, &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 /// 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. /// 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 /// `N` is the maximum of `max_reorg_depth` and the number of block hashes needed to satisfy the
/// `BLOCKHASH` opcode in the EVM. /// `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 /// Make a block and its parent chain part of the canonical chain by committing it to the
/// database. /// database.

View File

@ -12,8 +12,8 @@ workspace = true
[dependencies] [dependencies]
# reth # reth
reth-blockchain-tree-api.workspace = true
reth-primitives.workspace = true reth-primitives.workspace = true
reth-interfaces.workspace = true
reth-storage-errors.workspace = true reth-storage-errors.workspace = true
reth-execution-errors.workspace = true reth-execution-errors.workspace = true
reth-db.workspace = true reth-db.workspace = true

View File

@ -5,14 +5,14 @@ use crate::{
state::{BlockchainId, TreeState}, state::{BlockchainId, TreeState},
AppendableChain, BlockIndices, BlockchainTreeConfig, BundleStateData, TreeExternals, 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_consensus::{Consensus, ConsensusError};
use reth_db::database::Database; use reth_db::database::Database;
use reth_evm::execute::BlockExecutorProvider; use reth_evm::execute::BlockExecutorProvider;
use reth_execution_errors::{BlockExecutionError, BlockValidationError}; use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_interfaces::blockchain_tree::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError, InsertBlockErrorKind},
BlockAttachment, BlockStatus, BlockValidationKind, CanonicalOutcome, InsertPayloadOk,
};
use reth_primitives::{ use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, ForkBlock, GotExpected, Hardfork, PruneModes, Receipt, BlockHash, BlockNumHash, BlockNumber, ForkBlock, GotExpected, Hardfork, PruneModes, Receipt,
SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256, SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,

View File

@ -5,14 +5,14 @@
use super::externals::TreeExternals; use super::externals::TreeExternals;
use crate::BundleStateDataRef; use crate::BundleStateDataRef;
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, InsertBlockErrorKind},
BlockAttachment, BlockValidationKind,
};
use reth_consensus::{Consensus, ConsensusError}; use reth_consensus::{Consensus, ConsensusError};
use reth_db::database::Database; use reth_db::database::Database;
use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor}; use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor};
use reth_execution_errors::BlockExecutionError; use reth_execution_errors::BlockExecutionError;
use reth_interfaces::blockchain_tree::{
error::{BlockchainTreeError, InsertBlockErrorKind},
BlockAttachment, BlockValidationKind,
};
use reth_primitives::{ use reth_primitives::{
BlockHash, BlockNumber, ForkBlock, GotExpected, Receipts, SealedBlockWithSenders, SealedHeader, BlockHash, BlockNumber, ForkBlock, GotExpected, Receipts, SealedBlockWithSenders, SealedHeader,
U256, U256,

View File

@ -18,6 +18,9 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))] #![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 mod blockchain_tree;
pub use blockchain_tree::BlockchainTree; pub use blockchain_tree::BlockchainTree;

View File

@ -1,11 +1,8 @@
use reth_interfaces::{ use reth_blockchain_tree_api::{
blockchain_tree::{ self,
error::{BlockchainTreeError, CanonicalError, InsertBlockError}, error::{BlockchainTreeError, CanonicalError, InsertBlockError, ProviderError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome, BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk, InsertPayloadOk,
},
provider::ProviderError,
RethResult,
}; };
use reth_primitives::{ use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders, BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
@ -57,11 +54,11 @@ impl BlockchainTreeEngine for NoopBlockchainTree {
fn connect_buffered_blocks_to_canonical_hashes_and_finalize( fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self, &self,
_last_finalized_block: BlockNumber, _last_finalized_block: BlockNumber,
) -> RethResult<()> { ) -> Result<(), CanonicalError> {
Ok(()) Ok(())
} }
fn connect_buffered_blocks_to_canonical_hashes(&self) -> RethResult<()> { fn connect_buffered_blocks_to_canonical_hashes(&self) -> Result<(), CanonicalError> {
Ok(()) Ok(())
} }
@ -71,7 +68,7 @@ impl BlockchainTreeEngine for NoopBlockchainTree {
fn update_block_hashes_and_clear_buffered( fn update_block_hashes_and_clear_buffered(
&self, &self,
) -> RethResult<BTreeMap<BlockNumber, BlockHash>> { ) -> Result<BTreeMap<BlockNumber, BlockHash>, CanonicalError> {
Ok(BTreeMap::new()) Ok(BTreeMap::new())
} }
} }

View File

@ -2,16 +2,13 @@
use super::BlockchainTree; use super::BlockchainTree;
use parking_lot::RwLock; use parking_lot::RwLock;
use reth_blockchain_tree_api::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
};
use reth_db::database::Database; use reth_db::database::Database;
use reth_evm::execute::BlockExecutorProvider; use reth_evm::execute::BlockExecutorProvider;
use reth_interfaces::{
blockchain_tree::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
},
RethResult,
};
use reth_primitives::{ use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders, BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader, SealedHeader,
@ -74,7 +71,7 @@ where
fn connect_buffered_blocks_to_canonical_hashes_and_finalize( fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self, &self,
last_finalized_block: BlockNumber, last_finalized_block: BlockNumber,
) -> RethResult<()> { ) -> Result<(), CanonicalError> {
trace!(target: "blockchain_tree", last_finalized_block, "Connecting buffered blocks to canonical hashes and finalizing the tree"); trace!(target: "blockchain_tree", last_finalized_block, "Connecting buffered blocks to canonical hashes and finalizing the tree");
let mut tree = self.tree.write(); let mut tree = self.tree.write();
let res = let res =
@ -85,14 +82,14 @@ where
fn update_block_hashes_and_clear_buffered( fn update_block_hashes_and_clear_buffered(
&self, &self,
) -> RethResult<BTreeMap<BlockNumber, BlockHash>> { ) -> Result<BTreeMap<BlockNumber, BlockHash>, CanonicalError> {
let mut tree = self.tree.write(); let mut tree = self.tree.write();
let res = tree.update_block_hashes_and_clear_buffered(); let res = tree.update_block_hashes_and_clear_buffered();
tree.update_chains_metrics(); tree.update_chains_metrics();
Ok(res?) 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"); trace!(target: "blockchain_tree", "Connecting buffered blocks to canonical hashes");
let mut tree = self.tree.write(); let mut tree = self.tree.write();
let res = tree.connect_buffered_blocks_to_canonical_hashes(); let res = tree.connect_buffered_blocks_to_canonical_hashes();

View File

@ -1656,7 +1656,7 @@ where
self.blockchain.connect_buffered_blocks_to_canonical_hashes() self.blockchain.connect_buffered_blocks_to_canonical_hashes()
{ {
error!(target: "consensus::engine", %error, "Error connecting buffered blocks to canonical hashes on hook result"); 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())
} }
} }
} }

View File

@ -11,12 +11,12 @@ repository.workspace = true
workspace = true workspace = true
[dependencies] [dependencies]
reth-blockchain-tree-api.workspace = true
reth-consensus.workspace = true reth-consensus.workspace = true
reth-execution-errors.workspace = true reth-execution-errors.workspace = true
reth-fs-util.workspace = true reth-fs-util.workspace = true
reth-network-api.workspace = true reth-network-api.workspace = true
reth-network-p2p.workspace = true reth-network-p2p.workspace = true
reth-primitives.workspace = true
reth-storage-errors.workspace = true reth-storage-errors.workspace = true
# misc # misc

View File

@ -32,7 +32,7 @@ pub use reth_execution_errors::trie;
pub use reth_network_p2p::sync; pub use reth_network_p2p::sync;
/// BlockchainTree related traits. /// 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. /// Common test helpers for mocking out Consensus, Downloaders and Header Clients.
#[cfg(feature = "test-utils")] #[cfg(feature = "test-utils")]

View File

@ -13,6 +13,7 @@ workspace = true
[dependencies] [dependencies]
# reth # reth
reth-blockchain-tree-api.workspace = true
reth-execution-errors.workspace = true reth-execution-errors.workspace = true
reth-primitives.workspace = true reth-primitives.workspace = true
reth-fs-util.workspace = true reth-fs-util.workspace = true

View File

@ -7,20 +7,16 @@ use crate::{
StateProviderBox, StateProviderFactory, StaticFileProviderFactory, TransactionVariant, StateProviderBox, StateProviderFactory, StaticFileProviderFactory, TransactionVariant,
TransactionsProvider, TreeViewer, WithdrawalsProvider, TransactionsProvider, TreeViewer, WithdrawalsProvider,
}; };
use reth_blockchain_tree_api::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
};
use reth_db::{ use reth_db::{
database::Database, database::Database,
models::{AccountBeforeTx, StoredBlockBodyIndices}, models::{AccountBeforeTx, StoredBlockBodyIndices},
}; };
use reth_evm::ConfigureEvmEnv; use reth_evm::ConfigureEvmEnv;
use reth_interfaces::{
blockchain_tree::{
error::{CanonicalError, InsertBlockError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
},
provider::ProviderResult,
RethResult,
};
use reth_primitives::{ use reth_primitives::{
stage::{StageCheckpoint, StageId}, stage::{StageCheckpoint, StageId},
Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumber, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumber,
@ -29,6 +25,7 @@ use reth_primitives::{
TransactionSigned, TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256, TransactionSigned, TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256,
U256, U256,
}; };
use reth_storage_errors::provider::ProviderResult;
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg}; use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg};
use std::{ use std::{
collections::{BTreeMap, HashSet}, collections::{BTreeMap, HashSet},
@ -669,18 +666,20 @@ where
self.tree.finalize_block(finalized_block) self.tree.finalize_block(finalized_block)
} }
fn update_block_hashes_and_clear_buffered(&self) -> RethResult<BTreeMap<BlockNumber, B256>> {
self.tree.update_block_hashes_and_clear_buffered()
}
fn connect_buffered_blocks_to_canonical_hashes_and_finalize( fn connect_buffered_blocks_to_canonical_hashes_and_finalize(
&self, &self,
last_finalized_block: BlockNumber, last_finalized_block: BlockNumber,
) -> RethResult<()> { ) -> Result<(), CanonicalError> {
self.tree.connect_buffered_blocks_to_canonical_hashes_and_finalize(last_finalized_block) 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<BTreeMap<BlockNumber, B256>, 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() self.tree.connect_buffered_blocks_to_canonical_hashes()
} }