From 2c636521ce75a892fbf00e790694a72406b22ee7 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 13 Nov 2023 22:46:36 +0100 Subject: [PATCH] Fix test --- substrate/frame/sassafras/src/lib.rs | 8 ++++++-- substrate/frame/sassafras/src/tests.rs | 11 ++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/substrate/frame/sassafras/src/lib.rs b/substrate/frame/sassafras/src/lib.rs index 5494ee9f7127..7c42ce242898 100644 --- a/substrate/frame/sassafras/src/lib.rs +++ b/substrate/frame/sassafras/src/lib.rs @@ -583,13 +583,12 @@ impl Pallet { Self::epoch_start(EpochIndex::::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::::get()).expect(PROOF).into() } @@ -805,7 +804,12 @@ impl Pallet { /// 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 { + if frame_system::Pallet::::block_number() < One::one() { + return None + } let epoch_idx = EpochIndex::::get(); let epoch_len = T::EpochLength::get(); let mut slot_idx = Self::slot_index(slot); diff --git a/substrate/frame/sassafras/src/tests.rs b/substrate/frame/sassafras/src/tests.rs index f3422db08348..a75abf223c8f 100644 --- a/substrate/frame/sassafras/src/tests.rs +++ b/substrate/frame/sassafras/src/tests.rs @@ -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::::set(genesis_slot); + frame_system::Pallet::::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);