Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Pass client and task_executor to BuildParachainContext #484

Merged
merged 2 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 47 additions & 16 deletions collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`.
pub trait BuildParachainContext {
/// The parachain context produced by the `build` function.
type ParachainContext: self::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.
Expand Down Expand Up @@ -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
where S: AbstractService<Block = polkadot_service::Block, RuntimeApi = polkadot_service::RuntimeApi, Backend = B, SelectChain = SC, NetworkSpecialization = PolkadotProtocol, CallExecutor = CE>,
SC: polkadot_service::SelectChain<polkadot_service::Block> + 'static,
B: polkadot_service::Backend<polkadot_service::Block, polkadot_service::Blake2Hasher> + 'static,
CE: polkadot_service::CallExecutor<polkadot_service::Block, polkadot_service::Blake2Hasher> + Clone + Send + Sync + 'static
where
S: AbstractService<
Block = Block,
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 client = service.client();
Expand All @@ -301,7 +320,7 @@ impl<P, E> Worker for CollationNode<P, E> 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(()));
};

Expand Down Expand Up @@ -334,13 +353,25 @@ impl<P, E> Worker for CollationNode<P, E> 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(),
) {
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
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) => {
Expand Down
1 change: 1 addition & 0 deletions test-parachains/adder/collator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 18 additions & 5 deletions test-parachains/adder/collator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -101,7 +105,16 @@ impl ParachainContext for AdderContext {
impl BuildParachainContext for AdderContext {
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 })
}
}
Expand Down