// Copyright 2020-2021 Parity Technologies (UK) Ltd. // This file is part of Cumulus. // Substrate is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Substrate is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . //! Cumulus service //! //! Provides functions for starting a collator node or a normal full node. use cumulus_client_cli::CollatorOptions; use cumulus_client_consensus_common::ParachainConsensus; use cumulus_primitives_core::{CollectCollationInfo, ParaId}; use cumulus_relay_chain_interface::RelayChainInterface; use polkadot_primitives::v1::CollatorPair; use sc_client_api::{ Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, UsageProvider, }; use sc_consensus::{ import_queue::{ImportQueue, IncomingBlock, Link, Origin}, BlockImport, }; use sc_service::{Configuration, TaskManager}; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_consensus::BlockOrigin; use sp_core::traits::SpawnNamed; use sp_runtime::{ traits::{Block as BlockT, NumberFor}, Justifications, }; use std::{sync::Arc, time::Duration}; pub mod genesis; /// Parameters given to [`start_collator`]. pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, RCInterface, Spawner, IQ> { pub block_status: Arc, pub client: Arc, pub announce_block: Arc>) + Send + Sync>, pub spawner: Spawner, pub para_id: ParaId, pub relay_chain_interface: RCInterface, pub task_manager: &'a mut TaskManager, pub parachain_consensus: Box>, pub import_queue: IQ, pub collator_key: CollatorPair, pub relay_chain_slot_duration: Duration, } /// Start a collator node for a parachain. /// /// A collator is similar to a validator in a normal blockchain. /// It is responsible for producing blocks and sending the blocks to a /// parachain validator for validation and inclusion into the relay chain. pub async fn start_collator<'a, Block, BS, Client, Backend, RCInterface, Spawner, IQ>( StartCollatorParams { block_status, client, announce_block, spawner, para_id, task_manager, relay_chain_interface, parachain_consensus, import_queue, collator_key, relay_chain_slot_duration, }: StartCollatorParams<'a, Block, BS, Client, RCInterface, Spawner, IQ>, ) -> sc_service::error::Result<()> where Block: BlockT, BS: BlockBackend + Send + Sync + 'static, Client: Finalizer + UsageProvider + HeaderBackend + Send + Sync + BlockBackend + BlockchainEvents + ProvideRuntimeApi + 'static, Client::Api: CollectCollationInfo, for<'b> &'b Client: BlockImport, Spawner: SpawnNamed + Clone + Send + Sync + 'static, RCInterface: RelayChainInterface + Clone + 'static, Backend: BackendT + 'static, IQ: ImportQueue + 'static, { let consensus = cumulus_client_consensus_common::run_parachain_consensus( para_id, client.clone(), relay_chain_interface.clone(), announce_block.clone(), ); task_manager .spawn_essential_handle() .spawn("cumulus-consensus", None, consensus); let overseer_handle = relay_chain_interface .overseer_handle() .map_err(|e| sc_service::Error::Application(Box::new(e)))? .ok_or_else(|| "Polkadot full node did not provide an `OverseerHandle`!")?; let pov_recovery = cumulus_client_pov_recovery::PoVRecovery::new( overseer_handle.clone(), // We want that collators wait at maximum the relay chain slot duration before starting // to recover blocks. cumulus_client_pov_recovery::RecoveryDelay::WithMax { max: relay_chain_slot_duration }, client.clone(), import_queue, relay_chain_interface.clone(), para_id, ); task_manager .spawn_essential_handle() .spawn("cumulus-pov-recovery", None, pov_recovery.run()); cumulus_client_collator::start_collator(cumulus_client_collator::StartCollatorParams { runtime_api: client.clone(), block_status, announce_block, overseer_handle, spawner, para_id, key: collator_key, parachain_consensus, }) .await; Ok(()) } /// Parameters given to [`start_full_node`]. pub struct StartFullNodeParams<'a, Block: BlockT, Client, RCInterface, IQ> { pub para_id: ParaId, pub client: Arc, pub relay_chain_interface: RCInterface, pub task_manager: &'a mut TaskManager, pub announce_block: Arc>) + Send + Sync>, pub relay_chain_slot_duration: Duration, pub import_queue: IQ, pub collator_options: CollatorOptions, } /// Start a full node for a parachain. /// /// A full node will only sync the given parachain and will follow the /// tip of the chain. pub fn start_full_node( StartFullNodeParams { client, announce_block, task_manager, relay_chain_interface, para_id, relay_chain_slot_duration, import_queue, collator_options, }: StartFullNodeParams, ) -> sc_service::error::Result<()> where Block: BlockT, Client: Finalizer + UsageProvider + Send + Sync + BlockBackend + BlockchainEvents + 'static, for<'a> &'a Client: BlockImport, Backend: BackendT + 'static, RCInterface: RelayChainInterface + Clone + 'static, IQ: ImportQueue + 'static, { let consensus = cumulus_client_consensus_common::run_parachain_consensus( para_id, client.clone(), relay_chain_interface.clone(), announce_block, ); task_manager .spawn_essential_handle() .spawn("cumulus-consensus", None, consensus); // PoV Recovery is currently not supported when we connect to the // relay chain via RPC, so we return early. The node will work, but not be able to recover PoVs from the // relay chain if blocks are not announced on parachain. This will be enabled again once // https://github.com/paritytech/cumulus/issues/545 is finished. if collator_options.relay_chain_rpc_url.is_some() { return Ok(()) } let overseer_handle = relay_chain_interface .overseer_handle() .map_err(|e| sc_service::Error::Application(Box::new(e)))? .ok_or_else(|| "Polkadot full node did not provide an `OverseerHandle`!")?; let pov_recovery = cumulus_client_pov_recovery::PoVRecovery::new( overseer_handle, // Full nodes should at least wait 2.5 minutes (assuming 6 seconds slot duration) and // in maximum 5 minutes before starting to recover blocks. Collators should already start // the recovery way before full nodes try to recover a certain block and then share the // block with the network using "the normal way". Full nodes are just the "last resort" // for block recovery. cumulus_client_pov_recovery::RecoveryDelay::WithMinAndMax { min: relay_chain_slot_duration * 25, max: relay_chain_slot_duration * 50, }, client.clone(), import_queue, relay_chain_interface.clone(), para_id, ); task_manager .spawn_essential_handle() .spawn("cumulus-pov-recovery", None, pov_recovery.run()); Ok(()) } /// Prepare the parachain's node configuration /// /// This function will disable the default announcement of Substrate for the parachain in favor /// of the one of Cumulus. pub fn prepare_node_config(mut parachain_config: Configuration) -> Configuration { parachain_config.announce_block = false; parachain_config } /// A shared import queue /// /// This is basically a hack until the Substrate side is implemented properly. #[derive(Clone)] pub struct SharedImportQueue(Arc>>); impl SharedImportQueue { /// Create a new instance of the shared import queue. pub fn new + 'static>(import_queue: IQ) -> Self { Self(Arc::new(parking_lot::Mutex::new(import_queue))) } } impl ImportQueue for SharedImportQueue { fn import_blocks(&mut self, origin: BlockOrigin, blocks: Vec>) { self.0.lock().import_blocks(origin, blocks) } fn import_justifications( &mut self, who: Origin, hash: Block::Hash, number: NumberFor, justifications: Justifications, ) { self.0.lock().import_justifications(who, hash, number, justifications) } fn poll_actions(&mut self, cx: &mut std::task::Context, link: &mut dyn Link) { self.0.lock().poll_actions(cx, link) } }