Skip to content

Commit 2ab6b32

Browse files
author
Ludo Galabru
committed
fix: add retry logic to work around unexpected responses from bitcoind
1 parent c5335a7 commit 2ab6b32

File tree

2 files changed

+60
-20
lines changed
  • components/chainhook-sdk/src

2 files changed

+60
-20
lines changed

components/chainhook-sdk/src/indexer/bitcoin/mod.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub fn standardize_bitcoin_block(
328328
block: BitcoinBlockFullBreakdown,
329329
network: &BitcoinNetwork,
330330
ctx: &Context,
331-
) -> Result<BitcoinBlockData, String> {
331+
) -> Result<BitcoinBlockData, (String, bool)> {
332332
let mut transactions = vec![];
333333
let block_height = block.height as u64;
334334
let expected_magic_bytes = get_stacks_canonical_magic_bytes(&network);
@@ -364,24 +364,36 @@ pub fn standardize_bitcoin_block(
364364
if input.is_coinbase() {
365365
continue;
366366
}
367-
let prevout = input.prevout.as_ref().ok_or(format!(
368-
"error retrieving prevout for transaction {}, input #{} (block #{})",
369-
tx.txid, index, block.height
367+
let prevout = input.prevout.as_ref().ok_or((
368+
format!(
369+
"error retrieving prevout for transaction {}, input #{} (block #{})",
370+
tx.txid, index, block.height
371+
),
372+
true,
370373
))?;
371374

372-
let txid = input.txid.as_ref().ok_or(format!(
373-
"error retrieving txid for transaction {}, input #{} (block #{})",
374-
tx.txid, index, block.height
375+
let txid = input.txid.as_ref().ok_or((
376+
format!(
377+
"error retrieving txid for transaction {}, input #{} (block #{})",
378+
tx.txid, index, block.height
379+
),
380+
true,
375381
))?;
376382

377-
let vout = input.vout.ok_or(format!(
378-
"error retrieving vout for transaction {}, input #{} (block #{})",
379-
tx.txid, index, block.height
383+
let vout = input.vout.ok_or((
384+
format!(
385+
"error retrieving vout for transaction {}, input #{} (block #{})",
386+
tx.txid, index, block.height
387+
),
388+
true,
380389
))?;
381390

382-
let script_sig = input.script_sig.ok_or(format!(
383-
"error retrieving script_sig for transaction {}, input #{} (block #{})",
384-
tx.txid, index, block.height
391+
let script_sig = input.script_sig.ok_or((
392+
format!(
393+
"error retrieving script_sig for transaction {}, input #{} (block #{})",
394+
tx.txid, index, block.height
395+
),
396+
true,
385397
))?;
386398

387399
sats_in += prevout.value.to_sat();

components/chainhook-sdk/src/observer/mod.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use crate::hord::{
1818
revert_hord_db_with_augmented_bitcoin_block, update_hord_db_and_augment_bitcoin_block,
1919
HordConfig,
2020
};
21-
use crate::indexer::bitcoin::{standardize_bitcoin_block, BitcoinBlockFullBreakdown};
21+
use crate::indexer::bitcoin::{
22+
download_and_parse_block_with_retry, standardize_bitcoin_block, BitcoinBlockFullBreakdown,
23+
};
2224
use crate::indexer::{Indexer, IndexerConfig};
2325
use crate::utils::{send_request, Context};
2426

@@ -702,17 +704,43 @@ pub async fn start_observer_commands_handler(
702704
}
703705
break;
704706
}
705-
ObserverCommand::ProcessBitcoinBlock(block_data) => {
706-
let new_block =
707-
match standardize_bitcoin_block(block_data, &config.bitcoin_network, &ctx) {
708-
Ok(block) => block,
709-
Err(e) => {
707+
ObserverCommand::ProcessBitcoinBlock(mut block_data) => {
708+
let block_hash = block_data.hash.to_string();
709+
let new_block = loop {
710+
match standardize_bitcoin_block(
711+
block_data.clone(),
712+
&config.bitcoin_network,
713+
&ctx,
714+
) {
715+
Ok(block) => break block,
716+
Err((e, retry)) => {
710717
ctx.try_log(|logger| {
711718
slog::error!(logger, "Error standardizing block: {}", e)
712719
});
713-
continue;
720+
if retry {
721+
block_data = match download_and_parse_block_with_retry(
722+
&block_hash,
723+
&config.get_bitcoin_config(),
724+
&ctx,
725+
)
726+
.await
727+
{
728+
Ok(block) => block,
729+
Err(e) => {
730+
ctx.try_log(|logger| {
731+
slog::warn!(
732+
logger,
733+
"unable to download_and_parse_block: {}",
734+
e.to_string()
735+
)
736+
});
737+
continue;
738+
}
739+
};
740+
}
714741
}
715742
};
743+
};
716744
bitcoin_block_store.insert(new_block.block_identifier.clone(), new_block);
717745
}
718746
ObserverCommand::CacheBitcoinBlock(block) => {

0 commit comments

Comments
 (0)