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
@@ -19,7 +19,7 @@
use super::{
AllowedSlots, AuthorityId, AuthorityIndex, AuthoritySignature, BabeAuthorityWeight,
BabeEpochConfiguration, SlotNumber, BABE_ENGINE_ID,
BabeEpochConfiguration, Slot, BABE_ENGINE_ID,
};
use codec::{Codec, Decode, Encode};
use sp_std::vec::Vec;
@@ -32,8 +32,8 @@ use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
pub struct PrimaryPreDigest {
/// Authority index
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
/// Slot
pub slot: Slot,
/// VRF output
pub vrf_output: VRFOutput,
/// VRF proof
@@ -50,8 +50,8 @@ pub struct SecondaryPlainPreDigest {
/// it makes things easier for higher-level users of the chain data to
/// be aware of the author of a secondary-slot block.
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
/// Slot
pub slot: Slot,
}
/// BABE secondary deterministic slot assignment with VRF outputs.
@@ -59,8 +59,8 @@ pub struct SecondaryPlainPreDigest {
pub struct SecondaryVRFPreDigest {
/// Authority index
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
/// Slot
pub slot: Slot,
/// VRF output
pub vrf_output: VRFOutput,
/// VRF proof
@@ -93,12 +93,12 @@ impl PreDigest {
}
}
/// Returns the slot number of the pre digest.
pub fn slot_number(&self) -> SlotNumber {
/// Returns the slot of the pre digest.
pub fn slot(&self) -> Slot {
match self {
PreDigest::Primary(primary) => primary.slot_number,
PreDigest::SecondaryPlain(secondary) => secondary.slot_number,
PreDigest::SecondaryVRF(secondary) => secondary.slot_number,
PreDigest::Primary(primary) => primary.slot,
PreDigest::SecondaryPlain(secondary) => secondary.slot,
PreDigest::SecondaryVRF(secondary) => secondary.slot,
}
}
@@ -31,7 +31,7 @@ use sp_std::result::Result;
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babeslot";
/// The type of the BABE inherent.
pub type InherentType = u64;
pub type InherentType = sp_consensus_slots::Slot;
/// Auxiliary trait to extract BABE inherent data.
pub trait BabeInherentData {
/// Get BABE inherent data.
@@ -82,8 +82,8 @@ impl ProvideInherentData for InherentDataProvider {
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
let timestamp = inherent_data.timestamp_inherent_data()?;
let slot_number = timestamp / self.slot_duration;
inherent_data.put_data(INHERENT_IDENTIFIER, &slot_number)
let slot = timestamp / self.slot_duration;
inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
}
fn error_to_string(&self, error: &[u8]) -> Option<String> {
+15 -16
View File
@@ -76,8 +76,7 @@ pub const MEDIAN_ALGORITHM_CARDINALITY: usize = 1200; // arbitrary suggestion by
/// The index of an authority.
pub type AuthorityIndex = u32;
/// A slot number.
pub use sp_consensus_slots::SlotNumber;
pub use sp_consensus_slots::Slot;
/// An equivocation proof for multiple block authorships on the same slot (i.e. double vote).
pub type EquivocationProof<H> = sp_consensus_slots::EquivocationProof<H, AuthorityId>;
@@ -93,11 +92,11 @@ pub type BabeBlockWeight = u32;
/// Make a VRF transcript from given randomness, slot number and epoch.
pub fn make_transcript(
randomness: &Randomness,
slot_number: u64,
slot: Slot,
epoch: u64,
) -> Transcript {
let mut transcript = Transcript::new(&BABE_ENGINE_ID);
transcript.append_u64(b"slot number", slot_number);
transcript.append_u64(b"slot number", *slot);
transcript.append_u64(b"current epoch", epoch);
transcript.append_message(b"chain randomness", &randomness[..]);
transcript
@@ -107,13 +106,13 @@ pub fn make_transcript(
#[cfg(feature = "std")]
pub fn make_transcript_data(
randomness: &Randomness,
slot_number: u64,
slot: Slot,
epoch: u64,
) -> VRFTranscriptData {
VRFTranscriptData {
label: &BABE_ENGINE_ID,
items: vec![
("slot number", VRFTranscriptValue::U64(slot_number)),
("slot number", VRFTranscriptValue::U64(*slot)),
("current epoch", VRFTranscriptValue::U64(epoch)),
("chain randomness", VRFTranscriptValue::Bytes(randomness.to_vec())),
]
@@ -147,7 +146,7 @@ pub struct BabeGenesisConfigurationV1 {
pub slot_duration: u64,
/// The duration of epochs in slots.
pub epoch_length: SlotNumber,
pub epoch_length: u64,
/// A constant value that is used in the threshold calculation formula.
/// Expressed as a rational where the first member of the tuple is the
@@ -195,7 +194,7 @@ pub struct BabeGenesisConfiguration {
pub slot_duration: u64,
/// The duration of epochs in slots.
pub epoch_length: SlotNumber,
pub epoch_length: u64,
/// A constant value that is used in the threshold calculation formula.
/// Expressed as a rational where the first member of the tuple is the
@@ -303,8 +302,8 @@ where
// both headers must be targetting the same slot and it must
// be the same as the one in the proof.
if proof.slot_number != first_pre_digest.slot_number() ||
first_pre_digest.slot_number() != second_pre_digest.slot_number()
if proof.slot != first_pre_digest.slot() ||
first_pre_digest.slot() != second_pre_digest.slot()
{
return None;
}
@@ -356,9 +355,9 @@ pub struct Epoch {
/// The epoch index.
pub epoch_index: u64,
/// The starting slot of the epoch.
pub start_slot: SlotNumber,
pub start_slot: Slot,
/// The duration of this epoch.
pub duration: SlotNumber,
pub duration: u64,
/// The authorities and their weights.
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
/// Randomness for this epoch.
@@ -376,8 +375,8 @@ sp_api::decl_runtime_apis! {
#[changed_in(2)]
fn configuration() -> BabeGenesisConfigurationV1;
/// Returns the slot number that started the current epoch.
fn current_epoch_start() -> SlotNumber;
/// Returns the slot that started the current epoch.
fn current_epoch_start() -> Slot;
/// Returns information regarding the current epoch.
fn current_epoch() -> Epoch;
@@ -391,14 +390,14 @@ sp_api::decl_runtime_apis! {
/// session historical module to prove that a given authority key is
/// tied to a given staking identity during a specific session. Proofs
/// of key ownership are necessary for submitting equivocation reports.
/// NOTE: even though the API takes a `slot_number` as parameter the current
/// NOTE: even though the API takes a `slot` as parameter the current
/// implementations ignores this parameter and instead relies on this
/// method being called at the correct block height, i.e. any point at
/// which the epoch for the given slot is live on-chain. Future
/// implementations will instead use indexed data through an offchain
/// worker, not requiring older states to be available.
fn generate_key_ownership_proof(
slot_number: SlotNumber,
slot: Slot,
authority_id: AuthorityId,
) -> Option<OpaqueKeyOwnershipProof>;