git.delta.rocks / unique-network / refs/commits / 2c040b3e73e9

difftreelog

source

node/cli/src/service.rs33.5 KiBsourcehistory
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// std18use std::sync::Arc;19use std::sync::Mutex;20use std::collections::BTreeMap;21use std::time::Duration;22use std::pin::Pin;23use fc_rpc_core::types::FeeHistoryCache;24use futures::{25	Stream, StreamExt,26	stream::select,27	task::{Context, Poll},28};29use tokio::time::Interval;3031use unique_rpc::overrides_handle;3233use serde::{Serialize, Deserialize};3435// Cumulus Imports36use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};37use cumulus_client_consensus_common::{38	ParachainConsensus, ParachainBlockImport as TParachainBlockImport,39};40use cumulus_client_service::{41	prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,42};43use cumulus_client_cli::CollatorOptions;44use cumulus_client_network::BlockAnnounceValidator;45use cumulus_primitives_core::ParaId;46use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;47use cumulus_relay_chain_interface::{RelayChainInterface, RelayChainResult};48use cumulus_relay_chain_minimal_node::build_minimal_relay_chain_node;4950// Substrate Imports51use sp_api::BlockT;52use sc_executor::NativeElseWasmExecutor;53use sc_executor::NativeExecutionDispatch;54use sc_network::NetworkBlock;55use sc_network_sync::SyncingService;56use sc_service::{BasePath, Configuration, PartialComponents, TaskManager};57use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};58use sp_keystore::SyncCryptoStorePtr;59use sp_runtime::traits::BlakeTwo256;60use substrate_prometheus_endpoint::Registry;61use sc_client_api::BlockchainEvents;62use sc_consensus::ImportQueue;6364use polkadot_service::CollatorPair;6566// Frontier Imports67use fc_rpc_core::types::FilterPool;68use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};6970use up_common::types::opaque::*;7172use crate::chain_spec::RuntimeIdentification;7374/// Unique native executor instance.75#[cfg(feature = "unique-runtime")]76pub struct UniqueRuntimeExecutor;7778#[cfg(feature = "quartz-runtime")]79/// Quartz native executor instance.80pub struct QuartzRuntimeExecutor;8182/// Opal native executor instance.83pub struct OpalRuntimeExecutor;8485#[cfg(all(feature = "unique-runtime", feature = "runtime-benchmarks"))]86pub type DefaultRuntimeExecutor = UniqueRuntimeExecutor;8788#[cfg(all(89	not(feature = "unique-runtime"),90	feature = "quartz-runtime",91	feature = "runtime-benchmarks"92))]93pub type DefaultRuntimeExecutor = QuartzRuntimeExecutor;9495#[cfg(all(96	not(feature = "unique-runtime"),97	not(feature = "quartz-runtime"),98	feature = "runtime-benchmarks"99))]100pub type DefaultRuntimeExecutor = OpalRuntimeExecutor;101102#[cfg(feature = "unique-runtime")]103impl NativeExecutionDispatch for UniqueRuntimeExecutor {104	/// Only enable the benchmarking host functions when we actually want to benchmark.105	#[cfg(feature = "runtime-benchmarks")]106	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;107	/// Otherwise we only use the default Substrate host functions.108	#[cfg(not(feature = "runtime-benchmarks"))]109	type ExtendHostFunctions = ();110111	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {112		unique_runtime::api::dispatch(method, data)113	}114115	fn native_version() -> sc_executor::NativeVersion {116		unique_runtime::native_version()117	}118}119120#[cfg(feature = "quartz-runtime")]121impl NativeExecutionDispatch for QuartzRuntimeExecutor {122	/// Only enable the benchmarking host functions when we actually want to benchmark.123	#[cfg(feature = "runtime-benchmarks")]124	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;125	/// Otherwise we only use the default Substrate host functions.126	#[cfg(not(feature = "runtime-benchmarks"))]127	type ExtendHostFunctions = ();128129	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {130		quartz_runtime::api::dispatch(method, data)131	}132133	fn native_version() -> sc_executor::NativeVersion {134		quartz_runtime::native_version()135	}136}137138impl NativeExecutionDispatch for OpalRuntimeExecutor {139	/// Only enable the benchmarking host functions when we actually want to benchmark.140	#[cfg(feature = "runtime-benchmarks")]141	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;142	/// Otherwise we only use the default Substrate host functions.143	#[cfg(not(feature = "runtime-benchmarks"))]144	type ExtendHostFunctions = ();145146	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {147		opal_runtime::api::dispatch(method, data)148	}149150	fn native_version() -> sc_executor::NativeVersion {151		opal_runtime::native_version()152	}153}154155pub struct AutosealInterval {156	interval: Interval,157}158159impl AutosealInterval {160	pub fn new(config: &Configuration, interval: Duration) -> Self {161		let _tokio_runtime = config.tokio_handle.enter();162		let interval = tokio::time::interval(interval);163164		Self { interval }165	}166}167168impl Stream for AutosealInterval {169	type Item = tokio::time::Instant;170171	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {172		self.interval.poll_tick(cx).map(Some)173	}174}175176pub fn open_frontier_backend<Block: BlockT, C: sp_blockchain::HeaderBackend<Block>>(177	client: Arc<C>,178	config: &Configuration,179) -> Result<Arc<fc_db::Backend<Block>>, String> {180	let config_dir = config181		.base_path182		.as_ref()183		.map(|base_path| base_path.config_dir(config.chain_spec.id()))184		.unwrap_or_else(|| {185			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())186		});187	let database_dir = config_dir.join("frontier").join("db");188189	Ok(Arc::new(fc_db::Backend::<Block>::new(190		client,191		&fc_db::DatabaseSettings {192			source: fc_db::DatabaseSource::RocksDb {193				path: database_dir,194				cache_size: 0,195			},196		},197	)?))198}199200type FullClient<RuntimeApi, ExecutorDispatch> =201	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;202type FullBackend = sc_service::TFullBackend<Block>;203type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;204type ParachainBlockImport<RuntimeApi, ExecutorDispatch> =205	TParachainBlockImport<Block, Arc<FullClient<RuntimeApi, ExecutorDispatch>>, FullBackend>;206207/// Starts a `ServiceBuilder` for a full service.208///209/// Use this macro if you don't actually need the full service, but just the builder in order to210/// be able to perform chain operations.211#[allow(clippy::type_complexity)]212pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(213	config: &Configuration,214	build_import_queue: BIQ,215) -> Result<216	PartialComponents<217		FullClient<RuntimeApi, ExecutorDispatch>,218		FullBackend,219		FullSelectChain,220		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,221		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,222		(223			Option<Telemetry>,224			Option<FilterPool>,225			Arc<fc_db::Backend<Block>>,226			Option<TelemetryWorkerHandle>,227			FeeHistoryCache,228		),229	>,230	sc_service::Error,231>232where233	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,234	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>235		+ Send236		+ Sync237		+ 'static,238	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,239	ExecutorDispatch: NativeExecutionDispatch + 'static,240	BIQ: FnOnce(241		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,242		Arc<FullBackend>,243		&Configuration,244		Option<TelemetryHandle>,245		&TaskManager,246	) -> Result<247		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,248		sc_service::Error,249	>,250{251	let _telemetry = config252		.telemetry_endpoints253		.clone()254		.filter(|x| !x.is_empty())255		.map(|endpoints| -> Result<_, sc_telemetry::Error> {256			let worker = TelemetryWorker::new(16)?;257			let telemetry = worker.handle().new_telemetry(endpoints);258			Ok((worker, telemetry))259		})260		.transpose()?;261262	let telemetry = config263		.telemetry_endpoints264		.clone()265		.filter(|x| !x.is_empty())266		.map(|endpoints| -> Result<_, sc_telemetry::Error> {267			let worker = TelemetryWorker::new(16)?;268			let telemetry = worker.handle().new_telemetry(endpoints);269			Ok((worker, telemetry))270		})271		.transpose()?;272273	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(274		config.wasm_method,275		config.default_heap_pages,276		config.max_runtime_instances,277		config.runtime_cache_size,278	);279280	let (client, backend, keystore_container, task_manager) =281		sc_service::new_full_parts::<Block, RuntimeApi, _>(282			config,283			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),284			executor,285		)?;286	let client = Arc::new(client);287288	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());289290	let telemetry = telemetry.map(|(worker, telemetry)| {291		task_manager292			.spawn_handle()293			.spawn("telemetry", None, worker.run());294		telemetry295	});296297	let select_chain = sc_consensus::LongestChain::new(backend.clone());298299	let transaction_pool = sc_transaction_pool::BasicPool::new_full(300		config.transaction_pool.clone(),301		config.role.is_authority().into(),302		config.prometheus_registry(),303		task_manager.spawn_essential_handle(),304		client.clone(),305	);306307	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));308309	let frontier_backend = open_frontier_backend(client.clone(), config)?;310311	let import_queue = build_import_queue(312		client.clone(),313		backend.clone(),314		config,315		telemetry.as_ref().map(|telemetry| telemetry.handle()),316		&task_manager,317	)?;318	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));319320	let params = PartialComponents {321		backend,322		client,323		import_queue,324		keystore_container,325		task_manager,326		transaction_pool,327		select_chain,328		other: (329			telemetry,330			filter_pool,331			frontier_backend,332			telemetry_worker_handle,333			fee_history_cache,334		),335	};336337	Ok(params)338}339340async fn build_relay_chain_interface(341	polkadot_config: Configuration,342	parachain_config: &Configuration,343	telemetry_worker_handle: Option<TelemetryWorkerHandle>,344	task_manager: &mut TaskManager,345	collator_options: CollatorOptions,346	hwbench: Option<sc_sysinfo::HwBench>,347) -> RelayChainResult<(348	Arc<(dyn RelayChainInterface + 'static)>,349	Option<CollatorPair>,350)> {351	if collator_options.relay_chain_rpc_urls.is_empty() {352		build_inprocess_relay_chain(353			polkadot_config,354			parachain_config,355			telemetry_worker_handle,356			task_manager,357			hwbench,358		)359	} else {360		build_minimal_relay_chain_node(361			polkadot_config,362			task_manager,363			collator_options.relay_chain_rpc_urls,364		)365		.await366	}367}368369/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.370///371/// This is the actual implementation that is abstract over the executor and the runtime api.372#[sc_tracing::logging::prefix_logs_with("Parachain")]373async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(374	parachain_config: Configuration,375	polkadot_config: Configuration,376	collator_options: CollatorOptions,377	id: ParaId,378	build_import_queue: BIQ,379	build_consensus: BIC,380	hwbench: Option<sc_sysinfo::HwBench>,381) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>382where383	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,384	Runtime: RuntimeInstance + Send + Sync + 'static,385	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,386	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,387	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>388		+ Send389		+ Sync390		+ 'static,391	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>392		+ fp_rpc::EthereumRuntimeRPCApi<Block>393		+ fp_rpc::ConvertTransactionRuntimeApi<Block>394		+ sp_session::SessionKeys<Block>395		+ sp_block_builder::BlockBuilder<Block>396		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>397		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>398		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>399		+ app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>400		+ up_pov_estimate_rpc::PovEstimateApi<Block>401		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>402		+ sp_api::Metadata<Block>403		+ sp_offchain::OffchainWorkerApi<Block>404		+ cumulus_primitives_core::CollectCollationInfo<Block>,405	ExecutorDispatch: NativeExecutionDispatch + 'static,406	BIQ: FnOnce(407		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,408		Arc<FullBackend>,409		&Configuration,410		Option<TelemetryHandle>,411		&TaskManager,412	) -> Result<413		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,414		sc_service::Error,415	>,416	BIC: FnOnce(417		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,418		Arc<FullBackend>,419		Option<&Registry>,420		Option<TelemetryHandle>,421		&TaskManager,422		Arc<dyn RelayChainInterface>,423		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,424		Arc<SyncingService<Block>>,425		SyncCryptoStorePtr,426		bool,427	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,428{429	let parachain_config = prepare_node_config(parachain_config);430431	let params =432		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;433	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =434		params.other;435436	let client = params.client.clone();437	let backend = params.backend.clone();438	let mut task_manager = params.task_manager;439440	let (relay_chain_interface, collator_key) = build_relay_chain_interface(441		polkadot_config,442		&parachain_config,443		telemetry_worker_handle,444		&mut task_manager,445		collator_options.clone(),446		hwbench.clone(),447	)448	.await449	.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;450451	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);452453	let force_authoring = parachain_config.force_authoring;454	let validator = parachain_config.role.is_authority();455	let prometheus_registry = parachain_config.prometheus_registry().cloned();456	let transaction_pool = params.transaction_pool.clone();457	let import_queue_service = params.import_queue.service();458459	let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =460		sc_service::build_network(sc_service::BuildNetworkParams {461			config: &parachain_config,462			client: client.clone(),463			transaction_pool: transaction_pool.clone(),464			spawn_handle: task_manager.spawn_handle(),465			import_queue: params.import_queue,466			block_announce_validator_builder: Some(Box::new(|_| {467				Box::new(block_announce_validator)468			})),469			warp_sync_params: None,470		})?;471472	let rpc_client = client.clone();473	let rpc_pool = transaction_pool.clone();474	let select_chain = params.select_chain.clone();475	let rpc_network = network.clone();476	let rpc_sync_service = sync_service.clone();477478	let rpc_frontier_backend = frontier_backend.clone();479480	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCacheTask::new(481		task_manager.spawn_handle(),482		overrides_handle::<_, _, Runtime>(client.clone()),483		50,484		50,485		prometheus_registry.clone(),486	));487488	task_manager.spawn_essential_handle().spawn(489		"frontier-mapping-sync-worker",490		None,491		MappingSyncWorker::new(492			client.import_notification_stream(),493			Duration::new(6, 0),494			client.clone(),495			backend.clone(),496			overrides_handle::<_, _, Runtime>(client.clone()),497			frontier_backend.clone(),498			3,499			0,500			SyncStrategy::Normal,501		)502		.for_each(|()| futures::future::ready(())),503	);504505	#[cfg(feature = "pov-estimate")]506	let rpc_backend = backend.clone();507508	let runtime_id = parachain_config.chain_spec.runtime_id();509510	let rpc_builder = Box::new(move |deny_unsafe, subscription_task_executor| {511		let full_deps = unique_rpc::FullDeps {512			runtime_id: runtime_id.clone(),513514			#[cfg(feature = "pov-estimate")]515			exec_params: uc_rpc::pov_estimate::ExecutorParams {516				wasm_method: parachain_config.wasm_method,517				default_heap_pages: parachain_config.default_heap_pages,518				max_runtime_instances: parachain_config.max_runtime_instances,519				runtime_cache_size: parachain_config.runtime_cache_size,520			},521522			#[cfg(feature = "pov-estimate")]523			backend: rpc_backend.clone(),524525			eth_backend: rpc_frontier_backend.clone(),526			deny_unsafe,527			client: rpc_client.clone(),528			pool: rpc_pool.clone(),529			graph: rpc_pool.pool().clone(),530			// TODO: Unhardcode531			enable_dev_signer: false,532			filter_pool: filter_pool.clone(),533			network: rpc_network.clone(),534			sync: rpc_sync_service.clone(),535			select_chain: select_chain.clone(),536			is_authority: validator,537			// TODO: Unhardcode538			max_past_logs: 10000,539			block_data_cache: block_data_cache.clone(),540			fee_history_cache: fee_history_cache.clone(),541			// TODO: Unhardcode542			fee_history_limit: 2048,543		};544545		unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(546			full_deps,547			subscription_task_executor,548		)549		.map_err(Into::into)550	});551552	sc_service::spawn_tasks(sc_service::SpawnTasksParams {553		rpc_builder,554		client: client.clone(),555		transaction_pool: transaction_pool.clone(),556		task_manager: &mut task_manager,557		config: parachain_config,558		keystore: params.keystore_container.sync_keystore(),559		backend: backend.clone(),560		network: network.clone(),561		sync_service: sync_service.clone(),562		system_rpc_tx,563		telemetry: telemetry.as_mut(),564		tx_handler_controller,565	})?;566567	if let Some(hwbench) = hwbench {568		sc_sysinfo::print_hwbench(&hwbench);569570		if let Some(ref mut telemetry) = telemetry {571			let telemetry_handle = telemetry.handle();572			task_manager.spawn_handle().spawn(573				"telemetry_hwbench",574				None,575				sc_sysinfo::initialize_hwbench_telemetry(telemetry_handle, hwbench),576			);577		}578	}579580	let announce_block = {581		let sync_service = sync_service.clone();582		Arc::new(Box::new(move |hash, data| {583			sync_service.announce_block(hash, data)584		}))585	};586587	let relay_chain_slot_duration = Duration::from_secs(6);588589	let overseer_handle = relay_chain_interface590		.overseer_handle()591		.map_err(|e| sc_service::Error::Application(Box::new(e)))?;592593	if validator {594		let parachain_consensus = build_consensus(595			client.clone(),596			backend.clone(),597			prometheus_registry.as_ref(),598			telemetry.as_ref().map(|t| t.handle()),599			&task_manager,600			relay_chain_interface.clone(),601			transaction_pool,602			sync_service.clone(),603			params.keystore_container.sync_keystore(),604			force_authoring,605		)?;606607		let spawner = task_manager.spawn_handle();608609		let params = StartCollatorParams {610			para_id: id,611			block_status: client.clone(),612			announce_block,613			client: client.clone(),614			task_manager: &mut task_manager,615			spawner,616			parachain_consensus,617			import_queue: import_queue_service,618			collator_key: collator_key.expect("Command line arguments do not allow this. qed"),619			relay_chain_interface,620			relay_chain_slot_duration,621			recovery_handle: Box::new(overseer_handle),622		};623624		start_collator(params).await?;625	} else {626		let params = StartFullNodeParams {627			client: client.clone(),628			announce_block,629			task_manager: &mut task_manager,630			para_id: id,631			import_queue: import_queue_service,632			relay_chain_interface,633			relay_chain_slot_duration,634			recovery_handle: Box::new(overseer_handle),635		};636637		start_full_node(params)?;638	}639640	start_network.start_network();641642	Ok((task_manager, client))643}644645/// Build the import queue for the the parachain runtime.646pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(647	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,648	backend: Arc<FullBackend>,649	config: &Configuration,650	telemetry: Option<TelemetryHandle>,651	task_manager: &TaskManager,652) -> Result<653	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,654	sc_service::Error,655>656where657	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>658		+ Send659		+ Sync660		+ 'static,661	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>662		+ sp_block_builder::BlockBuilder<Block>663		+ sp_consensus_aura::AuraApi<Block, AuraId>664		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,665	ExecutorDispatch: NativeExecutionDispatch + 'static,666{667	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;668669	let block_import = ParachainBlockImport::new(client.clone(), backend.clone());670671	cumulus_client_consensus_aura::import_queue::<672		sp_consensus_aura::sr25519::AuthorityPair,673		_,674		_,675		_,676		_,677		_,678	>(cumulus_client_consensus_aura::ImportQueueParams {679		block_import,680		client: client.clone(),681		create_inherent_data_providers: move |_, _| async move {682			let time = sp_timestamp::InherentDataProvider::from_system_time();683684			let slot =685				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(686					*time,687					slot_duration,688				);689690			Ok((slot, time))691		},692		registry: config.prometheus_registry(),693		spawner: &task_manager.spawn_essential_handle(),694		telemetry,695	})696	.map_err(Into::into)697}698699/// Start a normal parachain node.700pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(701	parachain_config: Configuration,702	polkadot_config: Configuration,703	collator_options: CollatorOptions,704	id: ParaId,705	hwbench: Option<sc_sysinfo::HwBench>,706) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>707where708	Runtime: RuntimeInstance + Send + Sync + 'static,709	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,710	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,711	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>712		+ Send713		+ Sync714		+ 'static,715	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>716		+ fp_rpc::EthereumRuntimeRPCApi<Block>717		+ fp_rpc::ConvertTransactionRuntimeApi<Block>718		+ sp_session::SessionKeys<Block>719		+ sp_block_builder::BlockBuilder<Block>720		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>721		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>722		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>723		+ app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>724		+ up_pov_estimate_rpc::PovEstimateApi<Block>725		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>726		+ sp_api::Metadata<Block>727		+ sp_offchain::OffchainWorkerApi<Block>728		+ cumulus_primitives_core::CollectCollationInfo<Block>729		+ sp_consensus_aura::AuraApi<Block, AuraId>,730	ExecutorDispatch: NativeExecutionDispatch + 'static,731{732	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(733		parachain_config,734		polkadot_config,735		collator_options,736		id,737		parachain_build_import_queue,738		|client,739		 backend,740		 prometheus_registry,741		 telemetry,742		 task_manager,743		 relay_chain_interface,744		 transaction_pool,745		 sync_oracle,746		 keystore,747		 force_authoring| {748			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;749750			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(751				task_manager.spawn_handle(),752				client.clone(),753				transaction_pool,754				prometheus_registry,755				telemetry.clone(),756			);757758			let block_import = ParachainBlockImport::new(client.clone(), backend.clone());759760			Ok(AuraConsensus::build::<761				sp_consensus_aura::sr25519::AuthorityPair,762				_,763				_,764				_,765				_,766				_,767				_,768			>(BuildAuraConsensusParams {769				proposer_factory,770				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {771					let relay_chain_interface = relay_chain_interface.clone();772					async move {773						let parachain_inherent =774						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(775							relay_parent,776							&relay_chain_interface,777							&validation_data,778							id,779						).await;780781						let time = sp_timestamp::InherentDataProvider::from_system_time();782783						let slot =784						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(785							*time,786							slot_duration,787						);788789						let parachain_inherent = parachain_inherent.ok_or_else(|| {790							Box::<dyn std::error::Error + Send + Sync>::from(791								"Failed to create parachain inherent",792							)793						})?;794						Ok((slot, time, parachain_inherent))795					}796				},797				block_import,798				para_client: client,799				backoff_authoring_blocks: Option::<()>::None,800				sync_oracle,801				keystore,802				force_authoring,803				slot_duration,804				// We got around 500ms for proposing805				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),806				telemetry,807				max_block_proposal_slot_portion: None,808			}))809		},810		hwbench,811	)812	.await813}814815fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(816	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,817	_: Arc<FullBackend>,818	config: &Configuration,819	_: Option<TelemetryHandle>,820	task_manager: &TaskManager,821) -> Result<822	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,823	sc_service::Error,824>825where826	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>827		+ Send828		+ Sync829		+ 'static,830	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>831		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,832	ExecutorDispatch: NativeExecutionDispatch + 'static,833{834	Ok(sc_consensus_manual_seal::import_queue(835		Box::new(client.clone()),836		&task_manager.spawn_essential_handle(),837		config.prometheus_registry(),838	))839}840841/// Builds a new development service. This service uses instant seal, and mocks842/// the parachain inherent843pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(844	config: Configuration,845	autoseal_interval: Duration,846) -> sc_service::error::Result<TaskManager>847where848	Runtime: RuntimeInstance + Send + Sync + 'static,849	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,850	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,851	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>852		+ Send853		+ Sync854		+ 'static,855	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>856		+ fp_rpc::EthereumRuntimeRPCApi<Block>857		+ fp_rpc::ConvertTransactionRuntimeApi<Block>858		+ sp_session::SessionKeys<Block>859		+ sp_block_builder::BlockBuilder<Block>860		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>861		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>862		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>863		+ app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>864		+ up_pov_estimate_rpc::PovEstimateApi<Block>865		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>866		+ sp_api::Metadata<Block>867		+ sp_offchain::OffchainWorkerApi<Block>868		+ cumulus_primitives_core::CollectCollationInfo<Block>869		+ sp_consensus_aura::AuraApi<Block, AuraId>,870	ExecutorDispatch: NativeExecutionDispatch + 'static,871{872	use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};873	use fc_consensus::FrontierBlockImport;874	use sc_client_api::HeaderBackend;875876	let sc_service::PartialComponents {877		client,878		backend,879		mut task_manager,880		import_queue,881		keystore_container,882		select_chain: maybe_select_chain,883		transaction_pool,884		other:885			(telemetry, filter_pool, frontier_backend, _telemetry_worker_handle, fee_history_cache),886	} = new_partial::<RuntimeApi, ExecutorDispatch, _>(887		&config,888		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,889	)?;890	let prometheus_registry = config.prometheus_registry().cloned();891892	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCacheTask::new(893		task_manager.spawn_handle(),894		overrides_handle::<_, _, Runtime>(client.clone()),895		50,896		50,897		prometheus_registry.clone(),898	));899900	let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =901		sc_service::build_network(sc_service::BuildNetworkParams {902			config: &config,903			client: client.clone(),904			transaction_pool: transaction_pool.clone(),905			spawn_handle: task_manager.spawn_handle(),906			import_queue,907			block_announce_validator_builder: None,908			warp_sync_params: None,909		})?;910911	if config.offchain_worker.enabled {912		sc_service::build_offchain_workers(913			&config,914			task_manager.spawn_handle(),915			client.clone(),916			network.clone(),917		);918	}919920	let collator = config.role.is_authority();921922	let select_chain = maybe_select_chain.clone();923924	if collator {925		let block_import =926			FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());927928		let env = sc_basic_authorship::ProposerFactory::new(929			task_manager.spawn_handle(),930			client.clone(),931			transaction_pool.clone(),932			prometheus_registry.as_ref(),933			telemetry.as_ref().map(|x| x.handle()),934		);935936		let transactions_commands_stream: Box<937			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,938		> = Box::new(939			transaction_pool940				.pool()941				.validated_pool()942				.import_notification_stream()943				.map(|_| EngineCommand::SealNewBlock {944					create_empty: true,945					finalize: false, // todo:collator finalize true946					parent_hash: None,947					sender: None,948				}),949		);950951		let autoseal_interval = Box::pin(AutosealInterval::new(&config, autoseal_interval));952		let idle_commands_stream: Box<953			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,954		> = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock {955			create_empty: true,956			finalize: false, // todo:collator finalize true957			parent_hash: None,958			sender: None,959		}));960961		let commands_stream = select(transactions_commands_stream, idle_commands_stream);962963		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;964		let client_set_aside_for_cidp = client.clone();965966		task_manager.spawn_essential_handle().spawn_blocking(967			"authorship_task",968			Some("block-authoring"),969			run_manual_seal(ManualSealParams {970				block_import,971				env,972				client: client.clone(),973				pool: transaction_pool.clone(),974				commands_stream,975				select_chain: select_chain.clone(),976				consensus_data_provider: None,977				create_inherent_data_providers: move |block: Hash, ()| {978					let current_para_block = client_set_aside_for_cidp979						.number(block)980						.expect("Header lookup should succeed")981						.expect("Header passed in as parent should be present in backend.");982983					let client_for_xcm = client_set_aside_for_cidp.clone();984					async move {985						let time = sp_timestamp::InherentDataProvider::from_system_time();986987						let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {988							current_para_block,989							relay_offset: 1000,990							relay_blocks_per_para_block: 2,991							para_blocks_per_relay_epoch: 0,992							xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(993								&*client_for_xcm,994								block,995								Default::default(),996								Default::default(),997							),998							relay_randomness_config: (),999							raw_downward_messages: vec![],1000							raw_horizontal_messages: vec![],1001						};10021003						let slot =1004						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(1005							*time,1006							slot_duration,1007						);10081009						Ok((time, slot, mocked_parachain))1010					}1011				},1012			}),1013		);1014	}10151016	task_manager.spawn_essential_handle().spawn(1017		"frontier-mapping-sync-worker",1018		Some("block-authoring"),1019		MappingSyncWorker::new(1020			client.import_notification_stream(),1021			Duration::new(6, 0),1022			client.clone(),1023			backend.clone(),1024			overrides_handle::<_, _, Runtime>(client.clone()),1025			frontier_backend.clone(),1026			3,1027			0,1028			SyncStrategy::Normal,1029		)1030		.for_each(|()| futures::future::ready(())),1031	);10321033	let rpc_client = client.clone();1034	let rpc_pool = transaction_pool.clone();1035	let rpc_network = network.clone();1036	let rpc_sync_service = sync_service.clone();1037	let rpc_frontier_backend = frontier_backend.clone();10381039	#[cfg(feature = "pov-estimate")]1040	let rpc_backend = backend.clone();10411042	let runtime_id = config.chain_spec.runtime_id();10431044	let rpc_builder = Box::new(move |deny_unsafe, subscription_executor| {1045		let full_deps = unique_rpc::FullDeps {1046			runtime_id: runtime_id.clone(),10471048			#[cfg(feature = "pov-estimate")]1049			exec_params: uc_rpc::pov_estimate::ExecutorParams {1050				wasm_method: config.wasm_method,1051				default_heap_pages: config.default_heap_pages,1052				max_runtime_instances: config.max_runtime_instances,1053				runtime_cache_size: config.runtime_cache_size,1054			},10551056			#[cfg(feature = "pov-estimate")]1057			backend: rpc_backend.clone(),1058			eth_backend: rpc_frontier_backend.clone(),1059			deny_unsafe,1060			client: rpc_client.clone(),1061			pool: rpc_pool.clone(),1062			graph: rpc_pool.pool().clone(),1063			// TODO: Unhardcode1064			enable_dev_signer: false,1065			filter_pool: filter_pool.clone(),1066			network: rpc_network.clone(),1067			sync: rpc_sync_service.clone(),1068			select_chain: select_chain.clone(),1069			is_authority: collator,1070			// TODO: Unhardcode1071			max_past_logs: 10000,1072			block_data_cache: block_data_cache.clone(),1073			fee_history_cache: fee_history_cache.clone(),1074			// TODO: Unhardcode1075			fee_history_limit: 2048,1076		};10771078		unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(1079			full_deps,1080			subscription_executor,1081		)1082		.map_err(Into::into)1083	});10841085	sc_service::spawn_tasks(sc_service::SpawnTasksParams {1086		network,1087		sync_service,1088		client,1089		keystore: keystore_container.sync_keystore(),1090		task_manager: &mut task_manager,1091		transaction_pool,1092		rpc_builder,1093		backend,1094		system_rpc_tx,1095		config,1096		telemetry: None,1097		tx_handler_controller,1098	})?;10991100	network_starter.start_network();1101	Ok(task_manager)1102}