Split PrimaryPreDigest and SecondaryPreDigest (#5373)

* Split PrimaryPreDigest and SecondaryPreDigest

* Update client/consensus/babe/src/lib.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* Update primitives/consensus/babe/src/digests.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* Update primitives/consensus/babe/src/digests.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* Update primitives/consensus/babe/src/digests.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

Co-authored-by: André Silva <andre.beat@gmail.com>
This commit is contained in:
Wei Tang
2020-03-24 18:07:51 +01:00
committed by GitHub
parent 06525e2b47
commit 2e558908e6
8 changed files with 98 additions and 88 deletions
@@ -27,40 +27,67 @@ use codec::{Decode, Encode};
#[cfg(feature = "std")]
use codec::Codec;
use sp_std::vec::Vec;
use sp_runtime::RuntimeDebug;
use sp_consensus_vrf::schnorrkel::{self, Randomness};
#[cfg(feature = "std")]
use sp_consensus_vrf::schnorrkel::SignatureError;
/// Raw BABE primary slot assignment pre-digest.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct RawPrimaryPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::RawVRFProof> {
/// Authority index
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
/// VRF output
pub vrf_output: VRFOutput,
/// VRF proof
pub vrf_proof: VRFProof,
}
#[cfg(feature = "std")]
/// BABE primary slot assignment pre-digest for std environment.
pub type PrimaryPreDigest = RawPrimaryPreDigest<schnorrkel::VRFOutput, schnorrkel::VRFProof>;
#[cfg(feature = "std")]
impl TryFrom<RawPrimaryPreDigest> for PrimaryPreDigest {
type Error = SignatureError;
fn try_from(raw: RawPrimaryPreDigest) -> Result<PrimaryPreDigest, SignatureError> {
Ok(PrimaryPreDigest {
authority_index: raw.authority_index,
slot_number: raw.slot_number,
vrf_output: raw.vrf_output.try_into()?,
vrf_proof: raw.vrf_proof.try_into()?,
})
}
}
/// BABE secondary slot assignment pre-digest.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct SecondaryPreDigest {
/// Authority index
///
/// This is not strictly-speaking necessary, since the secondary slots
/// are assigned based on slot number and epoch randomness. But including
/// it makes things easier for higher-level users of the chain data to
/// be aware of the author of a secondary-slot block.
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
}
/// A BABE pre-runtime digest. This contains all data required to validate a
/// block and for the BABE runtime module. Slots can be assigned to a primary
/// (VRF based) and to a secondary (slot number based).
#[derive(Clone, Debug, Encode, Decode)]
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub enum RawPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::RawVRFProof> {
/// A primary VRF-based slot assignment.
#[codec(index = "1")]
Primary {
/// Authority index
authority_index: super::AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// VRF output
vrf_output: VRFOutput,
/// VRF proof
vrf_proof: VRFProof,
},
Primary(RawPrimaryPreDigest<VRFOutput, VRFProof>),
/// A secondary deterministic slot assignment.
#[codec(index = "2")]
Secondary {
/// Authority index
///
/// This is not strictly-speaking necessary, since the secondary slots
/// are assigned based on slot number and epoch randomness. But including
/// it makes things easier for higher-level users of the chain data to
/// be aware of the author of a secondary-slot block.
authority_index: super::AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
},
Secondary(SecondaryPreDigest),
}
#[cfg(feature = "std")]
@@ -71,16 +98,16 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
/// Returns the slot number of the pre digest.
pub fn authority_index(&self) -> AuthorityIndex {
match self {
RawPreDigest::Primary { authority_index, .. } => *authority_index,
RawPreDigest::Secondary { authority_index, .. } => *authority_index,
RawPreDigest::Primary(primary) => primary.authority_index,
RawPreDigest::Secondary(secondary) => secondary.authority_index,
}
}
/// Returns the slot number of the pre digest.
pub fn slot_number(&self) -> SlotNumber {
match self {
RawPreDigest::Primary { slot_number, .. } => *slot_number,
RawPreDigest::Secondary { slot_number, .. } => *slot_number,
RawPreDigest::Primary(primary) => primary.slot_number,
RawPreDigest::Secondary(secondary) => secondary.slot_number,
}
}
@@ -88,8 +115,8 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
/// of the chain.
pub fn added_weight(&self) -> crate::BabeBlockWeight {
match self {
RawPreDigest::Primary { .. } => 1,
RawPreDigest::Secondary { .. } => 0,
RawPreDigest::Primary(_) => 1,
RawPreDigest::Secondary(_) => 0,
}
}
}
@@ -100,25 +127,15 @@ impl TryFrom<RawPreDigest> for PreDigest {
fn try_from(raw: RawPreDigest) -> Result<PreDigest, SignatureError> {
Ok(match raw {
RawPreDigest::Primary { authority_index, slot_number, vrf_output, vrf_proof } =>
RawPreDigest::Primary {
authority_index,
slot_number,
vrf_output: vrf_output.try_into()?,
vrf_proof: vrf_proof.try_into()?,
},
RawPreDigest::Secondary { authority_index, slot_number } =>
RawPreDigest::Secondary {
authority_index,
slot_number,
}
RawPreDigest::Primary(primary) => PreDigest::Primary(primary.try_into()?),
RawPreDigest::Secondary(secondary) => PreDigest::Secondary(secondary),
})
}
}
/// Information about the next epoch. This is broadcast in the first block
/// of the epoch.
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone, sp_runtime::RuntimeDebug)]
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone, RuntimeDebug)]
pub struct NextEpochDescriptor {
/// The authorities.
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,