Skip to content

Commit

Permalink
feat(ismp-grandpa): add benchmarking support
Browse files Browse the repository at this point in the history
- Add WeightInfo trait and default implementation
- Integrate benchmarking feature flag and dependencies
- Configure benchmarking in Gargantua and Nexus runtimes
- Add placeholder weight values for pallet extrinsics

This implements benchmarking support for ismp-grandpa, addressing #350.
  • Loading branch information
sylvaincormier committed Dec 15, 2024
1 parent 46da0fb commit 8507b80
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions modules/ismp/clients/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ keywords = ["substrate", "polkadot-sdk", "ISMP", "interoperability", "GRANDPA"]
readme = "./README.md"

[dependencies]
anyhow = {workspace = true}
codec = { workspace = true, features = ["derive"] }
primitive-types = { workspace = true }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
merkle-mountain-range = { workspace = true }
finality-grandpa = { version = "0.16.0", features = ["derive-codec"], default-features = false }
frame-benchmarking = { workspace = true, optional = true }

# polytope labs
ismp = { workspace = true }
Expand Down Expand Up @@ -60,3 +62,12 @@ std = [
"finality-grandpa/std",
]
try-runtime = []



runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-ismp/runtime-benchmarks",
]
51 changes: 51 additions & 0 deletions modules/ismp/clients/grandpa/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use ismp::host::StateMachine;

#[benchmarks(where T: Config)]
mod benchmarks {
use super::*;

#[benchmark]
fn add_state_machines() -> Result<(), BenchmarkError> {
let state_machines: Vec<AddStateMachine> = (0..10)
.map(|i| AddStateMachine {
state_machine: StateMachine::Polkadot(i as u32),
slot_duration: 6000,
})
.collect();

#[block]
{
Pallet::<T>::add_state_machines(RawOrigin::Root.into(), state_machines)?;
}

Ok(())
}

#[benchmark]
fn remove_state_machines() -> Result<(), BenchmarkError> {
let state_machines: Vec<StateMachine> = (0..10)
.map(|i| StateMachine::Polkadot(i as u32))
.collect();

for i in 0..10 {
SupportedStateMachines::<T>::insert(
StateMachine::Polkadot(i as u32),
6000
);
}

#[block]
{
Pallet::<T>::remove_state_machines(RawOrigin::Root.into(), state_machines)?;
}

Ok(())
}
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
22 changes: 22 additions & 0 deletions modules/ismp/clients/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,34 @@
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;

#[cfg(test)]
pub mod mock;

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

pub mod consensus;
pub mod messages;

use alloc::vec::Vec;
use ismp::host::StateMachine;
pub use pallet::*;
use frame_support::pallet_prelude::Weight;
pub trait WeightInfo {
fn add_state_machines() -> Weight;
fn remove_state_machines() -> Weight;
}

pub struct SubstrateWeight;
impl WeightInfo for SubstrateWeight {
fn add_state_machines() -> Weight {
Weight::from_parts(10_000_000, 0)
}

fn remove_state_machines() -> Weight {
Weight::from_parts(10_000_000, 0)
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -41,6 +62,7 @@ pub mod pallet {

/// IsmpHost implementation
type IsmpHost: IsmpHost + Default;
type WeightInfo: WeightInfo;
}

/// Events emitted by this pallet
Expand Down
36 changes: 36 additions & 0 deletions modules/ismp/clients/grandpa/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of Hyperbridge.

// Copyright (C) Polytope Labs Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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.

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;

/// Weights for ismp_grandpa
pub struct WeightInfo<T>(PhantomData<T>);

impl<T: frame_system::Config> crate::WeightInfo for WeightInfo<T> {
fn add_state_machines() -> Weight {
Weight::from_parts(20_000, 0)
.saturating_add(T::DbWeight::get().writes(10))
}

fn remove_state_machines() -> Weight {
Weight::from_parts(20_000, 0)
.saturating_add(T::DbWeight::get().writes(10))
}
}
3 changes: 2 additions & 1 deletion parachain/runtimes/gargantua/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ runtime-benchmarks = [
"pallet-message-queue/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"parachains-common/runtime-benchmarks"
"parachains-common/runtime-benchmarks",
"ismp-grandpa/runtime-benchmarks",
]

try-runtime = [
Expand Down
6 changes: 4 additions & 2 deletions parachain/runtimes/gargantua/src/ismp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl pallet_state_coprocessor::Config for Runtime {
type Mmr = Mmr;
}


pub struct Coprocessor;

impl Get<Option<StateMachine>> for Coprocessor {
Expand Down Expand Up @@ -107,8 +108,9 @@ impl pallet_ismp::Config for Runtime {
}

impl ismp_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = Ismp;
type RuntimeEvent = RuntimeEvent;
type IsmpHost = pallet_ismp::Pallet<Runtime>;
type WeightInfo = ismp_grandpa::weights::WeightInfo<Runtime>;
}

impl pallet_token_governor::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions parachain/runtimes/gargantua/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ mod benches {
[pallet_collective, TechnicalCollective]
[cumulus_pallet_parachain_system, ParachainSystem]
[pallet_session, SessionBench::<Runtime>]
[ismp_grandpa, IsmpGrandpa]

);
}

Expand Down
10 changes: 6 additions & 4 deletions parachain/runtimes/nexus/src/ismp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl Get<Option<StateMachine>> for Coprocessor {
}
}

impl ismp_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = pallet_ismp::Pallet<Runtime>;
type WeightInfo = ismp_grandpa::weights::WeightInfo<Runtime>; // Changed line
}
impl pallet_ismp::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AdminOrigin = EnsureRoot<AccountId>;
Expand All @@ -108,10 +113,7 @@ impl pallet_ismp::Config for Runtime {
type WeightProvider = ();
}

impl ismp_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = Ismp;
}


impl pallet_ismp_relayer::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
Expand Down

0 comments on commit 8507b80

Please sign in to comment.