difftreelog
Autoseal after idle n seconds
in: master
4 files changed
node/cli/Cargo.tomldiffbeforeafterboth--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -294,6 +294,7 @@
[dependencies]
futures = '0.3.17'
+futures-timer = '3.0.2'
log = '0.4.14'
flexi_logger = "0.15.7"
parking_lot = '0.11.2'
node/cli/src/cli.rsdiffbeforeafterboth--- a/node/cli/src/cli.rs
+++ b/node/cli/src/cli.rs
@@ -104,6 +104,15 @@
#[structopt(flatten)]
pub run: cumulus_client_cli::RunCmd,
+ /// When running the node in the `--dev` mode and
+ /// there is no transaction in the transaction pool,
+ /// an empty block will be sealed automatically
+ /// after the `--idle-autoseal-interval` milliseconds.
+ ///
+ /// Default interval is 500 milliseconds
+ #[structopt(default_value = "500", long)]
+ pub idle_autoseal_interval: u64,
+
/// Relaychain arguments
#[structopt(raw = true)]
pub relaychain_args: Vec<String>,
node/cli/src/command.rsdiffbeforeafterboth--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -35,7 +35,7 @@
use crate::{
chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},
cli::{Cli, RelayChainCli, Subcommand},
- service::{new_partial, start_node, start_dev_node},
+ service::{new_partial, start_node, start_dev_node, AutosealInterval},
};
#[cfg(feature = "unique-runtime")]
@@ -60,7 +60,7 @@
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::traits::Block as BlockT;
-use std::{io::Write, net::SocketAddr};
+use std::{io::Write, net::SocketAddr, time::Duration};
use unique_runtime_common::types::Block;
@@ -405,8 +405,12 @@
if is_dev_service {
info!("Running Dev service");
+ let autoseal_interval = AutosealInterval::new(
+ Duration::from_millis(cli.idle_autoseal_interval)
+ )?;
+
return start_node_using_chain_runtime! {
- start_dev_node(config).map_err(Into::into)
+ start_dev_node(config, autoseal_interval).map_err(Into::into)
};
};
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_cli::CollatorOptions;38use cumulus_client_network::BlockAnnounceValidator;39use cumulus_primitives_core::ParaId;40use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;41use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};42use cumulus_relay_chain_rpc_interface::RelayChainRPCInterface;4344// Substrate Imports45use sc_client_api::ExecutorProvider;46use sc_executor::NativeElseWasmExecutor;47use sc_executor::NativeExecutionDispatch;48use sc_network::NetworkService;49use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};50use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};51use sp_keystore::SyncCryptoStorePtr;52use sp_runtime::traits::BlakeTwo256;53use substrate_prometheus_endpoint::Registry;54use sc_client_api::BlockchainEvents;5556use polkadot_service::CollatorPair;5758// Frontier Imports59use fc_rpc_core::types::FilterPool;60use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};6162use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6364/// Unique native executor instance.65#[cfg(feature = "unique-runtime")]66pub struct UniqueRuntimeExecutor;6768#[cfg(feature = "quartz-runtime")]69/// Quartz native executor instance.7071pub struct QuartzRuntimeExecutor;7273/// Opal native executor instance.74pub struct OpalRuntimeExecutor;7576#[cfg(feature = "unique-runtime")]77impl NativeExecutionDispatch for UniqueRuntimeExecutor {78 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7980 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {81 unique_runtime::api::dispatch(method, data)82 }8384 fn native_version() -> sc_executor::NativeVersion {85 unique_runtime::native_version()86 }87}8889#[cfg(feature = "quartz-runtime")]90impl NativeExecutionDispatch for QuartzRuntimeExecutor {91 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9293 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {94 quartz_runtime::api::dispatch(method, data)95 }9697 fn native_version() -> sc_executor::NativeVersion {98 quartz_runtime::native_version()99 }100}101102impl NativeExecutionDispatch for OpalRuntimeExecutor {103 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;104105 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {106 opal_runtime::api::dispatch(method, data)107 }108109 fn native_version() -> sc_executor::NativeVersion {110 opal_runtime::native_version()111 }112}113114pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {115 let config_dir = config116 .base_path117 .as_ref()118 .map(|base_path| base_path.config_dir(config.chain_spec.id()))119 .unwrap_or_else(|| {120 BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())121 });122 let database_dir = config_dir.join("frontier").join("db");123124 Ok(Arc::new(fc_db::Backend::<Block>::new(125 &fc_db::DatabaseSettings {126 source: fc_db::DatabaseSettingsSrc::RocksDb {127 path: database_dir,128 cache_size: 0,129 },130 },131 )?))132}133134type FullClient<RuntimeApi, ExecutorDispatch> =135 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;136type FullBackend = sc_service::TFullBackend<Block>;137type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;138139/// Starts a `ServiceBuilder` for a full service.140///141/// Use this macro if you don't actually need the full service, but just the builder in order to142/// be able to perform chain operations.143#[allow(clippy::type_complexity)]144pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(145 config: &Configuration,146 build_import_queue: BIQ,147) -> Result<148 PartialComponents<149 FullClient<RuntimeApi, ExecutorDispatch>,150 FullBackend,151 FullSelectChain,152 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,153 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,154 (155 Option<Telemetry>,156 Option<FilterPool>,157 Arc<fc_db::Backend<Block>>,158 Option<TelemetryWorkerHandle>,159 FeeHistoryCache,160 ),161 >,162 sc_service::Error,163>164where165 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,166 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>167 + Send168 + Sync169 + 'static,170 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,171 ExecutorDispatch: NativeExecutionDispatch + 'static,172 BIQ: FnOnce(173 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,174 &Configuration,175 Option<TelemetryHandle>,176 &TaskManager,177 ) -> Result<178 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,179 sc_service::Error,180 >,181{182 let _telemetry = config183 .telemetry_endpoints184 .clone()185 .filter(|x| !x.is_empty())186 .map(|endpoints| -> Result<_, sc_telemetry::Error> {187 let worker = TelemetryWorker::new(16)?;188 let telemetry = worker.handle().new_telemetry(endpoints);189 Ok((worker, telemetry))190 })191 .transpose()?;192193 let telemetry = config194 .telemetry_endpoints195 .clone()196 .filter(|x| !x.is_empty())197 .map(|endpoints| -> Result<_, sc_telemetry::Error> {198 let worker = TelemetryWorker::new(16)?;199 let telemetry = worker.handle().new_telemetry(endpoints);200 Ok((worker, telemetry))201 })202 .transpose()?;203204 let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(205 config.wasm_method,206 config.default_heap_pages,207 config.max_runtime_instances,208 config.runtime_cache_size,209 );210211 let (client, backend, keystore_container, task_manager) =212 sc_service::new_full_parts::<Block, RuntimeApi, _>(213 config,214 telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),215 executor,216 )?;217 let client = Arc::new(client);218219 let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());220221 let telemetry = telemetry.map(|(worker, telemetry)| {222 task_manager223 .spawn_handle()224 .spawn("telemetry", None, worker.run());225 telemetry226 });227228 let select_chain = sc_consensus::LongestChain::new(backend.clone());229230 let transaction_pool = sc_transaction_pool::BasicPool::new_full(231 config.transaction_pool.clone(),232 config.role.is_authority().into(),233 config.prometheus_registry(),234 task_manager.spawn_essential_handle(),235 client.clone(),236 );237238 let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));239240 let frontier_backend = open_frontier_backend(config)?;241242 let import_queue = build_import_queue(243 client.clone(),244 config,245 telemetry.as_ref().map(|telemetry| telemetry.handle()),246 &task_manager,247 )?;248 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));249250 let params = PartialComponents {251 backend,252 client,253 import_queue,254 keystore_container,255 task_manager,256 transaction_pool,257 select_chain,258 other: (259 telemetry,260 filter_pool,261 frontier_backend,262 telemetry_worker_handle,263 fee_history_cache,264 ),265 };266267 Ok(params)268}269270async fn build_relay_chain_interface(271 polkadot_config: Configuration,272 parachain_config: &Configuration,273 telemetry_worker_handle: Option<TelemetryWorkerHandle>,274 task_manager: &mut TaskManager,275 collator_options: CollatorOptions,276) -> RelayChainResult<(277 Arc<(dyn RelayChainInterface + 'static)>,278 Option<CollatorPair>,279)> {280 match collator_options.relay_chain_rpc_url {281 Some(relay_chain_url) => Ok((282 Arc::new(RelayChainRPCInterface::new(relay_chain_url).await?) as Arc<_>,283 None,284 )),285 None => build_inprocess_relay_chain(286 polkadot_config,287 parachain_config,288 telemetry_worker_handle,289 task_manager,290 ),291 }292}293294/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.295///296/// This is the actual implementation that is abstract over the executor and the runtime api.297#[sc_tracing::logging::prefix_logs_with("Parachain")]298async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(299 parachain_config: Configuration,300 polkadot_config: Configuration,301 collator_options: CollatorOptions,302 id: ParaId,303 build_import_queue: BIQ,304 build_consensus: BIC,305) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>306where307 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,308 Runtime: RuntimeInstance + Send + Sync + 'static,309 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,310 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,311 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>312 + Send313 + Sync314 + 'static,315 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>316 + fp_rpc::EthereumRuntimeRPCApi<Block>317 + sp_session::SessionKeys<Block>318 + sp_block_builder::BlockBuilder<Block>319 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>320 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>321 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>322 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>323 + sp_api::Metadata<Block>324 + sp_offchain::OffchainWorkerApi<Block>325 + cumulus_primitives_core::CollectCollationInfo<Block>,326 ExecutorDispatch: NativeExecutionDispatch + 'static,327 BIQ: FnOnce(328 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,329 &Configuration,330 Option<TelemetryHandle>,331 &TaskManager,332 ) -> Result<333 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,334 sc_service::Error,335 >,336 BIC: FnOnce(337 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,338 Option<&Registry>,339 Option<TelemetryHandle>,340 &TaskManager,341 Arc<dyn RelayChainInterface>,342 Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,343 Arc<NetworkService<Block, Hash>>,344 SyncCryptoStorePtr,345 bool,346 ) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,347{348 if matches!(parachain_config.role, Role::Light) {349 return Err("Light client not supported!".into());350 }351352 let parachain_config = prepare_node_config(parachain_config);353354 let params =355 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(¶chain_config, build_import_queue)?;356 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =357 params.other;358359 let client = params.client.clone();360 let backend = params.backend.clone();361 let mut task_manager = params.task_manager;362363 let (relay_chain_interface, collator_key) = build_relay_chain_interface(364 polkadot_config,365 ¶chain_config,366 telemetry_worker_handle,367 &mut task_manager,368 collator_options.clone(),369 )370 .await371 .map_err(|e| match e {372 RelayChainError::ServiceError(polkadot_service::Error::Sub(x)) => x,373 s => s.to_string().into(),374 })?;375376 let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);377378 let force_authoring = parachain_config.force_authoring;379 let validator = parachain_config.role.is_authority();380 let prometheus_registry = parachain_config.prometheus_registry().cloned();381 let transaction_pool = params.transaction_pool.clone();382 let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);383384 let (network, system_rpc_tx, start_network) =385 sc_service::build_network(sc_service::BuildNetworkParams {386 config: ¶chain_config,387 client: client.clone(),388 transaction_pool: transaction_pool.clone(),389 spawn_handle: task_manager.spawn_handle(),390 import_queue: import_queue.clone(),391 block_announce_validator_builder: Some(Box::new(|_| {392 Box::new(block_announce_validator)393 })),394 warp_sync: None,395 })?;396397 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());398 let rpc_client = client.clone();399 let rpc_pool = transaction_pool.clone();400 let select_chain = params.select_chain.clone();401 let rpc_network = network.clone();402403 let rpc_frontier_backend = frontier_backend.clone();404405 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(406 task_manager.spawn_handle(),407 overrides_handle::<_, _, Runtime>(client.clone()),408 50,409 50,410 ));411412 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {413 let full_deps = unique_rpc::FullDeps {414 backend: rpc_frontier_backend.clone(),415 deny_unsafe,416 client: rpc_client.clone(),417 pool: rpc_pool.clone(),418 graph: rpc_pool.pool().clone(),419 // TODO: Unhardcode420 enable_dev_signer: false,421 filter_pool: filter_pool.clone(),422 network: rpc_network.clone(),423 select_chain: select_chain.clone(),424 is_authority: validator,425 // TODO: Unhardcode426 max_past_logs: 10000,427 block_data_cache: block_data_cache.clone(),428 fee_history_cache: fee_history_cache.clone(),429 // TODO: Unhardcode430 fee_history_limit: 2048,431 };432433 Ok(434 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(435 full_deps,436 subscription_executor.clone(),437 ),438 )439 });440441 task_manager.spawn_essential_handle().spawn(442 "frontier-mapping-sync-worker",443 None,444 MappingSyncWorker::new(445 client.import_notification_stream(),446 Duration::new(6, 0),447 client.clone(),448 backend.clone(),449 frontier_backend.clone(),450 SyncStrategy::Normal,451 )452 .for_each(|()| futures::future::ready(())),453 );454455 sc_service::spawn_tasks(sc_service::SpawnTasksParams {456 rpc_extensions_builder,457 client: client.clone(),458 transaction_pool: transaction_pool.clone(),459 task_manager: &mut task_manager,460 config: parachain_config,461 keystore: params.keystore_container.sync_keystore(),462 backend: backend.clone(),463 network: network.clone(),464 system_rpc_tx,465 telemetry: telemetry.as_mut(),466 })?;467468 let announce_block = {469 let network = network.clone();470 Arc::new(move |hash, data| network.announce_block(hash, data))471 };472473 let relay_chain_slot_duration = Duration::from_secs(6);474475 if validator {476 let parachain_consensus = build_consensus(477 client.clone(),478 prometheus_registry.as_ref(),479 telemetry.as_ref().map(|t| t.handle()),480 &task_manager,481 relay_chain_interface.clone(),482 transaction_pool,483 network,484 params.keystore_container.sync_keystore(),485 force_authoring,486 )?;487488 let spawner = task_manager.spawn_handle();489490 let params = StartCollatorParams {491 para_id: id,492 block_status: client.clone(),493 announce_block,494 client: client.clone(),495 task_manager: &mut task_manager,496 spawner,497 parachain_consensus,498 import_queue,499 collator_key: collator_key.expect("Command line arguments do not allow this. qed"),500 relay_chain_interface,501 relay_chain_slot_duration,502 };503504 start_collator(params).await?;505 } else {506 let params = StartFullNodeParams {507 client: client.clone(),508 announce_block,509 task_manager: &mut task_manager,510 para_id: id,511 import_queue,512 relay_chain_interface,513 relay_chain_slot_duration,514 collator_options,515 };516517 start_full_node(params)?;518 }519520 start_network.start_network();521522 Ok((task_manager, client))523}524525/// Build the import queue for the the parachain runtime.526pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(527 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,528 config: &Configuration,529 telemetry: Option<TelemetryHandle>,530 task_manager: &TaskManager,531) -> Result<532 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,533 sc_service::Error,534>535where536 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>537 + Send538 + Sync539 + 'static,540 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>541 + sp_block_builder::BlockBuilder<Block>542 + sp_consensus_aura::AuraApi<Block, AuraId>543 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,544 ExecutorDispatch: NativeExecutionDispatch + 'static,545{546 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;547548 cumulus_client_consensus_aura::import_queue::<549 sp_consensus_aura::sr25519::AuthorityPair,550 _,551 _,552 _,553 _,554 _,555 _,556 >(cumulus_client_consensus_aura::ImportQueueParams {557 block_import: client.clone(),558 client: client.clone(),559 create_inherent_data_providers: move |_, _| async move {560 let time = sp_timestamp::InherentDataProvider::from_system_time();561562 let slot =563 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(564 *time,565 slot_duration,566 );567568 Ok((time, slot))569 },570 registry: config.prometheus_registry(),571 can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),572 spawner: &task_manager.spawn_essential_handle(),573 telemetry,574 })575 .map_err(Into::into)576}577578/// Start a normal parachain node.579pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(580 parachain_config: Configuration,581 polkadot_config: Configuration,582 collator_options: CollatorOptions,583 id: ParaId,584) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>585where586 Runtime: RuntimeInstance + Send + Sync + 'static,587 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,588 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,589 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>590 + Send591 + Sync592 + 'static,593 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>594 + fp_rpc::EthereumRuntimeRPCApi<Block>595 + sp_session::SessionKeys<Block>596 + sp_block_builder::BlockBuilder<Block>597 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>598 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>599 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>600 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>601 + sp_api::Metadata<Block>602 + sp_offchain::OffchainWorkerApi<Block>603 + cumulus_primitives_core::CollectCollationInfo<Block>604 + sp_consensus_aura::AuraApi<Block, AuraId>,605 ExecutorDispatch: NativeExecutionDispatch + 'static,606{607 start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(608 parachain_config,609 polkadot_config,610 collator_options,611 id,612 parachain_build_import_queue,613 |client,614 prometheus_registry,615 telemetry,616 task_manager,617 relay_chain_interface,618 transaction_pool,619 sync_oracle,620 keystore,621 force_authoring| {622 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;623624 let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(625 task_manager.spawn_handle(),626 client.clone(),627 transaction_pool,628 prometheus_registry,629 telemetry.clone(),630 );631632 Ok(AuraConsensus::build::<633 sp_consensus_aura::sr25519::AuthorityPair,634 _,635 _,636 _,637 _,638 _,639 _,640 >(BuildAuraConsensusParams {641 proposer_factory,642 create_inherent_data_providers: move |_, (relay_parent, validation_data)| {643 let relay_chain_interface = relay_chain_interface.clone();644 async move {645 let parachain_inherent =646 cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(647 relay_parent,648 &relay_chain_interface,649 &validation_data,650 id,651 ).await;652653 let time = sp_timestamp::InherentDataProvider::from_system_time();654655 let slot =656 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(657 *time,658 slot_duration,659 );660661 let parachain_inherent = parachain_inherent.ok_or_else(|| {662 Box::<dyn std::error::Error + Send + Sync>::from(663 "Failed to create parachain inherent",664 )665 })?;666 Ok((time, slot, parachain_inherent))667 }668 },669 block_import: client.clone(),670 para_client: client,671 backoff_authoring_blocks: Option::<()>::None,672 sync_oracle,673 keystore,674 force_authoring,675 slot_duration,676 // We got around 500ms for proposing677 block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),678 telemetry,679 max_block_proposal_slot_portion: None,680 }))681 },682 )683 .await684}685686fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(687 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,688 config: &Configuration,689 _: Option<TelemetryHandle>,690 task_manager: &TaskManager,691) -> Result<692 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,693 sc_service::Error,694>695where696 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>697 + Send698 + Sync699 + 'static,700 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>701 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,702 ExecutorDispatch: NativeExecutionDispatch + 'static,703{704 Ok(sc_consensus_manual_seal::import_queue(705 Box::new(client.clone()),706 &task_manager.spawn_essential_handle(),707 config.prometheus_registry(),708 ))709}710711/// Builds a new development service. This service uses instant seal, and mocks712/// the parachain inherent713pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(714 config: Configuration,715) -> sc_service::error::Result<TaskManager>716where717 Runtime: RuntimeInstance + Send + Sync + 'static,718 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,719 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,720 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>721 + Send722 + Sync723 + 'static,724 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>725 + fp_rpc::EthereumRuntimeRPCApi<Block>726 + sp_session::SessionKeys<Block>727 + sp_block_builder::BlockBuilder<Block>728 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>729 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>730 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>731 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>732 + sp_api::Metadata<Block>733 + sp_offchain::OffchainWorkerApi<Block>734 + cumulus_primitives_core::CollectCollationInfo<Block>735 + sp_consensus_aura::AuraApi<Block, AuraId>,736 ExecutorDispatch: NativeExecutionDispatch + 'static,737{738 use futures::Stream;739 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};740 use fc_consensus::FrontierBlockImport;741 use sc_client_api::HeaderBackend;742743 let sc_service::PartialComponents {744 client,745 backend,746 mut task_manager,747 import_queue,748 keystore_container,749 select_chain: maybe_select_chain,750 transaction_pool,751 other:752 (telemetry, filter_pool, frontier_backend, _telemetry_worker_handle, fee_history_cache),753 } = new_partial::<RuntimeApi, ExecutorDispatch, _>(754 &config,755 dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,756 )?;757758 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(759 task_manager.spawn_handle(),760 overrides_handle::<_, _, Runtime>(client.clone()),761 50,762 50,763 ));764765 let (network, system_rpc_tx, network_starter) =766 sc_service::build_network(sc_service::BuildNetworkParams {767 config: &config,768 client: client.clone(),769 transaction_pool: transaction_pool.clone(),770 spawn_handle: task_manager.spawn_handle(),771 import_queue,772 block_announce_validator_builder: None,773 warp_sync: None,774 })?;775776 if config.offchain_worker.enabled {777 sc_service::build_offchain_workers(778 &config,779 task_manager.spawn_handle(),780 client.clone(),781 network.clone(),782 );783 }784785 let prometheus_registry = config.prometheus_registry().cloned();786 let collator = config.role.is_authority();787788 let select_chain = maybe_select_chain.clone();789790 if collator {791 let block_import =792 FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());793794 let env = sc_basic_authorship::ProposerFactory::new(795 task_manager.spawn_handle(),796 client.clone(),797 transaction_pool.clone(),798 prometheus_registry.as_ref(),799 telemetry.as_ref().map(|x| x.handle()),800 );801802 let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =803 Box::new(804 // This bit cribbed from the implementation of instant seal.805 transaction_pool806 .pool()807 .validated_pool()808 .import_notification_stream()809 .map(|_| EngineCommand::SealNewBlock {810 create_empty: true, // was false in Moonbeam811 finalize: false,812 parent_hash: None,813 sender: None,814 }),815 );816817 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;818 let client_set_aside_for_cidp = client.clone();819820 task_manager.spawn_essential_handle().spawn_blocking(821 "authorship_task",822 Some("block-authoring"),823 run_manual_seal(ManualSealParams {824 block_import,825 env,826 client: client.clone(),827 pool: transaction_pool.clone(),828 commands_stream,829 select_chain: select_chain.clone(),830 consensus_data_provider: None,831 create_inherent_data_providers: move |block: Hash, ()| {832 let current_para_block = client_set_aside_for_cidp833 .number(block)834 .expect("Header lookup should succeed")835 .expect("Header passed in as parent should be present in backend.");836837 let client_for_xcm = client_set_aside_for_cidp.clone();838 async move {839 let time = sp_timestamp::InherentDataProvider::from_system_time();840841 let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {842 current_para_block,843 relay_offset: 1000,844 relay_blocks_per_para_block: 2,845 xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(846 &*client_for_xcm,847 block,848 Default::default(),849 Default::default(),850 ),851 raw_downward_messages: vec![],852 raw_horizontal_messages: vec![],853 };854855 let slot =856 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(857 *time,858 slot_duration,859 );860861 Ok((time, slot, mocked_parachain))862 }863 },864 }),865 );866 }867868 task_manager.spawn_essential_handle().spawn(869 "frontier-mapping-sync-worker",870 Some("block-authoring"),871 MappingSyncWorker::new(872 client.import_notification_stream(),873 Duration::new(6, 0),874 client.clone(),875 backend.clone(),876 frontier_backend.clone(),877 SyncStrategy::Normal,878 )879 .for_each(|()| futures::future::ready(())),880 );881882 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());883 let rpc_client = client.clone();884 let rpc_pool = transaction_pool.clone();885 let rpc_network = network.clone();886 let rpc_frontier_backend = frontier_backend.clone();887 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {888 let full_deps = unique_rpc::FullDeps {889 backend: rpc_frontier_backend.clone(),890 deny_unsafe,891 client: rpc_client.clone(),892 pool: rpc_pool.clone(),893 graph: rpc_pool.pool().clone(),894 // TODO: Unhardcode895 enable_dev_signer: false,896 filter_pool: filter_pool.clone(),897 network: rpc_network.clone(),898 select_chain: select_chain.clone(),899 is_authority: collator,900 // TODO: Unhardcode901 max_past_logs: 10000,902 block_data_cache: block_data_cache.clone(),903 fee_history_cache: fee_history_cache.clone(),904 // TODO: Unhardcode905 fee_history_limit: 2048,906 };907908 Ok(909 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(910 full_deps,911 subscription_executor.clone(),912 ),913 )914 });915916 sc_service::spawn_tasks(sc_service::SpawnTasksParams {917 network,918 client,919 keystore: keystore_container.sync_keystore(),920 task_manager: &mut task_manager,921 transaction_pool,922 rpc_extensions_builder,923 backend,924 system_rpc_tx,925 config,926 telemetry: None,927 })?;928929 network_starter.start_network();930 Ok(task_manager)931}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 std::pin::Pin;25use fc_rpc_core::types::FeeHistoryCache;26use futures::Future;27use futures::{Stream, StreamExt, stream::select, task::{Context, Poll}};28use futures_timer::Delay;2930use unique_rpc::overrides_handle;3132use serde::{Serialize, Deserialize};3334// Cumulus Imports35use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};36use cumulus_client_consensus_common::ParachainConsensus;37use cumulus_client_service::{38 prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,39};40use cumulus_client_cli::CollatorOptions;41use cumulus_client_network::BlockAnnounceValidator;42use cumulus_primitives_core::ParaId;43use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;44use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};45use cumulus_relay_chain_rpc_interface::RelayChainRPCInterface;4647// Substrate Imports48use sc_client_api::ExecutorProvider;49use sc_executor::NativeElseWasmExecutor;50use sc_executor::NativeExecutionDispatch;51use sc_network::NetworkService;52use sc_service::{BasePath, Configuration, PartialComponents, Role, TaskManager};53use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};54use sp_keystore::SyncCryptoStorePtr;55use sp_runtime::traits::BlakeTwo256;56use substrate_prometheus_endpoint::Registry;57use sc_client_api::BlockchainEvents;5859use polkadot_service::CollatorPair;6061// Frontier Imports62use fc_rpc_core::types::FilterPool;63use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};6465use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};6667/// Unique native executor instance.68#[cfg(feature = "unique-runtime")]69pub struct UniqueRuntimeExecutor;7071#[cfg(feature = "quartz-runtime")]72/// Quartz native executor instance.7374pub struct QuartzRuntimeExecutor;7576/// Opal native executor instance.77pub struct OpalRuntimeExecutor;7879#[cfg(feature = "unique-runtime")]80impl NativeExecutionDispatch for UniqueRuntimeExecutor {81 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8283 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {84 unique_runtime::api::dispatch(method, data)85 }8687 fn native_version() -> sc_executor::NativeVersion {88 unique_runtime::native_version()89 }90}9192#[cfg(feature = "quartz-runtime")]93impl NativeExecutionDispatch for QuartzRuntimeExecutor {94 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9596 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {97 quartz_runtime::api::dispatch(method, data)98 }99100 fn native_version() -> sc_executor::NativeVersion {101 quartz_runtime::native_version()102 }103}104105impl NativeExecutionDispatch for OpalRuntimeExecutor {106 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;107108 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {109 opal_runtime::api::dispatch(method, data)110 }111112 fn native_version() -> sc_executor::NativeVersion {113 opal_runtime::native_version()114 }115}116117pub struct AutosealInterval {118 duration: Duration,119 delay_handle: Pin<Box<Delay>>120}121122impl AutosealInterval {123 pub fn new(duration: Duration) -> Result<Self, String> {124 if duration.is_zero() {125 return Err("Invalid autoseal interval: 0 seconds".into());126 }127128 Ok(Self {129 duration,130 delay_handle: Box::pin(Delay::new(duration))131 })132 }133}134135impl Stream for AutosealInterval {136 type Item = ();137138 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {139 match self.delay_handle.as_mut().poll(cx) {140 Poll::Ready(_) => {141 let duration = self.duration;142 self.delay_handle.reset(duration);143144 Poll::Ready(Some(()))145 }146 Poll::Pending => Poll::Pending147 }148 }149}150151pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {152 let config_dir = config153 .base_path154 .as_ref()155 .map(|base_path| base_path.config_dir(config.chain_spec.id()))156 .unwrap_or_else(|| {157 BasePath::from_project("", "", "unique").config_dir(config.chain_spec.id())158 });159 let database_dir = config_dir.join("frontier").join("db");160161 Ok(Arc::new(fc_db::Backend::<Block>::new(162 &fc_db::DatabaseSettings {163 source: fc_db::DatabaseSettingsSrc::RocksDb {164 path: database_dir,165 cache_size: 0,166 },167 },168 )?))169}170171type FullClient<RuntimeApi, ExecutorDispatch> =172 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;173type FullBackend = sc_service::TFullBackend<Block>;174type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;175176/// Starts a `ServiceBuilder` for a full service.177///178/// Use this macro if you don't actually need the full service, but just the builder in order to179/// be able to perform chain operations.180#[allow(clippy::type_complexity)]181pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(182 config: &Configuration,183 build_import_queue: BIQ,184) -> Result<185 PartialComponents<186 FullClient<RuntimeApi, ExecutorDispatch>,187 FullBackend,188 FullSelectChain,189 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,190 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,191 (192 Option<Telemetry>,193 Option<FilterPool>,194 Arc<fc_db::Backend<Block>>,195 Option<TelemetryWorkerHandle>,196 FeeHistoryCache,197 ),198 >,199 sc_service::Error,200>201where202 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,203 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>204 + Send205 + Sync206 + 'static,207 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,208 ExecutorDispatch: NativeExecutionDispatch + 'static,209 BIQ: FnOnce(210 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,211 &Configuration,212 Option<TelemetryHandle>,213 &TaskManager,214 ) -> Result<215 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,216 sc_service::Error,217 >,218{219 let _telemetry = config220 .telemetry_endpoints221 .clone()222 .filter(|x| !x.is_empty())223 .map(|endpoints| -> Result<_, sc_telemetry::Error> {224 let worker = TelemetryWorker::new(16)?;225 let telemetry = worker.handle().new_telemetry(endpoints);226 Ok((worker, telemetry))227 })228 .transpose()?;229230 let telemetry = config231 .telemetry_endpoints232 .clone()233 .filter(|x| !x.is_empty())234 .map(|endpoints| -> Result<_, sc_telemetry::Error> {235 let worker = TelemetryWorker::new(16)?;236 let telemetry = worker.handle().new_telemetry(endpoints);237 Ok((worker, telemetry))238 })239 .transpose()?;240241 let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(242 config.wasm_method,243 config.default_heap_pages,244 config.max_runtime_instances,245 config.runtime_cache_size,246 );247248 let (client, backend, keystore_container, task_manager) =249 sc_service::new_full_parts::<Block, RuntimeApi, _>(250 config,251 telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),252 executor,253 )?;254 let client = Arc::new(client);255256 let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());257258 let telemetry = telemetry.map(|(worker, telemetry)| {259 task_manager260 .spawn_handle()261 .spawn("telemetry", None, worker.run());262 telemetry263 });264265 let select_chain = sc_consensus::LongestChain::new(backend.clone());266267 let transaction_pool = sc_transaction_pool::BasicPool::new_full(268 config.transaction_pool.clone(),269 config.role.is_authority().into(),270 config.prometheus_registry(),271 task_manager.spawn_essential_handle(),272 client.clone(),273 );274275 let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));276277 let frontier_backend = open_frontier_backend(config)?;278279 let import_queue = build_import_queue(280 client.clone(),281 config,282 telemetry.as_ref().map(|telemetry| telemetry.handle()),283 &task_manager,284 )?;285 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));286287 let params = PartialComponents {288 backend,289 client,290 import_queue,291 keystore_container,292 task_manager,293 transaction_pool,294 select_chain,295 other: (296 telemetry,297 filter_pool,298 frontier_backend,299 telemetry_worker_handle,300 fee_history_cache,301 ),302 };303304 Ok(params)305}306307async fn build_relay_chain_interface(308 polkadot_config: Configuration,309 parachain_config: &Configuration,310 telemetry_worker_handle: Option<TelemetryWorkerHandle>,311 task_manager: &mut TaskManager,312 collator_options: CollatorOptions,313) -> RelayChainResult<(314 Arc<(dyn RelayChainInterface + 'static)>,315 Option<CollatorPair>,316)> {317 match collator_options.relay_chain_rpc_url {318 Some(relay_chain_url) => Ok((319 Arc::new(RelayChainRPCInterface::new(relay_chain_url).await?) as Arc<_>,320 None,321 )),322 None => build_inprocess_relay_chain(323 polkadot_config,324 parachain_config,325 telemetry_worker_handle,326 task_manager,327 ),328 }329}330331/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.332///333/// This is the actual implementation that is abstract over the executor and the runtime api.334#[sc_tracing::logging::prefix_logs_with("Parachain")]335async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(336 parachain_config: Configuration,337 polkadot_config: Configuration,338 collator_options: CollatorOptions,339 id: ParaId,340 build_import_queue: BIQ,341 build_consensus: BIC,342) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>343where344 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,345 Runtime: RuntimeInstance + Send + Sync + 'static,346 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,347 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,348 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>349 + Send350 + Sync351 + 'static,352 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>353 + fp_rpc::EthereumRuntimeRPCApi<Block>354 + sp_session::SessionKeys<Block>355 + sp_block_builder::BlockBuilder<Block>356 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>357 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>358 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>359 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>360 + sp_api::Metadata<Block>361 + sp_offchain::OffchainWorkerApi<Block>362 + cumulus_primitives_core::CollectCollationInfo<Block>,363 ExecutorDispatch: NativeExecutionDispatch + 'static,364 BIQ: FnOnce(365 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,366 &Configuration,367 Option<TelemetryHandle>,368 &TaskManager,369 ) -> Result<370 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,371 sc_service::Error,372 >,373 BIC: FnOnce(374 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,375 Option<&Registry>,376 Option<TelemetryHandle>,377 &TaskManager,378 Arc<dyn RelayChainInterface>,379 Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,380 Arc<NetworkService<Block, Hash>>,381 SyncCryptoStorePtr,382 bool,383 ) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,384{385 if matches!(parachain_config.role, Role::Light) {386 return Err("Light client not supported!".into());387 }388389 let parachain_config = prepare_node_config(parachain_config);390391 let params =392 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(¶chain_config, build_import_queue)?;393 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =394 params.other;395396 let client = params.client.clone();397 let backend = params.backend.clone();398 let mut task_manager = params.task_manager;399400 let (relay_chain_interface, collator_key) = build_relay_chain_interface(401 polkadot_config,402 ¶chain_config,403 telemetry_worker_handle,404 &mut task_manager,405 collator_options.clone(),406 )407 .await408 .map_err(|e| match e {409 RelayChainError::ServiceError(polkadot_service::Error::Sub(x)) => x,410 s => s.to_string().into(),411 })?;412413 let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);414415 let force_authoring = parachain_config.force_authoring;416 let validator = parachain_config.role.is_authority();417 let prometheus_registry = parachain_config.prometheus_registry().cloned();418 let transaction_pool = params.transaction_pool.clone();419 let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);420421 let (network, system_rpc_tx, start_network) =422 sc_service::build_network(sc_service::BuildNetworkParams {423 config: ¶chain_config,424 client: client.clone(),425 transaction_pool: transaction_pool.clone(),426 spawn_handle: task_manager.spawn_handle(),427 import_queue: import_queue.clone(),428 block_announce_validator_builder: Some(Box::new(|_| {429 Box::new(block_announce_validator)430 })),431 warp_sync: None,432 })?;433434 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());435 let rpc_client = client.clone();436 let rpc_pool = transaction_pool.clone();437 let select_chain = params.select_chain.clone();438 let rpc_network = network.clone();439440 let rpc_frontier_backend = frontier_backend.clone();441442 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(443 task_manager.spawn_handle(),444 overrides_handle::<_, _, Runtime>(client.clone()),445 50,446 50,447 ));448449 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {450 let full_deps = unique_rpc::FullDeps {451 backend: rpc_frontier_backend.clone(),452 deny_unsafe,453 client: rpc_client.clone(),454 pool: rpc_pool.clone(),455 graph: rpc_pool.pool().clone(),456 // TODO: Unhardcode457 enable_dev_signer: false,458 filter_pool: filter_pool.clone(),459 network: rpc_network.clone(),460 select_chain: select_chain.clone(),461 is_authority: validator,462 // TODO: Unhardcode463 max_past_logs: 10000,464 block_data_cache: block_data_cache.clone(),465 fee_history_cache: fee_history_cache.clone(),466 // TODO: Unhardcode467 fee_history_limit: 2048,468 };469470 Ok(471 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(472 full_deps,473 subscription_executor.clone(),474 ),475 )476 });477478 task_manager.spawn_essential_handle().spawn(479 "frontier-mapping-sync-worker",480 None,481 MappingSyncWorker::new(482 client.import_notification_stream(),483 Duration::new(6, 0),484 client.clone(),485 backend.clone(),486 frontier_backend.clone(),487 SyncStrategy::Normal,488 )489 .for_each(|()| futures::future::ready(())),490 );491492 sc_service::spawn_tasks(sc_service::SpawnTasksParams {493 rpc_extensions_builder,494 client: client.clone(),495 transaction_pool: transaction_pool.clone(),496 task_manager: &mut task_manager,497 config: parachain_config,498 keystore: params.keystore_container.sync_keystore(),499 backend: backend.clone(),500 network: network.clone(),501 system_rpc_tx,502 telemetry: telemetry.as_mut(),503 })?;504505 let announce_block = {506 let network = network.clone();507 Arc::new(move |hash, data| network.announce_block(hash, data))508 };509510 let relay_chain_slot_duration = Duration::from_secs(6);511512 if validator {513 let parachain_consensus = build_consensus(514 client.clone(),515 prometheus_registry.as_ref(),516 telemetry.as_ref().map(|t| t.handle()),517 &task_manager,518 relay_chain_interface.clone(),519 transaction_pool,520 network,521 params.keystore_container.sync_keystore(),522 force_authoring,523 )?;524525 let spawner = task_manager.spawn_handle();526527 let params = StartCollatorParams {528 para_id: id,529 block_status: client.clone(),530 announce_block,531 client: client.clone(),532 task_manager: &mut task_manager,533 spawner,534 parachain_consensus,535 import_queue,536 collator_key: collator_key.expect("Command line arguments do not allow this. qed"),537 relay_chain_interface,538 relay_chain_slot_duration,539 };540541 start_collator(params).await?;542 } else {543 let params = StartFullNodeParams {544 client: client.clone(),545 announce_block,546 task_manager: &mut task_manager,547 para_id: id,548 import_queue,549 relay_chain_interface,550 relay_chain_slot_duration,551 collator_options,552 };553554 start_full_node(params)?;555 }556557 start_network.start_network();558559 Ok((task_manager, client))560}561562/// Build the import queue for the the parachain runtime.563pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(564 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,565 config: &Configuration,566 telemetry: Option<TelemetryHandle>,567 task_manager: &TaskManager,568) -> Result<569 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,570 sc_service::Error,571>572where573 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>574 + Send575 + Sync576 + 'static,577 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>578 + sp_block_builder::BlockBuilder<Block>579 + sp_consensus_aura::AuraApi<Block, AuraId>580 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,581 ExecutorDispatch: NativeExecutionDispatch + 'static,582{583 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;584585 cumulus_client_consensus_aura::import_queue::<586 sp_consensus_aura::sr25519::AuthorityPair,587 _,588 _,589 _,590 _,591 _,592 _,593 >(cumulus_client_consensus_aura::ImportQueueParams {594 block_import: client.clone(),595 client: client.clone(),596 create_inherent_data_providers: move |_, _| async move {597 let time = sp_timestamp::InherentDataProvider::from_system_time();598599 let slot =600 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(601 *time,602 slot_duration,603 );604605 Ok((time, slot))606 },607 registry: config.prometheus_registry(),608 can_author_with: sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),609 spawner: &task_manager.spawn_essential_handle(),610 telemetry,611 })612 .map_err(Into::into)613}614615/// Start a normal parachain node.616pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(617 parachain_config: Configuration,618 polkadot_config: Configuration,619 collator_options: CollatorOptions,620 id: ParaId,621) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>622where623 Runtime: RuntimeInstance + Send + Sync + 'static,624 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,625 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,626 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>627 + Send628 + Sync629 + 'static,630 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>631 + fp_rpc::EthereumRuntimeRPCApi<Block>632 + sp_session::SessionKeys<Block>633 + sp_block_builder::BlockBuilder<Block>634 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>635 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>636 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>637 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>638 + sp_api::Metadata<Block>639 + sp_offchain::OffchainWorkerApi<Block>640 + cumulus_primitives_core::CollectCollationInfo<Block>641 + sp_consensus_aura::AuraApi<Block, AuraId>,642 ExecutorDispatch: NativeExecutionDispatch + 'static,643{644 start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(645 parachain_config,646 polkadot_config,647 collator_options,648 id,649 parachain_build_import_queue,650 |client,651 prometheus_registry,652 telemetry,653 task_manager,654 relay_chain_interface,655 transaction_pool,656 sync_oracle,657 keystore,658 force_authoring| {659 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;660661 let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(662 task_manager.spawn_handle(),663 client.clone(),664 transaction_pool,665 prometheus_registry,666 telemetry.clone(),667 );668669 Ok(AuraConsensus::build::<670 sp_consensus_aura::sr25519::AuthorityPair,671 _,672 _,673 _,674 _,675 _,676 _,677 >(BuildAuraConsensusParams {678 proposer_factory,679 create_inherent_data_providers: move |_, (relay_parent, validation_data)| {680 let relay_chain_interface = relay_chain_interface.clone();681 async move {682 let parachain_inherent =683 cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(684 relay_parent,685 &relay_chain_interface,686 &validation_data,687 id,688 ).await;689690 let time = sp_timestamp::InherentDataProvider::from_system_time();691692 let slot =693 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(694 *time,695 slot_duration,696 );697698 let parachain_inherent = parachain_inherent.ok_or_else(|| {699 Box::<dyn std::error::Error + Send + Sync>::from(700 "Failed to create parachain inherent",701 )702 })?;703 Ok((time, slot, parachain_inherent))704 }705 },706 block_import: client.clone(),707 para_client: client,708 backoff_authoring_blocks: Option::<()>::None,709 sync_oracle,710 keystore,711 force_authoring,712 slot_duration,713 // We got around 500ms for proposing714 block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),715 telemetry,716 max_block_proposal_slot_portion: None,717 }))718 },719 )720 .await721}722723fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(724 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,725 config: &Configuration,726 _: Option<TelemetryHandle>,727 task_manager: &TaskManager,728) -> Result<729 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,730 sc_service::Error,731>732where733 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>734 + Send735 + Sync736 + 'static,737 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>738 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,739 ExecutorDispatch: NativeExecutionDispatch + 'static,740{741 Ok(sc_consensus_manual_seal::import_queue(742 Box::new(client.clone()),743 &task_manager.spawn_essential_handle(),744 config.prometheus_registry(),745 ))746}747748/// Builds a new development service. This service uses instant seal, and mocks749/// the parachain inherent750pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(751 config: Configuration,752 autoseal_interval: AutosealInterval,753) -> sc_service::error::Result<TaskManager>754where755 Runtime: RuntimeInstance + Send + Sync + 'static,756 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,757 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,758 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>759 + Send760 + Sync761 + 'static,762 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>763 + fp_rpc::EthereumRuntimeRPCApi<Block>764 + sp_session::SessionKeys<Block>765 + sp_block_builder::BlockBuilder<Block>766 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>767 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>768 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>769 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>770 + sp_api::Metadata<Block>771 + sp_offchain::OffchainWorkerApi<Block>772 + cumulus_primitives_core::CollectCollationInfo<Block>773 + sp_consensus_aura::AuraApi<Block, AuraId>,774 ExecutorDispatch: NativeExecutionDispatch + 'static,775{776 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};777 use fc_consensus::FrontierBlockImport;778 use sc_client_api::HeaderBackend;779780 let sc_service::PartialComponents {781 client,782 backend,783 mut task_manager,784 import_queue,785 keystore_container,786 select_chain: maybe_select_chain,787 transaction_pool,788 other:789 (telemetry, filter_pool, frontier_backend, _telemetry_worker_handle, fee_history_cache),790 } = new_partial::<RuntimeApi, ExecutorDispatch, _>(791 &config,792 dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,793 )?;794795 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(796 task_manager.spawn_handle(),797 overrides_handle::<_, _, Runtime>(client.clone()),798 50,799 50,800 ));801802 let (network, system_rpc_tx, network_starter) =803 sc_service::build_network(sc_service::BuildNetworkParams {804 config: &config,805 client: client.clone(),806 transaction_pool: transaction_pool.clone(),807 spawn_handle: task_manager.spawn_handle(),808 import_queue,809 block_announce_validator_builder: None,810 warp_sync: None,811 })?;812813 if config.offchain_worker.enabled {814 sc_service::build_offchain_workers(815 &config,816 task_manager.spawn_handle(),817 client.clone(),818 network.clone(),819 );820 }821822 let prometheus_registry = config.prometheus_registry().cloned();823 let collator = config.role.is_authority();824825 let select_chain = maybe_select_chain.clone();826827 if collator {828 let block_import =829 FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());830831 let env = sc_basic_authorship::ProposerFactory::new(832 task_manager.spawn_handle(),833 client.clone(),834 transaction_pool.clone(),835 prometheus_registry.as_ref(),836 telemetry.as_ref().map(|x| x.handle()),837 );838839 let transactions_commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =840 Box::new(841 transaction_pool842 .pool()843 .validated_pool()844 .import_notification_stream()845 .map(|_| EngineCommand::SealNewBlock {846 create_empty: true,847 finalize: false,848 parent_hash: None,849 sender: None,850 }),851 );852853 let autoseal_interval = Box::pin(autoseal_interval);854 let idle_commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =855 Box::new(856 autoseal_interval.map(|_| EngineCommand::SealNewBlock {857 create_empty: true,858 finalize: false,859 parent_hash: None,860 sender: None,861 })862 );863864 let commands_stream = select(865 transactions_commands_stream,866 idle_commands_stream867 );868869 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;870 let client_set_aside_for_cidp = client.clone();871872 task_manager.spawn_essential_handle().spawn_blocking(873 "authorship_task",874 Some("block-authoring"),875 run_manual_seal(ManualSealParams {876 block_import,877 env,878 client: client.clone(),879 pool: transaction_pool.clone(),880 commands_stream,881 select_chain: select_chain.clone(),882 consensus_data_provider: None,883 create_inherent_data_providers: move |block: Hash, ()| {884 let current_para_block = client_set_aside_for_cidp885 .number(block)886 .expect("Header lookup should succeed")887 .expect("Header passed in as parent should be present in backend.");888889 let client_for_xcm = client_set_aside_for_cidp.clone();890 async move {891 let time = sp_timestamp::InherentDataProvider::from_system_time();892893 let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {894 current_para_block,895 relay_offset: 1000,896 relay_blocks_per_para_block: 2,897 xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(898 &*client_for_xcm,899 block,900 Default::default(),901 Default::default(),902 ),903 raw_downward_messages: vec![],904 raw_horizontal_messages: vec![],905 };906907 let slot =908 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(909 *time,910 slot_duration,911 );912913 Ok((time, slot, mocked_parachain))914 }915 },916 }),917 );918 }919920 task_manager.spawn_essential_handle().spawn(921 "frontier-mapping-sync-worker",922 Some("block-authoring"),923 MappingSyncWorker::new(924 client.import_notification_stream(),925 Duration::new(6, 0),926 client.clone(),927 backend.clone(),928 frontier_backend.clone(),929 SyncStrategy::Normal,930 )931 .for_each(|()| futures::future::ready(())),932 );933934 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());935 let rpc_client = client.clone();936 let rpc_pool = transaction_pool.clone();937 let rpc_network = network.clone();938 let rpc_frontier_backend = frontier_backend.clone();939 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {940 let full_deps = unique_rpc::FullDeps {941 backend: rpc_frontier_backend.clone(),942 deny_unsafe,943 client: rpc_client.clone(),944 pool: rpc_pool.clone(),945 graph: rpc_pool.pool().clone(),946 // TODO: Unhardcode947 enable_dev_signer: false,948 filter_pool: filter_pool.clone(),949 network: rpc_network.clone(),950 select_chain: select_chain.clone(),951 is_authority: collator,952 // TODO: Unhardcode953 max_past_logs: 10000,954 block_data_cache: block_data_cache.clone(),955 fee_history_cache: fee_history_cache.clone(),956 // TODO: Unhardcode957 fee_history_limit: 2048,958 };959960 Ok(961 unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(962 full_deps,963 subscription_executor.clone(),964 ),965 )966 });967968 sc_service::spawn_tasks(sc_service::SpawnTasksParams {969 network,970 client,971 keystore: keystore_container.sync_keystore(),972 task_manager: &mut task_manager,973 transaction_pool,974 rpc_extensions_builder,975 backend,976 system_rpc_tx,977 config,978 telemetry: None,979 })?;980981 network_starter.start_network();982 Ok(task_manager)983}