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
+38 -34
View File
@@ -28,7 +28,7 @@
//!
//! NOTE: Aura itself is designed to be generic over the crypto used.
#![forbid(missing_docs, unsafe_code)]
use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug};
use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug, pin::Pin};
use parity_codec::{Encode, Decode, Codec};
use consensus_common::{self, BlockImport, Environment, Proposer,
@@ -52,9 +52,9 @@ use runtime_primitives::traits::{Block as BlockT, Header, DigestItemFor, Provide
use primitives::Pair;
use inherents::{InherentDataProviders, InherentData};
use futures::{Future, IntoFuture, future};
use futures::{prelude::*, future};
use parking_lot::Mutex;
use tokio_timer::Timeout;
use futures_timer::Delay;
use log::{error, warn, debug, info, trace};
use srml_aura::{
@@ -128,7 +128,7 @@ impl SlotCompatible for AuraSlotCompatible {
}
}
/// Start the aura worker. The returned future should be run in a tokio runtime.
/// Start the aura worker. The returned future should be run in a futures executor.
pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
slot_duration: SlotDuration,
local_key: Arc<P>,
@@ -139,13 +139,13 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
sync_oracle: SO,
inherent_data_providers: InherentDataProviders,
force_authoring: bool,
) -> Result<impl Future<Item=(), Error=()>, consensus_common::Error> where
) -> Result<impl futures01::Future<Item = (), Error = ()>, consensus_common::Error> where
B: BlockT<Header=H>,
C: ProvideRuntimeApi + ProvideCache<B> + AuxStore + Send + Sync,
C::Api: AuraApi<B, AuthorityId<P>>,
SC: SelectChain<B>,
E::Proposer: Proposer<B, Error=Error>,
<<E::Proposer as Proposer<B>>::Create as IntoFuture>::Future: Send + 'static,
<E::Proposer as Proposer<B>>::Create: Unpin + Send + 'static,
P: Pair + Send + Sync + 'static,
P::Public: Hash + Member + Encode + Decode,
P::Signature: Hash + Member + Encode + Decode,
@@ -174,7 +174,7 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
sync_oracle,
inherent_data_providers,
AuraSlotCompatible,
))
).map(|()| Ok::<(), ()>(())).compat())
}
struct AuraWorker<C, E, I, P, SO> {
@@ -192,7 +192,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
C::Api: AuraApi<B, AuthorityId<P>>,
E: Environment<B, Error=Error>,
E::Proposer: Proposer<B, Error=Error>,
<<E::Proposer as Proposer<B>>::Create as IntoFuture>::Future: Send + 'static,
<E::Proposer as Proposer<B>>::Create: Unpin + Send + 'static,
H: Header<Hash=B::Hash>,
I: BlockImport<B> + Send + Sync + 'static,
P: Pair + Send + Sync + 'static,
@@ -201,7 +201,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
SO: SyncOracle + Send + Clone,
Error: ::std::error::Error + Send + From<::consensus_common::Error> + From<I::Error> + 'static,
{
type OnSlot = Box<dyn Future<Item=(), Error=consensus_common::Error> + Send>;
type OnSlot = Pin<Box<dyn Future<Output = Result<(), consensus_common::Error>> + Send>>;
fn on_slot(
&self,
@@ -228,7 +228,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
telemetry!(CONSENSUS_WARN; "aura.unable_fetching_authorities";
"slot" => ?chain_head.hash(), "err" => ?e
);
return Box::new(future::ok(()));
return Box::pin(future::ready(Ok(())));
}
};
@@ -237,11 +237,11 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
telemetry!(CONSENSUS_DEBUG; "aura.skipping_proposal_slot";
"authorities_len" => authorities.len()
);
return Box::new(future::ok(()));
return Box::pin(future::ready(Ok(())));
}
let maybe_author = slot_author::<P>(slot_num, &authorities);
let proposal_work = match maybe_author {
None => return Box::new(future::ok(())),
None => return Box::pin(future::ready(Ok(()))),
Some(author) => if author == &public_key {
debug!(
target: "aura", "Starting authorship at slot {}; timestamp = {}",
@@ -260,14 +260,14 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
telemetry!(CONSENSUS_WARN; "aura.unable_authoring_block";
"slot" => slot_num, "err" => ?e
);
return Box::new(future::ok(()))
return Box::pin(future::ready(Ok(())))
}
};
let remaining_duration = slot_info.remaining_duration();
// deadline our production to approx. the end of the
// slot
Timeout::new(
futures::future::select(
proposer.propose(
slot_info.inherent_data,
generic::Digest {
@@ -276,15 +276,21 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
],
},
remaining_duration,
).into_future(),
remaining_duration,
)
).map_err(|e| consensus_common::Error::ClientImport(format!("{:?}", e)).into()),
Delay::new(remaining_duration)
.map_err(|err| consensus_common::Error::FaultyTimer(err).into())
).map(|v| match v {
futures::future::Either::Left((v, _)) => v,
futures::future::Either::Right((Ok(_), _)) =>
Err(consensus_common::Error::ClientImport("Timeout in the AuRa proposer".into())),
futures::future::Either::Right((Err(err), _)) => Err(err),
})
} else {
return Box::new(future::ok(()));
return Box::pin(future::ready(Ok(())));
}
};
Box::new(proposal_work.map(move |b| {
Box::pin(proposal_work.map_ok(move |b| {
// minor hack since we don't have access to the timestamp
// that is actually set by the proposer.
let slot_after_building = SignedDuration::default().slot_now(slot_duration);
@@ -346,7 +352,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
"hash" => ?parent_hash, "err" => ?e
);
}
}).map_err(|e| consensus_common::Error::ClientImport(format!("{:?}", e)).into()))
}))
}
}
@@ -708,8 +714,6 @@ pub fn import_queue<B, C, P>(
#[cfg(test)]
mod tests {
use super::*;
use futures::{Async, stream::Stream as _};
use futures03::{StreamExt as _, TryStreamExt as _};
use consensus_common::NoNetwork as DummyOracle;
use network::test::*;
use network::test::{Block as TestBlock, PeersClient, PeersFullClient};
@@ -747,15 +751,16 @@ mod tests {
impl Proposer<TestBlock> for DummyProposer {
type Error = Error;
type Create = Result<TestBlock, Error>;
type Create = future::Ready<Result<TestBlock, Error>>;
fn propose(
&self,
_: InherentData,
digests: DigestFor<TestBlock>,
_: Duration,
) -> Result<TestBlock, Error> {
self.1.new_block(digests).unwrap().bake().map_err(|e| e.into())
) -> Self::Create {
let r = self.1.new_block(digests).unwrap().bake().map_err(|e| e.into());
future::ready(r)
}
}
@@ -839,9 +844,8 @@ mod tests {
let environ = Arc::new(DummyFactory(client.clone()));
import_notifications.push(
client.import_notification_stream()
.map(|v| Ok::<_, ()>(v)).compat()
.take_while(|n| Ok(!(n.origin != BlockOrigin::Own && n.header.number() < &5)))
.for_each(move |_| Ok(()))
.take_while(|n| future::ready(!(n.origin != BlockOrigin::Own && n.header.number() < &5)))
.for_each(move |_| future::ready(()))
);
let slot_duration = SlotDuration::get_or_compute(&*client)
@@ -867,13 +871,13 @@ mod tests {
runtime.spawn(aura);
}
// wait for all finalized on each.
let wait_for = ::futures::future::join_all(import_notifications)
.map(|_| ())
.map_err(|_| ());
runtime.spawn(futures01::future::poll_fn(move || {
net.lock().poll();
Ok::<_, ()>(futures01::Async::NotReady::<()>)
}));
let drive_to_completion = futures::future::poll_fn(|| { net.lock().poll(); Ok(Async::NotReady) });
let _ = runtime.block_on(wait_for.select(drive_to_completion).map_err(|_| ())).unwrap();
runtime.block_on(future::join_all(import_notifications)
.map(|_| Ok::<(), ()>(())).compat()).unwrap();
}
#[test]