From 30549bebcd1bddbfa571e9babcd0dc8d3210b277 Mon Sep 17 00:00:00 2001 From: PJColombo Date: Tue, 11 Feb 2025 01:41:53 +0100 Subject: [PATCH] fix: update current slot accordingly when searching for a common ancient block during a reorg --- src/slots_processor/mod.rs | 83 +++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/src/slots_processor/mod.rs b/src/slots_processor/mod.rs index fe67bb3..a659b29 100644 --- a/src/slots_processor/mod.rs +++ b/src/slots_processor/mod.rs @@ -91,7 +91,7 @@ impl SlotsProcessor { { info!( new_head_slot = block_header.slot, - old__head_slot = prev_block_header.slot, + old_head_slot = prev_block_header.slot, new_head_block_root = ?block_header.root, old_head_block_root = ?prev_block_header.root, "Reorg detected!", @@ -270,65 +270,58 @@ impl SlotsProcessor { let mut rewinded_blocks: Vec = vec![]; while reorg_depth <= MAX_ALLOWED_REORG_DEPTH && current_old_slot > 0 { - reorg_depth += 1; - // We iterate over blocks by slot and not block root as blobscan blocks don't // have parent root we can use to traverse the chain - let old_blobscan_block = match self + if let Some(old_blobscan_block) = self .context .blobscan_client() .get_block(current_old_slot) .await? { - Some(block) => block, - None => { - current_old_slot -= 1; - - continue; - } - }; + let canonical_block_path = self + .get_canonical_block_path(&old_blobscan_block, new_head_header.root) + .await?; - let canonical_block_path = self - .get_canonical_block_path(&old_blobscan_block, new_head_header.root) - .await?; - - // If a path exists, we've found the common ancient block - // and can proceed with handling the reorg. - if !canonical_block_path.is_empty() { - let canonical_block_path = - canonical_block_path.into_iter().rev().collect::>(); - - let canonical_block_headers: Vec = canonical_block_path - .iter() - .map(|block| block.into()) - .collect::>(); - - // If the new canonical block path includes blocks beyond the new head block, - // they were skipped and must be processed. - for block in canonical_block_headers.iter() { - if block.slot != new_head_header.slot { - self.process_block(block) - .await - .with_context(|| format!("Failed to sync forwarded block"))?; + // If a path exists, we've found the common ancient block + if !canonical_block_path.is_empty() { + let canonical_block_path = + canonical_block_path.into_iter().rev().collect::>(); + + let canonical_block_headers: Vec = canonical_block_path + .iter() + .map(|block| block.into()) + .collect::>(); + + // If the new canonical block path includes blocks beyond the new head block, + // they were skipped and must be processed. + for block in canonical_block_headers.iter() { + if block.slot != new_head_header.slot { + self.process_block(block) + .await + .with_context(|| format!("Failed to sync forwarded block"))?; + } } - } - let forwarded_blocks = canonical_block_path - .iter() - .map(|block| block.execution_block_hash) - .collect::>(); + let forwarded_blocks = canonical_block_path + .iter() + .map(|block| block.execution_block_hash) + .collect::>(); - self.context - .blobscan_client() - .handle_reorg(rewinded_blocks.clone(), forwarded_blocks.clone()) - .await?; + self.context + .blobscan_client() + .handle_reorg(rewinded_blocks.clone(), forwarded_blocks.clone()) + .await?; - info!(rewinded_blocks = ?rewinded_blocks, forwarded_blocks = ?forwarded_blocks, "Reorg handled!",); + info!(rewinded_blocks = ?rewinded_blocks, forwarded_blocks = ?forwarded_blocks, "Reorg handled!"); - return Ok(()); + return Ok(()); + } + + rewinded_blocks.push(old_blobscan_block.hash); } - rewinded_blocks.push(old_blobscan_block.hash); + current_old_slot -= 1; + reorg_depth += 1; } Err(anyhow!("No common block found").into())