Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure author inherent included #17

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
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
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
Loading