consensus: handle justification sync for blocks authored locally (#8698)

* consensus: add trait to control justification sync process

* network: implement JustificationSyncLink for NetworkService

* slots: handle justification sync in slot worker

* babe: fix slot worker instantiation

* aura: fix slot worker instantiation

* pow: handle justification sync in miner

* babe: fix tests

* aura: fix tests

* node: fix compilation

* node-template: fix compilation

* consensus: rename justification sync link parameter

* aura: fix test compilation

* consensus: slots: move JustificationSyncLink out of on_slot
This commit is contained in:
André Silva
2021-06-04 22:31:06 +01:00
committed by GitHub
parent a477d4dadf
commit c44b552d8f
13 changed files with 228 additions and 70 deletions
@@ -68,6 +68,30 @@ impl ImportResult {
ImportResult::Imported(aux)
}
/// Handles any necessary request for justifications (or clearing of pending requests) based on
/// the outcome of this block import.
pub fn handle_justification<B>(
&self,
hash: &B::Hash,
number: NumberFor<B>,
justification_sync_link: &mut dyn JustificationSyncLink<B>,
) where
B: BlockT,
{
match self {
ImportResult::Imported(aux) => {
if aux.clear_justification_requests {
justification_sync_link.clear_justification_requests();
}
if aux.needs_justification {
justification_sync_link.request_justification(hash, number);
}
}
_ => {}
}
}
}
/// Block data origin.
@@ -354,3 +378,32 @@ pub trait JustificationImport<B: BlockT> {
justification: Justification,
) -> Result<(), Self::Error>;
}
/// Control the synchronization process of block justifications.
///
/// When importing blocks different consensus engines might require that
/// additional finality data is provided (i.e. a justification for the block).
/// This trait abstracts the required methods to issue those requests
pub trait JustificationSyncLink<B: BlockT>: Send + Sync {
/// Request a justification for the given block.
fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
/// Clear all pending justification requests.
fn clear_justification_requests(&self);
}
impl<B: BlockT> JustificationSyncLink<B> for () {
fn request_justification(&self, _hash: &B::Hash, _number: NumberFor<B>) {}
fn clear_justification_requests(&self) {}
}
impl<B: BlockT, L: JustificationSyncLink<B>> JustificationSyncLink<B> for Arc<L> {
fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
L::request_justification(&*self, hash, number);
}
fn clear_justification_requests(&self) {
L::clear_justification_requests(&*self);
}
}
@@ -49,8 +49,8 @@ mod metrics;
pub use self::error::Error;
pub use block_import::{
BlockImport, BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, BlockCheckParams,
ImportResult, JustificationImport,
BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, ForkChoiceStrategy,
ImportResult, ImportedAux, JustificationImport, JustificationSyncLink,
};
pub use select_chain::SelectChain;
pub use sp_state_machine::Backend as StateBackend;