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

No longer panic in the old subscription JSON-RPC functions if block can't be decoded #2442

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
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
119 changes: 73 additions & 46 deletions bin/light-base/src/json_rpc_service/state_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::runtime_service;
use futures::{lock::MutexGuard, prelude::*};
use smoldot::{
header,
informant::HashDisplay,
json_rpc::{self, methods, requests_subscriptions},
network::protocol,
remove_metadata_length_prefix,
Expand Down Expand Up @@ -439,24 +440,37 @@ impl<TPlat: Platform> Background<TPlat> {
))
.await;

let header = match methods::Header::from_scale_encoded_header(
&block.scale_encoded_header,
) {
Ok(h) => h,
Err(error) => {
log::warn!(
target: &me.log_target,
"`chain_subscribeAllHeads` subscription has skipped \
block due to undecodable header. Hash: {}. Error: {}",
HashDisplay(&header::hash_from_scale_encoded_header(&block.scale_encoded_header)),
error,
);
continue;
}
};

let _ = me
.requests_subscriptions
.try_push_notification(
&state_machine_subscription,
methods::ServerToClient::chain_newHead {
subscription: (&subscription_id).into(),
result: methods::Header::from_scale_encoded_header(
&block.scale_encoded_header,
)
.unwrap(),
result: header,
}
.to_json_call_object_parameters(None),
)
.await;
}
Some(runtime_service::Notification::Finalized { .. }) => {}
None => {
// TODO: ?!
// TODO: must recreate the channel
return;
}
}
Expand Down Expand Up @@ -537,28 +551,34 @@ impl<TPlat: Platform> Background<TPlat> {
let me = self.clone();
async move {
loop {
match blocks_list.next().await {
Some(block) => {
let header =
methods::Header::from_scale_encoded_header(&block).unwrap();

me.requests_subscriptions
.set_queued_notification(
&state_machine_subscription,
0,
methods::ServerToClient::chain_finalizedHead {
subscription: (&subscription_id).into(),
result: header,
}
.to_json_call_object_parameters(None),
)
.await;
}
None => {
// TODO: ?!
return;
// Stream returned by `subscribe_finalized` is always unlimited.
let header = blocks_list.next().await.unwrap();

let header = match methods::Header::from_scale_encoded_header(&header) {
Ok(h) => h,
Err(error) => {
log::warn!(
target: &me.log_target,
"`chain_subscribeFinalizedHeads` subscription has skipped block \
due to undecodable header. Hash: {}. Error: {}",
HashDisplay(&header::hash_from_scale_encoded_header(&header)),
error,
);
continue;
}
}
};

me.requests_subscriptions
.set_queued_notification(
&state_machine_subscription,
0,
methods::ServerToClient::chain_finalizedHead {
subscription: (&subscription_id).into(),
result: header,
}
.to_json_call_object_parameters(None),
)
.await;
}
}
};
Expand Down Expand Up @@ -636,27 +656,34 @@ impl<TPlat: Platform> Background<TPlat> {
let me = self.clone();
async move {
loop {
match blocks_list.next().await {
Some(block) => {
let header =
methods::Header::from_scale_encoded_header(&block).unwrap();
me.requests_subscriptions
.set_queued_notification(
&state_machine_subscription,
0,
methods::ServerToClient::chain_newHead {
subscription: (&subscription_id).into(),
result: header,
}
.to_json_call_object_parameters(None),
)
.await;
}
None => {
// TODO: ?!
return;
// Stream returned by `subscribe_best` is always unlimited.
let header = blocks_list.next().await.unwrap();

let header = match methods::Header::from_scale_encoded_header(&header) {
Ok(h) => h,
Err(error) => {
log::warn!(
target: &me.log_target,
"`chain_subscribeNewHeads` subscription has skipped block due to \
undecodable header. Hash: {}. Error: {}",
HashDisplay(&header::hash_from_scale_encoded_header(&header)),
error,
);
continue;
}
}
};

me.requests_subscriptions
.set_queued_notification(
&state_machine_subscription,
0,
methods::ServerToClient::chain_newHead {
subscription: (&subscription_id).into(),
result: header,
}
.to_json_call_object_parameters(None),
)
.await;
}
}
};
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 @@ -6,6 +6,10 @@

- Block headers with a digest item of type `Other` no longer fail to parse. ([#2425](https://github.com/paritytech/smoldot/pull/2425))

### Fixed

- The `chain_subscribeAllHeads`, `chain_subscribeNewHeads`, and `chain_subscribeFinalizedHeads` JSON-RPC functions no longer panic if connected to a chain whose headers are in a format that can't be decoded. Instead, no notification is sent and a warning is printed. ([#2442](https://github.com/paritytech/smoldot/pull/2442))

## 0.6.20 - 2022-06-23

### Changed
Expand Down