difftreelog
Use common types in node
in: master
1 file changed
node/cli/src/service.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.1819// std20use std::sync::Arc;21use std::sync::Mutex;22use std::collections::BTreeMap;23use std::time::Duration;24use fc_rpc_core::types::FeeHistoryCache;25use futures::StreamExt;2627use unique_rpc::overrides_handle;2829use serde::{Serialize, Deserialize};3031// Cumulus Imports32use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};33use cumulus_client_consensus_common::ParachainConsensus;34use cumulus_client_service::{35 prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,36};37use cumulus_client_network::BlockAnnounceValidator;38use cumulus_primitives_core::ParaId;39use cumulus_relay_chain_interface::RelayChainInterface;40use cumulus_relay_chain_local::build_relay_chain_interface;4142// Substrate Imports43use sc_client_api::ExecutorProvider;44use sc_executor::NativeElseWasmExecutor;45use sc_executor::NativeExecutionDispatch;46use sc_network::NetworkService;47use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};48use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};49use sp_consensus::SlotData;50use sp_keystore::SyncCryptoStorePtr;51use sp_runtime::traits::BlakeTwo256;52use substrate_prometheus_endpoint::Registry;53use sc_client_api::BlockchainEvents;5455// Frontier Imports56use fc_rpc_core::types::FilterPool;57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};5859// Runtime type overrides60type BlockNumber = u32;61type Header = sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256>;62pub type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;63type Hash = sp_core::H256;6465use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index};6667/// Native executor instance.68pub struct UniqueRuntimeExecutor;69pub struct QuartzRuntimeExecutor;70pub struct OpalRuntimeExecutor;7172#[cfg(feature = "unique-runtime")]73impl NativeExecutionDispatch for UniqueRuntimeExecutor {74 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7576 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {77 unique_runtime::api::dispatch(method, data)78 }7980 fn native_version() -> sc_executor::NativeVersion {81 unique_runtime::native_version()82 }83}8485#[cfg(feature = "quartz-runtime")]86impl NativeExecutionDispatch for QuartzRuntimeExecutor {87 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8889 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {90 quartz_runtime::api::dispatch(method, data)91 }9293 fn native_version() -> sc_executor::NativeVersion {94 quartz_runtime::native_version()95 }96}9798impl NativeExecutionDispatch for OpalRuntimeExecutor {99 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;100101 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {102 opal_runtime::api::dispatch(method, data)103 }104105 fn native_version() -> sc_executor::NativeVersion {106 opal_runtime::native_version()107 }108}109110pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {111 let config_dir = config112 .base_path113 .as_ref()114 .map(|base_path| base_path.config_dir(config.chain_spec.id()))115 .unwrap_or_else(|| {116 BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())117 });118 let database_dir = config_dir.join("frontier").join("db");119120 Ok(Arc::new(fc_db::Backend::<Block>::new(121 &fc_db::DatabaseSettings {122 source: fc_db::DatabaseSettingsSrc::RocksDb {123 path: database_dir,124 cache_size: 0,125 },126 },127 )?))128}129130type FullClient<RuntimeApi, ExecutorDispatch> =131 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;132type FullBackend = sc_service::TFullBackend<Block>;133type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;134135/// Starts a `ServiceBuilder` for a full service.136///137/// Use this macro if you don't actually need the full service, but just the builder in order to138/// be able to perform chain operations.139#[allow(clippy::type_complexity)]140pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(141 config: &Configuration,142 build_import_queue: BIQ,143) -> Result<144 PartialComponents<145 FullClient<RuntimeApi, ExecutorDispatch>,146 FullBackend,147 FullSelectChain,148 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,149 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,150 (151 Option<Telemetry>,152 Option<FilterPool>,153 Arc<fc_db::Backend<Block>>,154 Option<TelemetryWorkerHandle>,155 FeeHistoryCache,156 ),157 >,158 sc_service::Error,159>160where161 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,162 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>163 + Send164 + Sync165 + 'static,166 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,167 ExecutorDispatch: NativeExecutionDispatch + 'static,168 BIQ: FnOnce(169 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,170 &Configuration,171 Option<TelemetryHandle>,172 &TaskManager,173 ) -> Result<174 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,175 sc_service::Error,176 >,177{178 let _telemetry = config179 .telemetry_endpoints180 .clone()181 .filter(|x| !x.is_empty())182 .map(|endpoints| -> Result<_, sc_telemetry::Error> {183 let worker = TelemetryWorker::new(16)?;184 let telemetry = worker.handle().new_telemetry(endpoints);185 Ok((worker, telemetry))186 })187 .transpose()?;188189 let telemetry = config190 .telemetry_endpoints191 .clone()192 .filter(|x| !x.is_empty())193 .map(|endpoints| -> Result<_, sc_telemetry::Error> {194 let worker = TelemetryWorker::new(16)?;195 let telemetry = worker.handle().new_telemetry(endpoints);196 Ok((worker, telemetry))197 })198 .transpose()?;199200 let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(201 config.wasm_method,202 config.default_heap_pages,203 config.max_runtime_instances,204 config.runtime_cache_size,205 );206207 let (client, backend, keystore_container, task_manager) =208 sc_service::new_full_parts::<Block, RuntimeApi, _>(209 config,210 telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),211 executor,212 )?;213 let client = Arc::new(client);214215 let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());216217 let telemetry = telemetry.map(|(worker, telemetry)| {218 task_manager219 .spawn_handle()220 .spawn("telemetry", None, worker.run());221 telemetry222 });223224 let select_chain = sc_consensus::LongestChain::new(backend.clone());225226 let transaction_pool = sc_transaction_pool::BasicPool::new_full(227 config.transaction_pool.clone(),228 config.role.is_authority().into(),229 config.prometheus_registry(),230 task_manager.spawn_essential_handle(),231 client.clone(),232 );233234 let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));235236 let frontier_backend = open_frontier_backend(config)?;237238 let import_queue = build_import_queue(239 client.clone(),240 config,241 telemetry.as_ref().map(|telemetry| telemetry.handle()),242 &task_manager,243 )?;244 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));245246 let params = PartialComponents {247 backend,248 client,249 import_queue,250 keystore_container,251 task_manager,252 transaction_pool,253 select_chain,254 other: (255 telemetry,256 filter_pool,257 frontier_backend,258 telemetry_worker_handle,259 fee_history_cache,260 ),261 };262263 Ok(params)264}265266/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.267///268/// This is the actual implementation that is abstract over the executor and the runtime api.269#[sc_tracing::logging::prefix_logs_with("Parachain")]270async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(271 parachain_config: Configuration,272 polkadot_config: Configuration,273 id: ParaId,274 build_import_queue: BIQ,275 build_consensus: BIC,276) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>277where278 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,279 Runtime: RuntimeInstance + Send + Sync + 'static,280 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,281 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,282 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>283 + Send284 + Sync285 + 'static,286 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>287 + fp_rpc::EthereumRuntimeRPCApi<Block>288 + sp_session::SessionKeys<Block>289 + sp_block_builder::BlockBuilder<Block>290 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>291 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>292 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>293 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>294 + sp_api::Metadata<Block>295 + sp_offchain::OffchainWorkerApi<Block>296 + cumulus_primitives_core::CollectCollationInfo<Block>,297 ExecutorDispatch: NativeExecutionDispatch + 'static,298 BIQ: FnOnce(299 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,300 &Configuration,301 Option<TelemetryHandle>,302 &TaskManager,303 ) -> Result<304 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,305 sc_service::Error,306 >,307 BIC: FnOnce(308 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,309 Option<&Registry>,310 Option<TelemetryHandle>,311 &TaskManager,312 Arc<dyn RelayChainInterface>,313 Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,314 Arc<NetworkService<Block, Hash>>,315 SyncCryptoStorePtr,316 bool,317 ) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,318{319 if matches!(parachain_config.role, Role::Light) {320 return Err("Light client not supported!".into());321 }322323 let parachain_config = prepare_node_config(parachain_config);324325 let params =326 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(¶chain_config, build_import_queue)?;327 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =328 params.other;329330 let client = params.client.clone();331 let backend = params.backend.clone();332 let mut task_manager = params.task_manager;333334 let (relay_chain_interface, collator_key) =335 build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)336 .map_err(|e| match e {337 polkadot_service::Error::Sub(x) => x,338 s => format!("{}", s).into(),339 })?;340341 let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);342343 let force_authoring = parachain_config.force_authoring;344 let validator = parachain_config.role.is_authority();345 let prometheus_registry = parachain_config.prometheus_registry().cloned();346 let transaction_pool = params.transaction_pool.clone();347 let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);348349 let (network, system_rpc_tx, start_network) =350 sc_service::build_network(sc_service::BuildNetworkParams {351 config: ¶chain_config,352 client: client.clone(),353 transaction_pool: transaction_pool.clone(),354 spawn_handle: task_manager.spawn_handle(),355 import_queue: import_queue.clone(),356 block_announce_validator_builder: Some(Box::new(|_| {357 Box::new(block_announce_validator)358 })),359 warp_sync: None,360 })?;361362 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());363 let rpc_client = client.clone();364 let rpc_pool = transaction_pool.clone();365 let select_chain = params.select_chain.clone();366 let rpc_network = network.clone();367368 let rpc_frontier_backend = frontier_backend.clone();369370 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(371 task_manager.spawn_handle(),372 overrides_handle::<_, _, Runtime>(client.clone()),373 50,374 50,375 ));376377 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {378 let full_deps = unique_rpc::FullDeps {379 backend: rpc_frontier_backend.clone(),380 deny_unsafe,381 client: rpc_client.clone(),382 pool: rpc_pool.clone(),383 graph: rpc_pool.pool().clone(),384 // TODO: Unhardcode385 enable_dev_signer: false,386 filter_pool: filter_pool.clone(),387 network: rpc_network.clone(),388 select_chain: select_chain.clone(),389 is_authority: validator,390 // TODO: Unhardcode391 max_past_logs: 10000,392 block_data_cache: block_data_cache.clone(),393 fee_history_cache: fee_history_cache.clone(),394 // TODO: Unhardcode395 fee_history_limit: 2048,396 };397398 Ok(399 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(400 full_deps,401 subscription_executor.clone(),402 ),403 )404 });405406 task_manager.spawn_essential_handle().spawn(407 "frontier-mapping-sync-worker",408 None,409 MappingSyncWorker::new(410 client.import_notification_stream(),411 Duration::new(6, 0),412 client.clone(),413 backend.clone(),414 frontier_backend.clone(),415 SyncStrategy::Normal,416 )417 .for_each(|()| futures::future::ready(())),418 );419420 sc_service::spawn_tasks(sc_service::SpawnTasksParams {421 rpc_extensions_builder,422 client: client.clone(),423 transaction_pool: transaction_pool.clone(),424 task_manager: &mut task_manager,425 config: parachain_config,426 keystore: params.keystore_container.sync_keystore(),427 backend: backend.clone(),428 network: network.clone(),429 system_rpc_tx,430 telemetry: telemetry.as_mut(),431 })?;432433 let announce_block = {434 let network = network.clone();435 Arc::new(move |hash, data| network.announce_block(hash, data))436 };437438 let relay_chain_slot_duration = Duration::from_secs(6);439440 if validator {441 let parachain_consensus = build_consensus(442 client.clone(),443 prometheus_registry.as_ref(),444 telemetry.as_ref().map(|t| t.handle()),445 &task_manager,446 relay_chain_interface.clone(),447 transaction_pool,448 network,449 params.keystore_container.sync_keystore(),450 force_authoring,451 )?;452453 let spawner = task_manager.spawn_handle();454455 let params = StartCollatorParams {456 para_id: id,457 block_status: client.clone(),458 announce_block,459 client: client.clone(),460 task_manager: &mut task_manager,461 spawner,462 parachain_consensus,463 import_queue,464 collator_key,465 relay_chain_interface,466 relay_chain_slot_duration,467 };468469 start_collator(params).await?;470 } else {471 let params = StartFullNodeParams {472 client: client.clone(),473 announce_block,474 task_manager: &mut task_manager,475 para_id: id,476 import_queue,477 relay_chain_interface,478 relay_chain_slot_duration,479 };480481 start_full_node(params)?;482 }483484 start_network.start_network();485486 Ok((task_manager, client))487}488489/// Build the import queue for the the parachain runtime.490pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(491 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,492 config: &Configuration,493 telemetry: Option<TelemetryHandle>,494 task_manager: &TaskManager,495) -> Result<496 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,497 sc_service::Error,498>499where500 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>501 + Send502 + Sync503 + 'static,504 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>505 + sp_block_builder::BlockBuilder<Block>506 + sp_consensus_aura::AuraApi<Block, AuraId>507 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,508 ExecutorDispatch: NativeExecutionDispatch + 'static,509{510 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;511512 cumulus_client_consensus_aura::import_queue::<513 sp_consensus_aura::sr25519::AuthorityPair,514 _,515 _,516 _,517 _,518 _,519 _,520 >(cumulus_client_consensus_aura::ImportQueueParams {521 block_import: client.clone(),522 client: client.clone(),523 create_inherent_data_providers: move |_, _| async move {524 let time = sp_timestamp::InherentDataProvider::from_system_time();525526 let slot =527 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(528 *time,529 slot_duration.slot_duration(),530 );531532 Ok((time, slot))533 },534 registry: config.prometheus_registry(),535 can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),536 spawner: &task_manager.spawn_essential_handle(),537 telemetry,538 })539 .map_err(Into::into)540}541542/// Start a normal parachain node.543pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(544 parachain_config: Configuration,545 polkadot_config: Configuration,546 id: ParaId,547) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>548where549 Runtime: RuntimeInstance + Send + Sync + 'static,550 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,551 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,552 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>553 + Send554 + Sync555 + 'static,556 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>557 + fp_rpc::EthereumRuntimeRPCApi<Block>558 + sp_session::SessionKeys<Block>559 + sp_block_builder::BlockBuilder<Block>560 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>561 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>562 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>563 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>564 + sp_api::Metadata<Block>565 + sp_offchain::OffchainWorkerApi<Block>566 + cumulus_primitives_core::CollectCollationInfo<Block>567 + sp_consensus_aura::AuraApi<Block, AuraId>,568 ExecutorDispatch: NativeExecutionDispatch + 'static,569{570 start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(571 parachain_config,572 polkadot_config,573 id,574 parachain_build_import_queue,575 |client,576 prometheus_registry,577 telemetry,578 task_manager,579 relay_chain_interface,580 transaction_pool,581 sync_oracle,582 keystore,583 force_authoring| {584 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;585586 let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(587 task_manager.spawn_handle(),588 client.clone(),589 transaction_pool,590 prometheus_registry,591 telemetry.clone(),592 );593594 Ok(AuraConsensus::build::<595 sp_consensus_aura::sr25519::AuthorityPair,596 _,597 _,598 _,599 _,600 _,601 _,602 >(BuildAuraConsensusParams {603 proposer_factory,604 create_inherent_data_providers: move |_, (relay_parent, validation_data)| {605 let relay_chain_interface = relay_chain_interface.clone();606 async move {607 let parachain_inherent =608 cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(609 relay_parent,610 &relay_chain_interface,611 &validation_data,612 id,613 ).await;614615 let time = sp_timestamp::InherentDataProvider::from_system_time();616617 let slot =618 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(619 *time,620 slot_duration.slot_duration(),621 );622623 let parachain_inherent = parachain_inherent.ok_or_else(|| {624 Box::<dyn std::error::Error + Send + Sync>::from(625 "Failed to create parachain inherent",626 )627 })?;628 Ok((time, slot, parachain_inherent))629 }630 },631 block_import: client.clone(),632 para_client: client,633 backoff_authoring_blocks: Option::<()>::None,634 sync_oracle,635 keystore,636 force_authoring,637 slot_duration: *slot_duration,638 // We got around 500ms for proposing639 block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),640 telemetry,641 max_block_proposal_slot_portion: None,642 }))643 },644 )645 .await646}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.1819// std20use std::sync::Arc;21use std::sync::Mutex;22use std::collections::BTreeMap;23use std::time::Duration;24use fc_rpc_core::types::FeeHistoryCache;25use futures::StreamExt;2627use unique_rpc::overrides_handle;2829use serde::{Serialize, Deserialize};3031// Cumulus Imports32use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};33use cumulus_client_consensus_common::ParachainConsensus;34use cumulus_client_service::{35 prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,36};37use cumulus_client_network::BlockAnnounceValidator;38use cumulus_primitives_core::ParaId;39use cumulus_relay_chain_interface::RelayChainInterface;40use cumulus_relay_chain_local::build_relay_chain_interface;4142// Substrate Imports43use sc_client_api::ExecutorProvider;44use sc_executor::NativeElseWasmExecutor;45use sc_executor::NativeExecutionDispatch;46use sc_network::NetworkService;47use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};48use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};49use sp_consensus::SlotData;50use sp_keystore::SyncCryptoStorePtr;51use sp_runtime::traits::BlakeTwo256;52use substrate_prometheus_endpoint::Registry;53use sc_client_api::BlockchainEvents;5455// Frontier Imports56use fc_rpc_core::types::FilterPool;57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};5859use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6061/// Native executor instance.62pub struct UniqueRuntimeExecutor;63pub struct QuartzRuntimeExecutor;64pub struct OpalRuntimeExecutor;6566#[cfg(feature = "unique-runtime")]67impl NativeExecutionDispatch for UniqueRuntimeExecutor {68 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;6970 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {71 unique_runtime::api::dispatch(method, data)72 }7374 fn native_version() -> sc_executor::NativeVersion {75 unique_runtime::native_version()76 }77}7879#[cfg(feature = "quartz-runtime")]80impl NativeExecutionDispatch for QuartzRuntimeExecutor {81 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8283 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {84 quartz_runtime::api::dispatch(method, data)85 }8687 fn native_version() -> sc_executor::NativeVersion {88 quartz_runtime::native_version()89 }90}9192impl NativeExecutionDispatch for OpalRuntimeExecutor {93 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9495 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {96 opal_runtime::api::dispatch(method, data)97 }9899 fn native_version() -> sc_executor::NativeVersion {100 opal_runtime::native_version()101 }102}103104pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {105 let config_dir = config106 .base_path107 .as_ref()108 .map(|base_path| base_path.config_dir(config.chain_spec.id()))109 .unwrap_or_else(|| {110 BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())111 });112 let database_dir = config_dir.join("frontier").join("db");113114 Ok(Arc::new(fc_db::Backend::<Block>::new(115 &fc_db::DatabaseSettings {116 source: fc_db::DatabaseSettingsSrc::RocksDb {117 path: database_dir,118 cache_size: 0,119 },120 },121 )?))122}123124type FullClient<RuntimeApi, ExecutorDispatch> =125 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;126type FullBackend = sc_service::TFullBackend<Block>;127type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;128129/// Starts a `ServiceBuilder` for a full service.130///131/// Use this macro if you don't actually need the full service, but just the builder in order to132/// be able to perform chain operations.133#[allow(clippy::type_complexity)]134pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(135 config: &Configuration,136 build_import_queue: BIQ,137) -> Result<138 PartialComponents<139 FullClient<RuntimeApi, ExecutorDispatch>,140 FullBackend,141 FullSelectChain,142 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,143 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,144 (145 Option<Telemetry>,146 Option<FilterPool>,147 Arc<fc_db::Backend<Block>>,148 Option<TelemetryWorkerHandle>,149 FeeHistoryCache,150 ),151 >,152 sc_service::Error,153>154where155 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,156 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>157 + Send158 + Sync159 + 'static,160 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,161 ExecutorDispatch: NativeExecutionDispatch + 'static,162 BIQ: FnOnce(163 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,164 &Configuration,165 Option<TelemetryHandle>,166 &TaskManager,167 ) -> Result<168 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,169 sc_service::Error,170 >,171{172 let _telemetry = config173 .telemetry_endpoints174 .clone()175 .filter(|x| !x.is_empty())176 .map(|endpoints| -> Result<_, sc_telemetry::Error> {177 let worker = TelemetryWorker::new(16)?;178 let telemetry = worker.handle().new_telemetry(endpoints);179 Ok((worker, telemetry))180 })181 .transpose()?;182183 let telemetry = config184 .telemetry_endpoints185 .clone()186 .filter(|x| !x.is_empty())187 .map(|endpoints| -> Result<_, sc_telemetry::Error> {188 let worker = TelemetryWorker::new(16)?;189 let telemetry = worker.handle().new_telemetry(endpoints);190 Ok((worker, telemetry))191 })192 .transpose()?;193194 let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(195 config.wasm_method,196 config.default_heap_pages,197 config.max_runtime_instances,198 config.runtime_cache_size,199 );200201 let (client, backend, keystore_container, task_manager) =202 sc_service::new_full_parts::<Block, RuntimeApi, _>(203 config,204 telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),205 executor,206 )?;207 let client = Arc::new(client);208209 let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());210211 let telemetry = telemetry.map(|(worker, telemetry)| {212 task_manager213 .spawn_handle()214 .spawn("telemetry", None, worker.run());215 telemetry216 });217218 let select_chain = sc_consensus::LongestChain::new(backend.clone());219220 let transaction_pool = sc_transaction_pool::BasicPool::new_full(221 config.transaction_pool.clone(),222 config.role.is_authority().into(),223 config.prometheus_registry(),224 task_manager.spawn_essential_handle(),225 client.clone(),226 );227228 let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));229230 let frontier_backend = open_frontier_backend(config)?;231232 let import_queue = build_import_queue(233 client.clone(),234 config,235 telemetry.as_ref().map(|telemetry| telemetry.handle()),236 &task_manager,237 )?;238 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));239240 let params = PartialComponents {241 backend,242 client,243 import_queue,244 keystore_container,245 task_manager,246 transaction_pool,247 select_chain,248 other: (249 telemetry,250 filter_pool,251 frontier_backend,252 telemetry_worker_handle,253 fee_history_cache,254 ),255 };256257 Ok(params)258}259260/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.261///262/// This is the actual implementation that is abstract over the executor and the runtime api.263#[sc_tracing::logging::prefix_logs_with("Parachain")]264async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(265 parachain_config: Configuration,266 polkadot_config: Configuration,267 id: ParaId,268 build_import_queue: BIQ,269 build_consensus: BIC,270) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>271where272 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,273 Runtime: RuntimeInstance + Send + Sync + 'static,274 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,275 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,276 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>277 + Send278 + Sync279 + 'static,280 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>281 + fp_rpc::EthereumRuntimeRPCApi<Block>282 + sp_session::SessionKeys<Block>283 + sp_block_builder::BlockBuilder<Block>284 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>285 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>286 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>287 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>288 + sp_api::Metadata<Block>289 + sp_offchain::OffchainWorkerApi<Block>290 + cumulus_primitives_core::CollectCollationInfo<Block>,291 ExecutorDispatch: NativeExecutionDispatch + 'static,292 BIQ: FnOnce(293 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,294 &Configuration,295 Option<TelemetryHandle>,296 &TaskManager,297 ) -> Result<298 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,299 sc_service::Error,300 >,301 BIC: FnOnce(302 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,303 Option<&Registry>,304 Option<TelemetryHandle>,305 &TaskManager,306 Arc<dyn RelayChainInterface>,307 Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,308 Arc<NetworkService<Block, Hash>>,309 SyncCryptoStorePtr,310 bool,311 ) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,312{313 if matches!(parachain_config.role, Role::Light) {314 return Err("Light client not supported!".into());315 }316317 let parachain_config = prepare_node_config(parachain_config);318319 let params =320 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(¶chain_config, build_import_queue)?;321 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =322 params.other;323324 let client = params.client.clone();325 let backend = params.backend.clone();326 let mut task_manager = params.task_manager;327328 let (relay_chain_interface, collator_key) =329 build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager)330 .map_err(|e| match e {331 polkadot_service::Error::Sub(x) => x,332 s => format!("{}", s).into(),333 })?;334335 let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);336337 let force_authoring = parachain_config.force_authoring;338 let validator = parachain_config.role.is_authority();339 let prometheus_registry = parachain_config.prometheus_registry().cloned();340 let transaction_pool = params.transaction_pool.clone();341 let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);342343 let (network, system_rpc_tx, start_network) =344 sc_service::build_network(sc_service::BuildNetworkParams {345 config: ¶chain_config,346 client: client.clone(),347 transaction_pool: transaction_pool.clone(),348 spawn_handle: task_manager.spawn_handle(),349 import_queue: import_queue.clone(),350 block_announce_validator_builder: Some(Box::new(|_| {351 Box::new(block_announce_validator)352 })),353 warp_sync: None,354 })?;355356 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());357 let rpc_client = client.clone();358 let rpc_pool = transaction_pool.clone();359 let select_chain = params.select_chain.clone();360 let rpc_network = network.clone();361362 let rpc_frontier_backend = frontier_backend.clone();363364 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(365 task_manager.spawn_handle(),366 overrides_handle::<_, _, Runtime>(client.clone()),367 50,368 50,369 ));370371 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {372 let full_deps = unique_rpc::FullDeps {373 backend: rpc_frontier_backend.clone(),374 deny_unsafe,375 client: rpc_client.clone(),376 pool: rpc_pool.clone(),377 graph: rpc_pool.pool().clone(),378 // TODO: Unhardcode379 enable_dev_signer: false,380 filter_pool: filter_pool.clone(),381 network: rpc_network.clone(),382 select_chain: select_chain.clone(),383 is_authority: validator,384 // TODO: Unhardcode385 max_past_logs: 10000,386 block_data_cache: block_data_cache.clone(),387 fee_history_cache: fee_history_cache.clone(),388 // TODO: Unhardcode389 fee_history_limit: 2048,390 };391392 Ok(393 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(394 full_deps,395 subscription_executor.clone(),396 ),397 )398 });399400 task_manager.spawn_essential_handle().spawn(401 "frontier-mapping-sync-worker",402 None,403 MappingSyncWorker::new(404 client.import_notification_stream(),405 Duration::new(6, 0),406 client.clone(),407 backend.clone(),408 frontier_backend.clone(),409 SyncStrategy::Normal,410 )411 .for_each(|()| futures::future::ready(())),412 );413414 sc_service::spawn_tasks(sc_service::SpawnTasksParams {415 rpc_extensions_builder,416 client: client.clone(),417 transaction_pool: transaction_pool.clone(),418 task_manager: &mut task_manager,419 config: parachain_config,420 keystore: params.keystore_container.sync_keystore(),421 backend: backend.clone(),422 network: network.clone(),423 system_rpc_tx,424 telemetry: telemetry.as_mut(),425 })?;426427 let announce_block = {428 let network = network.clone();429 Arc::new(move |hash, data| network.announce_block(hash, data))430 };431432 let relay_chain_slot_duration = Duration::from_secs(6);433434 if validator {435 let parachain_consensus = build_consensus(436 client.clone(),437 prometheus_registry.as_ref(),438 telemetry.as_ref().map(|t| t.handle()),439 &task_manager,440 relay_chain_interface.clone(),441 transaction_pool,442 network,443 params.keystore_container.sync_keystore(),444 force_authoring,445 )?;446447 let spawner = task_manager.spawn_handle();448449 let params = StartCollatorParams {450 para_id: id,451 block_status: client.clone(),452 announce_block,453 client: client.clone(),454 task_manager: &mut task_manager,455 spawner,456 parachain_consensus,457 import_queue,458 collator_key,459 relay_chain_interface,460 relay_chain_slot_duration,461 };462463 start_collator(params).await?;464 } else {465 let params = StartFullNodeParams {466 client: client.clone(),467 announce_block,468 task_manager: &mut task_manager,469 para_id: id,470 import_queue,471 relay_chain_interface,472 relay_chain_slot_duration,473 };474475 start_full_node(params)?;476 }477478 start_network.start_network();479480 Ok((task_manager, client))481}482483/// Build the import queue for the the parachain runtime.484pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(485 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,486 config: &Configuration,487 telemetry: Option<TelemetryHandle>,488 task_manager: &TaskManager,489) -> Result<490 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,491 sc_service::Error,492>493where494 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>495 + Send496 + Sync497 + 'static,498 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>499 + sp_block_builder::BlockBuilder<Block>500 + sp_consensus_aura::AuraApi<Block, AuraId>501 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,502 ExecutorDispatch: NativeExecutionDispatch + 'static,503{504 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;505506 cumulus_client_consensus_aura::import_queue::<507 sp_consensus_aura::sr25519::AuthorityPair,508 _,509 _,510 _,511 _,512 _,513 _,514 >(cumulus_client_consensus_aura::ImportQueueParams {515 block_import: client.clone(),516 client: client.clone(),517 create_inherent_data_providers: move |_, _| async move {518 let time = sp_timestamp::InherentDataProvider::from_system_time();519520 let slot =521 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(522 *time,523 slot_duration.slot_duration(),524 );525526 Ok((time, slot))527 },528 registry: config.prometheus_registry(),529 can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),530 spawner: &task_manager.spawn_essential_handle(),531 telemetry,532 })533 .map_err(Into::into)534}535536/// Start a normal parachain node.537pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(538 parachain_config: Configuration,539 polkadot_config: Configuration,540 id: ParaId,541) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>542where543 Runtime: RuntimeInstance + Send + Sync + 'static,544 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,545 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,546 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>547 + Send548 + Sync549 + 'static,550 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>551 + fp_rpc::EthereumRuntimeRPCApi<Block>552 + sp_session::SessionKeys<Block>553 + sp_block_builder::BlockBuilder<Block>554 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>555 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>556 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>557 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>558 + sp_api::Metadata<Block>559 + sp_offchain::OffchainWorkerApi<Block>560 + cumulus_primitives_core::CollectCollationInfo<Block>561 + sp_consensus_aura::AuraApi<Block, AuraId>,562 ExecutorDispatch: NativeExecutionDispatch + 'static,563{564 start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(565 parachain_config,566 polkadot_config,567 id,568 parachain_build_import_queue,569 |client,570 prometheus_registry,571 telemetry,572 task_manager,573 relay_chain_interface,574 transaction_pool,575 sync_oracle,576 keystore,577 force_authoring| {578 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;579580 let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(581 task_manager.spawn_handle(),582 client.clone(),583 transaction_pool,584 prometheus_registry,585 telemetry.clone(),586 );587588 Ok(AuraConsensus::build::<589 sp_consensus_aura::sr25519::AuthorityPair,590 _,591 _,592 _,593 _,594 _,595 _,596 >(BuildAuraConsensusParams {597 proposer_factory,598 create_inherent_data_providers: move |_, (relay_parent, validation_data)| {599 let relay_chain_interface = relay_chain_interface.clone();600 async move {601 let parachain_inherent =602 cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(603 relay_parent,604 &relay_chain_interface,605 &validation_data,606 id,607 ).await;608609 let time = sp_timestamp::InherentDataProvider::from_system_time();610611 let slot =612 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(613 *time,614 slot_duration.slot_duration(),615 );616617 let parachain_inherent = parachain_inherent.ok_or_else(|| {618 Box::<dyn std::error::Error + Send + Sync>::from(619 "Failed to create parachain inherent",620 )621 })?;622 Ok((time, slot, parachain_inherent))623 }624 },625 block_import: client.clone(),626 para_client: client,627 backoff_authoring_blocks: Option::<()>::None,628 sync_oracle,629 keystore,630 force_authoring,631 slot_duration: *slot_duration,632 // We got around 500ms for proposing633 block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),634 telemetry,635 max_block_proposal_slot_portion: None,636 }))637 },638 )639 .await640}