Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from JoshOrndorff/joshy-slot-number-strictly-i…
Browse files Browse the repository at this point in the history
…ncreasing

Ensure slot number is strictly increasing
  • Loading branch information
4meta5 authored Apr 7, 2022
2 parents df9d214 + f3f9cdd commit fc2209b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pallets/author-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ pub mod pallet {
#[pallet::storage]
pub type Author<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;

/// The highest slot that has been seen in the history of this chain.
/// This is a strictly-increasing value.
#[pallet::storage]
pub type HighestSlotSeen<T: Config> = StorageValue<_, u32, ValueQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: T::BlockNumber) -> Weight {
Expand Down Expand Up @@ -116,14 +121,24 @@ pub mod pallet {
pub fn kick_off_authorship_validation(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_none(origin)?;

// First check that the slot number is valid (greater than the previous highest)
let slot = T::SlotBeacon::slot();
assert!(
slot > HighestSlotSeen::<T>::get(),
"Block invalid; Supplied slot number is not high enough"
);

// Now check that the author is valid in this slot
let author = <Author<T>>::get()
.expect("Block invalid, no authorship information supplied in preruntime digest.");

assert!(
T::CanAuthor::can_author(&author, &T::SlotBeacon::slot()),
"Block invalid, supplied author is not eligible."
);

// Once that is validated, update the stored slot number
HighestSlotSeen::<T>::put(slot);

Ok(Pays::No.into())
}
}
Expand Down

0 comments on commit fc2209b

Please sign in to comment.