referrerpolicy=no-referrer-when-downgrade

frame_system_benchmarking/
inner.rs

1// This file is part of Substrate.
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//! Frame System benchmarks.
19
20use alloc::{vec, vec::Vec};
21use codec::Encode;
22use frame_benchmarking::v2::*;
23use frame_support::{dispatch::DispatchClass, storage, traits::Get};
24use frame_system::{Call, Pallet as System, RawOrigin};
25use sp_core::storage::well_known_keys;
26use sp_runtime::traits::Hash;
27
28pub struct Pallet<T: Config>(System<T>);
29pub trait Config: frame_system::Config {
30	/// Adds ability to the Runtime to test against their sample code.
31	///
32	/// Default is `../res/kitchensink_runtime.compact.compressed.wasm`.
33	fn prepare_set_code_data() -> Vec<u8> {
34		include_bytes!("../res/kitchensink_runtime.compact.compressed.wasm").to_vec()
35	}
36
37	/// Adds ability to the Runtime to prepare/initialize before running benchmark `set_code`.
38	fn setup_set_code_requirements(_code: &Vec<u8>) -> Result<(), BenchmarkError> {
39		Ok(())
40	}
41
42	/// Adds ability to the Runtime to do custom validation after benchmark.
43	///
44	/// Default is checking for `CodeUpdated` event .
45	fn verify_set_code() {
46		System::<Self>::assert_last_event(frame_system::Event::<Self>::CodeUpdated.into());
47	}
48}
49
50#[benchmarks]
51mod benchmarks {
52	use super::*;
53
54	#[benchmark]
55	fn remark(
56		b: Linear<0, { *T::BlockLength::get().max.get(DispatchClass::Normal) as u32 }>,
57	) -> Result<(), BenchmarkError> {
58		let remark_message = vec![1; b as usize];
59		let caller = whitelisted_caller();
60
61		#[extrinsic_call]
62		remark(RawOrigin::Signed(caller), remark_message);
63
64		Ok(())
65	}
66
67	#[benchmark]
68	fn remark_with_event(
69		b: Linear<0, { *T::BlockLength::get().max.get(DispatchClass::Normal) as u32 }>,
70	) -> Result<(), BenchmarkError> {
71		let remark_message = vec![1; b as usize];
72		let caller: T::AccountId = whitelisted_caller();
73		let hash = T::Hashing::hash(&remark_message[..]);
74
75		#[extrinsic_call]
76		remark_with_event(RawOrigin::Signed(caller.clone()), remark_message);
77
78		System::<T>::assert_last_event(
79			frame_system::Event::<T>::Remarked { sender: caller, hash }.into(),
80		);
81		Ok(())
82	}
83
84	#[benchmark]
85	fn set_heap_pages() -> Result<(), BenchmarkError> {
86		#[extrinsic_call]
87		set_heap_pages(RawOrigin::Root, Default::default());
88
89		Ok(())
90	}
91
92	#[benchmark]
93	fn set_code() -> Result<(), BenchmarkError> {
94		let runtime_blob = T::prepare_set_code_data();
95		T::setup_set_code_requirements(&runtime_blob)?;
96
97		#[extrinsic_call]
98		set_code(RawOrigin::Root, runtime_blob);
99
100		T::verify_set_code();
101		Ok(())
102	}
103
104	#[benchmark(extra)]
105	fn set_code_without_checks() -> Result<(), BenchmarkError> {
106		// Assume Wasm ~4MB
107		let code = vec![1; 4_000_000 as usize];
108		T::setup_set_code_requirements(&code)?;
109
110		#[block]
111		{
112			System::<T>::set_code_without_checks(RawOrigin::Root.into(), code)?;
113		}
114
115		let current_code =
116			storage::unhashed::get_raw(well_known_keys::CODE).ok_or("Code not stored.")?;
117		assert_eq!(current_code.len(), 4_000_000 as usize);
118		Ok(())
119	}
120
121	#[benchmark(skip_meta)]
122	fn set_storage(i: Linear<0, { 1_000 }>) -> Result<(), BenchmarkError> {
123		// Set up i items to add
124		let mut items = Vec::new();
125		for j in 0..i {
126			let hash = (i, j).using_encoded(T::Hashing::hash).as_ref().to_vec();
127			items.push((hash.clone(), hash.clone()));
128		}
129
130		let items_to_verify = items.clone();
131
132		#[extrinsic_call]
133		set_storage(RawOrigin::Root, items);
134
135		// Verify that they're actually in the storage.
136		for (item, _) in items_to_verify {
137			let value = storage::unhashed::get_raw(&item).ok_or("No value stored")?;
138			assert_eq!(value, *item);
139		}
140		Ok(())
141	}
142
143	#[benchmark(skip_meta)]
144	fn kill_storage(i: Linear<0, { 1_000 }>) -> Result<(), BenchmarkError> {
145		// Add i items to storage
146		let mut items = Vec::with_capacity(i as usize);
147		for j in 0..i {
148			let hash = (i, j).using_encoded(T::Hashing::hash).as_ref().to_vec();
149			storage::unhashed::put_raw(&hash, &hash);
150			items.push(hash);
151		}
152
153		// Verify that they're actually in the storage.
154		for item in &items {
155			let value = storage::unhashed::get_raw(item).ok_or("No value stored")?;
156			assert_eq!(value, *item);
157		}
158
159		let items_to_verify = items.clone();
160
161		#[extrinsic_call]
162		kill_storage(RawOrigin::Root, items);
163
164		// Verify that they're not in the storage anymore.
165		for item in items_to_verify {
166			assert!(storage::unhashed::get_raw(&item).is_none());
167		}
168		Ok(())
169	}
170
171	#[benchmark(skip_meta)]
172	fn kill_prefix(p: Linear<0, { 1_000 }>) -> Result<(), BenchmarkError> {
173		let prefix = p.using_encoded(T::Hashing::hash).as_ref().to_vec();
174		let mut items = Vec::with_capacity(p as usize);
175		// add p items that share a prefix
176		for i in 0..p {
177			let hash = (p, i).using_encoded(T::Hashing::hash).as_ref().to_vec();
178			let key = [&prefix[..], &hash[..]].concat();
179			storage::unhashed::put_raw(&key, &key);
180			items.push(key);
181		}
182
183		// Verify that they're actually in the storage.
184		for item in &items {
185			let value = storage::unhashed::get_raw(item).ok_or("No value stored")?;
186			assert_eq!(value, *item);
187		}
188
189		#[extrinsic_call]
190		kill_prefix(RawOrigin::Root, prefix, p);
191
192		// Verify that they're not in the storage anymore.
193		for item in items {
194			assert!(storage::unhashed::get_raw(&item).is_none());
195		}
196		Ok(())
197	}
198
199	#[benchmark]
200	fn authorize_upgrade() -> Result<(), BenchmarkError> {
201		let runtime_blob = T::prepare_set_code_data();
202		T::setup_set_code_requirements(&runtime_blob)?;
203		let hash = T::Hashing::hash(&runtime_blob);
204
205		#[extrinsic_call]
206		authorize_upgrade(RawOrigin::Root, hash);
207
208		assert_eq!(System::<T>::authorized_upgrade().unwrap().code_hash(), &hash);
209		Ok(())
210	}
211
212	#[benchmark]
213	fn apply_authorized_upgrade() -> Result<(), BenchmarkError> {
214		let runtime_blob = T::prepare_set_code_data();
215		T::setup_set_code_requirements(&runtime_blob)?;
216		let hash = T::Hashing::hash(&runtime_blob);
217		// Will be heavier when it needs to do verification (i.e. don't use `...without_checks`).
218		System::<T>::authorize_upgrade(RawOrigin::Root.into(), hash)?;
219
220		#[extrinsic_call]
221		apply_authorized_upgrade(RawOrigin::Root, runtime_blob);
222
223		// Can't check for `CodeUpdated` in parachain upgrades. Just check that the authorization is
224		// gone.
225		assert!(System::<T>::authorized_upgrade().is_none());
226		Ok(())
227	}
228
229	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
230}