diff --git a/Cargo.lock b/Cargo.lock index e877cd9d24860..b80dbc7713825 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11132,6 +11132,7 @@ dependencies = [ name = "pallet-parameters" version = "0.1.0" dependencies = [ + "cumulus-primitives-storage-weight-reclaim", "docify", "frame-benchmarking", "frame-support", diff --git a/substrate/frame/parameters/Cargo.toml b/substrate/frame/parameters/Cargo.toml index b718b391019a7..7504e67cedb0b 100644 --- a/substrate/frame/parameters/Cargo.toml +++ b/substrate/frame/parameters/Cargo.toml @@ -19,6 +19,7 @@ frame-system = { path = "../system", default-features = false } sp-core = { path = "../../primitives/core", default-features = false } sp-runtime = { path = "../../primitives/runtime", default-features = false } sp-std = { path = "../../primitives/std", default-features = false } +cumulus-primitives-storage-weight-reclaim = { path = "../../../cumulus/primitives/storage-weight-reclaim", default-features = false} frame-benchmarking = { path = "../benchmarking", default-features = false, optional = true } [dev-dependencies] @@ -39,6 +40,7 @@ std = [ "sp-core/std", "sp-runtime/std", "sp-std/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 55a6f1ff91ded..c170d4e270b3b 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 {