Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Document a bit tricky condition to avoid duplicate finalization notif…
Browse files Browse the repository at this point in the history
…ications
  • Loading branch information
davxy committed Jan 26, 2022
1 parent 1529b5b commit e242b42
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,6 @@ where
let finalized =
route_from_finalized.enacted().iter().map(|elem| elem.hash).collect::<Vec<_>>();

// Prevent inclusion of stale heads that were already included in previous calls.
let last_finalized_number = self
.backend
.blockchain()
Expand All @@ -862,6 +861,16 @@ where
sp_blockchain::tree_route(self.backend.blockchain(), block, head)?;
let retracted = route_from_finalized.retracted();
let pivot = route_from_finalized.common_block();
// It is not guaranteed that `backend.blockchain().leaves()` doesn't return
// heads that were in a stale state before this finalization and thus already
// included in previous notifications. We want to skip such heads.
// Given the "route" from the currently finalized block to the head under
// analysis, the condition for it to be added to the new stale heads list is:
// `!retracted.is_empty() && !(last_finalized_number > pivot.number)`
// 1. "route" has some "retractions".
// 2. previously finalized block number is not greater than the "route" pivot:
// - if `last_finalized_number <= pivot.number` then this is a new stale head;
// - else the stale head was already included by some previous finalization.
if !(retracted.is_empty() || last_finalized_number > pivot.number) {
stale_heads.push(head);
}
Expand Down
1 change: 1 addition & 0 deletions client/service/test/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ fn finality_notifications_content() {
.build()
.unwrap()
.block;

// Postpone import to test behavior of import of finalized block.

ClientExt::finalize_block(&client, BlockId::Hash(a2.hash()), None).unwrap();
Expand Down

0 comments on commit e242b42

Please sign in to comment.