Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Aura: Skip initialize block & remove cache (#9132)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bkchr committed Jun 17, 2021
1 parent c666a25 commit 3bb42b9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 48 deletions.
39 changes: 0 additions & 39 deletions client/consensus/aura/src/import_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,43 +317,6 @@ impl<B: BlockT, C, P, CAW, CIDP> Verifier<B> for AuraVerifier<C, P, CAW, CIDP> w
}
}

fn initialize_authorities_cache<A, B, C>(client: &C) -> Result<(), ConsensusError> where
A: Codec + Debug,
B: BlockT,
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + UsageProvider<B>,
C::Api: AuraApi<B, A>,
{
// 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<Vec<A>> = 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 {
Expand Down Expand Up @@ -438,8 +401,6 @@ pub fn import_queue<'a, P, Block, I, C, S, CAW, CIDP>(
CIDP: CreateInherentDataProviders<Block, ()> + Sync + Send + 'static,
CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
{
initialize_authorities_cache(&*client)?;

let verifier = build_verifier::<P, _, _, _>(
BuildVerifierParams {
client,
Expand Down
13 changes: 4 additions & 9 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -546,14 +546,9 @@ fn authorities<A, B, C>(client: &C, at: &BlockId<B>) -> Result<Vec<A>, Consensus
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B>,
C::Api: AuraApi<B, A>,
{
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())
}

Expand Down
1 change: 1 addition & 0 deletions primitives/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthorityId>;
}
}
Expand Down

0 comments on commit 3bb42b9

Please sign in to comment.