mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-01 01:01:01 +00:00
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:
@@ -20,13 +20,13 @@ use std::{sync::Arc, collections::HashMap};
|
||||
|
||||
use log::debug;
|
||||
use parity_scale_codec::Encode;
|
||||
use parking_lot::RwLockWriteGuard;
|
||||
|
||||
use sp_blockchain::{BlockStatus, well_known_cache_keys};
|
||||
use sc_client_api::{backend::Backend, utils::is_descendent_of};
|
||||
use sc_telemetry::TelemetryHandle;
|
||||
use sp_utils::mpsc::TracingUnboundedSender;
|
||||
use sp_api::TransactionFor;
|
||||
use sc_consensus::shared_data::{SharedDataLockedUpgradable, SharedDataLocked};
|
||||
|
||||
use sp_consensus::{
|
||||
BlockImport, Error as ConsensusError,
|
||||
@@ -99,7 +99,7 @@ impl<BE, Block: BlockT, Client, SC> JustificationImport<Block>
|
||||
let chain_info = self.inner.info();
|
||||
|
||||
// request justifications for all pending changes for which change blocks have already been imported
|
||||
let authorities = self.authority_set.inner().read();
|
||||
let authorities = self.authority_set.inner();
|
||||
for pending_change in authorities.pending_changes() {
|
||||
if pending_change.delay_kind == DelayKind::Finalized &&
|
||||
pending_change.effective_number() > chain_info.finalized_number &&
|
||||
@@ -157,30 +157,30 @@ impl<H, N> AppliedChanges<H, N> {
|
||||
}
|
||||
}
|
||||
|
||||
struct PendingSetChanges<'a, Block: 'a + BlockT> {
|
||||
struct PendingSetChanges<Block: BlockT> {
|
||||
just_in_case: Option<(
|
||||
AuthoritySet<Block::Hash, NumberFor<Block>>,
|
||||
RwLockWriteGuard<'a, AuthoritySet<Block::Hash, NumberFor<Block>>>,
|
||||
SharedDataLockedUpgradable<AuthoritySet<Block::Hash, NumberFor<Block>>>,
|
||||
)>,
|
||||
applied_changes: AppliedChanges<Block::Hash, NumberFor<Block>>,
|
||||
do_pause: bool,
|
||||
}
|
||||
|
||||
impl<'a, Block: 'a + BlockT> PendingSetChanges<'a, Block> {
|
||||
impl<Block: BlockT> PendingSetChanges<Block> {
|
||||
// revert the pending set change explicitly.
|
||||
fn revert(self) { }
|
||||
fn revert(self) {}
|
||||
|
||||
fn defuse(mut self) -> (AppliedChanges<Block::Hash, NumberFor<Block>>, bool) {
|
||||
self.just_in_case = None;
|
||||
let applied_changes = ::std::mem::replace(&mut self.applied_changes, AppliedChanges::None);
|
||||
let applied_changes = std::mem::replace(&mut self.applied_changes, AppliedChanges::None);
|
||||
(applied_changes, self.do_pause)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Block: 'a + BlockT> Drop for PendingSetChanges<'a, Block> {
|
||||
impl<Block: BlockT> Drop for PendingSetChanges<Block> {
|
||||
fn drop(&mut self) {
|
||||
if let Some((old_set, mut authorities)) = self.just_in_case.take() {
|
||||
*authorities = old_set;
|
||||
*authorities.upgrade() = old_set;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,33 +269,38 @@ where
|
||||
// when we update the authorities, we need to hold the lock
|
||||
// until the block is written to prevent a race if we need to restore
|
||||
// the old authority set on error or panic.
|
||||
struct InnerGuard<'a, T: 'a> {
|
||||
old: Option<T>,
|
||||
guard: Option<RwLockWriteGuard<'a, T>>,
|
||||
struct InnerGuard<'a, H, N> {
|
||||
old: Option<AuthoritySet<H, N>>,
|
||||
guard: Option<SharedDataLocked<'a, AuthoritySet<H, N>>>,
|
||||
}
|
||||
|
||||
impl<'a, T: 'a> InnerGuard<'a, T> {
|
||||
fn as_mut(&mut self) -> &mut T {
|
||||
impl<'a, H, N> InnerGuard<'a, H, N> {
|
||||
fn as_mut(&mut self) -> &mut AuthoritySet<H, N> {
|
||||
&mut **self.guard.as_mut().expect("only taken on deconstruction; qed")
|
||||
}
|
||||
|
||||
fn set_old(&mut self, old: T) {
|
||||
fn set_old(&mut self, old: AuthoritySet<H, N>) {
|
||||
if self.old.is_none() {
|
||||
// ignore "newer" old changes.
|
||||
self.old = Some(old);
|
||||
}
|
||||
}
|
||||
|
||||
fn consume(mut self) -> Option<(T, RwLockWriteGuard<'a, T>)> {
|
||||
fn consume(
|
||||
mut self,
|
||||
) -> Option<(AuthoritySet<H, N>, SharedDataLocked<'a, AuthoritySet<H, N>>)> {
|
||||
if let Some(old) = self.old.take() {
|
||||
Some((old, self.guard.take().expect("only taken on deconstruction; qed")))
|
||||
Some((
|
||||
old,
|
||||
self.guard.take().expect("only taken on deconstruction; qed"),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'a> Drop for InnerGuard<'a, T> {
|
||||
impl<'a, H, N> Drop for InnerGuard<'a, H, N> {
|
||||
fn drop(&mut self) {
|
||||
if let (Some(mut guard), Some(old)) = (self.guard.take(), self.old.take()) {
|
||||
*guard = old;
|
||||
@@ -315,7 +320,7 @@ where
|
||||
let is_descendent_of = is_descendent_of(&*self.inner, Some((hash, parent_hash)));
|
||||
|
||||
let mut guard = InnerGuard {
|
||||
guard: Some(self.authority_set.inner().write()),
|
||||
guard: Some(self.authority_set.inner_locked()),
|
||||
old: None,
|
||||
};
|
||||
|
||||
@@ -413,10 +418,13 @@ where
|
||||
);
|
||||
}
|
||||
|
||||
let just_in_case = just_in_case.map(|(o, i)| (o, i.release_mutex()));
|
||||
|
||||
Ok(PendingSetChanges { just_in_case, applied_changes, do_pause })
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<BE, Block: BlockT, Client, SC> BlockImport<Block>
|
||||
for GrandpaBlockImport<BE, Block, Client, SC> where
|
||||
NumberFor<Block>: finality_grandpa::BlockNumberOps,
|
||||
@@ -425,11 +433,13 @@ impl<BE, Block: BlockT, Client, SC> BlockImport<Block>
|
||||
Client: crate::ClientForGrandpa<Block, BE>,
|
||||
for<'a> &'a Client:
|
||||
BlockImport<Block, Error = ConsensusError, Transaction = TransactionFor<Client, Block>>,
|
||||
TransactionFor<Client, Block>: Send + 'static,
|
||||
SC: Send,
|
||||
{
|
||||
type Error = ConsensusError;
|
||||
type Transaction = TransactionFor<Client, Block>;
|
||||
|
||||
fn import_block(
|
||||
async fn import_block(
|
||||
&mut self,
|
||||
mut block: BlockImportParams<Block, Self::Transaction>,
|
||||
new_cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
|
||||
@@ -452,7 +462,7 @@ impl<BE, Block: BlockT, Client, SC> BlockImport<Block>
|
||||
|
||||
// we don't want to finalize on `inner.import_block`
|
||||
let mut justifications = block.justifications.take();
|
||||
let import_result = (&*self.inner).import_block(block, new_cache);
|
||||
let import_result = (&*self.inner).import_block(block, new_cache).await;
|
||||
|
||||
let mut imported_aux = {
|
||||
match import_result {
|
||||
@@ -556,11 +566,11 @@ impl<BE, Block: BlockT, Client, SC> BlockImport<Block>
|
||||
Ok(ImportResult::Imported(imported_aux))
|
||||
}
|
||||
|
||||
fn check_block(
|
||||
async fn check_block(
|
||||
&mut self,
|
||||
block: BlockCheckParams<Block>,
|
||||
) -> Result<ImportResult, Self::Error> {
|
||||
self.inner.check_block(block)
|
||||
self.inner.check_block(block).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -580,8 +590,7 @@ impl<Backend, Block: BlockT, Client, SC> GrandpaBlockImport<Backend, Block, Clie
|
||||
.iter()
|
||||
.find(|(set_id, _)| *set_id == authority_set.set_id())
|
||||
{
|
||||
let mut authority_set = authority_set.inner().write();
|
||||
authority_set.current_authorities = change.next_authorities.clone();
|
||||
authority_set.inner().current_authorities = change.next_authorities.clone();
|
||||
}
|
||||
|
||||
// index authority set hard forks by block hash so that they can be used
|
||||
@@ -596,7 +605,7 @@ impl<Backend, Block: BlockT, Client, SC> GrandpaBlockImport<Backend, Block, Clie
|
||||
// any *pending* standard changes, checking by the block hash at which
|
||||
// they were announced.
|
||||
{
|
||||
let mut authority_set = authority_set.inner().write();
|
||||
let mut authority_set = authority_set.inner();
|
||||
|
||||
authority_set.pending_standard_changes = authority_set
|
||||
.pending_standard_changes
|
||||
|
||||
Reference in New Issue
Block a user