diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index b99d42da09d3f..c8654cbbbd6ed 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -40,6 +40,9 @@ use sp_std::{vec, vec::Vec}; pub use pallet::*; pub use weights::WeightInfo; +/// The size of each value in the `TrashData` storage in bytes. +pub const VALUE_SIZE: usize = 1024; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -48,6 +51,9 @@ pub mod pallet { pub trait Config: frame_system::Config { type RuntimeEvent: From + IsType<::RuntimeEvent>; + /// The admin origin that can set computational limits and initialize the pallet. + type AdminOrigin: EnsureOrigin; + /// Weight information for this pallet. type WeightInfo: WeightInfo; } @@ -58,11 +64,11 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - /// The pallet has been (re)initialized by root. + /// The pallet has been (re)initialized. PalletInitialized { reinit: bool }, - /// The computation limit has been updated by root. + /// The computation limit has been updated. ComputationLimitSet { compute: Perbill }, - /// The storage limit has been updated by root. + /// The storage limit has been updated. StorageLimitSet { storage: Perbill }, } @@ -96,7 +102,7 @@ pub mod pallet { pub(super) type TrashData = StorageMap< Hasher = Twox64Concat, Key = u32, - Value = [u8; 1024], + Value = [u8; VALUE_SIZE], QueryKind = OptionQuery, MaxValues = ConstU32<65_000>, >; @@ -129,7 +135,8 @@ pub mod pallet { >::put(self.storage); if self.trash_data_count <= 65_000 { - (0..self.trash_data_count).for_each(|i| TrashData::::insert(i, [i as u8; 1024])); + (0..self.trash_data_count) + .for_each(|i| TrashData::::insert(i, Pallet::::gen_value(i))); } TrashDataCount::::set(self.trash_data_count); @@ -174,7 +181,11 @@ pub mod pallet { impl Pallet { /// Initializes the pallet by writing into `TrashData`. /// - /// Only callable by Root. A good default for `trash_count` is `5_000`. + /// `current_count` is the current number of elements in `TrashData`. This can be set to + /// `None` when the pallet is first initialized. + /// + /// Only callable by Root or `AdminOrigin`. A good default for `new_count` is + /// `5_000`. #[pallet::call_index(0)] #[pallet::weight( T::WeightInfo::initialize_pallet_grow(witness_count.unwrap_or_default()) @@ -185,7 +196,7 @@ pub mod pallet { new_count: u32, witness_count: Option, ) -> DispatchResult { - ensure_root(origin)?; + T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?; let current_count = TrashDataCount::::get(); ensure!( @@ -194,7 +205,8 @@ pub mod pallet { ); if new_count > current_count { - (current_count..new_count).for_each(|i| TrashData::::insert(i, [i as u8; 1024])); + (current_count..new_count) + .for_each(|i| TrashData::::insert(i, Self::gen_value(i))); } else { (new_count..current_count).for_each(TrashData::::remove); } @@ -204,28 +216,31 @@ pub mod pallet { Ok(()) } - /// Set the `Compute` storage value that determines how much of the - /// block's weight `ref_time` to use during `on_idle`. + /// Set how much of the remaining `ref_time` weight should be consumed by `on_idle`. /// - /// Only callable by Root. + /// Only callable by Root or `AdminOrigin`. #[pallet::call_index(1)] #[pallet::weight(T::WeightInfo::set_compute())] pub fn set_compute(origin: OriginFor, compute: Perbill) -> DispatchResult { - ensure_root(origin)?; + T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?; + Compute::::set(compute); Self::deposit_event(Event::ComputationLimitSet { compute }); Ok(()) } - /// Set the `Storage` storage value that determines the PoV size usage - /// for each block. + /// Set how much of the remaining `proof_size` weight should be consumed by `on_idle`. + // + /// 100% means that all remaining `proof_size` will be consumed. The PoV metering will + /// likely be an /// - /// Only callable by Root. + /// Only callable by Root or `AdminOrigin`. #[pallet::call_index(2)] #[pallet::weight(T::WeightInfo::set_storage())] pub fn set_storage(origin: OriginFor, storage: Perbill) -> DispatchResult { - ensure_root(origin)?; + T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?; + Storage::::set(storage); Self::deposit_event(Event::StorageLimitSet { storage }); @@ -282,7 +297,7 @@ pub mod pallet { // compiler does not know that (hopefully). debug_assert!(clobber.len() == 64); if clobber.len() == 65 { - TrashData::::insert(0, [clobber[0] as u8; 1024]); + TrashData::::insert(0, [clobber[0] as u8; VALUE_SIZE]); } } @@ -319,5 +334,17 @@ pub mod pallet { Some(i) => Ok(i as u32), } } + + /// Generate a pseudo-random deterministic value from a `seed`. + pub(crate) fn gen_value(seed: u32) -> [u8; VALUE_SIZE] { + let mut ret = [0u8; VALUE_SIZE]; + + for i in 0u32..(VALUE_SIZE as u32 / 32) { + let hash = (seed, i).using_encoded(sp_core::twox_256); + ret[i as usize * 32..(i + 1) as usize * 32].copy_from_slice(&hash); + } + + ret + } } } diff --git a/frame/glutton/src/mock.rs b/frame/glutton/src/mock.rs index c8be354f48e28..8c331dc97ab2b 100644 --- a/frame/glutton/src/mock.rs +++ b/frame/glutton/src/mock.rs @@ -68,6 +68,7 @@ impl frame_system::Config for Test { impl Config for Test { type RuntimeEvent = RuntimeEvent; + type AdminOrigin = frame_system::EnsureRoot; type WeightInfo = (); } diff --git a/frame/glutton/src/tests.rs b/frame/glutton/src/tests.rs index d75f2da5cb7ee..ba215e1eea1c3 100644 --- a/frame/glutton/src/tests.rs +++ b/frame/glutton/src/tests.rs @@ -43,8 +43,8 @@ fn initialize_pallet_works() { Error::::AlreadyInitialized ); - assert_eq!(TrashData::::get(0), Some([0; 1024])); - assert_eq!(TrashData::::get(1), Some([1; 1024])); + assert_eq!(TrashData::::get(0), Some(Pallet::::gen_value(0))); + assert_eq!(TrashData::::get(1), Some(Pallet::::gen_value(1))); assert_eq!(TrashData::::get(2), None); assert_eq!(TrashDataCount::::get(), 2); @@ -224,3 +224,14 @@ fn waste_at_most_proof_size_weight_close_enough() { ); }); } + +#[test] +fn gen_value_works() { + let g0 = Pallet::::gen_value(0); + let g1 = Pallet::::gen_value(1); + + assert_eq!(g0.len(), VALUE_SIZE); + assert_ne!(g0, g1, "Is distinct"); + assert_ne!(g0, [0; VALUE_SIZE], "Is not zero"); + assert_eq!(g0, Pallet::::gen_value(0), "Is deterministic"); +}