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 a chainHead_unstable_finalizedDatabase JSON-RPC method #2749

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions bin/light-base/src/json_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,14 @@ impl<TPlat: Platform> Background<TPlat> {
)
.await;
}
methods::MethodCall::chainHead_unstable_finalizedDatabase { max_size_bytes } => {
self.chain_head_unstable_finalized_database(
request_id,
&state_machine_request_id,
max_size_bytes,
)
.await;
}
methods::MethodCall::chainSpec_unstable_chainName {} => {
self.chain_spec_unstable_chain_name(request_id, &state_machine_request_id)
.await;
Expand Down
25 changes: 25 additions & 0 deletions bin/light-base/src/json_rpc_service/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,31 @@ impl<TPlat: Platform> Background<TPlat> {
.await;
}
}

/// Handles a call to [`methods::MethodCall::chainHead_unstable_finalizedDatabase`].
pub(super) async fn chain_head_unstable_finalized_database(
self: &Arc<Self>,
request_id: &str,
state_machine_request_id: &requests_subscriptions::RequestId,
max_size_bytes: Option<u64>,
) {
// TODO: move the encode_database function where it makes more sense
let response = super::super::encode_database(
&self.network_service.0,
&self.sync_service,
self.sync_service.block_number_bytes(),
usize::try_from(max_size_bytes.unwrap_or(u64::max_value())).unwrap_or(usize::max_value()),
)
.await;

self.requests_subscriptions
.respond(
state_machine_request_id,
methods::Response::chainHead_unstable_finalizedDatabase(response.into())
.to_json_response(request_id),
)
.await;
}
}

fn convert_runtime_spec(
Expand Down
22 changes: 15 additions & 7 deletions bin/light-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ struct ChainServices<TPlat: platform::Platform> {
sync_service: Arc<sync_service::SyncService<TPlat>>,
runtime_service: Arc<runtime_service::RuntimeService<TPlat>>,
transactions_service: Arc<transactions_service::TransactionsService<TPlat>>,
// TODO: can be grabbed from the sync service instead
block_number_bytes: usize,
}

Expand Down Expand Up @@ -917,7 +918,13 @@ impl<TPlat: platform::Platform, TChain> Client<TPlat, TChain> {
// Wait for the chain to finish initializing before we can obtain the database.
(&mut services).await;
let services = Pin::new(&mut services).take_output().unwrap();
encode_database(&services, max_size).await
encode_database(
&services.network_service,
&services.sync_service,
services.block_number_bytes,
max_size,
)
.await
}
}
}
Expand Down Expand Up @@ -1085,14 +1092,16 @@ async fn start_services<TPlat: platform::Platform>(
}

async fn encode_database<TPlat: platform::Platform>(
services: &ChainServices<TPlat>,
network_service: &network_service::NetworkService<TPlat>,
sync_service: &sync_service::SyncService<TPlat>,
block_number_bytes: usize,
max_size: usize,
) -> String {
// Craft the structure containing all the data that we would like to include.
let mut database_draft = SerdeDatabase {
chain: match services.sync_service.serialize_chain_information().await {
chain: match sync_service.serialize_chain_information().await {
Some(ci) => {
let encoded = finalized_serialize::encode_chain(&ci, services.block_number_bytes);
let encoded = finalized_serialize::encode_chain(&ci, block_number_bytes);
serde_json::from_str(&encoded).unwrap()
}
None => {
Expand All @@ -1106,9 +1115,8 @@ async fn encode_database<TPlat: platform::Platform>(
};
}
},
nodes: services
.network_service
.discovered_nodes(0)
nodes: network_service
.discovered_nodes(0) // TODO: hacky chain_index
.await
.map(|(peer_id, addrs)| {
(
Expand Down
6 changes: 4 additions & 2 deletions src/json_rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,12 @@ define_methods! {
transaction_unstable_submitAndWatch(transaction: HexString) -> Cow<'a, str>,
transaction_unstable_unwatch(subscription: Cow<'a, str>) -> (),

// This function is a custom addition in smoldot. As of the writing of this comment, there is
// no plan to standardize it. See https://github.com/paritytech/smoldot/issues/2245.
// These functions are a custom addition in smoldot. As of the writing of this comment, there
// is no plan to standardize them. See <https://github.com/paritytech/smoldot/issues/2245> and
// <https://github.com/paritytech/smoldot/issues/2456>.
network_unstable_subscribeEvents() -> Cow<'a, str>,
network_unstable_unsubscribeEvents(subscription: Cow<'a, str>) -> (),
chainHead_unstable_finalizedDatabase(max_size_bytes: Option<u64>) -> Cow<'a, str>,
}

define_methods! {
Expand Down