From 908ccab1b6293013869ffdfbf3d880588604905f Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 18 Oct 2022 14:31:55 +0200 Subject: [PATCH 1/4] Reduce parachain template complexity --- parachain-template/node/src/command.rs | 26 ++---- parachain-template/node/src/service.rs | 122 ++++++------------------- 2 files changed, 39 insertions(+), 109 deletions(-) diff --git a/parachain-template/node/src/command.rs b/parachain-template/node/src/command.rs index ee497b413f5..b7a942cec84 100644 --- a/parachain-template/node/src/command.rs +++ b/parachain-template/node/src/command.rs @@ -5,7 +5,7 @@ use cumulus_client_cli::generate_genesis_block; use cumulus_primitives_core::ParaId; use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE}; use log::{info, warn}; -use parachain_template_runtime::{Block, RuntimeApi}; +use parachain_template_runtime::Block; use sc_cli::{ ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams, Result, RuntimeVersion, SharedParams, SubstrateCli, @@ -20,7 +20,7 @@ use sp_runtime::traits::{AccountIdConversion, Block as BlockT}; use crate::{ chain_spec, cli::{Cli, RelayChainCli, Subcommand}, - service::{new_partial, TemplateRuntimeExecutor}, + service::{new_partial, ParachainNativeExecutor}, }; fn load_spec(id: &str) -> std::result::Result, String> { @@ -116,11 +116,7 @@ macro_rules! construct_async_run { (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{ let runner = $cli.create_runner($cmd)?; runner.async_run(|$config| { - let $components = new_partial::< - RuntimeApi, - TemplateRuntimeExecutor, - _ - >( + let $components = new_partial::<_>( &$config, crate::service::parachain_build_import_queue, )?; @@ -204,17 +200,15 @@ pub fn run() -> Result<()> { match cmd { BenchmarkCmd::Pallet(cmd) => if cfg!(feature = "runtime-benchmarks") { - runner.sync_run(|config| cmd.run::(config)) + runner.sync_run(|config| cmd.run::(config)) } else { Err("Benchmarking wasn't enabled when building the node. \ You can enable it with `--features runtime-benchmarks`." .into()) }, BenchmarkCmd::Block(cmd) => runner.sync_run(|config| { - let partials = new_partial::( - &config, - crate::service::parachain_build_import_queue, - )?; + let partials = + new_partial::<_>(&config, crate::service::parachain_build_import_queue)?; cmd.run(partials.client) }), #[cfg(not(feature = "runtime-benchmarks"))] @@ -227,10 +221,8 @@ pub fn run() -> Result<()> { .into()), #[cfg(feature = "runtime-benchmarks")] BenchmarkCmd::Storage(cmd) => runner.sync_run(|config| { - let partials = new_partial::( - &config, - crate::service::parachain_build_import_queue, - )?; + let partials = + new_partial::<_>(&config, crate::service::parachain_build_import_queue)?; let db = partials.backend.expose_db(); let storage = partials.backend.expose_storage(); @@ -255,7 +247,7 @@ pub fn run() -> Result<()> { .map_err(|e| format!("Error: {:?}", e))?; runner.async_run(|config| { - Ok((cmd.run::(config), task_manager)) + Ok((cmd.run::(config), task_manager)) }) } else { Err("Try-runtime must be enabled by `--features try-runtime`.".into()) diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 91d8d54244d..76077964984 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -8,9 +8,7 @@ use jsonrpsee::RpcModule; use cumulus_client_cli::CollatorOptions; // Local Runtime Types -use parachain_template_runtime::{ - opaque::Block, AccountId, Balance, Hash, Index as Nonce, RuntimeApi, -}; +use parachain_template_runtime::{opaque::Block, Hash, RuntimeApi}; // Cumulus Imports use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion}; @@ -30,17 +28,15 @@ use sc_network::NetworkService; use sc_network_common::service::NetworkBlock; use sc_service::{Configuration, PartialComponents, TFullBackend, TFullClient, TaskManager}; use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle}; -use sp_api::ConstructRuntimeApi; use sp_keystore::SyncCryptoStorePtr; -use sp_runtime::traits::BlakeTwo256; use substrate_prometheus_endpoint::Registry; use polkadot_service::CollatorPair; -/// Native executor instance. -pub struct TemplateRuntimeExecutor; +/// Native executor type. +pub struct ParachainNativeExecutor; -impl sc_executor::NativeExecutionDispatch for TemplateRuntimeExecutor { +impl sc_executor::NativeExecutionDispatch for ParachainNativeExecutor { type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions; fn dispatch(method: &str, data: &[u8]) -> Option> { @@ -52,56 +48,39 @@ impl sc_executor::NativeExecutionDispatch for TemplateRuntimeExecutor { } } +type ParachainExecutor = NativeElseWasmExecutor; + +type ParachainClient = TFullClient; + +type ParachainBackend = TFullBackend; + /// Starts a `ServiceBuilder` for a full service. /// /// Use this macro if you don't actually need the full service, but just the builder in order to /// be able to perform chain operations. #[allow(clippy::type_complexity)] -pub fn new_partial( +pub fn new_partial( config: &Configuration, build_import_queue: BIQ, ) -> Result< PartialComponents< - TFullClient>, - TFullBackend, + ParachainClient, + ParachainBackend, (), - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, - sc_transaction_pool::FullPool< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue, + sc_transaction_pool::FullPool, (Option, Option), >, sc_service::Error, > where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, - RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue - + sp_api::Metadata - + sp_session::SessionKeys - + sp_api::ApiExt< - Block, - StateBackend = sc_client_api::StateBackendFor, Block>, - > + sp_offchain::OffchainWorkerApi - + sp_block_builder::BlockBuilder, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, - Executor: sc_executor::NativeExecutionDispatch + 'static, BIQ: FnOnce( - Arc>>, + Arc, &Configuration, Option, &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue, sc_service::Error, >, { @@ -116,7 +95,7 @@ where }) .transpose()?; - let executor = sc_executor::NativeElseWasmExecutor::::new( + let executor = ParachainExecutor::new( config.wasm_method, config.default_heap_pages, config.max_runtime_instances, @@ -192,7 +171,7 @@ async fn build_relay_chain_interface( /// /// This is the actual implementation that is abstract over the executor and the runtime api. #[sc_tracing::logging::prefix_logs_with("Parachain")] -async fn start_node_impl( +async fn start_node_impl( parachain_config: Configuration, polkadot_config: Configuration, collator_options: CollatorOptions, @@ -201,57 +180,25 @@ async fn start_node_impl( build_import_queue: BIQ, build_consensus: BIC, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> +) -> sc_service::error::Result<(TaskManager, Arc)> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, - RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue - + sp_api::Metadata - + sp_session::SessionKeys - + sp_api::ApiExt< - Block, - StateBackend = sc_client_api::StateBackendFor, Block>, - > + sp_offchain::OffchainWorkerApi - + sp_block_builder::BlockBuilder - + cumulus_primitives_core::CollectCollationInfo - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + substrate_frame_rpc_system::AccountNonceApi, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, - Executor: sc_executor::NativeExecutionDispatch + 'static, - RB: Fn( - Arc>, - ) -> Result, sc_service::Error> - + Send - + 'static, + RB: Fn(Arc) -> Result, sc_service::Error> + Send + 'static, BIQ: FnOnce( - Arc>>, + Arc, &Configuration, Option, &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue, sc_service::Error, > + 'static, BIC: FnOnce( - Arc>>, + Arc, Option<&Registry>, Option, &TaskManager, Arc, - Arc< - sc_transaction_pool::FullPool< - Block, - TFullClient>, - >, - >, + Arc>, Arc>, SyncCryptoStorePtr, bool, @@ -259,7 +206,7 @@ where { let parachain_config = prepare_node_config(parachain_config); - let params = new_partial::(¶chain_config, build_import_queue)?; + let params = new_partial::(¶chain_config, build_import_queue)?; let (mut telemetry, telemetry_worker_handle) = params.other; let client = params.client.clone(); @@ -400,17 +347,11 @@ where /// Build the import queue for the parachain runtime. #[allow(clippy::type_complexity)] pub fn parachain_build_import_queue( - client: Arc>>, + client: Arc, config: &Configuration, telemetry: Option, task_manager: &TaskManager, -) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, - sc_service::Error, -> { +) -> Result, sc_service::Error> { let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; cumulus_client_consensus_aura::import_queue::< @@ -448,11 +389,8 @@ pub async fn start_parachain_node( collator_options: CollatorOptions, id: ParaId, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> { - start_node_impl::( +) -> sc_service::error::Result<(TaskManager, Arc)> { + start_node_impl::<_, _, _>( parachain_config, polkadot_config, collator_options, From c5c73bbf8b587cde46c9d8162e77dada398d234e Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 18 Oct 2022 19:03:24 +0200 Subject: [PATCH 2/4] Client and backend type alias in polkadot-parachains --- polkadot-parachain/src/service.rs | 224 +++++++++--------------------- 1 file changed, 64 insertions(+), 160 deletions(-) diff --git a/polkadot-parachain/src/service.rs b/polkadot-parachain/src/service.rs index ca47af08eec..2f38da0bd5d 100644 --- a/polkadot-parachain/src/service.rs +++ b/polkadot-parachain/src/service.rs @@ -69,6 +69,10 @@ type HostFunctions = sp_io::SubstrateHostFunctions; type HostFunctions = (sp_io::SubstrateHostFunctions, frame_benchmarking::benchmarking::HostFunctions); +type ParachainClient = TFullClient>; + +type ParachainBackend = TFullBackend; + /// Native executor instance. pub struct ShellRuntimeExecutor; @@ -153,45 +157,33 @@ pub fn new_partial( build_import_queue: BIQ, ) -> Result< PartialComponents< - TFullClient>, - TFullBackend, + ParachainClient, + ParachainBackend, (), - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, - sc_transaction_pool::FullPool< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, + sc_transaction_pool::FullPool>, (Option, Option), >, sc_service::Error, > where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, + sc_client_api::StateBackendFor: sp_api::StateBackend, BIQ: FnOnce( - Arc>>, + Arc>, &Configuration, Option, &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, sc_service::Error, >, { @@ -292,54 +284,38 @@ async fn start_shell_node_impl( build_import_queue: BIQ, build_consensus: BIC, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> +) -> sc_service::error::Result<(TaskManager, Arc>)> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, - RB: Fn( - Arc>>, - ) -> Result, sc_service::Error> + sc_client_api::StateBackendFor: sp_api::StateBackend, + RB: Fn(Arc>) -> Result, sc_service::Error> + Send + 'static, BIQ: FnOnce( - Arc>>, + Arc>, &Configuration, Option, &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, sc_service::Error, >, BIC: FnOnce( - Arc>>, + Arc>, Option<&Registry>, Option, &TaskManager, Arc, - Arc< - sc_transaction_pool::FullPool< - Block, - TFullClient>, - >, - >, + Arc>>, Arc>, SyncCryptoStorePtr, bool, @@ -488,56 +464,40 @@ async fn start_node_impl( build_import_queue: BIQ, build_consensus: BIC, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> +) -> sc_service::error::Result<(TaskManager, Arc>)> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + frame_rpc_system::AccountNonceApi, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, - RB: Fn( - Arc>>, - ) -> Result, sc_service::Error> + sc_client_api::StateBackendFor: sp_api::StateBackend, + RB: Fn(Arc>) -> Result, sc_service::Error> + Send + 'static, BIQ: FnOnce( - Arc>>, + Arc>, &Configuration, Option, &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, sc_service::Error, > + 'static, BIC: FnOnce( - Arc>>, + Arc>, Option<&Registry>, Option, &TaskManager, Arc, - Arc< - sc_transaction_pool::FullPool< - Block, - TFullClient>, - >, - >, + Arc>>, Arc>, SyncCryptoStorePtr, bool, @@ -686,17 +646,12 @@ where /// Build the import queue for the rococo parachain runtime. pub fn rococo_parachain_build_import_queue( - client: Arc< - TFullClient>, - >, + client: Arc>, config: &Configuration, telemetry: Option, task_manager: &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, sc_service::Error, > { let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; @@ -738,7 +693,7 @@ pub async fn start_rococo_parachain_node( hwbench: Option, ) -> sc_service::error::Result<( TaskManager, - Arc>>, + Arc>, )> { start_node_impl::( parachain_config, @@ -820,31 +775,22 @@ pub async fn start_rococo_parachain_node( /// Build the import queue for the shell runtime. pub fn shell_build_import_queue( - client: Arc>>, + client: Arc>, config: &Configuration, _: Option, task_manager: &TaskManager, -) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, - sc_service::Error, -> +) -> Result>, sc_service::Error> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, + sc_client_api::StateBackendFor: sp_api::StateBackend, { cumulus_client_consensus_relay_chain::import_queue( client.clone(), @@ -863,25 +809,19 @@ pub async fn start_shell_node( collator_options: CollatorOptions, id: ParaId, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> +) -> sc_service::error::Result<(TaskManager, Arc>)> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, + sc_client_api::StateBackendFor: sp_api::StateBackend, { start_shell_node_impl::( parachain_config, @@ -1048,32 +988,23 @@ where /// Build the import queue for Statemint and other Aura-based runtimes. pub fn aura_build_import_queue( - client: Arc>>, + client: Arc>, config: &Configuration, telemetry_handle: Option, task_manager: &TaskManager, -) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, - sc_service::Error, -> +) -> Result>, sc_service::Error> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + sp_consensus_aura::AuraApi::Pair as Pair>::Public>, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, + sc_client_api::StateBackendFor: sp_api::StateBackend, <::Pair as Pair>::Signature: TryFrom> + std::hash::Hash + sp_runtime::traits::Member + Codec, { @@ -1131,28 +1062,22 @@ pub async fn start_generic_aura_node( collator_options: CollatorOptions, id: ParaId, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> +) -> sc_service::error::Result<(TaskManager, Arc>)> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo + sp_consensus_aura::AuraApi::Pair as Pair>::Public> + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + frame_rpc_system::AccountNonceApi, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, + sc_client_api::StateBackendFor: sp_api::StateBackend, <::Pair as Pair>::Signature: TryFrom> + std::hash::Hash + sp_runtime::traits::Member + Codec, { @@ -1302,56 +1227,40 @@ async fn start_contracts_rococo_node_impl( build_import_queue: BIQ, build_consensus: BIC, hwbench: Option, -) -> sc_service::error::Result<( - TaskManager, - Arc>>, -)> +) -> sc_service::error::Result<(TaskManager, Arc>)> where - RuntimeApi: ConstructRuntimeApi>> - + Send - + Sync - + 'static, + RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static, RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_api::Metadata + sp_session::SessionKeys + sp_api::ApiExt< Block, - StateBackend = sc_client_api::StateBackendFor, Block>, + StateBackend = sc_client_api::StateBackendFor, > + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + frame_rpc_system::AccountNonceApi, - sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, - RB: Fn( - Arc>>, - ) -> Result, sc_service::Error> + sc_client_api::StateBackendFor: sp_api::StateBackend, + RB: Fn(Arc>) -> Result, sc_service::Error> + Send + 'static, BIQ: FnOnce( - Arc>>, + Arc>, &Configuration, Option, &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, sc_service::Error, > + 'static, BIC: FnOnce( - Arc>>, + Arc>, Option<&Registry>, Option, &TaskManager, Arc, - Arc< - sc_transaction_pool::FullPool< - Block, - TFullClient>, - >, - >, + Arc>>, Arc>, SyncCryptoStorePtr, bool, @@ -1500,17 +1409,12 @@ where #[allow(clippy::type_complexity)] pub fn contracts_rococo_build_import_queue( - client: Arc< - TFullClient>, - >, + client: Arc>, config: &Configuration, telemetry: Option, task_manager: &TaskManager, ) -> Result< - sc_consensus::DefaultImportQueue< - Block, - TFullClient>, - >, + sc_consensus::DefaultImportQueue>, sc_service::Error, > { let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; @@ -1552,7 +1456,7 @@ pub async fn start_contracts_rococo_node( hwbench: Option, ) -> sc_service::error::Result<( TaskManager, - Arc>>, + Arc>, )> { start_contracts_rococo_node_impl::( parachain_config, From 87ff3b88f6d50c38ef21968c5044eb6a1608801d Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Wed, 19 Oct 2022 08:41:26 +0200 Subject: [PATCH 3/4] Further simplification of template --- parachain-template/node/src/command.rs | 11 +- parachain-template/node/src/service.rs | 187 ++++++++++--------------- 2 files changed, 80 insertions(+), 118 deletions(-) diff --git a/parachain-template/node/src/command.rs b/parachain-template/node/src/command.rs index b7a942cec84..837cd15abf5 100644 --- a/parachain-template/node/src/command.rs +++ b/parachain-template/node/src/command.rs @@ -116,10 +116,7 @@ macro_rules! construct_async_run { (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{ let runner = $cli.create_runner($cmd)?; runner.async_run(|$config| { - let $components = new_partial::<_>( - &$config, - crate::service::parachain_build_import_queue, - )?; + let $components = new_partial(&$config)?; let task_manager = $components.task_manager; { $( $code )* }.map(|v| (v, task_manager)) }) @@ -207,8 +204,7 @@ pub fn run() -> Result<()> { .into()) }, BenchmarkCmd::Block(cmd) => runner.sync_run(|config| { - let partials = - new_partial::<_>(&config, crate::service::parachain_build_import_queue)?; + let partials = new_partial(&config)?; cmd.run(partials.client) }), #[cfg(not(feature = "runtime-benchmarks"))] @@ -221,8 +217,7 @@ pub fn run() -> Result<()> { .into()), #[cfg(feature = "runtime-benchmarks")] BenchmarkCmd::Storage(cmd) => runner.sync_run(|config| { - let partials = - new_partial::<_>(&config, crate::service::parachain_build_import_queue)?; + let partials = new_partial(&config)?; let db = partials.backend.expose_db(); let storage = partials.backend.expose_storage(); diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 76077964984..b0927cc426b 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -58,10 +58,8 @@ type ParachainBackend = TFullBackend; /// /// Use this macro if you don't actually need the full service, but just the builder in order to /// be able to perform chain operations. -#[allow(clippy::type_complexity)] -pub fn new_partial( +pub fn new_partial( config: &Configuration, - build_import_queue: BIQ, ) -> Result< PartialComponents< ParachainClient, @@ -72,18 +70,7 @@ pub fn new_partial( (Option, Option), >, sc_service::Error, -> -where - BIQ: FnOnce( - Arc, - &Configuration, - Option, - &TaskManager, - ) -> Result< - sc_consensus::DefaultImportQueue, - sc_service::Error, - >, -{ +> { let telemetry = config .telemetry_endpoints .clone() @@ -132,7 +119,7 @@ where &task_manager, )?; - let params = PartialComponents { + Ok(PartialComponents { backend, client, import_queue, @@ -141,9 +128,7 @@ where transaction_pool, select_chain: (), other: (telemetry, telemetry_worker_handle), - }; - - Ok(params) + }) } async fn build_relay_chain_interface( @@ -171,42 +156,20 @@ async fn build_relay_chain_interface( /// /// This is the actual implementation that is abstract over the executor and the runtime api. #[sc_tracing::logging::prefix_logs_with("Parachain")] -async fn start_node_impl( +async fn start_node_impl( parachain_config: Configuration, polkadot_config: Configuration, collator_options: CollatorOptions, id: ParaId, _rpc_ext_builder: RB, - build_import_queue: BIQ, - build_consensus: BIC, hwbench: Option, ) -> sc_service::error::Result<(TaskManager, Arc)> where RB: Fn(Arc) -> Result, sc_service::Error> + Send + 'static, - BIQ: FnOnce( - Arc, - &Configuration, - Option, - &TaskManager, - ) -> Result< - sc_consensus::DefaultImportQueue, - sc_service::Error, - > + 'static, - BIC: FnOnce( - Arc, - Option<&Registry>, - Option, - &TaskManager, - Arc, - Arc>, - Arc>, - SyncCryptoStorePtr, - bool, - ) -> Result>, sc_service::Error>, { let parachain_config = prepare_node_config(parachain_config); - let params = new_partial::(¶chain_config, build_import_queue)?; + let params = new_partial(¶chain_config)?; let (mut telemetry, telemetry_worker_handle) = params.other; let client = params.client.clone(); @@ -307,6 +270,7 @@ where network, params.keystore_container.sync_keystore(), force_authoring, + id, )?; let spawner = task_manager.spawn_handle(); @@ -345,8 +309,7 @@ where } /// Build the import queue for the parachain runtime. -#[allow(clippy::type_complexity)] -pub fn parachain_build_import_queue( +fn build_import_queue( client: Arc, config: &Configuration, telemetry: Option, @@ -382,6 +345,74 @@ pub fn parachain_build_import_queue( .map_err(Into::into) } +fn build_consensus( + client: Arc, + prometheus_registry: Option<&Registry>, + telemetry: Option, + task_manager: &TaskManager, + relay_chain_interface: Arc, + transaction_pool: Arc>, + sync_oracle: Arc>, + keystore: SyncCryptoStorePtr, + force_authoring: bool, + id: ParaId, +) -> Result>, sc_service::Error> { + let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; + + let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording( + task_manager.spawn_handle(), + client.clone(), + transaction_pool, + prometheus_registry, + telemetry.clone(), + ); + + let params = BuildAuraConsensusParams { + proposer_factory, + create_inherent_data_providers: move |_, (relay_parent, validation_data)| { + let relay_chain_interface = relay_chain_interface.clone(); + async move { + let parachain_inherent = + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( + relay_parent, + &relay_chain_interface, + &validation_data, + id, + ) + .await; + let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); + + let slot = + sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( + *timestamp, + slot_duration, + ); + + let parachain_inherent = parachain_inherent.ok_or_else(|| { + Box::::from( + "Failed to create parachain inherent", + ) + })?; + Ok((slot, timestamp, parachain_inherent)) + } + }, + block_import: client.clone(), + para_client: client, + backoff_authoring_blocks: Option::<()>::None, + sync_oracle, + keystore, + force_authoring, + slot_duration, + // We got around 500ms for proposing + block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), + // And a maximum of 750ms if slots are skipped + max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), + telemetry, + }; + + Ok(AuraConsensus::build::(params)) +} + /// Start a parachain node. pub async fn start_parachain_node( parachain_config: Configuration, @@ -390,76 +421,12 @@ pub async fn start_parachain_node( id: ParaId, hwbench: Option, ) -> sc_service::error::Result<(TaskManager, Arc)> { - start_node_impl::<_, _, _>( + start_node_impl( parachain_config, polkadot_config, collator_options, id, |_| Ok(RpcModule::new(())), - parachain_build_import_queue, - |client, - prometheus_registry, - telemetry, - task_manager, - relay_chain_interface, - transaction_pool, - sync_oracle, - keystore, - force_authoring| { - let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; - - let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording( - task_manager.spawn_handle(), - client.clone(), - transaction_pool, - prometheus_registry, - telemetry.clone(), - ); - - Ok(AuraConsensus::build::( - BuildAuraConsensusParams { - proposer_factory, - create_inherent_data_providers: move |_, (relay_parent, validation_data)| { - let relay_chain_interface = relay_chain_interface.clone(); - async move { - let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( - relay_parent, - &relay_chain_interface, - &validation_data, - id, - ).await; - let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); - - let slot = - sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, - slot_duration, - ); - - let parachain_inherent = parachain_inherent.ok_or_else(|| { - Box::::from( - "Failed to create parachain inherent", - ) - })?; - Ok((slot, timestamp, parachain_inherent)) - } - }, - block_import: client.clone(), - para_client: client, - backoff_authoring_blocks: Option::<()>::None, - sync_oracle, - keystore, - force_authoring, - slot_duration, - // We got around 500ms for proposing - block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), - // And a maximum of 750ms if slots are skipped - max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), - telemetry, - }, - )) - }, hwbench, ) .await From 1d39f8023f10309e452911b5788d94c325cc3cd3 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Wed, 19 Oct 2022 09:04:33 +0200 Subject: [PATCH 4/4] One last tweak --- parachain-template/node/src/service.rs | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index b0927cc426b..0980bb2cf78 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -3,9 +3,6 @@ // std use std::{sync::Arc, time::Duration}; -// rpc -use jsonrpsee::RpcModule; - use cumulus_client_cli::CollatorOptions; // Local Runtime Types use parachain_template_runtime::{opaque::Block, Hash, RuntimeApi}; @@ -156,17 +153,13 @@ async fn build_relay_chain_interface( /// /// This is the actual implementation that is abstract over the executor and the runtime api. #[sc_tracing::logging::prefix_logs_with("Parachain")] -async fn start_node_impl( +async fn start_node_impl( parachain_config: Configuration, polkadot_config: Configuration, collator_options: CollatorOptions, id: ParaId, - _rpc_ext_builder: RB, hwbench: Option, -) -> sc_service::error::Result<(TaskManager, Arc)> -where - RB: Fn(Arc) -> Result, sc_service::Error> + Send + 'static, -{ +) -> sc_service::error::Result<(TaskManager, Arc)> { let parachain_config = prepare_node_config(parachain_config); let params = new_partial(¶chain_config)?; @@ -421,13 +414,5 @@ pub async fn start_parachain_node( id: ParaId, hwbench: Option, ) -> sc_service::error::Result<(TaskManager, Arc)> { - start_node_impl( - parachain_config, - polkadot_config, - collator_options, - id, - |_| Ok(RpcModule::new(())), - hwbench, - ) - .await + start_node_impl(parachain_config, polkadot_config, collator_options, id, hwbench).await }