mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 01:11:08 +00:00
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:
@@ -20,6 +20,7 @@ sp-api = { version = "2.0.0", default-features = false, path = "../../api" }
|
||||
sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" }
|
||||
sp-inherents = { version = "2.0.0", default-features = false, path = "../../inherents" }
|
||||
sp-timestamp = { version = "2.0.0", default-features = false, path = "../../timestamp" }
|
||||
sp-consensus-slots = { version = "0.8.1", default-features = false, path = "../slots" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
|
||||
@@ -26,7 +26,7 @@ use sp_inherents::{InherentDataProviders, ProvideInherentData};
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
|
||||
|
||||
/// The type of the Aura inherent.
|
||||
pub type InherentType = u64;
|
||||
pub type InherentType = sp_consensus_slots::Slot;
|
||||
|
||||
/// Auxiliary trait to extract Aura inherent data.
|
||||
pub trait AuraInherentData {
|
||||
@@ -87,8 +87,8 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
|
||||
let timestamp = inherent_data.timestamp_inherent_data()?;
|
||||
let slot_num = timestamp / self.slot_duration;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot_num)
|
||||
let slot = timestamp / self.slot_duration;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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>;
|
||||
|
||||
|
||||
@@ -15,10 +15,12 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] }
|
||||
sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" }
|
||||
sp-arithmetic = { version = "2.0.0", default-features = false, path = "../../arithmetic" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"sp-runtime/std",
|
||||
"sp-arithmetic/std",
|
||||
]
|
||||
|
||||
@@ -21,8 +21,76 @@
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
|
||||
/// A slot number.
|
||||
pub type SlotNumber = u64;
|
||||
/// Unit type wrapper that represents a slot.
|
||||
#[derive(Debug, Encode, Decode, Eq, Clone, Copy, Default, Ord)]
|
||||
pub struct Slot(u64);
|
||||
|
||||
impl core::ops::Deref for Slot {
|
||||
type Target = u64;
|
||||
|
||||
fn deref(&self) -> &u64 {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::Add for Slot {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self(self.0 + other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::Add<u64> for Slot {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: u64) -> Self {
|
||||
Self(self.0 + other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Into<u64> + Copy> core::cmp::PartialEq<T> for Slot {
|
||||
fn eq(&self, eq: &T) -> bool {
|
||||
self.0 == (*eq).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Into<u64> + Copy> core::cmp::PartialOrd<T> for Slot {
|
||||
fn partial_cmp(&self, other: &T) -> Option<core::cmp::Ordering> {
|
||||
self.0.partial_cmp(&(*other).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Slot {
|
||||
/// Saturating addition.
|
||||
pub fn saturating_add<T: Into<u64>>(self, rhs: T) -> Self {
|
||||
Self(self.0.saturating_add(rhs.into()))
|
||||
}
|
||||
|
||||
/// Saturating subtraction.
|
||||
pub fn saturating_sub<T: Into<u64>>(self, rhs: T) -> Self {
|
||||
Self(self.0.saturating_sub(rhs.into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::fmt::Display for Slot {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for Slot {
|
||||
fn from(slot: u64) -> Slot {
|
||||
Slot(slot)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Slot> for u64 {
|
||||
fn from(slot: Slot) -> u64 {
|
||||
slot.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents an equivocation proof. An equivocation happens when a validator
|
||||
/// produces more than one block on the same slot. The proof of equivocation
|
||||
@@ -32,8 +100,8 @@ pub type SlotNumber = u64;
|
||||
pub struct EquivocationProof<Header, Id> {
|
||||
/// Returns the authority id of the equivocator.
|
||||
pub offender: Id,
|
||||
/// The slot number at which the equivocation happened.
|
||||
pub slot_number: SlotNumber,
|
||||
/// The slot at which the equivocation happened.
|
||||
pub slot: Slot,
|
||||
/// The first header involved in the equivocation.
|
||||
pub first_header: Header,
|
||||
/// The second header involved in the equivocation.
|
||||
|
||||
Reference in New Issue
Block a user