use cumulus_client_cli::CollatorOptions;
use cumulus_client_consensus_common::ParachainConsensus;
use cumulus_client_network::{AssumeSybilResistance, RequireSecondedInBlockAnnounce};
use cumulus_client_pov_recovery::{PoVRecovery, RecoveryDelayRange, RecoveryHandle};
use cumulus_primitives_core::{CollectCollationInfo, ParaId};
use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;
use cumulus_relay_chain_interface::{RelayChainInterface, RelayChainResult};
use cumulus_relay_chain_minimal_node::{
build_minimal_relay_chain_node_light_client, build_minimal_relay_chain_node_with_rpc,
};
use futures::{
channel::{mpsc, oneshot},
FutureExt, StreamExt,
};
use polkadot_primitives::{CollatorPair, OccupiedCoreAssumption};
use sc_client_api::{
Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, ProofProvider, UsageProvider,
};
use sc_consensus::{
import_queue::{ImportQueue, ImportQueueService},
BlockImport,
};
use sc_network::{config::SyncMode, NetworkService};
use sc_network_sync::SyncingService;
use sc_network_transactions::TransactionsHandlerController;
use sc_service::{Configuration, NetworkStarter, SpawnTaskHandle, TaskManager, WarpSyncParams};
use sc_telemetry::{log, TelemetryWorkerHandle};
use sc_utils::mpsc::TracingUnboundedSender;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{HeaderBackend, HeaderMetadata};
use sp_core::{traits::SpawnNamed, Decode};
use sp_runtime::traits::{Block as BlockT, BlockIdTo};
use std::{sync::Arc, time::Duration};
const RECOVERY_CHAN_SIZE: usize = 8;
const LOG_TARGET_SYNC: &str = "sync::cumulus";
pub enum DARecoveryProfile {
Collator,
FullNode,
Other(RecoveryDelayRange),
}
pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, RCInterface, Spawner> {
pub block_status: Arc<BS>,
pub client: Arc<Client>,
pub announce_block: Arc<dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync>,
pub spawner: Spawner,
pub para_id: ParaId,
pub relay_chain_interface: RCInterface,
pub task_manager: &'a mut TaskManager,
pub parachain_consensus: Box<dyn ParachainConsensus<Block>>,
pub import_queue: Box<dyn ImportQueueService<Block>>,
pub collator_key: CollatorPair,
pub relay_chain_slot_duration: Duration,
pub recovery_handle: Box<dyn RecoveryHandle>,
pub sync_service: Arc<SyncingService<Block>>,
}
pub struct StartRelayChainTasksParams<'a, Block: BlockT, Client, RCInterface> {
pub client: Arc<Client>,
pub announce_block: Arc<dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync>,
pub para_id: ParaId,
pub relay_chain_interface: RCInterface,
pub task_manager: &'a mut TaskManager,
pub da_recovery_profile: DARecoveryProfile,
pub import_queue: Box<dyn ImportQueueService<Block>>,
pub relay_chain_slot_duration: Duration,
pub recovery_handle: Box<dyn RecoveryHandle>,
pub sync_service: Arc<SyncingService<Block>>,
}
pub struct StartFullNodeParams<'a, Block: BlockT, Client, RCInterface> {
pub para_id: ParaId,
pub client: Arc<Client>,
pub relay_chain_interface: RCInterface,
pub task_manager: &'a mut TaskManager,
pub announce_block: Arc<dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync>,
pub relay_chain_slot_duration: Duration,
pub import_queue: Box<dyn ImportQueueService<Block>>,
pub recovery_handle: Box<dyn RecoveryHandle>,
pub sync_service: Arc<SyncingService<Block>>,
}
#[deprecated = "use start_relay_chain_tasks instead"]
pub async fn start_collator<'a, Block, BS, Client, Backend, RCInterface, Spawner>(
StartCollatorParams {
block_status,
client,
announce_block,
spawner,
para_id,
task_manager,
relay_chain_interface,
parachain_consensus,
import_queue,
collator_key,
relay_chain_slot_duration,
recovery_handle,
sync_service,
}: StartCollatorParams<'a, Block, BS, Client, RCInterface, Spawner>,
) -> sc_service::error::Result<()>
where
Block: BlockT,
BS: BlockBackend<Block> + Send + Sync + 'static,
Client: Finalizer<Block, Backend>
+ UsageProvider<Block>
+ HeaderBackend<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ BlockchainEvents<Block>
+ ProvideRuntimeApi<Block>
+ 'static,
Client::Api: CollectCollationInfo<Block>,
for<'b> &'b Client: BlockImport<Block>,
Spawner: SpawnNamed + Clone + Send + Sync + 'static,
RCInterface: RelayChainInterface + Clone + 'static,
Backend: BackendT<Block> + 'static,
{
let overseer_handle = relay_chain_interface
.overseer_handle()
.map_err(|e| sc_service::Error::Application(Box::new(e)))?;
start_relay_chain_tasks(StartRelayChainTasksParams {
client: client.clone(),
announce_block: announce_block.clone(),
para_id,
task_manager,
da_recovery_profile: DARecoveryProfile::Collator,
relay_chain_interface,
import_queue,
relay_chain_slot_duration,
recovery_handle,
sync_service,
})?;
#[allow(deprecated)]
cumulus_client_collator::start_collator(cumulus_client_collator::StartCollatorParams {
runtime_api: client,
block_status,
announce_block,
overseer_handle,
spawner,
para_id,
key: collator_key,
parachain_consensus,
})
.await;
Ok(())
}
pub fn start_relay_chain_tasks<Block, Client, Backend, RCInterface>(
StartRelayChainTasksParams {
client,
announce_block,
para_id,
task_manager,
da_recovery_profile,
relay_chain_interface,
import_queue,
relay_chain_slot_duration,
recovery_handle,
sync_service,
}: StartRelayChainTasksParams<Block, Client, RCInterface>,
) -> sc_service::error::Result<()>
where
Block: BlockT,
Client: Finalizer<Block, Backend>
+ UsageProvider<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ BlockchainEvents<Block>
+ 'static,
for<'a> &'a Client: BlockImport<Block>,
Backend: BackendT<Block> + 'static,
RCInterface: RelayChainInterface + Clone + 'static,
{
let (recovery_chan_tx, recovery_chan_rx) = mpsc::channel(RECOVERY_CHAN_SIZE);
let consensus = cumulus_client_consensus_common::run_parachain_consensus(
para_id,
client.clone(),
relay_chain_interface.clone(),
announce_block.clone(),
Some(recovery_chan_tx),
);
task_manager
.spawn_essential_handle()
.spawn_blocking("cumulus-consensus", None, consensus);
let da_recovery_profile = match da_recovery_profile {
DARecoveryProfile::Collator => {
RecoveryDelayRange {
min: relay_chain_slot_duration / 2,
max: relay_chain_slot_duration,
}
},
DARecoveryProfile::FullNode => {
RecoveryDelayRange {
min: relay_chain_slot_duration * 25,
max: relay_chain_slot_duration * 50,
}
},
DARecoveryProfile::Other(profile) => profile,
};
let pov_recovery = PoVRecovery::new(
recovery_handle,
da_recovery_profile,
client.clone(),
import_queue,
relay_chain_interface.clone(),
para_id,
recovery_chan_rx,
sync_service,
);
task_manager
.spawn_essential_handle()
.spawn("cumulus-pov-recovery", None, pov_recovery.run());
Ok(())
}
#[deprecated = "use start_relay_chain_tasks instead"]
pub fn start_full_node<Block, Client, Backend, RCInterface>(
StartFullNodeParams {
client,
announce_block,
task_manager,
relay_chain_interface,
para_id,
relay_chain_slot_duration,
import_queue,
recovery_handle,
sync_service,
}: StartFullNodeParams<Block, Client, RCInterface>,
) -> sc_service::error::Result<()>
where
Block: BlockT,
Client: Finalizer<Block, Backend>
+ UsageProvider<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ BlockchainEvents<Block>
+ 'static,
for<'a> &'a Client: BlockImport<Block>,
Backend: BackendT<Block> + 'static,
RCInterface: RelayChainInterface + Clone + 'static,
{
start_relay_chain_tasks(StartRelayChainTasksParams {
client,
announce_block,
task_manager,
relay_chain_interface,
para_id,
relay_chain_slot_duration,
import_queue,
recovery_handle,
sync_service,
da_recovery_profile: DARecoveryProfile::FullNode,
})
}
#[deprecated = "This is old consensus architecture only for backwards compatibility \
and will be removed in the future"]
pub mod old_consensus {
#[allow(deprecated)]
pub use cumulus_client_collator::{start_collator, start_collator_sync, StartCollatorParams};
}
pub fn prepare_node_config(mut parachain_config: Configuration) -> Configuration {
parachain_config.announce_block = false;
parachain_config
}
pub async fn build_relay_chain_interface(
relay_chain_config: Configuration,
parachain_config: &Configuration,
telemetry_worker_handle: Option<TelemetryWorkerHandle>,
task_manager: &mut TaskManager,
collator_options: CollatorOptions,
hwbench: Option<sc_sysinfo::HwBench>,
) -> RelayChainResult<(Arc<(dyn RelayChainInterface + 'static)>, Option<CollatorPair>)> {
match collator_options.relay_chain_mode {
cumulus_client_cli::RelayChainMode::Embedded => build_inprocess_relay_chain(
relay_chain_config,
parachain_config,
telemetry_worker_handle,
task_manager,
hwbench,
),
cumulus_client_cli::RelayChainMode::ExternalRpc(rpc_target_urls) =>
build_minimal_relay_chain_node_with_rpc(
relay_chain_config,
task_manager,
rpc_target_urls,
)
.await,
cumulus_client_cli::RelayChainMode::LightClient =>
build_minimal_relay_chain_node_light_client(relay_chain_config, task_manager).await,
}
}
pub enum CollatorSybilResistance {
Resistant,
Unresistant,
}
pub struct BuildNetworkParams<
'a,
Block: BlockT,
Client: ProvideRuntimeApi<Block>
+ BlockBackend<Block>
+ HeaderMetadata<Block, Error = sp_blockchain::Error>
+ HeaderBackend<Block>
+ BlockIdTo<Block>
+ 'static,
RCInterface,
IQ,
> where
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
{
pub parachain_config: &'a Configuration,
pub net_config: sc_network::config::FullNetworkConfiguration,
pub client: Arc<Client>,
pub transaction_pool: Arc<sc_transaction_pool::FullPool<Block, Client>>,
pub para_id: ParaId,
pub relay_chain_interface: RCInterface,
pub spawn_handle: SpawnTaskHandle,
pub import_queue: IQ,
pub sybil_resistance_level: CollatorSybilResistance,
}
pub async fn build_network<'a, Block, Client, RCInterface, IQ>(
BuildNetworkParams {
parachain_config,
net_config,
client,
transaction_pool,
para_id,
spawn_handle,
relay_chain_interface,
import_queue,
sybil_resistance_level,
}: BuildNetworkParams<'a, Block, Client, RCInterface, IQ>,
) -> sc_service::error::Result<(
Arc<NetworkService<Block, Block::Hash>>,
TracingUnboundedSender<sc_rpc::system::Request<Block>>,
TransactionsHandlerController<Block::Hash>,
NetworkStarter,
Arc<SyncingService<Block>>,
)>
where
Block: BlockT,
Client: UsageProvider<Block>
+ HeaderBackend<Block>
+ sp_consensus::block_validation::Chain<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ BlockchainEvents<Block>
+ ProvideRuntimeApi<Block>
+ HeaderMetadata<Block, Error = sp_blockchain::Error>
+ BlockIdTo<Block, Error = sp_blockchain::Error>
+ ProofProvider<Block>
+ 'static,
Client::Api: CollectCollationInfo<Block>
+ sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
for<'b> &'b Client: BlockImport<Block>,
RCInterface: RelayChainInterface + Clone + 'static,
IQ: ImportQueue<Block> + 'static,
{
let warp_sync_params = match parachain_config.network.sync_mode {
SyncMode::Warp => {
let target_block = warp_sync_get::<Block, RCInterface>(
para_id,
relay_chain_interface.clone(),
spawn_handle.clone(),
);
Some(WarpSyncParams::WaitForTarget(target_block))
},
_ => None,
};
let block_announce_validator = match sybil_resistance_level {
CollatorSybilResistance::Resistant => {
let block_announce_validator = AssumeSybilResistance::allow_seconded_messages();
Box::new(block_announce_validator) as Box<_>
},
CollatorSybilResistance::Unresistant => {
let block_announce_validator =
RequireSecondedInBlockAnnounce::new(relay_chain_interface, para_id);
Box::new(block_announce_validator) as Box<_>
},
};
sc_service::build_network(sc_service::BuildNetworkParams {
config: parachain_config,
net_config,
client,
transaction_pool,
spawn_handle,
import_queue,
block_announce_validator_builder: Some(Box::new(move |_| block_announce_validator)),
warp_sync_params,
block_relay: None,
})
}
fn warp_sync_get<B, RCInterface>(
para_id: ParaId,
relay_chain_interface: RCInterface,
spawner: SpawnTaskHandle,
) -> oneshot::Receiver<<B as BlockT>::Header>
where
B: BlockT + 'static,
RCInterface: RelayChainInterface + 'static,
{
let (sender, receiver) = oneshot::channel::<B::Header>();
spawner.spawn(
"cumulus-parachain-wait-for-target-block",
None,
async move {
log::debug!(
target: "cumulus-network",
"waiting for announce block in a background task...",
);
let _ = wait_for_target_block::<B, _>(sender, para_id, relay_chain_interface)
.await
.map_err(|e| {
log::error!(
target: LOG_TARGET_SYNC,
"Unable to determine parachain target block {:?}",
e
)
});
}
.boxed(),
);
receiver
}
async fn wait_for_target_block<B, RCInterface>(
sender: oneshot::Sender<<B as BlockT>::Header>,
para_id: ParaId,
relay_chain_interface: RCInterface,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
B: BlockT + 'static,
RCInterface: RelayChainInterface + Send + 'static,
{
let mut imported_blocks = relay_chain_interface.import_notification_stream().await?.fuse();
while imported_blocks.next().await.is_some() {
let is_syncing = relay_chain_interface.is_major_syncing().await.map_err(|e| {
Box::<dyn std::error::Error + Send + Sync>::from(format!(
"Unable to determine sync status. {e}"
))
})?;
if !is_syncing {
let relay_chain_best_hash = relay_chain_interface
.finalized_block_hash()
.await
.map_err(|e| Box::new(e) as Box<_>)?;
let validation_data = relay_chain_interface
.persisted_validation_data(
relay_chain_best_hash,
para_id,
OccupiedCoreAssumption::TimedOut,
)
.await
.map_err(|e| format!("{e:?}"))?
.ok_or("Could not find parachain head in relay chain")?;
let target_block = B::Header::decode(&mut &validation_data.parent_head.0[..])
.map_err(|e| format!("Failed to decode parachain head: {e}"))?;
log::debug!(target: LOG_TARGET_SYNC, "Target block reached {:?}", target_block);
let _ = sender.send(target_block);
return Ok(())
}
}
Err("Stopping following imported blocks. Could not determine parachain target block".into())
}