git.delta.rocks / unique-network / refs/commits / 8cc39ba6d3e1

difftreelog

Implement autoseal in dev mode

Daniel Shiposha2022-03-15parent: #a78501c.patch.diff
in: master

4 files changed

modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -168,6 +168,9 @@
 [dependencies.serde_json]
 version = '1.0.68'
 
+[dependencies.sc-consensus-manual-seal]
+git = 'https://github.com/paritytech/substrate.git'
+branch = 'polkadot-v0.9.17'
 
 ################################################################################
 # Cumulus dependencies
modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
--- a/node/cli/src/chain_spec.rs
+++ b/node/cli/src/chain_spec.rs
@@ -68,6 +68,25 @@
 	}
 }
 
+pub enum ServiceId {
+	Prod,
+	Dev
+}
+
+pub trait ServiceIdentification {
+	fn service_id(&self) -> ServiceId;
+}
+
+impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {
+	fn service_id(&self) -> ServiceId {
+		if self.id().ends_with("dev") {
+			ServiceId::Dev
+		} else {
+			ServiceId::Prod
+		}
+	}
+}
+
 /// Helper function to generate a crypto pair from seed
 pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
 	TPublic::Pair::from_string(&format!("//{}", seed), None)
modifiednode/cli/src/command.rsdiffbeforeafterboth
--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -33,9 +33,9 @@
 // limitations under the License.
 
 use crate::{
-	chain_spec::{self, RuntimeId, RuntimeIdentification},
+	chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},
 	cli::{Cli, RelayChainCli, Subcommand},
-	service::new_partial,
+	service::{new_partial, start_node, start_dev_node},
 };
 
 #[cfg(feature = "unique-runtime")]
@@ -210,6 +210,7 @@
 			>(
 				&$config,
 				crate::service::parachain_build_import_queue,
+				ServiceId::Prod,
 			)?;
 			let task_manager = $components.task_manager;
 
@@ -245,6 +246,34 @@
 	}}
 }
 
+macro_rules! start_node_using_chain_runtime {
+	($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => {
+		match $config.chain_spec.runtime_id() {
+			#[cfg(feature = "unique-runtime")]
+			RuntimeId::Unique => $start_node_fn::<
+				unique_runtime::Runtime,
+				unique_runtime::RuntimeApi,
+				UniqueRuntimeExecutor,
+			>($config $(, $($args),+)?) $($code)*,
+
+			#[cfg(feature = "quartz-runtime")]
+			RuntimeId::Quartz => $start_node_fn::<
+				quartz_runtime::Runtime,
+				quartz_runtime::RuntimeApi,
+				QuartzRuntimeExecutor,
+			>($config $(, $($args),+)?) $($code)*,
+
+			RuntimeId::Opal => $start_node_fn::<
+				opal_runtime::Runtime,
+				opal_runtime::RuntimeApi,
+				OpalRuntimeExecutor,
+			>($config $(, $($args),+)?) $($code)*,
+
+			RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),
+		}
+	};
+}
+
 /// Parse command line arguments into service configuration.
 pub fn run() -> Result<()> {
 	let cli = Cli::from_args();
@@ -365,7 +394,20 @@
 			let runner = cli.create_runner(&cli.run.normalize())?;
 
 			runner.run_node_until_exit(|config| async move {
-				let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)
+				let extensions = chain_spec::Extensions::try_get(&*config.chain_spec);
+
+				let service_id = config.chain_spec.service_id();
+				let relay_chain_id = extensions.map(|e| e.relay_chain.clone());
+				let is_dev_service = matches![service_id, ServiceId::Dev]
+									|| relay_chain_id == Some("dev-service".into());
+
+				if is_dev_service {
+					return start_node_using_chain_runtime! {
+						start_dev_node(config).map_err(Into::into)
+					};
+				};
+
+				let para_id = extensions
 					.map(|e| e.para_id)
 					.ok_or("Could not find parachain ID in chain-spec.")?;
 
@@ -376,10 +418,10 @@
 						.chain(cli.relaychain_args.iter()),
 				);
 
-				let id = ParaId::from(para_id);
+				let para_id = ParaId::from(para_id);
 
 				let parachain_account =
-					AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);
+					AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&para_id);
 
 				let state_version =
 					RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
@@ -395,7 +437,7 @@
 				)
 				.map_err(|err| format!("Relay chain argument error: {}", err))?;
 
-				info!("Parachain id: {:?}", id);
+				info!("Parachain id: {:?}", para_id);
 				info!("Parachain Account: {}", parachain_account);
 				info!("Parachain genesis state: {}", genesis_state);
 				info!("Parachain genesis hash: {}", genesis_hash);
@@ -408,37 +450,11 @@
 					}
 				);
 
-				match config.chain_spec.runtime_id() {
-					#[cfg(feature = "unique-runtime")]
-					RuntimeId::Unique => crate::service::start_node::<
-						unique_runtime::Runtime,
-						unique_runtime::RuntimeApi,
-						UniqueRuntimeExecutor,
-					>(config, polkadot_config, id)
-					.await
-					.map(|r| r.0)
-					.map_err(Into::into),
-
-					#[cfg(feature = "quartz-runtime")]
-					RuntimeId::Quartz => crate::service::start_node::<
-						quartz_runtime::Runtime,
-						quartz_runtime::RuntimeApi,
-						QuartzRuntimeExecutor,
-					>(config, polkadot_config, id)
-					.await
-					.map(|r| r.0)
-					.map_err(Into::into),
-
-					RuntimeId::Opal => crate::service::start_node::<
-						opal_runtime::Runtime,
-						opal_runtime::RuntimeApi,
-						OpalRuntimeExecutor,
-					>(config, polkadot_config, id)
-					.await
-					.map(|r| r.0)
-					.map_err(Into::into),
-
-					RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),
+				start_node_using_chain_runtime! {
+					start_node(config, polkadot_config, para_id)
+						.await
+						.map(|r| r.0)
+						.map_err(Into::into)
 				}
 			})
 		}
