From 19e084fbc49bd5020de47504614f57585633c8f9 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Mon, 30 Sep 2024 14:46:00 +0200 Subject: [PATCH] getblocks retry 5 times on 'Block not found on disk' --- src/daemon.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/daemon.rs b/src/daemon.rs index 457bf4230..da27c6050 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -36,6 +36,9 @@ lazy_static! { ); } +const MAX_ATTEMPTS: u32 = 5; +const RETRY_WAIT_DURATION: Duration = Duration::from_secs(1); + fn parse_hash(value: &Value) -> Result where T: FromStr, @@ -495,7 +498,28 @@ impl Daemon { .iter() .map(|hash| json!([hash, /*verbose=*/ false])) .collect(); - let values = self.requests("getblock", ¶ms_list)?; + + let mut attempts = MAX_ATTEMPTS; + let values = loop { + attempts -= 1; + + match self.requests("getblock", ¶ms_list) { + Ok(blocks) => break blocks, + Err(e) => { + let err_msg = format!("{e:?}"); + if err_msg.contains("Block not found on disk") { + // There is a small chance the node returns the header but didn't finish to index the block + log::warn!("getblocks failing with: {e:?} trying {attempts} more time") + } else { + panic!("failed to get blocks from bitcoind: {}", err_msg); + } + } + } + if attempts == 0 { + panic!("failed to get blocks from bitcoind") + } + std::thread::sleep(RETRY_WAIT_DURATION); + }; let mut blocks = vec![]; for value in values { blocks.push(block_from_value(value)?);