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

Glutton pallet updates #14192

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions frame/glutton/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -48,6 +51,9 @@ pub mod pallet {
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The admin origin that can set computational limits and initialize the pallet.
type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Weight information for this pallet.
type WeightInfo: WeightInfo;
}
Expand All @@ -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 },
}

Expand Down Expand Up @@ -96,7 +102,7 @@ pub mod pallet {
pub(super) type TrashData<T: Config> = StorageMap<
Hasher = Twox64Concat,
Key = u32,
Value = [u8; 1024],
Value = [u8; VALUE_SIZE],
QueryKind = OptionQuery,
MaxValues = ConstU32<65_000>,
>;
Expand Down Expand Up @@ -129,7 +135,8 @@ pub mod pallet {
<Storage<T>>::put(self.storage);

if self.trash_data_count <= 65_000 {
(0..self.trash_data_count).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
(0..self.trash_data_count)
.for_each(|i| TrashData::<T>::insert(i, Pallet::<T>::gen_value(i)));
}

TrashDataCount::<T>::set(self.trash_data_count);
Expand Down Expand Up @@ -174,7 +181,11 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// 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())
Expand All @@ -185,7 +196,7 @@ pub mod pallet {
new_count: u32,
witness_count: Option<u32>,
) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?;

let current_count = TrashDataCount::<T>::get();
ensure!(
Expand All @@ -194,7 +205,8 @@ pub mod pallet {
);

if new_count > current_count {
(current_count..new_count).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
(current_count..new_count)
.for_each(|i| TrashData::<T>::insert(i, Self::gen_value(i)));
} else {
(new_count..current_count).for_each(TrashData::<T>::remove);
}
Expand All @@ -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<T>, compute: Perbill) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?;

Compute::<T>::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<T>, storage: Perbill) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?;

Storage::<T>::set(storage);

Self::deposit_event(Event::StorageLimitSet { storage });
Expand Down Expand Up @@ -282,7 +297,7 @@ pub mod pallet {
// compiler does not know that (hopefully).
debug_assert!(clobber.len() == 64);
if clobber.len() == 65 {
TrashData::<T>::insert(0, [clobber[0] as u8; 1024]);
TrashData::<T>::insert(0, [clobber[0] as u8; VALUE_SIZE]);
}
}

Expand Down Expand Up @@ -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
}
}
}
1 change: 1 addition & 0 deletions frame/glutton/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl frame_system::Config for Test {

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type WeightInfo = ();
}

Expand Down
15 changes: 13 additions & 2 deletions frame/glutton/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ fn initialize_pallet_works() {
Error::<Test>::AlreadyInitialized
);

assert_eq!(TrashData::<Test>::get(0), Some([0; 1024]));
assert_eq!(TrashData::<Test>::get(1), Some([1; 1024]));
assert_eq!(TrashData::<Test>::get(0), Some(Pallet::<Test>::gen_value(0)));
assert_eq!(TrashData::<Test>::get(1), Some(Pallet::<Test>::gen_value(1)));
assert_eq!(TrashData::<Test>::get(2), None);

assert_eq!(TrashDataCount::<Test>::get(), 2);
Expand Down Expand Up @@ -224,3 +224,14 @@ fn waste_at_most_proof_size_weight_close_enough() {
);
});
}

#[test]
fn gen_value_works() {
let g0 = Pallet::<Test>::gen_value(0);
let g1 = Pallet::<Test>::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::<Test>::gen_value(0), "Is deterministic");
}