From 77437683e279a4c5fc29845941487f16ba2c9b9a Mon Sep 17 00:00:00 2001 From: NachoPal Date: Mon, 22 May 2023 13:08:39 +0200 Subject: [PATCH 1/9] glutton gensis config added --- bin/node/cli/src/chain_spec.rs | 13 +++++++++---- bin/node/testing/src/genesis.rs | 9 +++++++-- frame/glutton/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 85a08e71cc5a9..5a91d9c0f5920 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -21,10 +21,10 @@ use grandpa_primitives::AuthorityId as GrandpaId; use kitchensink_runtime::{ constants::currency::*, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig, - BalancesConfig, Block, CouncilConfig, DemocracyConfig, ElectionsConfig, GrandpaConfig, - ImOnlineConfig, IndicesConfig, MaxNominations, NominationPoolsConfig, SessionConfig, - SessionKeys, SocietyConfig, StakerStatus, StakingConfig, SudoConfig, SystemConfig, - TechnicalCommitteeConfig, + BalancesConfig, Block, CouncilConfig, DemocracyConfig, ElectionsConfig, GluttonConfig, + GrandpaConfig, ImOnlineConfig, IndicesConfig, MaxNominations, NominationPoolsConfig, + SessionConfig, SessionKeys, SocietyConfig, StakerStatus, StakingConfig, SudoConfig, + SystemConfig, TechnicalCommitteeConfig, }; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use sc_chain_spec::ChainSpecExtension; @@ -372,6 +372,11 @@ pub fn testnet_genesis( min_join_bond: 1 * DOLLARS, ..Default::default() }, + glutton: GluttonConfig { + compute: Default::default(), + storage: Default::default(), + trash_data_count: Default::default(), + }, } } diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index d542bb29c2539..f91070017c0f8 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -21,8 +21,8 @@ use crate::keyring::*; use kitchensink_runtime::{ constants::currency::*, wasm_binary_unwrap, AccountId, AssetsConfig, BabeConfig, - BalancesConfig, GenesisConfig, GrandpaConfig, IndicesConfig, SessionConfig, SocietyConfig, - StakerStatus, StakingConfig, SystemConfig, BABE_GENESIS_EPOCH_CONFIG, + BalancesConfig, GenesisConfig, GluttonConfig, GrandpaConfig, IndicesConfig, SessionConfig, + SocietyConfig, StakerStatus, StakingConfig, SystemConfig, BABE_GENESIS_EPOCH_CONFIG, }; use sp_keyring::{Ed25519Keyring, Sr25519Keyring}; use sp_runtime::Perbill; @@ -94,5 +94,10 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec) -> Gen alliance: Default::default(), alliance_motion: Default::default(), nomination_pools: Default::default(), + glutton: GluttonConfig { + compute: Default::default(), + storage: Default::default(), + trash_data_count: Default::default(), + }, } } diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index e9a46374a5ade..b99d42da09d3f 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -105,6 +105,37 @@ pub mod pallet { #[pallet::storage] pub(crate) type TrashDataCount = 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 GenesisBuild for GenesisConfig { + fn build(&self) { + >::put(self.compute); + >::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])); + } + + TrashDataCount::::set(self.trash_data_count); + } + } + #[pallet::hooks] impl Hooks> for Pallet { fn integrity_test() { From d5978eecb1fdde782c0bdf0eb0f0becc9ee430bd Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Mon, 22 May 2023 18:10:44 +0200 Subject: [PATCH 2/9] Glutton pallet updates (#14192) * Add admin origin and other fixes Signed-off-by: Oliver Tale-Yazdi * Remove magic number Signed-off-by: Oliver Tale-Yazdi * Typo Signed-off-by: Oliver Tale-Yazdi --------- Signed-off-by: Oliver Tale-Yazdi --- frame/glutton/src/lib.rs | 61 +++++++++++++++++++++++++++----------- frame/glutton/src/mock.rs | 1 + frame/glutton/src/tests.rs | 15 ++++++++-- 3 files changed, 58 insertions(+), 19 deletions(-) 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"); +} From ab41d6464751bb7e4aa741cbefaf8fc901682092 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Mon, 22 May 2023 18:24:38 +0200 Subject: [PATCH 3/9] fix genesis_build --- frame/glutton/src/lib.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index c8654cbbbd6ed..aa7bb332d5871 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -42,6 +42,8 @@ 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] pub mod pallet { @@ -104,7 +106,7 @@ pub mod pallet { Key = u32, Value = [u8; VALUE_SIZE], QueryKind = OptionQuery, - MaxValues = ConstU32<65_000>, + MaxValues = ConstU32, >; /// The current number of entries in `TrashData`. @@ -131,15 +133,19 @@ pub mod pallet { #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { - >::put(self.compute); - >::put(self.storage); + assert!( + self.trash_data_count <= MAX_TRASH_DATA_ENTRIES, + "number of TrashData entries can not be bigger than {:?}", + MAX_TRASH_DATA_ENTRIES + ); - if self.trash_data_count <= 65_000 { - (0..self.trash_data_count) - .for_each(|i| TrashData::::insert(i, Pallet::::gen_value(i))); - } + (0..self.trash_data_count) + .for_each(|i| TrashData::::insert(i, Pallet::::gen_value(i))); TrashDataCount::::set(self.trash_data_count); + + >::put(self.compute); + >::put(self.storage); } } From 2123b96baf0650598d8e1e5d9dd32c26dae21c25 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Mon, 22 May 2023 21:26:09 +0200 Subject: [PATCH 4/9] Fix docs Signed-off-by: Oliver Tale-Yazdi --- frame/glutton/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index aa7bb332d5871..8a6226316edc0 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -238,8 +238,10 @@ pub mod pallet { /// 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 + /// 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 or `AdminOrigin`. #[pallet::call_index(2)] From b2b7ad6da81bc0c846e129d864f7cb69455c1db3 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Mon, 22 May 2023 21:27:47 +0200 Subject: [PATCH 5/9] Fix kitchensink runtime Signed-off-by: Oliver Tale-Yazdi --- bin/node/runtime/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a2cb64cadd7c5..0a272a766c1e5 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -377,6 +377,7 @@ impl pallet_scheduler::Config for Runtime { impl pallet_glutton::Config for Runtime { type RuntimeEvent = RuntimeEvent; + type AdminOrigin = EnsureRoot; type WeightInfo = pallet_glutton::weights::SubstrateWeight; } From 67da6321fb1afea6ae5e9170556a0a528b05a89d Mon Sep 17 00:00:00 2001 From: NachoPal Date: Tue, 23 May 2023 12:12:32 +0200 Subject: [PATCH 6/9] fix twox_256 --- frame/glutton/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index 8a6226316edc0..4ef536c52c6c7 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -36,6 +36,7 @@ use frame_support::{pallet_prelude::*, weights::WeightMeter}; use frame_system::pallet_prelude::*; use sp_runtime::{traits::Zero, Perbill}; use sp_std::{vec, vec::Vec}; +use sp_io::hashing::twox_256; pub use pallet::*; pub use weights::WeightInfo; @@ -348,7 +349,7 @@ pub mod pallet { 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); + let hash = (seed, i).using_encoded(|p| twox_256(p)); ret[i as usize * 32..(i + 1) as usize * 32].copy_from_slice(&hash); } From 4f9d468740af96cb0e2d217b03399724d83c2830 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Tue, 23 May 2023 12:13:43 +0200 Subject: [PATCH 7/9] fmt --- frame/glutton/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index 4ef536c52c6c7..19454c88e8841 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -34,9 +34,9 @@ pub mod weights; use blake2::{Blake2b512, Digest}; use frame_support::{pallet_prelude::*, weights::WeightMeter}; use frame_system::pallet_prelude::*; +use sp_io::hashing::twox_256; use sp_runtime::{traits::Zero, Perbill}; use sp_std::{vec, vec::Vec}; -use sp_io::hashing::twox_256; pub use pallet::*; pub use weights::WeightInfo; From f926893508e7b9809d93c233e9cb530b700a0da3 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Tue, 23 May 2023 12:30:08 +0200 Subject: [PATCH 8/9] twox_256 clean --- frame/glutton/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index 19454c88e8841..964897cc4316c 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -349,7 +349,7 @@ pub mod pallet { let mut ret = [0u8; VALUE_SIZE]; for i in 0u32..(VALUE_SIZE as u32 / 32) { - let hash = (seed, i).using_encoded(|p| twox_256(p)); + let hash = (seed, i).using_encoded(twox_256); ret[i as usize * 32..(i + 1) as usize * 32].copy_from_slice(&hash); } From d9f8f3b94469cfd7f82b9755ebd042eed4d01e5b Mon Sep 17 00:00:00 2001 From: Ignacio Palacios Date: Tue, 23 May 2023 14:05:34 +0200 Subject: [PATCH 9/9] nitpick Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> --- frame/glutton/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/glutton/src/lib.rs b/frame/glutton/src/lib.rs index 964897cc4316c..50a26a495f794 100644 --- a/frame/glutton/src/lib.rs +++ b/frame/glutton/src/lib.rs @@ -136,7 +136,7 @@ pub mod pallet { fn build(&self) { assert!( self.trash_data_count <= MAX_TRASH_DATA_ENTRIES, - "number of TrashData entries can not be bigger than {:?}", + "number of TrashData entries cannot be bigger than {:?}", MAX_TRASH_DATA_ENTRIES );