From bc59254f41100ff5b3a78d77865b02a1ffdb27f9 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Mon, 17 Jun 2019 08:35:01 +0200 Subject: [PATCH] allow asynchronous collation (#290) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * allow asynchronous collation * remove unnecessary leading colons Co-Authored-By: Bastian Köcher * remove unneeded bound * Fixes compilation --- polkadot/collator/src/lib.rs | 74 +++++++++++-------- .../adder/collator/src/main.rs | 2 + 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/polkadot/collator/src/lib.rs b/polkadot/collator/src/lib.rs index a95709148a..f6ac899a28 100644 --- a/polkadot/collator/src/lib.rs +++ b/polkadot/collator/src/lib.rs @@ -98,12 +98,14 @@ impl fmt::Display for Error { /// This can be implemented through an externally attached service or a stub. /// This is expected to be a lightweight, shared type like an Arc. pub trait ParachainContext: Clone { + type ProduceCandidate: IntoFuture; + /// Produce a candidate, given the latest ingress queue information and the last parachain head. fn produce_candidate>( &self, last_head: HeadData, ingress: I, - ) -> Result<(BlockData, HeadData, Extrinsic), InvalidHead>; + ) -> Self::ProduceCandidate; } /// Relay chain context needed to collate. @@ -134,38 +136,44 @@ pub fn collate<'a, R, P>( R::Error: 'a, R::FutureEgress: 'a, P: ParachainContext + 'a, + ::Future: Send, { let ingress = relay_context.unrouted_egress(local_id).into_future().map_err(Error::Polkadot); - ingress.and_then(move |ingress| { - let (block_data, head_data, mut extrinsic) = para_context.produce_candidate( - last_head, - ingress.0.iter().flat_map(|&(id, ref msgs)| msgs.iter().cloned().map(move |msg| (id, msg))) - ).map_err(Error::Collator)?; - - let block_data_hash = block_data.hash(); - let signature = key.sign(block_data_hash.as_ref()).into(); - let egress_queue_roots = - ::polkadot_validation::egress_roots(&mut extrinsic.outgoing_messages); - - let receipt = parachain::CandidateReceipt { - parachain_index: local_id, - collator: key.public(), - signature, - head_data, - egress_queue_roots, - fees: 0, - block_data_hash, - upward_messages: Vec::new(), - }; - - Ok(parachain::Collation { - receipt, - pov: PoVBlock { - block_data, - ingress, - }, + ingress + .and_then(move |ingress| { + para_context.produce_candidate( + last_head, + ingress.0.iter().flat_map(|&(id, ref msgs)| msgs.iter().cloned().map(move |msg| (id, msg))) + ) + .into_future() + .map(move |x| (ingress, x)) + .map_err(Error::Collator) + }) + .and_then(move |(ingress, (block_data, head_data, mut extrinsic))| { + let block_data_hash = block_data.hash(); + let signature = key.sign(block_data_hash.as_ref()).into(); + let egress_queue_roots = + polkadot_validation::egress_roots(&mut extrinsic.outgoing_messages); + + let receipt = parachain::CandidateReceipt { + parachain_index: local_id, + collator: key.public(), + signature, + head_data, + egress_queue_roots, + fees: 0, + block_data_hash, + upward_messages: Vec::new(), + }; + + Ok(parachain::Collation { + receipt, + pov: PoVBlock { + block_data, + ingress, + }, + }) }) - }) } /// Polkadot-api context. @@ -216,7 +224,8 @@ impl IntoExit for CollationNode where impl Worker for CollationNode where P: ParachainContext + Send + 'static, - E: Future + Clone + Send + Sync + 'static + E: Future + Clone + Send + Sync + 'static, + ::Future: Send + 'static, { type Work = Box + Send>; @@ -376,6 +385,7 @@ pub fn run_collator( version: VersionInfo, ) -> polkadot_cli::error::Result<()> where P: ParachainContext + Send + 'static, + ::Future: Send + 'static, E: IntoFuture, E::Future: Send + Clone + Sync + 'static, I: IntoIterator, @@ -413,6 +423,8 @@ mod tests { struct DummyParachainContext; impl ParachainContext for DummyParachainContext { + type ProduceCandidate = Result<(BlockData, HeadData, Extrinsic), InvalidHead>; + fn produce_candidate>( &self, _last_head: HeadData, diff --git a/polkadot/test-parachains/adder/collator/src/main.rs b/polkadot/test-parachains/adder/collator/src/main.rs index fd93b44e59..4b678b0115 100644 --- a/polkadot/test-parachains/adder/collator/src/main.rs +++ b/polkadot/test-parachains/adder/collator/src/main.rs @@ -45,6 +45,8 @@ struct AdderContext { /// The parachain context. impl ParachainContext for AdderContext { + type ProduceCandidate = Result<(BlockData, HeadData, Extrinsic), InvalidHead>; + fn produce_candidate>( &self, last_head: HeadData,