From 00a92f526aa6bb7695c249fc634dabf9f3d4928e Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 11 Oct 2023 14:35:42 +0200 Subject: [PATCH] chore: remove consensus generic (#4981) --- crates/blockchain-tree/src/blockchain_tree.rs | 15 ++++---- crates/blockchain-tree/src/chain.rs | 35 ++++++++----------- crates/blockchain-tree/src/externals.rs | 16 ++++++--- crates/blockchain-tree/src/shareable.rs | 25 ++++++------- .../consensus/beacon/src/engine/test_utils.rs | 3 +- 5 files changed, 42 insertions(+), 52 deletions(-) diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 401d9c7bab1c..08e16f9a54fc 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -74,7 +74,7 @@ use tracing::{debug, error, info, instrument, trace, warn}; /// and commit it to db. If we don't have the block, pipeline syncing should start to fetch the /// blocks from p2p. Do reorg in tables if canonical chain if needed. #[derive(Debug)] -pub struct BlockchainTree { +pub struct BlockchainTree { /// The tracked chains and their current data. chains: HashMap, /// Unconnected block buffer. @@ -84,7 +84,7 @@ pub struct BlockchainTree { /// Indices to block and their connection to the canonical chain. block_indices: BlockIndices, /// External components (the database, consensus engine etc.) - externals: TreeExternals, + externals: TreeExternals, /// Tree configuration config: BlockchainTreeConfig, /// Broadcast channel for canon state changes notifications. @@ -96,10 +96,10 @@ pub struct BlockchainTree { prune_modes: Option, } -impl BlockchainTree { +impl BlockchainTree { /// Create a new blockchain tree. pub fn new( - externals: TreeExternals, + externals: TreeExternals, config: BlockchainTreeConfig, prune_modes: Option, ) -> RethResult { @@ -1196,7 +1196,7 @@ mod tests { fn setup_externals( exec_res: Vec, - ) -> TreeExternals, Arc, TestExecutorFactory> { + ) -> TreeExternals, TestExecutorFactory> { let db = create_test_rw_db(); let consensus = Arc::new(TestConsensus::default()); let chain_spec = Arc::new( @@ -1281,10 +1281,7 @@ mod tests { self } - fn assert( - self, - tree: &BlockchainTree, - ) { + fn assert(self, tree: &BlockchainTree) { if let Some(chain_num) = self.chain_num { assert_eq!(tree.chains.len(), chain_num); } diff --git a/crates/blockchain-tree/src/chain.rs b/crates/blockchain-tree/src/chain.rs index 4e0d95d2081e..fc50847aea0f 100644 --- a/crates/blockchain-tree/src/chain.rs +++ b/crates/blockchain-tree/src/chain.rs @@ -60,16 +60,15 @@ impl AppendableChain { /// Create a new chain that forks off the canonical. /// /// This will also verify the state root of the block extending the canonical chain. - pub fn new_canonical_head_fork( + pub fn new_canonical_head_fork( block: SealedBlockWithSenders, parent_header: &SealedHeader, canonical_block_hashes: &BTreeMap, canonical_fork: ForkBlock, - externals: &TreeExternals, + externals: &TreeExternals, ) -> Result where DB: Database, - C: Consensus, EF: ExecutorFactory, { let state = BundleStateWithReceipts::default(); @@ -94,16 +93,15 @@ impl AppendableChain { } /// Create a new chain that forks off of the canonical chain. - pub fn new_canonical_fork( + pub fn new_canonical_fork( block: SealedBlockWithSenders, parent_header: &SealedHeader, canonical_block_hashes: &BTreeMap, canonical_fork: ForkBlock, - externals: &TreeExternals, + externals: &TreeExternals, ) -> Result where DB: Database, - C: Consensus, EF: ExecutorFactory, { let state = BundleStateWithReceipts::default(); @@ -130,17 +128,16 @@ impl AppendableChain { /// Create a new chain that forks off of an existing sidechain. /// /// This differs from [AppendableChain::new_canonical_fork] in that this starts a new fork. - pub(crate) fn new_chain_fork( + pub(crate) fn new_chain_fork( &self, block: SealedBlockWithSenders, side_chain_block_hashes: BTreeMap, canonical_block_hashes: &BTreeMap, canonical_fork: ForkBlock, - externals: &TreeExternals, + externals: &TreeExternals, ) -> Result where DB: Database, - C: Consensus, EF: ExecutorFactory, { let parent_number = block.number - 1; @@ -177,17 +174,16 @@ impl AppendableChain { /// Validate and execute the given block that _extends the canonical chain_, validating its /// state root after execution. - fn validate_and_execute( + fn validate_and_execute( block: SealedBlockWithSenders, parent_block: &SealedHeader, post_state_data_provider: BSDP, - externals: &TreeExternals, + externals: &TreeExternals, block_kind: BlockKind, ) -> RethResult where BSDP: BundleStateDataProvider, DB: Database, - C: Consensus, EF: ExecutorFactory, { // some checks are done before blocks comes here. @@ -225,16 +221,15 @@ impl AppendableChain { /// Validate and execute the given block that _extends the canonical chain_, validating its /// state root after execution. - fn validate_and_execute_canonical_head_descendant( + fn validate_and_execute_canonical_head_descendant( block: SealedBlockWithSenders, parent_block: &SealedHeader, post_state_data_provider: BSDP, - externals: &TreeExternals, + externals: &TreeExternals, ) -> RethResult where BSDP: BundleStateDataProvider, DB: Database, - C: Consensus, EF: ExecutorFactory, { Self::validate_and_execute( @@ -247,16 +242,15 @@ impl AppendableChain { } /// Validate and execute the given sidechain block, skipping state root validation. - fn validate_and_execute_sidechain( + fn validate_and_execute_sidechain( block: SealedBlockWithSenders, parent_block: &SealedHeader, post_state_data_provider: BSDP, - externals: &TreeExternals, + externals: &TreeExternals, ) -> RethResult where BSDP: BundleStateDataProvider, DB: Database, - C: Consensus, EF: ExecutorFactory, { Self::validate_and_execute( @@ -280,18 +274,17 @@ impl AppendableChain { /// is the canonical head, or: state root check can't be performed if the given canonical is /// __not__ the canonical head. #[track_caller] - pub(crate) fn append_block( + pub(crate) fn append_block( &mut self, block: SealedBlockWithSenders, side_chain_block_hashes: BTreeMap, canonical_block_hashes: &BTreeMap, - externals: &TreeExternals, + externals: &TreeExternals, canonical_fork: ForkBlock, block_kind: BlockKind, ) -> Result<(), InsertBlockError> where DB: Database, - C: Consensus, EF: ExecutorFactory, { let (_, parent_block) = self.blocks.last_key_value().expect("Chain has at least one block"); diff --git a/crates/blockchain-tree/src/externals.rs b/crates/blockchain-tree/src/externals.rs index b73f0258ebc8..9fd0163aab0e 100644 --- a/crates/blockchain-tree/src/externals.rs +++ b/crates/blockchain-tree/src/externals.rs @@ -1,6 +1,7 @@ //! Blockchain tree externals. use reth_db::database::Database; +use reth_interfaces::consensus::Consensus; use reth_primitives::ChainSpec; use reth_provider::ProviderFactory; use std::sync::Arc; @@ -15,25 +16,30 @@ use std::sync::Arc; /// - The executor factory to execute blocks with /// - The chain spec #[derive(Debug)] -pub struct TreeExternals { +pub struct TreeExternals { /// The database, used to commit the canonical chain, or unwind it. pub(crate) db: DB, /// The consensus engine. - pub(crate) consensus: C, + pub(crate) consensus: Arc, /// The executor factory to execute blocks with. pub(crate) executor_factory: EF, /// The chain spec. pub(crate) chain_spec: Arc, } -impl TreeExternals { +impl TreeExternals { /// Create new tree externals. - pub fn new(db: DB, consensus: C, executor_factory: EF, chain_spec: Arc) -> Self { + pub fn new( + db: DB, + consensus: Arc, + executor_factory: EF, + chain_spec: Arc, + ) -> Self { Self { db, consensus, executor_factory, chain_spec } } } -impl TreeExternals { +impl TreeExternals { /// Return shareable database helper structure. pub fn database(&self) -> ProviderFactory<&DB> { ProviderFactory::new(&self.db, self.chain_spec.clone()) diff --git a/crates/blockchain-tree/src/shareable.rs b/crates/blockchain-tree/src/shareable.rs index d1b777464cf9..e8b7759b14b5 100644 --- a/crates/blockchain-tree/src/shareable.rs +++ b/crates/blockchain-tree/src/shareable.rs @@ -7,7 +7,6 @@ use reth_interfaces::{ error::InsertBlockError, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome, InsertPayloadOk, }, - consensus::Consensus, RethResult, }; use reth_primitives::{ @@ -26,21 +25,19 @@ use tracing::trace; /// Shareable blockchain tree that is behind tokio::RwLock #[derive(Clone, Debug)] -pub struct ShareableBlockchainTree { +pub struct ShareableBlockchainTree { /// BlockchainTree - pub tree: Arc>>, + pub tree: Arc>>, } -impl ShareableBlockchainTree { +impl ShareableBlockchainTree { /// Create a new shareable database. - pub fn new(tree: BlockchainTree) -> Self { + pub fn new(tree: BlockchainTree) -> Self { Self { tree: Arc::new(RwLock::new(tree)) } } } -impl BlockchainTreeEngine - for ShareableBlockchainTree -{ +impl BlockchainTreeEngine for ShareableBlockchainTree { fn buffer_block(&self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> { let mut tree = self.tree.write(); // Blockchain tree metrics shouldn't be updated here, see @@ -103,9 +100,7 @@ impl BlockchainTreeEngine } } -impl BlockchainTreeViewer - for ShareableBlockchainTree -{ +impl BlockchainTreeViewer for ShareableBlockchainTree { fn blocks(&self) -> BTreeMap> { trace!(target: "blockchain_tree", "Returning all blocks in blockchain tree"); self.tree.read().block_indices().block_number_to_block_hashes().clone() @@ -193,8 +188,8 @@ impl BlockchainTreeViewer } } -impl BlockchainTreePendingStateProvider - for ShareableBlockchainTree +impl BlockchainTreePendingStateProvider + for ShareableBlockchainTree { fn find_pending_state_provider( &self, @@ -206,8 +201,8 @@ impl BlockchainTreePendingState } } -impl CanonStateSubscriptions - for ShareableBlockchainTree +impl CanonStateSubscriptions + for ShareableBlockchainTree { fn subscribe_to_canonical_state(&self) -> reth_provider::CanonStateNotifications { trace!(target: "blockchain_tree", "Registered subscriber for canonical state"); diff --git a/crates/consensus/beacon/src/engine/test_utils.rs b/crates/consensus/beacon/src/engine/test_utils.rs index 14e2a3bd2542..2879d86767ab 100644 --- a/crates/consensus/beacon/src/engine/test_utils.rs +++ b/crates/consensus/beacon/src/engine/test_utils.rs @@ -43,7 +43,6 @@ type TestBeaconConsensusEngine = BeaconConsensusEngine< Arc, ShareableBlockchainTree< Arc, - Arc, EitherExecutorFactory, >, >, @@ -484,7 +483,7 @@ where Pipeline::builder().add_stages(DefaultStages::new( HeaderSyncMode::Tip(tip_rx.clone()), - Arc::clone(&consensus) as Arc, + Arc::clone(&consensus), header_downloader, body_downloader, executor_factory.clone(),