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

difftreelog

source

node/cli/src/service.rs38.0 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// std18use std::{19	collections::BTreeMap,20	marker::PhantomData,21	pin::Pin,22	sync::{Arc, Mutex},23	time::Duration,24};2526use cumulus_client_cli::CollatorOptions;27use cumulus_client_collator::service::CollatorService;28#[cfg(not(feature = "lookahead"))]29use cumulus_client_consensus_aura::collators::basic::{30	run as run_aura, Params as BuildAuraConsensusParams,31};32#[cfg(feature = "lookahead")]33use cumulus_client_consensus_aura::collators::lookahead::{34	run as run_aura, Params as BuildAuraConsensusParams,35};36use cumulus_client_consensus_common::ParachainBlockImport as TParachainBlockImport;37use cumulus_client_consensus_proposer::Proposer;38use cumulus_client_service::{39	build_relay_chain_interface, prepare_node_config, start_relay_chain_tasks, DARecoveryProfile,40	StartRelayChainTasksParams,41};42use cumulus_primitives_core::ParaId;43use cumulus_primitives_parachain_inherent::ParachainInherentData;44use cumulus_relay_chain_interface::{OverseerHandle, RelayChainInterface};45use fc_mapping_sync::{kv::MappingSyncWorker, EthereumBlockNotificationSinks, SyncStrategy};46use fc_rpc::{47	frontier_backend_client::SystemAccountId32StorageOverride, EthBlockDataCacheTask, EthConfig,48	EthTask, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, SchemaV2Override,49	SchemaV3Override, StorageOverride,50};51use fc_rpc_core::types::{FeeHistoryCache, FilterPool};52use fp_rpc::EthereumRuntimeRPCApi;53use fp_storage::EthereumStorageSchema;54use futures::{55	stream::select,56	task::{Context, Poll},57	Stream, StreamExt,58};59use jsonrpsee::RpcModule;60use polkadot_service::CollatorPair;61use sc_client_api::{AuxStore, Backend, BlockOf, BlockchainEvents, StorageProvider};62use sc_consensus::ImportQueue;63use sc_executor::{NativeElseWasmExecutor, NativeExecutionDispatch};64use sc_network::NetworkBlock;65use sc_network_sync::SyncingService;66use sc_rpc::SubscriptionTaskExecutor;67use sc_service::{Configuration, PartialComponents, TaskManager};68use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};69use serde::{Deserialize, Serialize};70use sp_api::ProvideRuntimeApi;71use sp_block_builder::BlockBuilder;72use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};73use sp_consensus_aura::sr25519::AuthorityPair as AuraAuthorityPair;74use sp_keystore::KeystorePtr;75use sp_state_machine::Backend as StateBackend;76use substrate_prometheus_endpoint::Registry;77use tokio::time::Interval;78use up_common::types::{opaque::*, Nonce};7980pub type ParachainHostFunctions = (81	sp_io::SubstrateHostFunctions,82	cumulus_client_service::storage_proof_size::HostFunctions,83);8485use cumulus_primitives_core::PersistedValidationData;86use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;8788use crate::{89	chain_spec::RuntimeIdentification,90	rpc::{create_eth, create_full, EthDeps, FullDeps},91};9293/// Unique native executor instance.94#[cfg(feature = "unique-runtime")]95pub struct UniqueRuntimeExecutor;9697#[cfg(feature = "quartz-runtime")]98/// Quartz native executor instance.99pub struct QuartzRuntimeExecutor;100101/// Opal native executor instance.102pub struct OpalRuntimeExecutor;103104#[cfg(feature = "unique-runtime")]105impl NativeExecutionDispatch for UniqueRuntimeExecutor {106	/// Only enable the benchmarking host functions when we actually want to benchmark.107	#[cfg(feature = "runtime-benchmarks")]108	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;109	/// Otherwise we only use the default Substrate host functions.110	#[cfg(not(feature = "runtime-benchmarks"))]111	type ExtendHostFunctions = ParachainHostFunctions;112113	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {114		unique_runtime::api::dispatch(method, data)115	}116117	fn native_version() -> sc_executor::NativeVersion {118		unique_runtime::native_version()119	}120}121122#[cfg(feature = "quartz-runtime")]123impl NativeExecutionDispatch for QuartzRuntimeExecutor {124	/// Only enable the benchmarking host functions when we actually want to benchmark.125	#[cfg(feature = "runtime-benchmarks")]126	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;127	/// Otherwise we only use the default Substrate host functions.128	#[cfg(not(feature = "runtime-benchmarks"))]129	type ExtendHostFunctions = ParachainHostFunctions;130131	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {132		quartz_runtime::api::dispatch(method, data)133	}134135	fn native_version() -> sc_executor::NativeVersion {136		quartz_runtime::native_version()137	}138}139140impl NativeExecutionDispatch for OpalRuntimeExecutor {141	/// Only enable the benchmarking host functions when we actually want to benchmark.142	#[cfg(feature = "runtime-benchmarks")]143	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;144	/// Otherwise we only use the default Substrate host functions.145	#[cfg(not(feature = "runtime-benchmarks"))]146	type ExtendHostFunctions = ParachainHostFunctions;147148	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {149		opal_runtime::api::dispatch(method, data)150	}151152	fn native_version() -> sc_executor::NativeVersion {153		opal_runtime::native_version()154	}155}156157pub struct AutosealInterval {158	interval: Interval,159}160161impl AutosealInterval {162	pub fn new(config: &Configuration, interval: u64) -> Self {163		let _tokio_runtime = config.tokio_handle.enter();164		let interval = tokio::time::interval(Duration::from_millis(interval));165166		Self { interval }167	}168}169170impl Stream for AutosealInterval {171	type Item = tokio::time::Instant;172173	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {174		self.interval.poll_tick(cx).map(Some)175	}176}177178pub fn open_frontier_backend<C: HeaderBackend<Block>>(179	client: Arc<C>,180	config: &Configuration,181) -> Result<Arc<fc_db::kv::Backend<Block>>, String> {182	let config_dir = config.base_path.config_dir(config.chain_spec.id());183	let database_dir = config_dir.join("frontier").join("db");184185	Ok(Arc::new(fc_db::kv::Backend::<Block>::new(186		client,187		&fc_db::kv::DatabaseSettings {188			source: fc_db::DatabaseSource::RocksDb {189				path: database_dir,190				cache_size: 0,191			},192		},193	)?))194}195196type FullClient<RuntimeApi, ExecutorDispatch> =197	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;198type FullBackend = sc_service::TFullBackend<Block>;199type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;200type ParachainBlockImport<RuntimeApi, ExecutorDispatch> =201	TParachainBlockImport<Block, Arc<FullClient<RuntimeApi, ExecutorDispatch>>, FullBackend>;202203/// Generate a supertrait based on bounds, and blanket impl for it.204macro_rules! ez_bounds {205	($vis:vis trait $name:ident$(<$($gen:ident $(: $($(+)? $bound:path)*)?),* $(,)?>)? $(:)? $($(+)? $super:path)* {}) => {206		$vis trait $name $(<$($gen $(: $($bound+)*)?,)*>)?: $($super +)* {}207		impl<T, $($($gen $(: $($bound+)*)?,)*)?> $name$(<$($gen,)*>)? for T208		where T: $($super +)* {}209	}210}211ez_bounds!(212	pub trait RuntimeApiDep<Runtime: RuntimeInstance>:213		sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>214		+ sp_consensus_aura::AuraApi<Block, AuraId>215		+ fp_rpc::EthereumRuntimeRPCApi<Block>216		+ sp_session::SessionKeys<Block>217		+ sp_block_builder::BlockBuilder<Block>218		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>219		+ sp_api::ApiExt<Block>220		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>221		+ app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>222		+ up_pov_estimate_rpc::PovEstimateApi<Block>223		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>224		+ sp_api::Metadata<Block>225		+ sp_offchain::OffchainWorkerApi<Block>226		+ cumulus_primitives_core::CollectCollationInfo<Block>227		// Deprecated, not used.228		+ fp_rpc::ConvertTransactionRuntimeApi<Block>229	{230	}231);232#[cfg(not(feature = "lookahead"))]233ez_bounds!(234	pub trait LookaheadApiDep {}235);236#[cfg(feature = "lookahead")]237ez_bounds!(238	pub trait LookaheadApiDep: cumulus_primitives_aura::AuraUnincludedSegmentApi<Block> {}239);240241fn ethereum_parachain_inherent() -> ParachainInherentData {242	let (relay_parent_storage_root, relay_chain_state) =243		RelayStateSproofBuilder::default().into_state_root_and_proof();244	let vfp = PersistedValidationData {245		// This is a hack to make `cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases`246		// happy. Relay parent number can't be bigger than u32::MAX.247		relay_parent_number: u32::MAX,248		relay_parent_storage_root,249		..Default::default()250	};251252	ParachainInherentData {253		validation_data: vfp,254		relay_chain_state,255		downward_messages: Default::default(),256		horizontal_messages: Default::default(),257	}258}259260/// Starts a `ServiceBuilder` for a full service.261///262/// Use this macro if you don't actually need the full service, but just the builder in order to263/// be able to perform chain operations.264#[allow(clippy::type_complexity)]265pub fn new_partial<Runtime, RuntimeApi, ExecutorDispatch, BIQ>(266	config: &Configuration,267	build_import_queue: BIQ,268) -> Result<269	PartialComponents<270		FullClient<RuntimeApi, ExecutorDispatch>,271		FullBackend,272		FullSelectChain,273		sc_consensus::DefaultImportQueue<Block>,274		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,275		OtherPartial,276	>,277	sc_service::Error,278>279where280	sc_client_api::StateBackendFor<FullBackend, Block>: StateBackend<BlakeTwo256>,281	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>282		+ Send283		+ Sync284		+ 'static,285	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,286	Runtime: RuntimeInstance,287	ExecutorDispatch: NativeExecutionDispatch + 'static,288	BIQ: FnOnce(289		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,290		Arc<FullBackend>,291		&Configuration,292		Option<TelemetryHandle>,293		&TaskManager,294	) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error>,295{296	let telemetry = config297		.telemetry_endpoints298		.clone()299		.filter(|x| !x.is_empty())300		.map(|endpoints| -> Result<_, sc_telemetry::Error> {301			let worker = TelemetryWorker::new(16)?;302			let telemetry = worker.handle().new_telemetry(endpoints);303			Ok((worker, telemetry))304		})305		.transpose()?;306307	let executor = sc_service::new_native_or_wasm_executor(config);308309	let (client, backend, keystore_container, task_manager) =310		sc_service::new_full_parts::<Block, RuntimeApi, _>(311			config,312			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),313			executor,314		)?;315	let client = Arc::new(client);316317	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());318319	let telemetry = telemetry.map(|(worker, telemetry)| {320		task_manager321			.spawn_handle()322			.spawn("telemetry", None, worker.run());323		telemetry324	});325326	let select_chain = sc_consensus::LongestChain::new(backend.clone());327328	let transaction_pool = sc_transaction_pool::BasicPool::new_full(329		config.transaction_pool.clone(),330		config.role.is_authority().into(),331		config.prometheus_registry(),332		task_manager.spawn_essential_handle(),333		client.clone(),334	);335336	let eth_filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));337338	let eth_backend = open_frontier_backend(client.clone(), config)?;339340	let import_queue = build_import_queue(341		client.clone(),342		backend.clone(),343		config,344		telemetry.as_ref().map(|telemetry| telemetry.handle()),345		&task_manager,346	)?;347348	let params = PartialComponents {349		backend,350		client,351		import_queue,352		keystore_container,353		task_manager,354		transaction_pool,355		select_chain,356		other: OtherPartial {357			telemetry,358			eth_filter_pool,359			eth_backend,360			telemetry_worker_handle,361		},362	};363364	Ok(params)365}366367macro_rules! clone {368    ($($i:ident),* $(,)?) => {369		$(370			let $i = $i.clone();371		)*372    };373}374375/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.376///377/// This is the actual implementation that is abstract over the executor and the runtime api.378#[sc_tracing::logging::prefix_logs_with("Parachain")]379pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(380	parachain_config: Configuration,381	polkadot_config: Configuration,382	collator_options: CollatorOptions,383	para_id: ParaId,384	hwbench: Option<sc_sysinfo::HwBench>,385) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>386where387	sc_client_api::StateBackendFor<FullBackend, Block>: StateBackend<BlakeTwo256>,388	Runtime: RuntimeInstance + Send + Sync + 'static,389	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,390	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,391	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>392		+ Send393		+ Sync394		+ 'static,395	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,396	RuntimeApi::RuntimeApi: LookaheadApiDep,397	Runtime: RuntimeInstance,398	ExecutorDispatch: NativeExecutionDispatch + 'static,399{400	let parachain_config = prepare_node_config(parachain_config);401402	let params = new_partial::<Runtime, RuntimeApi, ExecutorDispatch, _>(403		&parachain_config,404		parachain_build_import_queue,405	)?;406	let OtherPartial {407		mut telemetry,408		telemetry_worker_handle,409		eth_filter_pool,410		eth_backend,411	} = params.other;412	let net_config = sc_network::config::FullNetworkConfiguration::new(&parachain_config.network);413414	let client = params.client.clone();415	let backend = params.backend.clone();416	let mut task_manager = params.task_manager;417418	let (relay_chain_interface, collator_key) = build_relay_chain_interface(419		polkadot_config,420		&parachain_config,421		telemetry_worker_handle,422		&mut task_manager,423		collator_options.clone(),424		hwbench.clone(),425	)426	.await427	.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;428429	// Aura is sybil-resistant, collator-selection is generally too.430	let block_announce_validator =431		cumulus_client_network::AssumeSybilResistance::allow_seconded_messages();432433	let validator = parachain_config.role.is_authority();434	let prometheus_registry = parachain_config.prometheus_registry().cloned();435	let transaction_pool = params.transaction_pool.clone();436	let import_queue_service = params.import_queue.service();437438	let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =439		sc_service::build_network(sc_service::BuildNetworkParams {440			config: &parachain_config,441			net_config,442			client: client.clone(),443			transaction_pool: transaction_pool.clone(),444			spawn_handle: task_manager.spawn_handle(),445			import_queue: params.import_queue,446			block_announce_validator_builder: Some(Box::new(|_| {447				Box::new(block_announce_validator)448			})),449			warp_sync_params: None,450			block_relay: None,451		})?;452453	let select_chain = params.select_chain.clone();454455	let runtime_id = parachain_config.chain_spec.runtime_id();456457	// Frontier458	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));459	let fee_history_limit = 2048;460461	let eth_pubsub_notification_sinks: Arc<462		EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,463	> = Default::default();464465	let overrides = overrides_handle(client.clone());466	let eth_block_data_cache = spawn_frontier_tasks(467		FrontierTaskParams {468			client: client.clone(),469			substrate_backend: backend.clone(),470			eth_filter_pool: eth_filter_pool.clone(),471			eth_backend: eth_backend.clone(),472			fee_history_limit,473			fee_history_cache: fee_history_cache.clone(),474			task_manager: &task_manager,475			prometheus_registry: prometheus_registry.clone(),476			overrides: overrides.clone(),477			sync_strategy: SyncStrategy::Parachain,478		},479		sync_service.clone(),480		eth_pubsub_notification_sinks.clone(),481	);482483	// Rpc484	let rpc_builder = Box::new({485		clone!(486			client,487			backend,488			eth_backend,489			eth_pubsub_notification_sinks,490			fee_history_cache,491			eth_block_data_cache,492			overrides,493			transaction_pool,494			network,495			sync_service,496		);497		move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| {498			clone!(499				backend,500				eth_block_data_cache,501				client,502				eth_backend,503				eth_filter_pool,504				eth_pubsub_notification_sinks,505				fee_history_cache,506				eth_block_data_cache,507				network,508				runtime_id,509				transaction_pool,510				select_chain,511				overrides,512			);513514			#[cfg(not(feature = "pov-estimate"))]515			let _ = backend;516517			let mut rpc_handle = RpcModule::new(());518519			let full_deps = FullDeps {520				client: client.clone(),521				runtime_id,522523				#[cfg(feature = "pov-estimate")]524				exec_params: uc_rpc::pov_estimate::ExecutorParams {525					wasm_method: parachain_config.wasm_method,526					default_heap_pages: parachain_config.default_heap_pages,527					max_runtime_instances: parachain_config.max_runtime_instances,528					runtime_cache_size: parachain_config.runtime_cache_size,529				},530531				#[cfg(feature = "pov-estimate")]532				backend,533534				deny_unsafe,535				pool: transaction_pool.clone(),536				select_chain,537			};538539			create_full::<_, _, _, Runtime, _>(&mut rpc_handle, full_deps)?;540541			let eth_deps = EthDeps {542				client,543				graph: transaction_pool.pool().clone(),544				pool: transaction_pool,545				is_authority: validator,546				network,547				eth_backend,548				// TODO: Unhardcode549				max_past_logs: 10000,550				fee_history_limit,551				fee_history_cache,552				eth_block_data_cache,553				// TODO: Unhardcode554				enable_dev_signer: false,555				eth_filter_pool,556				eth_pubsub_notification_sinks,557				overrides,558				sync: sync_service.clone(),559				pending_create_inherent_data_providers: |_, ()| async move {560					Ok((ethereum_parachain_inherent(),))561				},562			};563564			create_eth::<565				_,566				_,567				_,568				_,569				_,570				_,571				DefaultEthConfig<FullClient<RuntimeApi, ExecutorDispatch>>,572			>(573				&mut rpc_handle,574				eth_deps,575				subscription_task_executor.clone(),576			)?;577578			Ok(rpc_handle)579		}580	});581582	sc_service::spawn_tasks(sc_service::SpawnTasksParams {583		rpc_builder,584		client: client.clone(),585		transaction_pool: transaction_pool.clone(),586		task_manager: &mut task_manager,587		config: parachain_config,588		keystore: params.keystore_container.keystore(),589		backend: backend.clone(),590		network,591		sync_service: sync_service.clone(),592		system_rpc_tx,593		telemetry: telemetry.as_mut(),594		tx_handler_controller,595	})?;596597	if let Some(hwbench) = hwbench {598		sc_sysinfo::print_hwbench(&hwbench);599600		if let Some(ref mut telemetry) = telemetry {601			let telemetry_handle = telemetry.handle();602			task_manager.spawn_handle().spawn(603				"telemetry_hwbench",604				None,605				sc_sysinfo::initialize_hwbench_telemetry(telemetry_handle, hwbench),606			);607		}608	}609610	let announce_block = {611		let sync_service = sync_service.clone();612		Arc::new(Box::new(move |hash, data| {613			sync_service.announce_block(hash, data)614		}))615	};616617	let relay_chain_slot_duration = Duration::from_secs(6);618619	let overseer_handle = relay_chain_interface620		.overseer_handle()621		.map_err(|e| sc_service::Error::Application(Box::new(e)))?;622623	start_relay_chain_tasks(StartRelayChainTasksParams {624		client: client.clone(),625		announce_block: announce_block.clone(),626		para_id,627		relay_chain_interface: relay_chain_interface.clone(),628		task_manager: &mut task_manager,629		da_recovery_profile: if validator {630			DARecoveryProfile::Collator631		} else {632			DARecoveryProfile::FullNode633		},634		import_queue: import_queue_service,635		relay_chain_slot_duration,636		recovery_handle: Box::new(overseer_handle.clone()),637		sync_service: sync_service.clone(),638	})?;639640	if validator {641		start_consensus(642			client.clone(),643			transaction_pool,644			StartConsensusParameters {645				backend: backend.clone(),646				prometheus_registry: prometheus_registry.as_ref(),647				telemetry: telemetry.as_ref().map(|t| t.handle()),648				task_manager: &task_manager,649				relay_chain_interface: relay_chain_interface.clone(),650				sync_oracle: sync_service,651				keystore: params.keystore_container.keystore(),652				overseer_handle,653				relay_chain_slot_duration,654				para_id,655				collator_key: collator_key.expect("cli args do not allow this"),656				announce_block,657			},658		)?;659	}660661	start_network.start_network();662663	Ok((task_manager, client))664}665666/// Build the import queue for the the parachain runtime.667pub fn parachain_build_import_queue<Runtime, RuntimeApi, ExecutorDispatch>(668	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,669	backend: Arc<FullBackend>,670	config: &Configuration,671	telemetry: Option<TelemetryHandle>,672	task_manager: &TaskManager,673) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error>674where675	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>676		+ Send677		+ Sync678		+ 'static,679	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,680	Runtime: RuntimeInstance,681	ExecutorDispatch: NativeExecutionDispatch + 'static,682{683	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;684685	let block_import = ParachainBlockImport::new(client.clone(), backend);686687	cumulus_client_consensus_aura::import_queue::<688		sp_consensus_aura::sr25519::AuthorityPair,689		_,690		_,691		_,692		_,693		_,694	>(cumulus_client_consensus_aura::ImportQueueParams {695		block_import,696		client,697		create_inherent_data_providers: move |_, _| async move {698			let time = sp_timestamp::InherentDataProvider::from_system_time();699700			let slot =701				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(702					*time,703					slot_duration,704				);705706			Ok((slot, time))707		},708		registry: config.prometheus_registry(),709		spawner: &task_manager.spawn_essential_handle(),710		telemetry,711	})712	.map_err(Into::into)713}714715pub struct StartConsensusParameters<'a> {716	backend: Arc<FullBackend>,717	prometheus_registry: Option<&'a Registry>,718	telemetry: Option<TelemetryHandle>,719	task_manager: &'a TaskManager,720	relay_chain_interface: Arc<dyn RelayChainInterface>,721	sync_oracle: Arc<SyncingService<Block>>,722	keystore: KeystorePtr,723	overseer_handle: OverseerHandle,724	relay_chain_slot_duration: Duration,725	para_id: ParaId,726	collator_key: CollatorPair,727	announce_block: Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>,728}729730// Clones ignored for optional lookahead collator731#[allow(clippy::redundant_clone)]732pub fn start_consensus<ExecutorDispatch, RuntimeApi, Runtime>(733	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,734	transaction_pool: Arc<735		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,736	>,737	parameters: StartConsensusParameters<'_>,738) -> Result<(), sc_service::Error>739where740	ExecutorDispatch: NativeExecutionDispatch + 'static,741	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>742		+ Send743		+ Sync744		+ 'static,745	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,746	RuntimeApi::RuntimeApi: LookaheadApiDep,747	Runtime: RuntimeInstance,748{749	let StartConsensusParameters {750		backend,751		prometheus_registry,752		telemetry,753		task_manager,754		relay_chain_interface,755		sync_oracle,756		keystore,757		overseer_handle,758		relay_chain_slot_duration,759		para_id,760		collator_key,761		announce_block,762	} = parameters;763	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;764765	let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(766		task_manager.spawn_handle(),767		client.clone(),768		transaction_pool,769		prometheus_registry,770		telemetry,771	);772	let proposer = Proposer::new(proposer_factory);773774	let collator_service = CollatorService::new(775		client.clone(),776		Arc::new(task_manager.spawn_handle()),777		announce_block,778		client.clone(),779	);780781	let block_import = ParachainBlockImport::new(client.clone(), backend.clone());782783	let params = BuildAuraConsensusParams {784		create_inherent_data_providers: move |_, ()| async move { Ok(()) },785		block_import,786		para_client: client.clone(),787		#[cfg(feature = "lookahead")]788		para_backend: backend,789		para_id,790		relay_client: relay_chain_interface,791		sync_oracle,792		keystore,793		#[cfg(not(feature = "lookahead"))]794		slot_duration,795		proposer,796		collator_service,797		// With async-baking, we allowed to be both slower (longer authoring) and faster (multiple para blocks per relay block)798		#[cfg(not(feature = "lookahead"))]799		authoring_duration: Duration::from_millis(500),800		#[cfg(feature = "lookahead")]801		authoring_duration: Duration::from_millis(1500),802		overseer_handle,803		#[cfg(feature = "lookahead")]804		code_hash_provider: move |block_hash| {805			client806				.code_at(block_hash)807				.ok()808				.map(cumulus_primitives_core::relay_chain::ValidationCode)809				.map(|c| c.hash())810		},811		collator_key,812		relay_chain_slot_duration,813		#[cfg(not(feature = "lookahead"))]814		collation_request_receiver: None,815		#[cfg(feature = "lookahead")]816		reinitialize: false,817	};818819	task_manager.spawn_essential_handle().spawn(820		"aura",821		None,822		#[cfg(not(feature = "lookahead"))]823		run_aura::<_, AuraAuthorityPair, _, _, _, _, _, _, _>(params),824		#[cfg(feature = "lookahead")]825		run_aura::<_, AuraAuthorityPair, _, _, _, _, _, _, _, _, _>(params),826	);827	Ok(())828}829830fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(831	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,832	_: Arc<FullBackend>,833	config: &Configuration,834	_: Option<TelemetryHandle>,835	task_manager: &TaskManager,836) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error>837where838	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>839		+ Send840		+ Sync841		+ 'static,842	RuntimeApi::RuntimeApi:843		sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> + sp_api::ApiExt<Block>,844	ExecutorDispatch: NativeExecutionDispatch + 'static,845{846	Ok(sc_consensus_manual_seal::import_queue(847		Box::new(client),848		&task_manager.spawn_essential_handle(),849		config.prometheus_registry(),850	))851}852853pub struct OtherPartial {854	pub telemetry: Option<Telemetry>,855	pub telemetry_worker_handle: Option<TelemetryWorkerHandle>,856	pub eth_filter_pool: Option<FilterPool>,857	pub eth_backend: Arc<fc_db::kv::Backend<Block>>,858}859860struct DefaultEthConfig<C>(PhantomData<C>);861impl<C> EthConfig<Block, C> for DefaultEthConfig<C>862where863	C: StorageProvider<Block, FullBackend> + Sync + Send + 'static,864{865	type EstimateGasAdapter = ();866	type RuntimeStorageOverride = SystemAccountId32StorageOverride<Block, C, FullBackend>;867}868869/// Builds a new development service. This service uses instant seal, and mocks870/// the parachain inherent871pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(872	config: Configuration,873	autoseal_interval: u64,874	autoseal_finalize_delay: Option<u64>,875	disable_autoseal_on_tx: bool,876) -> sc_service::error::Result<TaskManager>877where878	Runtime: RuntimeInstance + Send + Sync + 'static,879	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,880	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,881	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>882		+ Send883		+ Sync884		+ 'static,885	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,886	ExecutorDispatch: NativeExecutionDispatch + 'static,887{888	use fc_consensus::FrontierBlockImport;889	use sc_consensus_manual_seal::{890		run_delayed_finalize, run_manual_seal, DelayedFinalizeParams, EngineCommand,891		ManualSealParams,892	};893894	let sc_service::PartialComponents {895		client,896		backend,897		mut task_manager,898		import_queue,899		keystore_container,900		select_chain: maybe_select_chain,901		transaction_pool,902		other:903			OtherPartial {904				telemetry,905				eth_filter_pool,906				eth_backend,907				telemetry_worker_handle: _,908			},909	} = new_partial::<Runtime, RuntimeApi, ExecutorDispatch, _>(910		&config,911		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,912	)?;913	let net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);914	let prometheus_registry = config.prometheus_registry().cloned();915916	let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =917		sc_service::build_network(sc_service::BuildNetworkParams {918			config: &config,919			net_config,920			client: client.clone(),921			transaction_pool: transaction_pool.clone(),922			spawn_handle: task_manager.spawn_handle(),923			import_queue,924			block_announce_validator_builder: None,925			warp_sync_params: None,926			block_relay: None,927		})?;928929	let collator = config.role.is_authority();930931	let select_chain = maybe_select_chain;932933	if collator {934		let block_import = FrontierBlockImport::new(client.clone(), client.clone());935936		let env = sc_basic_authorship::ProposerFactory::new(937			task_manager.spawn_handle(),938			client.clone(),939			transaction_pool.clone(),940			prometheus_registry.as_ref(),941			telemetry.as_ref().map(|x| x.handle()),942		);943944		let transactions_commands_stream: Box<945			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,946		> = Box::new(947			transaction_pool948				.pool()949				.validated_pool()950				.import_notification_stream()951				.filter(move |_| futures::future::ready(!disable_autoseal_on_tx))952				.map(|_| EngineCommand::SealNewBlock {953					create_empty: true,954					finalize: false,955					parent_hash: None,956					sender: None,957				}),958		);959960		let autoseal_interval = Box::pin(AutosealInterval::new(&config, autoseal_interval));961962		let idle_commands_stream: Box<963			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,964		> = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock {965			create_empty: true,966			finalize: false,967			parent_hash: None,968			sender: None,969		}));970971		let commands_stream = select(transactions_commands_stream, idle_commands_stream);972973		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;974		let client_set_aside_for_cidp = client.clone();975976		if let Some(delay_sec) = autoseal_finalize_delay {977			let spawn_handle = task_manager.spawn_handle();978979			task_manager.spawn_essential_handle().spawn_blocking(980				"finalization_task",981				Some("block-authoring"),982				run_delayed_finalize(DelayedFinalizeParams {983					client: client.clone(),984					delay_sec,985					spawn_handle,986				}),987			);988		}989990		task_manager.spawn_essential_handle().spawn_blocking(991			"authorship_task",992			Some("block-authoring"),993			run_manual_seal(ManualSealParams {994				block_import,995				env,996				client: client.clone(),997				pool: transaction_pool.clone(),998				commands_stream,999				select_chain: select_chain.clone(),1000				consensus_data_provider: None,1001				create_inherent_data_providers: move |block: Hash, ()| {1002					let current_para_block = client_set_aside_for_cidp1003						.number(block)1004						.expect("Header lookup should succeed")1005						.expect("Header passed in as parent should be present in backend.");10061007					let client_for_xcm = client_set_aside_for_cidp.clone();1008					async move {1009						let time = sp_timestamp::InherentDataProvider::from_system_time();10101011						let mocked_parachain = cumulus_client_parachain_inherent::MockValidationDataInherentDataProvider {1012							current_para_block,1013							relay_offset: 1000,1014							relay_blocks_per_para_block: 2,1015							para_blocks_per_relay_epoch: 0,1016							xcm_config: cumulus_client_parachain_inherent::MockXcmConfig::new(1017								&*client_for_xcm,1018								block,1019								Default::default(),1020								Default::default(),1021							),1022							relay_randomness_config: (),1023							raw_downward_messages: vec![],1024							raw_horizontal_messages: vec![],1025							additional_key_values: None,1026						};10271028						let slot =1029						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(1030							*time,1031							slot_duration,1032						);10331034						Ok((time, slot, mocked_parachain))1035					}1036				},1037			}),1038		);1039	}10401041	#[cfg(feature = "pov-estimate")]1042	let rpc_backend = backend.clone();10431044	let runtime_id = config.chain_spec.runtime_id();10451046	// Frontier1047	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));1048	let fee_history_limit = 2048;10491050	let eth_pubsub_notification_sinks: Arc<1051		EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,1052	> = Default::default();10531054	let overrides = overrides_handle(client.clone());1055	let eth_block_data_cache = spawn_frontier_tasks(1056		FrontierTaskParams {1057			client: client.clone(),1058			substrate_backend: backend.clone(),1059			eth_filter_pool: eth_filter_pool.clone(),1060			eth_backend: eth_backend.clone(),1061			fee_history_limit,1062			fee_history_cache: fee_history_cache.clone(),1063			task_manager: &task_manager,1064			prometheus_registry,1065			overrides: overrides.clone(),1066			sync_strategy: SyncStrategy::Normal,1067		},1068		sync_service.clone(),1069		eth_pubsub_notification_sinks.clone(),1070	);10711072	// Rpc1073	let rpc_builder = Box::new({1074		clone!(1075			client,1076			backend,1077			eth_backend,1078			eth_pubsub_notification_sinks,1079			fee_history_cache,1080			eth_block_data_cache,1081			overrides,1082			transaction_pool,1083			network,1084			sync_service,1085		);1086		move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| {1087			clone!(1088				backend,1089				eth_block_data_cache,1090				client,1091				eth_backend,1092				eth_filter_pool,1093				eth_pubsub_notification_sinks,1094				fee_history_cache,1095				eth_block_data_cache,1096				network,1097				runtime_id,1098				transaction_pool,1099				select_chain,1100				overrides,1101			);11021103			#[cfg(not(feature = "pov-estimate"))]1104			let _ = backend;11051106			let mut rpc_module = RpcModule::new(());11071108			let full_deps = FullDeps {1109				runtime_id,11101111				#[cfg(feature = "pov-estimate")]1112				exec_params: uc_rpc::pov_estimate::ExecutorParams {1113					wasm_method: config.wasm_method,1114					default_heap_pages: config.default_heap_pages,1115					max_runtime_instances: config.max_runtime_instances,1116					runtime_cache_size: config.runtime_cache_size,1117				},11181119				#[cfg(feature = "pov-estimate")]1120				backend,1121				// eth_backend,1122				deny_unsafe,1123				client: client.clone(),1124				pool: transaction_pool.clone(),1125				select_chain,1126			};11271128			create_full::<_, _, _, Runtime, _>(&mut rpc_module, full_deps)?;11291130			let eth_deps = EthDeps {1131				client,1132				graph: transaction_pool.pool().clone(),1133				pool: transaction_pool,1134				is_authority: true,1135				network,1136				eth_backend,1137				// TODO: Unhardcode1138				max_past_logs: 10000,1139				fee_history_limit,1140				fee_history_cache,1141				eth_block_data_cache,1142				// TODO: Unhardcode1143				enable_dev_signer: false,1144				eth_filter_pool,1145				eth_pubsub_notification_sinks,1146				overrides,1147				sync: sync_service.clone(),1148				// We don't have any inherents except parachain built-ins, which we can't even extract from inside `run_aura`.1149				pending_create_inherent_data_providers: |_, ()| async move {1150					Ok((ethereum_parachain_inherent(),))1151				},1152			};11531154			create_eth::<1155				_,1156				_,1157				_,1158				_,1159				_,1160				_,1161				DefaultEthConfig<FullClient<RuntimeApi, ExecutorDispatch>>,1162			>(1163				&mut rpc_module,1164				eth_deps,1165				subscription_task_executor.clone(),1166			)?;11671168			Ok(rpc_module)1169		}1170	});11711172	sc_service::spawn_tasks(sc_service::SpawnTasksParams {1173		network,1174		sync_service,1175		client,1176		keystore: keystore_container.keystore(),1177		task_manager: &mut task_manager,1178		transaction_pool,1179		rpc_builder,1180		backend,1181		system_rpc_tx,1182		config,1183		telemetry: None,1184		tx_handler_controller,1185	})?;11861187	network_starter.start_network();1188	Ok(task_manager)1189}11901191fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<OverrideHandle<Block>>1192where1193	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,1194	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,1195	C: Send + Sync + 'static,1196	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,1197	BE: Backend<Block> + 'static,1198	BE::State: StateBackend<BlakeTwo256>,1199{1200	let mut overrides_map = BTreeMap::new();1201	overrides_map.insert(1202		EthereumStorageSchema::V1,1203		Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1204	);1205	overrides_map.insert(1206		EthereumStorageSchema::V2,1207		Box::new(SchemaV2Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1208	);1209	overrides_map.insert(1210		EthereumStorageSchema::V3,1211		Box::new(SchemaV3Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1212	);12131214	Arc::new(OverrideHandle {1215		schemas: overrides_map,1216		fallback: Box::new(RuntimeApiStorageOverride::new(client)),1217	})1218}12191220pub struct FrontierTaskParams<'a, C, B> {1221	pub task_manager: &'a TaskManager,1222	pub client: Arc<C>,1223	pub substrate_backend: Arc<B>,1224	pub eth_backend: Arc<fc_db::kv::Backend<Block>>,1225	pub eth_filter_pool: Option<FilterPool>,1226	pub overrides: Arc<OverrideHandle<Block>>,1227	pub fee_history_limit: u64,1228	pub fee_history_cache: FeeHistoryCache,1229	pub sync_strategy: SyncStrategy,1230	pub prometheus_registry: Option<Registry>,1231}12321233pub fn spawn_frontier_tasks<C, B>(1234	params: FrontierTaskParams<C, B>,1235	sync: Arc<SyncingService<Block>>,1236	pubsub_notification_sinks: Arc<1237		EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,1238	>,1239) -> Arc<EthBlockDataCacheTask<Block>>1240where1241	C: ProvideRuntimeApi<Block> + BlockOf,1242	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,1243	C: BlockchainEvents<Block> + StorageProvider<Block, B>,1244	C: Send + Sync + 'static,1245	C::Api: EthereumRuntimeRPCApi<Block>,1246	C::Api: BlockBuilder<Block>,1247	B: Backend<Block> + 'static,1248	B::State: StateBackend<BlakeTwo256>,1249{1250	let FrontierTaskParams {1251		task_manager,1252		client,1253		substrate_backend,1254		eth_backend,1255		eth_filter_pool,1256		overrides,1257		fee_history_limit,1258		fee_history_cache,1259		sync_strategy,1260		prometheus_registry,1261	} = params;1262	// Frontier offchain DB task. Essential.1263	// Maps emulated ethereum data to substrate native data.1264	params.task_manager.spawn_essential_handle().spawn(1265		"frontier-mapping-sync-worker",1266		Some("frontier"),1267		MappingSyncWorker::new(1268			client.import_notification_stream(),1269			Duration::new(6, 0),1270			client.clone(),1271			substrate_backend,1272			overrides.clone(),1273			eth_backend,1274			3,1275			0,1276			sync_strategy,1277			sync,1278			pubsub_notification_sinks,1279		)1280		.for_each(|()| futures::future::ready(())),1281	);12821283	// Frontier `EthFilterApi` maintenance.1284	// Manages the pool of user-created Filters.1285	if let Some(eth_filter_pool) = eth_filter_pool {1286		// Each filter is allowed to stay in the pool for 100 blocks.1287		const FILTER_RETAIN_THRESHOLD: u64 = 100;1288		params.task_manager.spawn_essential_handle().spawn(1289			"frontier-filter-pool",1290			Some("frontier"),1291			EthTask::filter_pool_task(client.clone(), eth_filter_pool, FILTER_RETAIN_THRESHOLD),1292		);1293	}12941295	// Spawn Frontier FeeHistory cache maintenance task.1296	params.task_manager.spawn_essential_handle().spawn(1297		"frontier-fee-history",1298		Some("frontier"),1299		EthTask::fee_history_task(1300			client,1301			overrides.clone(),1302			fee_history_cache,1303			fee_history_limit,1304		),1305	);13061307	Arc::new(EthBlockDataCacheTask::new(1308		task_manager.spawn_handle(),1309		overrides,1310		50,1311		50,1312		prometheus_registry,1313	))1314}