Skip to content

Commit

Permalink
chore: move tree setup to builder (#7577)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Apr 11, 2024
1 parent 8304780 commit 6a83391
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 93 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

24 changes: 23 additions & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,26 @@ where
DB: Database + Clone,
EVM: ExecutorFactory,
{
/// Create a new blockchain tree.
/// Builds the blockchain tree for the node.
///
/// This method configures the blockchain tree, which is a critical component of the node,
/// responsible for managing the blockchain state, including blocks, transactions, and receipts.
/// It integrates with the consensus mechanism and the EVM for executing transactions.
///
/// # Parameters
/// - `externals`: External components required by the blockchain tree:
/// - `provider_factory`: A factory for creating various blockchain-related providers, such
/// as for accessing the database or static files.
/// - `consensus`: The consensus configuration, which defines how the node reaches agreement
/// on the blockchain state with other nodes.
/// - `evm_config`: The EVM (Ethereum Virtual Machine) configuration, which affects how
/// smart contracts and transactions are executed. Proper validation of this configuration
/// is crucial for the correct execution of transactions.
/// - `tree_config`: Configuration for the blockchain tree, including any parameters that affect
/// its structure or performance.
/// - `prune_modes`: Configuration for pruning old blockchain data. This helps in managing the
/// storage space efficiently. It's important to validate this configuration to ensure it does
/// not lead to unintended data loss.
pub fn new(
externals: TreeExternals<DB, EVM>,
config: BlockchainTreeConfig,
Expand Down Expand Up @@ -124,6 +143,9 @@ where
}

/// Set the sync metric events sender.
///
/// A transmitter for sending synchronization metrics. This is used for monitoring the node's
/// synchronization process with the blockchain network.
pub fn with_sync_metrics_tx(mut self, metrics_tx: MetricEventsSender) -> Self {
self.sync_metrics_tx = Some(metrics_tx);
self
Expand Down
18 changes: 12 additions & 6 deletions crates/node-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use reth_beacon_consensus::{
hooks::{EngineHooks, PruneHook, StaticFileHook},
BeaconConsensusEngine,
};
use reth_blockchain_tree::{BlockchainTreeConfig, ShareableBlockchainTree};
use reth_blockchain_tree::{
BlockchainTree, BlockchainTreeConfig, ShareableBlockchainTree, TreeExternals,
};
use reth_config::config::EtlConfig;
use reth_db::{
database::Database,
Expand Down Expand Up @@ -530,16 +532,20 @@ where

let prune_config = config.prune_config()?.or_else(|| reth_config.prune.clone());

// Configure the blockchain tree for the node
let evm_config = types.evm_config();
let tree_config = BlockchainTreeConfig::default();
let tree = config.build_blockchain_tree(
let tree_externals = TreeExternals::new(
provider_factory.clone(),
consensus.clone(),
prune_config.clone(),
sync_metrics_tx.clone(),
EvmProcessorFactory::new(config.chain.clone(), evm_config.clone()),
);
let tree = BlockchainTree::new(
tree_externals,
tree_config,
evm_config.clone(),
)?;
prune_config.as_ref().map(|config| config.segments.clone()),
)?
.with_sync_metrics_tx(sync_metrics_tx.clone());

let canon_state_notification_sender = tree.canon_state_notification_sender();
let blockchain_tree = ShareableBlockchainTree::new(tree);
Expand Down
2 changes: 0 additions & 2 deletions crates/node-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ reth-downloaders.workspace = true
reth-revm.workspace = true
reth-stages.workspace = true
reth-prune.workspace = true
reth-blockchain-tree.workspace = true
reth-static-file.workspace = true

# ethereum
Expand Down Expand Up @@ -106,7 +105,6 @@ optimism = [
"reth-rpc-types-compat/optimism",
"reth-auto-seal-consensus/optimism",
"reth-consensus-common/optimism",
"reth-blockchain-tree/optimism",
"reth-beacon-consensus/optimism",
]

Expand Down
85 changes: 2 additions & 83 deletions crates/node-core/src/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ use metrics_exporter_prometheus::PrometheusHandle;
use once_cell::sync::Lazy;
use reth_auto_seal_consensus::{AutoSealConsensus, MiningMode};
use reth_beacon_consensus::BeaconConsensus;
use reth_blockchain_tree::{
config::BlockchainTreeConfig, externals::TreeExternals, BlockchainTree,
};
use reth_config::{
config::{PruneConfig, StageConfig},
Config,
Expand Down Expand Up @@ -51,18 +48,14 @@ use reth_provider::{
CanonStateSubscriptions, HeaderProvider, HeaderSyncMode, ProviderFactory,
StageCheckpointReader,
};
use reth_revm::{
stack::{Hook, InspectorStackConfig},
EvmProcessorFactory,
};
use reth_revm::stack::{Hook, InspectorStackConfig};
use reth_stages::{
prelude::*,
stages::{
AccountHashingStage, ExecutionStage, ExecutionStageThresholds, IndexAccountHistoryStage,
IndexStorageHistoryStage, MerkleStage, SenderRecoveryStage, StorageHashingStage,
TransactionLookupStage,
},
MetricEvent,
};
use reth_static_file::StaticFileProducer;
use reth_tasks::TaskExecutor;
Expand All @@ -72,10 +65,7 @@ use reth_transaction_pool::{
};
use secp256k1::SecretKey;
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
use tokio::sync::{
mpsc::{Receiver, UnboundedSender},
watch,
};
use tokio::sync::{mpsc::Receiver, watch};
use tracing::*;

/// The default prometheus recorder handle. We use a global static to ensure that it is only
Expand Down Expand Up @@ -383,77 +373,6 @@ impl NodeConfig {
Ok(builder)
}

/// Builds the blockchain tree for the node.
///
/// This method configures the blockchain tree, which is a critical component of the node,
/// responsible for managing the blockchain state, including blocks, transactions, and receipts.
/// It integrates with the consensus mechanism and the EVM for executing transactions.
///
/// # Parameters
/// - `provider_factory`: A factory for creating various blockchain-related providers, such as
/// for accessing the database or static files.
/// - `consensus`: The consensus configuration, which defines how the node reaches agreement on
/// the blockchain state with other nodes.
/// - `prune_config`: Configuration for pruning old blockchain data. This helps in managing the
/// storage space efficiently. It's important to validate this configuration to ensure it does
/// not lead to unintended data loss.
/// - `sync_metrics_tx`: A transmitter for sending synchronization metrics. This is used for
/// monitoring the node's synchronization process with the blockchain network.
/// - `tree_config`: Configuration for the blockchain tree, including any parameters that affect
/// its structure or performance.
/// - `evm_config`: The EVM (Ethereum Virtual Machine) configuration, which affects how smart
/// contracts and transactions are executed. Proper validation of this configuration is
/// crucial for the correct execution of transactions.
///
/// # Returns
/// A `ShareableBlockchainTree` instance, which provides access to the blockchain state and
/// supports operations like block insertion, state reversion, and transaction execution.
///
/// # Example
/// ```rust,ignore
/// let tree = config.build_blockchain_tree(
/// provider_factory,
/// consensus,
/// prune_config,
/// sync_metrics_tx,
/// BlockchainTreeConfig::default(),
/// evm_config,
/// )?;
/// ```
///
/// # Note
/// Ensure that all configurations passed to this method are validated beforehand to prevent
/// runtime errors. Specifically, `prune_config` and `evm_config` should be checked to ensure
/// they meet the node's operational requirements.
pub fn build_blockchain_tree<DB, EvmConfig>(
&self,
provider_factory: ProviderFactory<DB>,
consensus: Arc<dyn Consensus>,
prune_config: Option<PruneConfig>,
sync_metrics_tx: UnboundedSender<MetricEvent>,
tree_config: BlockchainTreeConfig,
evm_config: EvmConfig,
) -> eyre::Result<BlockchainTree<DB, EvmProcessorFactory<EvmConfig>>>
where
DB: Database + Unpin + Clone + 'static,
EvmConfig: ConfigureEvm + Clone + 'static,
{
// configure blockchain tree
let tree_externals = TreeExternals::new(
provider_factory,
consensus.clone(),
EvmProcessorFactory::new(self.chain.clone(), evm_config),
);
let tree = BlockchainTree::new(
tree_externals,
tree_config,
prune_config.map(|config| config.segments),
)?
.with_sync_metrics_tx(sync_metrics_tx);

Ok(tree)
}

/// Build a transaction pool and spawn the transaction pool maintenance task
pub fn build_and_spawn_txpool<DB, Tree>(
&self,
Expand Down

0 comments on commit 6a83391

Please sign in to comment.