Skip to content

Commit 27ab44d

Browse files
committed
feat: check if chain service can backfill
1 parent e0e4ae9 commit 27ab44d

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

chain/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ pub struct AppConfig {
4040
)]
4141
pub backfill_from: Option<u32>,
4242

43+
#[clap(
44+
long,
45+
env,
46+
default_value = "3600",
47+
help = "Crawl from given height and do not update crawler_state"
48+
)]
49+
pub storage_read_past_height_limit: u32,
50+
4351
#[clap(flatten)]
4452
pub log: LogConfig,
4553
}

chain/src/main.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,22 @@ async fn main() -> Result<(), MainError> {
7979
rlimit::increase_nofile_limit(10240).unwrap();
8080
rlimit::increase_nofile_limit(u64::MAX).unwrap();
8181

82+
let last_block_height = namada_service::get_last_block(&client)
83+
.await
84+
.into_rpc_error()?;
85+
let crawler_state = db_service::try_get_chain_crawler_state(&conn)
86+
.await
87+
.into_db_error()?;
88+
89+
let limit = last_block_height - config.storage_read_past_height_limit;
90+
91+
let can_continue =
92+
crawler_state.map(|s| (s.last_processed_block >= limit, s));
93+
let can_backfill = config.backfill_from.map(|bf| (bf >= limit, bf));
94+
8295
// See if we can start from existing crawler_state
83-
let crawler_state = match (
84-
config.backfill_from,
85-
db_service::try_get_chain_crawler_state(&conn)
86-
.await
87-
.into_db_error()?,
88-
) {
89-
(Some(height), _) => {
96+
let crawler_state = match (can_backfill, can_continue) {
97+
(Some((true, height)), _) => {
9098
tracing::warn!("Backfilling from block height {}", height);
9199
Some(ChainCrawlerState {
92100
last_processed_block: height,
@@ -95,7 +103,7 @@ async fn main() -> Result<(), MainError> {
95103
timestamp: 0,
96104
})
97105
}
98-
(None, Some(crawler_state)) => {
106+
(None, Some((true, crawler_state))) => {
99107
tracing::debug!(
100108
"Found chain crawler state, attempting initial crawl at block \
101109
{}...",
@@ -151,9 +159,11 @@ async fn main() -> Result<(), MainError> {
151159
}
152160
}
153161
}
154-
(None, None) => {
155-
tracing::debug!(
156-
"No chain crawler state found, starting from initial_query..."
162+
_ => {
163+
tracing::warn!(
164+
"Couldn't continue from the last state. Cannot query more \
165+
than {} blocks in the past. Starting from initial_query...",
166+
config.storage_read_past_height_limit
157167
);
158168
None
159169
}

chain/src/services/namada.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ use super::utils::{
3939
query_storage_value,
4040
};
4141

42+
pub async fn get_last_block(
43+
client: &HttpClient,
44+
) -> anyhow::Result<BlockHeight> {
45+
let last_block = RPC
46+
.shell()
47+
.last_block(client)
48+
.await
49+
.context("Failed to query Namada's last committed block")?;
50+
51+
last_block
52+
.ok_or(anyhow::anyhow!("No last block found"))
53+
.map(|b| BlockHeight::from(b.height.0 as u32))
54+
}
55+
4256
pub async fn get_native_token(client: &HttpClient) -> anyhow::Result<Id> {
4357
let operation = || async {
4458
RPC.shell()

0 commit comments

Comments
 (0)