mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-20 15:05:40 +00:00
Add genesis config to Glutton pallet (#14188)
* glutton gensis config added * Glutton pallet updates (#14192) * Add admin origin and other fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove magic number Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Typo Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fix genesis_build * Fix docs Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix kitchensink runtime Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fix twox_256 * fmt * twox_256 clean * nitpick Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
This commit is contained in:
@@ -21,10 +21,10 @@
|
|||||||
use grandpa_primitives::AuthorityId as GrandpaId;
|
use grandpa_primitives::AuthorityId as GrandpaId;
|
||||||
use kitchensink_runtime::{
|
use kitchensink_runtime::{
|
||||||
constants::currency::*, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig,
|
constants::currency::*, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig,
|
||||||
BalancesConfig, Block, CouncilConfig, DemocracyConfig, ElectionsConfig, GrandpaConfig,
|
BalancesConfig, Block, CouncilConfig, DemocracyConfig, ElectionsConfig, GluttonConfig,
|
||||||
ImOnlineConfig, IndicesConfig, MaxNominations, NominationPoolsConfig, SessionConfig,
|
GrandpaConfig, ImOnlineConfig, IndicesConfig, MaxNominations, NominationPoolsConfig,
|
||||||
SessionKeys, SocietyConfig, StakerStatus, StakingConfig, SudoConfig, SystemConfig,
|
SessionConfig, SessionKeys, SocietyConfig, StakerStatus, StakingConfig, SudoConfig,
|
||||||
TechnicalCommitteeConfig,
|
SystemConfig, TechnicalCommitteeConfig,
|
||||||
};
|
};
|
||||||
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
|
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
|
||||||
use sc_chain_spec::ChainSpecExtension;
|
use sc_chain_spec::ChainSpecExtension;
|
||||||
@@ -372,6 +372,11 @@ pub fn testnet_genesis(
|
|||||||
min_join_bond: 1 * DOLLARS,
|
min_join_bond: 1 * DOLLARS,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
glutton: GluttonConfig {
|
||||||
|
compute: Default::default(),
|
||||||
|
storage: Default::default(),
|
||||||
|
trash_data_count: Default::default(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -377,6 +377,7 @@ impl pallet_scheduler::Config for Runtime {
|
|||||||
|
|
||||||
impl pallet_glutton::Config for Runtime {
|
impl pallet_glutton::Config for Runtime {
|
||||||
type RuntimeEvent = RuntimeEvent;
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type AdminOrigin = EnsureRoot<AccountId>;
|
||||||
type WeightInfo = pallet_glutton::weights::SubstrateWeight<Runtime>;
|
type WeightInfo = pallet_glutton::weights::SubstrateWeight<Runtime>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,8 @@
|
|||||||
use crate::keyring::*;
|
use crate::keyring::*;
|
||||||
use kitchensink_runtime::{
|
use kitchensink_runtime::{
|
||||||
constants::currency::*, wasm_binary_unwrap, AccountId, AssetsConfig, BabeConfig,
|
constants::currency::*, wasm_binary_unwrap, AccountId, AssetsConfig, BabeConfig,
|
||||||
BalancesConfig, GenesisConfig, GrandpaConfig, IndicesConfig, SessionConfig, SocietyConfig,
|
BalancesConfig, GenesisConfig, GluttonConfig, GrandpaConfig, IndicesConfig, SessionConfig,
|
||||||
StakerStatus, StakingConfig, SystemConfig, BABE_GENESIS_EPOCH_CONFIG,
|
SocietyConfig, StakerStatus, StakingConfig, SystemConfig, BABE_GENESIS_EPOCH_CONFIG,
|
||||||
};
|
};
|
||||||
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
|
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
|
||||||
use sp_runtime::Perbill;
|
use sp_runtime::Perbill;
|
||||||
@@ -94,5 +94,10 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Gen
|
|||||||
alliance: Default::default(),
|
alliance: Default::default(),
|
||||||
alliance_motion: Default::default(),
|
alliance_motion: Default::default(),
|
||||||
nomination_pools: Default::default(),
|
nomination_pools: Default::default(),
|
||||||
|
glutton: GluttonConfig {
|
||||||
|
compute: Default::default(),
|
||||||
|
storage: Default::default(),
|
||||||
|
trash_data_count: Default::default(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,12 +34,18 @@ pub mod weights;
|
|||||||
use blake2::{Blake2b512, Digest};
|
use blake2::{Blake2b512, Digest};
|
||||||
use frame_support::{pallet_prelude::*, weights::WeightMeter};
|
use frame_support::{pallet_prelude::*, weights::WeightMeter};
|
||||||
use frame_system::pallet_prelude::*;
|
use frame_system::pallet_prelude::*;
|
||||||
|
use sp_io::hashing::twox_256;
|
||||||
use sp_runtime::{traits::Zero, Perbill};
|
use sp_runtime::{traits::Zero, Perbill};
|
||||||
use sp_std::{vec, vec::Vec};
|
use sp_std::{vec, vec::Vec};
|
||||||
|
|
||||||
pub use pallet::*;
|
pub use pallet::*;
|
||||||
pub use weights::WeightInfo;
|
pub use weights::WeightInfo;
|
||||||
|
|
||||||
|
/// The size of each value in the `TrashData` storage in bytes.
|
||||||
|
pub const VALUE_SIZE: usize = 1024;
|
||||||
|
/// Max number of entries for `TrashData` storage item
|
||||||
|
pub const MAX_TRASH_DATA_ENTRIES: u32 = 65_000;
|
||||||
|
|
||||||
#[frame_support::pallet]
|
#[frame_support::pallet]
|
||||||
pub mod pallet {
|
pub mod pallet {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -48,6 +54,9 @@ pub mod pallet {
|
|||||||
pub trait Config: frame_system::Config {
|
pub trait Config: frame_system::Config {
|
||||||
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
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.
|
/// Weight information for this pallet.
|
||||||
type WeightInfo: WeightInfo;
|
type WeightInfo: WeightInfo;
|
||||||
}
|
}
|
||||||
@@ -58,11 +67,11 @@ pub mod pallet {
|
|||||||
#[pallet::event]
|
#[pallet::event]
|
||||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
/// The pallet has been (re)initialized by root.
|
/// The pallet has been (re)initialized.
|
||||||
PalletInitialized { reinit: bool },
|
PalletInitialized { reinit: bool },
|
||||||
/// The computation limit has been updated by root.
|
/// The computation limit has been updated.
|
||||||
ComputationLimitSet { compute: Perbill },
|
ComputationLimitSet { compute: Perbill },
|
||||||
/// The storage limit has been updated by root.
|
/// The storage limit has been updated.
|
||||||
StorageLimitSet { storage: Perbill },
|
StorageLimitSet { storage: Perbill },
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,15 +105,51 @@ pub mod pallet {
|
|||||||
pub(super) type TrashData<T: Config> = StorageMap<
|
pub(super) type TrashData<T: Config> = StorageMap<
|
||||||
Hasher = Twox64Concat,
|
Hasher = Twox64Concat,
|
||||||
Key = u32,
|
Key = u32,
|
||||||
Value = [u8; 1024],
|
Value = [u8; VALUE_SIZE],
|
||||||
QueryKind = OptionQuery,
|
QueryKind = OptionQuery,
|
||||||
MaxValues = ConstU32<65_000>,
|
MaxValues = ConstU32<MAX_TRASH_DATA_ENTRIES>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/// The current number of entries in `TrashData`.
|
/// The current number of entries in `TrashData`.
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
pub(crate) type TrashDataCount<T: Config> = StorageValue<_, u32, ValueQuery>;
|
pub(crate) type TrashDataCount<T: Config> = StorageValue<_, u32, ValueQuery>;
|
||||||
|
|
||||||
|
#[pallet::genesis_config]
|
||||||
|
pub struct GenesisConfig {
|
||||||
|
pub compute: Perbill,
|
||||||
|
pub storage: Perbill,
|
||||||
|
pub trash_data_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for GenesisConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
compute: Default::default(),
|
||||||
|
storage: Default::default(),
|
||||||
|
trash_data_count: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pallet::genesis_build]
|
||||||
|
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||||
|
fn build(&self) {
|
||||||
|
assert!(
|
||||||
|
self.trash_data_count <= MAX_TRASH_DATA_ENTRIES,
|
||||||
|
"number of TrashData entries cannot be bigger than {:?}",
|
||||||
|
MAX_TRASH_DATA_ENTRIES
|
||||||
|
);
|
||||||
|
|
||||||
|
(0..self.trash_data_count)
|
||||||
|
.for_each(|i| TrashData::<T>::insert(i, Pallet::<T>::gen_value(i)));
|
||||||
|
|
||||||
|
TrashDataCount::<T>::set(self.trash_data_count);
|
||||||
|
|
||||||
|
<Compute<T>>::put(self.compute);
|
||||||
|
<Storage<T>>::put(self.storage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[pallet::hooks]
|
#[pallet::hooks]
|
||||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||||
fn integrity_test() {
|
fn integrity_test() {
|
||||||
@@ -143,7 +188,11 @@ pub mod pallet {
|
|||||||
impl<T: Config> Pallet<T> {
|
impl<T: Config> Pallet<T> {
|
||||||
/// Initializes the pallet by writing into `TrashData`.
|
/// 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::call_index(0)]
|
||||||
#[pallet::weight(
|
#[pallet::weight(
|
||||||
T::WeightInfo::initialize_pallet_grow(witness_count.unwrap_or_default())
|
T::WeightInfo::initialize_pallet_grow(witness_count.unwrap_or_default())
|
||||||
@@ -154,7 +203,7 @@ pub mod pallet {
|
|||||||
new_count: u32,
|
new_count: u32,
|
||||||
witness_count: Option<u32>,
|
witness_count: Option<u32>,
|
||||||
) -> DispatchResult {
|
) -> DispatchResult {
|
||||||
ensure_root(origin)?;
|
T::AdminOrigin::try_origin(origin).map(|_| ()).or_else(|o| ensure_root(o))?;
|
||||||
|
|
||||||
let current_count = TrashDataCount::<T>::get();
|
let current_count = TrashDataCount::<T>::get();
|
||||||
ensure!(
|
ensure!(
|
||||||
@@ -163,7 +212,8 @@ pub mod pallet {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if new_count > current_count {
|
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 {
|
} else {
|
||||||
(new_count..current_count).for_each(TrashData::<T>::remove);
|
(new_count..current_count).for_each(TrashData::<T>::remove);
|
||||||
}
|
}
|
||||||
@@ -173,28 +223,33 @@ pub mod pallet {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the `Compute` storage value that determines how much of the
|
/// Set how much of the remaining `ref_time` weight should be consumed by `on_idle`.
|
||||||
/// block's weight `ref_time` to use during `on_idle`.
|
|
||||||
///
|
///
|
||||||
/// Only callable by Root.
|
/// Only callable by Root or `AdminOrigin`.
|
||||||
#[pallet::call_index(1)]
|
#[pallet::call_index(1)]
|
||||||
#[pallet::weight(T::WeightInfo::set_compute())]
|
#[pallet::weight(T::WeightInfo::set_compute())]
|
||||||
pub fn set_compute(origin: OriginFor<T>, compute: Perbill) -> DispatchResult {
|
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);
|
Compute::<T>::set(compute);
|
||||||
|
|
||||||
Self::deposit_event(Event::ComputationLimitSet { compute });
|
Self::deposit_event(Event::ComputationLimitSet { compute });
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the `Storage` storage value that determines the PoV size usage
|
/// Set how much of the remaining `proof_size` weight should be consumed by `on_idle`.
|
||||||
/// for each block.
|
//
|
||||||
|
/// 100% means that all remaining `proof_size` will be consumed. The PoV benchmarking
|
||||||
|
/// results that are used here are likely an over-estimation. 100% intended consumption will
|
||||||
|
/// therefore translate to less than 100% actual consumption. In the future, this could be
|
||||||
|
/// counter-acted by allowing the glutton to specify over-unity consumption ratios.
|
||||||
///
|
///
|
||||||
/// Only callable by Root.
|
/// Only callable by Root or `AdminOrigin`.
|
||||||
#[pallet::call_index(2)]
|
#[pallet::call_index(2)]
|
||||||
#[pallet::weight(T::WeightInfo::set_storage())]
|
#[pallet::weight(T::WeightInfo::set_storage())]
|
||||||
pub fn set_storage(origin: OriginFor<T>, storage: Perbill) -> DispatchResult {
|
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);
|
Storage::<T>::set(storage);
|
||||||
|
|
||||||
Self::deposit_event(Event::StorageLimitSet { storage });
|
Self::deposit_event(Event::StorageLimitSet { storage });
|
||||||
@@ -251,7 +306,7 @@ pub mod pallet {
|
|||||||
// compiler does not know that (hopefully).
|
// compiler does not know that (hopefully).
|
||||||
debug_assert!(clobber.len() == 64);
|
debug_assert!(clobber.len() == 64);
|
||||||
if clobber.len() == 65 {
|
if clobber.len() == 65 {
|
||||||
TrashData::<T>::insert(0, [clobber[0] as u8; 1024]);
|
TrashData::<T>::insert(0, [clobber[0] as u8; VALUE_SIZE]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,5 +343,17 @@ pub mod pallet {
|
|||||||
Some(i) => Ok(i as u32),
|
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(twox_256);
|
||||||
|
ret[i as usize * 32..(i + 1) as usize * 32].copy_from_slice(&hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ impl frame_system::Config for Test {
|
|||||||
|
|
||||||
impl Config for Test {
|
impl Config for Test {
|
||||||
type RuntimeEvent = RuntimeEvent;
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ fn initialize_pallet_works() {
|
|||||||
Error::<Test>::AlreadyInitialized
|
Error::<Test>::AlreadyInitialized
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(TrashData::<Test>::get(0), Some([0; 1024]));
|
assert_eq!(TrashData::<Test>::get(0), Some(Pallet::<Test>::gen_value(0)));
|
||||||
assert_eq!(TrashData::<Test>::get(1), Some([1; 1024]));
|
assert_eq!(TrashData::<Test>::get(1), Some(Pallet::<Test>::gen_value(1)));
|
||||||
assert_eq!(TrashData::<Test>::get(2), None);
|
assert_eq!(TrashData::<Test>::get(2), None);
|
||||||
|
|
||||||
assert_eq!(TrashDataCount::<Test>::get(), 2);
|
assert_eq!(TrashDataCount::<Test>::get(), 2);
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user