mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 08:11:04 +00:00
Parachain Consensus abstractions (#329)
* Move consensus to consensus-common crate * Move the parachain consensus out of the collator * Add first relay chain consensus stuff * Remove some warnings * Fix more stuff * Fix collator test * Change `ParachainConsensus` to take a mutable self * Make everything compile * Feedback
This commit is contained in:
+32
-129
@@ -18,53 +18,38 @@
|
||||
//!
|
||||
//! Provides functions for starting a collator node or a normal full node.
|
||||
|
||||
use cumulus_client_consensus_common::ParachainConsensus;
|
||||
use cumulus_primitives_core::ParaId;
|
||||
use futures::{Future, FutureExt};
|
||||
use polkadot_overseer::OverseerHandler;
|
||||
use futures::FutureExt;
|
||||
use polkadot_primitives::v1::{Block as PBlock, CollatorId, CollatorPair};
|
||||
use polkadot_service::{AbstractClient, Client as PClient, ClientHandle, RuntimeApiCollection};
|
||||
use sc_client_api::{
|
||||
Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, StateBackend, UsageProvider,
|
||||
Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, UsageProvider,
|
||||
};
|
||||
use sc_service::{error::Result as ServiceResult, Configuration, Role, TaskManager};
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_consensus::{BlockImport, Environment, Error as ConsensusError, Proposer};
|
||||
use sp_consensus::BlockImport;
|
||||
use sp_core::traits::SpawnNamed;
|
||||
use sp_inherents::InherentDataProviders;
|
||||
use sp_runtime::traits::{BlakeTwo256, Block as BlockT};
|
||||
use std::{marker::PhantomData, sync::Arc};
|
||||
|
||||
pub mod genesis;
|
||||
|
||||
/// Polkadot full node handles.
|
||||
type PFullNode<C> = polkadot_service::NewFull<C>;
|
||||
/// Relay chain full node handles.
|
||||
type RFullNode<C> = polkadot_service::NewFull<C>;
|
||||
|
||||
/// Parameters given to [`start_collator`].
|
||||
pub struct StartCollatorParams<
|
||||
'a,
|
||||
Block: BlockT,
|
||||
PF,
|
||||
BI,
|
||||
BS,
|
||||
Client,
|
||||
Backend,
|
||||
Spawner,
|
||||
PClient,
|
||||
PBackend,
|
||||
> {
|
||||
pub proposer_factory: PF,
|
||||
pub inherent_data_providers: InherentDataProviders,
|
||||
pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, Backend, Spawner, RClient> {
|
||||
pub backend: Arc<Backend>,
|
||||
pub block_import: BI,
|
||||
pub block_status: Arc<BS>,
|
||||
pub client: Arc<Client>,
|
||||
pub announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
|
||||
pub spawner: Spawner,
|
||||
pub para_id: ParaId,
|
||||
pub collator_key: CollatorPair,
|
||||
pub polkadot_full_node: PFullNode<PClient>,
|
||||
pub relay_chain_full_node: RFullNode<RClient>,
|
||||
pub task_manager: &'a mut TaskManager,
|
||||
pub polkadot_backend: Arc<PBackend>,
|
||||
pub parachain_consensus: Box<dyn ParachainConsensus<Block>>,
|
||||
}
|
||||
|
||||
/// Start a collator node for a parachain.
|
||||
@@ -72,33 +57,22 @@ pub struct StartCollatorParams<
|
||||
/// 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, PF, BI, BS, Client, Backend, Spawner, PClient, PBackend>(
|
||||
pub async fn start_collator<'a, Block, BS, Client, Backend, Spawner, RClient>(
|
||||
StartCollatorParams {
|
||||
proposer_factory,
|
||||
inherent_data_providers,
|
||||
backend,
|
||||
block_import,
|
||||
block_status,
|
||||
client,
|
||||
announce_block,
|
||||
spawner,
|
||||
para_id,
|
||||
collator_key,
|
||||
polkadot_full_node,
|
||||
task_manager,
|
||||
polkadot_backend,
|
||||
}: StartCollatorParams<'a, Block, PF, BI, BS, Client, Backend, Spawner, PClient, PBackend>,
|
||||
relay_chain_full_node,
|
||||
parachain_consensus,
|
||||
}: StartCollatorParams<'a, Block, BS, Client, Backend, Spawner, RClient>,
|
||||
) -> sc_service::error::Result<()>
|
||||
where
|
||||
Block: BlockT,
|
||||
PF: Environment<Block> + Send + 'static,
|
||||
BI: BlockImport<
|
||||
Block,
|
||||
Error = ConsensusError,
|
||||
Transaction = <PF::Proposer as Proposer<Block>>::Transaction,
|
||||
> + Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
BS: BlockBackend<Block> + Send + Sync + 'static,
|
||||
Client: Finalizer<Block, Backend>
|
||||
+ UsageProvider<Block>
|
||||
@@ -111,11 +85,9 @@ where
|
||||
for<'b> &'b Client: BlockImport<Block>,
|
||||
Backend: BackendT<Block> + 'static,
|
||||
Spawner: SpawnNamed + Clone + Send + Sync + 'static,
|
||||
PClient: ClientHandle,
|
||||
PBackend: BackendT<PBlock> + 'static,
|
||||
PBackend::State: StateBackend<BlakeTwo256>,
|
||||
RClient: ClientHandle,
|
||||
{
|
||||
polkadot_full_node.client.execute_with(StartConsensus {
|
||||
relay_chain_full_node.client.execute_with(StartConsensus {
|
||||
para_id,
|
||||
announce_block: announce_block.clone(),
|
||||
client: client.clone(),
|
||||
@@ -123,99 +95,30 @@ where
|
||||
_phantom: PhantomData,
|
||||
})?;
|
||||
|
||||
polkadot_full_node
|
||||
.client
|
||||
.execute_with(StartCollator {
|
||||
proposer_factory,
|
||||
inherent_data_providers,
|
||||
backend,
|
||||
announce_block,
|
||||
overseer_handler: polkadot_full_node
|
||||
.overseer_handler
|
||||
.ok_or_else(|| "Polkadot full node did not provided an `OverseerHandler`!")?,
|
||||
spawner,
|
||||
para_id,
|
||||
collator_key,
|
||||
block_import,
|
||||
block_status,
|
||||
polkadot_backend,
|
||||
})
|
||||
.await?;
|
||||
cumulus_client_collator::start_collator(cumulus_client_collator::StartCollatorParams {
|
||||
backend,
|
||||
block_status,
|
||||
announce_block,
|
||||
overseer_handler: relay_chain_full_node
|
||||
.overseer_handler
|
||||
.ok_or_else(|| "Polkadot full node did not provided an `OverseerHandler`!")?,
|
||||
spawner,
|
||||
para_id,
|
||||
key: collator_key,
|
||||
parachain_consensus,
|
||||
})
|
||||
.await;
|
||||
|
||||
task_manager.add_child(polkadot_full_node.task_manager);
|
||||
task_manager.add_child(relay_chain_full_node.task_manager);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct StartCollator<Block: BlockT, Backend, PF, BI, BS, Spawner, PBackend> {
|
||||
proposer_factory: PF,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
backend: Arc<Backend>,
|
||||
block_import: BI,
|
||||
block_status: Arc<BS>,
|
||||
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
|
||||
overseer_handler: OverseerHandler,
|
||||
spawner: Spawner,
|
||||
para_id: ParaId,
|
||||
collator_key: CollatorPair,
|
||||
polkadot_backend: Arc<PBackend>,
|
||||
}
|
||||
|
||||
impl<Block, Backend, PF, BI, BS, Spawner, PBackend2> polkadot_service::ExecuteWithClient
|
||||
for StartCollator<Block, Backend, PF, BI, BS, Spawner, PBackend2>
|
||||
where
|
||||
Block: BlockT,
|
||||
PF: Environment<Block> + Send + 'static,
|
||||
BI: BlockImport<
|
||||
Block,
|
||||
Error = ConsensusError,
|
||||
Transaction = <PF::Proposer as Proposer<Block>>::Transaction,
|
||||
> + Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
BS: BlockBackend<Block> + Send + Sync + 'static,
|
||||
Backend: BackendT<Block> + 'static,
|
||||
Spawner: SpawnNamed + Clone + Send + Sync + 'static,
|
||||
PBackend2: sc_client_api::Backend<PBlock> + 'static,
|
||||
PBackend2::State: sp_api::StateBackend<BlakeTwo256>,
|
||||
{
|
||||
type Output = std::pin::Pin<Box<dyn Future<Output = ServiceResult<()>>>>;
|
||||
|
||||
fn execute_with_client<PClient, Api, PBackend>(self, client: Arc<PClient>) -> Self::Output
|
||||
where
|
||||
<Api as sp_api::ApiExt<PBlock>>::StateBackend: sp_api::StateBackend<BlakeTwo256>,
|
||||
PBackend: sc_client_api::Backend<PBlock> + 'static,
|
||||
PBackend::State: sp_api::StateBackend<BlakeTwo256>,
|
||||
Api: RuntimeApiCollection<StateBackend = PBackend::State>,
|
||||
PClient: AbstractClient<PBlock, PBackend, Api = Api> + 'static,
|
||||
{
|
||||
async move {
|
||||
cumulus_client_collator::start_collator(cumulus_client_collator::StartCollatorParams {
|
||||
proposer_factory: self.proposer_factory,
|
||||
inherent_data_providers: self.inherent_data_providers,
|
||||
backend: self.backend,
|
||||
block_import: self.block_import,
|
||||
block_status: self.block_status,
|
||||
announce_block: self.announce_block,
|
||||
overseer_handler: self.overseer_handler,
|
||||
spawner: self.spawner,
|
||||
para_id: self.para_id,
|
||||
key: self.collator_key,
|
||||
polkadot_client: client,
|
||||
polkadot_backend: self.polkadot_backend,
|
||||
})
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameters given to [`start_full_node`].
|
||||
pub struct StartFullNodeParams<'a, Block: BlockT, Client, PClient> {
|
||||
pub para_id: ParaId,
|
||||
pub client: Arc<Client>,
|
||||
pub polkadot_full_node: PFullNode<PClient>,
|
||||
pub polkadot_full_node: RFullNode<PClient>,
|
||||
pub task_manager: &'a mut TaskManager,
|
||||
pub announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
|
||||
}
|
||||
@@ -291,7 +194,7 @@ where
|
||||
Api: RuntimeApiCollection<StateBackend = PBackend::State>,
|
||||
PClient: AbstractClient<PBlock, PBackend, Api = Api> + 'static,
|
||||
{
|
||||
let consensus = cumulus_client_consensus::run_parachain_consensus(
|
||||
let consensus = cumulus_client_consensus_common::run_parachain_consensus(
|
||||
self.para_id,
|
||||
self.client,
|
||||
client,
|
||||
@@ -330,7 +233,7 @@ pub fn prepare_node_config(mut parachain_config: Configuration) -> Configuration
|
||||
pub fn build_polkadot_full_node(
|
||||
config: Configuration,
|
||||
collator_id: CollatorId,
|
||||
) -> Result<PFullNode<PClient>, polkadot_service::Error> {
|
||||
) -> Result<RFullNode<PClient>, polkadot_service::Error> {
|
||||
let is_light = matches!(config.role, Role::Light);
|
||||
if is_light {
|
||||
Err(polkadot_service::Error::Sub(
|
||||
|
||||
Reference in New Issue
Block a user