diff --git a/Cargo.lock b/Cargo.lock index 4a4c4b2aa82a..4c5883b9d459 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,6 +24,7 @@ dependencies = [ "polkadot-collator 0.6.0", "polkadot-parachain 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)", ] diff --git a/collator/src/lib.rs b/collator/src/lib.rs index 0f6b0febea62..57b026721381 100644 --- a/collator/src/lib.rs +++ b/collator/src/lib.rs @@ -51,25 +51,25 @@ use std::time::Duration; use futures::{future, Stream, Future, IntoFuture}; use futures03::{TryStreamExt as _, StreamExt as _}; -use log::{info, warn}; +use log::{warn, error}; use client::BlockchainEvents; -use primitives::Pair; +use primitives::{Pair, Blake2Hasher}; use polkadot_primitives::{ BlockId, Hash, Block, parachain::{ - self, BlockData, DutyRoster, HeadData, ConsolidatedIngress, Message, Id as ParaId, OutgoingMessages, - PoVBlock, Status as ParachainStatus, ValidatorId, CollatorPair, + self, BlockData, DutyRoster, HeadData, ConsolidatedIngress, Message, Id as ParaId, + OutgoingMessages, PoVBlock, Status as ParachainStatus, ValidatorId, CollatorPair, } }; use polkadot_cli::{ - Worker, IntoExit, ProvideRuntimeApi, TaskExecutor, AbstractService, - CustomConfiguration, ParachainHost, + Worker, IntoExit, ProvideRuntimeApi, AbstractService, CustomConfiguration, ParachainHost, }; use polkadot_network::validation::{LeafWorkParams, ValidationNetwork}; use polkadot_network::{PolkadotNetworkService, PolkadotProtocol}; +use polkadot_runtime::RuntimeApi; use tokio::timer::Timeout; -pub use polkadot_cli::VersionInfo; +pub use polkadot_cli::{VersionInfo, TaskExecutor}; pub use polkadot_network::validation::Incoming; pub use polkadot_validation::SignedStatement; pub use polkadot_primitives::parachain::CollatorId; @@ -128,13 +128,24 @@ impl fmt::Display for Error { } } +/// The Polkadot client type. +pub type PolkadotClient = client::Client; + /// Something that can build a `ParachainContext`. pub trait BuildParachainContext { /// The parachain context produced by the `build` function. type ParachainContext: self::ParachainContext; /// Build the `ParachainContext`. - fn build(self, network: Arc) -> Result; + fn build( + self, + client: Arc>, + task_executor: TaskExecutor, + network: Arc, + ) -> Result + where + B: client::backend::Backend + 'static, + E: client::CallExecutor + Clone + Send + Sync + 'static; } /// Parachain context needed for collation. @@ -289,10 +300,18 @@ impl Worker for CollationNode where } fn work(self, service: &S, task_executor: TaskExecutor) -> Self::Work - where S: AbstractService, - SC: polkadot_service::SelectChain + 'static, - B: polkadot_service::Backend + 'static, - CE: polkadot_service::CallExecutor + Clone + Send + Sync + 'static + where + S: AbstractService< + Block = Block, + RuntimeApi = RuntimeApi, + Backend = B, + SelectChain = SC, + NetworkSpecialization = PolkadotProtocol, + CallExecutor = CE, + >, + SC: polkadot_service::SelectChain + 'static, + B: client::backend::Backend + 'static, + CE: client::CallExecutor + Clone + Send + Sync + 'static { let CollationNode { build_parachain_context, exit, para_id, key } = self; let client = service.client(); @@ -301,7 +320,7 @@ impl Worker for CollationNode where let select_chain = if let Some(select_chain) = service.select_chain() { select_chain } 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(())); }; @@ -334,13 +353,25 @@ impl Worker for CollationNode where exit.clone(), message_validator, 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 work = client.import_notification_stream() - .map(|v| Ok::<_, ()>(v)).compat() + .map(|v| Ok::<_, ()>(v)) + .compat() .for_each(move |notification| { macro_rules! try_fr { ($e:expr) => { diff --git a/test-parachains/adder/collator/Cargo.toml b/test-parachains/adder/collator/Cargo.toml index 4c4412f3db84..540a3505eebd 100644 --- a/test-parachains/adder/collator/Cargo.toml +++ b/test-parachains/adder/collator/Cargo.toml @@ -10,6 +10,7 @@ parachain = { package = "polkadot-parachain", path = "../../../parachain" } collator = { package = "polkadot-collator", path = "../../../collator" } primitives = { package = "polkadot-primitives", path = "../../../primitives" } 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" ctrlc = { version = "3.0", features = ["termination"] } futures = "0.1" diff --git a/test-parachains/adder/collator/src/main.rs b/test-parachains/adder/collator/src/main.rs index 473387d92911..6803131b89e6 100644 --- a/test-parachains/adder/collator/src/main.rs +++ b/test-parachains/adder/collator/src/main.rs @@ -21,13 +21,17 @@ use std::collections::HashMap; use std::sync::Arc; use adder::{HeadData as AdderHead, BlockData as AdderBody}; -use substrate_primitives::Pair; +use substrate_primitives::{Pair, Blake2Hasher}; use parachain::codec::{Encode, Decode}; use primitives::{ - Hash, - parachain::{HeadData, BlockData, Id as ParaId, Message, OutgoingMessages, Status as ParachainStatus}, + Hash, Block, + 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; const GENESIS: AdderHead = AdderHead { @@ -101,7 +105,16 @@ impl ParachainContext for AdderContext { impl BuildParachainContext for AdderContext { type ParachainContext = Self; - fn build(self, network: Arc) -> Result { + fn build( + self, + _: Arc>, + _: TaskExecutor, + network: Arc, + ) -> Result + where + B: client::backend::Backend + 'static, + E: client::CallExecutor + Clone + Send + Sync + 'static + { Ok(Self { _network: Some(network), ..self }) } }