From b05671029b88069b46ad0b5ab0be9f0f71b82d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Tue, 6 Aug 2024 11:35:34 +0200 Subject: [PATCH] Merge pull request #8 from moonbeam-foundation/tarekkma/pallet-parameters-account-weight --- Cargo.lock | 1 + substrate/frame/parameters/Cargo.toml | 2 ++ substrate/frame/parameters/src/lib.rs | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index f5e0ad990d19..466979cca512 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11081,6 +11081,7 @@ dependencies = [ name = "pallet-parameters" version = "0.8.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 bd232739b645..4a02752aabfc 100644 --- a/substrate/frame/parameters/Cargo.toml +++ b/substrate/frame/parameters/Cargo.toml @@ -18,6 +18,7 @@ frame-system.workspace = true sp-core.workspace = true sp-runtime.workspace = true frame-benchmarking = { optional = true, workspace = true } +cumulus-primitives-storage-weight-reclaim = { workspace = true, default-features = false} [dev-dependencies] sp-core = { features = ["std"], default-features = true, path = "../../primitives/core" } @@ -36,6 +37,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 {