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
+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;