Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed Nov 13, 2023
1 parent d9607b6 commit 2c63652
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 6 additions & 2 deletions substrate/frame/sassafras/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,12 @@ impl<T: Config> Pallet<T> {
Self::epoch_start(EpochIndex::<T>::get())
}

// Get the epoch's first slot.
/// Get the epoch's first slot.
fn epoch_start(epoch_index: u64) -> Slot {
const PROOF: &str = "slot number is u64; it should relate in some way to wall clock time; \
if u64 is not enough we should crash for safety; qed.";

let epoch_start = epoch_index.checked_mul(T::EpochLength::get()).expect(PROOF);

epoch_start.checked_add(*GenesisSlot::<T>::get()).expect(PROOF).into()
}

Expand Down Expand Up @@ -805,7 +804,12 @@ impl<T: Config> Pallet<T> {
/// Returns `None` if, according to the sorting strategy, there is no ticket associated to the
/// specified slot-index (happend if a ticket falls in the middle of an epoch and n > k),
/// or if the slot falls beyond the next epoch.
///
/// Before importing the first block this returns `None`.
pub fn slot_ticket_id(slot: Slot) -> Option<TicketId> {
if frame_system::Pallet::<T>::block_number() < One::one() {
return None
}
let epoch_idx = EpochIndex::<T>::get();
let epoch_len = T::EpochLength::get();
let mut slot_idx = Self::slot_index(slot);
Expand Down
11 changes: 6 additions & 5 deletions substrate/frame/sassafras/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,18 @@ fn slot_ticket_id_outside_in_fetch() {
unsorted_tickets_count: 0,
});

// Before initializing `GenesisSlot` value the pallet always return the first slot.
// Before importing the first block the pallet always return `None`
// This is a kind of special hardcoded case that should never happen in practice
// as the first thing the pallet does is to initialize the genesis slot.

assert_eq!(Sassafras::slot_ticket_id(0.into()), Some(curr_tickets[1]));
assert_eq!(Sassafras::slot_ticket_id(genesis_slot + 0), Some(curr_tickets[1]));
assert_eq!(Sassafras::slot_ticket_id(genesis_slot + 1), Some(curr_tickets[1]));
assert_eq!(Sassafras::slot_ticket_id(genesis_slot + 100), Some(curr_tickets[1]));
assert_eq!(Sassafras::slot_ticket_id(0.into()), None);
assert_eq!(Sassafras::slot_ticket_id(genesis_slot + 0), None);
assert_eq!(Sassafras::slot_ticket_id(genesis_slot + 1), None);
assert_eq!(Sassafras::slot_ticket_id(genesis_slot + 100), None);

// Initialize genesis slot..
GenesisSlot::<Test>::set(genesis_slot);
frame_system::Pallet::<Test>::set_block_number(One::one());

// Try to fetch a ticket for a slot before current epoch.
assert_eq!(Sassafras::slot_ticket_id(0.into()), None);
Expand Down

0 comments on commit 2c63652

Please sign in to comment.