Update futures and tokio for browser light client (#673)

* Make availability-store compile for WASM

* Use --manifest-path instead

* Make validation work on wasm!

* Switch to Spawn trait

* Migrate validation to std futures

* Migrate network to std futures

* Final changes to validation

* Tidy up network

* Tidy up validation

* Switch branch

* Migrate service

* Get polkadot to compile via wasm!

* Add browser-demo

* Add initial browser file

* Add browser-demo

* Tidy

* Temp switch back to substrate/master

* tidy

* Fix wasm build

* Re-add release flag

* Switch to polkadot-master

* Revert cli tokio version to avoid libp2p panic

* Update tokio version

* Fix availability store tests

* Fix validation tests

* Remove futures01 from availability-store

* Fix network tests

* Small changes

* Fix collator

* Fix typo

* Revert removal of tokio_executor that causes tokio version mismatch panic

* Fix adder test parachain

* Revert "Revert removal of tokio_executor that causes tokio version mismatch panic"

This reverts commit cfeb50c01d8df5e209483406a711e64761b44ae9.

* Update availability-store/src/worker.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Update network/src/lib.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Update network/src/lib.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Box pin changes

* Asyncify network functions

* Clean up browser validation worker error

* Fix av store test

* Nits

* Fix validation test

* Switch favicon

* Fix validation test again

* Revert "Asyncify network functions"

This reverts commit f20ae6548dc482cb1e75bc80641cfe55c6131a53.

* Add async blocks back in
This commit is contained in:
Ashley
2019-12-10 11:58:22 +01:00
committed by GitHub
parent df3ea965e7
commit 25aa988f9b
34 changed files with 826 additions and 444 deletions
+2 -3
View File
@@ -6,8 +6,7 @@ description = "Collator node implementation"
edition = "2018"
[dependencies]
futures01 = { package = "futures", version = "0.1.17" }
futures = { version = "0.3.1", features = ["compat"] }
futures = "0.3.1"
client = { package = "sc-client", git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
client-api = { package = "sc-client-api", git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
primitives = { package = "sp-core", git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
@@ -20,7 +19,7 @@ polkadot-network = { path = "../network" }
polkadot-validation = { path = "../validation" }
polkadot-service = { path = "../service" }
log = "0.4.8"
tokio = "0.1.22"
tokio = "0.2.1"
futures-timer = "1.0"
[dev-dependencies]
+25 -34
View File
@@ -49,11 +49,7 @@ use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use futures::{
future, Future, Stream, FutureExt, TryFutureExt, StreamExt,
compat::{Future01CompatExt, Stream01CompatExt}
};
use futures01::{Future as _};
use futures::{future, Future, Stream, FutureExt, TryFutureExt, StreamExt, task::Spawn};
use log::{warn, error};
use client::BlockchainEvents;
use primitives::{Pair, Blake2Hasher};
@@ -71,7 +67,7 @@ use polkadot_network::validation::{LeafWorkParams, ValidationNetwork};
use polkadot_network::{PolkadotNetworkService, PolkadotProtocol};
use polkadot_runtime::RuntimeApi;
pub use polkadot_cli::{VersionInfo, TaskExecutor};
pub use polkadot_cli::VersionInfo;
pub use polkadot_network::validation::Incoming;
pub use polkadot_validation::SignedStatement;
pub use polkadot_primitives::parachain::CollatorId;
@@ -83,7 +79,7 @@ const COLLATION_TIMEOUT: Duration = Duration::from_secs(30);
pub trait Network: Send + Sync {
/// Convert the given `CollatorId` to a `PeerId`.
fn collator_id_to_peer_id(&self, collator_id: CollatorId) ->
Box<dyn Future<Output=Option<PeerId>> + Unpin + Send>;
Box<dyn Future<Output=Option<PeerId>> + Send>;
/// Create a `Stream` of checked statements for the given `relay_parent`.
///
@@ -93,26 +89,19 @@ pub trait Network: Send + Sync {
fn checked_statements(&self, relay_parent: Hash) -> Box<dyn Stream<Item=SignedStatement>>;
}
impl<P, E> Network for ValidationNetwork<P, E, PolkadotNetworkService, TaskExecutor> where
impl<P, E, SP> Network for ValidationNetwork<P, E, PolkadotNetworkService, SP> where
P: 'static + Send + Sync,
E: 'static + Send + Sync,
SP: 'static + Spawn + Clone + Send + Sync,
{
fn collator_id_to_peer_id(&self, collator_id: CollatorId) ->
Box<dyn Future<Output=Option<PeerId>> + Unpin + Send>
Box<dyn Future<Output=Option<PeerId>> + Send>
{
Box::new(
Self::collator_id_to_peer_id(self, collator_id)
.compat()
.map(|res| res.ok().and_then(|id| id))
)
Box::new(Self::collator_id_to_peer_id(self, collator_id))
}
fn checked_statements(&self, relay_parent: Hash) -> Box<dyn Stream<Item=SignedStatement>> {
Box::new(
Self::checked_statements(self, relay_parent)
.compat()
.filter_map(|item| future::ready(item.ok()))
)
Box::new(Self::checked_statements(self, relay_parent))
}
}
@@ -147,15 +136,16 @@ pub trait BuildParachainContext {
type ParachainContext: self::ParachainContext;
/// Build the `ParachainContext`.
fn build<B, E>(
fn build<B, E, SP>(
self,
client: Arc<PolkadotClient<B, E>>,
task_executor: TaskExecutor,
spawner: SP,
network: Arc<dyn Network>,
) -> Result<Self::ParachainContext, ()>
where
B: client_api::backend::Backend<Block, Blake2Hasher> + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static;
E: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static,
SP: Spawn + Clone + Send + Sync + 'static;
}
/// Parachain context needed for collation.
@@ -239,16 +229,17 @@ pub async fn collate<R, P>(
}
/// Polkadot-api context.
struct ApiContext<P, E> {
network: Arc<ValidationNetwork<P, E, PolkadotNetworkService, TaskExecutor>>,
struct ApiContext<P, E, SP> {
network: Arc<ValidationNetwork<P, E, PolkadotNetworkService, SP>>,
parent_hash: Hash,
validators: Vec<ValidatorId>,
}
impl<P: 'static, E: 'static> RelayChainContext for ApiContext<P, E> where
impl<P: 'static, E: 'static, SP: 'static> RelayChainContext for ApiContext<P, E, SP> where
P: ProvideRuntimeApi + Send + Sync,
P::Api: ParachainHost<Block>,
E: futures::Future<Output=()> + Clone + Send + Sync + 'static,
SP: Spawn + Clone + Send + Sync
{
type Error = String;
type FutureEgress = Box<dyn Future<Output=Result<ConsolidatedIngress, String>> + Unpin + Send>;
@@ -262,7 +253,6 @@ impl<P: 'static, E: 'static> RelayChainContext for ApiContext<P, E> where
parent_hash: self.parent_hash,
authorities: self.validators.clone(),
})
.compat()
.map_err(|e| format!("unable to instantiate validation session: {:?}", e));
Box::new(future::ok(ConsolidatedIngress(Vec::new())))
@@ -302,7 +292,7 @@ impl<P, E> Worker for CollationNode<P, E> where
config
}
fn work<S, SC, B, CE>(self, service: &S, task_executor: TaskExecutor) -> Self::Work
fn work<S, SC, B, CE, SP>(self, service: &S, spawner: SP) -> Self::Work
where
S: AbstractService<
Block = Block,
@@ -314,7 +304,8 @@ impl<P, E> Worker for CollationNode<P, E> where
>,
SC: polkadot_service::SelectChain<Block> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + 'static,
CE: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static
CE: client::CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static,
SP: Spawn + Clone + Send + Sync + 'static,
{
let CollationNode { build_parachain_context, exit, para_id, key } = self;
let client = service.client();
@@ -356,12 +347,12 @@ impl<P, E> Worker for CollationNode<P, E> where
exit.clone(),
message_validator,
client.clone(),
task_executor.clone(),
spawner.clone(),
));
let parachain_context = match build_parachain_context.build(
client.clone(),
task_executor,
spawner,
validation_network.clone(),
) {
Ok(ctx) => ctx,
@@ -433,13 +424,13 @@ impl<P, E> Worker for CollationNode<P, E> where
outgoing,
);
let exit = inner_exit_2.clone().unit_error().compat();
tokio::spawn(res.select(exit).then(|_| Ok(())));
let exit = inner_exit_2.clone();
tokio::spawn(future::select(res, exit).map(drop));
})
});
future::Either::Right(collation_work)
}).map(|_| Ok::<_, ()>(()));
});
let deadlined = future::select(
work,
@@ -456,7 +447,7 @@ impl<P, E> Worker for CollationNode<P, E> where
let future = future::select(
silenced,
inner_exit.clone()
).map(|_| Ok::<_, ()>(())).compat();
).map(drop);
tokio::spawn(future);
future::ready(())