Skip to content

Commit

Permalink
Ensure author inherent included (#17)
Browse files Browse the repository at this point in the history
* Ensure that author inherent is included

* add comment to explain why it's needed

* typo

* apply suggestions
  • Loading branch information
librelois authored Jan 8, 2024
1 parent 80515b8 commit c0a493a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion pallets/author-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ pub mod pallet {
#[pallet::storage]
pub type Author<T: Config> = StorageValue<_, T::AuthorId, OptionQuery>;

/// Check if the inherent was included
#[pallet::storage]
pub type InherentIncluded<T: Config> = StorageValue<_, bool, ValueQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
Expand All @@ -106,7 +110,18 @@ pub mod pallet {
<Author<T>>::put(&author);
}

T::DbWeight::get().writes(1)
// on_initialize: 1 write
// on_finalize: 1 read + 1 write
T::DbWeight::get().reads_writes(1, 2)
}
fn on_finalize(_: BlockNumberFor<T>) {
// According to parity, the only way to ensure that a mandatory inherent is included
// is by checking on block finalization that the inherent set a particular storage item:
// https://github.com/paritytech/polkadot-sdk/issues/2841#issuecomment-1876040854
assert!(
InherentIncluded::<T>::take() == true,
"Block invalid, missing inherent `kick_off_authorship_validation`"
);
}
}

Expand All @@ -130,6 +145,8 @@ pub mod pallet {
"Block invalid, supplied author is not eligible."
);

InherentIncluded::<T>::put(true);

Ok(Pays::No.into())
}
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/author-inherent/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Minimum execution time: 25_775_000 picoseconds.
Weight::from_parts(26_398_000, 10418)
.saturating_add(T::DbWeight::get().reads(6_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
}

Expand Down

0 comments on commit c0a493a

Please sign in to comment.