babe, aura, pow: only call check_inherents if authoring version is compatible (#6862)

* pow: check can_author_with before calling check_inherents

* babe: check can_author_with before calling check_inherents

* aura: check can_author_with before calling check_inherents

* Fix node and node template compile

* Add missing comma

* Put each parameter on its own line

* Add debug print

* Fix line width too long

* Fix pow line width issue
This commit is contained in:
Wei Tang
2020-08-17 13:41:09 +02:00
committed by GitHub
parent 76290b31c0
commit 3f85b85e80
7 changed files with 97 additions and 24 deletions
+31 -7
View File
@@ -186,16 +186,19 @@ pub trait PowAlgorithm<B: BlockT> {
}
/// A block importer for PoW.
pub struct PowBlockImport<B: BlockT, I, C, S, Algorithm> {
pub struct PowBlockImport<B: BlockT, I, C, S, Algorithm, CAW> {
algorithm: Algorithm,
inner: I,
select_chain: Option<S>,
client: Arc<C>,
inherent_data_providers: sp_inherents::InherentDataProviders,
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
can_author_with: CAW,
}
impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone> Clone for PowBlockImport<B, I, C, S, Algorithm> {
impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone, CAW: Clone> Clone
for PowBlockImport<B, I, C, S, Algorithm, CAW>
{
fn clone(&self) -> Self {
Self {
algorithm: self.algorithm.clone(),
@@ -204,17 +207,19 @@ impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone> Clone for PowBlockImpor
client: self.client.clone(),
inherent_data_providers: self.inherent_data_providers.clone(),
check_inherents_after: self.check_inherents_after.clone(),
can_author_with: self.can_author_with.clone(),
}
}
}
impl<B, I, C, S, Algorithm> PowBlockImport<B, I, C, S, Algorithm> where
impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> where
B: BlockT,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
Algorithm: PowAlgorithm<B>,
CAW: CanAuthorWith<B>,
{
/// Create a new block import suitable to be used in PoW
pub fn new(
@@ -224,9 +229,17 @@ impl<B, I, C, S, Algorithm> PowBlockImport<B, I, C, S, Algorithm> where
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
select_chain: Option<S>,
inherent_data_providers: sp_inherents::InherentDataProviders,
can_author_with: CAW,
) -> Self {
Self { inner, client, algorithm, check_inherents_after,
select_chain, inherent_data_providers }
Self {
inner,
client,
algorithm,
check_inherents_after,
select_chain,
inherent_data_providers,
can_author_with,
}
}
fn check_inherents(
@@ -242,6 +255,16 @@ impl<B, I, C, S, Algorithm> PowBlockImport<B, I, C, S, Algorithm> where
return Ok(())
}
if let Err(e) = self.can_author_with.can_author_with(&block_id) {
debug!(
target: "pow",
"Skipping `check_inherents` as authoring version is not compatible: {}",
e,
);
return Ok(())
}
let inherent_res = self.client.runtime_api().check_inherents(
&block_id,
block,
@@ -270,7 +293,7 @@ impl<B, I, C, S, Algorithm> PowBlockImport<B, I, C, S, Algorithm> where
}
}
impl<B, I, C, S, Algorithm> BlockImport<B> for PowBlockImport<B, I, C, S, Algorithm> where
impl<B, I, C, S, Algorithm, CAW> BlockImport<B> for PowBlockImport<B, I, C, S, Algorithm, CAW> where
B: BlockT,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
@@ -279,6 +302,7 @@ impl<B, I, C, S, Algorithm> BlockImport<B> for PowBlockImport<B, I, C, S, Algori
C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
Algorithm: PowAlgorithm<B>,
Algorithm::Difficulty: 'static,
CAW: CanAuthorWith<B>,
{
type Error = ConsensusError;
type Transaction = sp_api::TransactionFor<C, B>;
@@ -649,7 +673,7 @@ fn mine_loop<B: BlockT, C, Algorithm, E, SO, S, CAW>(
};
log::info!("✅ Successfully mined block: {}", best_hash);
let (hash, seal) = {
let seal = DigestItem::Seal(POW_ENGINE_ID, seal);
let mut header = header.clone();