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
+17 -17
View File
@@ -33,7 +33,7 @@ use frame_support::{
};
use sp_io;
use sp_core::{H256, U256, crypto::{IsWrappedBy, KeyTypeId, Pair}};
use sp_consensus_babe::{AuthorityId, AuthorityPair, SlotNumber};
use sp_consensus_babe::{AuthorityId, AuthorityPair, Slot};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
use sp_staking::SessionIndex;
use pallet_staking::EraIndex;
@@ -255,14 +255,14 @@ pub fn go_to_block(n: u64, s: u64) {
System::parent_hash()
};
let pre_digest = make_secondary_plain_pre_digest(0, s);
let pre_digest = make_secondary_plain_pre_digest(0, s.into());
System::initialize(&n, &parent_hash, &pre_digest, InitKind::Full);
System::set_block_number(n);
Timestamp::set_timestamp(n);
if s > 1 {
CurrentSlot::put(s);
CurrentSlot::put(Slot::from(s));
}
System::on_initialize(n);
@@ -272,8 +272,8 @@ pub fn go_to_block(n: u64, s: u64) {
/// Slots will grow accordingly to blocks
pub fn progress_to_block(n: u64) {
let mut slot = Babe::current_slot() + 1;
for i in System::block_number()+1..=n {
let mut slot = u64::from(Babe::current_slot()) + 1;
for i in System::block_number() + 1 ..= n {
go_to_block(i, slot);
slot += 1;
}
@@ -294,14 +294,14 @@ pub fn start_era(era_index: EraIndex) {
pub fn make_primary_pre_digest(
authority_index: sp_consensus_babe::AuthorityIndex,
slot_number: sp_consensus_babe::SlotNumber,
slot: sp_consensus_babe::Slot,
vrf_output: VRFOutput,
vrf_proof: VRFProof,
) -> Digest {
let digest_data = sp_consensus_babe::digests::PreDigest::Primary(
sp_consensus_babe::digests::PrimaryPreDigest {
authority_index,
slot_number,
slot,
vrf_output,
vrf_proof,
}
@@ -312,12 +312,12 @@ pub fn make_primary_pre_digest(
pub fn make_secondary_plain_pre_digest(
authority_index: sp_consensus_babe::AuthorityIndex,
slot_number: sp_consensus_babe::SlotNumber,
slot: sp_consensus_babe::Slot,
) -> Digest {
let digest_data = sp_consensus_babe::digests::PreDigest::SecondaryPlain(
sp_consensus_babe::digests::SecondaryPlainPreDigest {
authority_index,
slot_number,
slot,
}
);
let log = DigestItem::PreRuntime(sp_consensus_babe::BABE_ENGINE_ID, digest_data.encode());
@@ -326,14 +326,14 @@ pub fn make_secondary_plain_pre_digest(
pub fn make_secondary_vrf_pre_digest(
authority_index: sp_consensus_babe::AuthorityIndex,
slot_number: sp_consensus_babe::SlotNumber,
slot: sp_consensus_babe::Slot,
vrf_output: VRFOutput,
vrf_proof: VRFProof,
) -> Digest {
let digest_data = sp_consensus_babe::digests::PreDigest::SecondaryVRF(
sp_consensus_babe::digests::SecondaryVRFPreDigest {
authority_index,
slot_number,
slot,
vrf_output,
vrf_proof,
}
@@ -343,11 +343,11 @@ pub fn make_secondary_vrf_pre_digest(
}
pub fn make_vrf_output(
slot_number: u64,
slot: Slot,
pair: &sp_consensus_babe::AuthorityPair
) -> (VRFOutput, VRFProof, [u8; 32]) {
let pair = sp_core::sr25519::Pair::from_ref(pair).as_ref();
let transcript = sp_consensus_babe::make_transcript(&Babe::randomness(), slot_number, 0);
let transcript = sp_consensus_babe::make_transcript(&Babe::randomness(), slot, 0);
let vrf_inout = pair.vrf_sign(transcript);
let vrf_randomness: sp_consensus_vrf::schnorrkel::Randomness = vrf_inout.0
.make_bytes::<[u8; 32]>(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT);
@@ -435,7 +435,7 @@ pub fn new_test_ext_raw_authorities(authorities: Vec<AuthorityId>) -> sp_io::Tes
pub fn generate_equivocation_proof(
offender_authority_index: u32,
offender_authority_pair: &AuthorityPair,
slot_number: SlotNumber,
slot: Slot,
) -> sp_consensus_babe::EquivocationProof<Header> {
use sp_consensus_babe::digests::CompatibleDigestItem;
@@ -444,7 +444,7 @@ pub fn generate_equivocation_proof(
let make_header = || {
let parent_hash = System::parent_hash();
let pre_digest = make_secondary_plain_pre_digest(offender_authority_index, slot_number);
let pre_digest = make_secondary_plain_pre_digest(offender_authority_index, slot);
System::initialize(&current_block, &parent_hash, &pre_digest, InitKind::Full);
System::set_block_number(current_block);
Timestamp::set_timestamp(current_block);
@@ -469,10 +469,10 @@ pub fn generate_equivocation_proof(
seal_header(&mut h2);
// restore previous runtime state
go_to_block(current_block, current_slot);
go_to_block(current_block, *current_slot);
sp_consensus_babe::EquivocationProof {
slot_number,
slot,
offender: offender_authority_pair.public(),
first_header: h1,
second_header: h2,