From 5e4955da4863ed7af7eae3802ab69482813e22b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Holm=20Gj=C3=B8rup?= Date: Wed, 29 Jan 2025 10:20:42 +0100 Subject: [PATCH] Address review comments --- backend-rust/src/indexer.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index 4e21adf8..582aed1e 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -113,15 +113,6 @@ impl IndexerService { registry: &mut Registry, config: IndexerServiceConfig, ) -> anyhow::Result { - let last_height_stored = sqlx::query!( - " -SELECT height FROM blocks ORDER BY height DESC LIMIT 1 -" - ) - .fetch_optional(&pool) - .await? - .map(|r| r.height); - // Handle TLS configuration and set timeouts according to the configuration for // every endpoint. let endpoints: Vec = endpoints @@ -144,10 +135,21 @@ SELECT height FROM blocks ORDER BY height DESC LIMIT 1 }) .collect::>()?; + let last_height_stored = sqlx::query!( + " +SELECT height FROM blocks ORDER BY height DESC LIMIT 1 +" + ) + .fetch_optional(&pool) + .await? + .map(|r| r.height); + let start_height = if let Some(height) = last_height_stored { u64::try_from(height)? + 1 } else { - save_genesis_data(endpoints[0].clone(), &pool).await?; + save_genesis_data(endpoints[0].clone(), &pool) + .await + .context("Failed initializing the database with the genesis block")?; 1 }; let genesis_block_hash: sdk_types::hashes::BlockHash = @@ -703,7 +705,8 @@ struct BlockData { /// Function for initializing the database with the genesis block. /// This should only be called if the database is empty. async fn save_genesis_data(endpoint: v2::Endpoint, pool: &PgPool) -> anyhow::Result<()> { - let mut client = v2::Client::new(endpoint).await?; + let mut client = + v2::Client::new(endpoint).await.context("Failed connecting a Concordium node")?; let mut tx = pool.begin().await.context("Failed to create SQL transaction")?; let genesis_height = v2::BlockIdentifier::AbsoluteHeight(0.into()); {