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