Introduce a Slot type (#7997)

* Introduce a `Slot` type

Instead of having some type definition that only was used in half of the
code or directly using `u64`, this adds a new unit type wrapper `Slot`.
This makes it especially easy for the outside api to know what type is
expected/returned.

* Change epoch duratioC

* rename all instances of slot number to slot

* Make the constructor private

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-01-28 20:44:22 +01:00
committed by GitHub
parent 6c2dd28dfb
commit b6294418f8
34 changed files with 549 additions and 445 deletions
+15 -15
View File
@@ -25,7 +25,7 @@ use frame_support::{
};
use mock::*;
use pallet_session::ShouldEndSession;
use sp_consensus_babe::AllowedSlots;
use sp_consensus_babe::{AllowedSlots, Slot};
use sp_core::crypto::Pair;
const EMPTY_RANDOMNESS: [u8; 32] = [
@@ -62,7 +62,7 @@ fn first_block_epoch_zero_start() {
let (pairs, mut ext) = new_test_ext_with_pairs(4);
ext.execute_with(|| {
let genesis_slot = 100;
let genesis_slot = Slot::from(100);
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let first_vrf = vrf_output;
@@ -73,7 +73,7 @@ fn first_block_epoch_zero_start() {
vrf_proof,
);
assert_eq!(Babe::genesis_slot(), 0);
assert_eq!(Babe::genesis_slot(), Slot::from(0));
System::initialize(
&1,
&Default::default(),
@@ -120,7 +120,7 @@ fn author_vrf_output_for_primary() {
let (pairs, mut ext) = new_test_ext_with_pairs(1);
ext.execute_with(|| {
let genesis_slot = 10;
let genesis_slot = Slot::from(10);
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let primary_pre_digest = make_primary_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
@@ -146,7 +146,7 @@ fn author_vrf_output_for_secondary_vrf() {
let (pairs, mut ext) = new_test_ext_with_pairs(1);
ext.execute_with(|| {
let genesis_slot = 10;
let genesis_slot = Slot::from(10);
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let secondary_vrf_pre_digest = make_secondary_vrf_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
@@ -170,7 +170,7 @@ fn author_vrf_output_for_secondary_vrf() {
#[test]
fn no_author_vrf_output_for_secondary_plain() {
new_test_ext(1).execute_with(|| {
let genesis_slot = 10;
let genesis_slot = Slot::from(10);
let secondary_plain_pre_digest = make_secondary_plain_pre_digest(0, genesis_slot);
System::initialize(
@@ -205,17 +205,17 @@ fn can_predict_next_epoch_change() {
assert_eq!(<Test as Config>::EpochDuration::get(), 3);
// this sets the genesis slot to 6;
go_to_block(1, 6);
assert_eq!(Babe::genesis_slot(), 6);
assert_eq!(Babe::current_slot(), 6);
assert_eq!(*Babe::genesis_slot(), 6);
assert_eq!(*Babe::current_slot(), 6);
assert_eq!(Babe::epoch_index(), 0);
progress_to_block(5);
assert_eq!(Babe::epoch_index(), 5 / 3);
assert_eq!(Babe::current_slot(), 10);
assert_eq!(*Babe::current_slot(), 10);
// next epoch change will be at
assert_eq!(Babe::current_epoch_start(), 9); // next change will be 12, 2 slots from now
assert_eq!(*Babe::current_epoch_start(), 9); // next change will be 12, 2 slots from now
assert_eq!(Babe::next_expected_epoch_change(System::block_number()), Some(5 + 2));
})
}
@@ -226,8 +226,8 @@ fn can_enact_next_config() {
assert_eq!(<Test as Config>::EpochDuration::get(), 3);
// this sets the genesis slot to 6;
go_to_block(1, 6);
assert_eq!(Babe::genesis_slot(), 6);
assert_eq!(Babe::current_slot(), 6);
assert_eq!(*Babe::genesis_slot(), 6);
assert_eq!(*Babe::current_slot(), 6);
assert_eq!(Babe::epoch_index(), 0);
go_to_block(2, 7);
@@ -269,12 +269,12 @@ fn can_fetch_current_and_next_epoch_data() {
let current_epoch = Babe::current_epoch();
assert_eq!(current_epoch.epoch_index, 3);
assert_eq!(current_epoch.start_slot, 10);
assert_eq!(*current_epoch.start_slot, 10);
assert_eq!(current_epoch.authorities.len(), 5);
let next_epoch = Babe::next_epoch();
assert_eq!(next_epoch.epoch_index, 4);
assert_eq!(next_epoch.start_slot, 13);
assert_eq!(*next_epoch.start_slot, 13);
assert_eq!(next_epoch.authorities.len(), 5);
// the on-chain randomness should always change across epochs
@@ -572,7 +572,7 @@ fn report_equivocation_invalid_equivocation_proof() {
&offending_authority_pair,
CurrentSlot::get(),
);
equivocation_proof.slot_number = 0;
equivocation_proof.slot = Slot::from(0);
assert_invalid_equivocation(equivocation_proof.clone());
// different slot numbers in headers