Pass client and task_executor to BuildParachainContext (#484)

* Pass `client` and `task_executor` to `BuildParachainContext`

* Update `Cargo.lock`
This commit is contained in:
Bastian Köcher
2019-10-19 13:26:15 +02:00
committed by GitHub
parent 2842be7b81
commit 687244f989
4 changed files with 67 additions and 21 deletions
+1
View File
@@ -24,6 +24,7 @@ dependencies = [
"polkadot-collator 0.6.0", "polkadot-collator 0.6.0",
"polkadot-parachain 0.6.0", "polkadot-parachain 0.6.0",
"polkadot-primitives 0.6.0", "polkadot-primitives 0.6.0",
"substrate-client 2.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)",
"substrate-primitives 2.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-primitives 2.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)",
] ]
+47 -16
View File
@@ -51,25 +51,25 @@ use std::time::Duration;
use futures::{future, Stream, Future, IntoFuture}; use futures::{future, Stream, Future, IntoFuture};
use futures03::{TryStreamExt as _, StreamExt as _}; use futures03::{TryStreamExt as _, StreamExt as _};
use log::{info, warn}; use log::{warn, error};
use client::BlockchainEvents; use client::BlockchainEvents;
use primitives::Pair; use primitives::{Pair, Blake2Hasher};
use polkadot_primitives::{ use polkadot_primitives::{
BlockId, Hash, Block, BlockId, Hash, Block,
parachain::{ parachain::{
self, BlockData, DutyRoster, HeadData, ConsolidatedIngress, Message, Id as ParaId, OutgoingMessages, self, BlockData, DutyRoster, HeadData, ConsolidatedIngress, Message, Id as ParaId,
PoVBlock, Status as ParachainStatus, ValidatorId, CollatorPair, OutgoingMessages, PoVBlock, Status as ParachainStatus, ValidatorId, CollatorPair,
} }
}; };
use polkadot_cli::{ use polkadot_cli::{
Worker, IntoExit, ProvideRuntimeApi, TaskExecutor, AbstractService, Worker, IntoExit, ProvideRuntimeApi, AbstractService, CustomConfiguration, ParachainHost,
CustomConfiguration, ParachainHost,
}; };
use polkadot_network::validation::{LeafWorkParams, ValidationNetwork}; use polkadot_network::validation::{LeafWorkParams, ValidationNetwork};
use polkadot_network::{PolkadotNetworkService, PolkadotProtocol}; use polkadot_network::{PolkadotNetworkService, PolkadotProtocol};
use polkadot_runtime::RuntimeApi;
use tokio::timer::Timeout; use tokio::timer::Timeout;
pub use polkadot_cli::VersionInfo; pub use polkadot_cli::{VersionInfo, TaskExecutor};
pub use polkadot_network::validation::Incoming; pub use polkadot_network::validation::Incoming;
pub use polkadot_validation::SignedStatement; pub use polkadot_validation::SignedStatement;
pub use polkadot_primitives::parachain::CollatorId; pub use polkadot_primitives::parachain::CollatorId;
@@ -128,13 +128,24 @@ impl<R: fmt::Display> fmt::Display for Error<R> {
} }
} }
/// The Polkadot client type.
pub type PolkadotClient<B, E> = client::Client<B, E, Block, RuntimeApi>;
/// Something that can build a `ParachainContext`. /// Something that can build a `ParachainContext`.
pub trait BuildParachainContext { pub trait BuildParachainContext {
/// The parachain context produced by the `build` function. /// The parachain context produced by the `build` function.
type ParachainContext: self::ParachainContext; type ParachainContext: self::ParachainContext;
/// Build the `ParachainContext`. /// Build the `ParachainContext`.
fn build(self, network: Arc<dyn Network>) -> Result<Self::ParachainContext, ()>; fn build<B, E>(
self,
client: Arc<PolkadotClient<B, E>>,
task_executor: TaskExecutor,
network: Arc<dyn Network>,
) -> Result<Self::ParachainContext, ()>
where
B: client::backend::Backend<Block, Blake2Hasher> + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static;
} }
/// Parachain context needed for collation. /// Parachain context needed for collation.
@@ -289,10 +300,18 @@ impl<P, E> Worker for CollationNode<P, E> where
} }
fn work<S, SC, B, CE>(self, service: &S, task_executor: TaskExecutor) -> Self::Work fn work<S, SC, B, CE>(self, service: &S, task_executor: TaskExecutor) -> Self::Work
where S: AbstractService<Block = polkadot_service::Block, RuntimeApi = polkadot_service::RuntimeApi, Backend = B, SelectChain = SC, NetworkSpecialization = PolkadotProtocol, CallExecutor = CE>, where
SC: polkadot_service::SelectChain<polkadot_service::Block> + 'static, S: AbstractService<
B: polkadot_service::Backend<polkadot_service::Block, polkadot_service::Blake2Hasher> + 'static, Block = Block,
CE: polkadot_service::CallExecutor<polkadot_service::Block, polkadot_service::Blake2Hasher> + Clone + Send + Sync + 'static RuntimeApi = RuntimeApi,
Backend = B,
SelectChain = SC,
NetworkSpecialization = PolkadotProtocol,
CallExecutor = CE,
>,
SC: polkadot_service::SelectChain<Block> + 'static,
B: client::backend::Backend<Block, Blake2Hasher> + 'static,
CE: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static
{ {
let CollationNode { build_parachain_context, exit, para_id, key } = self; let CollationNode { build_parachain_context, exit, para_id, key } = self;
let client = service.client(); let client = service.client();
@@ -301,7 +320,7 @@ impl<P, E> Worker for CollationNode<P, E> where
let select_chain = if let Some(select_chain) = service.select_chain() { let select_chain = if let Some(select_chain) = service.select_chain() {
select_chain select_chain
} else { } else {
info!("The node cannot work because it can't select chain."); error!("The node cannot work because it can't select chain.");
return Box::new(future::err(())); return Box::new(future::err(()));
}; };
@@ -334,13 +353,25 @@ impl<P, E> Worker for CollationNode<P, E> where
exit.clone(), exit.clone(),
message_validator, message_validator,
client.clone(), client.clone(),
task_executor, task_executor.clone(),
)); ));
let parachain_context = build_parachain_context.build(validation_network.clone()).unwrap(); let parachain_context = match build_parachain_context.build(
client.clone(),
task_executor,
validation_network.clone(),
) {
Ok(ctx) => ctx,
Err(()) => {
error!("Could not build the parachain context!");
return Box::new(future::err(()))
}
};
let inner_exit = exit.clone(); let inner_exit = exit.clone();
let work = client.import_notification_stream() let work = client.import_notification_stream()
.map(|v| Ok::<_, ()>(v)).compat() .map(|v| Ok::<_, ()>(v))
.compat()
.for_each(move |notification| { .for_each(move |notification| {
macro_rules! try_fr { macro_rules! try_fr {
($e:expr) => { ($e:expr) => {
@@ -10,6 +10,7 @@ parachain = { package = "polkadot-parachain", path = "../../../parachain" }
collator = { package = "polkadot-collator", path = "../../../collator" } collator = { package = "polkadot-collator", path = "../../../collator" }
primitives = { package = "polkadot-primitives", path = "../../../primitives" } primitives = { package = "polkadot-primitives", path = "../../../primitives" }
substrate-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } substrate-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
client = { package = "substrate-client", git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
parking_lot = "0.9.0" parking_lot = "0.9.0"
ctrlc = { version = "3.0", features = ["termination"] } ctrlc = { version = "3.0", features = ["termination"] }
futures = "0.1" futures = "0.1"
@@ -21,13 +21,17 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use adder::{HeadData as AdderHead, BlockData as AdderBody}; use adder::{HeadData as AdderHead, BlockData as AdderBody};
use substrate_primitives::Pair; use substrate_primitives::{Pair, Blake2Hasher};
use parachain::codec::{Encode, Decode}; use parachain::codec::{Encode, Decode};
use primitives::{ use primitives::{
Hash, Hash, Block,
parachain::{HeadData, BlockData, Id as ParaId, Message, OutgoingMessages, Status as ParachainStatus}, parachain::{
HeadData, BlockData, Id as ParaId, Message, OutgoingMessages, Status as ParachainStatus,
},
};
use collator::{
InvalidHead, ParachainContext, VersionInfo, Network, BuildParachainContext, TaskExecutor,
}; };
use collator::{InvalidHead, ParachainContext, VersionInfo, Network, BuildParachainContext};
use parking_lot::Mutex; use parking_lot::Mutex;
const GENESIS: AdderHead = AdderHead { const GENESIS: AdderHead = AdderHead {
@@ -101,7 +105,16 @@ impl ParachainContext for AdderContext {
impl BuildParachainContext for AdderContext { impl BuildParachainContext for AdderContext {
type ParachainContext = Self; type ParachainContext = Self;
fn build(self, network: Arc<dyn Network>) -> Result<Self::ParachainContext, ()> { fn build<B, E>(
self,
_: Arc<collator::PolkadotClient<B, E>>,
_: TaskExecutor,
network: Arc<dyn Network>,
) -> Result<Self::ParachainContext, ()>
where
B: client::backend::Backend<Block, Blake2Hasher> + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static
{
Ok(Self { _network: Some(network), ..self }) Ok(Self { _network: Some(network), ..self })
} }
} }