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

Fix #2617 #2703

Merged
merged 4 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
57 changes: 41 additions & 16 deletions bin/light-base/src/sync_service/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,22 +563,47 @@ pub(super) async fn start_parachain<TPlat: Platform>(
let _ = send_back.send(super::SubscribeAll {
finalized_block_scale_encoded_header: finalized_parahead.clone(),
finalized_block_runtime: None,
non_finalized_blocks_ancestry_order: async_tree.input_iter_unordered().filter_map(|block| {
// `async_op_user_data` is `Some` only if this block has
// already been reported on the output. In order to
// maintain consistency, only these blocks should be
// reported.
let parahead = block.async_op_user_data?.as_ref().unwrap();
let parent_hash = async_tree.parent(block.id)
.map(|idx| header::hash_from_scale_encoded_header(&async_tree.block_async_user_data(idx).unwrap().as_ref().unwrap()))
.unwrap_or_else(|| header::hash_from_scale_encoded_header(&finalized_parahead));

Some(super::BlockNotification {
is_new_best: block.is_output_best,
scale_encoded_header: parahead.clone(),
parent_hash,
})
}).collect(),
non_finalized_blocks_ancestry_order: {
let mut list = HashMap::<_, super::BlockNotification, _>::with_capacity_and_hasher(async_tree.num_input_non_finalized_blocks(), fnv::FnvBuildHasher::default());

for relay_block in async_tree.input_iter_unordered() {
let parablock = match relay_block.async_op_user_data {
Some(b) => b.as_ref().unwrap(),
None => continue,
};

let parablock_hash = header::hash_from_scale_encoded_header(&parablock);

match list.entry(parablock_hash) {
hashbrown::hash_map::Entry::Occupied(entry) => {
if relay_block.is_output_best {
entry.into_mut().is_new_best = true;
}
}
hashbrown::hash_map::Entry::Vacant(entry) => {
let parent_hash = async_tree
.ancestors(relay_block.id)
.find_map(|idx| {
let hash = header::hash_from_scale_encoded_header(&async_tree.block_async_user_data(idx).unwrap().as_ref().unwrap());
if hash != parablock_hash {
Some(hash)
} else {
None
}
})
.unwrap_or_else(|| header::hash_from_scale_encoded_header(&finalized_parahead));

entry.insert(super::BlockNotification {
is_new_best: relay_block.is_output_best,
scale_encoded_header: parablock.clone(),
parent_hash,
});
}
}
}

list.into_iter().map(|(_, v)| v).collect()
},
new_blocks,
});
} else {
Expand Down
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- Fix occasional panic when connecting to a parachain with forks and/or missed slots. ([#2703](https://github.com/paritytech/smoldot/pull/2703))

## 0.6.31 - 2022-08-30

### Changed
Expand Down
11 changes: 11 additions & 0 deletions src/chain/async_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ where
self.non_finalized_blocks.parent(node)
}

/// Returns the ancestors of the given node. The iterator stops when it reaches the finalized
/// block. The iterator is empty if the parent of the node is the finalized block.
///
/// # Panic
///
/// Panics if the [`NodeIndex`] is invalid.
///
pub fn ancestors(&'_ self, node: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_ {
self.non_finalized_blocks.ancestors(node)
}

/// Returns the list of children that have the given node as parent.
///
/// # Panic
Expand Down
11 changes: 11 additions & 0 deletions src/chain/fork_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ impl<T> ForkTree<T> {
}
}

/// Returns the ancestors of the given node. The iterator is empty if the node doesn't have
/// any parent.
///
/// # Panic
///
/// Panics if the [`NodeIndex`] is invalid.
///
pub fn ancestors(&'_ self, node: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_ {
iter::successors(Some(node), move |n| self.nodes[n.0].parent.map(NodeIndex)).skip(1)
}

/// Returns the parent of the given node. Returns `None` if the node doesn't have any parent.
///
/// # Panic
Expand Down