Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite with async/await #127

Closed
wants to merge 14 commits into from
20 changes: 17 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@ repository = "https://github.com/paritytech/finality-grandpa"
edition = "2018"

[dependencies]
futures = { version = "0.3.1", default-features = false }
async-trait = "0.1.36"
async-rwlock = { version = "1.3.0", optional = true }
futures = { version = "0.3.7", default-features = false, features = [ "async-await" ] }
futures-timer = { version = "2.0.2", optional = true }
log = { version = "0.4", optional = true }
parking_lot = { version = "0.9", optional = true }
parity-scale-codec = { version = "1.0.3", optional = true, default-features = false, features = ["derive"] }
num = { package = "num-traits", version = "0.2", default-features = false }
rand = {version = "0.6.0", optional = true }
either = { version = "1.5.3", default-features = false }

[dev-dependencies]
futures = { version = "0.3.7", default-features = false, features = [ "async-await", "thread-pool" ] }
rand = "0.7.0"
quickcheck = "0.9"
parking_lot = "0.9"
async-std = { version = "1.6.5", features = ["unstable"] }
env_logger = "0.8"


[features]
default = ["std"]
std = ["parity-scale-codec/std", "num/std", "parking_lot", "log", "futures-timer", "futures/executor"]
std = [
"parity-scale-codec/std",
"num/std",
"async-rwlock",
"log",
"futures-timer",
"futures/executor",
"futures/async-await",
]
derive-codec = ["parity-scale-codec"]
test-helpers = ["fuzz-helpers", "rand", "std"]
fuzz-helpers = []
43 changes: 18 additions & 25 deletions src/bridge_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@
//! Bridging round state between rounds.

use crate::round::State as RoundState;
use futures::task;
use parking_lot::{RwLock, RwLockReadGuard};
use async_rwlock::{RwLock, RwLockReadGuard};
use std::sync::Arc;
use std::task::Context;

// round state bridged across rounds.
struct Bridged<H, N> {
inner: RwLock<RoundState<H, N>>,
waker: task::AtomicWaker,
}

impl<H, N> Bridged<H, N> {
fn new(inner: RwLock<RoundState<H, N>>) -> Self {
Bridged {
inner,
waker: task::AtomicWaker::new(),
}
}
}
Expand All @@ -40,9 +36,8 @@ pub(crate) struct PriorView<H, N>(Arc<Bridged<H, N>>);

impl<H, N> PriorView<H, N> {
/// Push an update to the latter view.
pub(crate) fn update(&self, new: RoundState<H, N>) {
*self.0.inner.write() = new;
self.0.waker.wake();
pub(crate) async fn update(&self, new: RoundState<H, N>) {
*self.0.inner.write().await = new;
}
}

Expand All @@ -51,9 +46,8 @@ pub(crate) struct LatterView<H, N>(Arc<Bridged<H, N>>);

impl<H, N> LatterView<H, N> {
/// Fetch a handle to the last round-state.
pub(crate) fn get(&self, cx: &mut Context) -> RwLockReadGuard<RoundState<H, N>> {
self.0.waker.register(cx.waker());
self.0.inner.read()
pub(crate) async fn get(&self) -> RwLockReadGuard<'_, RoundState<H, N>> {
self.0.inner.read().await
}
}

Expand All @@ -73,7 +67,9 @@ pub(crate) fn bridge_state<H, N>(initial: RoundState<H, N>) -> (PriorView<H, N>,

#[cfg(test)]
mod tests {
use std::{sync::Barrier, task::Poll};
use async_std::sync::Barrier;
use futures::executor::LocalPool;
use futures::task::SpawnExt;
use super::*;

#[test]
Expand All @@ -85,28 +81,25 @@ mod tests {
completable: false,
};

let mut pool = LocalPool::new();
let (prior, latter) = bridge_state(initial);
let waits_for_finality = ::futures::future::poll_fn(move |cx| -> Poll<()> {
if latter.get(cx).finalized.is_some() {
Poll::Ready(())
} else {
Poll::Pending
}
});

let barrier = Arc::new(Barrier::new(2));
let barrier_other = barrier.clone();
::std::thread::spawn(move || {
barrier_other.wait();

pool.spawner().spawn(async move {
barrier_other.wait().await;
prior.update(RoundState {
prevote_ghost: Some(("5", 5)),
finalized: Some(("1", 1)),
estimate: Some(("3", 3)),
completable: true,
});
});
}).await;
}).unwrap();

barrier.wait();
futures::executor::block_on(waits_for_finality);
pool.run_until(async {
barrier.wait().await;
while !latter.get().await.finalized.is_some() {}
});
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//!
//! Equivocation detection and vote-set management is done in the `round` module.
//! The work for actually casting votes is done in the `voter` module.
#![recursion_limit="1024"]

#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
Expand Down
Loading