From f5d77eb7007b44e42a8ce4991aa054faa8bab6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 16 Jun 2021 22:29:38 +0200 Subject: [PATCH] Aura: Skip initialize block & remove cache This instructs the Aura runtime api to skip initialize block, when requesting the authorities. This is important, as we don't want to use the new authorities that should be used from the next block on. Besides that, it removes the caching stuff. The cache is not available on full nodes anyway. In the future we should store the authorities probably in the aux store. --- client/consensus/aura/src/import_queue.rs | 39 ----------------------- client/consensus/aura/src/lib.rs | 13 +++----- primitives/consensus/aura/src/lib.rs | 1 + 3 files changed, 5 insertions(+), 48 deletions(-) diff --git a/client/consensus/aura/src/import_queue.rs b/client/consensus/aura/src/import_queue.rs index 8034fd08a7eb6..c3faa5382686e 100644 --- a/client/consensus/aura/src/import_queue.rs +++ b/client/consensus/aura/src/import_queue.rs @@ -317,43 +317,6 @@ impl Verifier for AuraVerifier w } } -fn initialize_authorities_cache(client: &C) -> Result<(), ConsensusError> where - A: Codec + Debug, - B: BlockT, - C: ProvideRuntimeApi + BlockOf + ProvideCache + UsageProvider, - C::Api: AuraApi, -{ - // no cache => no initialization - let cache = match client.cache() { - Some(cache) => cache, - None => return Ok(()), - }; - - let best_hash = client.usage_info().chain.best_hash; - - // check if we already have initialized the cache - let map_err = |error| sp_consensus::Error::from(sp_consensus::Error::ClientImport( - format!( - "Error initializing authorities cache: {}", - error, - ))); - - let block_id = BlockId::hash(best_hash); - let authorities: Option> = cache - .get_at(&well_known_cache_keys::AUTHORITIES, &block_id) - .unwrap_or(None) - .and_then(|(_, _, v)| Decode::decode(&mut &v[..]).ok()); - if authorities.is_some() { - return Ok(()); - } - - let authorities = crate::authorities(client, &block_id)?; - cache.initialize(&well_known_cache_keys::AUTHORITIES, authorities.encode()) - .map_err(map_err)?; - - Ok(()) -} - /// Should we check for equivocation of a block author? #[derive(Debug, Clone, Copy)] pub enum CheckForEquivocation { @@ -438,8 +401,6 @@ pub fn import_queue<'a, P, Block, I, C, S, CAW, CIDP>( CIDP: CreateInherentDataProviders + Sync + Send + 'static, CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync, { - initialize_authorities_cache(&*client)?; - let verifier = build_verifier::( BuildVerifierParams { client, diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 702e4dc0bf1bd..d0b0cefe8ddca 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -45,7 +45,7 @@ use sp_consensus::{ BlockOrigin, Error as ConsensusError, SelectChain, }; use sc_client_api::{backend::AuxStore, BlockOf, UsageProvider}; -use sp_blockchain::{Result as CResult, well_known_cache_keys, ProvideCache, HeaderBackend}; +use sp_blockchain::{Result as CResult, ProvideCache, HeaderBackend}; use sp_core::crypto::Public; use sp_application_crypto::{AppKey, AppPublic}; use sp_runtime::{generic::BlockId, traits::NumberFor}; @@ -546,14 +546,9 @@ fn authorities(client: &C, at: &BlockId) -> Result, Consensus C: ProvideRuntimeApi + BlockOf + ProvideCache, C::Api: AuraApi, { - client - .cache() - .and_then(|cache| cache - .get_at(&well_known_cache_keys::AUTHORITIES, at) - .unwrap_or(None) - .and_then(|(_, _, v)| Decode::decode(&mut &v[..]).ok()) - ) - .or_else(|| AuraApi::authorities(&*client.runtime_api(), at).ok()) + client.runtime_api() + .authorities(at) + .ok() .ok_or_else(|| sp_consensus::Error::InvalidAuthoritiesSet.into()) } diff --git a/primitives/consensus/aura/src/lib.rs b/primitives/consensus/aura/src/lib.rs index a28e681fda27f..ef888a2ab855b 100644 --- a/primitives/consensus/aura/src/lib.rs +++ b/primitives/consensus/aura/src/lib.rs @@ -90,6 +90,7 @@ sp_api::decl_runtime_apis! { fn slot_duration() -> SlotDuration; // Return the current set of authorities. + #[skip_initialize_block] fn authorities() -> Vec; } }