Skip to content

Commit 3ab41c2

Browse files
authored
Do not panic when importing stale block notifications (#3152)
It can happens that we import stale block notifications after having pruned finalized blocks. In this case we should not panic and just gracefully handle it.
1 parent fe9c21b commit 3ab41c2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

light-base/src/runtime_service.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,13 +2491,15 @@ async fn run_background<TPlat: PlatformRef>(
24912491
let parent_index = if new_block.parent_hash == finalized_block.hash {
24922492
None
24932493
} else {
2494-
Some(
2495-
// TODO: O(n)
2496-
tree.input_output_iter_unordered()
2497-
.find(|block| block.user_data.hash == new_block.parent_hash)
2498-
.unwrap()
2499-
.id,
2500-
)
2494+
match tree
2495+
.input_output_iter_unordered()
2496+
.find(|block| block.user_data.hash == new_block.parent_hash)
2497+
{
2498+
Some(block) => Some(block.id),
2499+
// Parent already pruned by finalization; block is
2500+
// on a stale fork and can be silently discarded.
2501+
None => continue,
2502+
}
25012503
};
25022504

25032505
tree.input_insert_block(
@@ -2520,11 +2522,13 @@ async fn run_background<TPlat: PlatformRef>(
25202522
}
25212523
Tree::FinalizedBlockRuntimeUnknown { tree, .. } => {
25222524
// TODO: O(n)
2523-
let parent_index = tree
2525+
let parent_index = match tree
25242526
.input_output_iter_unordered()
25252527
.find(|block| block.user_data.hash == new_block.parent_hash)
2526-
.unwrap()
2527-
.id;
2528+
{
2529+
Some(block) => block.id,
2530+
None => continue,
2531+
};
25282532
tree.input_insert_block(
25292533
Block {
25302534
hash: header::hash_from_scale_encoded_header(

0 commit comments

Comments
 (0)