Skip to content

Commit

Permalink
Add full node support for chainHead_unstable_header (#1126)
Browse files Browse the repository at this point in the history
* Add full node support for `chainHead_unstable_header`

* Oops, wrong logic
  • Loading branch information
tomaka authored Sep 8, 2023
1 parent 05b9ceb commit 16fe315
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
32 changes: 25 additions & 7 deletions full-node/src/json_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,18 +547,38 @@ fn spawn_client_main_task(
client_main_task = task;

match request_process.request() {
smoldot::json_rpc::methods::MethodCall::chainHead_unstable_unpin {
methods::MethodCall::chainHead_unstable_header {
follow_subscription,
..
} => {
if let Some(follow_subscription) =
chain_head_follow_subscriptions.get_mut(&*follow_subscription)
{
let _ = follow_subscription
.send(chain_head_subscriptions::Message::Header {
request: request_process,
})
.await;
// TODO racy; doesn't handle situation where follow subscription stops
} else {
request_process
.respond(methods::Response::chainHead_unstable_header(None));
}
}
methods::MethodCall::chainHead_unstable_unpin {
follow_subscription,
hash,
} => {
if let Some(follow_subscription) =
chain_head_follow_subscriptions.get_mut(&*follow_subscription)
{
let block_hashes = match hash {
smoldot::json_rpc::methods::HashHexStringSingleOrArray::Array(list) => {
methods::HashHexStringSingleOrArray::Array(list) => {
list.into_iter().map(|h| h.0).collect::<Vec<_>>()
},
smoldot::json_rpc::methods::HashHexStringSingleOrArray::Single(hash) => vec![hash.0]
}
methods::HashHexStringSingleOrArray::Single(hash) => {
vec![hash.0]
}
};

let (outcome, outcome_rx) = oneshot::channel();
Expand Down Expand Up @@ -602,9 +622,7 @@ fn spawn_client_main_task(

match subscription_start.request() {
// TODO: enforce limit to number of subscriptions
smoldot::json_rpc::methods::MethodCall::chainHead_unstable_follow {
with_runtime,
} => {
methods::MethodCall::chainHead_unstable_follow { with_runtime } => {
let (tx, rx) = async_channel::bounded(16);
let subscription_id =
chain_head_subscriptions::spawn_chain_head_subscription_task(
Expand Down
37 changes: 37 additions & 0 deletions full-node/src/json_rpc_service/chain_head_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct Config {
}

pub enum Message {
Header {
request: service::RequestProcess,
},
Unpin {
block_hashes: Vec<[u8; 32]>,
outcome: oneshot::Sender<Result<(), ()>>,
Expand Down Expand Up @@ -161,6 +164,40 @@ pub async fn spawn_chain_head_subscription_task(mut config: Config) -> String {

match what_happened {
WhatHappened::ForegroundClosed => return,
WhatHappened::Foreground(Message::Header { request }) => {
let methods::MethodCall::chainHead_unstable_header { hash, .. } =
request.request()
else {
unreachable!()
};

if !pinned_blocks.contains(&hash.0) {
request.fail(service::ErrorResponse::InvalidParams);
continue;
}

let database_outcome = config
.database
.with_database(move |database| database.block_scale_encoded_header(&hash.0))
.await;

match database_outcome {
Ok(Some(header)) => {
request.respond(methods::Response::chainHead_unstable_header(Some(
methods::HexString(header),
)))
}
Ok(None) => {
// Should never happen given that blocks are pinned.
// TODO: log the problem
request.fail(service::ErrorResponse::InternalError);
}
Err(_) => {
// TODO: log the problem
request.fail(service::ErrorResponse::InternalError);
}
}
}
WhatHappened::Foreground(Message::Unpin {
block_hashes,
outcome,
Expand Down

0 comments on commit 16fe315

Please sign in to comment.