git.delta.rocks / unique-network / refs/commits / 5985fa11530c

difftreelog

source

node/cli/src/service.rs19.8 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;7172impl NativeExecutionDispatch for UniqueRuntimeExecutor {73	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7475	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {76		unique_runtime::api::dispatch(method, data)77	}7879	fn native_version() -> sc_executor::NativeVersion {80		unique_runtime::native_version()81	}82}8384impl NativeExecutionDispatch for QuartzRuntimeExecutor {85	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8687	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {88		unique_runtime::api::dispatch(method, data)89	}9091	fn native_version() -> sc_executor::NativeVersion {92		unique_runtime::native_version()93	}94}9596impl NativeExecutionDispatch for OpalRuntimeExecutor {97	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9899	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {100		unique_runtime::api::dispatch(method, data)101	}102103	fn native_version() -> sc_executor::NativeVersion {104		unique_runtime::native_version()105	}106}107108pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {109	let config_dir = config110		.base_path111		.as_ref()112		.map(|base_path| base_path.config_dir(config.chain_spec.id()))113		.unwrap_or_else(|| {114			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())115		});116	let database_dir = config_dir.join("frontier").join("db");117118	Ok(Arc::new(fc_db::Backend::<Block>::new(119		&fc_db::DatabaseSettings {120			source: fc_db::DatabaseSettingsSrc::RocksDb {121				path: database_dir,122				cache_size: 0,123			},124		},125	)?))126}127128type FullClient<RuntimeApi, ExecutorDispatch> =129	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;130type FullBackend = sc_service::TFullBackend<Block>;131type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;132133/// Starts a `ServiceBuilder` for a full service.134///135/// Use this macro if you don't actually need the full service, but just the builder in order to136/// be able to perform chain operations.137#[allow(clippy::type_complexity)]138pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(139	config: &Configuration,140	build_import_queue: BIQ,141) -> Result<142	PartialComponents<143		FullClient<RuntimeApi, ExecutorDispatch>,144		FullBackend,145		FullSelectChain,146		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,147		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,148		(149			Option<Telemetry>,150			Option<FilterPool>,151			Arc<fc_db::Backend<Block>>,152			Option<TelemetryWorkerHandle>,153			FeeHistoryCache,154		),155	>,156	sc_service::Error,157>158where159	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,160	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>161		+ Send162		+ Sync163		+ 'static,164	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,165	ExecutorDispatch: NativeExecutionDispatch + 'static,166	BIQ: FnOnce(167		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,168		&Configuration,169		Option<TelemetryHandle>,170		&TaskManager,171	) -> Result<172		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,173		sc_service::Error,174	>,175{176	let _telemetry = config177		.telemetry_endpoints178		.clone()179		.filter(|x| !x.is_empty())180		.map(|endpoints| -> Result<_, sc_telemetry::Error> {181			let worker = TelemetryWorker::new(16)?;182			let telemetry = worker.handle().new_telemetry(endpoints);183			Ok((worker, telemetry))184		})185		.transpose()?;186187	let telemetry = config188		.telemetry_endpoints189		.clone()190		.filter(|x| !x.is_empty())191		.map(|endpoints| -> Result<_, sc_telemetry::Error> {192			let worker = TelemetryWorker::new(16)?;193			let telemetry = worker.handle().new_telemetry(endpoints);194			Ok((worker, telemetry))195		})196		.transpose()?;197198	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(199		config.wasm_method,200		config.default_heap_pages,201		config.max_runtime_instances,202		config.runtime_cache_size,203	);204205	let (client, backend, keystore_container, task_manager) =206		sc_service::new_full_parts::<Block, RuntimeApi, _>(207			config,208			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),209			executor,210		)?;211	let client = Arc::new(client);212213	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());214215	let telemetry = telemetry.map(|(worker, telemetry)| {216		task_manager217			.spawn_handle()218			.spawn("telemetry", None, worker.run());219		telemetry220	});221222	let select_chain = sc_consensus::LongestChain::new(backend.clone());223224	let transaction_pool = sc_transaction_pool::BasicPool::new_full(225		config.transaction_pool.clone(),226		config.role.is_authority().into(),227		config.prometheus_registry(),228		task_manager.spawn_essential_handle(),229		client.clone(),230	);231232	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));233234	let frontier_backend = open_frontier_backend(config)?;235236	let import_queue = build_import_queue(237		client.clone(),238		config,239		telemetry.as_ref().map(|telemetry| telemetry.handle()),240		&task_manager,241	)?;242	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));243244	let params = PartialComponents {245		backend,246		client,247		import_queue,248		keystore_container,249		task_manager,250		transaction_pool,251		select_chain,252		other: (253			telemetry,254			filter_pool,255			frontier_backend,256			telemetry_worker_handle,257			fee_history_cache,258		),259	};260261	Ok(params)262}263264/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.265///266/// This is the actual implementation that is abstract over the executor and the runtime api.267#[sc_tracing::logging::prefix_logs_with("Parachain")]268async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(269	parachain_config: Configuration,270	polkadot_config: Configuration,271	id: ParaId,272	build_import_queue: BIQ,273	build_consensus: BIC,274) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>275where276	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,277	Runtime: RuntimeInstance + Send + Sync + 'static,278	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,279	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,280	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>281		+ Send282		+ Sync283		+ 'static,284	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>285		+ fp_rpc::EthereumRuntimeRPCApi<Block>286		+ sp_session::SessionKeys<Block>287		+ sp_block_builder::BlockBuilder<Block>288		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>289		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>290		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>291		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>292		+ sp_api::Metadata<Block>293		+ sp_offchain::OffchainWorkerApi<Block>294		+ cumulus_primitives_core::CollectCollationInfo<Block>,295	ExecutorDispatch: NativeExecutionDispatch + 'static,296	BIQ: FnOnce(297		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,298		&Configuration,299		Option<TelemetryHandle>,300		&TaskManager,301	) -> Result<302		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,303		sc_service::Error,304	>,305	BIC: FnOnce(306		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,307		Option<&Registry>,308		Option<TelemetryHandle>,309		&TaskManager,310		Arc<dyn RelayChainInterface>,311		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,312		Arc<NetworkService<Block, Hash>>,313		SyncCryptoStorePtr,314		bool,315	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,316{317	if matches!(parachain_config.role, Role::Light) {318		return Err("Light client not supported!".into());319	}320321	let parachain_config = prepare_node_config(parachain_config);322323	let params =324		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;325	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =326		params.other;327328	let client = params.client.clone();329	let backend = params.backend.clone();330	let mut task_manager = params.task_manager;331332	let (relay_chain_interface, collator_key) =333		build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)334			.map_err(|e| match e {335				polkadot_service::Error::Sub(x) => x,336				s => format!("{}", s).into(),337			})?;338339	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);340341	let force_authoring = parachain_config.force_authoring;342	let validator = parachain_config.role.is_authority();343	let prometheus_registry = parachain_config.prometheus_registry().cloned();344	let transaction_pool = params.transaction_pool.clone();345	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);346347	let (network, system_rpc_tx, start_network) =348		sc_service::build_network(sc_service::BuildNetworkParams {349			config: &parachain_config,350			client: client.clone(),351			transaction_pool: transaction_pool.clone(),352			spawn_handle: task_manager.spawn_handle(),353			import_queue: import_queue.clone(),354			block_announce_validator_builder: Some(Box::new(|_| {355				Box::new(block_announce_validator)356			})),357			warp_sync: None,358		})?;359360	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());361	let rpc_client = client.clone();362	let rpc_pool = transaction_pool.clone();363	let select_chain = params.select_chain.clone();364	let rpc_network = network.clone();365366	let rpc_frontier_backend = frontier_backend.clone();367368	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(369		task_manager.spawn_handle(),370		overrides_handle::<_, _, Runtime>(client.clone()),371		50,372		50,373	));374375	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {376		let full_deps = unique_rpc::FullDeps {377			backend: rpc_frontier_backend.clone(),378			deny_unsafe,379			client: rpc_client.clone(),380			pool: rpc_pool.clone(),381			graph: rpc_pool.pool().clone(),382			// TODO: Unhardcode383			enable_dev_signer: false,384			filter_pool: filter_pool.clone(),385			network: rpc_network.clone(),386			select_chain: select_chain.clone(),387			is_authority: validator,388			// TODO: Unhardcode389			max_past_logs: 10000,390			block_data_cache: block_data_cache.clone(),391			fee_history_cache: fee_history_cache.clone(),392			// TODO: Unhardcode393			fee_history_limit: 2048,394		};395396		Ok(397			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(398				full_deps,399				subscription_executor.clone(),400			),401		)402	});403404	task_manager.spawn_essential_handle().spawn(405		"frontier-mapping-sync-worker",406		None,407		MappingSyncWorker::new(408			client.import_notification_stream(),409			Duration::new(6, 0),410			client.clone(),411			backend.clone(),412			frontier_backend.clone(),413			SyncStrategy::Normal,414		)415		.for_each(|()| futures::future::ready(())),416	);417418	sc_service::spawn_tasks(sc_service::SpawnTasksParams {419		rpc_extensions_builder,420		client: client.clone(),421		transaction_pool: transaction_pool.clone(),422		task_manager: &mut task_manager,423		config: parachain_config,424		keystore: params.keystore_container.sync_keystore(),425		backend: backend.clone(),426		network: network.clone(),427		system_rpc_tx,428		telemetry: telemetry.as_mut(),429	})?;430431	let announce_block = {432		let network = network.clone();433		Arc::new(move |hash, data| network.announce_block(hash, data))434	};435436	let relay_chain_slot_duration = Duration::from_secs(6);437438	if validator {439		let parachain_consensus = build_consensus(440			client.clone(),441			prometheus_registry.as_ref(),442			telemetry.as_ref().map(|t| t.handle()),443			&task_manager,444			relay_chain_interface.clone(),445			transaction_pool,446			network,447			params.keystore_container.sync_keystore(),448			force_authoring,449		)?;450451		let spawner = task_manager.spawn_handle();452453		let params = StartCollatorParams {454			para_id: id,455			block_status: client.clone(),456			announce_block,457			client: client.clone(),458			task_manager: &mut task_manager,459			spawner,460			parachain_consensus,461			import_queue,462			collator_key,463			relay_chain_interface,464			relay_chain_slot_duration,465		};466467		start_collator(params).await?;468	} else {469		let params = StartFullNodeParams {470			client: client.clone(),471			announce_block,472			task_manager: &mut task_manager,473			para_id: id,474			import_queue,475			relay_chain_interface,476			relay_chain_slot_duration,477		};478479		start_full_node(params)?;480	}481482	start_network.start_network();483484	Ok((task_manager, client))485}486487/// Build the import queue for the the parachain runtime.488pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(489	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,490	config: &Configuration,491	telemetry: Option<TelemetryHandle>,492	task_manager: &TaskManager,493) -> Result<494	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,495	sc_service::Error,496>497where498	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>499		+ Send500		+ Sync501		+ 'static,502	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>503		+ sp_block_builder::BlockBuilder<Block>504		+ sp_consensus_aura::AuraApi<Block, AuraId>505		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,506	ExecutorDispatch: NativeExecutionDispatch + 'static,507{508	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;509510	cumulus_client_consensus_aura::import_queue::<511		sp_consensus_aura::sr25519::AuthorityPair,512		_,513		_,514		_,515		_,516		_,517		_,518	>(cumulus_client_consensus_aura::ImportQueueParams {519		block_import: client.clone(),520		client: client.clone(),521		create_inherent_data_providers: move |_, _| async move {522			let time = sp_timestamp::InherentDataProvider::from_system_time();523524			let slot =525				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(526					*time,527					slot_duration.slot_duration(),528				);529530			Ok((time, slot))531		},532		registry: config.prometheus_registry(),533		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),534		spawner: &task_manager.spawn_essential_handle(),535		telemetry,536	})537	.map_err(Into::into)538}539540/// Start a normal parachain node.541pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(542	parachain_config: Configuration,543	polkadot_config: Configuration,544	id: ParaId,545) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>546where547	Runtime: RuntimeInstance + Send + Sync + 'static,548	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,549	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,550	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>551		+ Send552		+ Sync553		+ 'static,554	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>555		+ fp_rpc::EthereumRuntimeRPCApi<Block>556		+ sp_session::SessionKeys<Block>557		+ sp_block_builder::BlockBuilder<Block>558		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>559		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>560		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>561		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>562		+ sp_api::Metadata<Block>563		+ sp_offchain::OffchainWorkerApi<Block>564		+ cumulus_primitives_core::CollectCollationInfo<Block>565		+ sp_consensus_aura::AuraApi<Block, AuraId>,566	ExecutorDispatch: NativeExecutionDispatch + 'static,567{568	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(569		parachain_config,570		polkadot_config,571		id,572		parachain_build_import_queue,573		|client,574		 prometheus_registry,575		 telemetry,576		 task_manager,577		 relay_chain_interface,578		 transaction_pool,579		 sync_oracle,580		 keystore,581		 force_authoring| {582			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;583584			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(585				task_manager.spawn_handle(),586				client.clone(),587				transaction_pool,588				prometheus_registry,589				telemetry.clone(),590			);591592			Ok(AuraConsensus::build::<593				sp_consensus_aura::sr25519::AuthorityPair,594				_,595				_,596				_,597				_,598				_,599				_,600			>(BuildAuraConsensusParams {601				proposer_factory,602				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {603					let relay_chain_interface = relay_chain_interface.clone();604					async move {605						let parachain_inherent =606						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(607							relay_parent,608							&relay_chain_interface,609							&validation_data,610							id,611						).await;612613						let time = sp_timestamp::InherentDataProvider::from_system_time();614615						let slot =616						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(617							*time,618							slot_duration.slot_duration(),619						);620621						let parachain_inherent = parachain_inherent.ok_or_else(|| {622							Box::<dyn std::error::Error + Send + Sync>::from(623								"Failed to create parachain inherent",624							)625						})?;626						Ok((time, slot, parachain_inherent))627					}628				},629				block_import: client.clone(),630				para_client: client,631				backoff_authoring_blocks: Option::<()>::None,632				sync_oracle,633				keystore,634				force_authoring,635				slot_duration: *slot_duration,636				// We got around 500ms for proposing637				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),638				telemetry,639				max_block_proposal_slot_portion: None,640			}))641		},642	)643	.await644}