Run polkadot using future (#63)

Fixes #56 and #44
This commit is contained in:
Cecile Tonglet
2020-02-27 17:54:52 +01:00
committed by GitHub
parent ad8b70a8e4
commit 0ea4b843e4
3 changed files with 116 additions and 240 deletions
+14 -14
View File
@@ -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)",
+68 -80
View File
@@ -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<Block, SP> {
setup_parachain: SP,
pub struct CollatorBuilder<Block: BlockT, PF, BI, Backend, Executor, Runtime> {
proposer_factory: PF,
inherent_data_providers: InherentDataProviders,
block_import: BI,
para_id: ParaId,
client: Arc<Client<Backend, Executor, Block, Runtime>>,
_marker: PhantomData<Block>,
}
impl<Block, SP> CollatorBuilder<Block, SP> {
impl<Block: BlockT, PF, BI, Backend, Executor, Runtime>
CollatorBuilder<Block, PF, BI, Backend, Executor, Runtime>
{
/// 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<Client<Backend, Executor, Block, Runtime>>,
) -> Self {
Self {
setup_parachain,
proposer_factory,
inherent_data_providers,
block_import,
para_id,
client,
_marker: PhantomData,
}
}
}
impl<Block: BlockT, SP: SetupParachain<Block>> BuildParachainContext for CollatorBuilder<Block, SP>
type TransactionFor<E, Block> =
<<E as Environment<Block>>::Proposer as Proposer<Block>>::Transaction;
impl<Block: BlockT, PF, BI, Backend, Executor, Runtime> BuildParachainContext
for CollatorBuilder<Block, PF, BI, Backend, Executor, Runtime>
where
<SP::ProposerFactory as Environment<Block>>::Proposer: Send,
PF: Environment<Block> + Send + 'static,
BI: BlockImport<Block, Error = sp_consensus::Error, Transaction = TransactionFor<PF, Block>>
+ Send
+ Sync
+ 'static,
Backend: sc_client_api::Backend<Block> + 'static,
Executor: sc_client_api::CallExecutor<Block> + Send + Sync + 'static,
Runtime: Send + Sync + 'static,
{
type ParachainContext = Collator<Block, SP::ProposerFactory, SP::BlockImport>;
type ParachainContext = Collator<Block, PF, BI>;
fn build<B, E, R, Spawner, Extrinsic>(
self,
client: Arc<PolkadotClient<B, E, R>>,
polkadot_client: Arc<PolkadotClient<B, E, R>>,
spawner: Spawner,
network: Arc<dyn CollatorNetwork>,
) -> Result<Self::ParachainContext, ()>
@@ -273,53 +302,32 @@ where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
B::State: sp_api::StateBackend<sp_core::Blake2Hasher>,
{
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<Block: BlockT>: Send {
/// The proposer factory of the parachain to build blocks.
type ProposerFactory: Environment<Block> + Send + 'static;
/// The block import for importing the blocks build by the collator.
type BlockImport: BlockImport<
Block,
Error = ConsensusError,
Transaction = <<Self::ProposerFactory as Environment<Block>>::Proposer as Proposer<
Block,
>>::Transaction,
> + Send
+ Sync
+ 'static;
/// Setup the parachain.
fn setup_parachain<P, SP>(
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<Block> for DummySetup {
type ProposerFactory = DummyFactory;
type BlockImport = Client;
fn setup_parachain<P, SP>(
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(),
+34 -146
View File
@@ -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<E: sc_service::ChainSpecExtension>(
mut parachain_config: Configuration<GenesisConfig, E>,
parachain_config: Configuration<GenesisConfig, E>,
key: Arc<CollatorPair>,
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<S, PF, BI> {
service: S,
proposer_factory: PF,
inherent_data_providers: InherentDataProviders,
block_import: BI,
}
type TransactionFor<E, Block> =
<<E as Environment<Block>>::Proposer as Proposer<Block>>::Transaction;
impl<S, PF, BI> cumulus_collator::SetupParachain<Block> for SetupParachain<S, PF, BI>
where
S: AbstractService,
PF: Environment<Block> + Send + 'static,
BI: BlockImport<Block, Error = sp_consensus::Error, Transaction = TransactionFor<PF, Block>>
+ Send
+ Sync
+ 'static,
{
type ProposerFactory = PF;
type BlockImport = BI;
fn setup_parachain<P: cumulus_consensus::PolkadotClient, Spawner>(
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)
})
}