git.delta.rocks / unique-network / refs/commits / 0a49946aa7b2

difftreelog

source

node/cli/src/service.rs35.9 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// 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_network::RequireSecondedInBlockAnnounce;39use cumulus_client_service::{40	build_relay_chain_interface, prepare_node_config, start_relay_chain_tasks, DARecoveryProfile,41	StartRelayChainTasksParams,42};43use cumulus_primitives_core::ParaId;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, StateBackend};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_runtime::traits::BlakeTwo256;76use substrate_prometheus_endpoint::Registry;77use tokio::time::Interval;78use up_common::types::{opaque::*, Nonce};7980use crate::{81	chain_spec::RuntimeIdentification,82	rpc::{create_eth, create_full, EthDeps, FullDeps},83};8485/// Unique native executor instance.86#[cfg(feature = "unique-runtime")]87pub struct UniqueRuntimeExecutor;8889#[cfg(feature = "quartz-runtime")]90/// Quartz native executor instance.91pub struct QuartzRuntimeExecutor;9293/// Opal native executor instance.94pub struct OpalRuntimeExecutor;9596#[cfg(feature = "unique-runtime")]97impl NativeExecutionDispatch for UniqueRuntimeExecutor {98	/// Only enable the benchmarking host functions when we actually want to benchmark.99	#[cfg(feature = "runtime-benchmarks")]100	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;101	/// Otherwise we only use the default Substrate host functions.102	#[cfg(not(feature = "runtime-benchmarks"))]103	type ExtendHostFunctions = ();104105	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {106		unique_runtime::api::dispatch(method, data)107	}108109	fn native_version() -> sc_executor::NativeVersion {110		unique_runtime::native_version()111	}112}113114#[cfg(feature = "quartz-runtime")]115impl NativeExecutionDispatch for QuartzRuntimeExecutor {116	/// Only enable the benchmarking host functions when we actually want to benchmark.117	#[cfg(feature = "runtime-benchmarks")]118	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;119	/// Otherwise we only use the default Substrate host functions.120	#[cfg(not(feature = "runtime-benchmarks"))]121	type ExtendHostFunctions = ();122123	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {124		quartz_runtime::api::dispatch(method, data)125	}126127	fn native_version() -> sc_executor::NativeVersion {128		quartz_runtime::native_version()129	}130}131132impl NativeExecutionDispatch for OpalRuntimeExecutor {133	/// Only enable the benchmarking host functions when we actually want to benchmark.134	#[cfg(feature = "runtime-benchmarks")]135	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;136	/// Otherwise we only use the default Substrate host functions.137	#[cfg(not(feature = "runtime-benchmarks"))]138	type ExtendHostFunctions = ();139140	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {141		opal_runtime::api::dispatch(method, data)142	}143144	fn native_version() -> sc_executor::NativeVersion {145		opal_runtime::native_version()146	}147}148149pub struct AutosealInterval {150	interval: Interval,151}152153impl AutosealInterval {154	pub fn new(config: &Configuration, interval: u64) -> Self {155		let _tokio_runtime = config.tokio_handle.enter();156		let interval = tokio::time::interval(Duration::from_millis(interval));157158		Self { interval }159	}160}161162impl Stream for AutosealInterval {163	type Item = tokio::time::Instant;164165	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {166		self.interval.poll_tick(cx).map(Some)167	}168}169170pub fn open_frontier_backend<C: HeaderBackend<Block>>(171	client: Arc<C>,172	config: &Configuration,173) -> Result<Arc<fc_db::kv::Backend<Block>>, String> {174	let config_dir = config.base_path.config_dir(config.chain_spec.id());175	let database_dir = config_dir.join("frontier").join("db");176177	Ok(Arc::new(fc_db::kv::Backend::<Block>::new(178		client,179		&fc_db::kv::DatabaseSettings {180			source: fc_db::DatabaseSource::RocksDb {181				path: database_dir,182				cache_size: 0,183			},184		},185	)?))186}187188type FullClient<RuntimeApi, ExecutorDispatch> =189	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;190type FullBackend = sc_service::TFullBackend<Block>;191type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;192type ParachainBlockImport<RuntimeApi, ExecutorDispatch> =193	TParachainBlockImport<Block, Arc<FullClient<RuntimeApi, ExecutorDispatch>>, FullBackend>;194195/// Generate a supertrait based on bounds, and blanket impl for it.196macro_rules! ez_bounds {197	($vis:vis trait $name:ident$(<$($gen:ident $(: $($(+)? $bound:path)*)?),* $(,)?>)? $(:)? $($(+)? $super:path)* {}) => {198		$vis trait $name $(<$($gen $(: $($bound+)*)?,)*>)?: $($super +)* {}199		impl<T, $($($gen $(: $($bound+)*)?,)*)?> $name$(<$($gen,)*>)? for T200		where T: $($super +)* {}201	}202}203ez_bounds!(204	pub trait RuntimeApiDep<Runtime: RuntimeInstance>:205		sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>206		+ sp_consensus_aura::AuraApi<Block, AuraId>207		+ fp_rpc::EthereumRuntimeRPCApi<Block>208		+ sp_session::SessionKeys<Block>209		+ sp_block_builder::BlockBuilder<Block>210		+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>211		+ sp_api::ApiExt<Block>212		+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>213		+ app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>214		+ up_pov_estimate_rpc::PovEstimateApi<Block>215		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>216		+ sp_api::Metadata<Block>217		+ sp_offchain::OffchainWorkerApi<Block>218		+ cumulus_primitives_core::CollectCollationInfo<Block>219		// Deprecated, not used.220		+ fp_rpc::ConvertTransactionRuntimeApi<Block>221	{222	}223);224225/// Starts a `ServiceBuilder` for a full service.226///227/// Use this macro if you don't actually need the full service, but just the builder in order to228/// be able to perform chain operations.229#[allow(clippy::type_complexity)]230pub fn new_partial<Runtime, RuntimeApi, ExecutorDispatch, BIQ>(231	config: &Configuration,232	build_import_queue: BIQ,233) -> Result<234	PartialComponents<235		FullClient<RuntimeApi, ExecutorDispatch>,236		FullBackend,237		FullSelectChain,238		sc_consensus::DefaultImportQueue<Block>,239		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,240		OtherPartial,241	>,242	sc_service::Error,243>244where245	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,246	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>247		+ Send248		+ Sync249		+ 'static,250	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,251	Runtime: RuntimeInstance,252	ExecutorDispatch: NativeExecutionDispatch + 'static,253	BIQ: FnOnce(254		Arc<FullClient<RuntimeApi, ExecutorDispatch>>,255		Arc<FullBackend>,256		&Configuration,257		Option<TelemetryHandle>,258		&TaskManager,259	) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error>,260{261	let telemetry = config262		.telemetry_endpoints263		.clone()264		.filter(|x| !x.is_empty())265		.map(|endpoints| -> Result<_, sc_telemetry::Error> {266			let worker = TelemetryWorker::new(16)?;267			let telemetry = worker.handle().new_telemetry(endpoints);268			Ok((worker, telemetry))269		})270		.transpose()?;271272	let executor = sc_service::new_native_or_wasm_executor(config);273274	let (client, backend, keystore_container, task_manager) =275		sc_service::new_full_parts::<Block, RuntimeApi, _>(276			config,277			telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),278			executor,279		)?;280	let client = Arc::new(client);281282	let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());283284	let telemetry = telemetry.map(|(worker, telemetry)| {285		task_manager286			.spawn_handle()287			.spawn("telemetry", None, worker.run());288		telemetry289	});290291	let select_chain = sc_consensus::LongestChain::new(backend.clone());292293	let transaction_pool = sc_transaction_pool::BasicPool::new_full(294		config.transaction_pool.clone(),295		config.role.is_authority().into(),296		config.prometheus_registry(),297		task_manager.spawn_essential_handle(),298		client.clone(),299	);300301	let eth_filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));302303	let eth_backend = open_frontier_backend(client.clone(), config)?;304305	let import_queue = build_import_queue(306		client.clone(),307		backend.clone(),308		config,309		telemetry.as_ref().map(|telemetry| telemetry.handle()),310		&task_manager,311	)?;312313	let params = PartialComponents {314		backend,315		client,316		import_queue,317		keystore_container,318		task_manager,319		transaction_pool,320		select_chain,321		other: OtherPartial {322			telemetry,323			eth_filter_pool,324			eth_backend,325			telemetry_worker_handle,326		},327	};328329	Ok(params)330}331332macro_rules! clone {333    ($($i:ident),* $(,)?) => {334		$(335			let $i = $i.clone();336		)*337    };338}339340/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.341///342/// This is the actual implementation that is abstract over the executor and the runtime api.343#[sc_tracing::logging::prefix_logs_with("Parachain")]344pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(345	parachain_config: Configuration,346	polkadot_config: Configuration,347	collator_options: CollatorOptions,348	para_id: ParaId,349	hwbench: Option<sc_sysinfo::HwBench>,350) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>351where352	sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,353	Runtime: RuntimeInstance + Send + Sync + 'static,354	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,355	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,356	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>357		+ Send358		+ Sync359		+ 'static,360	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,361	Runtime: RuntimeInstance,362	ExecutorDispatch: NativeExecutionDispatch + 'static,363{364	let parachain_config = prepare_node_config(parachain_config);365366	let params = new_partial::<Runtime, RuntimeApi, ExecutorDispatch, _>(367		&parachain_config,368		parachain_build_import_queue,369	)?;370	let OtherPartial {371		mut telemetry,372		telemetry_worker_handle,373		eth_filter_pool,374		eth_backend,375	} = params.other;376	let net_config = sc_network::config::FullNetworkConfiguration::new(&parachain_config.network);377378	let client = params.client.clone();379	let backend = params.backend.clone();380	let mut task_manager = params.task_manager;381382	let (relay_chain_interface, collator_key) = build_relay_chain_interface(383		polkadot_config,384		&parachain_config,385		telemetry_worker_handle,386		&mut task_manager,387		collator_options.clone(),388		hwbench.clone(),389	)390	.await391	.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;392393	let block_announce_validator =394		RequireSecondedInBlockAnnounce::new(relay_chain_interface.clone(), para_id);395396	let validator = parachain_config.role.is_authority();397	let prometheus_registry = parachain_config.prometheus_registry().cloned();398	let transaction_pool = params.transaction_pool.clone();399	let import_queue_service = params.import_queue.service();400401	let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =402		sc_service::build_network(sc_service::BuildNetworkParams {403			config: &parachain_config,404			net_config,405			client: client.clone(),406			transaction_pool: transaction_pool.clone(),407			spawn_handle: task_manager.spawn_handle(),408			import_queue: params.import_queue,409			block_announce_validator_builder: Some(Box::new(|_| {410				Box::new(block_announce_validator)411			})),412			warp_sync_params: None,413		})?;414415	let select_chain = params.select_chain.clone();416417	let runtime_id = parachain_config.chain_spec.runtime_id();418419	// Frontier420	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));421	let fee_history_limit = 2048;422423	let eth_pubsub_notification_sinks: Arc<424		EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,425	> = Default::default();426427	let overrides = overrides_handle(client.clone());428	let eth_block_data_cache = spawn_frontier_tasks(429		FrontierTaskParams {430			client: client.clone(),431			substrate_backend: backend.clone(),432			eth_filter_pool: eth_filter_pool.clone(),433			eth_backend: eth_backend.clone(),434			fee_history_limit,435			fee_history_cache: fee_history_cache.clone(),436			task_manager: &task_manager,437			prometheus_registry: prometheus_registry.clone(),438			overrides: overrides.clone(),439			sync_strategy: SyncStrategy::Parachain,440		},441		sync_service.clone(),442		eth_pubsub_notification_sinks.clone(),443	);444445	// Rpc446	let rpc_builder = Box::new({447		clone!(448			client,449			backend,450			eth_backend,451			eth_pubsub_notification_sinks,452			fee_history_cache,453			eth_block_data_cache,454			overrides,455			transaction_pool,456			network,457			sync_service,458		);459		move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| {460			clone!(461				backend,462				eth_block_data_cache,463				client,464				eth_backend,465				eth_filter_pool,466				eth_pubsub_notification_sinks,467				fee_history_cache,468				eth_block_data_cache,469				network,470				runtime_id,471				transaction_pool,472				select_chain,473				overrides,474			);475476			#[cfg(not(feature = "pov-estimate"))]477			let _ = backend;478479			let mut rpc_handle = RpcModule::new(());480481			let full_deps = FullDeps {482				client: client.clone(),483				runtime_id,484485				#[cfg(feature = "pov-estimate")]486				exec_params: uc_rpc::pov_estimate::ExecutorParams {487					wasm_method: parachain_config.wasm_method,488					default_heap_pages: parachain_config.default_heap_pages,489					max_runtime_instances: parachain_config.max_runtime_instances,490					runtime_cache_size: parachain_config.runtime_cache_size,491				},492493				#[cfg(feature = "pov-estimate")]494				backend,495496				deny_unsafe,497				pool: transaction_pool.clone(),498				select_chain,499			};500501			create_full::<_, _, _, Runtime, _>(&mut rpc_handle, full_deps)?;502503			let eth_deps = EthDeps {504				client,505				graph: transaction_pool.pool().clone(),506				pool: transaction_pool,507				is_authority: validator,508				network,509				eth_backend,510				// TODO: Unhardcode511				max_past_logs: 10000,512				fee_history_limit,513				fee_history_cache,514				eth_block_data_cache,515				// TODO: Unhardcode516				enable_dev_signer: false,517				eth_filter_pool,518				eth_pubsub_notification_sinks,519				overrides,520				sync: sync_service.clone(),521				pending_create_inherent_data_providers: |_, ()| async move { Ok(()) },522			};523524			create_eth::<525				_,526				_,527				_,528				_,529				_,530				_,531				DefaultEthConfig<FullClient<RuntimeApi, ExecutorDispatch>>,532			>(533				&mut rpc_handle,534				eth_deps,535				subscription_task_executor.clone(),536			)?;537538			Ok(rpc_handle)539		}540	});541542	sc_service::spawn_tasks(sc_service::SpawnTasksParams {543		rpc_builder,544		client: client.clone(),545		transaction_pool: transaction_pool.clone(),546		task_manager: &mut task_manager,547		config: parachain_config,548		keystore: params.keystore_container.keystore(),549		backend: backend.clone(),550		network,551		sync_service: sync_service.clone(),552		system_rpc_tx,553		telemetry: telemetry.as_mut(),554		tx_handler_controller,555	})?;556557	if let Some(hwbench) = hwbench {558		sc_sysinfo::print_hwbench(&hwbench);559560		if let Some(ref mut telemetry) = telemetry {561			let telemetry_handle = telemetry.handle();562			task_manager.spawn_handle().spawn(563				"telemetry_hwbench",564				None,565				sc_sysinfo::initialize_hwbench_telemetry(telemetry_handle, hwbench),566			);567		}568	}569570	let announce_block = {571		let sync_service = sync_service.clone();572		Arc::new(Box::new(move |hash, data| {573			sync_service.announce_block(hash, data)574		}))575	};576577	let relay_chain_slot_duration = Duration::from_secs(6);578579	let overseer_handle = relay_chain_interface580		.overseer_handle()581		.map_err(|e| sc_service::Error::Application(Box::new(e)))?;582583	start_relay_chain_tasks(StartRelayChainTasksParams {584		client: client.clone(),585		announce_block: announce_block.clone(),586		para_id,587		relay_chain_interface: relay_chain_interface.clone(),588		task_manager: &mut task_manager,589		da_recovery_profile: if validator {590			DARecoveryProfile::Collator591		} else {592			DARecoveryProfile::FullNode593		},594		import_queue: import_queue_service,595		relay_chain_slot_duration,596		recovery_handle: Box::new(overseer_handle.clone()),597		sync_service: sync_service.clone(),598	})?;599600	if validator {601		start_consensus(602			client.clone(),603			transaction_pool,604			StartConsensusParameters {605				backend: backend.clone(),606				prometheus_registry: prometheus_registry.as_ref(),607				telemetry: telemetry.as_ref().map(|t| t.handle()),608				task_manager: &task_manager,609				relay_chain_interface: relay_chain_interface.clone(),610				sync_oracle: sync_service,611				keystore: params.keystore_container.keystore(),612				overseer_handle,613				relay_chain_slot_duration,614				para_id,615				collator_key: collator_key.expect("cli args do not allow this"),616				announce_block,617			},618		)?;619	}620621	start_network.start_network();622623	Ok((task_manager, client))624}625626/// Build the import queue for the the parachain runtime.627pub fn parachain_build_import_queue<Runtime, RuntimeApi, ExecutorDispatch>(628	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,629	backend: Arc<FullBackend>,630	config: &Configuration,631	telemetry: Option<TelemetryHandle>,632	task_manager: &TaskManager,633) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error>634where635	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>636		+ Send637		+ Sync638		+ 'static,639	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,640	Runtime: RuntimeInstance,641	ExecutorDispatch: NativeExecutionDispatch + 'static,642{643	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;644645	let block_import = ParachainBlockImport::new(client.clone(), backend);646647	cumulus_client_consensus_aura::import_queue::<648		sp_consensus_aura::sr25519::AuthorityPair,649		_,650		_,651		_,652		_,653		_,654	>(cumulus_client_consensus_aura::ImportQueueParams {655		block_import,656		client,657		create_inherent_data_providers: move |_, _| async move {658			let time = sp_timestamp::InherentDataProvider::from_system_time();659660			let slot =661				sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(662					*time,663					slot_duration,664				);665666			Ok((slot, time))667		},668		registry: config.prometheus_registry(),669		spawner: &task_manager.spawn_essential_handle(),670		telemetry,671	})672	.map_err(Into::into)673}674675pub struct StartConsensusParameters<'a> {676	backend: Arc<FullBackend>,677	prometheus_registry: Option<&'a Registry>,678	telemetry: Option<TelemetryHandle>,679	task_manager: &'a TaskManager,680	relay_chain_interface: Arc<dyn RelayChainInterface>,681	sync_oracle: Arc<SyncingService<Block>>,682	keystore: KeystorePtr,683	overseer_handle: OverseerHandle,684	relay_chain_slot_duration: Duration,685	para_id: ParaId,686	collator_key: CollatorPair,687	announce_block: Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>,688}689690pub fn start_consensus<ExecutorDispatch, RuntimeApi, Runtime>(691	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,692	transaction_pool: Arc<693		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,694	>,695	parameters: StartConsensusParameters<'_>,696) -> Result<(), sc_service::Error>697where698	ExecutorDispatch: NativeExecutionDispatch + 'static,699	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>700		+ Send701		+ Sync702		+ 'static,703	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,704	Runtime: RuntimeInstance,705{706	let StartConsensusParameters {707		backend,708		prometheus_registry,709		telemetry,710		task_manager,711		relay_chain_interface,712		sync_oracle,713		keystore,714		overseer_handle,715		relay_chain_slot_duration,716		para_id,717		collator_key,718		announce_block,719	} = parameters;720	let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;721722	let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(723		task_manager.spawn_handle(),724		client.clone(),725		transaction_pool,726		prometheus_registry,727		telemetry,728	);729	let proposer = Proposer::new(proposer_factory);730731	let collator_service = CollatorService::new(732		client.clone(),733		Arc::new(task_manager.spawn_handle()),734		announce_block,735		client.clone(),736	);737738	let block_import = ParachainBlockImport::new(client.clone(), backend);739740	let params = BuildAuraConsensusParams {741		create_inherent_data_providers: move |_, ()| async move { Ok(()) },742		block_import,743		para_client: client,744		#[cfg(feature = "lookahead")]745		para_backend: backend,746		para_id,747		relay_client: relay_chain_interface,748		sync_oracle,749		keystore,750		slot_duration,751		proposer,752		collator_service,753		// With async-baking, we allowed to be both slower (longer authoring) and faster (multiple para blocks per relay block)754		authoring_duration: Duration::from_millis(500),755		overseer_handle,756		#[cfg(feature = "lookahead")]757		code_hash_provider: || {},758		collator_key,759		relay_chain_slot_duration,760	};761762	task_manager.spawn_essential_handle().spawn(763		"aura",764		None,765		run_aura::<_, AuraAuthorityPair, _, _, _, _, _, _, _>(params),766	);767	Ok(())768}769770fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(771	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,772	_: Arc<FullBackend>,773	config: &Configuration,774	_: Option<TelemetryHandle>,775	task_manager: &TaskManager,776) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error>777where778	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>779		+ Send780		+ Sync781		+ 'static,782	RuntimeApi::RuntimeApi:783		sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> + sp_api::ApiExt<Block>,784	ExecutorDispatch: NativeExecutionDispatch + 'static,785{786	Ok(sc_consensus_manual_seal::import_queue(787		Box::new(client),788		&task_manager.spawn_essential_handle(),789		config.prometheus_registry(),790	))791}792793pub struct OtherPartial {794	pub telemetry: Option<Telemetry>,795	pub telemetry_worker_handle: Option<TelemetryWorkerHandle>,796	pub eth_filter_pool: Option<FilterPool>,797	pub eth_backend: Arc<fc_db::kv::Backend<Block>>,798}799800struct DefaultEthConfig<C>(PhantomData<C>);801impl<C> EthConfig<Block, C> for DefaultEthConfig<C>802where803	C: StorageProvider<Block, FullBackend> + Sync + Send + 'static,804{805	type EstimateGasAdapter = ();806	type RuntimeStorageOverride = SystemAccountId32StorageOverride<Block, C, FullBackend>;807}808809/// Builds a new development service. This service uses instant seal, and mocks810/// the parachain inherent811pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(812	config: Configuration,813	autoseal_interval: u64,814	autoseal_finalize_delay: Option<u64>,815	disable_autoseal_on_tx: bool,816) -> sc_service::error::Result<TaskManager>817where818	Runtime: RuntimeInstance + Send + Sync + 'static,819	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,820	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,821	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>822		+ Send823		+ Sync824		+ 'static,825	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,826	ExecutorDispatch: NativeExecutionDispatch + 'static,827{828	use fc_consensus::FrontierBlockImport;829	use sc_consensus_manual_seal::{830		run_delayed_finalize, run_manual_seal, DelayedFinalizeParams, EngineCommand,831		ManualSealParams,832	};833834	let sc_service::PartialComponents {835		client,836		backend,837		mut task_manager,838		import_queue,839		keystore_container,840		select_chain: maybe_select_chain,841		transaction_pool,842		other:843			OtherPartial {844				telemetry,845				eth_filter_pool,846				eth_backend,847				telemetry_worker_handle: _,848			},849	} = new_partial::<Runtime, RuntimeApi, ExecutorDispatch, _>(850		&config,851		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,852	)?;853	let net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);854	let prometheus_registry = config.prometheus_registry().cloned();855856	let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =857		sc_service::build_network(sc_service::BuildNetworkParams {858			config: &config,859			net_config,860			client: client.clone(),861			transaction_pool: transaction_pool.clone(),862			spawn_handle: task_manager.spawn_handle(),863			import_queue,864			block_announce_validator_builder: None,865			warp_sync_params: None,866		})?;867868	let collator = config.role.is_authority();869870	let select_chain = maybe_select_chain;871872	if collator {873		let block_import = FrontierBlockImport::new(client.clone(), client.clone());874875		let env = sc_basic_authorship::ProposerFactory::new(876			task_manager.spawn_handle(),877			client.clone(),878			transaction_pool.clone(),879			prometheus_registry.as_ref(),880			telemetry.as_ref().map(|x| x.handle()),881		);882883		let transactions_commands_stream: Box<884			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,885		> = Box::new(886			transaction_pool887				.pool()888				.validated_pool()889				.import_notification_stream()890				.filter(move |_| futures::future::ready(!disable_autoseal_on_tx))891				.map(|_| EngineCommand::SealNewBlock {892					create_empty: true,893					finalize: false,894					parent_hash: None,895					sender: None,896				}),897		);898899		let autoseal_interval = Box::pin(AutosealInterval::new(&config, autoseal_interval));900901		let idle_commands_stream: Box<902			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,903		> = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock {904			create_empty: true,905			finalize: false,906			parent_hash: None,907			sender: None,908		}));909910		let commands_stream = select(transactions_commands_stream, idle_commands_stream);911912		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;913		let client_set_aside_for_cidp = client.clone();914915		if let Some(delay_sec) = autoseal_finalize_delay {916			let spawn_handle = task_manager.spawn_handle();917918			task_manager.spawn_essential_handle().spawn_blocking(919				"finalization_task",920				Some("block-authoring"),921				run_delayed_finalize(DelayedFinalizeParams {922					client: client.clone(),923					delay_sec,924					spawn_handle,925				}),926			);927		}928929		task_manager.spawn_essential_handle().spawn_blocking(930			"authorship_task",931			Some("block-authoring"),932			run_manual_seal(ManualSealParams {933				block_import,934				env,935				client: client.clone(),936				pool: transaction_pool.clone(),937				commands_stream,938				select_chain: select_chain.clone(),939				consensus_data_provider: None,940				create_inherent_data_providers: move |block: Hash, ()| {941					let current_para_block = client_set_aside_for_cidp942						.number(block)943						.expect("Header lookup should succeed")944						.expect("Header passed in as parent should be present in backend.");945946					let client_for_xcm = client_set_aside_for_cidp.clone();947					async move {948						let time = sp_timestamp::InherentDataProvider::from_system_time();949950						let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {951							current_para_block,952							relay_offset: 1000,953							relay_blocks_per_para_block: 2,954							para_blocks_per_relay_epoch: 0,955							xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(956								&*client_for_xcm,957								block,958								Default::default(),959								Default::default(),960							),961							relay_randomness_config: (),962							raw_downward_messages: vec![],963							raw_horizontal_messages: vec![],964						};965966						let slot =967						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(968							*time,969							slot_duration,970						);971972						Ok((time, slot, mocked_parachain))973					}974				},975			}),976		);977	}978979	#[cfg(feature = "pov-estimate")]980	let rpc_backend = backend.clone();981982	let runtime_id = config.chain_spec.runtime_id();983984	// Frontier985	let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));986	let fee_history_limit = 2048;987988	let eth_pubsub_notification_sinks: Arc<989		EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,990	> = Default::default();991992	let overrides = overrides_handle(client.clone());993	let eth_block_data_cache = spawn_frontier_tasks(994		FrontierTaskParams {995			client: client.clone(),996			substrate_backend: backend.clone(),997			eth_filter_pool: eth_filter_pool.clone(),998			eth_backend: eth_backend.clone(),999			fee_history_limit,1000			fee_history_cache: fee_history_cache.clone(),1001			task_manager: &task_manager,1002			prometheus_registry,1003			overrides: overrides.clone(),1004			sync_strategy: SyncStrategy::Normal,1005		},1006		sync_service.clone(),1007		eth_pubsub_notification_sinks.clone(),1008	);10091010	// Rpc1011	let rpc_builder = Box::new({1012		clone!(1013			client,1014			backend,1015			eth_backend,1016			eth_pubsub_notification_sinks,1017			fee_history_cache,1018			eth_block_data_cache,1019			overrides,1020			transaction_pool,1021			network,1022			sync_service,1023		);1024		move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| {1025			clone!(1026				backend,1027				eth_block_data_cache,1028				client,1029				eth_backend,1030				eth_filter_pool,1031				eth_pubsub_notification_sinks,1032				fee_history_cache,1033				eth_block_data_cache,1034				network,1035				runtime_id,1036				transaction_pool,1037				select_chain,1038				overrides,1039			);10401041			#[cfg(not(feature = "pov-estimate"))]1042			let _ = backend;10431044			let mut rpc_module = RpcModule::new(());10451046			let full_deps = FullDeps {1047				runtime_id,10481049				#[cfg(feature = "pov-estimate")]1050				exec_params: uc_rpc::pov_estimate::ExecutorParams {1051					wasm_method: config.wasm_method,1052					default_heap_pages: config.default_heap_pages,1053					max_runtime_instances: config.max_runtime_instances,1054					runtime_cache_size: config.runtime_cache_size,1055				},10561057				#[cfg(feature = "pov-estimate")]1058				backend,1059				// eth_backend,1060				deny_unsafe,1061				client: client.clone(),1062				pool: transaction_pool.clone(),1063				select_chain,1064			};10651066			create_full::<_, _, _, Runtime, _>(&mut rpc_module, full_deps)?;10671068			let eth_deps = EthDeps {1069				client,1070				graph: transaction_pool.pool().clone(),1071				pool: transaction_pool,1072				is_authority: true,1073				network,1074				eth_backend,1075				// TODO: Unhardcode1076				max_past_logs: 10000,1077				fee_history_limit,1078				fee_history_cache,1079				eth_block_data_cache,1080				// TODO: Unhardcode1081				enable_dev_signer: false,1082				eth_filter_pool,1083				eth_pubsub_notification_sinks,1084				overrides,1085				sync: sync_service.clone(),1086				// We don't have any inherents except parachain built-ins, which we can't even extract from inside `run_aura`.1087				pending_create_inherent_data_providers: |_, ()| async move { Ok(()) },1088			};10891090			create_eth::<1091				_,1092				_,1093				_,1094				_,1095				_,1096				_,1097				DefaultEthConfig<FullClient<RuntimeApi, ExecutorDispatch>>,1098			>(1099				&mut rpc_module,1100				eth_deps,1101				subscription_task_executor.clone(),1102			)?;11031104			Ok(rpc_module)1105		}1106	});11071108	sc_service::spawn_tasks(sc_service::SpawnTasksParams {1109		network,1110		sync_service,1111		client,1112		keystore: keystore_container.keystore(),1113		task_manager: &mut task_manager,1114		transaction_pool,1115		rpc_builder,1116		backend,1117		system_rpc_tx,1118		config,1119		telemetry: None,1120		tx_handler_controller,1121	})?;11221123	network_starter.start_network();1124	Ok(task_manager)1125}11261127fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<OverrideHandle<Block>>1128where1129	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,1130	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,1131	C: Send + Sync + 'static,1132	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,1133	BE: Backend<Block> + 'static,1134	BE::State: StateBackend<BlakeTwo256>,1135{1136	let mut overrides_map = BTreeMap::new();1137	overrides_map.insert(1138		EthereumStorageSchema::V1,1139		Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1140	);1141	overrides_map.insert(1142		EthereumStorageSchema::V2,1143		Box::new(SchemaV2Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1144	);1145	overrides_map.insert(1146		EthereumStorageSchema::V3,1147		Box::new(SchemaV3Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1148	);11491150	Arc::new(OverrideHandle {1151		schemas: overrides_map,1152		fallback: Box::new(RuntimeApiStorageOverride::new(client)),1153	})1154}11551156pub struct FrontierTaskParams<'a, C, B> {1157	pub task_manager: &'a TaskManager,1158	pub client: Arc<C>,1159	pub substrate_backend: Arc<B>,1160	pub eth_backend: Arc<fc_db::kv::Backend<Block>>,1161	pub eth_filter_pool: Option<FilterPool>,1162	pub overrides: Arc<OverrideHandle<Block>>,1163	pub fee_history_limit: u64,1164	pub fee_history_cache: FeeHistoryCache,1165	pub sync_strategy: SyncStrategy,1166	pub prometheus_registry: Option<Registry>,1167}11681169pub fn spawn_frontier_tasks<C, B>(1170	params: FrontierTaskParams<C, B>,1171	sync: Arc<SyncingService<Block>>,1172	pubsub_notification_sinks: Arc<1173		EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,1174	>,1175) -> Arc<EthBlockDataCacheTask<Block>>1176where1177	C: ProvideRuntimeApi<Block> + BlockOf,1178	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,1179	C: BlockchainEvents<Block> + StorageProvider<Block, B>,1180	C: Send + Sync + 'static,1181	C::Api: EthereumRuntimeRPCApi<Block>,1182	C::Api: BlockBuilder<Block>,1183	B: Backend<Block> + 'static,1184	B::State: StateBackend<BlakeTwo256>,1185{1186	let FrontierTaskParams {1187		task_manager,1188		client,1189		substrate_backend,1190		eth_backend,1191		eth_filter_pool,1192		overrides,1193		fee_history_limit,1194		fee_history_cache,1195		sync_strategy,1196		prometheus_registry,1197	} = params;1198	// Frontier offchain DB task. Essential.1199	// Maps emulated ethereum data to substrate native data.1200	params.task_manager.spawn_essential_handle().spawn(1201		"frontier-mapping-sync-worker",1202		Some("frontier"),1203		MappingSyncWorker::new(1204			client.import_notification_stream(),1205			Duration::new(6, 0),1206			client.clone(),1207			substrate_backend,1208			overrides.clone(),1209			eth_backend,1210			3,1211			0,1212			sync_strategy,1213			sync,1214			pubsub_notification_sinks,1215		)1216		.for_each(|()| futures::future::ready(())),1217	);12181219	// Frontier `EthFilterApi` maintenance.1220	// Manages the pool of user-created Filters.1221	if let Some(eth_filter_pool) = eth_filter_pool {1222		// Each filter is allowed to stay in the pool for 100 blocks.1223		const FILTER_RETAIN_THRESHOLD: u64 = 100;1224		params.task_manager.spawn_essential_handle().spawn(1225			"frontier-filter-pool",1226			Some("frontier"),1227			EthTask::filter_pool_task(client.clone(), eth_filter_pool, FILTER_RETAIN_THRESHOLD),1228		);1229	}12301231	// Spawn Frontier FeeHistory cache maintenance task.1232	params.task_manager.spawn_essential_handle().spawn(1233		"frontier-fee-history",1234		Some("frontier"),1235		EthTask::fee_history_task(1236			client,1237			overrides.clone(),1238			fee_history_cache,1239			fee_history_limit,1240		),1241	);12421243	Arc::new(EthBlockDataCacheTask::new(1244		task_manager.spawn_handle(),1245		overrides,1246		50,1247		50,1248		prometheus_registry,1249	))1250}