Skip to content

Commit

Permalink
Fix startup on brand new chain and catchup after long break. (#55)
Browse files Browse the repository at this point in the history
* Fix startup on brand new chain and catchup after long break.

* PR changes.
  • Loading branch information
piohei authored Sep 25, 2024
1 parent 512d69b commit 9b758de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
29 changes: 21 additions & 8 deletions src/tasks/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const TIME_BETWEEN_FEE_ESTIMATION_SECONDS: u64 = 30;

const GAS_PRICE_FOR_METRICS_FACTOR: f64 = 1e-9;

const MAX_RECENT_BLOCKS_TO_CHECK: u64 = 60;

pub async fn index_chain(app: Arc<App>, chain_id: u64) -> eyre::Result<()> {
loop {
index_inner(app.clone(), chain_id).await?;
Expand Down Expand Up @@ -108,20 +110,31 @@ pub async fn backfill_to_block(
rpc: &Provider<Http>,
latest_block: Block<H256>,
) -> 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.
if latest_block_number > MAX_RECENT_BLOCKS_TO_CHECK {
latest_block_number - MAX_RECENT_BLOCKS_TO_CHECK
} else {
0
}
};

tracing::info!(
latest_block_number,
Expand Down
12 changes: 6 additions & 6 deletions src/tasks/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use crate::app::App;
const BLOCK_PRUNING_INTERVAL: Duration = Duration::from_secs(60);
const TX_PRUNING_INTERVAL: Duration = Duration::from_secs(60);

const fn minutes(seconds: i64) -> i64 {
seconds * 60
const fn minutes(minutes: i64) -> i64 {
minutes * 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
Expand Down

0 comments on commit 9b758de

Please sign in to comment.