From 2675d44ba2c189a4157f5401cb6dd84cf2d28102 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 14:27:41 +0300 Subject: [PATCH 01/10] cumulus: Add chain spec to the `build_rpc_extensions` Signed-off-by: Alexandru Vasile --- cumulus/polkadot-parachain/src/rpc.rs | 3 +++ cumulus/polkadot-parachain/src/service.rs | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cumulus/polkadot-parachain/src/rpc.rs b/cumulus/polkadot-parachain/src/rpc.rs index 283a73d931d7..feb2ae912fe0 100644 --- a/cumulus/polkadot-parachain/src/rpc.rs +++ b/cumulus/polkadot-parachain/src/rpc.rs @@ -41,6 +41,7 @@ pub(crate) trait BuildRpcExtensions { client: Arc, backend: Arc, pool: Arc, + chain_spec: Box, ) -> sc_service::error::Result; } @@ -60,6 +61,7 @@ where _client: Arc>, _backend: Arc, _pool: Arc>>, + _chain_spec: Box, ) -> sc_service::error::Result { Ok(RpcExtension::new(())) } @@ -83,6 +85,7 @@ where client: Arc>, backend: Arc, pool: Arc>>, + chain_spec: Box, ) -> sc_service::error::Result { let build = || -> Result> { let mut module = RpcExtension::new(()); diff --git a/cumulus/polkadot-parachain/src/service.rs b/cumulus/polkadot-parachain/src/service.rs index 80698a2d7115..03aa5ce8401d 100644 --- a/cumulus/polkadot-parachain/src/service.rs +++ b/cumulus/polkadot-parachain/src/service.rs @@ -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( @@ -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, _| { Self::BuildRpcExtensions::build_rpc_extensions( deny_unsafe, client.clone(), backend_for_rpc.clone(), transaction_pool.clone(), + chain_spec, ) }) }; From cc900017986764854aa7dd30fd51285557d440e1 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 14:32:01 +0300 Subject: [PATCH 02/10] cumulus: Enable ChainSpec for polkadot-parachain Signed-off-by: Alexandru Vasile --- Cargo.lock | 1 + cumulus/polkadot-parachain/Cargo.toml | 1 + cumulus/polkadot-parachain/src/rpc.rs | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7466975fa428..2b5e9cbac4cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13900,6 +13900,7 @@ dependencies = [ "sc-network", "sc-network-sync", "sc-rpc", + "sc-rpc-spec-v2", "sc-service", "sc-sysinfo", "sc-telemetry", diff --git a/cumulus/polkadot-parachain/Cargo.toml b/cumulus/polkadot-parachain/Cargo.toml index b20d2a28fa7f..c32c258f61e3 100644 --- a/cumulus/polkadot-parachain/Cargo.toml +++ b/cumulus/polkadot-parachain/Cargo.toml @@ -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 } diff --git a/cumulus/polkadot-parachain/src/rpc.rs b/cumulus/polkadot-parachain/src/rpc.rs index feb2ae912fe0..681e407d30ab 100644 --- a/cumulus/polkadot-parachain/src/rpc.rs +++ b/cumulus/polkadot-parachain/src/rpc.rs @@ -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}; @@ -95,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) From 236ee3af57aa4896cbfd748c81009c99bb0cbeeb Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 14:43:14 +0300 Subject: [PATCH 03/10] Add PR doc Signed-off-by: Alexandru Vasile --- prdoc/pr_5205.prdoc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 prdoc/pr_5205.prdoc diff --git a/prdoc/pr_5205.prdoc b/prdoc/pr_5205.prdoc new file mode 100644 index 000000000000..7bb3519ec424 --- /dev/null +++ b/prdoc/pr_5205.prdoc @@ -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. + 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 From 24691015a3e1daf98a8e4c6fbe6c3c633e4d84aa Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 16:04:29 +0300 Subject: [PATCH 04/10] Revert "cumulus: Add chain spec to the `build_rpc_extensions`" This reverts commit 2675d44ba2c189a4157f5401cb6dd84cf2d28102. --- cumulus/polkadot-parachain/src/rpc.rs | 3 --- cumulus/polkadot-parachain/src/service.rs | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/cumulus/polkadot-parachain/src/rpc.rs b/cumulus/polkadot-parachain/src/rpc.rs index 681e407d30ab..463adcc48243 100644 --- a/cumulus/polkadot-parachain/src/rpc.rs +++ b/cumulus/polkadot-parachain/src/rpc.rs @@ -42,7 +42,6 @@ pub(crate) trait BuildRpcExtensions { client: Arc, backend: Arc, pool: Arc, - chain_spec: Box, ) -> sc_service::error::Result; } @@ -62,7 +61,6 @@ where _client: Arc>, _backend: Arc, _pool: Arc>>, - _chain_spec: Box, ) -> sc_service::error::Result { Ok(RpcExtension::new(())) } @@ -86,7 +84,6 @@ where client: Arc>, backend: Arc, pool: Arc>>, - chain_spec: Box, ) -> sc_service::error::Result { let build = || -> Result> { let mut module = RpcExtension::new(()); diff --git a/cumulus/polkadot-parachain/src/service.rs b/cumulus/polkadot-parachain/src/service.rs index 03aa5ce8401d..80698a2d7115 100644 --- a/cumulus/polkadot-parachain/src/service.rs +++ b/cumulus/polkadot-parachain/src/service.rs @@ -255,7 +255,6 @@ 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( @@ -282,13 +281,12 @@ pub(crate) trait NodeSpec { let transaction_pool = transaction_pool.clone(); let backend_for_rpc = backend.clone(); - Box::new(move |deny_unsafe: sc_rpc::DenyUnsafe, _| { + Box::new(move |deny_unsafe, _| { Self::BuildRpcExtensions::build_rpc_extensions( deny_unsafe, client.clone(), backend_for_rpc.clone(), transaction_pool.clone(), - chain_spec, ) }) }; From cf148337e496a6788db587c71a3c21e24d88e1eb Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 16:04:30 +0300 Subject: [PATCH 05/10] Revert "cumulus: Enable ChainSpec for polkadot-parachain" This reverts commit cc900017986764854aa7dd30fd51285557d440e1. Signed-off-by: Alexandru Vasile --- Cargo.lock | 1 - cumulus/polkadot-parachain/Cargo.toml | 1 - cumulus/polkadot-parachain/src/rpc.rs | 7 ------- 3 files changed, 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b5e9cbac4cf..7466975fa428 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13900,7 +13900,6 @@ dependencies = [ "sc-network", "sc-network-sync", "sc-rpc", - "sc-rpc-spec-v2", "sc-service", "sc-sysinfo", "sc-telemetry", diff --git a/cumulus/polkadot-parachain/Cargo.toml b/cumulus/polkadot-parachain/Cargo.toml index c32c258f61e3..b20d2a28fa7f 100644 --- a/cumulus/polkadot-parachain/Cargo.toml +++ b/cumulus/polkadot-parachain/Cargo.toml @@ -77,7 +77,6 @@ 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 } diff --git a/cumulus/polkadot-parachain/src/rpc.rs b/cumulus/polkadot-parachain/src/rpc.rs index 463adcc48243..283a73d931d7 100644 --- a/cumulus/polkadot-parachain/src/rpc.rs +++ b/cumulus/polkadot-parachain/src/rpc.rs @@ -28,7 +28,6 @@ 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}; @@ -93,12 +92,6 @@ 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) From 6c11c15c38a926a7d526bdff7a60d34b0fe91478 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 16:23:34 +0300 Subject: [PATCH 06/10] builder: Move init chainSpec to substrate builder Signed-off-by: Alexandru Vasile --- Cargo.lock | 1 - polkadot/rpc/src/lib.rs | 6 ------ substrate/bin/node/rpc/Cargo.toml | 1 - substrate/bin/node/rpc/src/lib.rs | 6 ------ substrate/client/service/src/builder.rs | 15 ++++++++++++--- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7466975fa428..d41b21df2912 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9278,7 +9278,6 @@ dependencies = [ "sc-mixnet", "sc-rpc", "sc-rpc-api", - "sc-rpc-spec-v2", "sc-sync-state-rpc", "sc-transaction-pool-api", "sp-api", diff --git a/polkadot/rpc/src/lib.rs b/polkadot/rpc/src/lib.rs index 7d678ada5ff5..eb0133b6e8fd 100644 --- a/polkadot/rpc/src/lib.rs +++ b/polkadot/rpc/src/lib.rs @@ -124,7 +124,6 @@ where use sc_consensus_babe_rpc::{Babe, BabeApiServer}; use sc_consensus_beefy_rpc::{Beefy, BeefyApiServer}; use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer}; - use sc_rpc_spec_v2::chain_spec::{ChainSpec, ChainSpecApiServer}; use sc_sync_state_rpc::{SyncState, SyncStateApiServer}; use substrate_frame_rpc_system::{System, SystemApiServer}; use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer}; @@ -139,11 +138,6 @@ where finality_provider, } = grandpa; - 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(); - - io.merge(ChainSpec::new(chain_name, genesis_hash, properties).into_rpc())?; io.merge(StateMigration::new(client.clone(), backend.clone(), deny_unsafe).into_rpc())?; io.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?; io.merge(TransactionPayment::new(client.clone()).into_rpc())?; diff --git a/substrate/bin/node/rpc/Cargo.toml b/substrate/bin/node/rpc/Cargo.toml index 2417496cde47..d85998e3c87b 100644 --- a/substrate/bin/node/rpc/Cargo.toml +++ b/substrate/bin/node/rpc/Cargo.toml @@ -32,7 +32,6 @@ sc-consensus-grandpa-rpc = { workspace = true, default-features = true } sc-mixnet = { workspace = true, default-features = true } sc-rpc = { workspace = true, default-features = true } sc-rpc-api = { workspace = true, default-features = true } -sc-rpc-spec-v2 = { workspace = true, default-features = true } sc-sync-state-rpc = { workspace = true, default-features = true } sc-transaction-pool-api = { workspace = true, default-features = true } sp-api = { workspace = true, default-features = true } diff --git a/substrate/bin/node/rpc/src/lib.rs b/substrate/bin/node/rpc/src/lib.rs index 52cd7f9561d2..c55e03ee9d6f 100644 --- a/substrate/bin/node/rpc/src/lib.rs +++ b/substrate/bin/node/rpc/src/lib.rs @@ -160,7 +160,6 @@ where mixnet::MixnetApiServer, statement::StatementApiServer, }; - use sc_rpc_spec_v2::chain_spec::{ChainSpec, ChainSpecApiServer}; use sc_sync_state_rpc::{SyncState, SyncStateApiServer}; use substrate_frame_rpc_system::{System, SystemApiServer}; use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer}; @@ -176,11 +175,6 @@ where finality_provider, } = grandpa; - let chain_name = chain_spec.name().to_string(); - let genesis_hash = client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"); - let properties = chain_spec.properties(); - io.merge(ChainSpec::new(chain_name, genesis_hash, properties).into_rpc())?; - io.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?; // Making synchronous calls in light client freezes the browser currently, // more context: https://github.com/paritytech/substrate/pull/3480 diff --git a/substrate/client/service/src/builder.rs b/substrate/client/service/src/builder.rs index 638f30fad10e..8d40294e43c9 100644 --- a/substrate/client/service/src/builder.rs +++ b/substrate/client/service/src/builder.rs @@ -68,6 +68,7 @@ use sc_rpc::{ use sc_rpc_spec_v2::{ archive::ArchiveApiServer, chain_head::ChainHeadApiServer, + chain_spec::ChainSpecApiServer, transaction::{TransactionApiServer, TransactionBroadcastApiServer}, }; use sc_telemetry::{telemetry, ConnectionMessage, Telemetry, TelemetryHandle, SUBSTRATE_INFO}; @@ -675,13 +676,12 @@ where // - block pruning in archive mode: The block's body is kept around let is_archive_node = config.state_pruning.as_ref().map(|sp| sp.is_archive()).unwrap_or(false) && config.blocks_pruning.is_archive(); + let genesis_hash = client.hash(Zero::zero()).ok().flatten().expect("Genesis block exists; qed"); if is_archive_node { - let genesis_hash = - client.hash(Zero::zero()).ok().flatten().expect("Genesis block exists; qed"); let archive_v2 = sc_rpc_spec_v2::archive::Archive::new( client.clone(), backend.clone(), - genesis_hash, + genesis_hash.clone(), // Defaults to sensible limits for the `Archive`. sc_rpc_spec_v2::archive::ArchiveConfig::default(), ) @@ -689,6 +689,14 @@ where rpc_api.merge(archive_v2).map_err(|e| Error::Application(e.into()))?; } + // ChainSpec RPC-v2. + let chain_spec_v2 = sc_rpc_spec_v2::chain_spec::ChainSpec::new( + config.chain_spec.name().into(), + genesis_hash, + config.chain_spec.properties(), + ) + .into_rpc(); + let author = sc_rpc::author::Author::new( client.clone(), transaction_pool, @@ -712,6 +720,7 @@ where .merge(transaction_broadcast_rpc_v2) .map_err(|e| Error::Application(e.into()))?; rpc_api.merge(chain_head_v2).map_err(|e| Error::Application(e.into()))?; + rpc_api.merge(chain_spec_v2).map_err(|e| Error::Application(e.into()))?; // Part of the old RPC spec. rpc_api.merge(chain).map_err(|e| Error::Application(e.into()))?; From f705a93770adbcbcd00b833f6a9aa3956a9c0089 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 16:34:07 +0300 Subject: [PATCH 07/10] Fix clippy Signed-off-by: Alexandru Vasile --- substrate/client/service/src/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/client/service/src/builder.rs b/substrate/client/service/src/builder.rs index 8d40294e43c9..ebe3f553f7c2 100644 --- a/substrate/client/service/src/builder.rs +++ b/substrate/client/service/src/builder.rs @@ -681,7 +681,7 @@ where let archive_v2 = sc_rpc_spec_v2::archive::Archive::new( client.clone(), backend.clone(), - genesis_hash.clone(), + genesis_hash, // Defaults to sensible limits for the `Archive`. sc_rpc_spec_v2::archive::ArchiveConfig::default(), ) From 86177bd56ca65046f19378440958476959a76294 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 16:48:24 +0300 Subject: [PATCH 08/10] Update prdoc Signed-off-by: Alexandru Vasile --- prdoc/pr_5205.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_5205.prdoc b/prdoc/pr_5205.prdoc index 7bb3519ec424..9cfaceafcb54 100644 --- a/prdoc/pr_5205.prdoc +++ b/prdoc/pr_5205.prdoc @@ -12,5 +12,5 @@ doc: 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 + - name: sc-service bump: minor From 70f03e6069ce2a22a54cf090bd16a418299e555e Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 17:03:44 +0300 Subject: [PATCH 09/10] Update prdoc Signed-off-by: Alexandru Vasile --- prdoc/pr_5205.prdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prdoc/pr_5205.prdoc b/prdoc/pr_5205.prdoc index 9cfaceafcb54..7e5609f284c4 100644 --- a/prdoc/pr_5205.prdoc +++ b/prdoc/pr_5205.prdoc @@ -5,8 +5,8 @@ doc: - Runtime Dev - Node Dev description: | - Enables the `chainSpec_v1` rpc-v2 API for polkadot-parachains. - This RPC is needed to provide extra information about the specification of the chain. + 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. 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. From 6a00f91536a7ff95c7cf16026c5e20bac4ce3eea Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 1 Aug 2024 17:04:39 +0300 Subject: [PATCH 10/10] prdoc: Patch polkadot-rpc Signed-off-by: Alexandru Vasile --- prdoc/pr_5205.prdoc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/prdoc/pr_5205.prdoc b/prdoc/pr_5205.prdoc index 7e5609f284c4..48abfe50ca24 100644 --- a/prdoc/pr_5205.prdoc +++ b/prdoc/pr_5205.prdoc @@ -13,4 +13,6 @@ doc: crates: - name: sc-service - bump: minor + bump: patch + - name: polkadot-rpc + bump: patch