Make BlockImport and Verifier async (#8472)

* Make grandpa work

* Introduce `SharedData`

* Add test and fix bugs

* Switch to `SharedData`

* Make grandpa tests working

* More Babe work

* Make it async

* Fix fix

* Use `async_trait` in sc-consensus-slots

This makes the code a little bit easier to read and also expresses that
there can always only be one call at a time to `on_slot`.

* Make grandpa tests compile

* More Babe tests work

* Fix network test

* Start fixing service test

* Finish service-test

* Fix sc-consensus-aura

* Fix fix fix

* More fixes

* Make everything compile *yeah*

* Fix build when we have Rust 1.51

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/babe/src/tests.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/babe/src/tests.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Fix warning

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2021-03-30 11:19:49 +02:00
committed by GitHub
parent 2998b42311
commit 217c4be226
65 changed files with 1241 additions and 715 deletions
@@ -19,17 +19,17 @@
//! Utilities for dealing with authorities, authority sets, and handoffs.
use fork_tree::ForkTree;
use parking_lot::RwLock;
use parking_lot::MappedMutexGuard;
use finality_grandpa::voter_set::VoterSet;
use parity_scale_codec::{Encode, Decode};
use log::debug;
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO};
use sp_finality_grandpa::{AuthorityId, AuthorityList};
use sc_consensus::shared_data::{SharedData, SharedDataLocked};
use std::cmp::Ord;
use std::fmt::Debug;
use std::ops::Add;
use std::sync::Arc;
/// Error type returned on operations on the `AuthoritySet`.
#[derive(Debug, derive_more::Display)]
@@ -70,19 +70,30 @@ impl<N, E: std::error::Error> From<E> for Error<N, E> {
/// A shared authority set.
pub struct SharedAuthoritySet<H, N> {
inner: Arc<RwLock<AuthoritySet<H, N>>>,
inner: SharedData<AuthoritySet<H, N>>,
}
impl<H, N> Clone for SharedAuthoritySet<H, N> {
fn clone(&self) -> Self {
SharedAuthoritySet { inner: self.inner.clone() }
SharedAuthoritySet {
inner: self.inner.clone(),
}
}
}
impl<H, N> SharedAuthoritySet<H, N> {
/// Acquire a reference to the inner read-write lock.
pub(crate) fn inner(&self) -> &RwLock<AuthoritySet<H, N>> {
&*self.inner
/// Returns access to the [`AuthoritySet`].
pub(crate) fn inner(&self) -> MappedMutexGuard<AuthoritySet<H, N>> {
self.inner.shared_data()
}
/// Returns access to the [`AuthoritySet`] and locks it.
///
/// For more information see [`SharedDataLocked`].
pub(crate) fn inner_locked(
&self,
) -> SharedDataLocked<AuthoritySet<H, N>> {
self.inner.shared_data_locked()
}
}
@@ -93,17 +104,17 @@ where N: Add<Output=N> + Ord + Clone + Debug,
/// Get the earliest limit-block number that's higher or equal to the given
/// min number, if any.
pub(crate) fn current_limit(&self, min: N) -> Option<N> {
self.inner.read().current_limit(min)
self.inner().current_limit(min)
}
/// Get the current set ID. This is incremented every time the set changes.
pub fn set_id(&self) -> u64 {
self.inner.read().set_id
self.inner().set_id
}
/// Get the current authorities and their weights (for the current set ID).
pub fn current_authorities(&self) -> VoterSet<AuthorityId> {
VoterSet::new(self.inner.read().current_authorities.iter().cloned()).expect(
VoterSet::new(self.inner().current_authorities.iter().cloned()).expect(
"current_authorities is non-empty and weights are non-zero; \
constructor and all mutating operations on `AuthoritySet` ensure this; \
qed.",
@@ -112,18 +123,20 @@ where N: Add<Output=N> + Ord + Clone + Debug,
/// Clone the inner `AuthoritySet`.
pub fn clone_inner(&self) -> AuthoritySet<H, N> {
self.inner.read().clone()
self.inner().clone()
}
/// Clone the inner `AuthoritySetChanges`.
pub fn authority_set_changes(&self) -> AuthoritySetChanges<N> {
self.inner.read().authority_set_changes.clone()
self.inner().authority_set_changes.clone()
}
}
impl<H, N> From<AuthoritySet<H, N>> for SharedAuthoritySet<H, N> {
fn from(set: AuthoritySet<H, N>) -> Self {
SharedAuthoritySet { inner: Arc::new(RwLock::new(set)) }
SharedAuthoritySet {
inner: SharedData::new(set),
}
}
}