diff --git a/Cargo.lock b/Cargo.lock index 775a7fe99e1e..385d647bc0e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14364,6 +14364,7 @@ dependencies = [ name = "pallet-parameters" version = "0.1.0" dependencies = [ + "cumulus-primitives-storage-weight-reclaim", "docify", "frame-benchmarking 28.0.0", "frame-support 28.0.0", diff --git a/prdoc/pr_6477.prdoc b/prdoc/pr_6477.prdoc new file mode 100644 index 000000000000..ec145c22d6a5 --- /dev/null +++ b/prdoc/pr_6477.prdoc @@ -0,0 +1,16 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Account for parameters weight + +doc: + - audience: Runtime Dev + description: | + This PR addresses potential discrepancies in weight measurement by accounting for all parameters at block initialization. + It introduces a mechanism to account for the weight (both `ref_time` and `proof_size`) of all parameters at the start of each block. + This is necessary to account for parameters that might be accessed in scenarios not covered by benchmarking. + An example could be the parameter usage within the `OnChargeTransaction` of the `pallet_transaction_payment`. + +crates: +- name: pallet-parameters + bump: minor diff --git a/substrate/frame/parameters/Cargo.toml b/substrate/frame/parameters/Cargo.toml index a97ba1172a50..b41a818d748e 100644 --- a/substrate/frame/parameters/Cargo.toml +++ b/substrate/frame/parameters/Cargo.toml @@ -18,6 +18,7 @@ frame-support = { features = ["experimental"], workspace = true } frame-system = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } +cumulus-primitives-storage-weight-reclaim = { workspace = true, default-features = false} frame-benchmarking = { optional = true, workspace = true } [dev-dependencies] @@ -37,6 +38,7 @@ std = [ "serde", "sp-core/std", "sp-runtime/std", + "cumulus-primitives-storage-weight-reclaim/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", diff --git a/substrate/frame/parameters/src/lib.rs b/substrate/frame/parameters/src/lib.rs index 55a6f1ff91de..c170d4e270b3 100644 --- a/substrate/frame/parameters/src/lib.rs +++ b/substrate/frame/parameters/src/lib.rs @@ -125,6 +125,7 @@ use frame_support::traits::{ dynamic_params::{AggregatedKeyValue, IntoKey, Key, RuntimeParameterStore, TryIntoKey}, EnsureOriginWithArg, }; +use cumulus_primitives_storage_weight_reclaim::get_proof_size; mod benchmarking; #[cfg(test)] @@ -167,6 +168,22 @@ pub mod pallet { type WeightInfo: WeightInfo; } + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(_: BlockNumberFor) -> Weight { + let proof_size_before: u64 = get_proof_size().unwrap_or(0); + + let items = Parameters::::iter().count() as u64; + + let proof_size_after: u64 = get_proof_size().unwrap_or(0); + + let proof_size_diff = proof_size_after.saturating_sub(proof_size_before); + + Weight::from_parts(0, proof_size_diff) + .saturating_add(T::DbWeight::get().reads(items)) + } + } + #[pallet::event] #[pallet::generate_deposit(pub(crate) fn deposit_event)] pub enum Event {