git.delta.rocks / unique-network / refs/commits / 060d94005368

difftreelog

Fix NativeExecutionDispatches

Daniel Shiposha2022-03-14parent: #186cd5e.patch.diff
in: master

1 file changed

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_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}
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_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}9798#[cfg(feature = "opal-runtime")]99impl NativeExecutionDispatch for OpalRuntimeExecutor {100	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;101102	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {103		opal_runtime::api::dispatch(method, data)104	}105106	fn native_version() -> sc_executor::NativeVersion {107		opal_runtime::native_version()108	}109}110111pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {112	let config_dir = config113		.base_path114		.as_ref()115		.map(|base_path| base_path.config_dir(config.chain_spec.id()))116		.unwrap_or_else(|| {117			BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())118		});119	let database_dir = config_dir.join("frontier").join("db");120121	Ok(Arc::new(fc_db::Backend::<Block>::new(122		&fc_db::DatabaseSettings {123			source: fc_db::DatabaseSettingsSrc::RocksDb {124				path: database_dir,125				cache_size: 0,126			},127		},128	)?))129}130131type FullClient<RuntimeApi, ExecutorDispatch> =132	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;133type FullBackend = sc_service::TFullBackend<Block>;134type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;135136/// Starts a `ServiceBuilder` for a full service.137///138/// Use this macro if you don't actually need the full service, but just the builder in order to139/// be able to perform chain operations.140#[allow(clippy::type_complexity)]141pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(142	config: &Configuration,143	build_import_queue: BIQ,144) -> Result<145	PartialComponents<146		FullClient<RuntimeApi, ExecutorDispatch>,147		FullBackend,148		FullSelectChain,149		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,150		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,151		(152			Option<Telemetry>,153			Option<FilterPool>,154			Arc<fc_db::Backend<Block>>,155			Option<TelemetryWorkerHandle>,156			FeeHistoryCache,157		),158	>,159	sc_service::Error,160>161where162	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,163	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>164		+ Send165		+ Sync166		+ 'static,167	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,168	ExecutorDispatch: NativeExecutionDispatch + 'static,169	BIQ: FnOnce(170		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,171		&Configuration,172		Option<TelemetryHandle>,173		&TaskManager,174	) -> Result<175		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,176		sc_service::Error,177	>,178{179	let _telemetry = config180		.telemetry_endpoints181		.clone()182		.filter(|x| !x.is_empty())183		.map(|endpoints| -> Result<_, sc_telemetry::Error> {184			let worker = TelemetryWorker::new(16)?;185			let telemetry = worker.handle().new_telemetry(endpoints);186			Ok((worker, telemetry))187		})188		.transpose()?;189190	let telemetry = config191		.telemetry_endpoints192		.clone()193		.filter(|x| !x.is_empty())194		.map(|endpoints| -> Result<_, sc_telemetry::Error> {195			let worker = TelemetryWorker::new(16)?;196			let telemetry = worker.handle().new_telemetry(endpoints);197			Ok((worker, telemetry))198		})199		.transpose()?;200201	let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(202		config.wasm_method,203		config.default_heap_pages,204		config.max_runtime_instances,205		config.runtime_cache_size,206	);207208	let (client, backend, keystore_container, task_manager) =209		sc_service::new_full_parts::<Block, RuntimeApi, _>(210			config,211			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),212			executor,213		)?;214	let client = Arc::new(client);215216	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());217218	let telemetry = telemetry.map(|(worker, telemetry)| {219		task_manager220			.spawn_handle()221			.spawn("telemetry", None, worker.run());222		telemetry223	});224225	let select_chain = sc_consensus::LongestChain::new(backend.clone());226227	let transaction_pool = sc_transaction_pool::BasicPool::new_full(228		config.transaction_pool.clone(),229		config.role.is_authority().into(),230		config.prometheus_registry(),231		task_manager.spawn_essential_handle(),232		client.clone(),233	);234235	let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));236237	let frontier_backend = open_frontier_backend(config)?;238239	let import_queue = build_import_queue(240		client.clone(),241		config,242		telemetry.as_ref().map(|telemetry| telemetry.handle()),243		&task_manager,244	)?;245	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));246247	let params = PartialComponents {248		backend,249		client,250		import_queue,251		keystore_container,252		task_manager,253		transaction_pool,254		select_chain,255		other: (256			telemetry,257			filter_pool,258			frontier_backend,259			telemetry_worker_handle,260			fee_history_cache,261		),262	};263264	Ok(params)265}266267/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.268///269/// This is the actual implementation that is abstract over the executor and the runtime api.270#[sc_tracing::logging::prefix_logs_with("Parachain")]271async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(272	parachain_config: Configuration,273	polkadot_config: Configuration,274	id: ParaId,275	build_import_queue: BIQ,276	build_consensus: BIC,277) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>278where279	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,280	Runtime: RuntimeInstance + Send + Sync + 'static,281	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,282	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,283	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>284		+ Send285		+ Sync286		+ 'static,287	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>288		+ fp_rpc::EthereumRuntimeRPCApi<Block>289		+ sp_session::SessionKeys<Block>290		+ sp_block_builder::BlockBuilder<Block>291		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>292		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>293		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>294		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>295		+ sp_api::Metadata<Block>296		+ sp_offchain::OffchainWorkerApi<Block>297		+ cumulus_primitives_core::CollectCollationInfo<Block>,298	ExecutorDispatch: NativeExecutionDispatch + 'static,299	BIQ: FnOnce(300		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,301		&Configuration,302		Option<TelemetryHandle>,303		&TaskManager,304	) -> Result<305		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,306		sc_service::Error,307	>,308	BIC: FnOnce(309		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,310		Option<&Registry>,311		Option<TelemetryHandle>,312		&TaskManager,313		Arc<dyn RelayChainInterface>,314		Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,315		Arc<NetworkService<Block, Hash>>,316		SyncCryptoStorePtr,317		bool,318	) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,319{320	if matches!(parachain_config.role, Role::Light) {321		return Err("Light client not supported!".into());322	}323324	let parachain_config = prepare_node_config(parachain_config);325326	let params =327		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;328	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =329		params.other;330331	let client = params.client.clone();332	let backend = params.backend.clone();333	let mut task_manager = params.task_manager;334335	let (relay_chain_interface, collator_key) =336		build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)337			.map_err(|e| match e {338				polkadot_service::Error::Sub(x) => x,339				s => format!("{}", s).into(),340			})?;341342	let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);343344	let force_authoring = parachain_config.force_authoring;345	let validator = parachain_config.role.is_authority();346	let prometheus_registry = parachain_config.prometheus_registry().cloned();347	let transaction_pool = params.transaction_pool.clone();348	let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);349350	let (network, system_rpc_tx, start_network) =351		sc_service::build_network(sc_service::BuildNetworkParams {352			config: &parachain_config,353			client: client.clone(),354			transaction_pool: transaction_pool.clone(),355			spawn_handle: task_manager.spawn_handle(),356			import_queue: import_queue.clone(),357			block_announce_validator_builder: Some(Box::new(|_| {358				Box::new(block_announce_validator)359			})),360			warp_sync: None,361		})?;362363	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());364	let rpc_client = client.clone();365	let rpc_pool = transaction_pool.clone();366	let select_chain = params.select_chain.clone();367	let rpc_network = network.clone();368369	let rpc_frontier_backend = frontier_backend.clone();370371	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(372		task_manager.spawn_handle(),373		overrides_handle::<_, _, Runtime>(client.clone()),374		50,375		50,376	));377378	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {379		let full_deps = unique_rpc::FullDeps {380			backend: rpc_frontier_backend.clone(),381			deny_unsafe,382			client: rpc_client.clone(),383			pool: rpc_pool.clone(),384			graph: rpc_pool.pool().clone(),385			// TODO: Unhardcode386			enable_dev_signer: false,387			filter_pool: filter_pool.clone(),388			network: rpc_network.clone(),389			select_chain: select_chain.clone(),390			is_authority: validator,391			// TODO: Unhardcode392			max_past_logs: 10000,393			block_data_cache: block_data_cache.clone(),394			fee_history_cache: fee_history_cache.clone(),395			// TODO: Unhardcode396			fee_history_limit: 2048,397		};398399		Ok(400			unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(401				full_deps,402				subscription_executor.clone(),403			),404		)405	});406407	task_manager.spawn_essential_handle().spawn(408		"frontier-mapping-sync-worker",409		None,410		MappingSyncWorker::new(411			client.import_notification_stream(),412			Duration::new(6, 0),413			client.clone(),414			backend.clone(),415			frontier_backend.clone(),416			SyncStrategy::Normal,417		)418		.for_each(|()| futures::future::ready(())),419	);420421	sc_service::spawn_tasks(sc_service::SpawnTasksParams {422		rpc_extensions_builder,423		client: client.clone(),424		transaction_pool: transaction_pool.clone(),425		task_manager: &mut task_manager,426		config: parachain_config,427		keystore: params.keystore_container.sync_keystore(),428		backend: backend.clone(),429		network: network.clone(),430		system_rpc_tx,431		telemetry: telemetry.as_mut(),432	})?;433434	let announce_block = {435		let network = network.clone();436		Arc::new(move |hash, data| network.announce_block(hash, data))437	};438439	let relay_chain_slot_duration = Duration::from_secs(6);440441	if validator {442		let parachain_consensus = build_consensus(443			client.clone(),444			prometheus_registry.as_ref(),445			telemetry.as_ref().map(|t| t.handle()),446			&task_manager,447			relay_chain_interface.clone(),448			transaction_pool,449			network,450			params.keystore_container.sync_keystore(),451			force_authoring,452		)?;453454		let spawner = task_manager.spawn_handle();455456		let params = StartCollatorParams {457			para_id: id,458			block_status: client.clone(),459			announce_block,460			client: client.clone(),461			task_manager: &mut task_manager,462			spawner,463			parachain_consensus,464			import_queue,465			collator_key,466			relay_chain_interface,467			relay_chain_slot_duration,468		};469470		start_collator(params).await?;471	} else {472		let params = StartFullNodeParams {473			client: client.clone(),474			announce_block,475			task_manager: &mut task_manager,476			para_id: id,477			import_queue,478			relay_chain_interface,479			relay_chain_slot_duration,480		};481482		start_full_node(params)?;483	}484485	start_network.start_network();486487	Ok((task_manager, client))488}489490/// Build the import queue for the the parachain runtime.491pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(492	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,493	config: &Configuration,494	telemetry: Option<TelemetryHandle>,495	task_manager: &TaskManager,496) -> Result<497	sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,498	sc_service::Error,499>500where501	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>502		+ Send503		+ Sync504		+ 'static,505	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>506		+ sp_block_builder::BlockBuilder<Block>507		+ sp_consensus_aura::AuraApi<Block, AuraId>508		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,509	ExecutorDispatch: NativeExecutionDispatch + 'static,510{511	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;512513	cumulus_client_consensus_aura::import_queue::<514		sp_consensus_aura::sr25519::AuthorityPair,515		_,516		_,517		_,518		_,519		_,520		_,521	>(cumulus_client_consensus_aura::ImportQueueParams {522		block_import: client.clone(),523		client: client.clone(),524		create_inherent_data_providers: move |_, _| async move {525			let time = sp_timestamp::InherentDataProvider::from_system_time();526527			let slot =528				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(529					*time,530					slot_duration.slot_duration(),531				);532533			Ok((time, slot))534		},535		registry: config.prometheus_registry(),536		can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),537		spawner: &task_manager.spawn_essential_handle(),538		telemetry,539	})540	.map_err(Into::into)541}542543/// Start a normal parachain node.544pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(545	parachain_config: Configuration,546	polkadot_config: Configuration,547	id: ParaId,548) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>549where550	Runtime: RuntimeInstance + Send + Sync + 'static,551	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,552	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,553	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>554		+ Send555		+ Sync556		+ 'static,557	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>558		+ fp_rpc::EthereumRuntimeRPCApi<Block>559		+ sp_session::SessionKeys<Block>560		+ sp_block_builder::BlockBuilder<Block>561		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>562		+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>563		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>564		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>565		+ sp_api::Metadata<Block>566		+ sp_offchain::OffchainWorkerApi<Block>567		+ cumulus_primitives_core::CollectCollationInfo<Block>568		+ sp_consensus_aura::AuraApi<Block, AuraId>,569	ExecutorDispatch: NativeExecutionDispatch + 'static,570{571	start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(572		parachain_config,573		polkadot_config,574		id,575		parachain_build_import_queue,576		|client,577		 prometheus_registry,578		 telemetry,579		 task_manager,580		 relay_chain_interface,581		 transaction_pool,582		 sync_oracle,583		 keystore,584		 force_authoring| {585			let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;586587			let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(588				task_manager.spawn_handle(),589				client.clone(),590				transaction_pool,591				prometheus_registry,592				telemetry.clone(),593			);594595			Ok(AuraConsensus::build::<596				sp_consensus_aura::sr25519::AuthorityPair,597				_,598				_,599				_,600				_,601				_,602				_,603			>(BuildAuraConsensusParams {604				proposer_factory,605				create_inherent_data_providers: move |_, (relay_parent, validation_data)| {606					let relay_chain_interface = relay_chain_interface.clone();607					async move {608						let parachain_inherent =609						cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(610							relay_parent,611							&relay_chain_interface,612							&validation_data,613							id,614						).await;615616						let time = sp_timestamp::InherentDataProvider::from_system_time();617618						let slot =619						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(620							*time,621							slot_duration.slot_duration(),622						);623624						let parachain_inherent = parachain_inherent.ok_or_else(|| {625							Box::<dyn std::error::Error + Send + Sync>::from(626								"Failed to create parachain inherent",627							)628						})?;629						Ok((time, slot, parachain_inherent))630					}631				},632				block_import: client.clone(),633				para_client: client,634				backoff_authoring_blocks: Option::<()>::None,635				sync_oracle,636				keystore,637				force_authoring,638				slot_duration: *slot_duration,639				// We got around 500ms for proposing640				block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),641				telemetry,642				max_block_proposal_slot_portion: None,643			}))644		},645	)646	.await647}