modifiednode/cli/src/service.rsdiffbeforeafterboth
before · node/cli/src/service.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.1819// std20use std::sync::Arc;21use std::sync::Mutex;22use std::collections::BTreeMap;23use std::time::Duration;24use fc_rpc_core::types::FeeHistoryCache;25use futures::StreamExt;2627use unique_rpc::overrides_handle;2829use serde::{Serialize, Deserialize};3031// Cumulus Imports32use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};33use cumulus_client_consensus_common::ParachainConsensus;34use cumulus_client_service::{35	prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,36};37use cumulus_client_network::BlockAnnounceValidator;38use cumulus_primitives_core::ParaId;39use cumulus_relay_chain_interface::RelayChainInterface;40use cumulus_relay_chain_local::build_relay_chain_interface;4142// Substrate Imports43use sc_client_api::ExecutorProvider;44use sc_executor::NativeElseWasmExecutor;45use sc_executor::NativeExecutionDispatch;46use sc_network::NetworkService;47use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};48use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};49use sp_consensus::SlotData;50use sp_keystore::SyncCryptoStorePtr;51use sp_runtime::traits::BlakeTwo256;52use substrate_prometheus_endpoint::Registry;53use sc_client_api::BlockchainEvents;5455// Frontier Imports56use fc_rpc_core::types::FilterPool;57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};5859use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6061/// Native executor instance.62pub struct UniqueRuntimeExecutor;63pub struct QuartzRuntimeExecutor;64pub struct OpalRuntimeExecutor;6566#[cfg(feature = "unique-runtime")]67impl NativeExecutionDispatch for UniqueRuntimeExecutor {68	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;6970	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {71		unique_runtime::api::dispatch(method, data)72	}7374	fn native_version() -> sc_executor::NativeVersion {75		unique_runtime::native_version()76	}77}7879#[cfg(feature = "quartz-runtime")]80impl NativeExecutionDispatch for QuartzRuntimeExecutor {81	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8283	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {84		quartz_runtime::api::dispatch(method, data)85	}8687	fn native_version() -> sc_executor::NativeVersion {88		quartz_runtime::native_version()89	}90}9192impl NativeExecutionDispatch for OpalRuntimeExecutor {93	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9495	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {96		opal_runtime::api::dispatch(method, data)97	}9899	fn native_version() -> sc_executor::NativeVersion {100		opal_runtime::native_version()101	}102}103104pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {105	let config_dir = config106		.base_path107		.as_ref()108		.map(|base_path| base_path.config_dir(config.chain_spec.id()))109		.unwrap_or_else(|| {110			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())111		});112	let database_dir = config_dir.join("frontier").join("db");113114	Ok(Arc::new(fc_db::Backend::<Block>::new(115		&fc_db::DatabaseSettings {116			source: fc_db::DatabaseSettingsSrc::RocksDb {117				path: database_dir,118				cache_size: 0,119			},120		},121	)?))122}123124type FullClient<RuntimeApi, ExecutorDispatch> =125	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;126type FullBackend = sc_service::TFullBackend<Block>;127type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;128129/// Starts a `ServiceBuilder` for a full service.130///131/// Use this macro if you don't actually need the full service, but just the builder in order to132/// be able to perform chain operations.133#[allow(clippy::type_complexity)]134pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(135	config: &Configuration,136	build_import_queue: BIQ,137) -> Result<138	PartialComponents<139		FullClient<RuntimeApi, ExecutorDispatch>,140		FullBackend,141		FullSelectChain,142		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,143		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,144		(145			Option<Telemetry>,146			Option<FilterPool>,147			Arc<fc_db::Backend<Block>>,148			Option<TelemetryWorkerHandle>,149			FeeHistoryCache,150		),151	>,152	sc_service::Error,153>154where155	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,156	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>157		+ Send158		+ Sync159		+ 'static,160	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,161	ExecutorDispatch: NativeExecutionDispatch + 'static,162	BIQ: FnOnce(163		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,164		&Configuration,165		Option<TelemetryHandle>,166		&TaskManager,167	) -> Result<168		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,169		sc_service::Error,170	>,171{172	let _telemetry = config173		.telemetry_endpoints174		.clone()175		.filter(|x| !x.is_empty())176		.map(|endpoints| -> Result<_, sc_telemetry::Error> {177			let worker = TelemetryWorker::new(16)?;178			let telemetry = worker.handle().new_telemetry(endpoints);179			Ok((worker, telemetry))180		})181		.transpose()?;182183	let telemetry = config184		.telemetry_endpoints185		.clone()186		.filter(|x| !x.is_empty())187		.map(|endpoints| -> Result<_, sc_telemetry::Error> {188			let worker = TelemetryWorker::new(16)?;189			let telemetry = worker.handle().new_telemetry(endpoints);190			Ok((worker, telemetry))191		})192		.transpose()?;193194	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(195		config.wasm_method,196		config.default_heap_pages,197		config.max_runtime_instances,198		config.runtime_cache_size,199	);200201	let (client, backend, keystore_container, task_manager) =202		sc_service::new_full_parts::<Block, RuntimeApi, _>(203			config,204			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),205			executor,206		)?;207	let client = Arc::new(client);208209	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());210211	let telemetry = telemetry.map(|(worker, telemetry)| {212		task_manager213			.spawn_handle()214			.spawn("telemetry", None, worker.run());215		telemetry216	});217218	let select_chain = sc_consensus::LongestChain::new(backend.clone());219220	let transaction_pool = sc_transaction_pool::BasicPool::new_full(221		config.transaction_pool.clone(),222		config.role.is_authority().into(),223		config.prometheus_registry(),224		task_manager.spawn_essential_handle(),225		client.clone(),226	);227228	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));229230	let frontier_backend = open_frontier_backend(config)?;231232	let import_queue = build_import_queue(233		client.clone(),234		config,235		telemetry.as_ref().map(|telemetry| telemetry.handle()),236		&task_manager,237	)?;238	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));239240	let params = PartialComponents {241		backend,242		client,243		import_queue,244		keystore_container,245		task_manager,246		transaction_pool,247		select_chain,248		other: (249			telemetry,250			filter_pool,251			frontier_backend,252			telemetry_worker_handle,253			fee_history_cache,254		),255	};256257	Ok(params)258}259260/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.261///262/// This is the actual implementation that is abstract over the executor and the runtime api.263#[sc_tracing::logging::prefix_logs_with("Parachain")]264async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(265	parachain_config: Configuration,266	polkadot_config: Configuration,267	id: ParaId,268	build_import_queue: BIQ,269	build_consensus: BIC,270) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>271where272	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,273	Runtime: RuntimeInstance + Send + Sync + 'static,274	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,275	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,276	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>277		+ Send278		+ Sync279		+ 'static,280	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>281		+ fp_rpc::EthereumRuntimeRPCApi<Block>282		+ sp_session::SessionKeys<Block>283		+ sp_block_builder::BlockBuilder<Block>284		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>285		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>286		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>287		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>288		+ sp_api::Metadata<Block>289		+ sp_offchain::OffchainWorkerApi<Block>290		+ cumulus_primitives_core::CollectCollationInfo<Block>,291	ExecutorDispatch: NativeExecutionDispatch + 'static,292	BIQ: FnOnce(293		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,294		&Configuration,295		Option<TelemetryHandle>,296		&TaskManager,297	) -> Result<298		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,299		sc_service::Error,300	>,301	BIC: FnOnce(302		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,303		Option<&Registry>,304		Option<TelemetryHandle>,305		&TaskManager,306		Arc<dyn RelayChainInterface>,307		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,308		Arc<NetworkService<Block, Hash>>,309		SyncCryptoStorePtr,310		bool,311	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,312{313	if matches!(parachain_config.role, Role::Light) {314		return Err("Light client not supported!".into());315	}316317	let parachain_config = prepare_node_config(parachain_config);318319	let params =320		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;321	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =322		params.other;323324	let client = params.client.clone();325	let backend = params.backend.clone();326	let mut task_manager = params.task_manager;327328	let (relay_chain_interface, collator_key) =329		build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)330			.map_err(|e| match e {331				polkadot_service::Error::Sub(x) => x,332				s => format!("{}", s).into(),333			})?;334335	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);336337	let force_authoring = parachain_config.force_authoring;338	let validator = parachain_config.role.is_authority();339	let prometheus_registry = parachain_config.prometheus_registry().cloned();340	let transaction_pool = params.transaction_pool.clone();341	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);342343	let (network, system_rpc_tx, start_network) =344		sc_service::build_network(sc_service::BuildNetworkParams {345			config: &parachain_config,346			client: client.clone(),347			transaction_pool: transaction_pool.clone(),348			spawn_handle: task_manager.spawn_handle(),349			import_queue: import_queue.clone(),350			block_announce_validator_builder: Some(Box::new(|_| {351				Box::new(block_announce_validator)352			})),353			warp_sync: None,354		})?;355356	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());357	let rpc_client = client.clone();358	let rpc_pool = transaction_pool.clone();359	let select_chain = params.select_chain.clone();360	let rpc_network = network.clone();361362	let rpc_frontier_backend = frontier_backend.clone();363364	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(365		task_manager.spawn_handle(),366		overrides_handle::<_, _, Runtime>(client.clone()),367		50,368		50,369	));370371	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {372		let full_deps = unique_rpc::FullDeps {373			backend: rpc_frontier_backend.clone(),374			deny_unsafe,375			client: rpc_client.clone(),376			pool: rpc_pool.clone(),377			graph: rpc_pool.pool().clone(),378			// TODO: Unhardcode379			enable_dev_signer: false,380			filter_pool: filter_pool.clone(),381			network: rpc_network.clone(),382			select_chain: select_chain.clone(),383			is_authority: validator,384			// TODO: Unhardcode385			max_past_logs: 10000,386			block_data_cache: block_data_cache.clone(),387			fee_history_cache: fee_history_cache.clone(),388			// TODO: Unhardcode389			fee_history_limit: 2048,390		};391392		Ok(393			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(394				full_deps,395				subscription_executor.clone(),396			),397		)398	});399400	task_manager.spawn_essential_handle().spawn(401		"frontier-mapping-sync-worker",402		None,403		MappingSyncWorker::new(404			client.import_notification_stream(),405			Duration::new(6, 0),406			client.clone(),407			backend.clone(),408			frontier_backend.clone(),409			SyncStrategy::Normal,410		)411		.for_each(|()| futures::future::ready(())),412	);413414	sc_service::spawn_tasks(sc_service::SpawnTasksParams {415		rpc_extensions_builder,416		client: client.clone(),417		transaction_pool: transaction_pool.clone(),418		task_manager: &mut task_manager,419		config: parachain_config,420		keystore: params.keystore_container.sync_keystore(),421		backend: backend.clone(),422		network: network.clone(),423		system_rpc_tx,424		telemetry: telemetry.as_mut(),425	})?;426427	let announce_block = {428		let network = network.clone();429		Arc::new(move |hash, data| network.announce_block(hash, data))430	};431432	let relay_chain_slot_duration = Duration::from_secs(6);433434	if validator {435		let parachain_consensus = build_consensus(436			client.clone(),437			prometheus_registry.as_ref(),438			telemetry.as_ref().map(|t| t.handle()),439			&task_manager,440			relay_chain_interface.clone(),441			transaction_pool,442			network,443			params.keystore_container.sync_keystore(),444			force_authoring,445		)?;446447		let spawner = task_manager.spawn_handle();448449		let params = StartCollatorParams {450			para_id: id,451			block_status: client.clone(),452			announce_block,453			client: client.clone(),454			task_manager: &mut task_manager,455			spawner,456			parachain_consensus,457			import_queue,458			collator_key,459			relay_chain_interface,460			relay_chain_slot_duration,461		};462463		start_collator(params).await?;464	} else {465		let params = StartFullNodeParams {466			client: client.clone(),467			announce_block,468			task_manager: &mut task_manager,469			para_id: id,470			import_queue,471			relay_chain_interface,472			relay_chain_slot_duration,473		};474475		start_full_node(params)?;476	}477478	start_network.start_network();479480	Ok((task_manager, client))481}482483/// Build the import queue for the the parachain runtime.484pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(485	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,486	config: &Configuration,487	telemetry: Option<TelemetryHandle>,488	task_manager: &TaskManager,489) -> Result<490	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,491	sc_service::Error,492>493where494	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>495		+ Send496		+ Sync497		+ 'static,498	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>499		+ sp_block_builder::BlockBuilder<Block>500		+ sp_consensus_aura::AuraApi<Block, AuraId>501		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,502	ExecutorDispatch: NativeExecutionDispatch + 'static,503{504	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;505506	cumulus_client_consensus_aura::import_queue::<507		sp_consensus_aura::sr25519::AuthorityPair,508		_,509		_,510		_,511		_,512		_,513		_,514	>(cumulus_client_consensus_aura::ImportQueueParams {515		block_import: client.clone(),516		client: client.clone(),517		create_inherent_data_providers: move |_, _| async move {518			let time = sp_timestamp::InherentDataProvider::from_system_time();519520			let slot =521				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(522					*time,523					slot_duration.slot_duration(),524				);525526			Ok((time, slot))527		},528		registry: config.prometheus_registry(),529		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),530		spawner: &task_manager.spawn_essential_handle(),531		telemetry,532	})533	.map_err(Into::into)534}535536/// Start a normal parachain node.537pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(538	parachain_config: Configuration,539	polkadot_config: Configuration,540	id: ParaId,541) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>542where543	Runtime: RuntimeInstance + Send + Sync + 'static,544	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,545	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,546	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>547		+ Send548		+ Sync549		+ 'static,550	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>551		+ fp_rpc::EthereumRuntimeRPCApi<Block>552		+ sp_session::SessionKeys<Block>553		+ sp_block_builder::BlockBuilder<Block>554		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>555		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>556		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>557		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>558		+ sp_api::Metadata<Block>559		+ sp_offchain::OffchainWorkerApi<Block>560		+ cumulus_primitives_core::CollectCollationInfo<Block>561		+ sp_consensus_aura::AuraApi<Block, AuraId>,562	ExecutorDispatch: NativeExecutionDispatch + 'static,563{564	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(565		parachain_config,566		polkadot_config,567		id,568		parachain_build_import_queue,569		|client,570		 prometheus_registry,571		 telemetry,572		 task_manager,573		 relay_chain_interface,574		 transaction_pool,575		 sync_oracle,576		 keystore,577		 force_authoring| {578			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;579580			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(581				task_manager.spawn_handle(),582				client.clone(),583				transaction_pool,584				prometheus_registry,585				telemetry.clone(),586			);587588			Ok(AuraConsensus::build::<589				sp_consensus_aura::sr25519::AuthorityPair,590				_,591				_,592				_,593				_,594				_,595				_,596			>(BuildAuraConsensusParams {597				proposer_factory,598				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {599					let relay_chain_interface = relay_chain_interface.clone();600					async move {601						let parachain_inherent =602						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(603							relay_parent,604							&relay_chain_interface,605							&validation_data,606							id,607						).await;608609						let time = sp_timestamp::InherentDataProvider::from_system_time();610611						let slot =612						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(613							*time,614							slot_duration.slot_duration(),615						);616617						let parachain_inherent = parachain_inherent.ok_or_else(|| {618							Box::<dyn std::error::Error + Send + Sync>::from(619								"Failed to create parachain inherent",620							)621						})?;622						Ok((time, slot, parachain_inherent))623					}624				},625				block_import: client.clone(),626				para_client: client,627				backoff_authoring_blocks: Option::<()>::None,628				sync_oracle,629				keystore,630				force_authoring,631				slot_duration: *slot_duration,632				// We got around 500ms for proposing633				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),634				telemetry,635				max_block_proposal_slot_portion: None,636			}))637		},638	)639	.await640}
after · node/cli/src/service.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.1819// std20use std::sync::Arc;21use std::sync::Mutex;22use std::collections::BTreeMap;23use std::time::Duration;24use fc_rpc_core::types::FeeHistoryCache;25use futures::StreamExt;2627use unique_rpc::overrides_handle;2829use serde::{Serialize, Deserialize};3031// Cumulus Imports32use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};33use cumulus_client_consensus_common::ParachainConsensus;34use cumulus_client_service::{35	prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,36};37use cumulus_client_network::BlockAnnounceValidator;38use cumulus_primitives_core::ParaId;39use cumulus_relay_chain_interface::RelayChainInterface;40use cumulus_relay_chain_local::build_relay_chain_interface;4142// Substrate Imports43use sc_client_api::ExecutorProvider;44use sc_executor::NativeElseWasmExecutor;45use sc_executor::NativeExecutionDispatch;46use sc_network::NetworkService;47use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};48use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};49use sp_consensus::SlotData;50use sp_keystore::SyncCryptoStorePtr;51use sp_runtime::traits::BlakeTwo256;52use substrate_prometheus_endpoint::Registry;53use sc_client_api::BlockchainEvents;5455// Frontier Imports56use fc_rpc_core::types::FilterPool;57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};5859use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};60use crate::chain_spec::ServiceId;6162/// Native executor instance.63pub struct UniqueRuntimeExecutor;64pub struct QuartzRuntimeExecutor;65pub struct OpalRuntimeExecutor;6667#[cfg(feature = "unique-runtime")]68impl NativeExecutionDispatch for UniqueRuntimeExecutor {69	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7071	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {72		unique_runtime::api::dispatch(method, data)73	}7475	fn native_version() -> sc_executor::NativeVersion {76		unique_runtime::native_version()77	}78}7980#[cfg(feature = "quartz-runtime")]81impl NativeExecutionDispatch for QuartzRuntimeExecutor {82	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8384	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {85		quartz_runtime::api::dispatch(method, data)86	}8788	fn native_version() -> sc_executor::NativeVersion {89		quartz_runtime::native_version()90	}91}9293impl NativeExecutionDispatch for OpalRuntimeExecutor {94	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9596	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {97		opal_runtime::api::dispatch(method, data)98	}99100	fn native_version() -> sc_executor::NativeVersion {101		opal_runtime::native_version()102	}103}104105pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {106	let config_dir = config107		.base_path108		.as_ref()109		.map(|base_path| base_path.config_dir(config.chain_spec.id()))110		.unwrap_or_else(|| {111			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())112		});113	let database_dir = config_dir.join("frontier").join("db");114115	Ok(Arc::new(fc_db::Backend::<Block>::new(116		&fc_db::DatabaseSettings {117			source: fc_db::DatabaseSettingsSrc::RocksDb {118				path: database_dir,119				cache_size: 0,120			},121		},122	)?))123}124125type FullClient<RuntimeApi, ExecutorDispatch> =126	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;127type FullBackend = sc_service::TFullBackend<Block>;128type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;129type MaybeSelectChain = Option<FullSelectChain>;130131/// Starts a `ServiceBuilder` for a full service.132///133/// Use this macro if you don't actually need the full service, but just the builder in order to134/// be able to perform chain operations.135#[allow(clippy::type_complexity)]136pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(137	config: &Configuration,138	build_import_queue: BIQ,139	service_id: ServiceId,140) -> Result<141	PartialComponents<142		FullClient<RuntimeApi, ExecutorDispatch>,143		FullBackend,144		MaybeSelectChain,145		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,146		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,147		(148			Option<Telemetry>,149			Option<FilterPool>,150			Arc<fc_db::Backend<Block>>,151			Option<TelemetryWorkerHandle>,152			FeeHistoryCache,153		),154	>,155	sc_service::Error,156>157where158	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,159	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>160		+ Send161		+ Sync162		+ 'static,163	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,164	ExecutorDispatch: NativeExecutionDispatch + 'static,165	BIQ: FnOnce(166		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,167		&Configuration,168		Option<TelemetryHandle>,169		&TaskManager,170	) -> Result<171		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,172		sc_service::Error,173	>,174{175	let _telemetry = config176		.telemetry_endpoints177		.clone()178		.filter(|x| !x.is_empty())179		.map(|endpoints| -> Result<_, sc_telemetry::Error> {180			let worker = TelemetryWorker::new(16)?;181			let telemetry = worker.handle().new_telemetry(endpoints);182			Ok((worker, telemetry))183		})184		.transpose()?;185186	let telemetry = config187		.telemetry_endpoints188		.clone()189		.filter(|x| !x.is_empty())190		.map(|endpoints| -> Result<_, sc_telemetry::Error> {191			let worker = TelemetryWorker::new(16)?;192			let telemetry = worker.handle().new_telemetry(endpoints);193			Ok((worker, telemetry))194		})195		.transpose()?;196197	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(198		config.wasm_method,199		config.default_heap_pages,200		config.max_runtime_instances,201		config.runtime_cache_size,202	);203204	let (client, backend, keystore_container, task_manager) =205		sc_service::new_full_parts::<Block, RuntimeApi, _>(206			config,207			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),208			executor,209		)?;210	let client = Arc::new(client);211212	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());213214	let telemetry = telemetry.map(|(worker, telemetry)| {215		task_manager216			.spawn_handle()217			.spawn("telemetry", None, worker.run());218		telemetry219	});220221	let select_chain = match service_id {222		ServiceId::Prod => Some(sc_consensus::LongestChain::new(backend.clone())),223		ServiceId::Dev => None224	};225226	let transaction_pool = sc_transaction_pool::BasicPool::new_full(227		config.transaction_pool.clone(),228		config.role.is_authority().into(),229		config.prometheus_registry(),230		task_manager.spawn_essential_handle(),231		client.clone(),232	);233234	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));235236	let frontier_backend = open_frontier_backend(config)?;237238	let import_queue = build_import_queue(239		client.clone(),240		config,241		telemetry.as_ref().map(|telemetry| telemetry.handle()),242		&task_manager,243	)?;244	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));245246	let params = PartialComponents {247		backend,248		client,249		import_queue,250		keystore_container,251		task_manager,252		transaction_pool,253		select_chain,254		other: (255			telemetry,256			filter_pool,257			frontier_backend,258			telemetry_worker_handle,259			fee_history_cache,260		),261	};262263	Ok(params)264}265266/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.267///268/// This is the actual implementation that is abstract over the executor and the runtime api.269#[sc_tracing::logging::prefix_logs_with("Parachain")]270async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(271	parachain_config: Configuration,272	polkadot_config: Configuration,273	id: ParaId,274	build_import_queue: BIQ,275	build_consensus: BIC,276) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>277where278	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,279	Runtime: RuntimeInstance + Send + Sync + 'static,280	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,281	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,282	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>283		+ Send284		+ Sync285		+ 'static,286	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>287		+ fp_rpc::EthereumRuntimeRPCApi<Block>288		+ sp_session::SessionKeys<Block>289		+ sp_block_builder::BlockBuilder<Block>290		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>291		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>292		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>293		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>294		+ sp_api::Metadata<Block>295		+ sp_offchain::OffchainWorkerApi<Block>296		+ cumulus_primitives_core::CollectCollationInfo<Block>,297	ExecutorDispatch: NativeExecutionDispatch + 'static,298	BIQ: FnOnce(299		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,300		&Configuration,301		Option<TelemetryHandle>,302		&TaskManager,303	) -> Result<304		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,305		sc_service::Error,306	>,307	BIC: FnOnce(308		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,309		Option<&Registry>,310		Option<TelemetryHandle>,311		&TaskManager,312		Arc<dyn RelayChainInterface>,313		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,314		Arc<NetworkService<Block, Hash>>,315		SyncCryptoStorePtr,316		bool,317	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,318{319	if matches!(parachain_config.role, Role::Light) {320		return Err("Light client not supported!".into());321	}322323	let parachain_config = prepare_node_config(parachain_config);324325	let params =326		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(327			&parachain_config, build_import_queue, ServiceId::Prod328		)?;329	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =330		params.other;331332	let client = params.client.clone();333	let backend = params.backend.clone();334	let mut task_manager = params.task_manager;335336	let (relay_chain_interface, collator_key) =337		build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)338			.map_err(|e| match e {339				polkadot_service::Error::Sub(x) => x,340				s => format!("{}", s).into(),341			})?;342343	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);344345	let force_authoring = parachain_config.force_authoring;346	let validator = parachain_config.role.is_authority();347	let prometheus_registry = parachain_config.prometheus_registry().cloned();348	let transaction_pool = params.transaction_pool.clone();349	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);350351	let (network, system_rpc_tx, start_network) =352		sc_service::build_network(sc_service::BuildNetworkParams {353			config: &parachain_config,354			client: client.clone(),355			transaction_pool: transaction_pool.clone(),356			spawn_handle: task_manager.spawn_handle(),357			import_queue: import_queue.clone(),358			block_announce_validator_builder: Some(Box::new(|_| {359				Box::new(block_announce_validator)360			})),361			warp_sync: None,362		})?;363364	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());365	let rpc_client = client.clone();366	let rpc_pool = transaction_pool.clone();367	let select_chain = params.select_chain368							.expect("select_chain always exists when running Prod service; qed")369							.clone();370	let rpc_network = network.clone();371372	let rpc_frontier_backend = frontier_backend.clone();373374	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(375		task_manager.spawn_handle(),376		overrides_handle::<_, _, Runtime>(client.clone()),377		50,378		50,379	));380381	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {382		let full_deps = unique_rpc::FullDeps {383			backend: rpc_frontier_backend.clone(),384			deny_unsafe,385			client: rpc_client.clone(),386			pool: rpc_pool.clone(),387			graph: rpc_pool.pool().clone(),388			// TODO: Unhardcode389			enable_dev_signer: false,390			filter_pool: filter_pool.clone(),391			network: rpc_network.clone(),392			select_chain: select_chain.clone(),393			is_authority: validator,394			// TODO: Unhardcode395			max_past_logs: 10000,396			block_data_cache: block_data_cache.clone(),397			fee_history_cache: fee_history_cache.clone(),398			// TODO: Unhardcode399			fee_history_limit: 2048,400		};401402		Ok(403			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(404				full_deps,405				subscription_executor.clone(),406			),407		)408	});409410	task_manager.spawn_essential_handle().spawn(411		"frontier-mapping-sync-worker",412		None,413		MappingSyncWorker::new(414			client.import_notification_stream(),415			Duration::new(6, 0),416			client.clone(),417			backend.clone(),418			frontier_backend.clone(),419			SyncStrategy::Normal,420		)421		.for_each(|()| futures::future::ready(())),422	);423424	sc_service::spawn_tasks(sc_service::SpawnTasksParams {425		rpc_extensions_builder,426		client: client.clone(),427		transaction_pool: transaction_pool.clone(),428		task_manager: &mut task_manager,429		config: parachain_config,430		keystore: params.keystore_container.sync_keystore(),431		backend: backend.clone(),432		network: network.clone(),433		system_rpc_tx,434		telemetry: telemetry.as_mut(),435	})?;436437	let announce_block = {438		let network = network.clone();439		Arc::new(move |hash, data| network.announce_block(hash, data))440	};441442	let relay_chain_slot_duration = Duration::from_secs(6);443444	if validator {445		let parachain_consensus = build_consensus(446			client.clone(),447			prometheus_registry.as_ref(),448			telemetry.as_ref().map(|t| t.handle()),449			&task_manager,450			relay_chain_interface.clone(),451			transaction_pool,452			network,453			params.keystore_container.sync_keystore(),454			force_authoring,455		)?;456457		let spawner = task_manager.spawn_handle();458459		let params = StartCollatorParams {460			para_id: id,461			block_status: client.clone(),462			announce_block,463			client: client.clone(),464			task_manager: &mut task_manager,465			spawner,466			parachain_consensus,467			import_queue,468			collator_key,469			relay_chain_interface,470			relay_chain_slot_duration,471		};472473		start_collator(params).await?;474	} else {475		let params = StartFullNodeParams {476			client: client.clone(),477			announce_block,478			task_manager: &mut task_manager,479			para_id: id,480			import_queue,481			relay_chain_interface,482			relay_chain_slot_duration,483		};484485		start_full_node(params)?;486	}487488	start_network.start_network();489490	Ok((task_manager, client))491}492493/// Build the import queue for the the parachain runtime.494pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(495	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,496	config: &Configuration,497	telemetry: Option<TelemetryHandle>,498	task_manager: &TaskManager,499) -> Result<500	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,501	sc_service::Error,502>503where504	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>505		+ Send506		+ Sync507		+ 'static,508	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>509		+ sp_block_builder::BlockBuilder<Block>510		+ sp_consensus_aura::AuraApi<Block, AuraId>511		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,512	ExecutorDispatch: NativeExecutionDispatch + 'static,513{514	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;515516	cumulus_client_consensus_aura::import_queue::<517		sp_consensus_aura::sr25519::AuthorityPair,518		_,519		_,520		_,521		_,522		_,523		_,524	>(cumulus_client_consensus_aura::ImportQueueParams {525		block_import: client.clone(),526		client: client.clone(),527		create_inherent_data_providers: move |_, _| async move {528			let time = sp_timestamp::InherentDataProvider::from_system_time();529530			let slot =531				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(532					*time,533					slot_duration.slot_duration(),534				);535536			Ok((time, slot))537		},538		registry: config.prometheus_registry(),539		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),540		spawner: &task_manager.spawn_essential_handle(),541		telemetry,542	})543	.map_err(Into::into)544}545546/// Start a normal parachain node.547pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(548	parachain_config: Configuration,549	polkadot_config: Configuration,550	id: ParaId,551) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>552where553	Runtime: RuntimeInstance + Send + Sync + 'static,554	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,555	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,556	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>557		+ Send558		+ Sync559		+ 'static,560	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>561		+ fp_rpc::EthereumRuntimeRPCApi<Block>562		+ sp_session::SessionKeys<Block>563		+ sp_block_builder::BlockBuilder<Block>564		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>565		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>566		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>567		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>568		+ sp_api::Metadata<Block>569		+ sp_offchain::OffchainWorkerApi<Block>570		+ cumulus_primitives_core::CollectCollationInfo<Block>571		+ sp_consensus_aura::AuraApi<Block, AuraId>,572	ExecutorDispatch: NativeExecutionDispatch + 'static,573{574	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(575		parachain_config,576		polkadot_config,577		id,578		parachain_build_import_queue,579		|client,580		 prometheus_registry,581		 telemetry,582		 task_manager,583		 relay_chain_interface,584		 transaction_pool,585		 sync_oracle,586		 keystore,587		 force_authoring| {588			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;589590			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(591				task_manager.spawn_handle(),592				client.clone(),593				transaction_pool,594				prometheus_registry,595				telemetry.clone(),596			);597598			Ok(AuraConsensus::build::<599				sp_consensus_aura::sr25519::AuthorityPair,600				_,601				_,602				_,603				_,604				_,605				_,606			>(BuildAuraConsensusParams {607				proposer_factory,608				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {609					let relay_chain_interface = relay_chain_interface.clone();610					async move {611						let parachain_inherent =612						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(613							relay_parent,614							&relay_chain_interface,615							&validation_data,616							id,617						).await;618619						let time = sp_timestamp::InherentDataProvider::from_system_time();620621						let slot =622						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(623							*time,624							slot_duration.slot_duration(),625						);626627						let parachain_inherent = parachain_inherent.ok_or_else(|| {628							Box::<dyn std::error::Error + Send + Sync>::from(629								"Failed to create parachain inherent",630							)631						})?;632						Ok((time, slot, parachain_inherent))633					}634				},635				block_import: client.clone(),636				para_client: client,637				backoff_authoring_blocks: Option::<()>::None,638				sync_oracle,639				keystore,640				force_authoring,641				slot_duration: *slot_duration,642				// We got around 500ms for proposing643				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),644				telemetry,645				max_block_proposal_slot_portion: None,646			}))647		},648	)649	.await650}651652fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(653	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,654	config: &Configuration,655	_: Option<TelemetryHandle>,656	task_manager: &TaskManager,657) -> Result<sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>, sc_service::Error>658where659	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>660	+ Send661	+ Sync662	+ 'static,663	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>664							+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,665	ExecutorDispatch: NativeExecutionDispatch + 'static,666{667	Ok(sc_consensus_manual_seal::import_queue(668		Box::new(client.clone()),669		&task_manager.spawn_essential_handle(),670		config.prometheus_registry(),671	))672}673674/// Builds a new development service. This service uses instant seal, and mocks675/// the parachain inherent676pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(config: Configuration)677	-> sc_service::error::Result<TaskManager>678where679	Runtime: RuntimeInstance + Send + Sync + 'static,680	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,681	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,682	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>683		+ Send684		+ Sync685		+ 'static,686	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>687							+ fp_rpc::EthereumRuntimeRPCApi<Block>688							+ sp_session::SessionKeys<Block>689							+ sp_block_builder::BlockBuilder<Block>690							+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>691							+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>692							+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>693							+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>694							+ sp_api::Metadata<Block>695							+ sp_offchain::OffchainWorkerApi<Block>696							+ cumulus_primitives_core::CollectCollationInfo<Block>697							+ sp_consensus_aura::AuraApi<Block, AuraId>,698	ExecutorDispatch: NativeExecutionDispatch + 'static,699{700	use futures::Stream;701	use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};702	use fc_consensus::FrontierBlockImport;703	use sc_client_api::HeaderBackend;704705	let sc_service::PartialComponents {706		client,707		backend,708		mut task_manager,709		import_queue,710		keystore_container,711		select_chain: maybe_select_chain,712		transaction_pool,713		other:714			(715				telemetry,716				filter_pool,717				frontier_backend,718				_telemetry_worker_handle,719				fee_history_cache,720			),721	} = new_partial::<RuntimeApi, ExecutorDispatch, _>(722		&config,723		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,724		ServiceId::Dev725	)?;726727	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(728		task_manager.spawn_handle(),729		overrides_handle::<_, _, Runtime>(client.clone()),730		50,731		50,732	));733734	let (network, system_rpc_tx, network_starter) =735		sc_service::build_network(sc_service::BuildNetworkParams {736			config: &config,737			client: client.clone(),738			transaction_pool: transaction_pool.clone(),739			spawn_handle: task_manager.spawn_handle(),740			import_queue,741			block_announce_validator_builder: None,742			warp_sync: None,743		})?;744745	if config.offchain_worker.enabled {746		sc_service::build_offchain_workers(747			&config,748			task_manager.spawn_handle(),749			client.clone(),750			network.clone(),751		);752	}753754	let prometheus_registry = config.prometheus_registry().cloned();755	let collator = config.role.is_authority();756757	let select_chain = maybe_select_chain.clone().expect(758		"`new_partial` builds a `LongestChainRule` when building dev service.\759			We specified the dev service when calling `new_partial`.\760			Therefore, a `LongestChainRule` is present. qed.",761	);762763	if collator {764		let block_import =765			FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());766767		let env = sc_basic_authorship::ProposerFactory::new(768			task_manager.spawn_handle(),769			client.clone(),770			transaction_pool.clone(),771			prometheus_registry.as_ref(),772			telemetry.as_ref().map(|x| x.handle()),773		);774775		let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =776			Box::new(777				// This bit cribbed from the implementation of instant seal.778				transaction_pool779					.pool()780					.validated_pool()781					.import_notification_stream()782					.map(|_| EngineCommand::SealNewBlock {783						create_empty: true, // was false in Moonbeam784						finalize: false,785						parent_hash: None,786						sender: None,787					}),788			);789790		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;791		let client_set_aside_for_cidp = client.clone();792793		task_manager.spawn_essential_handle().spawn_blocking(794			"authorship_task",795			Some("block-authoring"),796			run_manual_seal(ManualSealParams {797				block_import,798				env,799				client: client.clone(),800				pool: transaction_pool.clone(),801				commands_stream,802				select_chain: select_chain.clone(),803				consensus_data_provider: None,804				create_inherent_data_providers: move |block: Hash, ()| {805					let current_para_block = client_set_aside_for_cidp806						.number(block)807						.expect("Header lookup should succeed")808						.expect("Header passed in as parent should be present in backend.");809810					let client_for_xcm = client_set_aside_for_cidp.clone();811					async move {812						let time = sp_timestamp::InherentDataProvider::from_system_time();813814						let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {815							current_para_block,816							relay_offset: 1000,817							relay_blocks_per_para_block: 2,818							xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(819								&*client_for_xcm,820								block,821								Default::default(),822								Default::default(),823							),824							raw_downward_messages: vec![],825							raw_horizontal_messages: vec![],826						};827828						let slot =829						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(830							*time,831							slot_duration.slot_duration(),832						);833834						Ok((time, slot, mocked_parachain))835					}836				},837			}),838		);839	}840841	task_manager.spawn_essential_handle().spawn(842		"frontier-mapping-sync-worker",843		Some("block-authoring"),844		MappingSyncWorker::new(845			client.import_notification_stream(),846			Duration::new(6, 0),847			client.clone(),848			backend.clone(),849			frontier_backend.clone(),850			SyncStrategy::Normal,851		)852		.for_each(|()| futures::future::ready(())),853	);854855	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());856	let rpc_client = client.clone();857	let rpc_pool = transaction_pool.clone();858	let rpc_network = network.clone();859	let rpc_frontier_backend = frontier_backend.clone();860	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {861		let full_deps = unique_rpc::FullDeps {862			backend: rpc_frontier_backend.clone(),863			deny_unsafe,864			client: rpc_client.clone(),865			pool: rpc_pool.clone(),866			graph: rpc_pool.pool().clone(),867			// TODO: Unhardcode868			enable_dev_signer: false,869			filter_pool: filter_pool.clone(),870			network: rpc_network.clone(),871			select_chain: select_chain.clone(),872			is_authority: collator,873			// TODO: Unhardcode874			max_past_logs: 10000,875			block_data_cache: block_data_cache.clone(),876			fee_history_cache: fee_history_cache.clone(),877			// TODO: Unhardcode878			fee_history_limit: 2048,879		};880881		Ok(unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(882			full_deps,883			subscription_executor.clone(),884		))885	});886887	sc_service::spawn_tasks(sc_service::SpawnTasksParams {888		network,889		client,890		keystore: keystore_container.sync_keystore(),891		task_manager: &mut task_manager,892		transaction_pool,893		rpc_extensions_builder,894		backend,895		system_rpc_tx,896		config,897		telemetry: None,898	})?;899900	network_starter.start_network();901	Ok(task_manager)902}