Switch consensus crates to new futures (#3146)

* Switch consensus-common to new futures

* Fix tests

* More tests fixing

* Fix Babe tests

* Fix Babe tests
This commit is contained in:
Pierre Krieger
2019-07-25 04:55:50 +02:00
committed by Gavin Wood
parent 3a6a309d84
commit b31dcdf342
21 changed files with 208 additions and 184 deletions
+27 -24
View File
@@ -16,16 +16,15 @@
//! Utility stream for yielding slots in a loop.
//!
//! This is used instead of `tokio_timer::Interval` because it was unreliable.
//! This is used instead of `futures_timer::Interval` because it was unreliable.
use super::SlotCompatible;
use consensus_common::Error;
use futures::prelude::*;
use futures::try_ready;
use futures::{prelude::*, task::Context, task::Poll};
use inherents::{InherentData, InherentDataProviders};
use std::time::{Duration, Instant};
use tokio_timer::Delay;
use std::{pin::Pin, time::{Duration, Instant}};
use futures_timer::Delay;
/// Returns current duration since unix epoch.
pub fn duration_now() -> Duration {
@@ -121,47 +120,51 @@ impl<SC> Slots<SC> {
}
}
impl<SC: SlotCompatible> Stream for Slots<SC> {
type Item = SlotInfo;
type Error = Error;
impl<SC: SlotCompatible + Unpin> Stream for Slots<SC> {
type Item = Result<SlotInfo, Error>;
fn poll(&mut self) -> Poll<Option<SlotInfo>, Self::Error> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
loop {
let slot_duration = self.slot_duration;
self.inner_delay = match self.inner_delay.take() {
None => {
// schedule wait.
let wait_until = Instant::now() + time_until_next(duration_now(), slot_duration);
Some(Delay::new(wait_until))
let wait_dur = time_until_next(duration_now(), slot_duration);
Some(Delay::new(wait_dur))
}
Some(d) => Some(d),
};
if let Some(ref mut inner_delay) = self.inner_delay {
try_ready!(inner_delay
.poll()
.map_err(Error::FaultyTimer));
match Future::poll(Pin::new(inner_delay), cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(err)) => return Poll::Ready(Some(Err(Error::FaultyTimer(err)))),
Poll::Ready(Ok(())) => {}
}
}
// timeout has fired.
let inherent_data = self
.inherent_data_providers
.create_inherent_data()
.map_err(|s| consensus_common::Error::InherentData(s.into_owned()))?;
let (timestamp, slot_num, offset) = self
.timestamp_extractor
.extract_timestamp_and_slot(&inherent_data)?;
let inherent_data = match self.inherent_data_providers.create_inherent_data() {
Ok(id) => id,
Err(err) => return Poll::Ready(Some(Err(consensus_common::Error::InherentData(err.into_owned())))),
};
let result = self.timestamp_extractor.extract_timestamp_and_slot(&inherent_data);
let (timestamp, slot_num, offset) = match result {
Ok(v) => v,
Err(err) => return Poll::Ready(Some(Err(err))),
};
// reschedule delay for next slot.
let ends_at = Instant::now() + offset +
let ends_in = offset +
time_until_next(Duration::from_secs(timestamp), slot_duration);
self.inner_delay = Some(Delay::new(ends_at));
let ends_at = Instant::now() + ends_in;
self.inner_delay = Some(Delay::new(ends_in));
// never yield the same slot twice.
if slot_num > self.last_slot {
self.last_slot = slot_num;
break Ok(Async::Ready(Some(SlotInfo {
break Poll::Ready(Some(Ok(SlotInfo {
number: slot_num,
duration: self.slot_duration,
timestamp,