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

difftreelog

Merge branch 'develop' into release/opal-v918000

Igor Kozyrev2022-03-29parents: #412ca93 #80d5011.patch.diff
in: master

2 files changed

modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
--- a/node/cli/src/chain_spec.rs
+++ b/node/cli/src/chain_spec.rs
@@ -38,8 +38,12 @@
 pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;
 
 pub enum RuntimeId {
+	#[cfg(feature = "unique-runtime")]
 	Unique,
+
+	#[cfg(feature = "quartz-runtime")]
 	Quartz,
+
 	Opal,
 	Unknown(String),
 }
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_cli::CollatorOptions;38use cumulus_client_network::BlockAnnounceValidator;39use cumulus_primitives_core::ParaId;40use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;41use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};42use cumulus_relay_chain_rpc_interface::RelayChainRPCInterface;4344// Substrate Imports45use sc_client_api::ExecutorProvider;46use sc_executor::NativeElseWasmExecutor;47use sc_executor::NativeExecutionDispatch;48use sc_network::NetworkService;49use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};50use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};51use sp_keystore::SyncCryptoStorePtr;52use sp_runtime::traits::BlakeTwo256;53use substrate_prometheus_endpoint::Registry;54use sc_client_api::BlockchainEvents;5556use polkadot_service::CollatorPair;5758// Frontier Imports59use fc_rpc_core::types::FilterPool;60use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};6162use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6364/// Native executor instance.65pub struct UniqueRuntimeExecutor;66pub struct QuartzRuntimeExecutor;67pub struct OpalRuntimeExecutor;6869#[cfg(feature = "unique-runtime")]70impl NativeExecutionDispatch for UniqueRuntimeExecutor {71	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7273	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {74		unique_runtime::api::dispatch(method, data)75	}7677	fn native_version() -> sc_executor::NativeVersion {78		unique_runtime::native_version()79	}80}8182#[cfg(feature = "quartz-runtime")]83impl NativeExecutionDispatch for QuartzRuntimeExecutor {84	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8586	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {87		quartz_runtime::api::dispatch(method, data)88	}8990	fn native_version() -> sc_executor::NativeVersion {91		quartz_runtime::native_version()92	}93}9495impl NativeExecutionDispatch for OpalRuntimeExecutor {96	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9798	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {99		opal_runtime::api::dispatch(method, data)100	}101102	fn native_version() -> sc_executor::NativeVersion {103		opal_runtime::native_version()104	}105}106107pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {108	let config_dir = config109		.base_path110		.as_ref()111		.map(|base_path| base_path.config_dir(config.chain_spec.id()))112		.unwrap_or_else(|| {113			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())114		});115	let database_dir = config_dir.join("frontier").join("db");116117	Ok(Arc::new(fc_db::Backend::<Block>::new(118		&fc_db::DatabaseSettings {119			source: fc_db::DatabaseSettingsSrc::RocksDb {120				path: database_dir,121				cache_size: 0,122			},123		},124	)?))125}126127type FullClient<RuntimeApi, ExecutorDispatch> =128	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;129type FullBackend = sc_service::TFullBackend<Block>;130type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;131132/// Starts a `ServiceBuilder` for a full service.133///134/// Use this macro if you don't actually need the full service, but just the builder in order to135/// be able to perform chain operations.136#[allow(clippy::type_complexity)]137pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(138	config: &Configuration,139	build_import_queue: BIQ,140) -> Result<141	PartialComponents<142		FullClient<RuntimeApi, ExecutorDispatch>,143		FullBackend,144		FullSelectChain,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 = sc_consensus::LongestChain::new(backend.clone());222223	let transaction_pool = sc_transaction_pool::BasicPool::new_full(224		config.transaction_pool.clone(),225		config.role.is_authority().into(),226		config.prometheus_registry(),227		task_manager.spawn_essential_handle(),228		client.clone(),229	);230231	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));232233	let frontier_backend = open_frontier_backend(config)?;234235	let import_queue = build_import_queue(236		client.clone(),237		config,238		telemetry.as_ref().map(|telemetry| telemetry.handle()),239		&task_manager,240	)?;241	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));242243	let params = PartialComponents {244		backend,245		client,246		import_queue,247		keystore_container,248		task_manager,249		transaction_pool,250		select_chain,251		other: (252			telemetry,253			filter_pool,254			frontier_backend,255			telemetry_worker_handle,256			fee_history_cache,257		),258	};259260	Ok(params)261}262263async fn build_relay_chain_interface(264	polkadot_config: Configuration,265	parachain_config: &Configuration,266	telemetry_worker_handle: Option<TelemetryWorkerHandle>,267	task_manager: &mut TaskManager,268	collator_options: CollatorOptions,269) -> RelayChainResult<(270	Arc<(dyn RelayChainInterface + 'static)>,271	Option<CollatorPair>,272)> {273	match collator_options.relay_chain_rpc_url {274		Some(relay_chain_url) => Ok((275			Arc::new(RelayChainRPCInterface::new(relay_chain_url).await?) as Arc<_>,276			None,277		)),278		None => build_inprocess_relay_chain(279			polkadot_config,280			parachain_config,281			telemetry_worker_handle,282			task_manager,283		),284	}285}286287/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.288///289/// This is the actual implementation that is abstract over the executor and the runtime api.290#[sc_tracing::logging::prefix_logs_with("Parachain")]291async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(292	parachain_config: Configuration,293	polkadot_config: Configuration,294	collator_options: CollatorOptions,295	id: ParaId,296	build_import_queue: BIQ,297	build_consensus: BIC,298) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>299where300	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,301	Runtime: RuntimeInstance + Send + Sync + 'static,302	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,303	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,304	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>305		+ Send306		+ Sync307		+ 'static,308	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>309		+ fp_rpc::EthereumRuntimeRPCApi<Block>310		+ sp_session::SessionKeys<Block>311		+ sp_block_builder::BlockBuilder<Block>312		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>313		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>314		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>315		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>316		+ sp_api::Metadata<Block>317		+ sp_offchain::OffchainWorkerApi<Block>318		+ cumulus_primitives_core::CollectCollationInfo<Block>,319	ExecutorDispatch: NativeExecutionDispatch + 'static,320	BIQ: FnOnce(321		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,322		&Configuration,323		Option<TelemetryHandle>,324		&TaskManager,325	) -> Result<326		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,327		sc_service::Error,328	>,329	BIC: FnOnce(330		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,331		Option<&Registry>,332		Option<TelemetryHandle>,333		&TaskManager,334		Arc<dyn RelayChainInterface>,335		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,336		Arc<NetworkService<Block, Hash>>,337		SyncCryptoStorePtr,338		bool,339	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,340{341	if matches!(parachain_config.role, Role::Light) {342		return Err("Light client not supported!".into());343	}344345	let parachain_config = prepare_node_config(parachain_config);346347	let params =348		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;349	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =350		params.other;351352	let client = params.client.clone();353	let backend = params.backend.clone();354	let mut task_manager = params.task_manager;355356	let (relay_chain_interface, collator_key) = build_relay_chain_interface(357		polkadot_config,358		&parachain_config,359		telemetry_worker_handle,360		&mut task_manager,361		collator_options.clone(),362	)363	.await364	.map_err(|e| match e {365		RelayChainError::ServiceError(polkadot_service::Error::Sub(x)) => x,366		s => s.to_string().into(),367	})?;368369	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);370371	let force_authoring = parachain_config.force_authoring;372	let validator = parachain_config.role.is_authority();373	let prometheus_registry = parachain_config.prometheus_registry().cloned();374	let transaction_pool = params.transaction_pool.clone();375	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);376377	let (network, system_rpc_tx, start_network) =378		sc_service::build_network(sc_service::BuildNetworkParams {379			config: &parachain_config,380			client: client.clone(),381			transaction_pool: transaction_pool.clone(),382			spawn_handle: task_manager.spawn_handle(),383			import_queue: import_queue.clone(),384			block_announce_validator_builder: Some(Box::new(|_| {385				Box::new(block_announce_validator)386			})),387			warp_sync: None,388		})?;389390	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());391	let rpc_client = client.clone();392	let rpc_pool = transaction_pool.clone();393	let select_chain = params.select_chain.clone();394	let rpc_network = network.clone();395396	let rpc_frontier_backend = frontier_backend.clone();397398	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(399		task_manager.spawn_handle(),400		overrides_handle::<_, _, Runtime>(client.clone()),401		50,402		50,403	));404405	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {406		let full_deps = unique_rpc::FullDeps {407			backend: rpc_frontier_backend.clone(),408			deny_unsafe,409			client: rpc_client.clone(),410			pool: rpc_pool.clone(),411			graph: rpc_pool.pool().clone(),412			// TODO: Unhardcode413			enable_dev_signer: false,414			filter_pool: filter_pool.clone(),415			network: rpc_network.clone(),416			select_chain: select_chain.clone(),417			is_authority: validator,418			// TODO: Unhardcode419			max_past_logs: 10000,420			block_data_cache: block_data_cache.clone(),421			fee_history_cache: fee_history_cache.clone(),422			// TODO: Unhardcode423			fee_history_limit: 2048,424		};425426		Ok(427			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(428				full_deps,429				subscription_executor.clone(),430			),431		)432	});433434	task_manager.spawn_essential_handle().spawn(435		"frontier-mapping-sync-worker",436		None,437		MappingSyncWorker::new(438			client.import_notification_stream(),439			Duration::new(6, 0),440			client.clone(),441			backend.clone(),442			frontier_backend.clone(),443			SyncStrategy::Normal,444		)445		.for_each(|()| futures::future::ready(())),446	);447448	sc_service::spawn_tasks(sc_service::SpawnTasksParams {449		rpc_extensions_builder,450		client: client.clone(),451		transaction_pool: transaction_pool.clone(),452		task_manager: &mut task_manager,453		config: parachain_config,454		keystore: params.keystore_container.sync_keystore(),455		backend: backend.clone(),456		network: network.clone(),457		system_rpc_tx,458		telemetry: telemetry.as_mut(),459	})?;460461	let announce_block = {462		let network = network.clone();463		Arc::new(move |hash, data| network.announce_block(hash, data))464	};465466	let relay_chain_slot_duration = Duration::from_secs(6);467468	if validator {469		let parachain_consensus = build_consensus(470			client.clone(),471			prometheus_registry.as_ref(),472			telemetry.as_ref().map(|t| t.handle()),473			&task_manager,474			relay_chain_interface.clone(),475			transaction_pool,476			network,477			params.keystore_container.sync_keystore(),478			force_authoring,479		)?;480481		let spawner = task_manager.spawn_handle();482483		let params = StartCollatorParams {484			para_id: id,485			block_status: client.clone(),486			announce_block,487			client: client.clone(),488			task_manager: &mut task_manager,489			spawner,490			parachain_consensus,491			import_queue,492			collator_key: collator_key.expect("Command line arguments do not allow this. qed"),493			relay_chain_interface,494			relay_chain_slot_duration,495		};496497		start_collator(params).await?;498	} else {499		let params = StartFullNodeParams {500			client: client.clone(),501			announce_block,502			task_manager: &mut task_manager,503			para_id: id,504			import_queue,505			relay_chain_interface,506			relay_chain_slot_duration,507			collator_options,508		};509510		start_full_node(params)?;511	}512513	start_network.start_network();514515	Ok((task_manager, client))516}517518/// Build the import queue for the the parachain runtime.519pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(520	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,521	config: &Configuration,522	telemetry: Option<TelemetryHandle>,523	task_manager: &TaskManager,524) -> Result<525	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,526	sc_service::Error,527>528where529	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>530		+ Send531		+ Sync532		+ 'static,533	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>534		+ sp_block_builder::BlockBuilder<Block>535		+ sp_consensus_aura::AuraApi<Block, AuraId>536		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,537	ExecutorDispatch: NativeExecutionDispatch + 'static,538{539	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;540541	cumulus_client_consensus_aura::import_queue::<542		sp_consensus_aura::sr25519::AuthorityPair,543		_,544		_,545		_,546		_,547		_,548		_,549	>(cumulus_client_consensus_aura::ImportQueueParams {550		block_import: client.clone(),551		client: client.clone(),552		create_inherent_data_providers: move |_, _| async move {553			let time = sp_timestamp::InherentDataProvider::from_system_time();554555			let slot =556				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(557					*time,558					slot_duration,559				);560561			Ok((time, slot))562		},563		registry: config.prometheus_registry(),564		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),565		spawner: &task_manager.spawn_essential_handle(),566		telemetry,567	})568	.map_err(Into::into)569}570571/// Start a normal parachain node.572pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(573	parachain_config: Configuration,574	polkadot_config: Configuration,575	collator_options: CollatorOptions,576	id: ParaId,577) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>578where579	Runtime: RuntimeInstance + Send + Sync + 'static,580	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,581	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,582	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>583		+ Send584		+ Sync585		+ 'static,586	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>587		+ fp_rpc::EthereumRuntimeRPCApi<Block>588		+ sp_session::SessionKeys<Block>589		+ sp_block_builder::BlockBuilder<Block>590		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>591		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>592		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>593		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>594		+ sp_api::Metadata<Block>595		+ sp_offchain::OffchainWorkerApi<Block>596		+ cumulus_primitives_core::CollectCollationInfo<Block>597		+ sp_consensus_aura::AuraApi<Block, AuraId>,598	ExecutorDispatch: NativeExecutionDispatch + 'static,599{600	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(601		parachain_config,602		polkadot_config,603		collator_options,604		id,605		parachain_build_import_queue,606		|client,607		 prometheus_registry,608		 telemetry,609		 task_manager,610		 relay_chain_interface,611		 transaction_pool,612		 sync_oracle,613		 keystore,614		 force_authoring| {615			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;616617			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(618				task_manager.spawn_handle(),619				client.clone(),620				transaction_pool,621				prometheus_registry,622				telemetry.clone(),623			);624625			Ok(AuraConsensus::build::<626				sp_consensus_aura::sr25519::AuthorityPair,627				_,628				_,629				_,630				_,631				_,632				_,633			>(BuildAuraConsensusParams {634				proposer_factory,635				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {636					let relay_chain_interface = relay_chain_interface.clone();637					async move {638						let parachain_inherent =639						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(640							relay_parent,641							&relay_chain_interface,642							&validation_data,643							id,644						).await;645646						let time = sp_timestamp::InherentDataProvider::from_system_time();647648						let slot =649						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(650							*time,651							slot_duration,652						);653654						let parachain_inherent = parachain_inherent.ok_or_else(|| {655							Box::<dyn std::error::Error + Send + Sync>::from(656								"Failed to create parachain inherent",657							)658						})?;659						Ok((time, slot, parachain_inherent))660					}661				},662				block_import: client.clone(),663				para_client: client,664				backoff_authoring_blocks: Option::<()>::None,665				sync_oracle,666				keystore,667				force_authoring,668				slot_duration,669				// We got around 500ms for proposing670				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),671				telemetry,672				max_block_proposal_slot_portion: None,673			}))674		},675	)676	.await677}678679fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(680	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,681	config: &Configuration,682	_: Option<TelemetryHandle>,683	task_manager: &TaskManager,684) -> Result<685	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,686	sc_service::Error,687>688where689	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>690		+ Send691		+ Sync692		+ 'static,693	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>694		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,695	ExecutorDispatch: NativeExecutionDispatch + 'static,696{697	Ok(sc_consensus_manual_seal::import_queue(698		Box::new(client.clone()),699		&task_manager.spawn_essential_handle(),700		config.prometheus_registry(),701	))702}703704/// Builds a new development service. This service uses instant seal, and mocks705/// the parachain inherent706pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(707	config: Configuration,708) -> sc_service::error::Result<TaskManager>709where710	Runtime: RuntimeInstance + Send + Sync + 'static,711	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,712	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,713	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>714		+ Send715		+ Sync716		+ 'static,717	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>718		+ fp_rpc::EthereumRuntimeRPCApi<Block>719		+ sp_session::SessionKeys<Block>720		+ sp_block_builder::BlockBuilder<Block>721		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>722		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>723		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>724		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>725		+ sp_api::Metadata<Block>726		+ sp_offchain::OffchainWorkerApi<Block>727		+ cumulus_primitives_core::CollectCollationInfo<Block>728		+ sp_consensus_aura::AuraApi<Block, AuraId>,729	ExecutorDispatch: NativeExecutionDispatch + 'static,730{731	use futures::Stream;732	use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};733	use fc_consensus::FrontierBlockImport;734	use sc_client_api::HeaderBackend;735736	let sc_service::PartialComponents {737		client,738		backend,739		mut task_manager,740		import_queue,741		keystore_container,742		select_chain: maybe_select_chain,743		transaction_pool,744		other:745			(telemetry, filter_pool, frontier_backend, _telemetry_worker_handle, fee_history_cache),746	} = new_partial::<RuntimeApi, ExecutorDispatch, _>(747		&config,748		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,749	)?;750751	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(752		task_manager.spawn_handle(),753		overrides_handle::<_, _, Runtime>(client.clone()),754		50,755		50,756	));757758	let (network, system_rpc_tx, network_starter) =759		sc_service::build_network(sc_service::BuildNetworkParams {760			config: &config,761			client: client.clone(),762			transaction_pool: transaction_pool.clone(),763			spawn_handle: task_manager.spawn_handle(),764			import_queue,765			block_announce_validator_builder: None,766			warp_sync: None,767		})?;768769	if config.offchain_worker.enabled {770		sc_service::build_offchain_workers(771			&config,772			task_manager.spawn_handle(),773			client.clone(),774			network.clone(),775		);776	}777778	let prometheus_registry = config.prometheus_registry().cloned();779	let collator = config.role.is_authority();780781	let select_chain = maybe_select_chain.clone();782783	if collator {784		let block_import =785			FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());786787		let env = sc_basic_authorship::ProposerFactory::new(788			task_manager.spawn_handle(),789			client.clone(),790			transaction_pool.clone(),791			prometheus_registry.as_ref(),792			telemetry.as_ref().map(|x| x.handle()),793		);794795		let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =796			Box::new(797				// This bit cribbed from the implementation of instant seal.798				transaction_pool799					.pool()800					.validated_pool()801					.import_notification_stream()802					.map(|_| EngineCommand::SealNewBlock {803						create_empty: true, // was false in Moonbeam804						finalize: false,805						parent_hash: None,806						sender: None,807					}),808			);809810		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;811		let client_set_aside_for_cidp = client.clone();812813		task_manager.spawn_essential_handle().spawn_blocking(814			"authorship_task",815			Some("block-authoring"),816			run_manual_seal(ManualSealParams {817				block_import,818				env,819				client: client.clone(),820				pool: transaction_pool.clone(),821				commands_stream,822				select_chain: select_chain.clone(),823				consensus_data_provider: None,824				create_inherent_data_providers: move |block: Hash, ()| {825					let current_para_block = client_set_aside_for_cidp826						.number(block)827						.expect("Header lookup should succeed")828						.expect("Header passed in as parent should be present in backend.");829830					let client_for_xcm = client_set_aside_for_cidp.clone();831					async move {832						let time = sp_timestamp::InherentDataProvider::from_system_time();833834						let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {835							current_para_block,836							relay_offset: 1000,837							relay_blocks_per_para_block: 2,838							xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(839								&*client_for_xcm,840								block,841								Default::default(),842								Default::default(),843							),844							raw_downward_messages: vec![],845							raw_horizontal_messages: vec![],846						};847848						let slot =849						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(850							*time,851							slot_duration,852						);853854						Ok((time, slot, mocked_parachain))855					}856				},857			}),858		);859	}860861	task_manager.spawn_essential_handle().spawn(862		"frontier-mapping-sync-worker",863		Some("block-authoring"),864		MappingSyncWorker::new(865			client.import_notification_stream(),866			Duration::new(6, 0),867			client.clone(),868			backend.clone(),869			frontier_backend.clone(),870			SyncStrategy::Normal,871		)872		.for_each(|()| futures::future::ready(())),873	);874875	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());876	let rpc_client = client.clone();877	let rpc_pool = transaction_pool.clone();878	let rpc_network = network.clone();879	let rpc_frontier_backend = frontier_backend.clone();880	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {881		let full_deps = unique_rpc::FullDeps {882			backend: rpc_frontier_backend.clone(),883			deny_unsafe,884			client: rpc_client.clone(),885			pool: rpc_pool.clone(),886			graph: rpc_pool.pool().clone(),887			// TODO: Unhardcode888			enable_dev_signer: false,889			filter_pool: filter_pool.clone(),890			network: rpc_network.clone(),891			select_chain: select_chain.clone(),892			is_authority: collator,893			// TODO: Unhardcode894			max_past_logs: 10000,895			block_data_cache: block_data_cache.clone(),896			fee_history_cache: fee_history_cache.clone(),897			// TODO: Unhardcode898			fee_history_limit: 2048,899		};900901		Ok(902			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(903				full_deps,904				subscription_executor.clone(),905			),906		)907	});908909	sc_service::spawn_tasks(sc_service::SpawnTasksParams {910		network,911		client,912		keystore: keystore_container.sync_keystore(),913		task_manager: &mut task_manager,914		transaction_pool,915		rpc_extensions_builder,916		backend,917		system_rpc_tx,918		config,919		telemetry: None,920	})?;921922	network_starter.start_network();923	Ok(task_manager)924}
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_cli::CollatorOptions;38use cumulus_client_network::BlockAnnounceValidator;39use cumulus_primitives_core::ParaId;40use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;41use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};42use cumulus_relay_chain_rpc_interface::RelayChainRPCInterface;4344// Substrate Imports45use sc_client_api::ExecutorProvider;46use sc_executor::NativeElseWasmExecutor;47use sc_executor::NativeExecutionDispatch;48use sc_network::NetworkService;49use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};50use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};51use sp_keystore::SyncCryptoStorePtr;52use sp_runtime::traits::BlakeTwo256;53use substrate_prometheus_endpoint::Registry;54use sc_client_api::BlockchainEvents;5556use polkadot_service::CollatorPair;5758// Frontier Imports59use fc_rpc_core::types::FilterPool;60use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};6162use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6364/// Unique native executor instance.65#[cfg(feature = "unique-runtime")]66pub struct UniqueRuntimeExecutor;6768#[cfg(feature = "quartz-runtime")]69/// Quartz native executor instance.7071pub struct QuartzRuntimeExecutor;7273/// Opal native executor instance.74pub struct OpalRuntimeExecutor;7576#[cfg(feature = "unique-runtime")]77impl NativeExecutionDispatch for UniqueRuntimeExecutor {78	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7980	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {81		unique_runtime::api::dispatch(method, data)82	}8384	fn native_version() -> sc_executor::NativeVersion {85		unique_runtime::native_version()86	}87}8889#[cfg(feature = "quartz-runtime")]90impl NativeExecutionDispatch for QuartzRuntimeExecutor {91	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9293	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {94		quartz_runtime::api::dispatch(method, data)95	}9697	fn native_version() -> sc_executor::NativeVersion {98		quartz_runtime::native_version()99	}100}101102impl NativeExecutionDispatch for OpalRuntimeExecutor {103	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;104105	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {106		opal_runtime::api::dispatch(method, data)107	}108109	fn native_version() -> sc_executor::NativeVersion {110		opal_runtime::native_version()111	}112}113114pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {115	let config_dir = config116		.base_path117		.as_ref()118		.map(|base_path| base_path.config_dir(config.chain_spec.id()))119		.unwrap_or_else(|| {120			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())121		});122	let database_dir = config_dir.join("frontier").join("db");123124	Ok(Arc::new(fc_db::Backend::<Block>::new(125		&fc_db::DatabaseSettings {126			source: fc_db::DatabaseSettingsSrc::RocksDb {127				path: database_dir,128				cache_size: 0,129			},130		},131	)?))132}133134type FullClient<RuntimeApi, ExecutorDispatch> =135	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;136type FullBackend = sc_service::TFullBackend<Block>;137type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;138139/// Starts a `ServiceBuilder` for a full service.140///141/// Use this macro if you don't actually need the full service, but just the builder in order to142/// be able to perform chain operations.143#[allow(clippy::type_complexity)]144pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(145	config: &Configuration,146	build_import_queue: BIQ,147) -> Result<148	PartialComponents<149		FullClient<RuntimeApi, ExecutorDispatch>,150		FullBackend,151		FullSelectChain,152		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,153		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,154		(155			Option<Telemetry>,156			Option<FilterPool>,157			Arc<fc_db::Backend<Block>>,158			Option<TelemetryWorkerHandle>,159			FeeHistoryCache,160		),161	>,162	sc_service::Error,163>164where165	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,166	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>167		+ Send168		+ Sync169		+ 'static,170	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,171	ExecutorDispatch: NativeExecutionDispatch + 'static,172	BIQ: FnOnce(173		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,174		&Configuration,175		Option<TelemetryHandle>,176		&TaskManager,177	) -> Result<178		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,179		sc_service::Error,180	>,181{182	let _telemetry = config183		.telemetry_endpoints184		.clone()185		.filter(|x| !x.is_empty())186		.map(|endpoints| -> Result<_, sc_telemetry::Error> {187			let worker = TelemetryWorker::new(16)?;188			let telemetry = worker.handle().new_telemetry(endpoints);189			Ok((worker, telemetry))190		})191		.transpose()?;192193	let telemetry = config194		.telemetry_endpoints195		.clone()196		.filter(|x| !x.is_empty())197		.map(|endpoints| -> Result<_, sc_telemetry::Error> {198			let worker = TelemetryWorker::new(16)?;199			let telemetry = worker.handle().new_telemetry(endpoints);200			Ok((worker, telemetry))201		})202		.transpose()?;203204	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(205		config.wasm_method,206		config.default_heap_pages,207		config.max_runtime_instances,208		config.runtime_cache_size,209	);210211	let (client, backend, keystore_container, task_manager) =212		sc_service::new_full_parts::<Block, RuntimeApi, _>(213			config,214			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),215			executor,216		)?;217	let client = Arc::new(client);218219	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());220221	let telemetry = telemetry.map(|(worker, telemetry)| {222		task_manager223			.spawn_handle()224			.spawn("telemetry", None, worker.run());225		telemetry226	});227228	let select_chain = sc_consensus::LongestChain::new(backend.clone());229230	let transaction_pool = sc_transaction_pool::BasicPool::new_full(231		config.transaction_pool.clone(),232		config.role.is_authority().into(),233		config.prometheus_registry(),234		task_manager.spawn_essential_handle(),235		client.clone(),236	);237238	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));239240	let frontier_backend = open_frontier_backend(config)?;241242	let import_queue = build_import_queue(243		client.clone(),244		config,245		telemetry.as_ref().map(|telemetry| telemetry.handle()),246		&task_manager,247	)?;248	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));249250	let params = PartialComponents {251		backend,252		client,253		import_queue,254		keystore_container,255		task_manager,256		transaction_pool,257		select_chain,258		other: (259			telemetry,260			filter_pool,261			frontier_backend,262			telemetry_worker_handle,263			fee_history_cache,264		),265	};266267	Ok(params)268}269270async fn build_relay_chain_interface(271	polkadot_config: Configuration,272	parachain_config: &Configuration,273	telemetry_worker_handle: Option<TelemetryWorkerHandle>,274	task_manager: &mut TaskManager,275	collator_options: CollatorOptions,276) -> RelayChainResult<(277	Arc<(dyn RelayChainInterface + 'static)>,278	Option<CollatorPair>,279)> {280	match collator_options.relay_chain_rpc_url {281		Some(relay_chain_url) => Ok((282			Arc::new(RelayChainRPCInterface::new(relay_chain_url).await?) as Arc<_>,283			None,284		)),285		None => build_inprocess_relay_chain(286			polkadot_config,287			parachain_config,288			telemetry_worker_handle,289			task_manager,290		),291	}292}293294/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.295///296/// This is the actual implementation that is abstract over the executor and the runtime api.297#[sc_tracing::logging::prefix_logs_with("Parachain")]298async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(299	parachain_config: Configuration,300	polkadot_config: Configuration,301	collator_options: CollatorOptions,302	id: ParaId,303	build_import_queue: BIQ,304	build_consensus: BIC,305) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>306where307	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,308	Runtime: RuntimeInstance + Send + Sync + 'static,309	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,310	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,311	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>312		+ Send313		+ Sync314		+ 'static,315	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>316		+ fp_rpc::EthereumRuntimeRPCApi<Block>317		+ sp_session::SessionKeys<Block>318		+ sp_block_builder::BlockBuilder<Block>319		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>320		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>321		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>322		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>323		+ sp_api::Metadata<Block>324		+ sp_offchain::OffchainWorkerApi<Block>325		+ cumulus_primitives_core::CollectCollationInfo<Block>,326	ExecutorDispatch: NativeExecutionDispatch + 'static,327	BIQ: FnOnce(328		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,329		&Configuration,330		Option<TelemetryHandle>,331		&TaskManager,332	) -> Result<333		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,334		sc_service::Error,335	>,336	BIC: FnOnce(337		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,338		Option<&Registry>,339		Option<TelemetryHandle>,340		&TaskManager,341		Arc<dyn RelayChainInterface>,342		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,343		Arc<NetworkService<Block, Hash>>,344		SyncCryptoStorePtr,345		bool,346	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,347{348	if matches!(parachain_config.role, Role::Light) {349		return Err("Light client not supported!".into());350	}351352	let parachain_config = prepare_node_config(parachain_config);353354	let params =355		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;356	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =357		params.other;358359	let client = params.client.clone();360	let backend = params.backend.clone();361	let mut task_manager = params.task_manager;362363	let (relay_chain_interface, collator_key) = build_relay_chain_interface(364		polkadot_config,365		&parachain_config,366		telemetry_worker_handle,367		&mut task_manager,368		collator_options.clone(),369	)370	.await371	.map_err(|e| match e {372		RelayChainError::ServiceError(polkadot_service::Error::Sub(x)) => x,373		s => s.to_string().into(),374	})?;375376	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);377378	let force_authoring = parachain_config.force_authoring;379	let validator = parachain_config.role.is_authority();380	let prometheus_registry = parachain_config.prometheus_registry().cloned();381	let transaction_pool = params.transaction_pool.clone();382	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);383384	let (network, system_rpc_tx, start_network) =385		sc_service::build_network(sc_service::BuildNetworkParams {386			config: &parachain_config,387			client: client.clone(),388			transaction_pool: transaction_pool.clone(),389			spawn_handle: task_manager.spawn_handle(),390			import_queue: import_queue.clone(),391			block_announce_validator_builder: Some(Box::new(|_| {392				Box::new(block_announce_validator)393			})),394			warp_sync: None,395		})?;396397	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());398	let rpc_client = client.clone();399	let rpc_pool = transaction_pool.clone();400	let select_chain = params.select_chain.clone();401	let rpc_network = network.clone();402403	let rpc_frontier_backend = frontier_backend.clone();404405	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(406		task_manager.spawn_handle(),407		overrides_handle::<_, _, Runtime>(client.clone()),408		50,409		50,410	));411412	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {413		let full_deps = unique_rpc::FullDeps {414			backend: rpc_frontier_backend.clone(),415			deny_unsafe,416			client: rpc_client.clone(),417			pool: rpc_pool.clone(),418			graph: rpc_pool.pool().clone(),419			// TODO: Unhardcode420			enable_dev_signer: false,421			filter_pool: filter_pool.clone(),422			network: rpc_network.clone(),423			select_chain: select_chain.clone(),424			is_authority: validator,425			// TODO: Unhardcode426			max_past_logs: 10000,427			block_data_cache: block_data_cache.clone(),428			fee_history_cache: fee_history_cache.clone(),429			// TODO: Unhardcode430			fee_history_limit: 2048,431		};432433		Ok(434			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(435				full_deps,436				subscription_executor.clone(),437			),438		)439	});440441	task_manager.spawn_essential_handle().spawn(442		"frontier-mapping-sync-worker",443		None,444		MappingSyncWorker::new(445			client.import_notification_stream(),446			Duration::new(6, 0),447			client.clone(),448			backend.clone(),449			frontier_backend.clone(),450			SyncStrategy::Normal,451		)452		.for_each(|()| futures::future::ready(())),453	);454455	sc_service::spawn_tasks(sc_service::SpawnTasksParams {456		rpc_extensions_builder,457		client: client.clone(),458		transaction_pool: transaction_pool.clone(),459		task_manager: &mut task_manager,460		config: parachain_config,461		keystore: params.keystore_container.sync_keystore(),462		backend: backend.clone(),463		network: network.clone(),464		system_rpc_tx,465		telemetry: telemetry.as_mut(),466	})?;467468	let announce_block = {469		let network = network.clone();470		Arc::new(move |hash, data| network.announce_block(hash, data))471	};472473	let relay_chain_slot_duration = Duration::from_secs(6);474475	if validator {476		let parachain_consensus = build_consensus(477			client.clone(),478			prometheus_registry.as_ref(),479			telemetry.as_ref().map(|t| t.handle()),480			&task_manager,481			relay_chain_interface.clone(),482			transaction_pool,483			network,484			params.keystore_container.sync_keystore(),485			force_authoring,486		)?;487488		let spawner = task_manager.spawn_handle();489490		let params = StartCollatorParams {491			para_id: id,492			block_status: client.clone(),493			announce_block,494			client: client.clone(),495			task_manager: &mut task_manager,496			spawner,497			parachain_consensus,498			import_queue,499			collator_key: collator_key.expect("Command line arguments do not allow this. qed"),500			relay_chain_interface,501			relay_chain_slot_duration,502		};503504		start_collator(params).await?;505	} else {506		let params = StartFullNodeParams {507			client: client.clone(),508			announce_block,509			task_manager: &mut task_manager,510			para_id: id,511			import_queue,512			relay_chain_interface,513			relay_chain_slot_duration,514			collator_options,515		};516517		start_full_node(params)?;518	}519520	start_network.start_network();521522	Ok((task_manager, client))523}524525/// Build the import queue for the the parachain runtime.526pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(527	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,528	config: &Configuration,529	telemetry: Option<TelemetryHandle>,530	task_manager: &TaskManager,531) -> Result<532	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,533	sc_service::Error,534>535where536	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>537		+ Send538		+ Sync539		+ 'static,540	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>541		+ sp_block_builder::BlockBuilder<Block>542		+ sp_consensus_aura::AuraApi<Block, AuraId>543		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,544	ExecutorDispatch: NativeExecutionDispatch + 'static,545{546	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;547548	cumulus_client_consensus_aura::import_queue::<549		sp_consensus_aura::sr25519::AuthorityPair,550		_,551		_,552		_,553		_,554		_,555		_,556	>(cumulus_client_consensus_aura::ImportQueueParams {557		block_import: client.clone(),558		client: client.clone(),559		create_inherent_data_providers: move |_, _| async move {560			let time = sp_timestamp::InherentDataProvider::from_system_time();561562			let slot =563				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(564					*time,565					slot_duration,566				);567568			Ok((time, slot))569		},570		registry: config.prometheus_registry(),571		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),572		spawner: &task_manager.spawn_essential_handle(),573		telemetry,574	})575	.map_err(Into::into)576}577578/// Start a normal parachain node.579pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(580	parachain_config: Configuration,581	polkadot_config: Configuration,582	collator_options: CollatorOptions,583	id: ParaId,584) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>585where586	Runtime: RuntimeInstance + Send + Sync + 'static,587	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,588	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,589	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>590		+ Send591		+ Sync592		+ 'static,593	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>594		+ fp_rpc::EthereumRuntimeRPCApi<Block>595		+ sp_session::SessionKeys<Block>596		+ sp_block_builder::BlockBuilder<Block>597		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>598		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>599		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>600		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>601		+ sp_api::Metadata<Block>602		+ sp_offchain::OffchainWorkerApi<Block>603		+ cumulus_primitives_core::CollectCollationInfo<Block>604		+ sp_consensus_aura::AuraApi<Block, AuraId>,605	ExecutorDispatch: NativeExecutionDispatch + 'static,606{607	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(608		parachain_config,609		polkadot_config,610		collator_options,611		id,612		parachain_build_import_queue,613		|client,614		 prometheus_registry,615		 telemetry,616		 task_manager,617		 relay_chain_interface,618		 transaction_pool,619		 sync_oracle,620		 keystore,621		 force_authoring| {622			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;623624			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(625				task_manager.spawn_handle(),626				client.clone(),627				transaction_pool,628				prometheus_registry,629				telemetry.clone(),630			);631632			Ok(AuraConsensus::build::<633				sp_consensus_aura::sr25519::AuthorityPair,634				_,635				_,636				_,637				_,638				_,639				_,640			>(BuildAuraConsensusParams {641				proposer_factory,642				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {643					let relay_chain_interface = relay_chain_interface.clone();644					async move {645						let parachain_inherent =646						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(647							relay_parent,648							&relay_chain_interface,649							&validation_data,650							id,651						).await;652653						let time = sp_timestamp::InherentDataProvider::from_system_time();654655						let slot =656						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(657							*time,658							slot_duration,659						);660661						let parachain_inherent = parachain_inherent.ok_or_else(|| {662							Box::<dyn std::error::Error + Send + Sync>::from(663								"Failed to create parachain inherent",664							)665						})?;666						Ok((time, slot, parachain_inherent))667					}668				},669				block_import: client.clone(),670				para_client: client,671				backoff_authoring_blocks: Option::<()>::None,672				sync_oracle,673				keystore,674				force_authoring,675				slot_duration,676				// We got around 500ms for proposing677				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),678				telemetry,679				max_block_proposal_slot_portion: None,680			}))681		},682	)683	.await684}685686fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(687	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,688	config: &Configuration,689	_: Option<TelemetryHandle>,690	task_manager: &TaskManager,691) -> Result<692	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,693	sc_service::Error,694>695where696	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>697		+ Send698		+ Sync699		+ 'static,700	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>701		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,702	ExecutorDispatch: NativeExecutionDispatch + 'static,703{704	Ok(sc_consensus_manual_seal::import_queue(705		Box::new(client.clone()),706		&task_manager.spawn_essential_handle(),707		config.prometheus_registry(),708	))709}710711/// Builds a new development service. This service uses instant seal, and mocks712/// the parachain inherent713pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(714	config: Configuration,715) -> sc_service::error::Result<TaskManager>716where717	Runtime: RuntimeInstance + Send + Sync + 'static,718	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,719	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,720	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>721		+ Send722		+ Sync723		+ 'static,724	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>725		+ fp_rpc::EthereumRuntimeRPCApi<Block>726		+ sp_session::SessionKeys<Block>727		+ sp_block_builder::BlockBuilder<Block>728		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>729		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>730		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>731		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>732		+ sp_api::Metadata<Block>733		+ sp_offchain::OffchainWorkerApi<Block>734		+ cumulus_primitives_core::CollectCollationInfo<Block>735		+ sp_consensus_aura::AuraApi<Block, AuraId>,736	ExecutorDispatch: NativeExecutionDispatch + 'static,737{738	use futures::Stream;739	use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};740	use fc_consensus::FrontierBlockImport;741	use sc_client_api::HeaderBackend;742743	let sc_service::PartialComponents {744		client,745		backend,746		mut task_manager,747		import_queue,748		keystore_container,749		select_chain: maybe_select_chain,750		transaction_pool,751		other:752			(telemetry, filter_pool, frontier_backend, _telemetry_worker_handle, fee_history_cache),753	} = new_partial::<RuntimeApi, ExecutorDispatch, _>(754		&config,755		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,756	)?;757758	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(759		task_manager.spawn_handle(),760		overrides_handle::<_, _, Runtime>(client.clone()),761		50,762		50,763	));764765	let (network, system_rpc_tx, network_starter) =766		sc_service::build_network(sc_service::BuildNetworkParams {767			config: &config,768			client: client.clone(),769			transaction_pool: transaction_pool.clone(),770			spawn_handle: task_manager.spawn_handle(),771			import_queue,772			block_announce_validator_builder: None,773			warp_sync: None,774		})?;775776	if config.offchain_worker.enabled {777		sc_service::build_offchain_workers(778			&config,779			task_manager.spawn_handle(),780			client.clone(),781			network.clone(),782		);783	}784785	let prometheus_registry = config.prometheus_registry().cloned();786	let collator = config.role.is_authority();787788	let select_chain = maybe_select_chain.clone();789790	if collator {791		let block_import =792			FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());793794		let env = sc_basic_authorship::ProposerFactory::new(795			task_manager.spawn_handle(),796			client.clone(),797			transaction_pool.clone(),798			prometheus_registry.as_ref(),799			telemetry.as_ref().map(|x| x.handle()),800		);801802		let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =803			Box::new(804				// This bit cribbed from the implementation of instant seal.805				transaction_pool806					.pool()807					.validated_pool()808					.import_notification_stream()809					.map(|_| EngineCommand::SealNewBlock {810						create_empty: true, // was false in Moonbeam811						finalize: false,812						parent_hash: None,813						sender: None,814					}),815			);816817		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;818		let client_set_aside_for_cidp = client.clone();819820		task_manager.spawn_essential_handle().spawn_blocking(821			"authorship_task",822			Some("block-authoring"),823			run_manual_seal(ManualSealParams {824				block_import,825				env,826				client: client.clone(),827				pool: transaction_pool.clone(),828				commands_stream,829				select_chain: select_chain.clone(),830				consensus_data_provider: None,831				create_inherent_data_providers: move |block: Hash, ()| {832					let current_para_block = client_set_aside_for_cidp833						.number(block)834						.expect("Header lookup should succeed")835						.expect("Header passed in as parent should be present in backend.");836837					let client_for_xcm = client_set_aside_for_cidp.clone();838					async move {839						let time = sp_timestamp::InherentDataProvider::from_system_time();840841						let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {842							current_para_block,843							relay_offset: 1000,844							relay_blocks_per_para_block: 2,845							xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(846								&*client_for_xcm,847								block,848								Default::default(),849								Default::default(),850							),851							raw_downward_messages: vec![],852							raw_horizontal_messages: vec![],853						};854855						let slot =856						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(857							*time,858							slot_duration,859						);860861						Ok((time, slot, mocked_parachain))862					}863				},864			}),865		);866	}867868	task_manager.spawn_essential_handle().spawn(869		"frontier-mapping-sync-worker",870		Some("block-authoring"),871		MappingSyncWorker::new(872			client.import_notification_stream(),873			Duration::new(6, 0),874			client.clone(),875			backend.clone(),876			frontier_backend.clone(),877			SyncStrategy::Normal,878		)879		.for_each(|()| futures::future::ready(())),880	);881882	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());883	let rpc_client = client.clone();884	let rpc_pool = transaction_pool.clone();885	let rpc_network = network.clone();886	let rpc_frontier_backend = frontier_backend.clone();887	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {888		let full_deps = unique_rpc::FullDeps {889			backend: rpc_frontier_backend.clone(),890			deny_unsafe,891			client: rpc_client.clone(),892			pool: rpc_pool.clone(),893			graph: rpc_pool.pool().clone(),894			// TODO: Unhardcode895			enable_dev_signer: false,896			filter_pool: filter_pool.clone(),897			network: rpc_network.clone(),898			select_chain: select_chain.clone(),899			is_authority: collator,900			// TODO: Unhardcode901			max_past_logs: 10000,902			block_data_cache: block_data_cache.clone(),903			fee_history_cache: fee_history_cache.clone(),904			// TODO: Unhardcode905			fee_history_limit: 2048,906		};907908		Ok(909			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(910				full_deps,911				subscription_executor.clone(),912			),913		)914	});915916	sc_service::spawn_tasks(sc_service::SpawnTasksParams {917		network,918		client,919		keystore: keystore_container.sync_keystore(),920		task_manager: &mut task_manager,921		transaction_pool,922		rpc_extensions_builder,923		backend,924		system_rpc_tx,925		config,926		telemetry: None,927	})?;928929	network_starter.start_network();930	Ok(task_manager)931}