mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 22:01:04 +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:
@@ -78,13 +78,13 @@ pub trait Epoch {
|
||||
/// Descriptor for the next epoch.
|
||||
type NextEpochDescriptor;
|
||||
/// Type of the slot number.
|
||||
type SlotNumber: Ord + Copy;
|
||||
type Slot: Ord + Copy;
|
||||
|
||||
/// The starting slot of the epoch.
|
||||
fn start_slot(&self) -> Self::SlotNumber;
|
||||
fn start_slot(&self) -> Self::Slot;
|
||||
/// Produce the "end slot" of the epoch. This is NOT inclusive to the epoch,
|
||||
/// i.e. the slots covered by the epoch are `self.start_slot() .. self.end_slot()`.
|
||||
fn end_slot(&self) -> Self::SlotNumber;
|
||||
fn end_slot(&self) -> Self::Slot;
|
||||
/// Increment the epoch data, using the next epoch descriptor.
|
||||
fn increment(&self, descriptor: Self::NextEpochDescriptor) -> Self;
|
||||
}
|
||||
@@ -102,10 +102,10 @@ impl<'a, E: Epoch> From<&'a E> for EpochHeader<E> {
|
||||
#[derive(Eq, PartialEq, Encode, Decode, Debug)]
|
||||
pub struct EpochHeader<E: Epoch> {
|
||||
/// The starting slot of the epoch.
|
||||
pub start_slot: E::SlotNumber,
|
||||
pub start_slot: E::Slot,
|
||||
/// The end slot of the epoch. This is NOT inclusive to the epoch,
|
||||
/// i.e. the slots covered by the epoch are `self.start_slot() .. self.end_slot()`.
|
||||
pub end_slot: E::SlotNumber,
|
||||
pub end_slot: E::Slot,
|
||||
}
|
||||
|
||||
impl<E: Epoch> Clone for EpochHeader<E> {
|
||||
@@ -215,14 +215,14 @@ impl<E, ERef> ViableEpoch<E, ERef> where
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub enum ViableEpochDescriptor<Hash, Number, E: Epoch> {
|
||||
/// The epoch is an unimported genesis, with given start slot number.
|
||||
UnimportedGenesis(E::SlotNumber),
|
||||
UnimportedGenesis(E::Slot),
|
||||
/// The epoch is signaled and has been imported, with given identifier and header.
|
||||
Signaled(EpochIdentifier<Hash, Number>, EpochHeader<E>)
|
||||
}
|
||||
|
||||
impl<Hash, Number, E: Epoch> ViableEpochDescriptor<Hash, Number, E> {
|
||||
/// Start slot of the descriptor.
|
||||
pub fn start_slot(&self) -> E::SlotNumber {
|
||||
pub fn start_slot(&self) -> E::Slot {
|
||||
match self {
|
||||
Self::UnimportedGenesis(start_slot) => *start_slot,
|
||||
Self::Signaled(_, header) => header.start_slot,
|
||||
@@ -339,7 +339,7 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
|
||||
/// Map the epoch changes from one storing data to a different one.
|
||||
pub fn map<B, F>(self, mut f: F) -> EpochChanges<Hash, Number, B> where
|
||||
B: Epoch<SlotNumber=E::SlotNumber>,
|
||||
B: Epoch<Slot=E::Slot>,
|
||||
F: FnMut(&Hash, &Number, E) -> B,
|
||||
{
|
||||
EpochChanges {
|
||||
@@ -394,7 +394,7 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
descendent_of_builder: D,
|
||||
hash: &Hash,
|
||||
number: Number,
|
||||
slot: E::SlotNumber,
|
||||
slot: E::Slot,
|
||||
) -> Result<(), fork_tree::Error<D::Error>> {
|
||||
let is_descendent_of = descendent_of_builder
|
||||
.build_is_descendent_of(None);
|
||||
@@ -445,11 +445,11 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
descriptor: &ViableEpochDescriptor<Hash, Number, E>,
|
||||
make_genesis: G,
|
||||
) -> Option<ViableEpoch<E, &E>> where
|
||||
G: FnOnce(E::SlotNumber) -> E
|
||||
G: FnOnce(E::Slot) -> E
|
||||
{
|
||||
match descriptor {
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot_number) => {
|
||||
Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot_number)))
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot) => {
|
||||
Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot)))
|
||||
},
|
||||
ViableEpochDescriptor::Signaled(identifier, _) => {
|
||||
self.epoch(&identifier).map(ViableEpoch::Signaled)
|
||||
@@ -479,11 +479,11 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
descriptor: &ViableEpochDescriptor<Hash, Number, E>,
|
||||
make_genesis: G,
|
||||
) -> Option<ViableEpoch<E, &mut E>> where
|
||||
G: FnOnce(E::SlotNumber) -> E
|
||||
G: FnOnce(E::Slot) -> E
|
||||
{
|
||||
match descriptor {
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot_number) => {
|
||||
Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot_number)))
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot) => {
|
||||
Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot)))
|
||||
},
|
||||
ViableEpochDescriptor::Signaled(identifier, _) => {
|
||||
self.epoch_mut(&identifier).map(ViableEpoch::Signaled)
|
||||
@@ -500,12 +500,12 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
descriptor: &ViableEpochDescriptor<Hash, Number, E>,
|
||||
make_genesis: G
|
||||
) -> Option<E> where
|
||||
G: FnOnce(E::SlotNumber) -> E,
|
||||
G: FnOnce(E::Slot) -> E,
|
||||
E: Clone,
|
||||
{
|
||||
match descriptor {
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot_number) => {
|
||||
Some(make_genesis(*slot_number))
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot) => {
|
||||
Some(make_genesis(*slot))
|
||||
},
|
||||
ViableEpochDescriptor::Signaled(identifier, _) => {
|
||||
self.epoch(&identifier).cloned()
|
||||
@@ -523,17 +523,17 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
descendent_of_builder: D,
|
||||
parent_hash: &Hash,
|
||||
parent_number: Number,
|
||||
slot_number: E::SlotNumber,
|
||||
slot: E::Slot,
|
||||
make_genesis: G,
|
||||
) -> Result<Option<E>, fork_tree::Error<D::Error>> where
|
||||
G: FnOnce(E::SlotNumber) -> E,
|
||||
G: FnOnce(E::Slot) -> E,
|
||||
E: Clone,
|
||||
{
|
||||
let descriptor = self.epoch_descriptor_for_child_of(
|
||||
descendent_of_builder,
|
||||
parent_hash,
|
||||
parent_number,
|
||||
slot_number
|
||||
slot
|
||||
)?;
|
||||
|
||||
Ok(descriptor.and_then(|des| self.epoch_data(&des, make_genesis)))
|
||||
@@ -548,7 +548,7 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
descendent_of_builder: D,
|
||||
parent_hash: &Hash,
|
||||
parent_number: Number,
|
||||
slot_number: E::SlotNumber,
|
||||
slot: E::Slot,
|
||||
) -> Result<Option<ViableEpochDescriptor<Hash, Number, E>>, fork_tree::Error<D::Error>> {
|
||||
// find_node_where will give you the node in the fork-tree which is an ancestor
|
||||
// of the `parent_hash` by default. if the last epoch was signalled at the parent_hash,
|
||||
@@ -561,7 +561,7 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
|
||||
if parent_number == Zero::zero() {
|
||||
// need to insert the genesis epoch.
|
||||
return Ok(Some(ViableEpochDescriptor::UnimportedGenesis(slot_number)))
|
||||
return Ok(Some(ViableEpochDescriptor::UnimportedGenesis(slot)))
|
||||
}
|
||||
|
||||
// We want to find the deepest node in the tree which is an ancestor
|
||||
@@ -571,9 +571,9 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
// we need.
|
||||
let predicate = |epoch: &PersistedEpochHeader<E>| match *epoch {
|
||||
PersistedEpochHeader::Genesis(ref epoch_0, _) =>
|
||||
epoch_0.start_slot <= slot_number,
|
||||
epoch_0.start_slot <= slot,
|
||||
PersistedEpochHeader::Regular(ref epoch_n) =>
|
||||
epoch_n.start_slot <= slot_number,
|
||||
epoch_n.start_slot <= slot,
|
||||
};
|
||||
|
||||
self.inner.find_node_where(
|
||||
@@ -588,7 +588,7 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
|
||||
// and here we figure out which of the internal epochs
|
||||
// of a genesis node to use based on their start slot.
|
||||
PersistedEpochHeader::Genesis(ref epoch_0, ref epoch_1) =>
|
||||
if epoch_1.start_slot <= slot_number {
|
||||
if epoch_1.start_slot <= slot {
|
||||
(EpochIdentifierPosition::Genesis1, epoch_1.clone())
|
||||
} else {
|
||||
(EpochIdentifierPosition::Genesis0, epoch_0.clone())
|
||||
@@ -695,17 +695,17 @@ mod tests {
|
||||
}
|
||||
|
||||
type Hash = [u8; 1];
|
||||
type SlotNumber = u64;
|
||||
type Slot = u64;
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
struct Epoch {
|
||||
start_slot: SlotNumber,
|
||||
duration: SlotNumber,
|
||||
start_slot: Slot,
|
||||
duration: Slot,
|
||||
}
|
||||
|
||||
impl EpochT for Epoch {
|
||||
type NextEpochDescriptor = ();
|
||||
type SlotNumber = SlotNumber;
|
||||
type Slot = Slot;
|
||||
|
||||
fn increment(&self, _: ()) -> Self {
|
||||
Epoch {
|
||||
@@ -714,11 +714,11 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn end_slot(&self) -> SlotNumber {
|
||||
fn end_slot(&self) -> Slot {
|
||||
self.start_slot + self.duration
|
||||
}
|
||||
|
||||
fn start_slot(&self) -> SlotNumber {
|
||||
fn start_slot(&self) -> Slot {
|
||||
self.start_slot
|
||||
}
|
||||
}
|
||||
@@ -748,8 +748,8 @@ mod tests {
|
||||
).unwrap().unwrap();
|
||||
|
||||
match genesis_epoch {
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot_number) => {
|
||||
assert_eq!(slot_number, 10101u64);
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot) => {
|
||||
assert_eq!(slot, 10101u64);
|
||||
},
|
||||
_ => panic!("should be unimported genesis"),
|
||||
};
|
||||
@@ -762,8 +762,8 @@ mod tests {
|
||||
).unwrap().unwrap();
|
||||
|
||||
match genesis_epoch_2 {
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot_number) => {
|
||||
assert_eq!(slot_number, 10102u64);
|
||||
ViableEpochDescriptor::UnimportedGenesis(slot) => {
|
||||
assert_eq!(slot, 10102u64);
|
||||
},
|
||||
_ => panic!("should be unimported genesis"),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user