pallet-aura: add feature-flagged explicit slot duration type (#14649)

* aura: add feature-flagged explicit slot duration type

* fmt

* add some comments

* have node-template use new explicit feature

* fix mock

* fmt

* use the experimental feature flag instead

* checkout master Cargo.lock
This commit is contained in:
asynchronous rob
2023-08-03 21:58:59 -07:00
committed by GitHub
parent 476ee340d9
commit 08f680e281
5 changed files with 49 additions and 4 deletions
+38 -3
View File
@@ -60,6 +60,23 @@ pub use pallet::*;
const LOG_TARGET: &str = "runtime::aura";
/// A slot duration provider which infers the slot duration from the
/// [`pallet_timestamp::Config::MinimumPeriod`] by multiplying it by two, to ensure
/// that authors have the majority of their slot to author within.
///
/// This was the default behavior of the Aura pallet and may be used for
/// backwards compatibility.
///
/// Note that this type is likely not useful without the `experimental`
/// feature.
pub struct MinimumPeriodTimesTwo<T>(sp_std::marker::PhantomData<T>);
impl<T: pallet_timestamp::Config> Get<T::Moment> for MinimumPeriodTimesTwo<T> {
fn get() -> T::Moment {
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
@@ -95,6 +112,16 @@ pub mod pallet {
/// another pallet which enforces some limitation on the number of blocks authors can create
/// using the same slot.
type AllowMultipleBlocksPerSlot: Get<bool>;
/// The slot duration Aura should run with, expressed in milliseconds.
/// The effective value of this type should not change while the chain is running.
///
/// For backwards compatibility either use [`MinimumPeriodTimesTwo`] or a const.
///
/// This associated type is only present when compiled with the `experimental`
/// feature.
#[cfg(feature = "experimental")]
type SlotDuration: Get<<Self as pallet_timestamp::Config>::Moment>;
}
#[pallet::pallet]
@@ -218,9 +245,17 @@ impl<T: Config> Pallet<T> {
/// Determine the Aura slot-duration based on the Timestamp module configuration.
pub fn slot_duration() -> T::Moment {
// we double the minimum block-period so each author can always propose within
// the majority of its slot.
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
#[cfg(feature = "experimental")]
{
T::SlotDuration::get()
}
#[cfg(not(feature = "experimental"))]
{
// we double the minimum block-period so each author can always propose within
// the majority of its slot.
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
}
}
/// Ensure the correctness of the state of this pallet.
+6 -1
View File
@@ -30,6 +30,8 @@ use sp_runtime::{testing::UintAuthorityId, traits::IdentityLookup, BuildStorage}
type Block = frame_system::mocking::MockBlock<Test>;
const SLOT_DURATION: u64 = 2;
frame_support::construct_runtime!(
pub enum Test
{
@@ -68,7 +70,7 @@ impl frame_system::Config for Test {
impl pallet_timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = Aura;
type MinimumPeriod = ConstU64<1>;
type MinimumPeriod = ConstU64<{ SLOT_DURATION / 2 }>;
type WeightInfo = ();
}
@@ -100,6 +102,9 @@ impl pallet_aura::Config for Test {
type DisabledValidators = MockDisabledValidators;
type MaxAuthorities = ConstU32<10>;
type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot;
#[cfg(feature = "experimental")]
type SlotDuration = ConstU64<SLOT_DURATION>;
}
fn build_ext(authorities: Vec<u64>) -> sp_io::TestExternalities {