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

Add RuntimeService::block_runtime_code_heap_pages #1787

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
40 changes: 38 additions & 2 deletions bin/light-base/src/runtime_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,41 @@ impl<TPlat: Platform> RuntimeService<TPlat> {
subscription.next().await.unwrap()
}

/// Returns the value of the `:code` and `:heap_pages` storage items of the given block.
///
/// Returns `None` if the runtime of that block isn't known by the runtime service, for
melekes marked this conversation as resolved.
Show resolved Hide resolved
/// example because the block itself isn't known or because the runtime hasn't been downloaded
/// yet.
pub async fn block_runtime_code_heap_pages(
&self,
block_hash: &[u8; 32],
) -> Option<(Option<Vec<u8>>, Option<Vec<u8>>)> {
let guarded = self.guarded.lock().await;
let guarded = &*guarded;

let runtime_index = match &guarded.tree {
GuardedInner::FinalizedBlockRuntimeKnown {
tree: Some(tree),
finalized_block,
} if finalized_block.hash == *block_hash => *tree.finalized_async_user_data(),
GuardedInner::FinalizedBlockRuntimeKnown {
tree: Some(tree), ..
} => *tree
.input_iter_unordered()
.find(|block| block.user_data.hash == *block_hash)
.and_then(|block| block.async_op_user_data)?,
GuardedInner::FinalizedBlockRuntimeUnknown { tree: Some(tree) } => tree
.input_iter_unordered()
.find(|block| block.user_data.hash == *block_hash)
.and_then(|block| block.async_op_user_data)?
.unwrap(),
Copy link
Contributor

Choose a reason for hiding this comment

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

is it always safe to unwrap here?

_ => unreachable!(),
};

let runtime = &guarded.runtimes[runtime_index];
Some((runtime.runtime_code.clone(), runtime.heap_pages.clone()))
}

/// Returns the SCALE-encoded header of the current finalized block, plus an unlimited stream
/// that produces one item every time the finalized block is changed.
pub async fn subscribe_finalized(
Expand Down Expand Up @@ -2184,15 +2219,16 @@ struct Runtime {
///
/// Can be `None` if the storage is empty, in which case the runtime will have failed to
/// build.
// TODO: consider storing hash instead
///
/// > **Note**: This value is kept unhashed because it can be serialized on disk and reloaded
/// > on startup. See [`RuntimeService::block_runtime_code_heap_pages`].
runtime_code: Option<Vec<u8>>,

/// Undecoded storage value of `:heappages` corresponding to the
/// [`Runtime::runtime`] field.
///
/// Can be `None` if the storage is empty, in which case the runtime will have failed to
/// build.
// TODO: consider storing hash instead
heap_pages: Option<Vec<u8>>,
}

Expand Down