Skip to content

Commit

Permalink
fix: update current slot accordingly when searching for a common anci…
Browse files Browse the repository at this point in the history
…ent block during a reorg
  • Loading branch information
PJColombo committed Feb 11, 2025
1 parent f3ea424 commit 30549be
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions src/slots_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl SlotsProcessor<ReqwestTransport> {
{
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!",
Expand Down Expand Up @@ -270,65 +270,58 @@ impl SlotsProcessor<ReqwestTransport> {
let mut rewinded_blocks: Vec<B256> = 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::<Vec<_>>();

let canonical_block_headers: Vec<BlockHeader> = canonical_block_path
.iter()
.map(|block| block.into())
.collect::<Vec<_>>();

// 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::<Vec<_>>();

let canonical_block_headers: Vec<BlockHeader> = canonical_block_path
.iter()
.map(|block| block.into())
.collect::<Vec<_>>();

// 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::<Vec<_>>();
let forwarded_blocks = canonical_block_path
.iter()
.map(|block| block.execution_block_hash)
.collect::<Vec<_>>();

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())

Check warning on line 312 in src/slots_processor/mod.rs

View workflow job for this annotation

GitHub Actions / Run tests

useless use of `format!`

warning: useless use of `format!` --> src/slots_processor/mod.rs:312:46 | 312 | ... .with_context(|| format!("Failed to sync forwarded block"))?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"Failed to sync forwarded block".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `#[warn(clippy::useless_format)]` on by default
.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())
Expand Down

0 comments on commit 30549be

Please sign in to comment.