From e595b23dda3e3278e5253f7906765aa6e64e22f1 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 7 Jun 2023 22:12:43 -0500 Subject: [PATCH] rework as associated type --- frame/aura/src/lib.rs | 32 +++++++++++++++----------------- frame/aura/src/mock.rs | 2 ++ frame/aura/src/tests.rs | 8 +++++--- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 7b6fbcce57d1d..f64e3274d2a32 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -81,6 +81,20 @@ pub mod pallet { /// Blocks authored by a disabled validator will lead to a panic as part of this module's /// initialization. type DisabledValidators: DisabledValidators; + + /// Whether to allow block authors to create multiple blocks per slot. + /// + /// If this is `true`, the pallet will allow slots to stay the same across sequential blocks. + /// If this is `false`, the pallet will require that subsequent blocks always have higher slots + /// than previous ones. + /// + /// Regardless of the setting of this storage value, the pallet will always enforce the + /// invariant that slots don't move backwards as the chain progresses. + /// + /// The typical value for this should be 'false' unless this pallet is being augmented by + /// another pallet which enforces some limitation on the number of blocks authors can create + /// using the same slot. + type AllowMultipleBlocksPerSlot: Get; } #[pallet::pallet] @@ -92,7 +106,7 @@ pub mod pallet { if let Some(new_slot) = Self::current_slot_from_digests() { let current_slot = CurrentSlot::::get(); - if AllowMultipleBlocksPerSlot::::get() { + if T::AllowMultipleBlocksPerSlot::get() { assert!(current_slot <= new_slot, "Slot must not decrease"); } else { assert!(current_slot < new_slot, "Slot must increase"); @@ -133,22 +147,6 @@ pub mod pallet { #[pallet::getter(fn current_slot)] pub(super) type CurrentSlot = StorageValue<_, Slot, ValueQuery>; - /// Whether to allow authors to create multiple blocks per slot. - /// - /// If this is `true`, the pallet will allow slots to stay the same across sequential blocks. - /// If this is `false`, the pallet will require that subsequent blocks always have higher slots - /// than previous ones. - /// - /// Regardless of the setting of this storage value, the pallet will always enforce the - /// invariant that slots don't move backwards as the chain progresses. - /// - /// The typical value for this should be 'false' unless this pallet is being augmented by - /// another pallet which enforces some limitation on the number of blocks authors can create - /// using the same slot. - #[pallet::storage] - #[pallet::getter(fn allow_multiple_blocks)] - pub(super) type AllowMultipleBlocksPerSlot = StorageValue<_, bool, ValueQuery>; - #[pallet::genesis_config] #[derive(frame_support::DefaultNoBound)] pub struct GenesisConfig { diff --git a/frame/aura/src/mock.rs b/frame/aura/src/mock.rs index 72d11a0749933..c95b7451d0bd4 100644 --- a/frame/aura/src/mock.rs +++ b/frame/aura/src/mock.rs @@ -82,6 +82,7 @@ impl pallet_timestamp::Config for Test { parameter_types! { static DisabledValidatorTestValue: Vec = Default::default(); + pub static AllowMultipleBlocksPerSlot: bool = false; } pub struct MockDisabledValidators; @@ -106,6 +107,7 @@ impl pallet_aura::Config for Test { type AuthorityId = AuthorityId; type DisabledValidators = MockDisabledValidators; type MaxAuthorities = ConstU32<10>; + type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot; } pub fn new_test_ext(authorities: Vec) -> sp_io::TestExternalities { diff --git a/frame/aura/src/tests.rs b/frame/aura/src/tests.rs index 339b17c5675c2..d5e5f19d65ea5 100644 --- a/frame/aura/src/tests.rs +++ b/frame/aura/src/tests.rs @@ -19,7 +19,7 @@ #![cfg(test)] -use crate::mock::{new_test_ext, Aura, MockDisabledValidators, System, Test}; +use crate::mock::{new_test_ext, Aura, MockDisabledValidators, System}; use codec::Encode; use frame_support::traits::OnInitialize; use sp_consensus_aura::{Slot, AURA_ENGINE_ID}; @@ -59,6 +59,8 @@ fn disabled_validators_cannot_author_blocks() { #[should_panic(expected = "Slot must increase")] fn pallet_requires_slot_to_increase_unless_allowed() { new_test_ext(vec![0, 1, 2, 3]).execute_with(|| { + crate::mock::AllowMultipleBlocksPerSlot::set(false); + let slot = Slot::from(1); let pre_digest = Digest { logs: vec![DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())] }; @@ -82,7 +84,7 @@ fn pallet_can_allow_unchanged_slot() { System::reset_events(); System::initialize(&42, &System::parent_hash(), &pre_digest); - crate::AllowMultipleBlocksPerSlot::::put(true); + crate::mock::AllowMultipleBlocksPerSlot::set(true); // and we should be able to initialize the block with the same slot a second time. Aura::on_initialize(42); @@ -101,7 +103,7 @@ fn pallet_always_rejects_decreasing_slot() { System::reset_events(); System::initialize(&42, &System::parent_hash(), &pre_digest); - crate::AllowMultipleBlocksPerSlot::::put(true); + crate::mock::AllowMultipleBlocksPerSlot::set(true); Aura::on_initialize(42); System::finalize();