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
+77 -2
View File
@@ -95,7 +95,7 @@ pub enum ConsensusLog {
/// Configuration data used by the BABE consensus engine.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct BabeGenesisConfiguration {
pub struct BabeGenesisConfigurationV1 {
/// The slot duration in milliseconds for BABE. Currently, only
/// the value provided by this type at genesis will be used.
///
@@ -124,6 +124,76 @@ pub struct BabeGenesisConfiguration {
pub secondary_slots: bool,
}
impl From<BabeGenesisConfigurationV1> for BabeGenesisConfiguration {
fn from(v1: BabeGenesisConfigurationV1) -> Self {
Self {
slot_duration: v1.slot_duration,
epoch_length: v1.epoch_length,
c: v1.c,
genesis_authorities: v1.genesis_authorities,
randomness: v1.randomness,
allowed_slots: if v1.secondary_slots {
AllowedSlots::PrimaryAndSecondaryPlainSlots
} else {
AllowedSlots::PrimarySlots
},
}
}
}
/// Configuration data used by the BABE consensus engine.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct BabeGenesisConfiguration {
/// The slot duration in milliseconds for BABE. Currently, only
/// the value provided by this type at genesis will be used.
///
/// Dynamic slot duration may be supported in the future.
pub slot_duration: u64,
/// The duration of epochs in slots.
pub epoch_length: SlotNumber,
/// A constant value that is used in the threshold calculation formula.
/// Expressed as a rational where the first member of the tuple is the
/// numerator and the second is the denominator. The rational should
/// represent a value between 0 and 1.
/// In the threshold formula calculation, `1 - c` represents the probability
/// of a slot being empty.
pub c: (u64, u64),
/// The authorities for the genesis epoch.
pub genesis_authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
/// The randomness for the genesis epoch.
pub randomness: Randomness,
/// Type of allowed slots.
pub allowed_slots: AllowedSlots,
}
/// Types of allowed slots.
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub enum AllowedSlots {
/// Only allow primary slots.
PrimarySlots,
/// Allow primary and secondary plain slots.
PrimaryAndSecondaryPlainSlots,
/// Allow primary and secondary VRF slots.
PrimaryAndSecondaryVRFSlots,
}
impl AllowedSlots {
/// Whether plain secondary slots are allowed.
pub fn is_secondary_plain_slots_allowed(&self) -> bool {
*self == Self::PrimaryAndSecondaryPlainSlots
}
/// Whether VRF secondary slots are allowed.
pub fn is_secondary_vrf_slots_allowed(&self) -> bool {
*self == Self::PrimaryAndSecondaryVRFSlots
}
}
#[cfg(feature = "std")]
impl sp_consensus::SlotData for BabeGenesisConfiguration {
fn slot_duration(&self) -> u64 {
@@ -146,15 +216,20 @@ pub struct BabeEpochConfiguration {
/// Whether this chain should run with secondary slots, which are assigned
/// in round-robin manner.
pub secondary_slots: bool,
pub allowed_slots: AllowedSlots,
}
sp_api::decl_runtime_apis! {
/// API necessary for block authorship with BABE.
#[api_version(2)]
pub trait BabeApi {
/// Return the genesis configuration for BABE. The configuration is only read on genesis.
fn configuration() -> BabeGenesisConfiguration;
/// Return the configuration for BABE. Version 1.
#[changed_in(2)]
fn configuration() -> BabeGenesisConfigurationV1;
/// Returns the slot number that started the current epoch.
fn current_epoch_start() -> SlotNumber;
}