Add cumulus-service (#187)

* Add cumulus-service

Crate that abstracts the service of a parachain.

* Make finalization infallible
This commit is contained in:
Bastian Köcher
2020-08-08 08:07:33 +02:00
committed by GitHub
parent d8aabf0c32
commit 2efe482c40
11 changed files with 422 additions and 145 deletions
+22 -1
View File
@@ -920,7 +920,6 @@ dependencies = [
"polkadot-validation", "polkadot-validation",
"sc-cli", "sc-cli",
"sc-client-api", "sc-client-api",
"sc-service",
"sp-api", "sp-api",
"sp-blockchain", "sp-blockchain",
"sp-consensus", "sp-consensus",
@@ -1093,6 +1092,27 @@ dependencies = [
"trie-db", "trie-db",
] ]
[[package]]
name = "cumulus-service"
version = "0.1.0"
dependencies = [
"cumulus-collator",
"cumulus-consensus",
"cumulus-network",
"cumulus-primitives",
"polkadot-collator",
"polkadot-primitives",
"polkadot-service",
"sc-client-api",
"sc-service",
"sp-api",
"sp-blockchain",
"sp-consensus",
"sp-core",
"sp-inherents",
"sp-runtime",
]
[[package]] [[package]]
name = "cumulus-test-client" name = "cumulus-test-client"
version = "0.1.0" version = "0.1.0"
@@ -5788,6 +5808,7 @@ dependencies = [
"cumulus-contracts-parachain-runtime", "cumulus-contracts-parachain-runtime",
"cumulus-network", "cumulus-network",
"cumulus-primitives", "cumulus-primitives",
"cumulus-service",
"cumulus-test-parachain-runtime", "cumulus-test-parachain-runtime",
"derive_more 0.15.0", "derive_more 0.15.0",
"exit-future 0.1.4", "exit-future 0.1.4",
+1
View File
@@ -11,6 +11,7 @@ members = [
"rococo-parachains/pallets/token-dealer", "rococo-parachains/pallets/token-dealer",
"rococo-parachains/runtime", "rococo-parachains/runtime",
"runtime", "runtime",
"service",
"test/runtime", "test/runtime",
"test/client", "test/client",
"upward-message", "upward-message",
-1
View File
@@ -13,7 +13,6 @@ sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "roco
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" } sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" } sc-cli = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
# Polkadot dependencies # Polkadot dependencies
-11
View File
@@ -30,7 +30,6 @@ use cumulus_primitives::{
use cumulus_runtime::ParachainBlockData; use cumulus_runtime::ParachainBlockData;
use sc_client_api::{BlockBackend, Finalizer, StateBackend, UsageProvider}; use sc_client_api::{BlockBackend, Finalizer, StateBackend, UsageProvider};
use sc_service::Configuration;
use sp_api::ApiExt; use sp_api::ApiExt;
use sp_blockchain::HeaderBackend; use sp_blockchain::HeaderBackend;
use sp_consensus::{ use sp_consensus::{
@@ -547,16 +546,6 @@ where
} }
} }
/// Prepare the collator's node condifugration
///
/// This function will disable the default announcement of Substrate for the parachain in favor
/// of the one of Cumulus.
pub fn prepare_collator_config(mut parachain_config: Configuration) -> Configuration {
parachain_config.announce_block = false;
parachain_config
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+31 -20
View File
@@ -26,12 +26,10 @@ use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT}, traits::{Block as BlockT, Header as HeaderT},
}; };
use polkadot_primitives::v0::{ use polkadot_primitives::v0::{Block as PBlock, Hash as PHash, Id as ParaId, ParachainHost};
Id as ParaId, ParachainHost, Block as PBlock, Hash as PHash,
};
use codec::Decode; use codec::Decode;
use futures::{future, Future, FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt}; use futures::{future, Future, FutureExt, Stream, StreamExt};
use log::{error, trace, warn}; use log::{error, trace, warn};
use std::{marker::PhantomData, sync::Arc}; use std::{marker::PhantomData, sync::Arc};
@@ -118,26 +116,39 @@ where
polkadot polkadot
.finalized_heads(para_id)? .finalized_heads(para_id)?
.map(|head_data| { .filter_map(|head_data| {
<<Block as BlockT>::Header>::decode(&mut &head_data[..]) let res = match <<Block as BlockT>::Header>::decode(&mut &head_data[..]) {
.map_err(|_| Error::InvalidHeadData) Ok(header) => Some(header),
}) Err(err) => {
.try_for_each(move |p_head| {
future::ready(
finalize_block(&*local, p_head.hash())
.map_err(Error::Client)
.map(|_| ()),
)
})
.map_err(|e| {
warn!( warn!(
target: "cumulus-consensus", target: "cumulus-consensus",
"Failed to finalize block: {:?}", e) "Could not decode Parachain header for finalizing: {:?}",
}) err,
.map(|_| ()) );
None
}
}; };
Ok(future::select(follow_finalized, follow_new_best(para_id, local, polkadot, announce_block)?).map(|_| ())) future::ready(res)
})
.for_each(move |p_head| {
if let Err(e) = finalize_block(&*local, p_head.hash()) {
warn!(
target: "cumulus-consensus",
"Failed to finalize block: {:?}",
e,
);
}
future::ready(())
})
};
Ok(future::select(
follow_finalized,
follow_new_best(para_id, local, polkadot, announce_block)?,
)
.map(|_| ()))
} }
/// Follow the relay chain new best head, to update the Parachain new best head. /// Follow the relay chain new best head, to update the Parachain new best head.
+1
View File
@@ -57,6 +57,7 @@ cumulus-consensus = { path = "../consensus" }
cumulus-collator = { path = "../collator" } cumulus-collator = { path = "../collator" }
cumulus-network = { path = "../network" } cumulus-network = { path = "../network" }
cumulus-primitives = { path = "../primitives" } cumulus-primitives = { path = "../primitives" }
cumulus-service = { path = "../service" }
# Polkadot dependencies # Polkadot dependencies
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "rococo-branch" } polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "rococo-branch" }
+1 -1
View File
@@ -256,7 +256,7 @@ pub fn run() -> Result<()> {
if cli.run.base.validator { "yes" } else { "no" } if cli.run.base.validator { "yes" } else { "no" }
); );
crate::service::run_collator( crate::service::run_node(
config, config,
key, key,
polkadot_config, polkadot_config,
@@ -105,7 +105,7 @@ async fn integration_test() {
let parachain_config = let parachain_config =
parachain_config(task_executor.clone(), Charlie, vec![], para_id).unwrap(); parachain_config(task_executor.clone(), Charlie, vec![], para_id).unwrap();
let (_service, charlie_client) = let (_service, charlie_client) =
crate::service::run_collator(parachain_config, key, polkadot_config, para_id, true) crate::service::run_node(parachain_config, key, polkadot_config, para_id, true)
.unwrap(); .unwrap();
sleep(Duration::from_secs(3)).await; sleep(Duration::from_secs(3)).await;
charlie_client.wait_for_blocks(4).await; charlie_client.wait_for_blocks(4).await;
+71 -89
View File
@@ -15,19 +15,18 @@
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>. // along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use ansi_term::Color; use ansi_term::Color;
use cumulus_collator::{prepare_collator_config, CollatorBuilder}; use cumulus_network::DelayedBlockAnnounceValidator;
use cumulus_network::{DelayedBlockAnnounceValidator, JustifiedBlockAnnounceValidator}; use cumulus_service::{
use polkadot_primitives::v0::{CollatorPair, Block as PBlock, Id as ParaId}; prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
};
use polkadot_primitives::v0::CollatorPair;
use sc_executor::native_executor_instance; use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor; pub use sc_executor::NativeExecutor;
use sc_informant::OutputFormat; use sc_informant::OutputFormat;
use sc_service::{Configuration, PartialComponents, TaskManager, TFullBackend, TFullClient, Role}; use sc_service::{Configuration, PartialComponents, Role, TFullBackend, TFullClient, TaskManager};
use std::sync::Arc; use sp_runtime::traits::BlakeTwo256;
use sp_core::crypto::Pair;
use sp_trie::PrefixedMemoryDB; use sp_trie::PrefixedMemoryDB;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT}; use std::sync::Arc;
use polkadot_service::{AbstractClient, RuntimeApiCollection};
use sp_consensus::SyncOracle;
// Our native executor instance. // Our native executor instance.
native_executor_instance!( native_executor_instance!(
@@ -40,22 +39,36 @@ native_executor_instance!(
/// ///
/// Use this macro if you don't actually need the full service, but just the builder in order to /// Use this macro if you don't actually need the full service, but just the builder in order to
/// be able to perform chain operations. /// be able to perform chain operations.
pub fn new_partial(config: &mut Configuration) -> Result< pub fn new_partial(
config: &mut Configuration,
) -> Result<
PartialComponents< PartialComponents<
TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>, TFullClient<
parachain_runtime::opaque::Block,
parachain_runtime::RuntimeApi,
crate::service::Executor,
>,
TFullBackend<parachain_runtime::opaque::Block>, TFullBackend<parachain_runtime::opaque::Block>,
(), (),
sp_consensus::import_queue::BasicQueue<parachain_runtime::opaque::Block, PrefixedMemoryDB<BlakeTwo256>>, sp_consensus::import_queue::BasicQueue<
sc_transaction_pool::FullPool<parachain_runtime::opaque::Block, TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>>, parachain_runtime::opaque::Block,
PrefixedMemoryDB<BlakeTwo256>,
>,
sc_transaction_pool::FullPool<
parachain_runtime::opaque::Block,
TFullClient<
parachain_runtime::opaque::Block,
parachain_runtime::RuntimeApi,
crate::service::Executor,
>,
>,
(), (),
>, >,
sc_service::Error, sc_service::Error,
> > {
{
let inherent_data_providers = sp_inherents::InherentDataProviders::new(); let inherent_data_providers = sp_inherents::InherentDataProviders::new();
let (client, backend, keystore, task_manager) = let (client, backend, keystore, task_manager) = sc_service::new_full_parts::<
sc_service::new_full_parts::<
parachain_runtime::opaque::Block, parachain_runtime::opaque::Block,
parachain_runtime::RuntimeApi, parachain_runtime::RuntimeApi,
crate::service::Executor, crate::service::Executor,
@@ -96,24 +109,30 @@ pub fn new_partial(config: &mut Configuration) -> Result<
Ok(params) Ok(params)
} }
/// Run a collator node with the given parachain `Configuration` and relaychain `Configuration` /// Run a node with the given parachain `Configuration` and relay chain `Configuration`
/// ///
/// This function blocks until done. /// This function blocks until done.
pub fn run_collator( pub fn run_node(
parachain_config: Configuration, parachain_config: Configuration,
key: Arc<CollatorPair>, collator_key: Arc<CollatorPair>,
mut polkadot_config: polkadot_collator::Configuration, mut polkadot_config: polkadot_collator::Configuration,
id: polkadot_primitives::v0::Id, id: polkadot_primitives::v0::Id,
validator: bool, validator: bool,
) -> sc_service::error::Result<( ) -> sc_service::error::Result<(
TaskManager, TaskManager,
Arc<TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>>, Arc<
TFullClient<
parachain_runtime::opaque::Block,
parachain_runtime::RuntimeApi,
crate::service::Executor,
>,
>,
)> { )> {
if matches!(parachain_config.role, Role::Light) { if matches!(parachain_config.role, Role::Light) {
return Err("Light client not supported!".into()); return Err("Light client not supported!".into());
} }
let mut parachain_config = prepare_collator_config(parachain_config); let mut parachain_config = prepare_node_config(parachain_config);
parachain_config.informant_output_format = OutputFormat { parachain_config.informant_output_format = OutputFormat {
enable_color: true, enable_color: true,
@@ -125,7 +144,8 @@ pub fn run_collator(
}; };
let params = new_partial(&mut parachain_config)?; let params = new_partial(&mut parachain_config)?;
params.inherent_data_providers params
.inherent_data_providers
.register_provider(sp_timestamp::InherentDataProvider) .register_provider(sp_timestamp::InherentDataProvider)
.unwrap(); .unwrap();
@@ -171,7 +191,7 @@ pub fn run_collator(
}) })
}; };
let _rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { sc_service::spawn_tasks(sc_service::SpawnTasksParams {
on_demand: None, on_demand: None,
remote_blockchain: None, remote_blockchain: None,
rpc_extensions_builder, rpc_extensions_builder,
@@ -187,6 +207,8 @@ pub fn run_collator(
system_rpc_tx, system_rpc_tx,
})?; })?;
let announce_block = Arc::new(move |hash, data| network.announce_block(hash, data));
if validator { if validator {
let proposer_factory = sc_basic_authorship::ProposerFactory::new( let proposer_factory = sc_basic_authorship::ProposerFactory::new(
client.clone(), client.clone(),
@@ -194,76 +216,36 @@ pub fn run_collator(
prometheus_registry.as_ref(), prometheus_registry.as_ref(),
); );
let block_import = client.clone(); let params = StartCollatorParams {
let announce_block = Arc::new(move |hash, data| network.announce_block(hash, data));
let builder = CollatorBuilder::new(
proposer_factory,
params.inherent_data_providers,
block_import,
client.clone(),
id,
client.clone(),
announce_block,
block_announce_validator,
);
let (polkadot_future, polkadot_task_manager) =
polkadot_collator::start_collator(builder, id, key, polkadot_config)?;
task_manager
.spawn_essential_handle()
.spawn("polkadot", polkadot_future);
task_manager.add_child(polkadot_task_manager);
} else {
let is_light = matches!(polkadot_config.role, Role::Light);
let (polkadot_task_manager, client, handles) = if is_light {
Err("Light client not supported.".into())
} else {
polkadot_service::build_full(
polkadot_config,
Some((key.public(), id)),
None,
false,
6000,
None,
)
}?;
let polkadot_network = handles.polkadot_network.expect("polkadot service is started; qed");
client.execute_with(SetDelayedBlockAnnounceValidator {
block_announce_validator,
para_id: id, para_id: id,
polkadot_sync_oracle: Box::new(polkadot_network), block_import: client.clone(),
}); proposer_factory,
inherent_data_providers: params.inherent_data_providers,
block_status: client.clone(),
announce_block,
client: client.clone(),
block_announce_validator,
task_manager: &mut task_manager,
polkadot_config,
collator_key,
};
task_manager.add_child(polkadot_task_manager); start_collator(params)?;
} else {
let params = StartFullNodeParams {
client: client.clone(),
announce_block,
polkadot_config,
collator_key,
block_announce_validator,
task_manager: &mut task_manager,
para_id: id,
};
start_full_node(params)?;
} }
start_network.start_network(); start_network.start_network();
Ok((task_manager, client)) Ok((task_manager, client))
} }
struct SetDelayedBlockAnnounceValidator<B: BlockT> {
block_announce_validator: DelayedBlockAnnounceValidator<B>,
para_id: ParaId,
polkadot_sync_oracle: Box<dyn SyncOracle + Send>,
}
impl<B: BlockT> polkadot_service::ExecuteWithClient for SetDelayedBlockAnnounceValidator<B> {
type Output = ();
fn execute_with_client<Client, Api, Backend>(self, client: Arc<Client>) -> Self::Output
where<Api as sp_api::ApiExt<PBlock>>::StateBackend: sp_api::StateBackend<BlakeTwo256>,
Backend: sc_client_api::Backend<PBlock>,
Backend::State: sp_api::StateBackend<BlakeTwo256>,
Api: RuntimeApiCollection<StateBackend = Backend::State>,
Client: AbstractClient<PBlock, Backend, Api = Api> + 'static
{
self.block_announce_validator.set(Box::new(JustifiedBlockAnnounceValidator::new(
client,
self.para_id,
self.polkadot_sync_oracle,
)));
}
}
+27
View File
@@ -0,0 +1,27 @@
[package]
name = "cumulus-service"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
# Cumulus dependencies
cumulus-consensus = { path = "../consensus" }
cumulus-collator = { path = "../collator" }
cumulus-primitives = { path = "../primitives" }
cumulus-network = { path = "../network" }
# Substrate dependencies
sc-service = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "rococo-branch" }
# Polkadot dependencies
polkadot-collator = { git = "https://github.com/paritytech/polkadot", branch = "rococo-branch" }
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "rococo-branch" }
polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "rococo-branch" }
+246
View File
@@ -0,0 +1,246 @@
// Copyright 2020 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 <http://www.gnu.org/licenses/>.
//! Cumulus service
//!
//! Provides functions for starting a collator node or a normal full node.
use cumulus_collator::CollatorBuilder;
use cumulus_network::{DelayedBlockAnnounceValidator, JustifiedBlockAnnounceValidator};
use cumulus_primitives::ParaId;
use polkadot_primitives::v0::{Block as PBlock, CollatorPair};
use polkadot_service::{AbstractClient, RuntimeApiCollection};
use sc_client_api::{Backend as BackendT, BlockBackend, Finalizer, UsageProvider};
use sc_service::{Configuration, Role, TaskManager};
use sp_blockchain::{HeaderBackend, Result as ClientResult};
use sp_consensus::{BlockImport, Environment, Error as ConsensusError, Proposer, SyncOracle};
use sp_core::crypto::Pair;
use sp_inherents::InherentDataProviders;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT};
use std::{marker::PhantomData, sync::Arc};
/// Parameters given to [`start_collator`].
pub struct StartCollatorParams<'a, Block: BlockT, PF, BI, BS, Client> {
pub para_id: ParaId,
pub proposer_factory: PF,
pub inherent_data_providers: InherentDataProviders,
pub block_import: BI,
pub block_status: Arc<BS>,
pub announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
pub client: Arc<Client>,
pub block_announce_validator: DelayedBlockAnnounceValidator<Block>,
pub task_manager: &'a mut TaskManager,
pub polkadot_config: Configuration,
pub collator_key: Arc<CollatorPair>,
}
/// Start a collator node for a parachain.
///
/// A collator is similar to a validator in a normal blockchain.
/// It is reponsible for producing blocks and sending the blocks to a
/// parachain validator for validation and inclusion into the relay chain.
pub fn start_collator<'a, Block, PF, BI, BS, Client, Backend>(
StartCollatorParams {
para_id,
proposer_factory,
inherent_data_providers,
block_import,
block_status,
announce_block,
client,
block_announce_validator,
task_manager,
polkadot_config,
collator_key,
}: StartCollatorParams<'a, Block, PF, BI, BS, Client>,
) -> 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>
+ HeaderBackend<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ 'static,
for<'b> &'b Client: BlockImport<Block>,
Backend: BackendT<Block> + 'static,
{
let builder = CollatorBuilder::new(
proposer_factory,
inherent_data_providers,
block_import,
block_status,
para_id,
client,
announce_block,
block_announce_validator,
);
let (polkadot_future, polkadot_task_manager) =
polkadot_collator::start_collator(builder, para_id, collator_key, polkadot_config)?;
task_manager
.spawn_essential_handle()
.spawn("polkadot", polkadot_future);
task_manager.add_child(polkadot_task_manager);
Ok(())
}
/// Parameters given to [`start_full_node`].
pub struct StartFullNodeParams<'a, Block: BlockT, Client> {
pub polkadot_config: Configuration,
pub collator_key: Arc<CollatorPair>,
pub para_id: ParaId,
pub block_announce_validator: DelayedBlockAnnounceValidator<Block>,
pub client: Arc<Client>,
pub announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
pub task_manager: &'a mut TaskManager,
}
/// 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<Block, Client, Backend>(
StartFullNodeParams {
polkadot_config,
collator_key,
para_id,
block_announce_validator,
client,
announce_block,
task_manager,
}: StartFullNodeParams<Block, Client>,
) -> sc_service::error::Result<()>
where
Block: BlockT,
Client: Finalizer<Block, Backend>
+ UsageProvider<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ 'static,
for<'a> &'a Client: BlockImport<Block>,
Backend: BackendT<Block> + 'static,
{
let is_light = matches!(polkadot_config.role, Role::Light);
let (polkadot_task_manager, pclient, handles) = if is_light {
Err("Light client not supported.".into())
} else {
polkadot_service::build_full(
polkadot_config,
Some((collator_key.public(), para_id)),
None,
false,
6000,
None,
)
}?;
let polkadot_network = handles
.polkadot_network
.expect("Polkadot service is started; qed");
pclient.execute_with(InitParachainFullNode {
block_announce_validator,
para_id,
polkadot_sync_oracle: Box::new(polkadot_network),
announce_block,
client,
task_manager,
_phantom: PhantomData,
})?;
task_manager.add_child(polkadot_task_manager);
Ok(())
}
/// Prepare the parachain's node condifugration
///
/// 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
}
struct InitParachainFullNode<'a, Block: BlockT, Client, Backend> {
block_announce_validator: DelayedBlockAnnounceValidator<Block>,
para_id: ParaId,
polkadot_sync_oracle: Box<dyn SyncOracle + Send>,
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
client: Arc<Client>,
task_manager: &'a mut TaskManager,
_phantom: PhantomData<Backend>,
}
impl<'a, Block, Client, Backend> polkadot_service::ExecuteWithClient
for InitParachainFullNode<'a, Block, Client, Backend>
where
Block: BlockT,
Client: Finalizer<Block, Backend>
+ UsageProvider<Block>
+ Send
+ Sync
+ BlockBackend<Block>
+ 'static,
for<'b> &'b Client: BlockImport<Block>,
Backend: BackendT<Block> + 'static,
{
type Output = ClientResult<()>;
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>,
PBackend::State: sp_api::StateBackend<BlakeTwo256>,
Api: RuntimeApiCollection<StateBackend = PBackend::State>,
PClient: AbstractClient<PBlock, PBackend, Api = Api> + 'static,
{
self.block_announce_validator
.set(Box::new(JustifiedBlockAnnounceValidator::new(
client.clone(),
self.para_id,
self.polkadot_sync_oracle,
)));
let future = cumulus_consensus::follow_polkadot(
self.para_id,
self.client,
client,
self.announce_block,
)?;
self.task_manager
.spawn_essential_handle()
.spawn("cumulus-consensus", future);
Ok(())
}
}