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

More backports to the JSON-RPC API #2818

Merged
merged 4 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions bin/light-base/src/json_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,6 @@ impl<TPlat: Platform> Background<TPlat> {
hash,
key,
child_key,
r#type: ty,
network_config,
} => {
self.chain_head_storage(
Expand All @@ -1018,7 +1017,6 @@ impl<TPlat: Platform> Background<TPlat> {
hash,
key,
child_key,
ty,
network_config,
)
.await;
Expand Down
66 changes: 26 additions & 40 deletions bin/light-base/src/json_rpc_service/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ impl<TPlat: Platform> Background<TPlat> {
hash: methods::HashHexString,
key: methods::HexString,
child_key: Option<methods::HexString>,
ty: methods::StorageQueryType,
network_config: Option<methods::NetworkConfig>,
) {
let network_config = network_config.unwrap_or(methods::NetworkConfig {
Expand Down Expand Up @@ -892,19 +891,13 @@ impl<TPlat: Platform> Background<TPlat> {
return;
}

// Determine whether the requested block hash is valid, and if so its state trie root
// and number.
let block_storage_root_number = {
// Obtain the header of the requested block.
// Contains `None` if the subscription is disjoint.
let block_scale_encoded_header = {
let lock = self.subscriptions.lock().await;
if let Some(subscription) = lock.chain_head_follow.get(follow_subscription) {
if let Some(header) = subscription.pinned_blocks_headers.get(&hash.0) {
if let Ok(decoded) =
header::decode(&header, self.sync_service.block_number_bytes())
{
Some((*decoded.state_root, decoded.number))
} else {
None // TODO: what to return?!
}
Some(header.clone())
} else {
self.requests_subscriptions
.respond(
Expand Down Expand Up @@ -973,15 +966,18 @@ impl<TPlat: Platform> Background<TPlat> {
let task = {
let me = self.clone();
async move {
let response =
if let Some((block_storage_root, block_number)) = block_storage_root_number {
let response = match block_scale_encoded_header
.as_ref()
.map(|h| header::decode(&h, me.sync_service.block_number_bytes()))
{
Some(Ok(decoded_header)) => {
let response = me
.sync_service
.clone()
.storage_query(
block_number,
decoded_header.number,
&hash.0,
&block_storage_root,
&decoded_header.state_root,
iter::once(&key.0),
cmp::min(10, network_config.total_attempts),
Duration::from_millis(u64::from(cmp::min(
Expand All @@ -998,24 +994,7 @@ impl<TPlat: Platform> Background<TPlat> {
// and as such the outcome only ever contains one element.
debug_assert_eq!(values.len(), 1);
let value = values.into_iter().next().unwrap();

let output = match ty {
methods::StorageQueryType::Value => {
value.map(|v| methods::HexString(v).to_string())
}
methods::StorageQueryType::Size => {
value.map(|v| v.len().to_string())
}
methods::StorageQueryType::Hash => value.map(|v| {
methods::HexString(
blake2_rfc::blake2b::blake2b(32, &[], &v)
.as_bytes()
.to_vec(),
)
.to_string()
}),
};

let output = value.map(|v| methods::HexString(v).to_string());
methods::ServerToClient::chainHead_unstable_storageEvent {
subscription: (&subscription_id).into(),
result: methods::ChainHeadStorageEvent::Done { value: output },
Expand All @@ -1028,13 +1007,20 @@ impl<TPlat: Platform> Background<TPlat> {
}
.to_json_call_object_parameters(None),
}
} else {
methods::ServerToClient::chainHead_unstable_storageEvent {
subscription: (&subscription_id).into(),
result: methods::ChainHeadStorageEvent::Disjoint {},
}
.to_json_call_object_parameters(None)
};
}
Some(Err(err)) => methods::ServerToClient::chainHead_unstable_storageEvent {
subscription: (&subscription_id).into(),
result: methods::ChainHeadStorageEvent::Error {
error: err.to_string().into(),
},
}
.to_json_call_object_parameters(None),
None => methods::ServerToClient::chainHead_unstable_storageEvent {
subscription: (&subscription_id).into(),
result: methods::ChainHeadStorageEvent::Disjoint {},
}
.to_json_call_object_parameters(None),
};

me.requests_subscriptions
.set_queued_notification(&state_machine_subscription, 0, response)
Expand Down
5 changes: 5 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Fixed

- Remove "type" parameter from `chainHead_unstable_storage` JSON-RPC method, in accordance with the update in the JSON-RPC specification. ([#2818](https://github.com/paritytech/smoldot/pull/2818))
- The `chainHead_unstable_storage` JSON-RPC method now returns an `error` notification if the block's header couldn't be decoded instead of a `disjoint` notification. ([#2818](https://github.com/paritytech/smoldot/pull/2818))

## 0.7.0 - 2022-09-28

### Removed
Expand Down
17 changes: 4 additions & 13 deletions src/json_rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ define_methods! {
hash: HashHexString,
key: HexString,
#[rename = "childKey"] child_key: Option<HexString>,
r#type: StorageQueryType,
#[rename = "networkConfig"] network_config: Option<NetworkConfig>
) -> Cow<'a, str>,
chainHead_unstable_unfollow(
Expand Down Expand Up @@ -477,7 +476,7 @@ define_methods! {
chainHead_unstable_bodyEvent(subscription: Cow<'a, str>, result: ChainHeadBodyEvent) -> (),
chainHead_unstable_callEvent(subscription: Cow<'a, str>, result: ChainHeadCallEvent<'a>) -> (),
chainHead_unstable_followEvent(subscription: Cow<'a, str>, result: FollowEvent<'a>) -> (),
chainHead_unstable_storageEvent(subscription: Cow<'a, str>, result: ChainHeadStorageEvent) -> (),
chainHead_unstable_storageEvent(subscription: Cow<'a, str>, result: ChainHeadStorageEvent<'a>) -> (),
transaction_unstable_watchEvent(subscription: Cow<'a, str>, result: TransactionWatchEvent<'a>) -> (),

// This function is a custom addition in smoldot. As of the writing of this comment, there is
Expand Down Expand Up @@ -664,11 +663,13 @@ pub enum ChainHeadCallEvent<'a> {

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "event")]
pub enum ChainHeadStorageEvent {
pub enum ChainHeadStorageEvent<'a> {
#[serde(rename = "done")]
Done { value: Option<String> },
#[serde(rename = "inaccessible")]
Inaccessible {},
#[serde(rename = "error")]
Error { error: Cow<'a, str> },
#[serde(rename = "disjoint")]
Disjoint {},
}
Expand Down Expand Up @@ -970,16 +971,6 @@ pub struct StorageChangeSet {
pub changes: Vec<(HexString, Option<HexString>)>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum StorageQueryType {
#[serde(rename = "value")]
Value,
#[serde(rename = "hash")]
Hash,
#[serde(rename = "size")]
Size,
}

#[derive(Debug, Clone)]
pub struct SystemHealth {
pub is_syncing: bool,
Expand Down