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 support for state_call JSON-RPC function #2374

Merged
merged 6 commits into from
Jun 14, 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
15 changes: 14 additions & 1 deletion bin/light-base/src/json_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,20 @@ impl<TPlat: Platform> Background<TPlat> {
self.rpc_methods(request_id, &state_machine_request_id)
.await;
}
methods::MethodCall::state_call {
name,
parameters,
hash,
} => {
self.state_call(
request_id,
&state_machine_request_id,
&name,
parameters,
hash,
)
.await;
}
methods::MethodCall::state_getKeysPaged {
prefix,
count,
Expand Down Expand Up @@ -1069,7 +1083,6 @@ impl<TPlat: Platform> Background<TPlat> {
| methods::MethodCall::grandpa_roundState { .. }
| methods::MethodCall::offchain_localStorageGet { .. }
| methods::MethodCall::offchain_localStorageSet { .. }
| methods::MethodCall::state_call { .. }
| methods::MethodCall::state_getKeys { .. }
| methods::MethodCall::state_getPairs { .. }
| methods::MethodCall::state_getReadProof { .. }
Expand Down
43 changes: 43 additions & 0 deletions bin/light-base/src/json_rpc_service/state_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,49 @@ impl<TPlat: Platform> Background<TPlat> {
.await;
}

/// Handles a call to [`methods::MethodCall::state_call`].
pub(super) async fn state_call(
self: &Arc<Self>,
request_id: &str,
state_machine_request_id: &requests_subscriptions::RequestId,
function_to_call: &str,
call_parameters: methods::HexString,
hash: Option<methods::HashHexString>,
) {
let block_hash = if let Some(hash) = hash {
hash.0
} else {
header::hash_from_scale_encoded_header(
&sub_utils::subscribe_best(&self.runtime_service).await.0,
)
};

let result = self
.runtime_call(
&block_hash,
function_to_call,
iter::once(call_parameters.0),
3,
Duration::from_secs(10),
NonZeroU32::new(3).unwrap(),
)
.await;

let response = match result {
Ok(data) => methods::Response::state_call(methods::HexString(data.to_vec()))
.to_json_response(request_id),
Err(error) => json_rpc::parse::build_error_response(
request_id,
json_rpc::parse::ErrorResponse::ServerError(-32000, &error.to_string()),
None,
),
};

self.requests_subscriptions
.respond(state_machine_request_id, response)
.await;
}

/// Handles a call to [`methods::MethodCall::state_getKeysPaged`].
pub(super) async fn state_get_keys_paged(
self: &Arc<Self>,
Expand Down
1 change: 1 addition & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Add support for the `state_call` JSON-RPC function. ([#2374](https://github.com/paritytech/smoldot/pull/2374))
- The `relay_chain` and `para_id` fields in chain specifications can now alternatively be named respectively `relayChain` and `paraId`. This increases consistency with the other fields of chain specifications, which are all camelCase. ([#2366](https://github.com/paritytech/smoldot/pull/2366))

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/json_rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ define_methods! {
payment_queryInfo(extrinsic: HexString, hash: Option<HashHexString>) -> RuntimeDispatchInfo,
/// Returns a list of all JSON-RPC methods that are available.
rpc_methods() -> RpcMethods,
state_call() -> () [state_callAt], // TODO:
state_call(name: Cow<'a, str>, parameters: HexString, hash: Option<HashHexString>) -> HexString [state_callAt],
state_getKeys() -> (), // TODO:
state_getKeysPaged(prefix: Option<HexString>, count: u32, start_key: Option<HexString>, hash: Option<HashHexString>) -> Vec<HexString> [state_getKeysPagedAt],
state_getMetadata(hash: Option<HashHexString>) -> HexString,
Expand Down