referrerpolicy=no-referrer-when-downgrade

pallet_collective_content/
benchmarking.rs

1// Copyright (C) 2023 Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! The pallet benchmarks.
17
18use super::{Pallet as CollectiveContent, *};
19use frame_benchmarking::v2::*;
20use frame_support::traits::EnsureOrigin;
21
22fn assert_last_event<T: Config<I>, I: 'static>(generic_event: <T as Config<I>>::RuntimeEvent) {
23	frame_system::Pallet::<T>::assert_last_event(generic_event.into());
24}
25
26/// returns CID hash of 68 bytes of given `i`.
27fn create_cid(i: u8) -> OpaqueCid {
28	let cid: OpaqueCid = [i; 68].to_vec().try_into().unwrap();
29	cid
30}
31
32#[instance_benchmarks]
33mod benchmarks {
34	use super::*;
35
36	#[benchmark]
37	fn set_charter() -> Result<(), BenchmarkError> {
38		let cid: OpaqueCid = create_cid(1);
39		let origin =
40			T::CharterOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
41
42		#[extrinsic_call]
43		_(origin as T::RuntimeOrigin, cid.clone());
44
45		assert_eq!(Charter::<T, I>::get(), Some(cid.clone()));
46		assert_last_event::<T, I>(Event::NewCharterSet { cid }.into());
47		Ok(())
48	}
49
50	#[benchmark]
51	fn announce() -> Result<(), BenchmarkError> {
52		let expire_at = DispatchTime::<_>::At(10u32.into());
53		let now = frame_system::Pallet::<T>::block_number();
54		let cid: OpaqueCid = create_cid(1);
55		let origin = T::AnnouncementOrigin::try_successful_origin()
56			.map_err(|_| BenchmarkError::Weightless)?;
57
58		#[extrinsic_call]
59		_(origin as T::RuntimeOrigin, cid.clone(), Some(expire_at));
60
61		assert_eq!(<Announcements<T, I>>::count(), 1);
62		assert_last_event::<T, I>(
63			Event::AnnouncementAnnounced { cid, expire_at: expire_at.evaluate(now) }.into(),
64		);
65
66		Ok(())
67	}
68
69	#[benchmark]
70	fn remove_announcement() -> Result<(), BenchmarkError> {
71		let cid: OpaqueCid = create_cid(1);
72		let origin = T::AnnouncementOrigin::try_successful_origin()
73			.map_err(|_| BenchmarkError::Weightless)?;
74		CollectiveContent::<T, I>::announce(origin.clone(), cid.clone(), None)
75			.expect("could not publish an announcement");
76		assert_eq!(<Announcements<T, I>>::count(), 1);
77
78		#[extrinsic_call]
79		_(origin as T::RuntimeOrigin, cid.clone());
80
81		assert_eq!(<Announcements<T, I>>::count(), 0);
82		assert_last_event::<T, I>(Event::AnnouncementRemoved { cid }.into());
83
84		Ok(())
85	}
86
87	impl_benchmark_test_suite!(CollectiveContent, super::mock::new_bench_ext(), super::mock::Test);
88}