Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: log errors on block download failure; implement max retries #503

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions components/chainhook-sdk/src/indexer/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,21 @@ pub async fn download_and_parse_block_with_retry(
ctx: &Context,
) -> Result<BitcoinBlockFullBreakdown, String> {
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));
}
Expand All @@ -180,18 +183,21 @@ pub async fn retrieve_block_hash_with_retry(
ctx: &Context,
) -> Result<String, String> {
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));
}
Expand Down
Loading