Skip to content

Commit 1173a4c

Browse files
rphmeierbkchr
authored andcommitted
allow asynchronous collation (paritytech#290)
* allow asynchronous collation * remove unnecessary leading colons Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * remove unneeded bound * Fixes compilation
1 parent 8079602 commit 1173a4c

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

collator/src/lib.rs

+43-31
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ impl<R: fmt::Display> fmt::Display for Error<R> {
9898
/// This can be implemented through an externally attached service or a stub.
9999
/// This is expected to be a lightweight, shared type like an Arc.
100100
pub trait ParachainContext: Clone {
101+
type ProduceCandidate: IntoFuture<Item=(BlockData, HeadData, Extrinsic), Error=InvalidHead>;
102+
101103
/// Produce a candidate, given the latest ingress queue information and the last parachain head.
102104
fn produce_candidate<I: IntoIterator<Item=(ParaId, Message)>>(
103105
&self,
104106
last_head: HeadData,
105107
ingress: I,
106-
) -> Result<(BlockData, HeadData, Extrinsic), InvalidHead>;
108+
) -> Self::ProduceCandidate;
107109
}
108110

109111
/// Relay chain context needed to collate.
@@ -134,38 +136,44 @@ pub fn collate<'a, R, P>(
134136
R::Error: 'a,
135137
R::FutureEgress: 'a,
136138
P: ParachainContext + 'a,
139+
<P::ProduceCandidate as IntoFuture>::Future: Send,
137140
{
138141
let ingress = relay_context.unrouted_egress(local_id).into_future().map_err(Error::Polkadot);
139-
ingress.and_then(move |ingress| {
140-
let (block_data, head_data, mut extrinsic) = para_context.produce_candidate(
141-
last_head,
142-
ingress.0.iter().flat_map(|&(id, ref msgs)| msgs.iter().cloned().map(move |msg| (id, msg)))
143-
).map_err(Error::Collator)?;
144-
145-
let block_data_hash = block_data.hash();
146-
let signature = key.sign(block_data_hash.as_ref()).into();
147-
let egress_queue_roots =
148-
::polkadot_validation::egress_roots(&mut extrinsic.outgoing_messages);
149-
150-
let receipt = parachain::CandidateReceipt {
151-
parachain_index: local_id,
152-
collator: key.public(),
153-
signature,
154-
head_data,
155-
egress_queue_roots,
156-
fees: 0,
157-
block_data_hash,
158-
upward_messages: Vec::new(),
159-
};
160-
161-
Ok(parachain::Collation {
162-
receipt,
163-
pov: PoVBlock {
164-
block_data,
165-
ingress,
166-
},
142+
ingress
143+
.and_then(move |ingress| {
144+
para_context.produce_candidate(
145+
last_head,
146+
ingress.0.iter().flat_map(|&(id, ref msgs)| msgs.iter().cloned().map(move |msg| (id, msg)))
147+
)
148+
.into_future()
149+
.map(move |x| (ingress, x))
150+
.map_err(Error::Collator)
151+
})
152+
.and_then(move |(ingress, (block_data, head_data, mut extrinsic))| {
153+
let block_data_hash = block_data.hash();
154+
let signature = key.sign(block_data_hash.as_ref()).into();
155+
let egress_queue_roots =
156+
polkadot_validation::egress_roots(&mut extrinsic.outgoing_messages);
157+
158+
let receipt = parachain::CandidateReceipt {
159+
parachain_index: local_id,
160+
collator: key.public(),
161+
signature,
162+
head_data,
163+
egress_queue_roots,
164+
fees: 0,
165+
block_data_hash,
166+
upward_messages: Vec::new(),
167+
};
168+
169+
Ok(parachain::Collation {
170+
receipt,
171+
pov: PoVBlock {
172+
block_data,
173+
ingress,
174+
},
175+
})
167176
})
168-
})
169177
}
170178

171179
/// Polkadot-api context.
@@ -216,7 +224,8 @@ impl<P, E> IntoExit for CollationNode<P, E> where
216224

217225
impl<P, E> Worker for CollationNode<P, E> where
218226
P: ParachainContext + Send + 'static,
219-
E: Future<Item=(),Error=()> + Clone + Send + Sync + 'static
227+
E: Future<Item=(),Error=()> + Clone + Send + Sync + 'static,
228+
<P::ProduceCandidate as IntoFuture>::Future: Send + 'static,
220229
{
221230
type Work = Box<Future<Item=(),Error=()> + Send>;
222231

@@ -376,6 +385,7 @@ pub fn run_collator<P, E, I, ArgT>(
376385
version: VersionInfo,
377386
) -> polkadot_cli::error::Result<()> where
378387
P: ParachainContext + Send + 'static,
388+
<P::ProduceCandidate as IntoFuture>::Future: Send + 'static,
379389
E: IntoFuture<Item=(),Error=()>,
380390
E::Future: Send + Clone + Sync + 'static,
381391
I: IntoIterator<Item=ArgT>,
@@ -413,6 +423,8 @@ mod tests {
413423
struct DummyParachainContext;
414424

415425
impl ParachainContext for DummyParachainContext {
426+
type ProduceCandidate = Result<(BlockData, HeadData, Extrinsic), InvalidHead>;
427+
416428
fn produce_candidate<I: IntoIterator<Item=(ParaId, Message)>>(
417429
&self,
418430
_last_head: HeadData,

test-parachains/adder/collator/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct AdderContext {
4545

4646
/// The parachain context.
4747
impl ParachainContext for AdderContext {
48+
type ProduceCandidate = Result<(BlockData, HeadData, Extrinsic), InvalidHead>;
49+
4850
fn produce_candidate<I: IntoIterator<Item=(ParaId, Message)>>(
4951
&self,
5052
last_head: HeadData,

0 commit comments

Comments
 (0)