Skip to content

Commit 1ff1889

Browse files
Iv thea mh benchmarks (#804)
## Describe your changes Pallet `thea-message-handler` benchmarks, weights and feature gated runtime integration. ## Checklist before requesting a review - [x] I have performed a self-review of my code. - [x] If it is a core feature, I have added thorough tests. - [x] I removed all Clippy and Formatting Warnings. - [x] I added required Copyrights.
2 parents 176cb27 + 3274ef9 commit 1ff1889

File tree

7 files changed

+295
-8
lines changed

7 files changed

+295
-8
lines changed

Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/thea-message-handler/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ frame-benchmarking = { workspace = true, default-features = false, optional = tr
1616
sp-core = { workspace = true, default-features = false }
1717
bls-primitives = { workspace = true, default-features = false }
1818
thea-primitives = { path = "../../primitives/thea", default-features = false }
19-
19+
lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"], optional = true }
2020

2121
[dev-dependencies]
2222
pallet-assets = { workspace = true, features = ["std"] }
@@ -47,6 +47,7 @@ runtime-benchmarks = [
4747
"frame-benchmarking/runtime-benchmarks",
4848
"frame-support/runtime-benchmarks",
4949
"frame-system/runtime-benchmarks",
50+
"lazy_static",
5051
]
5152
try-runtime = ["frame-support/try-runtime"]
5253
parachain = ["bls-primitives/parachain"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// This file is part of Polkadex.
2+
//
3+
// Copyright (c) 2022-2023 Polkadex oü.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
//! Benchmarking setup for pallet thea-message-handler
20+
#![cfg(feature = "runtime-benchmarks")]
21+
22+
use super::*;
23+
use frame_benchmarking::benchmarks;
24+
use frame_support::BoundedVec;
25+
use frame_system::RawOrigin;
26+
use parity_scale_codec::Decode;
27+
28+
// Check if last event generated by pallet is the one we're expecting
29+
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
30+
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
31+
}
32+
33+
fn produce_authorities<T: Config>(
34+
) -> BoundedVec<<T as crate::Config>::TheaId, <T as crate::Config>::MaxAuthorities> {
35+
let mut authorities: BoundedVec<
36+
<T as crate::Config>::TheaId,
37+
<T as crate::Config>::MaxAuthorities,
38+
> = BoundedVec::with_bounded_capacity(1);
39+
let key = <T as crate::Config>::TheaId::decode(&mut PK.as_ref()).unwrap();
40+
authorities.try_push(key).unwrap();
41+
authorities
42+
}
43+
44+
lazy_static::lazy_static! {
45+
static ref M: Message = Message {
46+
block_no: u64::MAX,
47+
nonce: 1,
48+
data: [255u8; 576].into(), //10 MB
49+
network: 0u8,
50+
is_key_change: false,
51+
validator_set_id: 0,
52+
validator_set_len: 1,
53+
};
54+
}
55+
56+
const SIG: [u8; 48] = [
57+
149, 78, 11, 39, 209, 149, 209, 101, 74, 132, 154, 96, 46, 218, 114, 207, 95, 52, 40, 70, 44,
58+
13, 7, 236, 224, 87, 192, 58, 99, 125, 175, 25, 35, 186, 6, 53, 246, 152, 164, 191, 169, 212,
59+
133, 30, 143, 196, 55, 214,
60+
];
61+
62+
const PK: [u8; 96] = [
63+
128, 68, 92, 111, 149, 140, 246, 244, 137, 50, 23, 217, 197, 153, 235, 255, 228, 58, 108, 191,
64+
41, 203, 237, 112, 203, 173, 118, 41, 92, 3, 165, 18, 200, 173, 125, 232, 182, 162, 9, 122, 13,
65+
77, 41, 222, 92, 53, 60, 0, 22, 227, 136, 163, 35, 121, 27, 34, 208, 233, 191, 74, 36, 223, 17,
66+
34, 79, 35, 164, 208, 138, 207, 171, 53, 254, 213, 17, 141, 35, 196, 81, 247, 20, 171, 33, 187,
67+
152, 79, 229, 3, 121, 17, 242, 252, 147, 209, 50, 186,
68+
];
69+
70+
benchmarks! {
71+
insert_authorities {
72+
let b in 0 .. u32::MAX;
73+
let authorities = produce_authorities::<T>();
74+
let b = b as u64;
75+
}: _(RawOrigin::Root, authorities.clone(), b)
76+
verify {
77+
assert_eq!(<Authorities<T>>::get(b), authorities);
78+
assert_eq!(<ValidatorSetId<T>>::get(), b);
79+
}
80+
81+
incoming_message {
82+
let bitmap = vec!(u128::MAX);
83+
let authorities = produce_authorities::<T>();
84+
<ValidatorSetId<T>>::put(0);
85+
}: _(RawOrigin::None, bitmap, M.clone(), <T as crate::Config>::Signature::decode(&mut SIG.as_ref()).unwrap())
86+
verify {
87+
assert_last_event::<T>(Event::TheaMessageExecuted { message: M.clone() }.into());
88+
assert_eq!(1, <IncomingNonce<T>>::get());
89+
assert_eq!(M.clone(), <IncomingMessages<T>>::get(1).unwrap());
90+
}
91+
92+
update_incoming_nonce {
93+
let b in 1 .. u32::MAX;
94+
let b = b as u64;
95+
}: _(RawOrigin::Root, b)
96+
verify {
97+
assert_eq!(b, <IncomingNonce<T>>::get());
98+
}
99+
}
100+
101+
//#[cfg(test)]
102+
//use frame_benchmarking::impl_benchmark_test_suite;
103+
//
104+
//#[cfg(test)]
105+
//impl_benchmark_test_suite!(TheaMH, crate::mock::new_test_ext(), crate::mock::Test);

pallets/thea-message-handler/src/lib.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,27 @@
2727
2828
use frame_support::{pallet_prelude::*, traits::Get, BoundedVec, Parameter};
2929
use frame_system::pallet_prelude::*;
30+
pub use pallet::*;
3031
use parity_scale_codec::{Encode, MaxEncodedLen};
32+
use polkadex_primitives::utils::return_set_bits;
3133
use sp_runtime::{
3234
traits::{BlockNumberProvider, Member},
3335
transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
3436
RuntimeAppPublic, SaturatedConversion,
3537
};
3638
use sp_std::prelude::*;
37-
38-
pub use pallet::*;
39-
use polkadex_primitives::utils::return_set_bits;
4039
use thea_primitives::{types::Message, Network, ValidatorSet};
4140

41+
#[cfg(feature = "runtime-benchmarks")]
42+
mod benchmarking;
43+
44+
pub trait WeightInfo {
45+
fn insert_authorities(_b: u32) -> Weight;
46+
fn incoming_message() -> Weight;
47+
fn update_incoming_nonce(_b: u32) -> Weight;
48+
}
49+
pub mod weights;
50+
4251
#[frame_support::pallet]
4352
pub mod pallet {
4453
use super::*;
@@ -69,6 +78,9 @@ pub mod pallet {
6978

7079
/// Something that executes the payload
7180
type Executor: thea_primitives::TheaIncomingExecutor;
81+
82+
/// Type representing the weight of this pallet
83+
type WeightInfo: WeightInfo;
7284
}
7385

7486
#[pallet::pallet]
@@ -157,7 +169,7 @@ pub mod pallet {
157169
impl<T: Config> Pallet<T> {
158170
/// Inserts a new authority set using sudo
159171
#[pallet::call_index(0)]
160-
#[pallet::weight(Weight::default())]
172+
#[pallet::weight(<T as Config>::WeightInfo::insert_authorities(1))]
161173
#[transactional]
162174
pub fn insert_authorities(
163175
origin: OriginFor<T>,
@@ -172,7 +184,7 @@ pub mod pallet {
172184

173185
/// Handles the verified incoming message
174186
#[pallet::call_index(1)]
175-
#[pallet::weight(Weight::default())]
187+
#[pallet::weight(<T as Config>::WeightInfo::incoming_message())]
176188
#[transactional]
177189
pub fn incoming_message(
178190
origin: OriginFor<T>,
@@ -220,7 +232,7 @@ pub mod pallet {
220232

221233
/// A governance endpoint to update last processed nonce
222234
#[pallet::call_index(2)]
223-
#[pallet::weight(Weight::default())]
235+
#[pallet::weight(<T as Config>::WeightInfo::update_incoming_nonce(1))]
224236
#[transactional]
225237
pub fn update_incoming_nonce(origin: OriginFor<T>, nonce: u64) -> DispatchResult {
226238
ensure_root(origin)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// This file is part of Polkadex.
2+
//
3+
// Copyright (c) 2023 Polkadex oü.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
//! Autogenerated weights for `thea_message_handler`
20+
//!
21+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
22+
//! DATE: 2023-06-15, STEPS: `100`, REPEAT: `200`, LOW RANGE: `[]`, HIGH RANGE: `[]`
23+
//! WORST CASE MAP SIZE: `1000000`
24+
//! HOSTNAME: `Ubuntu-2204-jammy-amd64-base`, CPU: `Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz`
25+
//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
26+
27+
// Executed Command:
28+
// ./polkadex-node
29+
// benchmark
30+
// pallet
31+
// --pallet
32+
// thea-message-handler
33+
// --steps
34+
// 100
35+
// --repeat
36+
// 200
37+
// --extrinsic
38+
// *
39+
// --output
40+
// liquidity_weights.rs
41+
42+
#![cfg_attr(rustfmt, rustfmt_skip)]
43+
#![allow(unused_parens)]
44+
#![allow(unused_imports)]
45+
#![allow(missing_docs)]
46+
47+
use frame_support::{traits::Get, weights::Weight};
48+
use core::marker::PhantomData;
49+
50+
/// Weight functions for `thea_message_handler`.
51+
pub struct WeightInfo<T>(PhantomData<T>);
52+
impl<T: frame_system::Config> crate::WeightInfo for WeightInfo<T> {
53+
/// Storage: TheaMH Authorities (r:0 w:1)
54+
/// Proof Skipped: TheaMH Authorities (max_values: None, max_size: None, mode: Measured)
55+
/// Storage: TheaMH ValidatorSetId (r:0 w:1)
56+
/// Proof Skipped: TheaMH ValidatorSetId (max_values: Some(1), max_size: None, mode: Measured)
57+
/// The range of component `b` is `[0, 4294967295]`.
58+
fn insert_authorities(_b: u32, ) -> Weight {
59+
// Proof Size summary in bytes:
60+
// Measured: `0`
61+
// Estimated: `0`
62+
// Minimum execution time: 4_557_000 picoseconds.
63+
Weight::from_parts(5_048_603, 0)
64+
.saturating_add(Weight::from_parts(0, 0))
65+
.saturating_add(T::DbWeight::get().writes(2))
66+
}
67+
/// Storage: TheaMH IncomingNonce (r:1 w:1)
68+
/// Proof Skipped: TheaMH IncomingNonce (max_values: Some(1), max_size: None, mode: Measured)
69+
/// Storage: TheaMH ValidatorSetId (r:1 w:0)
70+
/// Proof Skipped: TheaMH ValidatorSetId (max_values: Some(1), max_size: None, mode: Measured)
71+
/// Storage: TheaMH IncomingMessages (r:0 w:1)
72+
/// Proof Skipped: TheaMH IncomingMessages (max_values: None, max_size: None, mode: Measured)
73+
fn incoming_message() -> Weight {
74+
// Proof Size summary in bytes:
75+
// Measured: `108`
76+
// Estimated: `1593`
77+
// Minimum execution time: 28_319_000 picoseconds.
78+
Weight::from_parts(36_715_000, 0)
79+
.saturating_add(Weight::from_parts(0, 1593))
80+
.saturating_add(T::DbWeight::get().reads(2))
81+
.saturating_add(T::DbWeight::get().writes(2))
82+
}
83+
/// Storage: TheaMH IncomingNonce (r:1 w:1)
84+
/// Proof Skipped: TheaMH IncomingNonce (max_values: Some(1), max_size: None, mode: Measured)
85+
/// The range of component `b` is `[1, 4294967295]`.
86+
fn update_incoming_nonce(_b: u32, ) -> Weight {
87+
// Proof Size summary in bytes:
88+
// Measured: `76`
89+
// Estimated: `1561`
90+
// Minimum execution time: 5_647_000 picoseconds.
91+
Weight::from_parts(6_126_151, 0)
92+
.saturating_add(Weight::from_parts(0, 1561))
93+
.saturating_add(T::DbWeight::get().reads(1))
94+
.saturating_add(T::DbWeight::get().writes(1))
95+
}
96+
}

runtime/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pallet-ocex-lmp = { path = "../pallets/ocex", default-features = false }
9999
# Thea
100100
thea = { path = "../pallets/thea", default-features = false }
101101
thea-executor = { path = "../pallets/thea-executor", default-features = false }
102+
thea-message-handler = { path = "../pallets/thea-message-handler", default-features = false, optional = true }
102103

103104
# Liquidity Pallet
104105
liquidity = { path = "../pallets/liquidity", default-features = false }
@@ -184,6 +185,7 @@ std = [
184185
"thea-executor/std",
185186
"thea-primitives/std",
186187
"frame-try-runtime?/std",
188+
"thea-message-handler?/std",
187189
]
188190
runtime-benchmarks = [
189191
#theirs
@@ -201,6 +203,7 @@ runtime-benchmarks = [
201203
"pallet-rewards/runtime-benchmarks",
202204
"liquidity/runtime-benchmarks",
203205
"thea/runtime-benchmarks",
206+
"thea-message-handler/runtime-benchmarks",
204207
]
205208

206209
try-runtime = [

0 commit comments

Comments
 (0)