123456789101112131415161718use std::sync::Arc;19use std::sync::Mutex;20use std::collections::BTreeMap;21use std::time::Duration;22use std::pin::Pin;23use fc_mapping_sync::EthereumBlockNotificationSinks;24use fc_rpc::EthBlockDataCacheTask;25use fc_rpc::EthTask;26use fc_rpc_core::types::FeeHistoryCache;27use futures::{28 Stream, StreamExt,29 stream::select,30 task::{Context, Poll},31};32use sc_rpc::SubscriptionTaskExecutor;33use sp_keystore::KeystorePtr;34use tokio::time::Interval;35use jsonrpsee::RpcModule;3637use serde::{Serialize, Deserialize};383940use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};41use cumulus_client_consensus_common::{42 ParachainConsensus, ParachainBlockImport as TParachainBlockImport,43};44use cumulus_client_service::{45 prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,46};47use cumulus_client_cli::CollatorOptions;48use cumulus_client_network::BlockAnnounceValidator;49use cumulus_primitives_core::ParaId;50use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;51use cumulus_relay_chain_interface::{RelayChainInterface, RelayChainResult};52use cumulus_relay_chain_minimal_node::build_minimal_relay_chain_node;535455use sp_api::{BlockT, HeaderT, ProvideRuntimeApi, StateBackend};56use sc_executor::NativeElseWasmExecutor;57use sc_executor::NativeExecutionDispatch;58use sc_network::NetworkBlock;59use sc_network_sync::SyncingService;60use sc_service::{Configuration, PartialComponents, TaskManager};61use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};62use sp_runtime::traits::BlakeTwo256;63use substrate_prometheus_endpoint::Registry;64use sc_client_api::{BlockchainEvents, BlockOf, Backend, AuxStore, StorageProvider};65use sp_blockchain::{HeaderBackend, HeaderMetadata, Error as BlockChainError};66use sc_consensus::ImportQueue;67use sp_core::H256;68use sp_block_builder::BlockBuilder;6970use polkadot_service::CollatorPair;717273use fc_rpc_core::types::FilterPool;74use fc_mapping_sync::{kv::MappingSyncWorker, SyncStrategy};75use fc_rpc::{76 StorageOverride, OverrideHandle, SchemaV1Override, SchemaV2Override, SchemaV3Override,77 RuntimeApiStorageOverride,78};79use fp_rpc::EthereumRuntimeRPCApi;80use fp_storage::EthereumStorageSchema;8182use up_common::types::opaque::*;8384use crate::chain_spec::RuntimeIdentification;858687#[cfg(feature = "unique-runtime")]88pub struct UniqueRuntimeExecutor;8990#[cfg(feature = "quartz-runtime")]9192pub struct QuartzRuntimeExecutor;939495pub struct OpalRuntimeExecutor;9697#[cfg(all(feature = "unique-runtime", feature = "runtime-benchmarks"))]98pub type DefaultRuntimeExecutor = UniqueRuntimeExecutor;99100#[cfg(all(101 not(feature = "unique-runtime"),102 feature = "quartz-runtime",103 feature = "runtime-benchmarks"104))]105pub type DefaultRuntimeExecutor = QuartzRuntimeExecutor;106107#[cfg(all(108 not(feature = "unique-runtime"),109 not(feature = "quartz-runtime"),110 feature = "runtime-benchmarks"111))]112pub type DefaultRuntimeExecutor = OpalRuntimeExecutor;113114#[cfg(feature = "unique-runtime")]115impl NativeExecutionDispatch for UniqueRuntimeExecutor {116 117 #[cfg(feature = "runtime-benchmarks")]118 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;119 120 #[cfg(not(feature = "runtime-benchmarks"))]121 type ExtendHostFunctions = ();122123 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {124 unique_runtime::api::dispatch(method, data)125 }126127 fn native_version() -> sc_executor::NativeVersion {128 unique_runtime::native_version()129 }130}131132#[cfg(feature = "quartz-runtime")]133impl NativeExecutionDispatch for QuartzRuntimeExecutor {134 135 #[cfg(feature = "runtime-benchmarks")]136 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;137 138 #[cfg(not(feature = "runtime-benchmarks"))]139 type ExtendHostFunctions = ();140141 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {142 quartz_runtime::api::dispatch(method, data)143 }144145 fn native_version() -> sc_executor::NativeVersion {146 quartz_runtime::native_version()147 }148}149150impl NativeExecutionDispatch for OpalRuntimeExecutor {151 152 #[cfg(feature = "runtime-benchmarks")]153 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;154 155 #[cfg(not(feature = "runtime-benchmarks"))]156 type ExtendHostFunctions = ();157158 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {159 opal_runtime::api::dispatch(method, data)160 }161162 fn native_version() -> sc_executor::NativeVersion {163 opal_runtime::native_version()164 }165}166167pub struct AutosealInterval {168 interval: Interval,169}170171impl AutosealInterval {172 pub fn new(config: &Configuration, interval: u64) -> Self {173 let _tokio_runtime = config.tokio_handle.enter();174 let interval = tokio::time::interval(Duration::from_millis(interval));175176 Self { interval }177 }178}179180impl Stream for AutosealInterval {181 type Item = tokio::time::Instant;182183 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {184 self.interval.poll_tick(cx).map(Some)185 }186}187188pub fn open_frontier_backend<Block: BlockT, C: HeaderBackend<Block>>(189 client: Arc<C>,190 config: &Configuration,191) -> Result<Arc<fc_db::kv::Backend<Block>>, String> {192 let config_dir = config.base_path.config_dir(config.chain_spec.id());193 let database_dir = config_dir.join("frontier").join("db");194195 Ok(Arc::new(fc_db::kv::Backend::<Block>::new(196 client,197 &fc_db::kv::DatabaseSettings {198 source: fc_db::DatabaseSource::RocksDb {199 path: database_dir,200 cache_size: 0,201 },202 },203 )?))204}205206type FullClient<RuntimeApi, ExecutorDispatch> =207 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;208type FullBackend = sc_service::TFullBackend<Block>;209type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;210type ParachainBlockImport<RuntimeApi, ExecutorDispatch> =211 TParachainBlockImport<Block, Arc<FullClient<RuntimeApi, ExecutorDispatch>>, FullBackend>;212213214215216217#[allow(clippy::type_complexity)]218pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(219 config: &Configuration,220 build_import_queue: BIQ,221) -> Result<222 PartialComponents<223 FullClient<RuntimeApi, ExecutorDispatch>,224 FullBackend,225 FullSelectChain,226 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,227 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,228 OtherPartial,229 >,230 sc_service::Error,231>232where233 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,234 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>235 + Send236 + Sync237 + 'static,238 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,239 ExecutorDispatch: NativeExecutionDispatch + 'static,240 BIQ: FnOnce(241 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,242 Arc<FullBackend>,243 &Configuration,244 Option<TelemetryHandle>,245 &TaskManager,246 ) -> Result<247 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,248 sc_service::Error,249 >,250{251 let telemetry = config252 .telemetry_endpoints253 .clone()254 .filter(|x| !x.is_empty())255 .map(|endpoints| -> Result<_, sc_telemetry::Error> {256 let worker = TelemetryWorker::new(16)?;257 let telemetry = worker.handle().new_telemetry(endpoints);258 Ok((worker, telemetry))259 })260 .transpose()?;261262 let executor = sc_service::new_native_or_wasm_executor(config);263264 let (client, backend, keystore_container, task_manager) =265 sc_service::new_full_parts::<Block, RuntimeApi, _>(266 config,267 telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),268 executor,269 )?;270 let client = Arc::new(client);271272 let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());273274 let telemetry = telemetry.map(|(worker, telemetry)| {275 task_manager276 .spawn_handle()277 .spawn("telemetry", None, worker.run());278 telemetry279 });280281 let select_chain = sc_consensus::LongestChain::new(backend.clone());282283 let transaction_pool = sc_transaction_pool::BasicPool::new_full(284 config.transaction_pool.clone(),285 config.role.is_authority().into(),286 config.prometheus_registry(),287 task_manager.spawn_essential_handle(),288 client.clone(),289 );290291 let eth_filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));292293 let eth_backend = open_frontier_backend(client.clone(), config)?;294295 let import_queue = build_import_queue(296 client.clone(),297 backend.clone(),298 config,299 telemetry.as_ref().map(|telemetry| telemetry.handle()),300 &task_manager,301 )?;302303 let params = PartialComponents {304 backend,305 client,306 import_queue,307 keystore_container,308 task_manager,309 transaction_pool,310 select_chain,311 other: OtherPartial {312 telemetry,313 eth_filter_pool,314 eth_backend,315 telemetry_worker_handle,316 },317 };318319 Ok(params)320}321322async fn build_relay_chain_interface(323 polkadot_config: Configuration,324 parachain_config: &Configuration,325 telemetry_worker_handle: Option<TelemetryWorkerHandle>,326 task_manager: &mut TaskManager,327 collator_options: CollatorOptions,328 hwbench: Option<sc_sysinfo::HwBench>,329) -> RelayChainResult<(330 Arc<(dyn RelayChainInterface + 'static)>,331 Option<CollatorPair>,332)> {333 if collator_options.relay_chain_rpc_urls.is_empty() {334 build_inprocess_relay_chain(335 polkadot_config,336 parachain_config,337 telemetry_worker_handle,338 task_manager,339 hwbench,340 )341 } else {342 build_minimal_relay_chain_node(343 polkadot_config,344 task_manager,345 collator_options.relay_chain_rpc_urls,346 )347 .await348 }349}350351macro_rules! clone {352 ($($i:ident),* $(,)?) => {353 $(354 let $i = $i.clone();355 )*356 };357}358359360361362#[sc_tracing::logging::prefix_logs_with("Parachain")]363async fn start_node_impl<Runtime, RuntimeApi, ExecutorDispatch, BIQ, BIC>(364 parachain_config: Configuration,365 polkadot_config: Configuration,366 collator_options: CollatorOptions,367 id: ParaId,368 build_import_queue: BIQ,369 build_consensus: BIC,370 hwbench: Option<sc_sysinfo::HwBench>,371) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>372where373 sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<BlakeTwo256>,374 Runtime: RuntimeInstance + Send + Sync + 'static,375 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,376 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,377 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>378 + Send379 + Sync380 + 'static,381 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>382 + fp_rpc::EthereumRuntimeRPCApi<Block>383 + fp_rpc::ConvertTransactionRuntimeApi<Block>384 + sp_session::SessionKeys<Block>385 + sp_block_builder::BlockBuilder<Block>386 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>387 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>388 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>389 + app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>390 + up_pov_estimate_rpc::PovEstimateApi<Block>391 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>392 + sp_api::Metadata<Block>393 + sp_offchain::OffchainWorkerApi<Block>394 + cumulus_primitives_core::CollectCollationInfo<Block>,395 ExecutorDispatch: NativeExecutionDispatch + 'static,396 BIQ: FnOnce(397 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,398 Arc<FullBackend>,399 &Configuration,400 Option<TelemetryHandle>,401 &TaskManager,402 ) -> Result<403 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,404 sc_service::Error,405 >,406 BIC: FnOnce(407 Arc<FullClient<RuntimeApi, ExecutorDispatch>>,408 Arc<FullBackend>,409 Option<&Registry>,410 Option<TelemetryHandle>,411 &TaskManager,412 Arc<dyn RelayChainInterface>,413 Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>>,414 Arc<SyncingService<Block>>,415 KeystorePtr,416 bool,417 ) -> Result<Box<dyn ParachainConsensus<Block>>, sc_service::Error>,418{419 let parachain_config = prepare_node_config(parachain_config);420421 let params =422 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(¶chain_config, build_import_queue)?;423 let OtherPartial {424 mut telemetry,425 telemetry_worker_handle,426 eth_filter_pool,427 eth_backend,428 } = params.other;429 let net_config = sc_network::config::FullNetworkConfiguration::new(¶chain_config.network);430431 let client = params.client.clone();432 let backend = params.backend.clone();433 let mut task_manager = params.task_manager;434435 let (relay_chain_interface, collator_key) = build_relay_chain_interface(436 polkadot_config,437 ¶chain_config,438 telemetry_worker_handle,439 &mut task_manager,440 collator_options.clone(),441 hwbench.clone(),442 )443 .await444 .map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;445446 let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);447448 let force_authoring = parachain_config.force_authoring;449 let validator = parachain_config.role.is_authority();450 let prometheus_registry = parachain_config.prometheus_registry().cloned();451 let transaction_pool = params.transaction_pool.clone();452 let import_queue_service = params.import_queue.service();453454 let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =455 sc_service::build_network(sc_service::BuildNetworkParams {456 config: ¶chain_config,457 net_config,458 client: client.clone(),459 transaction_pool: transaction_pool.clone(),460 spawn_handle: task_manager.spawn_handle(),461 import_queue: params.import_queue,462 block_announce_validator_builder: Some(Box::new(|_| {463 Box::new(block_announce_validator)464 })),465 warp_sync_params: None,466 })?;467468 let select_chain = params.select_chain.clone();469470 let runtime_id = parachain_config.chain_spec.runtime_id();471472 473 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));474 let fee_history_limit = 2048;475476 let eth_pubsub_notification_sinks: Arc<477 EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,478 > = Default::default();479480 let overrides = overrides_handle(client.clone());481 let eth_block_data_cache = spawn_frontier_tasks(482 FrontierTaskParams {483 client: client.clone(),484 substrate_backend: backend.clone(),485 eth_filter_pool: eth_filter_pool.clone(),486 eth_backend: eth_backend.clone(),487 fee_history_limit,488 fee_history_cache: fee_history_cache.clone(),489 task_manager: &task_manager,490 prometheus_registry: prometheus_registry.clone(),491 overrides: overrides.clone(),492 sync_strategy: SyncStrategy::Parachain,493 },494 sync_service.clone(),495 eth_pubsub_notification_sinks.clone(),496 );497498 499 let rpc_builder = Box::new({500 clone!(501 client,502 backend,503 eth_backend,504 eth_pubsub_notification_sinks,505 fee_history_cache,506 eth_block_data_cache,507 overrides,508 transaction_pool,509 network,510 sync_service,511 );512 move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| {513 clone!(514 backend,515 eth_block_data_cache,516 client,517 eth_backend,518 eth_filter_pool,519 eth_pubsub_notification_sinks,520 fee_history_cache,521 eth_block_data_cache,522 network,523 runtime_id,524 transaction_pool,525 select_chain,526 overrides,527 );528529 #[cfg(not(feature = "pov-estimate"))]530 let _ = backend;531532 let mut rpc_handle = RpcModule::new(());533534 let full_deps = unique_rpc::FullDeps {535 client: client.clone(),536 runtime_id,537538 #[cfg(feature = "pov-estimate")]539 exec_params: uc_rpc::pov_estimate::ExecutorParams {540 wasm_method: parachain_config.wasm_method,541 default_heap_pages: parachain_config.default_heap_pages,542 max_runtime_instances: parachain_config.max_runtime_instances,543 runtime_cache_size: parachain_config.runtime_cache_size,544 },545546 #[cfg(feature = "pov-estimate")]547 backend,548549 deny_unsafe,550 pool: transaction_pool.clone(),551 select_chain,552 };553554 unique_rpc::create_full::<_, _, _, Runtime, RuntimeApi, _>(&mut rpc_handle, full_deps)?;555556 let eth_deps = unique_rpc::EthDeps {557 client,558 graph: transaction_pool.pool().clone(),559 pool: transaction_pool,560 is_authority: validator,561 network,562 eth_backend,563 564 max_past_logs: 10000,565 fee_history_limit,566 fee_history_cache,567 eth_block_data_cache,568 569 enable_dev_signer: false,570 eth_filter_pool,571 eth_pubsub_notification_sinks,572 overrides,573 sync: sync_service.clone(),574 };575576 unique_rpc::create_eth(577 &mut rpc_handle,578 eth_deps,579 subscription_task_executor.clone(),580 )?;581582 Ok(rpc_handle)583 }584 });585586 sc_service::spawn_tasks(sc_service::SpawnTasksParams {587 rpc_builder,588 client: client.clone(),589 transaction_pool: transaction_pool.clone(),590 task_manager: &mut task_manager,591 config: parachain_config,592 keystore: params.keystore_container.keystore(),593 backend: backend.clone(),594 network: network.clone(),595 sync_service: sync_service.clone(),596 system_rpc_tx,597 telemetry: telemetry.as_mut(),598 tx_handler_controller,599 })?;600601 if let Some(hwbench) = hwbench {602 sc_sysinfo::print_hwbench(&hwbench);603604 if let Some(ref mut telemetry) = telemetry {605 let telemetry_handle = telemetry.handle();606 task_manager.spawn_handle().spawn(607 "telemetry_hwbench",608 None,609 sc_sysinfo::initialize_hwbench_telemetry(telemetry_handle, hwbench),610 );611 }612 }613614 let announce_block = {615 let sync_service = sync_service.clone();616 Arc::new(Box::new(move |hash, data| {617 sync_service.announce_block(hash, data)618 }))619 };620621 let relay_chain_slot_duration = Duration::from_secs(6);622623 let overseer_handle = relay_chain_interface624 .overseer_handle()625 .map_err(|e| sc_service::Error::Application(Box::new(e)))?;626627 if validator {628 let parachain_consensus = build_consensus(629 client.clone(),630 backend.clone(),631 prometheus_registry.as_ref(),632 telemetry.as_ref().map(|t| t.handle()),633 &task_manager,634 relay_chain_interface.clone(),635 transaction_pool,636 sync_service.clone(),637 params.keystore_container.keystore(),638 force_authoring,639 )?;640641 let spawner = task_manager.spawn_handle();642643 let params = StartCollatorParams {644 para_id: id,645 block_status: client.clone(),646 announce_block,647 client: client.clone(),648 task_manager: &mut task_manager,649 spawner,650 parachain_consensus,651 import_queue: import_queue_service,652 collator_key: collator_key.expect("Command line arguments do not allow this. qed"),653 relay_chain_interface,654 relay_chain_slot_duration,655 recovery_handle: Box::new(overseer_handle),656 sync_service,657 };658659 start_collator(params).await?;660 } else {661 let params = StartFullNodeParams {662 client: client.clone(),663 announce_block,664 task_manager: &mut task_manager,665 para_id: id,666 import_queue: import_queue_service,667 relay_chain_interface,668 relay_chain_slot_duration,669 recovery_handle: Box::new(overseer_handle),670 sync_service,671 };672673 start_full_node(params)?;674 }675676 start_network.start_network();677678 Ok((task_manager, client))679}680681682pub fn parachain_build_import_queue<RuntimeApi, ExecutorDispatch>(683 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,684 backend: Arc<FullBackend>,685 config: &Configuration,686 telemetry: Option<TelemetryHandle>,687 task_manager: &TaskManager,688) -> Result<689 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,690 sc_service::Error,691>692where693 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>694 + Send695 + Sync696 + 'static,697 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>698 + sp_block_builder::BlockBuilder<Block>699 + sp_consensus_aura::AuraApi<Block, AuraId>700 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,701 ExecutorDispatch: NativeExecutionDispatch + 'static,702{703 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;704705 let block_import = ParachainBlockImport::new(client.clone(), backend);706707 cumulus_client_consensus_aura::import_queue::<708 sp_consensus_aura::sr25519::AuthorityPair,709 _,710 _,711 _,712 _,713 _,714 >(cumulus_client_consensus_aura::ImportQueueParams {715 block_import,716 client,717 create_inherent_data_providers: move |_, _| async move {718 let time = sp_timestamp::InherentDataProvider::from_system_time();719720 let slot =721 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(722 *time,723 slot_duration,724 );725726 Ok((slot, time))727 },728 registry: config.prometheus_registry(),729 spawner: &task_manager.spawn_essential_handle(),730 telemetry,731 })732 .map_err(Into::into)733}734735736pub async fn start_node<Runtime, RuntimeApi, ExecutorDispatch>(737 parachain_config: Configuration,738 polkadot_config: Configuration,739 collator_options: CollatorOptions,740 id: ParaId,741 hwbench: Option<sc_sysinfo::HwBench>,742) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, ExecutorDispatch>>)>743where744 Runtime: RuntimeInstance + Send + Sync + 'static,745 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,746 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,747 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>748 + Send749 + Sync750 + 'static,751 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>752 + fp_rpc::EthereumRuntimeRPCApi<Block>753 + fp_rpc::ConvertTransactionRuntimeApi<Block>754 + sp_session::SessionKeys<Block>755 + sp_block_builder::BlockBuilder<Block>756 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>757 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>758 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>759 + app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>760 + up_pov_estimate_rpc::PovEstimateApi<Block>761 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>762 + sp_api::Metadata<Block>763 + sp_offchain::OffchainWorkerApi<Block>764 + cumulus_primitives_core::CollectCollationInfo<Block>765 + sp_consensus_aura::AuraApi<Block, AuraId>,766 ExecutorDispatch: NativeExecutionDispatch + 'static,767{768 start_node_impl::<Runtime, RuntimeApi, ExecutorDispatch, _, _>(769 parachain_config,770 polkadot_config,771 collator_options,772 id,773 parachain_build_import_queue,774 |client,775 backend,776 prometheus_registry,777 telemetry,778 task_manager,779 relay_chain_interface,780 transaction_pool,781 sync_oracle,782 keystore,783 force_authoring| {784 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;785786 let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(787 task_manager.spawn_handle(),788 client.clone(),789 transaction_pool,790 prometheus_registry,791 telemetry.clone(),792 );793794 let block_import = ParachainBlockImport::new(client.clone(), backend);795796 Ok(AuraConsensus::build::<797 sp_consensus_aura::sr25519::AuthorityPair,798 _,799 _,800 _,801 _,802 _,803 _,804 >(BuildAuraConsensusParams {805 proposer_factory,806 create_inherent_data_providers: move |_, (relay_parent, validation_data)| {807 let relay_chain_interface = relay_chain_interface.clone();808 async move {809 let parachain_inherent =810 cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(811 relay_parent,812 &relay_chain_interface,813 &validation_data,814 id,815 ).await;816817 let time = sp_timestamp::InherentDataProvider::from_system_time();818819 let slot =820 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(821 *time,822 slot_duration,823 );824825 let parachain_inherent = parachain_inherent.ok_or_else(|| {826 Box::<dyn std::error::Error + Send + Sync>::from(827 "Failed to create parachain inherent",828 )829 })?;830 Ok((slot, time, parachain_inherent))831 }832 },833 block_import,834 para_client: client,835 backoff_authoring_blocks: Option::<()>::None,836 sync_oracle,837 keystore,838 force_authoring,839 slot_duration,840 841 block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32),842 telemetry,843 max_block_proposal_slot_portion: None,844 }))845 },846 hwbench,847 )848 .await849}850851fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(852 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,853 _: Arc<FullBackend>,854 config: &Configuration,855 _: Option<TelemetryHandle>,856 task_manager: &TaskManager,857) -> Result<858 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,859 sc_service::Error,860>861where862 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>863 + Send864 + Sync865 + 'static,866 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>867 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,868 ExecutorDispatch: NativeExecutionDispatch + 'static,869{870 Ok(sc_consensus_manual_seal::import_queue(871 Box::new(client),872 &task_manager.spawn_essential_handle(),873 config.prometheus_registry(),874 ))875}876877pub struct OtherPartial {878 pub telemetry: Option<Telemetry>,879 pub telemetry_worker_handle: Option<TelemetryWorkerHandle>,880 pub eth_filter_pool: Option<FilterPool>,881 pub eth_backend: Arc<fc_db::kv::Backend<Block>>,882}883884885886pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(887 config: Configuration,888 autoseal_interval: u64,889 autoseal_finalize_delay: Option<u64>,890 disable_autoseal_on_tx: bool,891) -> sc_service::error::Result<TaskManager>892where893 Runtime: RuntimeInstance + Send + Sync + 'static,894 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,895 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,896 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>897 + Send898 + Sync899 + 'static,900 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>901 + fp_rpc::EthereumRuntimeRPCApi<Block>902 + fp_rpc::ConvertTransactionRuntimeApi<Block>903 + sp_session::SessionKeys<Block>904 + sp_block_builder::BlockBuilder<Block>905 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>906 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>907 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>908 + app_promotion_rpc::AppPromotionApi<Block, BlockNumber, Runtime::CrossAccountId, AccountId>909 + up_pov_estimate_rpc::PovEstimateApi<Block>910 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>911 + sp_api::Metadata<Block>912 + sp_offchain::OffchainWorkerApi<Block>913 + cumulus_primitives_core::CollectCollationInfo<Block>914 + sp_consensus_aura::AuraApi<Block, AuraId>,915 ExecutorDispatch: NativeExecutionDispatch + 'static,916{917 use sc_consensus_manual_seal::{918 run_manual_seal, run_delayed_finalize, EngineCommand, ManualSealParams,919 DelayedFinalizeParams,920 };921 use fc_consensus::FrontierBlockImport;922923 let sc_service::PartialComponents {924 client,925 backend,926 mut task_manager,927 import_queue,928 keystore_container,929 select_chain: maybe_select_chain,930 transaction_pool,931 other:932 OtherPartial {933 telemetry,934 eth_filter_pool,935 eth_backend,936 telemetry_worker_handle: _,937 },938 } = new_partial::<RuntimeApi, ExecutorDispatch, _>(939 &config,940 dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,941 )?;942 let net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);943 let prometheus_registry = config.prometheus_registry().cloned();944945 let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =946 sc_service::build_network(sc_service::BuildNetworkParams {947 config: &config,948 net_config,949 client: client.clone(),950 transaction_pool: transaction_pool.clone(),951 spawn_handle: task_manager.spawn_handle(),952 import_queue,953 block_announce_validator_builder: None,954 warp_sync_params: None,955 })?;956957 if config.offchain_worker.enabled {958 sc_service::build_offchain_workers(959 &config,960 task_manager.spawn_handle(),961 client.clone(),962 network.clone(),963 );964 }965966 let collator = config.role.is_authority();967968 let select_chain = maybe_select_chain;969970 if collator {971 let block_import = FrontierBlockImport::new(client.clone(), client.clone());972973 let env = sc_basic_authorship::ProposerFactory::new(974 task_manager.spawn_handle(),975 client.clone(),976 transaction_pool.clone(),977 prometheus_registry.as_ref(),978 telemetry.as_ref().map(|x| x.handle()),979 );980981 let transactions_commands_stream: Box<982 dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,983 > = Box::new(984 transaction_pool985 .pool()986 .validated_pool()987 .import_notification_stream()988 .filter(move |_| futures::future::ready(!disable_autoseal_on_tx))989 .map(|_| EngineCommand::SealNewBlock {990 create_empty: true,991 finalize: false,992 parent_hash: None,993 sender: None,994 }),995 );996997 let autoseal_interval = Box::pin(AutosealInterval::new(&config, autoseal_interval));998999 let idle_commands_stream: Box<1000 dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,1001 > = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock {1002 create_empty: true,1003 finalize: false,1004 parent_hash: None,1005 sender: None,1006 }));10071008 let commands_stream = select(transactions_commands_stream, idle_commands_stream);10091010 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;1011 let client_set_aside_for_cidp = client.clone();10121013 if let Some(delay_sec) = autoseal_finalize_delay {1014 let spawn_handle = task_manager.spawn_handle();10151016 task_manager.spawn_essential_handle().spawn_blocking(1017 "finalization_task",1018 Some("block-authoring"),1019 run_delayed_finalize(DelayedFinalizeParams {1020 client: client.clone(),1021 delay_sec,1022 spawn_handle,1023 }),1024 );1025 }10261027 task_manager.spawn_essential_handle().spawn_blocking(1028 "authorship_task",1029 Some("block-authoring"),1030 run_manual_seal(ManualSealParams {1031 block_import,1032 env,1033 client: client.clone(),1034 pool: transaction_pool.clone(),1035 commands_stream,1036 select_chain: select_chain.clone(),1037 consensus_data_provider: None,1038 create_inherent_data_providers: move |block: Hash, ()| {1039 let current_para_block = client_set_aside_for_cidp1040 .number(block)1041 .expect("Header lookup should succeed")1042 .expect("Header passed in as parent should be present in backend.");10431044 let client_for_xcm = client_set_aside_for_cidp.clone();1045 async move {1046 let time = sp_timestamp::InherentDataProvider::from_system_time();10471048 let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {1049 current_para_block,1050 relay_offset: 1000,1051 relay_blocks_per_para_block: 2,1052 para_blocks_per_relay_epoch: 0,1053 xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(1054 &*client_for_xcm,1055 block,1056 Default::default(),1057 Default::default(),1058 ),1059 relay_randomness_config: (),1060 raw_downward_messages: vec![],1061 raw_horizontal_messages: vec![],1062 };10631064 let slot =1065 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(1066 *time,1067 slot_duration,1068 );10691070 Ok((time, slot, mocked_parachain))1071 }1072 },1073 }),1074 );1075 }10761077 #[cfg(feature = "pov-estimate")]1078 let rpc_backend = backend.clone();10791080 let runtime_id = config.chain_spec.runtime_id();10811082 1083 let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));1084 let fee_history_limit = 2048;10851086 let eth_pubsub_notification_sinks: Arc<1087 EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<Block>>,1088 > = Default::default();10891090 let overrides = overrides_handle(client.clone());1091 let eth_block_data_cache = spawn_frontier_tasks(1092 FrontierTaskParams {1093 client: client.clone(),1094 substrate_backend: backend.clone(),1095 eth_filter_pool: eth_filter_pool.clone(),1096 eth_backend: eth_backend.clone(),1097 fee_history_limit,1098 fee_history_cache: fee_history_cache.clone(),1099 task_manager: &task_manager,1100 prometheus_registry,1101 overrides: overrides.clone(),1102 sync_strategy: SyncStrategy::Normal,1103 },1104 sync_service.clone(),1105 eth_pubsub_notification_sinks.clone(),1106 );11071108 1109 let rpc_builder = Box::new({1110 clone!(1111 client,1112 backend,1113 eth_backend,1114 eth_pubsub_notification_sinks,1115 fee_history_cache,1116 eth_block_data_cache,1117 overrides,1118 transaction_pool,1119 network,1120 sync_service,1121 );1122 move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| {1123 clone!(1124 backend,1125 eth_block_data_cache,1126 client,1127 eth_backend,1128 eth_filter_pool,1129 eth_pubsub_notification_sinks,1130 fee_history_cache,1131 eth_block_data_cache,1132 network,1133 runtime_id,1134 transaction_pool,1135 select_chain,1136 overrides,1137 );11381139 #[cfg(not(feature = "pov-estimate"))]1140 let _ = backend;11411142 let mut rpc_module = RpcModule::new(());11431144 let full_deps = unique_rpc::FullDeps {1145 runtime_id,11461147 #[cfg(feature = "pov-estimate")]1148 exec_params: uc_rpc::pov_estimate::ExecutorParams {1149 wasm_method: config.wasm_method,1150 default_heap_pages: config.default_heap_pages,1151 max_runtime_instances: config.max_runtime_instances,1152 runtime_cache_size: config.runtime_cache_size,1153 },11541155 #[cfg(feature = "pov-estimate")]1156 backend,1157 1158 deny_unsafe,1159 client: client.clone(),1160 pool: transaction_pool.clone(),1161 select_chain,1162 };11631164 unique_rpc::create_full::<_, _, _, Runtime, RuntimeApi, _>(&mut rpc_module, full_deps)?;11651166 let eth_deps = unique_rpc::EthDeps {1167 client,1168 graph: transaction_pool.pool().clone(),1169 pool: transaction_pool,1170 is_authority: true,1171 network,1172 eth_backend,1173 1174 max_past_logs: 10000,1175 fee_history_limit,1176 fee_history_cache,1177 eth_block_data_cache,1178 1179 enable_dev_signer: false,1180 eth_filter_pool,1181 eth_pubsub_notification_sinks,1182 overrides,1183 sync: sync_service.clone(),1184 };11851186 unique_rpc::create_eth(1187 &mut rpc_module,1188 eth_deps,1189 subscription_task_executor.clone(),1190 )?;11911192 Ok(rpc_module)1193 }1194 });11951196 sc_service::spawn_tasks(sc_service::SpawnTasksParams {1197 network,1198 sync_service,1199 client,1200 keystore: keystore_container.keystore(),1201 task_manager: &mut task_manager,1202 transaction_pool,1203 rpc_builder,1204 backend,1205 system_rpc_tx,1206 config,1207 telemetry: None,1208 tx_handler_controller,1209 })?;12101211 network_starter.start_network();1212 Ok(task_manager)1213}12141215fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<OverrideHandle<Block>>1216where1217 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,1218 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,1219 C: Send + Sync + 'static,1220 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,1221 BE: Backend<Block> + 'static,1222 BE::State: StateBackend<BlakeTwo256>,1223{1224 let mut overrides_map = BTreeMap::new();1225 overrides_map.insert(1226 EthereumStorageSchema::V1,1227 Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1228 );1229 overrides_map.insert(1230 EthereumStorageSchema::V2,1231 Box::new(SchemaV2Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1232 );1233 overrides_map.insert(1234 EthereumStorageSchema::V3,1235 Box::new(SchemaV3Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,1236 );12371238 Arc::new(OverrideHandle {1239 schemas: overrides_map,1240 fallback: Box::new(RuntimeApiStorageOverride::new(client)),1241 })1242}12431244pub struct FrontierTaskParams<'a, B: BlockT, C, BE> {1245 pub task_manager: &'a TaskManager,1246 pub client: Arc<C>,1247 pub substrate_backend: Arc<BE>,1248 pub eth_backend: Arc<fc_db::kv::Backend<B>>,1249 pub eth_filter_pool: Option<FilterPool>,1250 pub overrides: Arc<OverrideHandle<B>>,1251 pub fee_history_limit: u64,1252 pub fee_history_cache: FeeHistoryCache,1253 pub sync_strategy: SyncStrategy,1254 pub prometheus_registry: Option<Registry>,1255}12561257pub fn spawn_frontier_tasks<B, C, BE>(1258 params: FrontierTaskParams<B, C, BE>,1259 sync: Arc<SyncingService<B>>,1260 pubsub_notification_sinks: Arc<1261 EthereumBlockNotificationSinks<fc_mapping_sync::EthereumBlockNotification<B>>,1262 >,1263) -> Arc<EthBlockDataCacheTask<B>>1264where1265 C: ProvideRuntimeApi<B> + BlockOf,1266 C: HeaderBackend<B> + HeaderMetadata<B, Error = BlockChainError> + 'static,1267 C: BlockchainEvents<B> + StorageProvider<B, BE>,1268 C: Send + Sync + 'static,1269 C::Api: EthereumRuntimeRPCApi<B>,1270 C::Api: BlockBuilder<B>,1271 B: BlockT<Hash = H256> + Send + Sync + 'static,1272 B::Header: HeaderT<Number = u32>,1273 BE: Backend<B> + 'static,1274 BE::State: StateBackend<BlakeTwo256>,1275{1276 let FrontierTaskParams {1277 task_manager,1278 client,1279 substrate_backend,1280 eth_backend,1281 eth_filter_pool,1282 overrides,1283 fee_history_limit,1284 fee_history_cache,1285 sync_strategy,1286 prometheus_registry,1287 } = params;1288 1289 1290 params.task_manager.spawn_essential_handle().spawn(1291 "frontier-mapping-sync-worker",1292 Some("frontier"),1293 MappingSyncWorker::new(1294 client.import_notification_stream(),1295 Duration::new(6, 0),1296 client.clone(),1297 substrate_backend,1298 overrides.clone(),1299 eth_backend,1300 3,1301 0,1302 sync_strategy,1303 sync,1304 pubsub_notification_sinks,1305 )1306 .for_each(|()| futures::future::ready(())),1307 );13081309 1310 1311 if let Some(eth_filter_pool) = eth_filter_pool {1312 1313 const FILTER_RETAIN_THRESHOLD: u64 = 100;1314 params.task_manager.spawn_essential_handle().spawn(1315 "frontier-filter-pool",1316 Some("frontier"),1317 EthTask::filter_pool_task(client.clone(), eth_filter_pool, FILTER_RETAIN_THRESHOLD),1318 );1319 }13201321 1322 params.task_manager.spawn_essential_handle().spawn(1323 "frontier-fee-history",1324 Some("frontier"),1325 EthTask::fee_history_task(1326 client,1327 overrides.clone(),1328 fee_history_cache,1329 fee_history_limit,1330 ),1331 );13321333 Arc::new(EthBlockDataCacheTask::new(1334 task_manager.spawn_handle(),1335 overrides,1336 50,1337 50,1338 prometheus_registry,1339 ))1340}