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

hotfix: blockchain/backend: Skip genesis leaf to unblock syncing #5103

Merged
merged 18 commits into from
Jul 23, 2024
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
97 changes: 91 additions & 6 deletions substrate/primitives/blockchain/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ pub trait Backend<Block: BlockT>:
) -> std::result::Result<DisplacedLeavesAfterFinalization<Block>, Error> {
let leaves = self.leaves()?;

let now = std::time::Instant::now();
debug!(
target: crate::LOG_TARGET,
?leaves,
%finalized_block_hash,
?finalized_block_hash,
?finalized_block_number,
"Checking for displaced leaves after finalization."
);
Expand All @@ -281,7 +282,15 @@ pub trait Backend<Block: BlockT>:
);
return Ok(DisplacedLeavesAfterFinalization::default());
},
Err(e) => Err(e)?,
Err(e) => {
debug!(
target: crate::LOG_TARGET,
hash = ?finalized_block_hash,
err = ?e,
"Failed to fetch block"
);
return Err(e);
},
};
finalized_chain.push_front(MinimalBlockMetadata::from(&current_finalized));

Expand All @@ -297,10 +306,34 @@ pub trait Backend<Block: BlockT>:
let mut displaced_blocks_candidates = Vec::new();

for leaf_hash in leaves {
lexnv marked this conversation as resolved.
Show resolved Hide resolved
let info = self.info();
if leaf_hash == info.genesis_hash {
// Genesis block is not displaced
debug!(
target: crate::LOG_TARGET,
"Skip genesis block {leaf_hash:?} reporterd as leaf (elapsed {:?})", now.elapsed(),
);

continue;
}

let mut current_header_metadata =
MinimalBlockMetadata::from(&self.header_metadata(leaf_hash)?);
MinimalBlockMetadata::from(&self.header_metadata(leaf_hash).map_err(|err| {
debug!(
target: crate::LOG_TARGET,
?err,
"Failed to fetch header for {leaf_hash:?} (elapsed {:?})", now.elapsed(),
);
err
})?);
let leaf_number = current_header_metadata.number;

debug!(
target: crate::LOG_TARGET,
?leaf_number,
lexnv marked this conversation as resolved.
Show resolved Hide resolved
"Handle displaced leaf {leaf_hash:?} (elapsed {:?})", now.elapsed(),
);

// Collect all block hashes until the height of the finalized block
displaced_blocks_candidates.clear();
while current_header_metadata.number > finalized_block_number {
Expand All @@ -313,7 +346,15 @@ pub trait Backend<Block: BlockT>:
},
None => {
current_header_metadata =
MinimalBlockMetadata::from(&self.header_metadata(parent_hash)?);
MinimalBlockMetadata::from(&self.header_metadata(parent_hash).map_err(|err| {
debug!(
target: crate::LOG_TARGET,
?err,
"Failed to fetch header for parent hash {parent_hash:?} during leaf tracking (elapsed {:?})", now.elapsed(),
);

err
})?);
// Cache locally in case more branches above finalized block reference
// the same block hash
local_cache.insert(parent_hash, current_header_metadata);
Expand All @@ -324,6 +365,11 @@ pub trait Backend<Block: BlockT>:
// If points back to the finalized header then nothing left to do, this leaf will be
// checked again later
if current_header_metadata.hash == finalized_block_hash {
debug!(
target: crate::LOG_TARGET,
"Leaf points to the finalized header {leaf_hash:?}, skipping for now (elapsed {:?})", now.elapsed(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: IMO since we are using tracing lib now we should not use the inline formatting but the structured logging only. This plus some consistend naming gives us nicely formatted logs.

);

continue;
}

Expand All @@ -332,6 +378,14 @@ pub trait Backend<Block: BlockT>:
// check for this gap later.
displaced_blocks_candidates.push(current_header_metadata.hash);

debug!(
target: crate::LOG_TARGET,
current_hash = ?current_header_metadata.hash,
current_num = ?current_header_metadata.number,
?finalized_block_number,
"Looking for path from finalized block number to current leaf number"
);

// Collect the rest of the displaced blocks of leaf branch
for distance_from_finalized in 1_u32.. {
// Find block at `distance_from_finalized` from finalized block
Expand All @@ -352,7 +406,14 @@ pub trait Backend<Block: BlockT>:
);
break;
},
Err(e) => Err(e)?,
Err(e) => {
debug!(
target: crate::LOG_TARGET,
err = ?e,
"Failed to fetch header for parent hash {:?} during block collection (elapsed {:?})", to_fetch.parent, now.elapsed(),
);
return Err(e);
},
};
let metadata = MinimalBlockMetadata::from(&metadata);
let result = (metadata.number, metadata.hash);
Expand All @@ -372,20 +433,44 @@ pub trait Backend<Block: BlockT>:
// Reached finalized chain, nothing left to do
result.displaced_blocks.extend(displaced_blocks_candidates.drain(..));
result.displaced_leaves.push((leaf_number, leaf_hash));

debug!(
target: crate::LOG_TARGET,
"Found displaced leaf {leaf_hash:?} (elapsed {:?})", now.elapsed(),
);
break;
}

// Store displaced block and look deeper for block on finalized chain
debug!(
target: crate::LOG_TARGET,
"Found displaced block {parent_hash:?} and look further... (elapsed {:?})", now.elapsed(),
);
displaced_blocks_candidates.push(parent_hash);
current_header_metadata =
MinimalBlockMetadata::from(&self.header_metadata(parent_hash)?);
MinimalBlockMetadata::from(&self.header_metadata(parent_hash).map_err(|err| {
debug!(
target: crate::LOG_TARGET,
?err,
"Failed to fetch header for parent {parent_hash:?} during displaced block collection (elapsed {:?})", now.elapsed(),
);
err
})?);
}
}

// There could be duplicates shared by multiple branches, clean them up
result.displaced_blocks.sort_unstable();
result.displaced_blocks.dedup();

debug!(
target: crate::LOG_TARGET,
%finalized_block_hash,
?finalized_block_number,
"Finished with result {result:?} (elapsed {:?})",
now.elapsed(),
);

return Ok(result);
}
}
Expand Down
Loading