babe: secondary blocks with VRF (#5501)

* babe: secondary blocks with VRF

* Fix node runtime compile

* Fix test-utils runtime interface

* Fix babe tests

* typo: v == 2

* babe: support online configuration upgrades

* Fix rpc tests

* Fix runtime version tests

* Switch to use NextConfigDescriptor instead of changing runtime interface

* Fix tests

* epoch-changes: map function that allows converting with different epoch types

* Add migration script for the epoch config change

* Fix docs for PrimaryAndSecondaryVRFSlots

* Add docs of `SecondaryVRF` in babe crate

* babe-primitives: Secondary -> SecondaryPlain

* babe-client: Secondary -> SecondaryPlain

* Fix migration tests

* test-utils-runtime: Secondary -> SecondaryPlain

* Fix missing name change in test-utils-runtime

* Fix migration: Epoch should be EpochV0

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

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

* Fix new epochChanges version

* Fix babe-primitives naming changes

* Fix merge issues in babe-client

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
Co-authored-by: André Silva <andre.beat@gmail.com>
This commit is contained in:
Wei Tang
2020-04-24 17:03:03 +02:00
committed by GitHub
parent 8a3dcd6862
commit 969720c2ad
12 changed files with 265 additions and 52 deletions
@@ -65,7 +65,7 @@ impl TryFrom<RawPrimaryPreDigest> for PrimaryPreDigest {
/// BABE secondary slot assignment pre-digest.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct SecondaryPreDigest {
pub struct SecondaryPlainPreDigest {
/// Authority index
///
/// This is not strictly-speaking necessary, since the secondary slots
@@ -77,6 +77,37 @@ pub struct SecondaryPreDigest {
pub slot_number: SlotNumber,
}
/// BABE secondary deterministic slot assignment with VRF outputs.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct RawSecondaryVRFPreDigest<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 secondary slot assignment with VRF outputs pre-digest, for std environment.
pub type SecondaryVRFPreDigest = RawSecondaryVRFPreDigest<schnorrkel::VRFOutput, schnorrkel::VRFProof>;
#[cfg(feature = "std")]
impl TryFrom<RawSecondaryVRFPreDigest> for SecondaryVRFPreDigest {
type Error = SignatureError;
fn try_from(raw: RawSecondaryVRFPreDigest) -> Result<SecondaryVRFPreDigest, SignatureError> {
Ok(SecondaryVRFPreDigest {
authority_index: raw.authority_index,
slot_number: raw.slot_number,
vrf_output: raw.vrf_output.try_into()?,
vrf_proof: raw.vrf_proof.try_into()?,
})
}
}
/// 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).
@@ -87,7 +118,10 @@ pub enum RawPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::R
Primary(RawPrimaryPreDigest<VRFOutput, VRFProof>),
/// A secondary deterministic slot assignment.
#[codec(index = "2")]
Secondary(SecondaryPreDigest),
SecondaryPlain(SecondaryPlainPreDigest),
/// A secondary deterministic slot assignment with VRF outputs.
#[codec(index = "3")]
SecondaryVRF(RawSecondaryVRFPreDigest<VRFOutput, VRFProof>),
}
#[cfg(feature = "std")]
@@ -99,7 +133,8 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
pub fn authority_index(&self) -> AuthorityIndex {
match self {
RawPreDigest::Primary(primary) => primary.authority_index,
RawPreDigest::Secondary(secondary) => secondary.authority_index,
RawPreDigest::SecondaryPlain(secondary) => secondary.authority_index,
RawPreDigest::SecondaryVRF(secondary) => secondary.authority_index,
}
}
@@ -107,7 +142,8 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
pub fn slot_number(&self) -> SlotNumber {
match self {
RawPreDigest::Primary(primary) => primary.slot_number,
RawPreDigest::Secondary(secondary) => secondary.slot_number,
RawPreDigest::SecondaryPlain(secondary) => secondary.slot_number,
RawPreDigest::SecondaryVRF(secondary) => secondary.slot_number,
}
}
@@ -116,7 +152,7 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
pub fn added_weight(&self) -> crate::BabeBlockWeight {
match self {
RawPreDigest::Primary(_) => 1,
RawPreDigest::Secondary(_) => 0,
RawPreDigest::SecondaryPlain(_) | RawPreDigest::SecondaryVRF(_) => 0,
}
}
}
@@ -128,7 +164,8 @@ impl TryFrom<RawPreDigest> for PreDigest {
fn try_from(raw: RawPreDigest) -> Result<PreDigest, SignatureError> {
Ok(match raw {
RawPreDigest::Primary(primary) => PreDigest::Primary(primary.try_into()?),
RawPreDigest::Secondary(secondary) => PreDigest::Secondary(secondary),
RawPreDigest::SecondaryPlain(secondary) => PreDigest::SecondaryPlain(secondary),
RawPreDigest::SecondaryVRF(secondary) => PreDigest::SecondaryVRF(secondary.try_into()?),
})
}
}