From 490874ac7f189cb03a3d884e231a24957649e9ba Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Mon, 20 Sep 2021 15:54:53 +0200 Subject: [PATCH] split Basics --- node/service/src/lib.rs | 201 +++++++++++++++++++++++++--------------- 1 file changed, 128 insertions(+), 73 deletions(-) diff --git a/node/service/src/lib.rs b/node/service/src/lib.rs index 90d14ef7babe..d2e28f1a0da5 100644 --- a/node/service/src/lib.rs +++ b/node/service/src/lib.rs @@ -68,6 +68,8 @@ use polkadot_subsystem::jaeger; use std::{sync::Arc, time::Duration}; use prometheus_endpoint::Registry; +#[cfg(feature = "full-node")] +use service::KeystoreContainer; use service::RpcHandlers; use telemetry::TelemetryWorker; #[cfg(feature = "full-node")] @@ -304,12 +306,13 @@ fn jaeger_launch_collector_with_agent( #[cfg(feature = "full-node")] type FullSelectChain = relay_chain_selection::SelectRelayChain; #[cfg(feature = "full-node")] -type FullGrandpaBlockImport = grandpa::GrandpaBlockImport< - FullBackend, - Block, - FullClient, - FullSelectChain, ->; +type FullGrandpaBlockImport = + grandpa::GrandpaBlockImport< + FullBackend, + Block, + FullClient, + ChainSelection, + >; #[cfg(feature = "light-node")] type LightBackend = service::TLightBackendWithHash; @@ -319,36 +322,29 @@ type LightClient = service::TLightClientWithBackend; #[cfg(feature = "full-node")] -fn new_partial( +struct Basics +where + RuntimeApi: ConstructRuntimeApi> + + Send + + Sync + + 'static, + RuntimeApi::RuntimeApi: + RuntimeApiCollection>, + ExecutorDispatch: NativeExecutionDispatch + 'static, +{ + task_manager: TaskManager, + client: Arc>, + backend: Arc, + keystore_container: KeystoreContainer, + telemetry: Option, +} + +#[cfg(feature = "full-node")] +fn new_partial_basics( config: &mut Configuration, jaeger_agent: Option, telemetry_worker_handle: Option, -) -> Result< - service::PartialComponents< - FullClient, - FullBackend, - FullSelectChain, - sc_consensus::DefaultImportQueue>, - sc_transaction_pool::FullPool>, - ( - impl service::RpcExtensionBuilder, - ( - babe::BabeBlockImport< - Block, - FullClient, - FullGrandpaBlockImport, - >, - grandpa::LinkHalf, FullSelectChain>, - babe::BabeLink, - beefy_gadget::notification::BeefySignedCommitmentSender, - ), - grandpa::SharedVoterState, - std::time::Duration, // slot-duration - Option, - ), - >, - Error, -> +) -> Result, Error> where RuntimeApi: ConstructRuntimeApi> + Send @@ -391,6 +387,7 @@ where )?; let client = Arc::new(client); + let telemetry = telemetry.map(|(worker, telemetry)| { if let Some(worker) = worker { task_manager.spawn_handle().spawn("telemetry", worker.run()); @@ -400,12 +397,53 @@ where jaeger_launch_collector_with_agent(task_manager.spawn_handle(), &*config, jaeger_agent)?; - let select_chain = relay_chain_selection::SelectRelayChain::new( - backend.clone(), - Handle::new_disconnected(), - polkadot_node_subsystem_util::metrics::Metrics::register(config.prometheus_registry())?, - ); + Ok(Basics { task_manager, client, backend, keystore_container, telemetry }) +} +#[cfg(feature = "full-node")] +fn new_partial( + config: &mut Configuration, + Basics { task_manager, backend, client, keystore_container, telemetry }: Basics< + RuntimeApi, + ExecutorDispatch, + >, + select_chain: ChainSelection, +) -> Result< + service::PartialComponents< + FullClient, + FullBackend, + ChainSelection, + sc_consensus::DefaultImportQueue>, + sc_transaction_pool::FullPool>, + ( + impl service::RpcExtensionBuilder, + ( + babe::BabeBlockImport< + Block, + FullClient, + FullGrandpaBlockImport, + >, + grandpa::LinkHalf, ChainSelection>, + babe::BabeLink, + beefy_gadget::notification::BeefySignedCommitmentSender, + ), + grandpa::SharedVoterState, + std::time::Duration, // slot-duration + Option, + ), + >, + Error, +> +where + RuntimeApi: ConstructRuntimeApi> + + Send + + Sync + + 'static, + RuntimeApi::RuntimeApi: + RuntimeApiCollection>, + ExecutorDispatch: NativeExecutionDispatch + 'static, + ChainSelection: 'static + SelectChain, +{ let transaction_pool = sc_transaction_pool::BasicPool::new_full( config.transaction_pool.clone(), config.role.is_authority().into(), @@ -674,7 +712,23 @@ where let disable_grandpa = config.disable_grandpa; let name = config.network.node_name.clone(); - let service::PartialComponents { + + let basics = new_partial_basics::( + &mut config, + jaeger_agent, + telemetry_worker_handle, + )?; + + let prometheus_registry = config.prometheus_registry().cloned(); + + use relay_chain_selection::SelectRelayChain; + + let select_chain = SelectRelayChain::new( + basics.backend.clone(), + Handle::new_disconnected(), + polkadot_node_subsystem_util::metrics::Metrics::register(prometheus_registry.as_ref())?, + ); + let service::PartialComponents::<_, _, SelectRelayChain<_>, _, _, _> { client, backend, mut task_manager, @@ -683,14 +737,12 @@ where import_queue, transaction_pool, other: (rpc_extensions_builder, import_setup, rpc_setup, slot_duration, mut telemetry), - } = new_partial::( + } = new_partial::>( &mut config, - jaeger_agent, - telemetry_worker_handle, + basics, + select_chain, )?; - let prometheus_registry = config.prometheus_registry().cloned(); - let shared_voter_state = rpc_setup; let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht; @@ -876,8 +928,8 @@ where }, )?; let handle = Handle::Connected(overseer_handle.clone()); - let handle_clone = handle.clone(); + let handle_clone = handle.clone(); task_manager.spawn_essential_handle().spawn_blocking( "overseer", Box::pin(async move { @@ -1228,6 +1280,31 @@ where Ok((task_manager, rpc_handlers)) } +macro_rules! chain_ops { + ($config:expr, $jaeger_agent:expr, $telemetry_worker_handle:expr; $scope:ident, $executor:ident, $variant:ident) => {{ + let telemetry_worker_handle = $telemetry_worker_handle; + let jaeger_agent = $jaeger_agent; + let mut config = $config; + let basics = new_partial_basics::<$scope::RuntimeApi, $executor>( + config, + jaeger_agent, + telemetry_worker_handle, + )?; + + use ::sc_consensus::LongestChain; + // use the longest chain selection, since there is no overseer available + let chain_selection = LongestChain::new(basics.backend.clone()); + + let service::PartialComponents { client, backend, import_queue, task_manager, .. } = + new_partial::<$scope::RuntimeApi, $executor, LongestChain<_, Block>>( + &mut config, + basics, + chain_selection, + )?; + Ok((Arc::new(Client::$variant(client)), backend, import_queue, task_manager)) + }}; +} + /// Builds a new object suitable for chain operations. #[cfg(feature = "full-node")] pub fn new_chain_ops( @@ -1244,48 +1321,26 @@ pub fn new_chain_ops( > { config.keystore = service::config::KeystoreConfig::InMemory; + let telemetry_worker_handle = None; + #[cfg(feature = "rococo-native")] if config.chain_spec.is_rococo() || config.chain_spec.is_wococo() { - let service::PartialComponents { client, backend, import_queue, task_manager, .. } = - new_partial::( - config, - jaeger_agent, - None, - )?; - return Ok((Arc::new(Client::Rococo(client)), backend, import_queue, task_manager)) + return chain_ops!(config, jaeger_agent, telemetry_worker_handle; rococo_runtime, RococoExecutorDispatch, Rococo) } #[cfg(feature = "kusama-native")] if config.chain_spec.is_kusama() { - let service::PartialComponents { client, backend, import_queue, task_manager, .. } = - new_partial::( - config, - jaeger_agent, - None, - )?; - return Ok((Arc::new(Client::Kusama(client)), backend, import_queue, task_manager)) + return chain_ops!(config, jaeger_agent, telemetry_worker_handle; kusama_runtime, KusamaExecutorDispatch, Kusama) } #[cfg(feature = "westend-native")] if config.chain_spec.is_westend() { - let service::PartialComponents { client, backend, import_queue, task_manager, .. } = - new_partial::( - config, - jaeger_agent, - None, - )?; - return Ok((Arc::new(Client::Westend(client)), backend, import_queue, task_manager)) + return chain_ops!(config, jaeger_agent, telemetry_worker_handle; westend_runtime, WestendExecutorDispatch, Westend) } #[cfg(feature = "polkadot-native")] { - let service::PartialComponents { client, backend, import_queue, task_manager, .. } = - new_partial::( - config, - jaeger_agent, - None, - )?; - return Ok((Arc::new(Client::Polkadot(client)), backend, import_queue, task_manager)) + return chain_ops!(config, jaeger_agent, telemetry_worker_handle; polkadot_runtime, PolkadotExecutorDispatch, Polkadot) } #[cfg(not(feature = "polkadot-native"))]