diff --git a/cumulus/Cargo.lock b/cumulus/Cargo.lock index 1bfecf5c80..06c4d6d11a 100644 --- a/cumulus/Cargo.lock +++ b/cumulus/Cargo.lock @@ -2160,7 +2160,7 @@ dependencies = [ [[package]] name = "kusama-runtime" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "bitvec 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "frame-executive 2.0.0 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", @@ -3881,7 +3881,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "polkadot-availability-store" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)", "exit-future 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3909,7 +3909,7 @@ dependencies = [ [[package]] name = "polkadot-cli" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3929,7 +3929,7 @@ dependencies = [ [[package]] name = "polkadot-collator" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures-timer 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3955,7 +3955,7 @@ dependencies = [ [[package]] name = "polkadot-erasure-coding" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "derive_more 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "parity-scale-codec 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3968,7 +3968,7 @@ dependencies = [ [[package]] name = "polkadot-network" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3996,7 +3996,7 @@ dependencies = [ [[package]] name = "polkadot-parachain" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4016,7 +4016,7 @@ dependencies = [ [[package]] name = "polkadot-primitives" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "bitvec 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "pallet-babe 2.0.0 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", @@ -4036,7 +4036,7 @@ dependencies = [ [[package]] name = "polkadot-rpc" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "jsonrpc-core 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "pallet-transaction-payment-rpc 2.0.0 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", @@ -4053,7 +4053,7 @@ dependencies = [ [[package]] name = "polkadot-runtime" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "bitvec 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "frame-executive 2.0.0 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", @@ -4112,7 +4112,7 @@ dependencies = [ [[package]] name = "polkadot-runtime-common" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "bitvec 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "frame-support 2.0.0 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", @@ -4142,7 +4142,7 @@ dependencies = [ [[package]] name = "polkadot-service" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "frame-system-rpc-runtime-api 2.0.0 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4196,7 +4196,7 @@ dependencies = [ [[package]] name = "polkadot-statement-table" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "parity-scale-codec 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "polkadot-primitives 0.7.21 (git+https://github.com/paritytech/polkadot?branch=cumulus-branch)", @@ -4206,7 +4206,7 @@ dependencies = [ [[package]] name = "polkadot-validation" version = "0.7.21" -source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#b1f88ed21ec551c35ff2c2d776b214f2383a56d6" +source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#5ed74bac90ac24a2abd9828e310caaccde26f15c" dependencies = [ "bitvec 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "derive_more 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/cumulus/collator/src/lib.rs b/cumulus/collator/src/lib.rs index 50aace3ff6..5f25f08f58 100644 --- a/cumulus/collator/src/lib.rs +++ b/cumulus/collator/src/lib.rs @@ -18,6 +18,7 @@ use cumulus_runtime::ParachainBlockData; +use sc_client::Client; use sp_consensus::{ BlockImport, BlockImportParams, BlockOrigin, Environment, Error as ConsensusError, ForkChoiceStrategy, Proposal, Proposer, RecordProof, @@ -30,14 +31,15 @@ use polkadot_collator::{ PolkadotClient, }; use polkadot_primitives::{ - parachain::{self, BlockData, Status as ParachainStatus}, Block as PBlock, Hash as PHash, + parachain::{self, BlockData, Status as ParachainStatus, Id as ParaId}, Block as PBlock, + Hash as PHash, }; use codec::{Decode, Encode}; use log::{error, trace}; -use futures::{task::Spawn, Future, future}; +use futures::{task::Spawn, Future, future, FutureExt}; use std::{fmt::Debug, marker::PhantomData, sync::Arc, time::Duration, pin::Pin}; @@ -231,30 +233,57 @@ where } /// Implements `BuildParachainContext` to build a collator instance. -pub struct CollatorBuilder { - setup_parachain: SP, +pub struct CollatorBuilder { + proposer_factory: PF, + inherent_data_providers: InherentDataProviders, + block_import: BI, + para_id: ParaId, + client: Arc>, _marker: PhantomData, } -impl CollatorBuilder { +impl + CollatorBuilder +{ /// Create a new instance of self. - pub fn new(setup_parachain: SP) -> Self { + pub fn new( + proposer_factory: PF, + inherent_data_providers: InherentDataProviders, + block_import: BI, + para_id: ParaId, + client: Arc>, + ) -> Self { Self { - setup_parachain, + proposer_factory, + inherent_data_providers, + block_import, + para_id, + client, _marker: PhantomData, } } } -impl> BuildParachainContext for CollatorBuilder +type TransactionFor = + <>::Proposer as Proposer>::Transaction; + +impl BuildParachainContext + for CollatorBuilder where - >::Proposer: Send, + PF: Environment + Send + 'static, + BI: BlockImport> + + Send + + Sync + + 'static, + Backend: sc_client_api::Backend + 'static, + Executor: sc_client_api::CallExecutor + Send + Sync + 'static, + Runtime: Send + Sync + 'static, { - type ParachainContext = Collator; + type ParachainContext = Collator; fn build( self, - client: Arc>, + polkadot_client: Arc>, spawner: Spawner, network: Arc, ) -> Result @@ -273,53 +302,32 @@ where // Rust bug: https://github.com/rust-lang/rust/issues/24159 B::State: sp_api::StateBackend, { - let (proposer_factory, block_import, inherent_data_providers) = self - .setup_parachain - .setup_parachain(client, spawner) - .map_err(|e| error!("Error setting up the parachain: {}", e))?; + let follow = + match cumulus_consensus::follow_polkadot(self.para_id, self.client, polkadot_client) { + Ok(follow) => follow, + Err(e) => { + return Err(error!("Could not start following polkadot: {:?}", e)); + } + }; + + spawner + .spawn_obj( + Box::new( + follow.map(|_| ()), + ) + .into(), + ) + .map_err(|_| error!("Could not spawn parachain server!"))?; Ok(Collator::new( - proposer_factory, - inherent_data_providers, + self.proposer_factory, + self.inherent_data_providers, network, - block_import, + self.block_import, )) } } -/// Something that can setup a parachain. -pub trait SetupParachain: Send { - /// The proposer factory of the parachain to build blocks. - type ProposerFactory: Environment + Send + 'static; - /// The block import for importing the blocks build by the collator. - type BlockImport: BlockImport< - Block, - Error = ConsensusError, - Transaction = <>::Proposer as Proposer< - Block, - >>::Transaction, - > + Send - + Sync - + 'static; - - /// Setup the parachain. - fn setup_parachain( - self, - polkadot_client: P, - spawner: SP, - ) -> Result< - ( - Self::ProposerFactory, - Self::BlockImport, - InherentDataProviders, - ), - String, - > - where - P: cumulus_consensus::PolkadotClient, - SP: Spawn + Clone + Send + Sync + 'static; -} - #[cfg(test)] mod tests { use super::*; @@ -339,7 +347,7 @@ mod tests { use substrate_test_client::{NativeExecutor, WasmExecutionMethod::Interpreted}; use test_client::{ - Client, DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt, + DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt, }; use test_runtime::{Block, Header}; @@ -433,38 +441,19 @@ mod tests { } } - struct DummySetup; - - impl SetupParachain for DummySetup { - type ProposerFactory = DummyFactory; - type BlockImport = Client; - - fn setup_parachain( - self, - _: P, - _: SP, - ) -> Result< - ( - Self::ProposerFactory, - Self::BlockImport, - InherentDataProviders, - ), - String, - > { - Ok(( - DummyFactory, - TestClientBuilder::new().build(), - InherentDataProviders::default(), - )) - } - } - #[test] fn collates_produces_a_block() { + let id = ParaId::from(100); let _ = env_logger::try_init(); let spawner = futures::executor::ThreadPool::new().unwrap(); - let builder = CollatorBuilder::new(DummySetup); + let builder = CollatorBuilder::new( + DummyFactory, + InherentDataProviders::default(), + TestClientBuilder::new().build(), + id, + Arc::new(TestClientBuilder::new().build()), + ); let context = builder .build::<_, _, polkadot_service::polkadot_runtime::RuntimeApi, _, _>( Arc::new( @@ -479,7 +468,6 @@ mod tests { ) .expect("Creates parachain context"); - let id = ParaId::from(100); let header = Header::new( 0, Default::default(), diff --git a/cumulus/test/parachain/src/service.rs b/cumulus/test/parachain/src/service.rs index 317f710cbe..e92d2b4c18 100644 --- a/cumulus/test/parachain/src/service.rs +++ b/cumulus/test/parachain/src/service.rs @@ -21,19 +21,12 @@ use parachain_runtime::{self, opaque::Block, GenesisConfig}; use sc_executor::native_executor_instance; use sc_network::construct_simple_protocol; use sc_service::{AbstractService, Configuration}; -use sp_consensus::{BlockImport, Environment, Proposer}; -use sp_inherents::InherentDataProviders; -use sp_core::crypto::Pair; -use polkadot_collator::build_collator_service; use polkadot_primitives::parachain::CollatorPair; -use polkadot_service::IsKusama; use cumulus_collator::CollatorBuilder; -use futures::{future, task::Spawn, FutureExt, TryFutureExt}; - -use log::error; +use futures::FutureExt; pub use sc_executor::NativeExecutor; @@ -86,150 +79,45 @@ macro_rules! new_full_start { /// /// This function blocks until done. pub fn run_collator( - mut parachain_config: Configuration, + parachain_config: Configuration, key: Arc, - polkadot_config: polkadot_collator::Configuration, + mut polkadot_config: polkadot_collator::Configuration, ) -> sc_cli::error::Result<()> { - let para_id = crate::PARA_ID; + sc_cli::run_service_until_exit(parachain_config, move |parachain_config| { + polkadot_config.task_executor = parachain_config.task_executor.clone(); - if polkadot_config.expect_chain_spec().is_kusama() { - sc_cli::run_service_until_exit(polkadot_config, |polkadot_config| { - parachain_config.task_executor = polkadot_config.task_executor.clone(); + let (builder, inherent_data_providers) = new_full_start!(parachain_config); + inherent_data_providers + .register_provider(sp_timestamp::InherentDataProvider) + .unwrap(); - let (builder, inherent_data_providers) = new_full_start!(parachain_config); - inherent_data_providers - .register_provider(sp_timestamp::InherentDataProvider) - .unwrap(); + let service = builder + .with_network_protocol(|_| Ok(NodeProtocol::new()))? + .build()?; - let service = builder - .with_network_protocol(|_| Ok(NodeProtocol::new()))? - .build()?; - let proposer_factory = sc_basic_authorship::ProposerFactory { - client: service.client(), - transaction_pool: service.transaction_pool(), - }; + let proposer_factory = sc_basic_authorship::ProposerFactory { + client: service.client(), + transaction_pool: service.transaction_pool(), + }; - let block_import = service.client(); + let block_import = service.client(); + let client = service.client(); + let builder = CollatorBuilder::new( + proposer_factory, + inherent_data_providers, + block_import, + crate::PARA_ID, + client, + ); - let setup_parachain = SetupParachain { - service, - inherent_data_providers, - proposer_factory, - block_import, - }; + let polkadot_future = polkadot_collator::start_collator( + builder, + crate::PARA_ID, + key, + polkadot_config, + ).map(|_| ()); + service.spawn_essential_task("polkadot", polkadot_future); - let builder = CollatorBuilder::new(setup_parachain); - - build_collator_service( - polkadot_service::kusama_new_full(polkadot_config, Some((key.public(), para_id)), None, false, 6000)?, - para_id, - key, - builder, - ) - }) - } else { - sc_cli::run_service_until_exit(polkadot_config, |polkadot_config| { - parachain_config.task_executor = polkadot_config.task_executor.clone(); - - let (builder, inherent_data_providers) = new_full_start!(parachain_config); - inherent_data_providers - .register_provider(sp_timestamp::InherentDataProvider) - .unwrap(); - - let service = builder - .with_network_protocol(|_| Ok(NodeProtocol::new()))? - .build()?; - let proposer_factory = sc_basic_authorship::ProposerFactory { - client: service.client(), - transaction_pool: service.transaction_pool(), - }; - - let block_import = service.client(); - - let setup_parachain = SetupParachain { - service, - inherent_data_providers, - proposer_factory, - block_import, - }; - - let builder = CollatorBuilder::new(setup_parachain); - - build_collator_service( - polkadot_service::polkadot_new_full(polkadot_config, Some((key.public(), para_id)), None, false, 6000)?, - para_id, - key, - builder, - ) - }) - } -} - -struct SetupParachain { - service: S, - proposer_factory: PF, - inherent_data_providers: InherentDataProviders, - block_import: BI, -} - -type TransactionFor = - <>::Proposer as Proposer>::Transaction; - -impl cumulus_collator::SetupParachain for SetupParachain -where - S: AbstractService, - PF: Environment + Send + 'static, - BI: BlockImport> - + Send - + Sync - + 'static, -{ - type ProposerFactory = PF; - type BlockImport = BI; - - fn setup_parachain( - self, - polkadot_client: P, - spawner: Spawner, - ) -> Result< - ( - Self::ProposerFactory, - Self::BlockImport, - InherentDataProviders, - ), - String, - > - where - Spawner: Spawn + Clone + Send + Sync + 'static, - { - let client = self.service.client(); - - let follow = - match cumulus_consensus::follow_polkadot(crate::PARA_ID, client, polkadot_client) { - Ok(follow) => follow, - Err(e) => { - return Err(format!("Could not start following polkadot: {:?}", e)); - } - }; - - spawner - .spawn_obj( - Box::new( - future::select( - self.service - .map_err(|e| error!("Parachain service error: {:?}", e)), - follow, - ) - .map(|_| ()), - ) - .into(), - ) - .map_err(|_| "Could not spawn parachain server!")?; - - Ok(( - self.proposer_factory, - self.block_import, - self.inherent_data_providers, - )) - } + Ok(service) + }) }