Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for items weight in pallet parameters #6477

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions prdoc/pr_6477.prdoc
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions substrate/frame/parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -37,6 +38,7 @@ std = [
"serde",
"sp-core/std",
"sp-runtime/std",
"cumulus-primitives-storage-weight-reclaim/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down
17 changes: 17 additions & 0 deletions substrate/frame/parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -167,6 +168,22 @@ pub mod pallet {
type WeightInfo: WeightInfo;
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
let proof_size_before: u64 = get_proof_size().unwrap_or(0);

let items = Parameters::<T>::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<T: Config> {
Expand Down