Don't validate a block announcement when syncing (#177)

This commit is contained in:
Cecile Tonglet
2020-08-06 13:30:55 +02:00
committed by GitHub
parent 2042e7e789
commit 3f3beea7f0
5 changed files with 145 additions and 108 deletions
+12 -2
View File
@@ -23,7 +23,7 @@ mod tests;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Error as ClientError, HeaderBackend};
use sp_consensus::block_validation::{BlockAnnounceValidator, Validation};
use sp_consensus::{block_validation::{BlockAnnounceValidator, Validation}, SyncOracle};
use sp_core::traits::SpawnNamed;
use sp_runtime::{
generic::BlockId,
@@ -55,14 +55,20 @@ pub struct JustifiedBlockAnnounceValidator<B, P> {
phantom: PhantomData<B>,
polkadot_client: Arc<P>,
para_id: ParaId,
polkadot_sync_oracle: Box<dyn SyncOracle + Send>,
}
impl<B, P> JustifiedBlockAnnounceValidator<B, P> {
pub fn new(polkadot_client: Arc<P>, para_id: ParaId) -> Self {
pub fn new(
polkadot_client: Arc<P>,
para_id: ParaId,
polkadot_sync_oracle: Box<dyn SyncOracle + Send>,
) -> Self {
Self {
phantom: Default::default(),
polkadot_client,
para_id,
polkadot_sync_oracle,
}
}
}
@@ -77,6 +83,10 @@ where
header: &B::Header,
mut data: &[u8],
) -> Result<Validation, Box<dyn std::error::Error + Send>> {
if self.polkadot_sync_oracle.is_major_syncing() {
return Ok(Validation::Success { is_new_best: false });
}
let runtime_api = self.polkadot_client.runtime_api();
let polkadot_info = self.polkadot_client.info();
+18 -1
View File
@@ -32,6 +32,19 @@ use sp_core::H256;
use sp_keyring::Sr25519Keyring;
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero};
#[derive(Clone)]
struct DummyCollatorNetwork;
impl SyncOracle for DummyCollatorNetwork {
fn is_major_syncing(&mut self) -> bool {
false
}
fn is_offline(&mut self) -> bool {
unimplemented!("Not required in tests")
}
}
fn make_validator() -> JustifiedBlockAnnounceValidator<Block, TestApi> {
let (validator, _client) = make_validator_and_client();
@@ -46,7 +59,11 @@ fn make_validator_and_client() -> (
let client = Arc::new(TestApi::new(Arc::new(builder.build())));
(
JustifiedBlockAnnounceValidator::new(client.clone(), ParaId::from(56)),
JustifiedBlockAnnounceValidator::new(
client.clone(),
ParaId::from(56),
Box::new(DummyCollatorNetwork),
),
client,
)
}