git.delta.rocks / unique-network / refs/commits / d53bf3cc5d4a

difftreelog

source

node/cli/src/service.rs19.6 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//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.1819// std20use std::sync::Arc;21use std::sync::Mutex;22use std::collections::BTreeMap;23use std::time::Duration;24use fc_rpc_core::types::FeeHistoryCache;25use futures::StreamExt;2627use unique_rpc::overrides_handle;2829use serde::{Serialize, Deserialize};3031// Cumulus Imports32use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};33use cumulus_client_consensus_common::ParachainConsensus;34use cumulus_client_service::{35	prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,36};37use cumulus_client_network::BlockAnnounceValidator;38use cumulus_primitives_core::ParaId;39use cumulus_relay_chain_interface::RelayChainInterface;40use cumulus_relay_chain_local::build_relay_chain_interface;4142// Substrate Imports43use sc_client_api::ExecutorProvider;44use sc_executor::NativeElseWasmExecutor;45use sc_executor::NativeExecutionDispatch;46use sc_network::NetworkService;47use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};48use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};49use sp_consensus::SlotData;50use sp_keystore::SyncCryptoStorePtr;51use sp_runtime::traits::BlakeTwo256;52use substrate_prometheus_endpoint::Registry;53use sc_client_api::BlockchainEvents;5455// Frontier Imports56use fc_rpc_core::types::FilterPool;57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};5859use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6061/// Native executor instance.62pub struct UniqueRuntimeExecutor;63pub struct QuartzRuntimeExecutor;64pub struct OpalRuntimeExecutor;6566#[cfg(feature = "unique-runtime")]67impl NativeExecutionDispatch for UniqueRuntimeExecutor {68	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;6970	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {71		unique_runtime::api::dispatch(method, data)72	}7374	fn native_version() -> sc_executor::NativeVersion {75		unique_runtime::native_version()76	}77}7879#[cfg(feature = "quartz-runtime")]80impl NativeExecutionDispatch for QuartzRuntimeExecutor {81	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8283	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {84		quartz_runtime::api::dispatch(method, data)85	}8687	fn native_version() -> sc_executor::NativeVersion {88		quartz_runtime::native_version()89	}90}9192impl NativeExecutionDispatch for OpalRuntimeExecutor {93	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9495	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {96		opal_runtime::api::dispatch(method, data)97	}9899	fn native_version() -> sc_executor::NativeVersion {100		opal_runtime::native_version()101	}102}103104pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {105	let config_dir = config106		.base_path107		.as_ref()108		.map(|base_path| base_path.config_dir(config.chain_spec.id()))109		.unwrap_or_else(|| {110			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())111		});112	let database_dir = config_dir.join("frontier").join("db");113114	Ok(Arc::new(fc_db::Backend::<Block>::new(115		&fc_db::DatabaseSettings {116			source: fc_db::DatabaseSettingsSrc::RocksDb {117				path: database_dir,118				cache_size: 0,119			},120		},121	)?))122}123124type FullClient<RuntimeApi, ExecutorDispatch> =125	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;126type FullBackend = sc_service::TFullBackend<Block>;127type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;128129/// Starts a `ServiceBuilder` for a full service.130///131/// Use this macro if you don't actually need the full service, but just the builder in order to132/// be able to perform chain operations.133#[allow(clippy::type_complexity)]134pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(135	config: &Configuration,136	build_import_queue: BIQ,137) -> Result<138	PartialComponents<139		FullClient<RuntimeApi, ExecutorDispatch>,140		FullBackend,141		FullSelectChain,142		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,143		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,144		(145			Option<Telemetry>,146			Option<FilterPool>,147			Arc<fc_db::Backend<Block>>,148			Option<TelemetryWorkerHandle>,149			FeeHistoryCache,150		),151	>,152	sc_service::Error,153>154where155	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,156	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>157		+ Send158		+ Sync159		+ 'static,160	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,161	ExecutorDispatch: NativeExecutionDispatch + 'static,162	BIQ: FnOnce(163		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,164		&Configuration,165		Option<TelemetryHandle>,166		&TaskManager,167	) -> Result<168		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,169		sc_service::Error,170	>,171{172	let _telemetry = config173		.telemetry_endpoints174		.clone()175		.filter(|x| !x.is_empty())176		.map(|endpoints| -> Result<_, sc_telemetry::Error> {177			let worker = TelemetryWorker::new(16)?;178			let telemetry = worker.handle().new_telemetry(endpoints);179			Ok((worker, telemetry))180		})181		.transpose()?;182183	let telemetry = config184		.telemetry_endpoints185		.clone()186		.filter(|x| !x.is_empty())187		.map(|endpoints| -> Result<_, sc_telemetry::Error> {188			let worker = TelemetryWorker::new(16)?;189			let telemetry = worker.handle().new_telemetry(endpoints);190			Ok((worker, telemetry))191		})192		.transpose()?;193194	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(195		config.wasm_method,196		config.default_heap_pages,197		config.max_runtime_instances,198		config.runtime_cache_size,199	);200201	let (client, backend, keystore_container, task_manager) =202		sc_service::new_full_parts::<Block, RuntimeApi, _>(203			config,204			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),205			executor,206		)?;207	let client = Arc::new(client);208209	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());210211	let telemetry = telemetry.map(|(worker, telemetry)| {212		task_manager213			.spawn_handle()214			.spawn("telemetry", None, worker.run());215		telemetry216	});217218	let select_chain = sc_consensus::LongestChain::new(backend.clone());219220	let transaction_pool = sc_transaction_pool::BasicPool::new_full(221		config.transaction_pool.clone(),222		config.role.is_authority().into(),223		config.prometheus_registry(),224		task_manager.spawn_essential_handle(),225		client.clone(),226	);227228	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));229230	let frontier_backend = open_frontier_backend(config)?;231232	let import_queue = build_import_queue(233		client.clone(),234		config,235		telemetry.as_ref().map(|telemetry| telemetry.handle()),236		&task_manager,237	)?;238	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));239240	let params = PartialComponents {241		backend,242		client,243		import_queue,244		keystore_container,245		task_manager,246		transaction_pool,247		select_chain,248		other: (249			telemetry,250			filter_pool,251			frontier_backend,252			telemetry_worker_handle,253			fee_history_cache,254		),255	};256257	Ok(params)258}259260/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.261///262/// This is the actual implementation that is abstract over the executor and the runtime api.263#[sc_tracing::logging::prefix_logs_with("Parachain")]264async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(265	parachain_config: Configuration,266	polkadot_config: Configuration,267	id: ParaId,268	build_import_queue: BIQ,269	build_consensus: BIC,270) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>271where272	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,273	Runtime: RuntimeInstance + Send + Sync + 'static,274	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,275	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,276	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>277		+ Send278		+ Sync279		+ 'static,280	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>281		+ fp_rpc::EthereumRuntimeRPCApi<Block>282		+ sp_session::SessionKeys<Block>283		+ sp_block_builder::BlockBuilder<Block>284		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>285		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>286		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>287		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>288		+ sp_api::Metadata<Block>289		+ sp_offchain::OffchainWorkerApi<Block>290		+ cumulus_primitives_core::CollectCollationInfo<Block>,291	ExecutorDispatch: NativeExecutionDispatch + 'static,292	BIQ: FnOnce(293		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,294		&Configuration,295		Option<TelemetryHandle>,296		&TaskManager,297	) -> Result<298		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,299		sc_service::Error,300	>,301	BIC: FnOnce(302		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,303		Option<&Registry>,304		Option<TelemetryHandle>,305		&TaskManager,306		Arc<dyn RelayChainInterface>,307		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,308		Arc<NetworkService<Block, Hash>>,309		SyncCryptoStorePtr,310		bool,311	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,312{313	if matches!(parachain_config.role, Role::Light) {314		return Err("Light client not supported!".into());315	}316317	let parachain_config = prepare_node_config(parachain_config);318319	let params =320		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;321	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =322		params.other;323324	let client = params.client.clone();325	let backend = params.backend.clone();326	let mut task_manager = params.task_manager;327328	let (relay_chain_interface, collator_key) =329		build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)330			.map_err(|e| match e {331				polkadot_service::Error::Sub(x) => x,332				s => format!("{}", s).into(),333			})?;334335	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);336337	let force_authoring = parachain_config.force_authoring;338	let validator = parachain_config.role.is_authority();339	let prometheus_registry = parachain_config.prometheus_registry().cloned();340	let transaction_pool = params.transaction_pool.clone();341	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);342343	let (network, system_rpc_tx, start_network) =344		sc_service::build_network(sc_service::BuildNetworkParams {345			config: &parachain_config,346			client: client.clone(),347			transaction_pool: transaction_pool.clone(),348			spawn_handle: task_manager.spawn_handle(),349			import_queue: import_queue.clone(),350			block_announce_validator_builder: Some(Box::new(|_| {351				Box::new(block_announce_validator)352			})),353			warp_sync: None,354		})?;355356	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());357	let rpc_client = client.clone();358	let rpc_pool = transaction_pool.clone();359	let select_chain = params.select_chain.clone();360	let rpc_network = network.clone();361362	let rpc_frontier_backend = frontier_backend.clone();363364	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(365		task_manager.spawn_handle(),366		overrides_handle::<_, _, Runtime>(client.clone()),367		50,368		50,369	));370371	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {372		let full_deps = unique_rpc::FullDeps {373			backend: rpc_frontier_backend.clone(),374			deny_unsafe,375			client: rpc_client.clone(),376			pool: rpc_pool.clone(),377			graph: rpc_pool.pool().clone(),378			// TODO: Unhardcode379			enable_dev_signer: false,380			filter_pool: filter_pool.clone(),381			network: rpc_network.clone(),382			select_chain: select_chain.clone(),383			is_authority: validator,384			// TODO: Unhardcode385			max_past_logs: 10000,386			block_data_cache: block_data_cache.clone(),387			fee_history_cache: fee_history_cache.clone(),388			// TODO: Unhardcode389			fee_history_limit: 2048,390		};391392		Ok(393			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(394				full_deps,395				subscription_executor.clone(),396			),397		)398	});399400	task_manager.spawn_essential_handle().spawn(401		"frontier-mapping-sync-worker",402		None,403		MappingSyncWorker::new(404			client.import_notification_stream(),405			Duration::new(6, 0),406			client.clone(),407			backend.clone(),408			frontier_backend.clone(),409			SyncStrategy::Normal,410		)411		.for_each(|()| futures::future::ready(())),412	);413414	sc_service::spawn_tasks(sc_service::SpawnTasksParams {415		rpc_extensions_builder,416		client: client.clone(),417		transaction_pool: transaction_pool.clone(),418		task_manager: &mut task_manager,419		config: parachain_config,420		keystore: params.keystore_container.sync_keystore(),421		backend: backend.clone(),422		network: network.clone(),423		system_rpc_tx,424		telemetry: telemetry.as_mut(),425	})?;426427	let announce_block = {428		let network = network.clone();429		Arc::new(move |hash, data| network.announce_block(hash, data))430	};431432	let relay_chain_slot_duration = Duration::from_secs(6);433434	if validator {435		let parachain_consensus = build_consensus(436			client.clone(),437			prometheus_registry.as_ref(),438			telemetry.as_ref().map(|t| t.handle()),439			&task_manager,440			relay_chain_interface.clone(),441			transaction_pool,442			network,443			params.keystore_container.sync_keystore(),444			force_authoring,445		)?;446447		let spawner = task_manager.spawn_handle();448449		let params = StartCollatorParams {450			para_id: id,451			block_status: client.clone(),452			announce_block,453			client: client.clone(),454			task_manager: &mut task_manager,455			spawner,456			parachain_consensus,457			import_queue,458			collator_key,459			relay_chain_interface,460			relay_chain_slot_duration,461		};462463		start_collator(params).await?;464	} else {465		let params = StartFullNodeParams {466			client: client.clone(),467			announce_block,468			task_manager: &mut task_manager,469			para_id: id,470			import_queue,471			relay_chain_interface,472			relay_chain_slot_duration,473		};474475		start_full_node(params)?;476	}477478	start_network.start_network();479480	Ok((task_manager, client))481}482483/// Build the import queue for the the parachain runtime.484pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(485	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,486	config: &Configuration,487	telemetry: Option<TelemetryHandle>,488	task_manager: &TaskManager,489) -> Result<490	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,491	sc_service::Error,492>493where494	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>495		+ Send496		+ Sync497		+ 'static,498	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>499		+ sp_block_builder::BlockBuilder<Block>500		+ sp_consensus_aura::AuraApi<Block, AuraId>501		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,502	ExecutorDispatch: NativeExecutionDispatch + 'static,503{504	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;505506	cumulus_client_consensus_aura::import_queue::<507		sp_consensus_aura::sr25519::AuthorityPair,508		_,509		_,510		_,511		_,512		_,513		_,514	>(cumulus_client_consensus_aura::ImportQueueParams {515		block_import: client.clone(),516		client: client.clone(),517		create_inherent_data_providers: move |_, _| async move {518			let time = sp_timestamp::InherentDataProvider::from_system_time();519520			let slot =521				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(522					*time,523					slot_duration.slot_duration(),524				);525526			Ok((time, slot))527		},528		registry: config.prometheus_registry(),529		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),530		spawner: &task_manager.spawn_essential_handle(),531		telemetry,532	})533	.map_err(Into::into)534}535536/// Start a normal parachain node.537pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(538	parachain_config: Configuration,539	polkadot_config: Configuration,540	id: ParaId,541) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>542where543	Runtime: RuntimeInstance + Send + Sync + 'static,544	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,545	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,546	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>547		+ Send548		+ Sync549		+ 'static,550	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>551		+ fp_rpc::EthereumRuntimeRPCApi<Block>552		+ sp_session::SessionKeys<Block>553		+ sp_block_builder::BlockBuilder<Block>554		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>555		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>556		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>557		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>558		+ sp_api::Metadata<Block>559		+ sp_offchain::OffchainWorkerApi<Block>560		+ cumulus_primitives_core::CollectCollationInfo<Block>561		+ sp_consensus_aura::AuraApi<Block, AuraId>,562	ExecutorDispatch: NativeExecutionDispatch + 'static,563{564	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(565		parachain_config,566		polkadot_config,567		id,568		parachain_build_import_queue,569		|client,570		 prometheus_registry,571		 telemetry,572		 task_manager,573		 relay_chain_interface,574		 transaction_pool,575		 sync_oracle,576		 keystore,577		 force_authoring| {578			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;579580			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(581				task_manager.spawn_handle(),582				client.clone(),583				transaction_pool,584				prometheus_registry,585				telemetry.clone(),586			);587588			Ok(AuraConsensus::build::<589				sp_consensus_aura::sr25519::AuthorityPair,590				_,591				_,592				_,593				_,594				_,595				_,596			>(BuildAuraConsensusParams {597				proposer_factory,598				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {599					let relay_chain_interface = relay_chain_interface.clone();600					async move {601						let parachain_inherent =602						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(603							relay_parent,604							&relay_chain_interface,605							&validation_data,606							id,607						).await;608609						let time = sp_timestamp::InherentDataProvider::from_system_time();610611						let slot =612						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(613							*time,614							slot_duration.slot_duration(),615						);616617						let parachain_inherent = parachain_inherent.ok_or_else(|| {618							Box::<dyn std::error::Error + Send + Sync>::from(619								"Failed to create parachain inherent",620							)621						})?;622						Ok((time, slot, parachain_inherent))623					}624				},625				block_import: client.clone(),626				para_client: client,627				backoff_authoring_blocks: Option::<()>::None,628				sync_oracle,629				keystore,630				force_authoring,631				slot_duration: *slot_duration,632				// We got around 500ms for proposing633				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),634				telemetry,635				max_block_proposal_slot_portion: None,636			}))637		},638	)639	.await640}