1234567891011121314151617181920use 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};303132use 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;414243use 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;545556use fc_rpc_core::types::FilterPool;57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};5859use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};60use crate::chain_spec::ServiceId;616263pub struct UniqueRuntimeExecutor;64pub struct QuartzRuntimeExecutor;65pub struct OpalRuntimeExecutor;6667#[cfg(feature = "unique-runtime")]68impl NativeExecutionDispatch for UniqueRuntimeExecutor {69 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7071 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {72 unique_runtime::api::dispatch(method, data)73 }7475 fn native_version() -> sc_executor::NativeVersion {76 unique_runtime::native_version()77 }78}7980#[cfg(feature = "quartz-runtime")]81impl NativeExecutionDispatch for QuartzRuntimeExecutor {82 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8384 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {85 quartz_runtime::api::dispatch(method, data)86 }8788 fn native_version() -> sc_executor::NativeVersion {89 quartz_runtime::native_version()90 }91}9293impl NativeExecutionDispatch for OpalRuntimeExecutor {94 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9596 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {97 opal_runtime::api::dispatch(method, data)98 }99100 fn native_version() -> sc_executor::NativeVersion {101 opal_runtime::native_version()102 }103}104105pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {106 let config_dir = config107 .base_path108 .as_ref()109 .map(|base_path| base_path.config_dir(config.chain_spec.id()))110 .unwrap_or_else(|| {111 BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())112 });113 let database_dir = config_dir.join("frontier").join("db");114115 Ok(Arc::new(fc_db::Backend::<Block>::new(116 &fc_db::DatabaseSettings {117 source: fc_db::DatabaseSettingsSrc::RocksDb {118 path: database_dir,119 cache_size: 0,120 },121 },122 )?))123}124125type FullClient<RuntimeApi, ExecutorDispatch> =126 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;127type FullBackend = sc_service::TFullBackend<Block>;128type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;129130131132133134#[allow(clippy::type_complexity)]135pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(136 config: &Configuration,137 build_import_queue: BIQ,138 service_id: ServiceId,139) -> Result<140 PartialComponents<141 FullClient<RuntimeApi, ExecutorDispatch>,142 FullBackend,143 FullSelectChain,144 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,145 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,146 (147 Option<Telemetry>,148 Option<FilterPool>,149 Arc<fc_db::Backend<Block>>,150 Option<TelemetryWorkerHandle>,151 FeeHistoryCache,152 ),153 >,154 sc_service::Error,155>156where157 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,158 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>159 + Send160 + Sync161 + 'static,162 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,163 ExecutorDispatch: NativeExecutionDispatch + 'static,164 BIQ: FnOnce(165 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,166 &Configuration,167 Option<TelemetryHandle>,168 &TaskManager,169 ) -> Result<170 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,171 sc_service::Error,172 >,173{174 let _telemetry = config175 .telemetry_endpoints176 .clone()177 .filter(|x| !x.is_empty())178 .map(|endpoints| -> Result<_, sc_telemetry::Error> {179 let worker = TelemetryWorker::new(16)?;180 let telemetry = worker.handle().new_telemetry(endpoints);181 Ok((worker, telemetry))182 })183 .transpose()?;184185 let telemetry = config186 .telemetry_endpoints187 .clone()188 .filter(|x| !x.is_empty())189 .map(|endpoints| -> Result<_, sc_telemetry::Error> {190 let worker = TelemetryWorker::new(16)?;191 let telemetry = worker.handle().new_telemetry(endpoints);192 Ok((worker, telemetry))193 })194 .transpose()?;195196 let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(197 config.wasm_method,198 config.default_heap_pages,199 config.max_runtime_instances,200 config.runtime_cache_size,201 );202203 let (client, backend, keystore_container, task_manager) =204 sc_service::new_full_parts::<Block, RuntimeApi, _>(205 config,206 telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),207 executor,208 )?;209 let client = Arc::new(client);210211 let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());212213 let telemetry = telemetry.map(|(worker, telemetry)| {214 task_manager215 .spawn_handle()216 .spawn("telemetry", None, worker.run());217 telemetry218 });219220 let select_chain = sc_consensus::LongestChain::new(backend.clone());221222 let transaction_pool = sc_transaction_pool::BasicPool::new_full(223 config.transaction_pool.clone(),224 config.role.is_authority().into(),225 config.prometheus_registry(),226 task_manager.spawn_essential_handle(),227 client.clone(),228 );229230 let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));231232 let frontier_backend = open_frontier_backend(config)?;233234 let import_queue = build_import_queue(235 client.clone(),236 config,237 telemetry.as_ref().map(|telemetry| telemetry.handle()),238 &task_manager,239 )?;240 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));241242 let params = PartialComponents {243 backend,244 client,245 import_queue,246 keystore_container,247 task_manager,248 transaction_pool,249 select_chain,250 other: (251 telemetry,252 filter_pool,253 frontier_backend,254 telemetry_worker_handle,255 fee_history_cache,256 ),257 };258259 Ok(params)260}261262263264265#[sc_tracing::logging::prefix_logs_with("Parachain")]266async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(267 parachain_config: Configuration,268 polkadot_config: Configuration,269 id: ParaId,270 build_import_queue: BIQ,271 build_consensus: BIC,272) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>273where274 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,275 Runtime: RuntimeInstance + Send + Sync + 'static,276 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,277 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,278 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>279 + Send280 + Sync281 + 'static,282 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>283 + fp_rpc::EthereumRuntimeRPCApi<Block>284 + sp_session::SessionKeys<Block>285 + sp_block_builder::BlockBuilder<Block>286 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>287 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>288 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>289 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>290 + sp_api::Metadata<Block>291 + sp_offchain::OffchainWorkerApi<Block>292 + cumulus_primitives_core::CollectCollationInfo<Block>,293 ExecutorDispatch: NativeExecutionDispatch + 'static,294 BIQ: FnOnce(295 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,296 &Configuration,297 Option<TelemetryHandle>,298 &TaskManager,299 ) -> Result<300 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,301 sc_service::Error,302 >,303 BIC: FnOnce(304 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,305 Option<&Registry>,306 Option<TelemetryHandle>,307 &TaskManager,308 Arc<dyn RelayChainInterface>,309 Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,310 Arc<NetworkService<Block, Hash>>,311 SyncCryptoStorePtr,312 bool,313 ) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,314{315 if matches!(parachain_config.role, Role::Light) {316 return Err("Light client not supported!".into());317 }318319 let parachain_config = prepare_node_config(parachain_config);320321 let params = new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(322 ¶chain_config,323 build_import_queue,324 ServiceId::Prod,325 )?;326 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =327 params.other;328329 let client = params.client.clone();330 let backend = params.backend.clone();331 let mut task_manager = params.task_manager;332333 let (relay_chain_interface, collator_key) =334 build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)335 .map_err(|e| match e {336 polkadot_service::Error::Sub(x) => x,337 s => format!("{}", s).into(),338 })?;339340 let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);341342 let force_authoring = parachain_config.force_authoring;343 let validator = parachain_config.role.is_authority();344 let prometheus_registry = parachain_config.prometheus_registry().cloned();345 let transaction_pool = params.transaction_pool.clone();346 let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);347348 let (network, system_rpc_tx, start_network) =349 sc_service::build_network(sc_service::BuildNetworkParams {350 config: ¶chain_config,351 client: client.clone(),352 transaction_pool: transaction_pool.clone(),353 spawn_handle: task_manager.spawn_handle(),354 import_queue: import_queue.clone(),355 block_announce_validator_builder: Some(Box::new(|_| {356 Box::new(block_announce_validator)357 })),358 warp_sync: None,359 })?;360361 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());362 let rpc_client = client.clone();363 let rpc_pool = transaction_pool.clone();364 let select_chain = params.select_chain.clone();365 let rpc_network = network.clone();366367 let rpc_frontier_backend = frontier_backend.clone();368369 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(370 task_manager.spawn_handle(),371 overrides_handle::<_, _, Runtime>(client.clone()),372 50,373 50,374 ));375376 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {377 let full_deps = unique_rpc::FullDeps {378 backend: rpc_frontier_backend.clone(),379 deny_unsafe,380 client: rpc_client.clone(),381 pool: rpc_pool.clone(),382 graph: rpc_pool.pool().clone(),383 384 enable_dev_signer: false,385 filter_pool: filter_pool.clone(),386 network: rpc_network.clone(),387 select_chain: select_chain.clone(),388 is_authority: validator,389 390 max_past_logs: 10000,391 block_data_cache: block_data_cache.clone(),392 fee_history_cache: fee_history_cache.clone(),393 394 fee_history_limit: 2048,395 };396397 Ok(398 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(399 full_deps,400 subscription_executor.clone(),401 ),402 )403 });404405 task_manager.spawn_essential_handle().spawn(406 "frontier-mapping-sync-worker",407 None,408 MappingSyncWorker::new(409 client.import_notification_stream(),410 Duration::new(6, 0),411 client.clone(),412 backend.clone(),413 frontier_backend.clone(),414 SyncStrategy::Normal,415 )416 .for_each(|()| futures::future::ready(())),417 );418419 sc_service::spawn_tasks(sc_service::SpawnTasksParams {420 rpc_extensions_builder,421 client: client.clone(),422 transaction_pool: transaction_pool.clone(),423 task_manager: &mut task_manager,424 config: parachain_config,425 keystore: params.keystore_container.sync_keystore(),426 backend: backend.clone(),427 network: network.clone(),428 system_rpc_tx,429 telemetry: telemetry.as_mut(),430 })?;431432 let announce_block = {433 let network = network.clone();434 Arc::new(move |hash, data| network.announce_block(hash, data))435 };436437 let relay_chain_slot_duration = Duration::from_secs(6);438439 if validator {440 let parachain_consensus = build_consensus(441 client.clone(),442 prometheus_registry.as_ref(),443 telemetry.as_ref().map(|t| t.handle()),444 &task_manager,445 relay_chain_interface.clone(),446 transaction_pool,447 network,448 params.keystore_container.sync_keystore(),449 force_authoring,450 )?;451452 let spawner = task_manager.spawn_handle();453454 let params = StartCollatorParams {455 para_id: id,456 block_status: client.clone(),457 announce_block,458 client: client.clone(),459 task_manager: &mut task_manager,460 spawner,461 parachain_consensus,462 import_queue,463 collator_key,464 relay_chain_interface,465 relay_chain_slot_duration,466 };467468 start_collator(params).await?;469 } else {470 let params = StartFullNodeParams {471 client: client.clone(),472 announce_block,473 task_manager: &mut task_manager,474 para_id: id,475 import_queue,476 relay_chain_interface,477 relay_chain_slot_duration,478 };479480 start_full_node(params)?;481 }482483 start_network.start_network();484485 Ok((task_manager, client))486}487488489pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(490 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,491 config: &Configuration,492 telemetry: Option<TelemetryHandle>,493 task_manager: &TaskManager,494) -> Result<495 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,496 sc_service::Error,497>498where499 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>500 + Send501 + Sync502 + 'static,503 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>504 + sp_block_builder::BlockBuilder<Block>505 + sp_consensus_aura::AuraApi<Block, AuraId>506 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,507 ExecutorDispatch: NativeExecutionDispatch + 'static,508{509 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;510511 cumulus_client_consensus_aura::import_queue::<512 sp_consensus_aura::sr25519::AuthorityPair,513 _,514 _,515 _,516 _,517 _,518 _,519 >(cumulus_client_consensus_aura::ImportQueueParams {520 block_import: client.clone(),521 client: client.clone(),522 create_inherent_data_providers: move |_, _| async move {523 let time = sp_timestamp::InherentDataProvider::from_system_time();524525 let slot =526 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(527 *time,528 slot_duration.slot_duration(),529 );530531 Ok((time, slot))532 },533 registry: config.prometheus_registry(),534 can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),535 spawner: &task_manager.spawn_essential_handle(),536 telemetry,537 })538 .map_err(Into::into)539}540541542pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(543 parachain_config: Configuration,544 polkadot_config: Configuration,545 id: ParaId,546) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>547where548 Runtime: RuntimeInstance + Send + Sync + 'static,549 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,550 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,551 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>552 + Send553 + Sync554 + 'static,555 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>556 + fp_rpc::EthereumRuntimeRPCApi<Block>557 + sp_session::SessionKeys<Block>558 + sp_block_builder::BlockBuilder<Block>559 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>560 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>561 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>562 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>563 + sp_api::Metadata<Block>564 + sp_offchain::OffchainWorkerApi<Block>565 + cumulus_primitives_core::CollectCollationInfo<Block>566 + sp_consensus_aura::AuraApi<Block, AuraId>,567 ExecutorDispatch: NativeExecutionDispatch + 'static,568{569 start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(570 parachain_config,571 polkadot_config,572 id,573 parachain_build_import_queue,574 |client,575 prometheus_registry,576 telemetry,577 task_manager,578 relay_chain_interface,579 transaction_pool,580 sync_oracle,581 keystore,582 force_authoring| {583 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;584585 let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(586 task_manager.spawn_handle(),587 client.clone(),588 transaction_pool,589 prometheus_registry,590 telemetry.clone(),591 );592593 Ok(AuraConsensus::build::<594 sp_consensus_aura::sr25519::AuthorityPair,595 _,596 _,597 _,598 _,599 _,600 _,601 >(BuildAuraConsensusParams {602 proposer_factory,603 create_inherent_data_providers: move |_, (relay_parent, validation_data)| {604 let relay_chain_interface = relay_chain_interface.clone();605 async move {606 let parachain_inherent =607 cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(608 relay_parent,609 &relay_chain_interface,610 &validation_data,611 id,612 ).await;613614 let time = sp_timestamp::InherentDataProvider::from_system_time();615616 let slot =617 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(618 *time,619 slot_duration.slot_duration(),620 );621622 let parachain_inherent = parachain_inherent.ok_or_else(|| {623 Box::<dyn std::error::Error + Send + Sync>::from(624 "Failed to create parachain inherent",625 )626 })?;627 Ok((time, slot, parachain_inherent))628 }629 },630 block_import: client.clone(),631 para_client: client,632 backoff_authoring_blocks: Option::<()>::None,633 sync_oracle,634 keystore,635 force_authoring,636 slot_duration: *slot_duration,637 638 block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),639 telemetry,640 max_block_proposal_slot_portion: None,641 }))642 },643 )644 .await645}646647fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(648 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,649 config: &Configuration,650 _: Option<TelemetryHandle>,651 task_manager: &TaskManager,652) -> Result<653 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,654 sc_service::Error,655>656where657 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>658 + Send659 + Sync660 + 'static,661 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>662 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,663 ExecutorDispatch: NativeExecutionDispatch + 'static,664{665 Ok(sc_consensus_manual_seal::import_queue(666 Box::new(client.clone()),667 &task_manager.spawn_essential_handle(),668 config.prometheus_registry(),669 ))670}671672673674pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(675 config: Configuration,676) -> sc_service::error::Result<TaskManager>677where678 Runtime: RuntimeInstance + Send + Sync + 'static,679 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,680 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,681 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>682 + Send683 + Sync684 + 'static,685 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>686 + fp_rpc::EthereumRuntimeRPCApi<Block>687 + sp_session::SessionKeys<Block>688 + sp_block_builder::BlockBuilder<Block>689 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>690 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>691 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>692 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>693 + sp_api::Metadata<Block>694 + sp_offchain::OffchainWorkerApi<Block>695 + cumulus_primitives_core::CollectCollationInfo<Block>696 + sp_consensus_aura::AuraApi<Block, AuraId>,697 ExecutorDispatch: NativeExecutionDispatch + 'static,698{699 use futures::Stream;700 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};701 use fc_consensus::FrontierBlockImport;702 use sc_client_api::HeaderBackend;703704 let sc_service::PartialComponents {705 client,706 backend,707 mut task_manager,708 import_queue,709 keystore_container,710 select_chain: maybe_select_chain,711 transaction_pool,712 other:713 (telemetry, filter_pool, frontier_backend, _telemetry_worker_handle, fee_history_cache),714 } = new_partial::<RuntimeApi, ExecutorDispatch, _>(715 &config,716 dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,717 ServiceId::Dev,718 )?;719720 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(721 task_manager.spawn_handle(),722 overrides_handle::<_, _, Runtime>(client.clone()),723 50,724 50,725 ));726727 let (network, system_rpc_tx, network_starter) =728 sc_service::build_network(sc_service::BuildNetworkParams {729 config: &config,730 client: client.clone(),731 transaction_pool: transaction_pool.clone(),732 spawn_handle: task_manager.spawn_handle(),733 import_queue,734 block_announce_validator_builder: None,735 warp_sync: None,736 })?;737738 if config.offchain_worker.enabled {739 sc_service::build_offchain_workers(740 &config,741 task_manager.spawn_handle(),742 client.clone(),743 network.clone(),744 );745 }746747 let prometheus_registry = config.prometheus_registry().cloned();748 let collator = config.role.is_authority();749750 let select_chain = maybe_select_chain.clone();751752 if collator {753 let block_import =754 FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());755756 let env = sc_basic_authorship::ProposerFactory::new(757 task_manager.spawn_handle(),758 client.clone(),759 transaction_pool.clone(),760 prometheus_registry.as_ref(),761 telemetry.as_ref().map(|x| x.handle()),762 );763764 let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =765 Box::new(766 767 transaction_pool768 .pool()769 .validated_pool()770 .import_notification_stream()771 .map(|_| EngineCommand::SealNewBlock {772 create_empty: true, 773 finalize: false,774 parent_hash: None,775 sender: None,776 }),777 );778779 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;780 let client_set_aside_for_cidp = client.clone();781782 task_manager.spawn_essential_handle().spawn_blocking(783 "authorship_task",784 Some("block-authoring"),785 run_manual_seal(ManualSealParams {786 block_import,787 env,788 client: client.clone(),789 pool: transaction_pool.clone(),790 commands_stream,791 select_chain: select_chain.clone(),792 consensus_data_provider: None,793 create_inherent_data_providers: move |block: Hash, ()| {794 let current_para_block = client_set_aside_for_cidp795 .number(block)796 .expect("Header lookup should succeed")797 .expect("Header passed in as parent should be present in backend.");798799 let client_for_xcm = client_set_aside_for_cidp.clone();800 async move {801 let time = sp_timestamp::InherentDataProvider::from_system_time();802803 let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {804 current_para_block,805 relay_offset: 1000,806 relay_blocks_per_para_block: 2,807 xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(808 &*client_for_xcm,809 block,810 Default::default(),811 Default::default(),812 ),813 raw_downward_messages: vec![],814 raw_horizontal_messages: vec![],815 };816817 let slot =818 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(819 *time,820 slot_duration.slot_duration(),821 );822823 Ok((time, slot, mocked_parachain))824 }825 },826 }),827 );828 }829830 task_manager.spawn_essential_handle().spawn(831 "frontier-mapping-sync-worker",832 Some("block-authoring"),833 MappingSyncWorker::new(834 client.import_notification_stream(),835 Duration::new(6, 0),836 client.clone(),837 backend.clone(),838 frontier_backend.clone(),839 SyncStrategy::Normal,840 )841 .for_each(|()| futures::future::ready(())),842 );843844 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());845 let rpc_client = client.clone();846 let rpc_pool = transaction_pool.clone();847 let rpc_network = network.clone();848 let rpc_frontier_backend = frontier_backend.clone();849 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {850 let full_deps = unique_rpc::FullDeps {851 backend: rpc_frontier_backend.clone(),852 deny_unsafe,853 client: rpc_client.clone(),854 pool: rpc_pool.clone(),855 graph: rpc_pool.pool().clone(),856 857 enable_dev_signer: false,858 filter_pool: filter_pool.clone(),859 network: rpc_network.clone(),860 select_chain: select_chain.clone(),861 is_authority: collator,862 863 max_past_logs: 10000,864 block_data_cache: block_data_cache.clone(),865 fee_history_cache: fee_history_cache.clone(),866 867 fee_history_limit: 2048,868 };869870 Ok(871 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(872 full_deps,873 subscription_executor.clone(),874 ),875 )876 });877878 sc_service::spawn_tasks(sc_service::SpawnTasksParams {879 network,880 client,881 keystore: keystore_container.sync_keystore(),882 task_manager: &mut task_manager,883 transaction_pool,884 rpc_extensions_builder,885 backend,886 system_rpc_tx,887 config,888 telemetry: None,889 })?;890891 network_starter.start_network();892 Ok(task_manager)893}