babe: allow skipping over empty epochs (#11727)

* babe: allow skipping epochs in pallet

* babe: detect and skip epochs on client

* babe: cleaner epoch util functions

* babe: add test for runtime handling of skipped epochs

* babe: simpler implementation of client handling of skipped epochs

* babe: test client-side handling of skipped epochs

* babe: add comments on client-side skipped epochs

* babe: remove emptyline

* babe: make it resilient to forks

* babe: typo

* babe: overflow-safe math

* babe: add test for skipping epochs across different forks

* Fix tests

* FMT

Co-authored-by: Bastian Köcher <info@kchr.de>
This commit is contained in:
André Silva
2022-12-24 00:03:31 +00:00
committed by GitHub
parent e053d34cc5
commit 017cf70378
5 changed files with 386 additions and 23 deletions
@@ -360,6 +360,25 @@ pub struct Epoch {
pub config: BabeEpochConfiguration,
}
/// Returns the epoch index the given slot belongs to.
pub fn epoch_index(slot: Slot, genesis_slot: Slot, epoch_duration: u64) -> u64 {
*slot.saturating_sub(genesis_slot) / epoch_duration
}
/// Returns the first slot at the given epoch index.
pub fn epoch_start_slot(epoch_index: u64, genesis_slot: Slot, epoch_duration: u64) -> Slot {
// (epoch_index * epoch_duration) + genesis_slot
const PROOF: &str = "slot number is u64; it should relate in some way to wall clock time; \
if u64 is not enough we should crash for safety; qed.";
epoch_index
.checked_mul(epoch_duration)
.and_then(|slot| slot.checked_add(*genesis_slot))
.expect(PROOF)
.into()
}
sp_api::decl_runtime_apis! {
/// API necessary for block authorship with BABE.
#[api_version(2)]