git.delta.rocks / unique-network / refs/commits / 6b7300defa13

difftreelog

source

node/cli/src/service.rs19.9 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};5859// Runtime type overrides60type BlockNumber = u32;61type Header = sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256>;62pub type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;63type Hash = sp_core::H256;6465use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index};6667/// Native executor instance.68pub struct UniqueRuntimeExecutor;69pub struct QuartzRuntimeExecutor;70pub struct OpalRuntimeExecutor;7172#[cfg(feature = "unique-runtime")]73impl NativeExecutionDispatch for UniqueRuntimeExecutor {74	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7576	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {77		unique_runtime::api::dispatch(method, data)78	}7980	fn native_version() -> sc_executor::NativeVersion {81		unique_runtime::native_version()82	}83}8485#[cfg(feature = "quartz-runtime")]86impl NativeExecutionDispatch for QuartzRuntimeExecutor {87	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8889	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {90		quartz_runtime::api::dispatch(method, data)91	}9293	fn native_version() -> sc_executor::NativeVersion {94		quartz_runtime::native_version()95	}96}9798impl NativeExecutionDispatch for OpalRuntimeExecutor {99	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;100101	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {102		opal_runtime::api::dispatch(method, data)103	}104105	fn native_version() -> sc_executor::NativeVersion {106		opal_runtime::native_version()107	}108}109110pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {111	let config_dir = config112		.base_path113		.as_ref()114		.map(|base_path| base_path.config_dir(config.chain_spec.id()))115		.unwrap_or_else(|| {116			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())117		});118	let database_dir = config_dir.join("frontier").join("db");119120	Ok(Arc::new(fc_db::Backend::<Block>::new(121		&fc_db::DatabaseSettings {122			source: fc_db::DatabaseSettingsSrc::RocksDb {123				path: database_dir,124				cache_size: 0,125			},126		},127	)?))128}129130type FullClient<RuntimeApi, ExecutorDispatch> =131	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;132type FullBackend = sc_service::TFullBackend<Block>;133type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;134135/// Starts a `ServiceBuilder` for a full service.136///137/// Use this macro if you don't actually need the full service, but just the builder in order to138/// be able to perform chain operations.139#[allow(clippy::type_complexity)]140pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(141	config: &Configuration,142	build_import_queue: BIQ,143) -> Result<144	PartialComponents<145		FullClient<RuntimeApi, ExecutorDispatch>,146		FullBackend,147		FullSelectChain,148		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,149		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,150		(151			Option<Telemetry>,152			Option<FilterPool>,153			Arc<fc_db::Backend<Block>>,154			Option<TelemetryWorkerHandle>,155			FeeHistoryCache,156		),157	>,158	sc_service::Error,159>160where161	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,162	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>163		+ Send164		+ Sync165		+ 'static,166	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,167	ExecutorDispatch: NativeExecutionDispatch + 'static,168	BIQ: FnOnce(169		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,170		&Configuration,171		Option<TelemetryHandle>,172		&TaskManager,173	) -> Result<174		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,175		sc_service::Error,176	>,177{178	let _telemetry = config179		.telemetry_endpoints180		.clone()181		.filter(|x| !x.is_empty())182		.map(|endpoints| -> Result<_, sc_telemetry::Error> {183			let worker = TelemetryWorker::new(16)?;184			let telemetry = worker.handle().new_telemetry(endpoints);185			Ok((worker, telemetry))186		})187		.transpose()?;188189	let telemetry = config190		.telemetry_endpoints191		.clone()192		.filter(|x| !x.is_empty())193		.map(|endpoints| -> Result<_, sc_telemetry::Error> {194			let worker = TelemetryWorker::new(16)?;195			let telemetry = worker.handle().new_telemetry(endpoints);196			Ok((worker, telemetry))197		})198		.transpose()?;199200	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(201		config.wasm_method,202		config.default_heap_pages,203		config.max_runtime_instances,204		config.runtime_cache_size,205	);206207	let (client, backend, keystore_container, task_manager) =208		sc_service::new_full_parts::<Block, RuntimeApi, _>(209			config,210			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),211			executor,212		)?;213	let client = Arc::new(client);214215	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());216217	let telemetry = telemetry.map(|(worker, telemetry)| {218		task_manager219			.spawn_handle()220			.spawn("telemetry", None, worker.run());221		telemetry222	});223224	let select_chain = sc_consensus::LongestChain::new(backend.clone());225226	let transaction_pool = sc_transaction_pool::BasicPool::new_full(227		config.transaction_pool.clone(),228		config.role.is_authority().into(),229		config.prometheus_registry(),230		task_manager.spawn_essential_handle(),231		client.clone(),232	);233234	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));235236	let frontier_backend = open_frontier_backend(config)?;237238	let import_queue = build_import_queue(239		client.clone(),240		config,241		telemetry.as_ref().map(|telemetry| telemetry.handle()),242		&task_manager,243	)?;244	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));245246	let params = PartialComponents {247		backend,248		client,249		import_queue,250		keystore_container,251		task_manager,252		transaction_pool,253		select_chain,254		other: (255			telemetry,256			filter_pool,257			frontier_backend,258			telemetry_worker_handle,259			fee_history_cache,260		),261	};262263	Ok(params)264}265266/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.267///268/// This is the actual implementation that is abstract over the executor and the runtime api.269#[sc_tracing::logging::prefix_logs_with("Parachain")]270async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(271	parachain_config: Configuration,272	polkadot_config: Configuration,273	id: ParaId,274	build_import_queue: BIQ,275	build_consensus: BIC,276) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>277where278	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,279	Runtime: RuntimeInstance + Send + Sync + 'static,280	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,281	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,282	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>283		+ Send284		+ Sync285		+ 'static,286	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>287		+ fp_rpc::EthereumRuntimeRPCApi<Block>288		+ sp_session::SessionKeys<Block>289		+ sp_block_builder::BlockBuilder<Block>290		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>291		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>292		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>293		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>294		+ sp_api::Metadata<Block>295		+ sp_offchain::OffchainWorkerApi<Block>296		+ cumulus_primitives_core::CollectCollationInfo<Block>,297	ExecutorDispatch: NativeExecutionDispatch + 'static,298	BIQ: FnOnce(299		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,300		&Configuration,301		Option<TelemetryHandle>,302		&TaskManager,303	) -> Result<304		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,305		sc_service::Error,306	>,307	BIC: FnOnce(308		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,309		Option<&Registry>,310		Option<TelemetryHandle>,311		&TaskManager,312		Arc<dyn RelayChainInterface>,313		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,314		Arc<NetworkService<Block, Hash>>,315		SyncCryptoStorePtr,316		bool,317	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,318{319	if matches!(parachain_config.role, Role::Light) {320		return Err("Light client not supported!".into());321	}322323	let parachain_config = prepare_node_config(parachain_config);324325	let params =326		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;327	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =328		params.other;329330	let client = params.client.clone();331	let backend = params.backend.clone();332	let mut task_manager = params.task_manager;333334	let (relay_chain_interface, collator_key) =335		build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)336			.map_err(|e| match e {337				polkadot_service::Error::Sub(x) => x,338				s => format!("{}", s).into(),339			})?;340341	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);342343	let force_authoring = parachain_config.force_authoring;344	let validator = parachain_config.role.is_authority();345	let prometheus_registry = parachain_config.prometheus_registry().cloned();346	let transaction_pool = params.transaction_pool.clone();347	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);348349	let (network, system_rpc_tx, start_network) =350		sc_service::build_network(sc_service::BuildNetworkParams {351			config: &parachain_config,352			client: client.clone(),353			transaction_pool: transaction_pool.clone(),354			spawn_handle: task_manager.spawn_handle(),355			import_queue: import_queue.clone(),356			block_announce_validator_builder: Some(Box::new(|_| {357				Box::new(block_announce_validator)358			})),359			warp_sync: None,360		})?;361362	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());363	let rpc_client = client.clone();364	let rpc_pool = transaction_pool.clone();365	let select_chain = params.select_chain.clone();366	let rpc_network = network.clone();367368	let rpc_frontier_backend = frontier_backend.clone();369370	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(371		task_manager.spawn_handle(),372		overrides_handle::<_, _, Runtime>(client.clone()),373		50,374		50,375	));376377	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {378		let full_deps = unique_rpc::FullDeps {379			backend: rpc_frontier_backend.clone(),380			deny_unsafe,381			client: rpc_client.clone(),382			pool: rpc_pool.clone(),383			graph: rpc_pool.pool().clone(),384			// TODO: Unhardcode385			enable_dev_signer: false,386			filter_pool: filter_pool.clone(),387			network: rpc_network.clone(),388			select_chain: select_chain.clone(),389			is_authority: validator,390			// TODO: Unhardcode391			max_past_logs: 10000,392			block_data_cache: block_data_cache.clone(),393			fee_history_cache: fee_history_cache.clone(),394			// TODO: Unhardcode395			fee_history_limit: 2048,396		};397398		Ok(399			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(400				full_deps,401				subscription_executor.clone(),402			),403		)404	});405406	task_manager.spawn_essential_handle().spawn(407		"frontier-mapping-sync-worker",408		None,409		MappingSyncWorker::new(410			client.import_notification_stream(),411			Duration::new(6, 0),412			client.clone(),413			backend.clone(),414			frontier_backend.clone(),415			SyncStrategy::Normal,416		)417		.for_each(|()| futures::future::ready(())),418	);419420	sc_service::spawn_tasks(sc_service::SpawnTasksParams {421		rpc_extensions_builder,422		client: client.clone(),423		transaction_pool: transaction_pool.clone(),424		task_manager: &mut task_manager,425		config: parachain_config,426		keystore: params.keystore_container.sync_keystore(),427		backend: backend.clone(),428		network: network.clone(),429		system_rpc_tx,430		telemetry: telemetry.as_mut(),431	})?;432433	let announce_block = {434		let network = network.clone();435		Arc::new(move |hash, data| network.announce_block(hash, data))436	};437438	let relay_chain_slot_duration = Duration::from_secs(6);439440	if validator {441		let parachain_consensus = build_consensus(442			client.clone(),443			prometheus_registry.as_ref(),444			telemetry.as_ref().map(|t| t.handle()),445			&task_manager,446			relay_chain_interface.clone(),447			transaction_pool,448			network,449			params.keystore_container.sync_keystore(),450			force_authoring,451		)?;452453		let spawner = task_manager.spawn_handle();454455		let params = StartCollatorParams {456			para_id: id,457			block_status: client.clone(),458			announce_block,459			client: client.clone(),460			task_manager: &mut task_manager,461			spawner,462			parachain_consensus,463			import_queue,464			collator_key,465			relay_chain_interface,466			relay_chain_slot_duration,467		};468469		start_collator(params).await?;470	} else {471		let params = StartFullNodeParams {472			client: client.clone(),473			announce_block,474			task_manager: &mut task_manager,475			para_id: id,476			import_queue,477			relay_chain_interface,478			relay_chain_slot_duration,479		};480481		start_full_node(params)?;482	}483484	start_network.start_network();485486	Ok((task_manager, client))487}488489/// Build the import queue for the the parachain runtime.490pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(491	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,492	config: &Configuration,493	telemetry: Option<TelemetryHandle>,494	task_manager: &TaskManager,495) -> Result<496	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,497	sc_service::Error,498>499where500	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>501		+ Send502		+ Sync503		+ 'static,504	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>505		+ sp_block_builder::BlockBuilder<Block>506		+ sp_consensus_aura::AuraApi<Block, AuraId>507		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,508	ExecutorDispatch: NativeExecutionDispatch + 'static,509{510	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;511512	cumulus_client_consensus_aura::import_queue::<513		sp_consensus_aura::sr25519::AuthorityPair,514		_,515		_,516		_,517		_,518		_,519		_,520	>(cumulus_client_consensus_aura::ImportQueueParams {521		block_import: client.clone(),522		client: client.clone(),523		create_inherent_data_providers: move |_, _| async move {524			let time = sp_timestamp::InherentDataProvider::from_system_time();525526			let slot =527				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(528					*time,529					slot_duration.slot_duration(),530				);531532			Ok((time, slot))533		},534		registry: config.prometheus_registry(),535		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),536		spawner: &task_manager.spawn_essential_handle(),537		telemetry,538	})539	.map_err(Into::into)540}541542/// Start a normal parachain node.543pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(544	parachain_config: Configuration,545	polkadot_config: Configuration,546	id: ParaId,547) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>548where549	Runtime: RuntimeInstance + Send + Sync + 'static,550	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,551	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,552	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>553		+ Send554		+ Sync555		+ 'static,556	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>557		+ fp_rpc::EthereumRuntimeRPCApi<Block>558		+ sp_session::SessionKeys<Block>559		+ sp_block_builder::BlockBuilder<Block>560		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>561		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>562		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>563		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>564		+ sp_api::Metadata<Block>565		+ sp_offchain::OffchainWorkerApi<Block>566		+ cumulus_primitives_core::CollectCollationInfo<Block>567		+ sp_consensus_aura::AuraApi<Block, AuraId>,568	ExecutorDispatch: NativeExecutionDispatch + 'static,569{570	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(571		parachain_config,572		polkadot_config,573		id,574		parachain_build_import_queue,575		|client,576		 prometheus_registry,577		 telemetry,578		 task_manager,579		 relay_chain_interface,580		 transaction_pool,581		 sync_oracle,582		 keystore,583		 force_authoring| {584			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;585586			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(587				task_manager.spawn_handle(),588				client.clone(),589				transaction_pool,590				prometheus_registry,591				telemetry.clone(),592			);593594			Ok(AuraConsensus::build::<595				sp_consensus_aura::sr25519::AuthorityPair,596				_,597				_,598				_,599				_,600				_,601				_,602			>(BuildAuraConsensusParams {603				proposer_factory,604				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {605					let relay_chain_interface = relay_chain_interface.clone();606					async move {607						let parachain_inherent =608						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(609							relay_parent,610							&relay_chain_interface,611							&validation_data,612							id,613						).await;614615						let time = sp_timestamp::InherentDataProvider::from_system_time();616617						let slot =618						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(619							*time,620							slot_duration.slot_duration(),621						);622623						let parachain_inherent = parachain_inherent.ok_or_else(|| {624							Box::<dyn std::error::Error + Send + Sync>::from(625								"Failed to create parachain inherent",626							)627						})?;628						Ok((time, slot, parachain_inherent))629					}630				},631				block_import: client.clone(),632				para_client: client,633				backoff_authoring_blocks: Option::<()>::None,634				sync_oracle,635				keystore,636				force_authoring,637				slot_duration: *slot_duration,638				// We got around 500ms for proposing639				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),640				telemetry,641				max_block_proposal_slot_portion: None,642			}))643		},644	)645	.await646}