From 60b2ac7e66f05b594efa1057c9e545b3d9e87fe6 Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Thu, 15 Feb 2024 12:19:55 -0500 Subject: [PATCH 1/2] fix: log errors on block download failure; implement max retries --- components/chainhook-sdk/src/indexer/bitcoin/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/chainhook-sdk/src/indexer/bitcoin/mod.rs b/components/chainhook-sdk/src/indexer/bitcoin/mod.rs index 44c999b86..cde3d08b2 100644 --- a/components/chainhook-sdk/src/indexer/bitcoin/mod.rs +++ b/components/chainhook-sdk/src/indexer/bitcoin/mod.rs @@ -153,18 +153,21 @@ pub async fn download_and_parse_block_with_retry( ctx: &Context, ) -> Result { let mut errors_count = 0; + let max_retries = 10; let block = loop { match download_and_parse_block(http_client, block_hash, bitcoin_config, ctx).await { Ok(result) => break result, - Err(_e) => { + Err(e) => { errors_count += 1; - if errors_count > 3 { + if errors_count > 3 && errors_count < max_retries { ctx.try_log(|logger| { slog::warn!( logger, - "unable to fetch and parse block #{block_hash}: will retry in a few seconds (attempt #{errors_count}).", + "unable to fetch and parse block #{block_hash}: will retry in a few seconds (attempt #{errors_count}). Error: {e}", ) }); + } else if errors_count == max_retries { + return Err(format!("unable to fetch and parse block #{block_hash} after {errors_count} attempts. Error: {e}")); } std::thread::sleep(std::time::Duration::from_secs(1)); } From c577146474c59470055dc1235c046d7dc9ac1d5b Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Thu, 15 Feb 2024 12:55:11 -0500 Subject: [PATCH 2/2] log and return error for retrieve block hash --- components/chainhook-sdk/src/indexer/bitcoin/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/chainhook-sdk/src/indexer/bitcoin/mod.rs b/components/chainhook-sdk/src/indexer/bitcoin/mod.rs index cde3d08b2..997c2c0bd 100644 --- a/components/chainhook-sdk/src/indexer/bitcoin/mod.rs +++ b/components/chainhook-sdk/src/indexer/bitcoin/mod.rs @@ -183,18 +183,21 @@ pub async fn retrieve_block_hash_with_retry( ctx: &Context, ) -> Result { let mut errors_count = 0; + let max_retries = 10; let block_hash = loop { match retrieve_block_hash(http_client, block_height, bitcoin_config, ctx).await { Ok(result) => break result, - Err(_e) => { + Err(e) => { errors_count += 1; - if errors_count > 3 { + if errors_count > 3 && errors_count < max_retries { ctx.try_log(|logger| { slog::warn!( logger, - "unable to retrieve block hash #{block_height}: will retry in a few seconds (attempt #{errors_count}).", + "unable to retrieve block hash #{block_height}: will retry in a few seconds (attempt #{errors_count}). Error: {e}", ) }); + } else if errors_count == max_retries { + return Err(format!("unable to retrieve block hash #{block_height} after {errors_count} attempts. Error: {e}")); } std::thread::sleep(std::time::Duration::from_secs(2)); }