referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_parachain_system/
benchmarking.rs

1// This file is part of Cumulus.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Benchmarking for the parachain-system pallet.
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use super::*;
23use crate::parachain_inherent::InboundDownwardMessages;
24use cumulus_primitives_core::{relay_chain::Hash as RelayHash, InboundDownwardMessage};
25use frame_benchmarking::v2::*;
26use sp_runtime::traits::BlakeTwo256;
27
28#[benchmarks]
29mod benchmarks {
30	use super::*;
31
32	/// Enqueue `n` messages via `enqueue_inbound_downward_messages`.
33	///
34	/// The limit is set to `1000` for benchmarking purposes as the actual limit is only known at
35	/// runtime. However, the limit (and default) for Dotsama are magnitudes smaller.
36	#[benchmark]
37	fn enqueue_inbound_downward_messages(n: Linear<0, 1000>) {
38		let msg = InboundDownwardMessage {
39			sent_at: n, // The block number does not matter.
40			msg: vec![0u8; MaxDmpMessageLenOf::<T>::get() as usize],
41		};
42		let msgs = vec![msg; n as usize];
43		let head = mqp_head(&msgs);
44
45		#[block]
46		{
47			Pallet::<T>::enqueue_inbound_downward_messages(
48				head,
49				InboundDownwardMessages::new(msgs).into_abridged(&mut usize::MAX.clone()),
50			);
51		}
52
53		assert_eq!(ProcessedDownwardMessages::<T>::get(), n);
54		assert_eq!(LastDmqMqcHead::<T>::get().head(), head);
55	}
56
57	/// Re-implements an easy version of the `MessageQueueChain` for testing purposes.
58	fn mqp_head(msgs: &Vec<InboundDownwardMessage>) -> RelayHash {
59		let mut head = Default::default();
60		for msg in msgs.iter() {
61			let msg_hash = BlakeTwo256::hash_of(&msg.msg);
62			head = BlakeTwo256::hash_of(&(head, msg.sent_at, msg_hash));
63		}
64		head
65	}
66
67	impl_benchmark_test_suite! {
68		Pallet,
69		crate::mock::new_test_ext(),
70		crate::mock::Test
71	}
72}