update to latest substrate - protocol API update (#130)

* update to latest substrate - protocol API update

* nits

* remove the wait on a future

* use std mpsc for oneshot
This commit is contained in:
Gregory Terzian
2019-02-10 00:52:57 +08:00
committed by Gav Wood
parent 9fb22ce28d
commit cdb2faf155
5 changed files with 358 additions and 289 deletions
+32 -19
View File
@@ -170,14 +170,10 @@ impl<P: ProvideRuntimeApi + Send + Sync + 'static> Router<P>
produced.extrinsic,
);
let mut gossip = network.consensus_gossip().write();
// propagate the statement.
// consider something more targeted than gossip in the future.
let signed = table.sign_and_import(produced.validity);
network.with_spec(|_, ctx|
gossip.multicast(ctx, attestation_topic, signed.encode(), false)
);
network.gossip_consensus_message(attestation_topic, signed.encode(), false);
})
.map_err(|e| debug!(target: "p_net", "Failed to produce statements: {:?}", e))
}
@@ -195,29 +191,32 @@ impl<P: ProvideRuntimeApi + Send> TableRouter for Router<P>
let candidate = self.table.sign_and_import(GenericStatement::Candidate(receipt));
self.knowledge.lock().note_candidate(hash, Some(block_data), Some(extrinsic));
let mut gossip = self.network.consensus_gossip().write();
self.network.with_spec(|_spec, ctx| {
gossip.multicast(ctx, self.attestation_topic, candidate.encode(), false);
});
self.network.gossip_consensus_message(self.attestation_topic, candidate.encode(), false);
}
fn fetch_block_data(&self, candidate: &CandidateReceipt) -> BlockDataReceiver {
let parent_hash = self.parent_hash;
let rx = self.network.with_spec(|spec, ctx| { spec.fetch_block_data(ctx, candidate, parent_hash) });
BlockDataReceiver { inner: rx }
let parent_hash = self.parent_hash.clone();
let candidate = candidate.clone();
let (tx, rx) = ::futures::sync::oneshot::channel();
self.network.with_spec(move |spec, ctx| {
let inner_rx = spec.fetch_block_data(ctx, &candidate, parent_hash);
let _ = tx.send(inner_rx);
});
BlockDataReceiver { outer: rx, inner: None }
}
}
impl<P> Drop for Router<P> {
fn drop(&mut self) {
let parent_hash = &self.parent_hash;
self.network.with_spec(|spec, _| spec.remove_consensus(parent_hash));
let parent_hash = self.parent_hash.clone();
self.network.with_spec(move |spec, _| spec.remove_consensus(&parent_hash));
}
}
/// Receiver for block data.
pub struct BlockDataReceiver {
inner: ::futures::sync::oneshot::Receiver<BlockData>,
outer: ::futures::sync::oneshot::Receiver<::futures::sync::oneshot::Receiver<BlockData>>,
inner: Option<::futures::sync::oneshot::Receiver<BlockData>>
}
impl Future for BlockDataReceiver {
@@ -225,10 +224,24 @@ impl Future for BlockDataReceiver {
type Error = io::Error;
fn poll(&mut self) -> Poll<BlockData, io::Error> {
self.inner.poll().map_err(|_| io::Error::new(
io::ErrorKind::Other,
"Sending end of channel hung up",
))
if let Some(ref mut inner) = self.inner {
return inner
.poll()
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"Sending end of channel hung up",
))
}
if let Ok(futures::Async::Ready(mut inner)) = self.outer.poll() {
let poll_result = inner.poll();
self.inner = Some(inner);
return poll_result
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"Sending end of channel hung up",
))
}
Ok(futures::Async::NotReady)
}
}