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 panic with unpinning pruned blocks #2386

Merged
merged 5 commits into from
Jun 14, 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
56 changes: 35 additions & 21 deletions bin/light-base/src/runtime_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl<TPlat: Platform> RuntimeService<TPlat> {
tree.finalized_async_user_data().clone(),
*decoded_finalized_block.state_root,
decoded_finalized_block.number,
false,
),
);

Expand Down Expand Up @@ -278,6 +279,7 @@ impl<TPlat: Platform> RuntimeService<TPlat> {
runtime.clone(),
*decoded_header.state_root,
decoded_header.number,
true,
),
);

Expand Down Expand Up @@ -353,13 +355,12 @@ impl<TPlat: Platform> RuntimeService<TPlat> {
if let GuardedInner::FinalizedBlockRuntimeKnown {
all_blocks_subscriptions,
pinned_blocks,
finalized_block,
..
} = &mut guarded_lock.tree
{
let unpinned_block_height =
let block_counts_towards_limit =
match pinned_blocks.remove(&(subscription_id.0, *block_hash)) {
Some((_, _, h)) => h,
Some((_, _, _, to_remove)) => !to_remove,
None => {
// Cold path.
if all_blocks_subscriptions.contains_key(&subscription_id.0) {
Expand All @@ -372,12 +373,7 @@ impl<TPlat: Platform> RuntimeService<TPlat> {

guarded_lock.runtimes.retain(|_, rt| rt.strong_count() > 0);

let block_is_finalized = unpinned_block_height
<= header::decode(&finalized_block.scale_encoded_header)
.unwrap()
.number;

if block_is_finalized {
if block_counts_towards_limit {
let (_, finalized_pinned_remaining) = all_blocks_subscriptions
.get_mut(&subscription_id.0)
.unwrap();
Expand Down Expand Up @@ -406,7 +402,7 @@ impl<TPlat: Platform> RuntimeService<TPlat> {
let mut guarded = self.guarded.lock().await;
let guarded = &mut *guarded;

let (runtime, block_state_root_hash, block_number) = {
let (runtime, block_state_root_hash, block_number, _) = {
if let GuardedInner::FinalizedBlockRuntimeKnown { pinned_blocks, .. } =
&mut guarded.tree
{
Expand Down Expand Up @@ -972,9 +968,10 @@ enum GuardedInner<TPlat: Platform> {
/// blocks from this container that are no longer relevant.
///
/// Keys are `(subscription_id, block_hash)`. Values are indices within
/// [`Guarded::runtimes`], state trie root hashes, and block numbers.
/// [`Guarded::runtimes`], state trie root hashes, block numbers, and whether the block
/// is non-finalized and part of the canonical chain.
// TODO: use structs instead of tuples
pinned_blocks: BTreeMap<(u64, [u8; 32]), (Arc<Runtime>, [u8; 32], u64)>,
pinned_blocks: BTreeMap<(u64, [u8; 32]), (Arc<Runtime>, [u8; 32], u64, bool)>,
},
FinalizedBlockRuntimeUnknown {
/// Tree of blocks. Holds the state of the download of everything. Always `Some` when the
Expand Down Expand Up @@ -1489,25 +1486,37 @@ impl<TPlat: Platform> Background<TPlat> {
let all_blocks_notif = Notification::Finalized {
best_block_hash,
hash: finalized_block.hash,
pruned_blocks: pruned_blocks
.into_iter()
.map(|(_, b, _)| b.hash)
.collect(),
pruned_blocks: pruned_blocks.iter().map(|(_, b, _)| b.hash).collect(),
};

let mut to_remove = Vec::new();
for (subscription_id, (sender, finalized_pinned_remaining)) in
all_blocks_subscriptions.iter_mut()
{
if *finalized_pinned_remaining == 0 {
let count_limit = pruned_blocks.len() + 1;

if *finalized_pinned_remaining < count_limit {
to_remove.push(*subscription_id);
continue;
}

if sender.try_send(all_blocks_notif.clone()).is_ok() {
*finalized_pinned_remaining -= 1;
} else {
if sender.try_send(all_blocks_notif.clone()).is_err() {
to_remove.push(*subscription_id);
continue;
}

*finalized_pinned_remaining -= count_limit;

// Mark the finalized and pruned blocks as finalized or non-canonical.
melekes marked this conversation as resolved.
Show resolved Hide resolved
for block in iter::once(&finalized_block.hash)
.chain(pruned_blocks.iter().map(|(_, b, _)| &b.hash))
{
if let Some((_, _, _, non_finalized_canonical)) =
pinned_blocks.get_mut(&(*subscription_id, *block))
{
debug_assert!(*non_finalized_canonical);
*non_finalized_canonical = false;
}
}
}
for to_remove in to_remove {
Expand Down Expand Up @@ -1570,7 +1579,12 @@ impl<TPlat: Platform> Background<TPlat> {
if sender.try_send(notif.clone()).is_ok() {
pinned_blocks.insert(
(*subscription_id, block_hash),
(block_runtime.clone(), block_state_root_hash, block_number),
(
block_runtime.clone(),
block_state_root_hash,
block_number,
true,
),
);
} else {
to_remove.push(*subscription_id);
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 panic introduced in v0.6.18 in case of a fork in the chain related to tracking the number of blocks kept alive in the node's memory. ([#2386](https://github.com/paritytech/smoldot/pull/2386))

## 0.6.18 - 2022-06-14

### Added
Expand Down