Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit b2f4332

Browse files
committed
BabeApi functions return a StandaloneEpoch
1 parent bbc7b0e commit b2f4332

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,11 +1205,11 @@ impl_runtime_apis! {
12051205
Babe::current_epoch_start()
12061206
}
12071207

1208-
fn current_epoch() -> sp_consensus_babe::Epoch {
1208+
fn current_epoch() -> sp_consensus_babe::StandaloneEpoch {
12091209
Babe::current_epoch()
12101210
}
12111211

1212-
fn next_epoch() -> sp_consensus_babe::Epoch {
1212+
fn next_epoch() -> sp_consensus_babe::StandaloneEpoch {
12131213
Babe::next_epoch()
12141214
}
12151215

frame/babe/src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use sp_timestamp::OnTimestampSet;
4343
use sp_consensus_babe::{
4444
digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
4545
inherents::{BabeInherentData, INHERENT_IDENTIFIER},
46-
BabeAuthorityWeight, ConsensusLog, Epoch, EquivocationProof, Slot, BABE_ENGINE_ID,
46+
BabeAuthorityWeight, ConsensusLog, EquivocationProof, Slot, BABE_ENGINE_ID, StandaloneEpoch,
47+
BabeEpochConfiguration,
4748
};
4849
use sp_consensus_vrf::schnorrkel;
4950
use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent};
@@ -188,6 +189,9 @@ decl_storage! {
188189
// variable to its underlying value.
189190
pub Randomness get(fn randomness): schnorrkel::Randomness;
190191

192+
/// The Current epoch configuration.
193+
EpochConfig: Option<BabeEpochConfiguration>;
194+
191195
/// Next epoch configuration, if changed.
192196
NextEpochConfig: Option<NextConfigDescriptor>;
193197

@@ -486,6 +490,8 @@ impl<T: Config> Module<T> {
486490
Self::deposit_consensus(ConsensusLog::NextEpochData(next_epoch));
487491

488492
if let Some(next_config) = NextEpochConfig::take() {
493+
let config: BabeEpochConfiguration = next_config.clone().into();
494+
EpochConfig::put(config);
489495
Self::deposit_consensus(ConsensusLog::NextConfigData(next_config));
490496
}
491497
}
@@ -498,30 +504,38 @@ impl<T: Config> Module<T> {
498504
}
499505

500506
/// Produces information about the current epoch.
501-
pub fn current_epoch() -> Epoch {
502-
Epoch {
507+
pub fn current_epoch() -> StandaloneEpoch {
508+
let epoch_config = EpochConfig::get().unwrap();
509+
510+
StandaloneEpoch {
503511
epoch_index: EpochIndex::get(),
504512
start_slot: Self::current_epoch_start(),
505513
duration: T::EpochDuration::get(),
506514
authorities: Self::authorities(),
507515
randomness: Self::randomness(),
516+
c: epoch_config.c,
517+
allowed_slots: epoch_config.allowed_slots,
508518
}
509519
}
510520

511521
/// Produces information about the next epoch (which was already previously
512522
/// announced).
513-
pub fn next_epoch() -> Epoch {
523+
pub fn next_epoch() -> StandaloneEpoch {
514524
let next_epoch_index = EpochIndex::get().checked_add(1).expect(
515525
"epoch index is u64; it is always only incremented by one; \
516526
if u64 is not enough we should crash for safety; qed.",
517527
);
518528

519-
Epoch {
529+
let epoch_config = EpochConfig::get().unwrap();
530+
531+
StandaloneEpoch {
520532
epoch_index: next_epoch_index,
521533
start_slot: Self::epoch_start(next_epoch_index),
522534
duration: T::EpochDuration::get(),
523535
authorities: NextAuthorities::get(),
524536
randomness: NextRandomness::get(),
537+
c: epoch_config.c,
538+
allowed_slots: epoch_config.allowed_slots,
525539
}
526540
}
527541

primitives/consensus/babe/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,31 @@ pub struct Epoch {
364364
pub randomness: [u8; VRF_OUTPUT_LENGTH],
365365
}
366366

367+
/// BABE epoch information with `c` and `allowed_slots`
368+
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
369+
pub struct StandaloneEpoch {
370+
/// The epoch index.
371+
pub epoch_index: u64,
372+
/// The starting slot of the epoch.
373+
pub start_slot: Slot,
374+
/// The duration of this epoch.
375+
pub duration: u64,
376+
/// The authorities and their weights.
377+
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
378+
/// Randomness for this epoch.
379+
pub randomness: [u8; VRF_OUTPUT_LENGTH],
380+
/// A constant value that is used in the threshold calculation formula.
381+
/// Expressed as a rational where the first member of the tuple is the
382+
/// numerator and the second is the denominator. The rational should
383+
/// represent a value between 0 and 1.
384+
/// In the threshold formula calculation, `1 - c` represents the probability
385+
/// of a slot being empty.
386+
pub c: (u64, u64),
387+
/// Whether this chain should run with secondary slots, which are assigned
388+
/// in round-robin manner.
389+
pub allowed_slots: AllowedSlots,
390+
}
391+
367392
sp_api::decl_runtime_apis! {
368393
/// API necessary for block authorship with BABE.
369394
#[api_version(2)]
@@ -379,11 +404,11 @@ sp_api::decl_runtime_apis! {
379404
fn current_epoch_start() -> Slot;
380405

381406
/// Returns information regarding the current epoch.
382-
fn current_epoch() -> Epoch;
407+
fn current_epoch() -> StandaloneEpoch;
383408

384409
/// Returns information regarding the next epoch (which was already
385410
/// previously announced).
386-
fn next_epoch() -> Epoch;
411+
fn next_epoch() -> StandaloneEpoch;
387412

388413
/// Generates a proof of key ownership for the given authority in the
389414
/// current epoch. An example usage of this module is coupled with the

test-utils/runtime/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,11 @@ cfg_if! {
743743
<pallet_babe::Module<Runtime>>::current_epoch_start()
744744
}
745745

746-
fn current_epoch() -> sp_consensus_babe::Epoch {
746+
fn current_epoch() -> sp_consensus_babe::StandaloneEpoch {
747747
<pallet_babe::Module<Runtime>>::current_epoch()
748748
}
749749

750-
fn next_epoch() -> sp_consensus_babe::Epoch {
750+
fn next_epoch() -> sp_consensus_babe::StandaloneEpoch {
751751
<pallet_babe::Module<Runtime>>::next_epoch()
752752
}
753753

@@ -1002,11 +1002,11 @@ cfg_if! {
10021002
<pallet_babe::Module<Runtime>>::current_epoch_start()
10031003
}
10041004

1005-
fn current_epoch() -> sp_consensus_babe::Epoch {
1005+
fn current_epoch() -> sp_consensus_babe::StandaloneEpoch {
10061006
<pallet_babe::Module<Runtime>>::current_epoch()
10071007
}
10081008

1009-
fn next_epoch() -> sp_consensus_babe::Epoch {
1009+
fn next_epoch() -> sp_consensus_babe::StandaloneEpoch {
10101010
<pallet_babe::Module<Runtime>>::next_epoch()
10111011
}
10121012

0 commit comments

Comments
 (0)