From a50facc8fac77ff3398b4377a72bacbdf63dfca4 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 4 Apr 2022 13:59:35 -0400 Subject: [PATCH 1/3] Basic storage and validation of slot numbers --- pallets/author-inherent/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pallets/author-inherent/src/lib.rs b/pallets/author-inherent/src/lib.rs index 1a5d2491..19869459 100644 --- a/pallets/author-inherent/src/lib.rs +++ b/pallets/author-inherent/src/lib.rs @@ -83,6 +83,11 @@ pub mod pallet { #[pallet::storage] pub type Author = 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 = StorageValue<_, u32, ValueQuery>; + #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(_: T::BlockNumber) -> Weight { @@ -116,6 +121,16 @@ pub mod pallet { pub fn kick_off_authorship_validation(origin: OriginFor) -> 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::::get(), + "Block invalid; Supplied slot number is not high enough" + ); + + HighestSlotSeen::::put(slot); + + // Now check that the author is valid in this slot let author = >::get() .expect("Block invalid, no authorship information supplied in preruntime digest."); From ca9bc6b65c3bab22a2d2db9064ea594afa82f6bc Mon Sep 17 00:00:00 2001 From: Amar Singh Date: Tue, 5 Apr 2022 10:48:26 -0400 Subject: [PATCH 2/3] Update pallets/author-inherent/src/lib.rs --- pallets/author-inherent/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/author-inherent/src/lib.rs b/pallets/author-inherent/src/lib.rs index 19869459..93c6fb79 100644 --- a/pallets/author-inherent/src/lib.rs +++ b/pallets/author-inherent/src/lib.rs @@ -122,7 +122,7 @@ pub mod pallet { ensure_none(origin)?; // First check that the slot number is valid (greater than the previous highest) - let slot= T::SlotBeacon::slot(); + let slot = T::SlotBeacon::slot(); assert!( slot > HighestSlotSeen::::get(), "Block invalid; Supplied slot number is not high enough" From f3f9cddd07f1b89c40aedcc13305b16c1020b4da Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Thu, 7 Apr 2022 01:28:24 -0400 Subject: [PATCH 3/3] delay write until after validation checks --- pallets/author-inherent/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/author-inherent/src/lib.rs b/pallets/author-inherent/src/lib.rs index 93c6fb79..dcdc2e9e 100644 --- a/pallets/author-inherent/src/lib.rs +++ b/pallets/author-inherent/src/lib.rs @@ -128,17 +128,17 @@ pub mod pallet { "Block invalid; Supplied slot number is not high enough" ); - HighestSlotSeen::::put(slot); - // Now check that the author is valid in this slot let author = >::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::::put(slot); + Ok(Pays::No.into()) } }