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

Ensure slot number is strictly increasing #41

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
4meta5 marked this conversation as resolved.
Show resolved Hide resolved

#[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()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T::CanAuthor::can_author(&author, &T::SlotBeacon::slot()),
T::CanAuthor::can_author(&author, &slot),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just missed this, not sure how much better it is

"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