From 1d4b12eceaa1d6bb143d318d861bcc65e205bbc6 Mon Sep 17 00:00:00 2001 From: fmiguelgarcia Date: Wed, 28 Jun 2023 12:27:42 +0200 Subject: [PATCH 1/2] Fix the `set_code`'s weights --- pallets/system/src/lib.rs | 22 ++++++++++++++++++++-- runtime/src/constants.rs | 17 +++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pallets/system/src/lib.rs b/pallets/system/src/lib.rs index b84f8dafe..a2b3d129d 100644 --- a/pallets/system/src/lib.rs +++ b/pallets/system/src/lib.rs @@ -445,7 +445,7 @@ pub mod pallet { /// expensive. We will treat this as a full block. /// # #[pallet::call_index(2)] - #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] + #[pallet::weight(weights::set_code::())] pub fn set_code(origin: OriginFor, code: Vec) -> DispatchResultWithPostInfo { ensure_root(origin)?; Self::can_set_code(&code)?; @@ -463,7 +463,7 @@ pub mod pallet { /// The weight of this function is dependent on the runtime. We will treat this as a full /// block. # #[pallet::call_index(3)] - #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] + #[pallet::weight(weights::set_code::())] pub fn set_code_without_checks( origin: OriginFor, code: Vec, @@ -1867,3 +1867,21 @@ pub mod pallet_prelude { /// Type alias for the `BlockNumber` associated type of system config. pub type BlockNumberFor = ::BlockNumber; } + +mod weigths { + use super::*; + + /// Weight for `system::set_code`. + /// + /// It fills the whole block in terms of weight. + fn set_code() -> (Weight, DispatchClass) { + let class = DispatchClass::Operational; + let block_weights = T::BlockWeights::get(); + let max = block_weights + .get(class) + .max_extrinsic + .unwrap_or(block_weights.max_block); + + (max, class) + } +} diff --git a/runtime/src/constants.rs b/runtime/src/constants.rs index af3c57912..9cfa473c4 100644 --- a/runtime/src/constants.rs +++ b/runtime/src/constants.rs @@ -100,7 +100,9 @@ pub mod time { pub mod system { use da_primitives::NORMAL_DISPATCH_RATIO; - use frame_support::weights::constants::{ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND}; + use frame_support::weights::constants::{ + ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_MILLIS, WEIGHT_REF_TIME_PER_SECOND, + }; use frame_system::limits::BlockWeights as SystemBlockWeights; use super::*; @@ -112,10 +114,21 @@ pub mod system { /// We assume that ~10% of the block weight is consumed by `on_initialize` handlers. /// This is used to limit the maximal weight of a single extrinsic. const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); + + /// Proof size allowed up to 500ms. + const MAX_POV_SIZE: u64 = WEIGHT_REF_TIME_PER_MILLIS.saturating_mul(500); + /// We allow for 2 seconds of compute with a 6 second average block time, with maximum proof size. + #[cfg(feature = "fast-runtime")] const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_ref_time(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2)) - .set_proof_size(u64::MAX); + .set_proof_size(MAX_POV_SIZE); + + /// We allow for 5 seconds of compute with a 20 second average block time, with maximum proof size. + #[cfg(not(feature = "fast-runtime"))] + const MAXIMUM_BLOCK_WEIGHT: Weight = + Weight::from_ref_time(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(5)) + .set_proof_size(MAX_POV_SIZE); parameter_types! { pub RuntimeBlockWeights: SystemBlockWeights = SystemBlockWeights::builder() From cea467c57619e7d606a3ccdf9615c280771dcb9b Mon Sep 17 00:00:00 2001 From: fmiguelgarcia Date: Wed, 28 Jun 2023 12:42:52 +0200 Subject: [PATCH 2/2] Improve naming & code org --- pallets/system/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pallets/system/src/lib.rs b/pallets/system/src/lib.rs index a2b3d129d..111591313 100644 --- a/pallets/system/src/lib.rs +++ b/pallets/system/src/lib.rs @@ -445,7 +445,7 @@ pub mod pallet { /// expensive. We will treat this as a full block. /// # #[pallet::call_index(2)] - #[pallet::weight(weights::set_code::())] + #[pallet::weight(weight_helper::set_code::())] pub fn set_code(origin: OriginFor, code: Vec) -> DispatchResultWithPostInfo { ensure_root(origin)?; Self::can_set_code(&code)?; @@ -463,7 +463,7 @@ pub mod pallet { /// The weight of this function is dependent on the runtime. We will treat this as a full /// block. # #[pallet::call_index(3)] - #[pallet::weight(weights::set_code::())] + #[pallet::weight(weight_helper::set_code::())] pub fn set_code_without_checks( origin: OriginFor, code: Vec, @@ -1868,13 +1868,13 @@ pub mod pallet_prelude { pub type BlockNumberFor = ::BlockNumber; } -mod weigths { +mod weight_helper { use super::*; /// Weight for `system::set_code`. /// /// It fills the whole block in terms of weight. - fn set_code() -> (Weight, DispatchClass) { + pub(crate) fn set_code() -> (Weight, DispatchClass) { let class = DispatchClass::Operational; let block_weights = T::BlockWeights::get(); let max = block_weights