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

rpc: Enable ChainSpec for polkadot-parachain #5205

Merged
merged 11 commits into from
Aug 2, 2024
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cumulus/polkadot-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ sp-block-builder = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
sc-chain-spec = { workspace = true, default-features = true }
sc-rpc = { workspace = true, default-features = true }
sc-rpc-spec-v2 = { workspace = true, default-features = true }
sp-version = { workspace = true, default-features = true }
sc-tracing = { workspace = true, default-features = true }
sp-offchain = { workspace = true, default-features = true }
Expand Down
10 changes: 10 additions & 0 deletions cumulus/polkadot-parachain/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use sc_rpc::{
dev::{Dev, DevApiServer},
DenyUnsafe,
};
use sc_rpc_spec_v2::chain_spec::{ChainSpec, ChainSpecApiServer};
use std::{marker::PhantomData, sync::Arc};
use substrate_frame_rpc_system::{System, SystemApiServer};
use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};
Expand All @@ -41,6 +42,7 @@ pub(crate) trait BuildRpcExtensions<Client, Backend, Pool> {
client: Arc<Client>,
backend: Arc<Backend>,
pool: Arc<Pool>,
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
) -> sc_service::error::Result<RpcExtension>;
}

Expand All @@ -60,6 +62,7 @@ where
_client: Arc<ParachainClient<RuntimeApi>>,
_backend: Arc<ParachainBackend>,
_pool: Arc<sc_transaction_pool::FullPool<Block, ParachainClient<RuntimeApi>>>,
_chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
) -> sc_service::error::Result<RpcExtension> {
Ok(RpcExtension::new(()))
}
Expand All @@ -83,6 +86,7 @@ where
client: Arc<ParachainClient<RuntimeApi>>,
backend: Arc<ParachainBackend>,
pool: Arc<sc_transaction_pool::FullPool<Block, ParachainClient<RuntimeApi>>>,
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
) -> sc_service::error::Result<RpcExtension> {
let build = || -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>> {
let mut module = RpcExtension::new(());
Expand All @@ -92,6 +96,12 @@ where
module.merge(StateMigration::new(client.clone(), backend, deny_unsafe).into_rpc())?;
module.merge(Dev::new(client, deny_unsafe).into_rpc())?;

// RPC spec v2 method.
let chain_name = chain_spec.name().to_string();
let genesis_hash = client.hash(0).ok().flatten().expect("Genesis block exists; qed");
let properties = chain_spec.properties();
module.merge(ChainSpec::new(chain_name, genesis_hash, properties).into_rpc())?;

Ok(module)
};
build().map_err(Into::into)
Expand Down
4 changes: 3 additions & 1 deletion cumulus/polkadot-parachain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pub(crate) trait NodeSpec {

let validator = parachain_config.role.is_authority();
let prometheus_registry = parachain_config.prometheus_registry().cloned();
let chain_spec = parachain_config.chain_spec.cloned();
let transaction_pool = params.transaction_pool.clone();
let import_queue_service = params.import_queue.service();
let net_config = FullNetworkConfiguration::<_, _, Net>::new(
Expand All @@ -281,12 +282,13 @@ pub(crate) trait NodeSpec {
let transaction_pool = transaction_pool.clone();
let backend_for_rpc = backend.clone();

Box::new(move |deny_unsafe, _| {
Box::new(move |deny_unsafe: sc_rpc::DenyUnsafe, _| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the type needed now in contrast to before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've hit the autocomplete by mistake :D No longer needed since I was able to obtain the same by moving the initialization to the substrate builder

Self::BuildRpcExtensions::build_rpc_extensions(
deny_unsafe,
client.clone(),
backend_for_rpc.clone(),
transaction_pool.clone(),
chain_spec,
)
})
};
Expand Down
16 changes: 16 additions & 0 deletions prdoc/pr_5205.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
title: Enable ChainSpec API for polkadot-parachain

doc:
- audience:
- Runtime Dev
- Node Dev
description: |
Enables the `chainSpec_v1` rpc-v2 API for polkadot-parachains.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to re-phrase this as:

The substrate-service-builder now includes the entire rpc v2 API. The chainspec API was previously defined as rpc extension where for instance chains would need to enable it explicitly for it to be enabled which isn't the case anymore

This RPC is needed to provide extra information about the specification of the chain.
At the same time, this paves the way for implementing in the future a `chainSpec_v1_getSpec`
method that can extract the chainSpec of any chain (including parachains) for the use with lightclients.
For more info about the `chainSpec`, please see the specification: https://github.com/paritytech/json-rpc-interface-spec/blob/main/src/api/chainSpec.md.

crates:
- name: polkadot-parachain-bin
bump: minor
Loading