Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the same ProviderFactory in reth node #5778

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
debug!(target: "reth::cli", "configured blockchain tree");

// fetch the head block from the database
let head = self.lookup_head(Arc::clone(&db)).wrap_err("the head block is missing")?;
let head =
self.lookup_head(provider_factory.clone()).wrap_err("the head block is missing")?;

// setup the blockchain provider
let blockchain_db =
Expand Down Expand Up @@ -346,7 +347,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
let default_peers_path = data_dir.known_peers_path();
let network_config = self.load_network_config(
&config,
Arc::clone(&db),
provider_factory.clone(),
ctx.task_executor.clone(),
head,
secret_key,
Expand Down Expand Up @@ -389,7 +390,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
let max_block = if let Some(block) = self.debug.max_block {
Some(block)
} else if let Some(tip) = self.debug.tip {
Some(self.lookup_or_fetch_tip(&db, &network_client, tip).await?)
Some(self.lookup_or_fetch_tip(provider_factory.clone(), &network_client, tip).await?)
} else {
None
};
Expand Down Expand Up @@ -425,7 +426,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
&config.stages,
client.clone(),
Arc::clone(&consensus),
provider_factory,
provider_factory.clone(),
&ctx.task_executor,
sync_metrics_tx,
prune_config.clone(),
Expand All @@ -445,7 +446,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
&config.stages,
network_client.clone(),
Arc::clone(&consensus),
provider_factory,
provider_factory.clone(),
&ctx.task_executor,
sync_metrics_tx,
prune_config.clone(),
Expand Down Expand Up @@ -476,7 +477,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
let pruner_events = if let Some(prune_config) = prune_config {
let mut pruner = self.build_pruner(
&prune_config,
db.clone(),
provider_factory,
tree_config,
snapshotter.highest_snapshot_receiver(),
);
Expand Down Expand Up @@ -747,8 +748,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
/// Fetches the head block from the database.
///
/// If the database is empty, returns the genesis block.
fn lookup_head<DB: Database>(&self, db: DB) -> RethResult<Head> {
let factory = ProviderFactory::new(db, self.chain.clone());
fn lookup_head<DB: Database>(&self, factory: ProviderFactory<DB>) -> RethResult<Head> {
let provider = factory.provider()?;

let head = provider.get_stage_checkpoint(StageId::Finish)?.unwrap_or_default().block_number;
Expand Down Expand Up @@ -780,31 +780,30 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
/// NOTE: The download is attempted with infinite retries.
async fn lookup_or_fetch_tip<DB, Client>(
&self,
db: DB,
provider_factory: ProviderFactory<DB>,
client: Client,
tip: B256,
) -> RethResult<u64>
where
DB: Database,
Client: HeadersClient,
{
Ok(self.fetch_tip(db, client, BlockHashOrNumber::Hash(tip)).await?.number)
Ok(self.fetch_tip(provider_factory, client, BlockHashOrNumber::Hash(tip)).await?.number)
}

/// Attempt to look up the block with the given number and return the header.
///
/// NOTE: The download is attempted with infinite retries.
async fn fetch_tip<DB, Client>(
&self,
db: DB,
factory: ProviderFactory<DB>,
client: Client,
tip: BlockHashOrNumber,
) -> RethResult<SealedHeader>
where
DB: Database,
Client: HeadersClient,
{
let factory = ProviderFactory::new(db, self.chain.clone());
let provider = factory.provider()?;

let header = provider.header_by_hash_or_number(tip)?;
Expand Down Expand Up @@ -832,7 +831,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
fn load_network_config<DB: Database>(
&self,
config: &Config,
db: DB,
provider_factory: ProviderFactory<DB>,
executor: TaskExecutor,
head: Head,
secret_key: SecretKey,
Expand Down Expand Up @@ -862,7 +861,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
.sequencer_endpoint(self.rollup.sequencer_http.clone())
.disable_tx_gossip(self.rollup.disable_txpool_gossip);

cfg_builder.build(ProviderFactory::new(db, self.chain.clone()))
cfg_builder.build(provider_factory)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -980,7 +979,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
fn build_pruner<DB: Database>(
&self,
config: &PruneConfig,
db: DB,
provider_factory: ProviderFactory<DB>,
tree_config: BlockchainTreeConfig,
highest_snapshots_rx: HighestSnapshotsTracker,
) -> Pruner<DB> {
Expand Down Expand Up @@ -1014,8 +1013,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
);

Pruner::new(
db,
self.chain.clone(),
provider_factory,
segments.into_vec(),
config.block_interval,
self.chain.prune_delete_limit,
Expand Down
6 changes: 3 additions & 3 deletions crates/consensus/beacon/src/engine/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ where
BlockchainTree::new(externals, config, None).expect("failed to create tree"),
);
let latest = self.base_config.chain_spec.genesis_header().seal_slow();
let blockchain_provider = BlockchainProvider::with_latest(provider_factory, tree, latest);
let blockchain_provider =
BlockchainProvider::with_latest(provider_factory.clone(), tree, latest);

let pruner = Pruner::new(
db.clone(),
self.base_config.chain_spec.clone(),
provider_factory,
vec![],
5,
self.base_config.chain_spec.prune_delete_limit,
Expand Down
11 changes: 6 additions & 5 deletions crates/prune/src/pruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Metrics, PrunerError, PrunerEvent,
};
use reth_db::database::Database;
use reth_primitives::{BlockNumber, ChainSpec, PruneMode, PruneProgress, PruneSegment};
use reth_primitives::{BlockNumber, PruneMode, PruneProgress, PruneSegment};
use reth_provider::{ProviderFactory, PruneCheckpointReader};
use reth_snapshot::HighestSnapshotsTracker;
use reth_tokio_util::EventListeners;
Expand Down Expand Up @@ -46,16 +46,15 @@ pub struct Pruner<DB> {
impl<DB: Database> Pruner<DB> {
/// Creates a new [Pruner].
pub fn new(
db: DB,
chain_spec: Arc<ChainSpec>,
provider_factory: ProviderFactory<DB>,
segments: Vec<Arc<dyn Segment<DB>>>,
min_block_interval: usize,
delete_limit: usize,
prune_max_blocks_per_run: usize,
highest_snapshots_tracker: HighestSnapshotsTracker,
) -> Self {
Self {
provider_factory: ProviderFactory::new(db, chain_spec),
provider_factory,
segments,
min_block_interval,
previous_tip_block_number: None,
Expand Down Expand Up @@ -267,12 +266,14 @@ mod tests {
use crate::Pruner;
use reth_db::test_utils::create_test_rw_db;
use reth_primitives::MAINNET;
use reth_provider::ProviderFactory;
use tokio::sync::watch;

#[test]
fn is_pruning_needed() {
let db = create_test_rw_db();
let mut pruner = Pruner::new(db, MAINNET.clone(), vec![], 5, 0, 5, watch::channel(None).1);
let provider_factory = ProviderFactory::new(db, MAINNET.clone());
let mut pruner = Pruner::new(provider_factory, vec![], 5, 0, 5, watch::channel(None).1);

// No last pruned block number was set before
let first_block_number = 1;
Expand Down
Loading