mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 22:51:13 +00:00
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:
@@ -0,0 +1,64 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2018-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Aura (Authority-Round) digests
|
||||
//!
|
||||
//! This implements the digests for AuRa, to allow the private
|
||||
//! `CompatibleDigestItem` trait to appear in public interfaces.
|
||||
|
||||
use crate::AURA_ENGINE_ID;
|
||||
use sp_runtime::generic::DigestItem;
|
||||
use sp_consensus_slots::Slot;
|
||||
use codec::{Encode, Codec};
|
||||
use sp_std::fmt::Debug;
|
||||
|
||||
/// A digest item which is usable with aura consensus.
|
||||
pub trait CompatibleDigestItem<Signature>: Sized {
|
||||
/// Construct a digest item which contains a signature on the hash.
|
||||
fn aura_seal(signature: Signature) -> Self;
|
||||
|
||||
/// If this item is an Aura seal, return the signature.
|
||||
fn as_aura_seal(&self) -> Option<Signature>;
|
||||
|
||||
/// Construct a digest item which contains the slot number
|
||||
fn aura_pre_digest(slot: Slot) -> Self;
|
||||
|
||||
/// If this item is an AuRa pre-digest, return the slot number
|
||||
fn as_aura_pre_digest(&self) -> Option<Slot>;
|
||||
}
|
||||
|
||||
impl<Signature, Hash> CompatibleDigestItem<Signature> for DigestItem<Hash> where
|
||||
Signature: Codec,
|
||||
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
|
||||
{
|
||||
fn aura_seal(signature: Signature) -> Self {
|
||||
DigestItem::Seal(AURA_ENGINE_ID, signature.encode())
|
||||
}
|
||||
|
||||
fn as_aura_seal(&self) -> Option<Signature> {
|
||||
self.seal_try_to(&AURA_ENGINE_ID)
|
||||
}
|
||||
|
||||
fn aura_pre_digest(slot: Slot) -> Self {
|
||||
DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())
|
||||
}
|
||||
|
||||
fn as_aura_pre_digest(&self) -> Option<Slot> {
|
||||
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> {
|
||||
|
||||
Reference in New Issue
Block a user