Move AuRa digest from client to primitives (#8245)

* Move AuRa digest from client to primitives

This makes the digest stuff usable from inside the runtime ;)

* Update primitives/runtime/src/generic/digest.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Review feedback

* Make BABE use the new functionality

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2021-03-03 09:41:27 +01:00
committed by GitHub
parent aca3332953
commit dc190a69f2
5 changed files with 99 additions and 55 deletions
+20 -24
View File
@@ -77,16 +77,13 @@ use sp_consensus_slots::Slot;
use sp_api::ApiExt;
pub use sp_consensus_aura::{
ConsensusLog, AuraApi, AURA_ENGINE_ID,
ConsensusLog, AuraApi, AURA_ENGINE_ID, digests::CompatibleDigestItem,
inherents::{
InherentType as AuraInherent,
AuraInherentData, INHERENT_IDENTIFIER, InherentDataProvider,
},
};
pub use sp_consensus::SyncOracle;
pub use digests::CompatibleDigestItem;
mod digests;
type AuthorityId<P> = <P as Pair>::Public;
@@ -271,7 +268,7 @@ where
_claim: &Self::Claim,
) -> Vec<sp_runtime::DigestItem<B::Hash>> {
vec![
<DigestItemFor<B> as CompatibleDigestItem<P>>::aura_pre_digest(slot),
<DigestItemFor<B> as CompatibleDigestItem<P::Signature>>::aura_pre_digest(slot),
]
}
@@ -308,7 +305,9 @@ where
signature, public
))?;
let signature_digest_item = <DigestItemFor<B> as CompatibleDigestItem<P>>::aura_seal(signature);
let signature_digest_item = <
DigestItemFor<B> as CompatibleDigestItem<P::Signature>
>::aura_seal(signature);
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
import_block.post_digests.push(signature_digest_item);
@@ -326,7 +325,7 @@ where
fn should_backoff(&self, slot: Slot, chain_head: &B::Header) -> bool {
if let Some(ref strategy) = self.backoff_authoring_blocks {
if let Ok(chain_head_slot) = find_pre_digest::<B, P>(chain_head) {
if let Ok(chain_head_slot) = find_pre_digest::<B, P::Signature>(chain_head) {
return strategy.should_backoff(
*chain_head.number(),
chain_head_slot,
@@ -356,7 +355,7 @@ where
) -> Option<std::time::Duration> {
let slot_remaining = self.slot_remaining_duration(slot_info);
let parent_slot = match find_pre_digest::<B, P>(head) {
let parent_slot = match find_pre_digest::<B, P::Signature>(head) {
Err(_) => return Some(slot_remaining),
Ok(d) => d,
};
@@ -414,11 +413,7 @@ impl<B: BlockT> std::convert::From<Error<B>> for String {
}
}
fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<Slot, Error<B>>
where DigestItemFor<B>: CompatibleDigestItem<P>,
P::Signature: Decode,
P::Public: Encode + Decode + PartialEq + Clone,
{
fn find_pre_digest<B: BlockT, Signature: Codec>(header: &B::Header) -> Result<Slot, Error<B>> {
if header.number().is_zero() {
return Ok(0.into());
}
@@ -426,7 +421,7 @@ fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<Slot, Error
let mut pre_digest: Option<Slot> = None;
for log in header.digest().logs() {
trace!(target: "aura", "Checking log {:?}", log);
match (log.as_aura_pre_digest(), pre_digest.is_some()) {
match (CompatibleDigestItem::<Signature>::as_aura_pre_digest(log), pre_digest.is_some()) {
(Some(_), true) => Err(aura_err(Error::MultipleHeaders))?,
(None, _) => trace!(target: "aura", "Ignoring digest not meant for us"),
(s, false) => pre_digest = s,
@@ -435,8 +430,9 @@ fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<Slot, Error
pre_digest.ok_or_else(|| aura_err(Error::NoDigestFound))
}
/// check a header has been signed by the right key. If the slot is too far in the future, an error will be returned.
/// if it's successful, returns the pre-header and the digest item containing the seal.
/// check a header has been signed by the right key. If the slot is too far in the future, an error
/// will be returned. If it's successful, returns the pre-header and the digest item
/// containing the seal.
///
/// This digest item will always return `Some` when used with `as_aura_seal`.
//
@@ -447,8 +443,8 @@ fn check_header<C, B: BlockT, P: Pair>(
hash: B::Hash,
authorities: &[AuthorityId<P>],
) -> Result<CheckedHeader<B::Header, (Slot, DigestItemFor<B>)>, Error<B>> where
DigestItemFor<B>: CompatibleDigestItem<P>,
P::Signature: Decode,
DigestItemFor<B>: CompatibleDigestItem<P::Signature>,
P::Signature: Codec,
C: sc_client_api::backend::AuxStore,
P::Public: Encode + Decode + PartialEq + Clone,
{
@@ -461,7 +457,7 @@ fn check_header<C, B: BlockT, P: Pair>(
aura_err(Error::HeaderBadSeal(hash))
})?;
let slot = find_pre_digest::<B, _>(&header)?;
let slot = find_pre_digest::<B, P::Signature>(&header)?;
if slot > slot_now {
header.digest_mut().push(seal);
@@ -582,7 +578,7 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
ProvideCache<B> +
BlockOf,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B>,
DigestItemFor<B>: CompatibleDigestItem<P>,
DigestItemFor<B>: CompatibleDigestItem<P::Signature>,
P: Pair + Send + Sync + 'static,
P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + 'static,
P::Signature: Encode + Decode,
@@ -805,7 +801,7 @@ impl<Block: BlockT, C, I, P> BlockImport<Block> for AuraBlockImport<Block, C, I,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
let hash = block.post_hash();
let slot = find_pre_digest::<Block, P>(&block.header)
let slot = find_pre_digest::<Block, P::Signature>(&block.header)
.expect("valid Aura headers must contain a predigest; \
header has been already verified; qed");
@@ -816,7 +812,7 @@ impl<Block: BlockT, C, I, P> BlockImport<Block> for AuraBlockImport<Block, C, I,
Error::<Block>::ParentUnavailable(parent_hash, hash)
).into()))?;
let parent_slot = find_pre_digest::<Block, P>(&parent_header)
let parent_slot = find_pre_digest::<Block, P::Signature>(&parent_header)
.expect("valid Aura headers contain a pre-digest; \
parent header has already been verified; qed");
@@ -848,7 +844,7 @@ pub fn import_queue<B, I, C, P, S, CAW>(
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B>,
C: 'static + ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + Send + Sync + AuxStore + HeaderBackend<B>,
I: BlockImport<B, Error=ConsensusError, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
DigestItemFor<B>: CompatibleDigestItem<P>,
DigestItemFor<B>: CompatibleDigestItem<P::Signature>,
P: Pair + Send + Sync + 'static,
P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode,
P::Signature: Encode + Decode,
@@ -858,7 +854,7 @@ pub fn import_queue<B, I, C, P, S, CAW>(
register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.get())?;
initialize_authorities_cache(&*client)?;
let verifier = AuraVerifier {
let verifier = AuraVerifier::<_, P, _> {
client,
inherent_data_providers,
phantom: PhantomData,
@@ -21,22 +21,19 @@
//! This implements the digests for AuRa, to allow the private
//! `CompatibleDigestItem` trait to appear in public interfaces.
use sp_core::Pair;
use sp_consensus_aura::AURA_ENGINE_ID;
use sp_runtime::generic::{DigestItem, OpaqueDigestItemId};
use crate::AURA_ENGINE_ID;
use sp_runtime::generic::DigestItem;
use sp_consensus_slots::Slot;
use codec::{Encode, Codec};
use std::fmt::Debug;
type Signature<P> = <P as Pair>::Signature;
use sp_std::fmt::Debug;
/// A digest item which is usable with aura consensus.
pub trait CompatibleDigestItem<P: Pair>: Sized {
pub trait CompatibleDigestItem<Signature>: Sized {
/// Construct a digest item which contains a signature on the hash.
fn aura_seal(signature: Signature<P>) -> Self;
fn aura_seal(signature: Signature) -> Self;
/// If this item is an Aura seal, return the signature.
fn as_aura_seal(&self) -> Option<Signature<P>>;
fn as_aura_seal(&self) -> Option<Signature>;
/// Construct a digest item which contains the slot number
fn aura_pre_digest(slot: Slot) -> Self;
@@ -45,17 +42,16 @@ pub trait CompatibleDigestItem<P: Pair>: Sized {
fn as_aura_pre_digest(&self) -> Option<Slot>;
}
impl<P, Hash> CompatibleDigestItem<P> for DigestItem<Hash> where
P: Pair,
Signature<P>: Codec,
impl<Signature, Hash> CompatibleDigestItem<Signature> for DigestItem<Hash> where
Signature: Codec,
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
{
fn aura_seal(signature: Signature<P>) -> Self {
fn aura_seal(signature: Signature) -> Self {
DigestItem::Seal(AURA_ENGINE_ID, signature.encode())
}
fn as_aura_seal(&self) -> Option<Signature<P>> {
self.try_to(OpaqueDigestItemId::Seal(&AURA_ENGINE_ID))
fn as_aura_seal(&self) -> Option<Signature> {
self.seal_try_to(&AURA_ENGINE_ID)
}
fn aura_pre_digest(slot: Slot) -> Self {
@@ -63,6 +59,6 @@ impl<P, Hash> CompatibleDigestItem<P> for DigestItem<Hash> where
}
fn as_aura_pre_digest(&self) -> Option<Slot> {
self.try_to(OpaqueDigestItemId::PreRuntime(&AURA_ENGINE_ID))
self.pre_runtime_try_to(&AURA_ENGINE_ID)
}
}
@@ -23,6 +23,7 @@ use codec::{Encode, Decode, Codec};
use sp_std::vec::Vec;
use sp_runtime::ConsensusEngineId;
pub mod digests;
pub mod inherents;
pub mod sr25519 {
@@ -23,7 +23,7 @@ use super::{
};
use codec::{Codec, Decode, Encode};
use sp_std::vec::Vec;
use sp_runtime::{generic::OpaqueDigestItemId, DigestItem, RuntimeDebug};
use sp_runtime::{DigestItem, RuntimeDebug};
use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
@@ -184,7 +184,7 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
}
fn as_babe_pre_digest(&self) -> Option<PreDigest> {
self.try_to(OpaqueDigestItemId::PreRuntime(&BABE_ENGINE_ID))
self.pre_runtime_try_to(&BABE_ENGINE_ID)
}
fn babe_seal(signature: AuthoritySignature) -> Self {
@@ -192,11 +192,11 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
}
fn as_babe_seal(&self) -> Option<AuthoritySignature> {
self.try_to(OpaqueDigestItemId::Seal(&BABE_ENGINE_ID))
self.seal_try_to(&BABE_ENGINE_ID)
}
fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor> {
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
self.consensus_try_to(&BABE_ENGINE_ID)
.and_then(|x: super::ConsensusLog| match x {
super::ConsensusLog::NextEpochData(n) => Some(n),
_ => None,
@@ -204,7 +204,7 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
}
fn as_next_config_descriptor(&self) -> Option<NextConfigDescriptor> {
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
self.consensus_try_to(&BABE_ENGINE_ID)
.and_then(|x: super::ConsensusLog| match x {
super::ConsensusLog::NextConfigData(n) => Some(n),
_ => None,
@@ -62,16 +62,12 @@ impl<Hash> Digest<Hash> {
/// Get reference to the first digest item that matches the passed predicate.
pub fn log<T: ?Sized, F: Fn(&DigestItem<Hash>) -> Option<&T>>(&self, predicate: F) -> Option<&T> {
self.logs().iter()
.filter_map(predicate)
.next()
self.logs().iter().find_map(predicate)
}
/// Get a conversion of the first digest item that successfully converts using the function.
pub fn convert_first<T, F: Fn(&DigestItem<Hash>) -> Option<T>>(&self, predicate: F) -> Option<T> {
self.logs().iter()
.filter_map(predicate)
.next()
self.logs().iter().find_map(predicate)
}
}
@@ -251,10 +247,7 @@ impl<Hash> DigestItem<Hash> {
/// Returns Some if `self` is a `DigestItem::Other`.
pub fn as_other(&self) -> Option<&[u8]> {
match *self {
DigestItem::Other(ref v) => Some(&v[..]),
_ => None,
}
self.dref().as_other()
}
/// Returns the opaque data contained in the item if `Some` if this entry has the id given.
@@ -267,6 +260,29 @@ impl<Hash> DigestItem<Hash> {
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
self.dref().try_to::<T>(id)
}
/// Try to match this to a `Self::Seal`, check `id` matches and decode it.
///
/// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.dref().seal_try_to(id)
}
/// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
///
/// Returns `None` if this isn't a consensus item, the `id` doesn't match or
/// when the decoding fails.
pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.dref().consensus_try_to(id)
}
/// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
///
/// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
/// when the decoding fails.
pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.dref().pre_runtime_try_to(id)
}
}
impl<Hash: Encode> Encode for DigestItem<Hash> {
@@ -374,6 +390,41 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
self.try_as_raw(id).and_then(|mut x| Decode::decode(&mut x).ok())
}
/// Try to match this to a `Self::Seal`, check `id` matches and decode it.
///
/// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
match self {
Self::Seal(v, s) if *v == id =>
Decode::decode(&mut &s[..]).ok(),
_ => None,
}
}
/// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
///
/// Returns `None` if this isn't a consensus item, the `id` doesn't match or
/// when the decoding fails.
pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
match self {
Self::Consensus(v, s) if *v == id =>
Decode::decode(&mut &s[..]).ok(),
_ => None,
}
}
/// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
///
/// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
/// when the decoding fails.
pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
match self {
Self::PreRuntime(v, s) if *v == id =>
Decode::decode(&mut &s[..]).ok(),
_ => None,
}
}
}
impl<'a, Hash: Encode> Encode for DigestItemRef<'a, Hash> {