Skip to content

Commit

Permalink
add benchmarking setup trait for mmr pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed Jul 15, 2024
1 parent c6f3512 commit 45f8fe5
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 10 deletions.
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.

23 changes: 18 additions & 5 deletions polkadot/runtime/parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ workspace = true
[dependencies]
impl-trait-for-tuples = "0.2.2"
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive", "max-encoded-len"] }
codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = [
"derive",
"max-encoded-len",
] }
log = { workspace = true }
rustc-hex = { version = "2.1.0", default-features = false }
scale-info = { version = "2.11.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.11.1", default-features = false, features = [
"derive",
] }
serde = { features = ["alloc", "derive"], workspace = true }
derive_more = "0.99.17"
bitflags = "1.3.2"
Expand All @@ -24,10 +29,16 @@ sp-api = { path = "../../../substrate/primitives/api", default-features = false
sp-inherents = { path = "../../../substrate/primitives/inherents", default-features = false }
sp-std = { package = "sp-std", path = "../../../substrate/primitives/std", default-features = false }
sp-io = { path = "../../../substrate/primitives/io", default-features = false }
sp-runtime = { path = "../../../substrate/primitives/runtime", default-features = false, features = ["serde"] }
sp-runtime = { path = "../../../substrate/primitives/runtime", default-features = false, features = [
"serde",
] }
sp-session = { path = "../../../substrate/primitives/session", default-features = false }
sp-staking = { path = "../../../substrate/primitives/staking", default-features = false, features = ["serde"] }
sp-core = { path = "../../../substrate/primitives/core", default-features = false, features = ["serde"] }
sp-staking = { path = "../../../substrate/primitives/staking", default-features = false, features = [
"serde",
] }
sp-core = { path = "../../../substrate/primitives/core", default-features = false, features = [
"serde",
] }
sp-keystore = { path = "../../../substrate/primitives/keystore", optional = true, default-features = false }
sp-application-crypto = { path = "../../../substrate/primitives/application-crypto", default-features = false, optional = true }
sp-tracing = { path = "../../../substrate/primitives/tracing", default-features = false, optional = true }
Expand All @@ -39,6 +50,7 @@ pallet-balances = { path = "../../../substrate/frame/balances", default-features
pallet-babe = { path = "../../../substrate/frame/babe", default-features = false }
pallet-broker = { path = "../../../substrate/frame/broker", default-features = false }
pallet-message-queue = { path = "../../../substrate/frame/message-queue", default-features = false }
pallet-mmr = { path = "../../../substrate/frame/merkle-mountain-range", default-features = false, optional = true }
pallet-session = { path = "../../../substrate/frame/session", default-features = false }
pallet-staking = { path = "../../../substrate/frame/staking", default-features = false }
pallet-timestamp = { path = "../../../substrate/frame/timestamp", default-features = false }
Expand Down Expand Up @@ -124,6 +136,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-broker/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-mmr/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/parachains/src/paras/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use polkadot_primitives::{
};
use sp_runtime::traits::{One, Saturating};

pub mod mmr_setup;
mod pvf_check;

use self::pvf_check::{VoteCause, VoteOutcome};
Expand Down
39 changes: 39 additions & 0 deletions polkadot/runtime/parachains/src/paras/benchmarking/mmr_setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! Implements benchmarking setup for the `merkle-mountain-range` pallet.
use crate::paras::*;
use pallet_mmr::BenchmarkHelper;

/// Struct to setup benchmarks for the `merkle-mountain-range` pallet.
pub struct MmrSetup<T>(sp_std::marker::PhantomData<T>);

impl<T> BenchmarkHelper for MmrSetup<T>
where
T: Config,
{
fn setup() {
// Create a head with 1024 bytes of data.
let head = vec![42u8; 1024];

for para in 0..(2 * MAX_PARA_HEADS) {
let id = (para as u32).into();
let h = head.clone().into();
Pallet::<T>::heads_insert(&id, h);
}
}
}
2 changes: 1 addition & 1 deletion polkadot/runtime/parachains/src/paras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ use serde::{Deserialize, Serialize};
pub use crate::Origin as ParachainOrigin;

#[cfg(feature = "runtime-benchmarks")]
pub(crate) mod benchmarking;
pub mod benchmarking;

#[cfg(test)]
pub(crate) mod tests;
Expand Down
4 changes: 3 additions & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,11 @@ impl pallet_mmr::Config for Runtime {
const INDEXING_PREFIX: &'static [u8] = mmr::INDEXING_PREFIX;
type Hashing = Keccak256;
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
type WeightInfo = ();
type LeafData = pallet_beefy_mmr::Pallet<Runtime>;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = parachains_paras::benchmarking::mmr_setup::MmrSetup<Runtime>;
}

parameter_types! {
Expand Down
7 changes: 5 additions & 2 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,11 @@ impl pallet_mmr::Config for Runtime {
const INDEXING_PREFIX: &'static [u8] = mmr::INDEXING_PREFIX;
type Hashing = Keccak256;
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
type WeightInfo = ();
type LeafData = pallet_beefy_mmr::Pallet<Runtime>;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = parachains_paras::benchmarking::mmr_setup::MmrSetup<Runtime>;
}

/// MMR helper types.
Expand Down Expand Up @@ -1005,7 +1007,8 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
matches!(
c,
RuntimeCall::Staking(..) |
RuntimeCall::Session(..) | RuntimeCall::Utility(..) |
RuntimeCall::Session(..) |
RuntimeCall::Utility(..) |
RuntimeCall::FastUnstake(..) |
RuntimeCall::VoterList(..) |
RuntimeCall::NominationPools(..)
Expand Down
2 changes: 2 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,8 @@ impl pallet_mmr::Config for Runtime {
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand Down
4 changes: 3 additions & 1 deletion substrate/frame/merkle-mountain-range/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false }
log = { workspace = true }
scale-info = { version = "2.11.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.11.1", default-features = false, features = [
"derive",
] }
frame-benchmarking = { path = "../benchmarking", default-features = false, optional = true }
frame-support = { path = "../support", default-features = false }
frame-system = { path = "../system", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/merkle-mountain-range/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ benchmarks_instance_pallet! {
let x in 1 .. 1_000;

let leaves = x as NodeIndex;

<<T as pallet::Config::<I>>::BenchmarkHelper as BenchmarkHelper>::setup();
}: {
for b in 0..leaves {
Pallet::<T, I>::on_initialize((b as u32).into());
Expand Down
15 changes: 15 additions & 0 deletions substrate/frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ pub trait WeightInfo {
fn on_initialize(peaks: NodeIndex) -> Weight;
}

/// This trait decoples dependencies on pallets needed for benchmarking.
#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper {
fn setup();
}

#[cfg(feature = "runtime-benchmarks")]
impl BenchmarkHelper for () {
fn setup() {}
}

/// An MMR specific to the pallet.
type ModuleMmr<StorageType, T, I> = mmr::Mmr<StorageType, T, I, LeafOf<T, I>>;

Expand Down Expand Up @@ -203,6 +214,10 @@ pub mod pallet {

/// Weights for this pallet.
type WeightInfo: WeightInfo;

/// Benchmarking setup helper trait.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper;
}

/// Latest MMR Root hash.
Expand Down

0 comments on commit 45f8fe5

Please sign in to comment.