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

feat(ismp-grandpa): add benchmarking support #353

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

13 changes: 12 additions & 1 deletion modules/ismp/clients/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ 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 }
sp-std = { workspace = true }

# polytope labs
ismp = { workspace = true }
grandpa-verifier-primitives = { workspace = true }
grandpa-verifier-primitives = { workspace = true }
grandpa-verifier = { workspace = true }
pallet-ismp = { workspace = true }

Expand All @@ -40,6 +43,7 @@ substrate-state-machine = { workspace = true }
[features]
default = ["std"]
std = [
"sp-std/std",
"codec/std",
"frame-support/std",
"frame-system/std",
Expand All @@ -60,3 +64,10 @@ std = [
"finality-grandpa/std",
]
try-runtime = []

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-ismp/runtime-benchmarks",
]
50 changes: 50 additions & 0 deletions modules/ismp/clients/grandpa/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this benchmark be dynamic? so that the weight is calculated based on the number of items to be added?

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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this too

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(())
}
}

13 changes: 12 additions & 1 deletion modules/ismp/clients/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;


#[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;
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should take the length of state machines

}

pub mod weights;
#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -41,6 +51,7 @@ pub mod pallet {

/// IsmpHost implementation
type IsmpHost: IsmpHost + Default;
type WeightInfo: WeightInfo;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some doc comments will be nice

}

/// Events emitted by this pallet
Expand Down Expand Up @@ -89,7 +100,7 @@ pub mod pallet {

/// Remove a state machine from the list of supported state machines
#[pallet::call_index(1)]
#[pallet::weight(T::DbWeight::get().writes(state_machines.len() as u64))]
#[pallet::weight(T::WeightInfo::remove_state_machines())]
pub fn remove_state_machines(
origin: OriginFor<T>,
state_machines: Vec<StateMachine>,
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
11 changes: 7 additions & 4 deletions parachain/runtimes/nexus/src/ismp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ 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>;
}

impl pallet_ismp::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AdminOrigin = EnsureRoot<AccountId>;
Expand All @@ -108,10 +114,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