babe: support online configuration upgrades (#5514)

* babe: support online configuration upgrades

* 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 migration tests

* 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 unused imports

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Wei Tang
2020-04-24 15:59:14 +02:00
committed by GitHub
parent a01e608dff
commit 770cc24c47
12 changed files with 300 additions and 86 deletions
@@ -18,7 +18,7 @@
#[cfg(feature = "std")]
use super::{BABE_ENGINE_ID, AuthoritySignature};
use super::{AuthorityId, AuthorityIndex, SlotNumber, BabeAuthorityWeight};
use super::{AuthorityId, AuthorityIndex, SlotNumber, BabeAuthorityWeight, BabeEpochConfiguration};
#[cfg(feature = "std")]
use sp_runtime::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
@@ -135,7 +135,7 @@ impl TryFrom<RawPreDigest> for PreDigest {
/// Information about the next epoch. This is broadcast in the first block
/// of the epoch.
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone, RuntimeDebug)]
#[derive(Decode, Encode, PartialEq, Eq, Clone, RuntimeDebug)]
pub struct NextEpochDescriptor {
/// The authorities.
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
@@ -144,6 +144,10 @@ pub struct NextEpochDescriptor {
pub randomness: Randomness,
}
/// Information about the next epoch config, if changed. This is broadcast in the first
/// block of the epoch, and applies using the same rules as `NextEpochDescriptor`.
pub type NextConfigDescriptor = BabeEpochConfiguration;
/// A digest item which is usable with BABE consensus.
#[cfg(feature = "std")]
pub trait CompatibleDigestItem: Sized {
@@ -159,8 +163,11 @@ pub trait CompatibleDigestItem: Sized {
/// If this item is a BABE signature, return the signature.
fn as_babe_seal(&self) -> Option<AuthoritySignature>;
/// If this item is a BABE epoch, return it.
/// If this item is a BABE epoch descriptor, return it.
fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor>;
/// If this item is a BABE config descriptor, return it.
fn as_next_config_descriptor(&self) -> Option<NextConfigDescriptor>;
}
#[cfg(feature = "std")]
@@ -190,4 +197,12 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
_ => None,
})
}
fn as_next_config_descriptor(&self) -> Option<NextConfigDescriptor> {
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
.and_then(|x: super::ConsensusLog| match x {
super::ConsensusLog::NextConfigData(n) => Some(n),
_ => None,
})
}
}
+25 -8
View File
@@ -29,7 +29,7 @@ pub use sp_consensus_vrf::schnorrkel::{
use codec::{Encode, Decode};
use sp_std::vec::Vec;
use sp_runtime::{ConsensusEngineId, RuntimeDebug};
use crate::digests::NextEpochDescriptor;
use crate::digests::{NextEpochDescriptor, NextConfigDescriptor};
mod app {
use sp_application_crypto::{app_crypto, key_types::BABE, sr25519};
@@ -87,11 +87,15 @@ pub enum ConsensusLog {
/// Disable the authority with given index.
#[codec(index = "2")]
OnDisabled(AuthorityIndex),
/// The epoch has changed, and the epoch after the current one will
/// enact different epoch configurations.
#[codec(index = "3")]
NextConfigData(NextConfigDescriptor),
}
/// Configuration data used by the BABE consensus engine.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct BabeConfiguration {
pub struct BabeGenesisConfiguration {
/// The slot duration in milliseconds for BABE. Currently, only
/// the value provided by this type at genesis will be used.
///
@@ -121,7 +125,7 @@ pub struct BabeConfiguration {
}
#[cfg(feature = "std")]
impl sp_consensus::SlotData for BabeConfiguration {
impl sp_consensus::SlotData for BabeGenesisConfiguration {
fn slot_duration(&self) -> u64 {
self.slot_duration
}
@@ -129,14 +133,27 @@ impl sp_consensus::SlotData for BabeConfiguration {
const SLOT_KEY: &'static [u8] = b"babe_configuration";
}
/// Configuration data used by the BABE consensus engine.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct BabeEpochConfiguration {
/// 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),
/// Whether this chain should run with secondary slots, which are assigned
/// in round-robin manner.
pub secondary_slots: bool,
}
sp_api::decl_runtime_apis! {
/// API necessary for block authorship with BABE.
pub trait BabeApi {
/// Return the configuration for BABE. Currently,
/// only the value provided by this type at genesis will be used.
///
/// Dynamic configuration may be supported in the future.
fn configuration() -> BabeConfiguration;
/// Return the genesis configuration for BABE. The configuration is only read on genesis.
fn configuration() -> BabeGenesisConfiguration;
/// Returns the slot number that started the current epoch.
fn current_epoch_start() -> SlotNumber;