Use JustifiedBlockAnnounceValidator for parachain block announce validator (#96)

This commit is contained in:
Cecile Tonglet
2020-05-19 17:56:31 +02:00
committed by GitHub
parent dfc95f0d0e
commit 1727dc6291
7 changed files with 115 additions and 24 deletions
+33
View File
@@ -40,6 +40,7 @@ use futures::task::Spawn;
use log::{error, trace};
use std::{marker::PhantomData, sync::Arc};
use parking_lot::Mutex;
/// Validate that data is a valid justification from a relay-chain validator that the block is a
/// valid parachain-block candidate.
@@ -148,6 +149,38 @@ where
}
}
/// A `BlockAnnounceValidator` that will be able to validate data when its internal
/// `BlockAnnounceValidator` is set.
pub struct DelayedBlockAnnounceValidator<B: BlockT>(Arc<Mutex<Option<Box<dyn BlockAnnounceValidator<B> + Send>>>>);
impl<B: BlockT> DelayedBlockAnnounceValidator<B> {
pub fn new() -> DelayedBlockAnnounceValidator<B> {
DelayedBlockAnnounceValidator(Arc::new(Mutex::new(None)))
}
pub fn set(&self, validator: Box<dyn BlockAnnounceValidator<B> + Send>) {
*self.0.lock() = Some(validator);
}
}
impl<B: BlockT> Clone for DelayedBlockAnnounceValidator<B> {
fn clone(&self) -> DelayedBlockAnnounceValidator<B> {
DelayedBlockAnnounceValidator(self.0.clone())
}
}
impl<B: BlockT> BlockAnnounceValidator<B> for DelayedBlockAnnounceValidator<B> {
fn validate(
&mut self,
header: &B::Header,
data: &[u8],
) -> Result<Validation, Box<dyn std::error::Error + Send>> {
self.0.lock().as_mut()
.expect("BlockAnnounceValidator is set before validating the first announcement; qed")
.validate(header, data)
}
}
/// Wait before announcing a block that a candidate message has been received for this block, then
/// add this message as justification for the block announcement.
///