From 313e5b7827aca2b4e9b3a26886869aabfdbaf1dd Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 7 Jan 2024 23:05:17 +0100 Subject: [PATCH] chore: cleanup max block fetching (#5960) --- bin/reth/src/builder/mod.rs | 38 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/bin/reth/src/builder/mod.rs b/bin/reth/src/builder/mod.rs index 2e589d67c..1c54a2911 100644 --- a/bin/reth/src/builder/mod.rs +++ b/bin/reth/src/builder/mod.rs @@ -438,19 +438,19 @@ impl NodeConfig { /// Returns the max block that the node should run to, looking it up from the network if /// necessary - pub async fn max_block( + async fn max_block( &self, network_client: &Client, - provider_factory: ProviderFactory, + provider: Provider, ) -> eyre::Result> where - DB: Database, + Provider: HeaderProvider, Client: HeadersClient, { 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(provider_factory, network_client, tip).await?) + Some(self.lookup_or_fetch_tip(provider, network_client, tip).await?) } else { None }; @@ -751,42 +751,38 @@ impl NodeConfig { /// If it doesn't exist, download the header and return the block number. /// /// NOTE: The download is attempted with infinite retries. - async fn lookup_or_fetch_tip( + async fn lookup_or_fetch_tip( &self, - provider_factory: ProviderFactory, + provider: Provider, client: Client, tip: B256, ) -> RethResult where - DB: Database, + Provider: HeaderProvider, Client: HeadersClient, { - Ok(self.fetch_tip(provider_factory, client, BlockHashOrNumber::Hash(tip)).await?.number) + let header = provider.header_by_hash_or_number(tip.into())?; + + // try to look up the header in the database + if let Some(header) = header { + info!(target: "reth::cli", ?tip, "Successfully looked up tip block in the database"); + return Ok(header.number) + } + + Ok(self.fetch_tip_from_network(client, tip.into()).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( + async fn fetch_tip_from_network( &self, - factory: ProviderFactory, client: Client, tip: BlockHashOrNumber, ) -> RethResult where - DB: Database, Client: HeadersClient, { - let provider = factory.provider()?; - - let header = provider.header_by_hash_or_number(tip)?; - - // try to look up the header in the database - if let Some(header) = header { - info!(target: "reth::cli", ?tip, "Successfully looked up tip block in the database"); - return Ok(header.seal_slow()) - } - info!(target: "reth::cli", ?tip, "Fetching tip block from the network."); loop { match get_single_header(&client, tip).await {