From 146d7d01700721e3c5c1636e6b7bca5961481af2 Mon Sep 17 00:00:00 2001 From: Piotr Heilman Date: Thu, 19 Sep 2024 11:15:32 +0200 Subject: [PATCH] Fix startup on brand new chain and catchup after long break. --- src/tasks/index.rs | 29 +++++++++++++++++++++-------- src/tasks/prune.rs | 8 ++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/tasks/index.rs b/src/tasks/index.rs index bb3854f..17892f3 100644 --- a/src/tasks/index.rs +++ b/src/tasks/index.rs @@ -108,20 +108,33 @@ pub async fn backfill_to_block( rpc: &Provider, latest_block: Block, ) -> eyre::Result<()> { + // Get the first block from the stream and backfill any missing blocks + let latest_block_number = latest_block + .number + .context("Missing block number")? + .as_u64(); + let next_block_number: u64 = if let Some(latest_db_block_number) = app.db.get_latest_block_number(chain_id).await? { latest_db_block_number + 1 } else { - tracing::info!(chain_id, "No latest block"); - 0 - }; + tracing::info!( + chain_id, + "No latest block in database. Will choose best candidate." + ); - // Get the first block from the stream and backfill any missing blocks - let latest_block_number = latest_block - .number - .context("Missing block number")? - .as_u64(); + // Because we do not store all the blocks (we clean up older blocks) there is no need + // to scan ALL the blocks. Especially as this may take a lot of time... We are trying + // here to move back in time "enough" to get some estimates later. + let jump_back_blocks = 60u64; + + if latest_block_number > jump_back_blocks { + latest_block_number - jump_back_blocks + } else { + 0 + } + }; tracing::info!( latest_block_number, diff --git a/src/tasks/prune.rs b/src/tasks/prune.rs index c505eb0..303e1fa 100644 --- a/src/tasks/prune.rs +++ b/src/tasks/prune.rs @@ -12,12 +12,12 @@ const fn minutes(seconds: i64) -> i64 { seconds * 60 } -const fn hours(seconds: i64) -> i64 { - minutes(seconds) * 60 +const fn hours(hours: i64) -> i64 { + minutes(hours) * 60 } -const fn days(seconds: i64) -> i64 { - hours(seconds) * 24 +const fn days(days: i64) -> i64 { + hours(days) * 24 } // TODO: This should be a per network setting