From 2e7932fac159aa8855feeed7578319ae0e6d8296 Mon Sep 17 00:00:00 2001 From: asynchronous rob Date: Thu, 3 Aug 2023 21:58:59 -0700 Subject: [PATCH] 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 --- bin/node-template/runtime/Cargo.toml | 1 + bin/node-template/runtime/src/lib.rs | 3 ++ frame/aura/Cargo.toml | 1 + frame/aura/src/lib.rs | 41 ++++++++++++++++++++++++++-- frame/aura/src/mock.rs | 7 ++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/bin/node-template/runtime/Cargo.toml b/bin/node-template/runtime/Cargo.toml index 1ad172dc831de..51e30187c9d63 100644 --- a/bin/node-template/runtime/Cargo.toml +++ b/bin/node-template/runtime/Cargo.toml @@ -114,3 +114,4 @@ try-runtime = [ "pallet-transaction-payment/try-runtime", "sp-runtime/try-runtime" ] +experimental = ["pallet-aura/experimental"] diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 8fae73ef08571..c3375d2ee601a 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -207,6 +207,9 @@ impl pallet_aura::Config for Runtime { type DisabledValidators = (); type MaxAuthorities = ConstU32<32>; type AllowMultipleBlocksPerSlot = ConstBool; + + #[cfg(feature = "experimental")] + type SlotDuration = pallet_aura::MinimumPeriodTimesTwo; } impl pallet_grandpa::Config for Runtime { diff --git a/frame/aura/Cargo.toml b/frame/aura/Cargo.toml index aea8ab4adfa86..99c8c2fb497f0 100644 --- a/frame/aura/Cargo.toml +++ b/frame/aura/Cargo.toml @@ -48,3 +48,4 @@ try-runtime = [ "pallet-timestamp/try-runtime", "sp-runtime/try-runtime" ] +experimental = [] diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 7a1969d905fcc..641d5fc47e90e 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -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(sp_std::marker::PhantomData); + +impl Get for MinimumPeriodTimesTwo { + fn get() -> T::Moment { + ::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; + + /// 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<::Moment>; } #[pallet::pallet] @@ -218,9 +245,17 @@ impl Pallet { /// 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. - ::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. + ::MinimumPeriod::get().saturating_mul(2u32.into()) + } } /// Ensure the correctness of the state of this pallet. diff --git a/frame/aura/src/mock.rs b/frame/aura/src/mock.rs index 53b87ff36ec60..39b798c2f6841 100644 --- a/frame/aura/src/mock.rs +++ b/frame/aura/src/mock.rs @@ -30,6 +30,8 @@ use sp_runtime::{testing::UintAuthorityId, traits::IdentityLookup, BuildStorage} type Block = frame_system::mocking::MockBlock; +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; } fn build_ext(authorities: Vec) -> sp_io::TestExternalities {