Skip to content

Commit

Permalink
More backports to the JSON-RPC API (#2818)
Browse files Browse the repository at this point in the history
In complement with #2812, this
PR updates `chainHead_unstable_storage` with the latest changes in the
JSON-RPC API:
paritytech/json-rpc-interface-spec#16 and
paritytech/json-rpc-interface-spec#17

We should now be up to date.
  • Loading branch information
tomaka authored Oct 4, 2022
1 parent e4fc5a2 commit c0b8cfa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
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
2 changes: 2 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- No longer silently discard justifications when receive a block from the network that was already known locally. ([#2800](https://github.com/paritytech/smoldot/pull/2800))
- CPU-heavy operations such as verifying finality proofs or compiling the runtime will now better respect the CPU rate limit. ([#2803](https://github.com/paritytech/smoldot/pull/2803))
- Fix the `finalizedBlockHashes` and `prunedBlockHashes` fields having wrong names in `chainHead_unstable_followEvent` events. ([#2812](https://github.com/paritytech/smoldot/pull/2812))
- 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

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

0 comments on commit c0b8cfa

Please sign in to comment.