mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-24 07:45:49 +00:00
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:
@@ -114,3 +114,4 @@ try-runtime = [
|
|||||||
"pallet-transaction-payment/try-runtime",
|
"pallet-transaction-payment/try-runtime",
|
||||||
"sp-runtime/try-runtime"
|
"sp-runtime/try-runtime"
|
||||||
]
|
]
|
||||||
|
experimental = ["pallet-aura/experimental"]
|
||||||
|
|||||||
@@ -207,6 +207,9 @@ impl pallet_aura::Config for Runtime {
|
|||||||
type DisabledValidators = ();
|
type DisabledValidators = ();
|
||||||
type MaxAuthorities = ConstU32<32>;
|
type MaxAuthorities = ConstU32<32>;
|
||||||
type AllowMultipleBlocksPerSlot = ConstBool<false>;
|
type AllowMultipleBlocksPerSlot = ConstBool<false>;
|
||||||
|
|
||||||
|
#[cfg(feature = "experimental")]
|
||||||
|
type SlotDuration = pallet_aura::MinimumPeriodTimesTwo<Runtime>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl pallet_grandpa::Config for Runtime {
|
impl pallet_grandpa::Config for Runtime {
|
||||||
|
|||||||
@@ -48,3 +48,4 @@ try-runtime = [
|
|||||||
"pallet-timestamp/try-runtime",
|
"pallet-timestamp/try-runtime",
|
||||||
"sp-runtime/try-runtime"
|
"sp-runtime/try-runtime"
|
||||||
]
|
]
|
||||||
|
experimental = []
|
||||||
|
|||||||
@@ -60,6 +60,23 @@ pub use pallet::*;
|
|||||||
|
|
||||||
const LOG_TARGET: &str = "runtime::aura";
|
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]
|
#[frame_support::pallet]
|
||||||
pub mod pallet {
|
pub mod pallet {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -95,6 +112,16 @@ pub mod pallet {
|
|||||||
/// another pallet which enforces some limitation on the number of blocks authors can create
|
/// another pallet which enforces some limitation on the number of blocks authors can create
|
||||||
/// using the same slot.
|
/// using the same slot.
|
||||||
type AllowMultipleBlocksPerSlot: Get<bool>;
|
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]
|
#[pallet::pallet]
|
||||||
@@ -218,9 +245,17 @@ impl<T: Config> Pallet<T> {
|
|||||||
|
|
||||||
/// Determine the Aura slot-duration based on the Timestamp module configuration.
|
/// Determine the Aura slot-duration based on the Timestamp module configuration.
|
||||||
pub fn slot_duration() -> T::Moment {
|
pub fn slot_duration() -> T::Moment {
|
||||||
// we double the minimum block-period so each author can always propose within
|
#[cfg(feature = "experimental")]
|
||||||
// the majority of its slot.
|
{
|
||||||
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
|
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.
|
/// Ensure the correctness of the state of this pallet.
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ use sp_runtime::{testing::UintAuthorityId, traits::IdentityLookup, BuildStorage}
|
|||||||
|
|
||||||
type Block = frame_system::mocking::MockBlock<Test>;
|
type Block = frame_system::mocking::MockBlock<Test>;
|
||||||
|
|
||||||
|
const SLOT_DURATION: u64 = 2;
|
||||||
|
|
||||||
frame_support::construct_runtime!(
|
frame_support::construct_runtime!(
|
||||||
pub enum Test
|
pub enum Test
|
||||||
{
|
{
|
||||||
@@ -68,7 +70,7 @@ impl frame_system::Config for Test {
|
|||||||
impl pallet_timestamp::Config for Test {
|
impl pallet_timestamp::Config for Test {
|
||||||
type Moment = u64;
|
type Moment = u64;
|
||||||
type OnTimestampSet = Aura;
|
type OnTimestampSet = Aura;
|
||||||
type MinimumPeriod = ConstU64<1>;
|
type MinimumPeriod = ConstU64<{ SLOT_DURATION / 2 }>;
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +102,9 @@ impl pallet_aura::Config for Test {
|
|||||||
type DisabledValidators = MockDisabledValidators;
|
type DisabledValidators = MockDisabledValidators;
|
||||||
type MaxAuthorities = ConstU32<10>;
|
type MaxAuthorities = ConstU32<10>;
|
||||||
type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot;
|
type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot;
|
||||||
|
|
||||||
|
#[cfg(feature = "experimental")]
|
||||||
|
type SlotDuration = ConstU64<SLOT_DURATION>;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_ext(authorities: Vec<u64>) -> sp_io::TestExternalities {
|
fn build_ext(authorities: Vec<u64>) -> sp_io::TestExternalities {
|
||||||
|
|||||||
Reference in New Issue
Block a user