Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
pallet-aura: add feature-flagged explicit slot duration type (#14649)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rphmeier authored Aug 4, 2023
1 parent 5fe0393 commit 2e7932f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions bin/node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,4 @@ try-runtime = [
"pallet-transaction-payment/try-runtime",
"sp-runtime/try-runtime"
]
experimental = ["pallet-aura/experimental"]
3 changes: 3 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ impl pallet_aura::Config for Runtime {
type DisabledValidators = ();
type MaxAuthorities = ConstU32<32>;
type AllowMultipleBlocksPerSlot = ConstBool<false>;

#[cfg(feature = "experimental")]
type SlotDuration = pallet_aura::MinimumPeriodTimesTwo<Runtime>;
}

impl pallet_grandpa::Config for Runtime {
Expand Down
1 change: 1 addition & 0 deletions frame/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ try-runtime = [
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime"
]
experimental = []
41 changes: 38 additions & 3 deletions frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion frame/aura/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 = ();
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2e7932f

Please sign in to comment.