1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// 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.
1718//! # WARNING
19//!
20//! **DO NOT USE ON VALUE-BEARING CHAINS. THIS PALLET IS ONLY INTENDED FOR TESTING USAGE.**
21//!
22//! # Glutton Pallet
23//!
24//! Pallet that consumes `ref_time` and `proof_size` of a block. Based on the `Compute` and
25//! `Storage` parameters the pallet consumes the adequate amount of weight.
2627#![deny(missing_docs)]
28#![cfg_attr(not(feature = "std"), no_std)]
2930#[cfg(feature = "runtime-benchmarks")]
31mod benchmarking;
32#[cfg(test)]
33mod mock;
34#[cfg(test)]
35mod tests;
36pub mod weights;
3738extern crate alloc;
3940use alloc::{vec, vec::Vec};
41use blake2::{Blake2b512, Digest};
42use frame_support::{pallet_prelude::*, weights::WeightMeter, DefaultNoBound};
43use frame_system::pallet_prelude::*;
44use sp_io::hashing::twox_256;
45use sp_runtime::{traits::Zero, FixedPointNumber, FixedU64};
4647pub use pallet::*;
48pub use weights::WeightInfo;
4950/// The size of each value in the `TrashData` storage in bytes.
51pub const VALUE_SIZE: usize = 1024;
52/// Max number of entries for the `TrashData` map.
53pub const MAX_TRASH_DATA_ENTRIES: u32 = 65_000;
54/// Hard limit for any other resource limit (in units).
55pub const RESOURCE_HARD_LIMIT: FixedU64 = FixedU64::from_u32(10);
5657#[frame_support::pallet]
58pub mod pallet {
59use super::*;
6061#[pallet::config]
62pub trait Config: frame_system::Config {
63/// The overarching event type.
64#[allow(deprecated)]
65type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
6667/// The admin origin that can set computational limits and initialize the pallet.
68type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
6970/// Weight information for this pallet.
71type WeightInfo: WeightInfo;
72 }
7374#[pallet::pallet]
75pub struct Pallet<T>(_);
7677#[pallet::event]
78 #[pallet::generate_deposit(pub(super) fn deposit_event)]
79pub enum Event {
80/// The pallet has been (re)initialized.
81PalletInitialized {
82/// Whether the pallet has been re-initialized.
83reinit: bool,
84 },
85/// The computation limit has been updated.
86ComputationLimitSet {
87/// The computation limit.
88compute: FixedU64,
89 },
90/// The storage limit has been updated.
91StorageLimitSet {
92/// The storage limit.
93storage: FixedU64,
94 },
95/// The block length limit has been updated.
96BlockLengthLimitSet {
97/// The block length limit.
98block_length: FixedU64,
99 },
100 }
101102#[pallet::error]
103pub enum Error<T> {
104/// The pallet was already initialized.
105 ///
106 /// Set `witness_count` to `Some` to bypass this error.
107AlreadyInitialized,
108109/// The limit was over [`crate::RESOURCE_HARD_LIMIT`].
110InsaneLimit,
111 }
112113/// The proportion of the remaining `ref_time` to consume during `on_idle`.
114 ///
115 /// `1.0` is mapped to `100%`. Must be at most [`crate::RESOURCE_HARD_LIMIT`]. Setting this to
116 /// over `1.0` could stall the chain.
117#[pallet::storage]
118pub(crate) type Compute<T: Config> = StorageValue<_, FixedU64, ValueQuery>;
119120/// The proportion of the remaining `proof_size` to consume during `on_idle`.
121 ///
122 /// `1.0` is mapped to `100%`. Must be at most [`crate::RESOURCE_HARD_LIMIT`]. Setting this to
123 /// over `1.0` could stall the chain.
124#[pallet::storage]
125pub(crate) type Storage<T: Config> = StorageValue<_, FixedU64, ValueQuery>;
126127/// The proportion of the `block length` to consume on each block.
128 ///
129 /// `1.0` is mapped to `100%`. Must be at most [`crate::RESOURCE_HARD_LIMIT`]. Setting this to
130 /// over `1.0` could stall the chain.
131#[pallet::storage]
132pub(crate) type Length<T: Config> = StorageValue<_, FixedU64, ValueQuery>;
133134/// Storage map used for wasting proof size.
135 ///
136 /// It contains no meaningful data - hence the name "Trash". The maximal number of entries is
137 /// set to 65k, which is just below the next jump at 16^4. This is important to reduce the proof
138 /// size benchmarking overestimate. The assumption here is that we won't have more than 65k *
139 /// 1KiB = 65MiB of proof size wasting in practice. However, this limit is not enforced, so the
140 /// pallet would also work out of the box with more entries, but its benchmarked proof weight
141 /// would possibly be underestimated in that case.
142#[pallet::storage]
143pub(super) type TrashData<T: Config> = StorageMap<
144 Hasher = Twox64Concat,
145 Key = u32,
146 Value = [u8; VALUE_SIZE],
147 QueryKind = OptionQuery,
148 MaxValues = ConstU32<MAX_TRASH_DATA_ENTRIES>,
149 >;
150151/// The current number of entries in `TrashData`.
152#[pallet::storage]
153pub(crate) type TrashDataCount<T: Config> = StorageValue<_, u32, ValueQuery>;
154155#[pallet::genesis_config]
156 #[derive(DefaultNoBound)]
157pub struct GenesisConfig<T: Config> {
158/// The compute limit.
159pub compute: FixedU64,
160/// The storage limit.
161pub storage: FixedU64,
162/// The amount of trash data for wasting proof size.
163pub trash_data_count: u32,
164/// The block length limit.
165pub block_length: FixedU64,
166#[serde(skip)]
167/// The required configuration field.
168pub _config: core::marker::PhantomData<T>,
169 }
170171#[pallet::genesis_build]
172impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
173fn build(&self) {
174assert!(
175self.trash_data_count <= MAX_TRASH_DATA_ENTRIES,
176"number of TrashData entries cannot be bigger than {:?}",
177 MAX_TRASH_DATA_ENTRIES
178 );
179180 (0..self.trash_data_count)
181 .for_each(|i| TrashData::<T>::insert(i, Pallet::<T>::gen_value(i)));
182183 TrashDataCount::<T>::set(self.trash_data_count);
184185assert!(self.compute <= RESOURCE_HARD_LIMIT, "Compute limit is insane");
186 <Compute<T>>::put(self.compute);
187188assert!(self.storage <= RESOURCE_HARD_LIMIT, "Storage limit is insane");
189 <Storage<T>>::put(self.storage);
190191assert!(self.block_length <= RESOURCE_HARD_LIMIT, "Block length limit is insane");
192 <Length<T>>::put(self.block_length);
193 }
194 }
195196#[pallet::hooks]
197impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
198fn integrity_test() {
199assert!(
200 !T::WeightInfo::waste_ref_time_iter(1).ref_time().is_zero(),
201"Weight zero; would get stuck in an infinite loop"
202);
203assert!(
204 !T::WeightInfo::waste_proof_size_some(1).proof_size().is_zero(),
205"Weight zero; would get stuck in an infinite loop"
206);
207 }
208209fn on_idle(_: BlockNumberFor<T>, remaining_weight: Weight) -> Weight {
210let mut meter = WeightMeter::with_limit(remaining_weight);
211if meter.try_consume(T::WeightInfo::empty_on_idle()).is_err() {
212return T::WeightInfo::empty_on_idle()
213 }
214215let proof_size_limit =
216 Storage::<T>::get().saturating_mul_int(meter.remaining().proof_size());
217let computation_weight_limit =
218 Compute::<T>::get().saturating_mul_int(meter.remaining().ref_time());
219let mut meter = WeightMeter::with_limit(Weight::from_parts(
220 computation_weight_limit,
221 proof_size_limit,
222 ));
223224Self::waste_at_most_proof_size(&mut meter);
225Self::waste_at_most_ref_time(&mut meter);
226227 meter.consumed()
228 }
229 }
230231#[pallet::inherent]
232impl<T: Config> ProvideInherent for Pallet<T> {
233type Call = Call<T>;
234type Error = sp_inherents::MakeFatalError<()>;
235236const INHERENT_IDENTIFIER: InherentIdentifier = *b"bloated0";
237238fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
239let max_block_length = *T::BlockLength::get().max.get(DispatchClass::Mandatory);
240let bloat_size = Length::<T>::get().saturating_mul_int(max_block_length) as usize;
241let amount_trash = bloat_size / VALUE_SIZE;
242let garbage = TrashData::<T>::iter()
243 .map(|(_k, v)| v)
244 .collect::<Vec<_>>()
245 .into_iter()
246 .cycle()
247 .take(amount_trash)
248 .collect::<Vec<_>>();
249250Some(Call::bloat { garbage })
251 }
252253fn is_inherent(call: &Self::Call) -> bool {
254matches!(call, Call::bloat { .. })
255 }
256257fn check_inherent(call: &Self::Call, _: &InherentData) -> Result<(), Self::Error> {
258match call {
259 Call::bloat { .. } => Ok(()),
260_ => unreachable!("other calls are not inherents"),
261 }
262 }
263 }
264265#[pallet::call(weight = T::WeightInfo)]
266impl<T: Config> Pallet<T> {
267/// Initialize the pallet. Should be called once, if no genesis state was provided.
268 ///
269 /// `current_count` is the current number of elements in `TrashData`. This can be set to
270 /// `None` when the pallet is first initialized.
271 ///
272 /// Only callable by Root or `AdminOrigin`. A good default for `new_count` is `5_000`.
273#[pallet::call_index(0)]
274 #[pallet::weight(
275 T::WeightInfo::initialize_pallet_grow(witness_count.unwrap_or_default())
276 .max(T::WeightInfo::initialize_pallet_shrink(witness_count.unwrap_or_default()))
277 )]
278pub fn initialize_pallet(
279 origin: OriginFor<T>,
280 new_count: u32,
281 witness_count: Option<u32>,
282 ) -> DispatchResult {
283 T::AdminOrigin::ensure_origin_or_root(origin)?;
284285let current_count = TrashDataCount::<T>::get();
286ensure!(
287 current_count == witness_count.unwrap_or_default(),
288 Error::<T>::AlreadyInitialized
289 );
290291if new_count > current_count {
292 (current_count..new_count)
293 .for_each(|i| TrashData::<T>::insert(i, Self::gen_value(i)));
294 } else {
295 (new_count..current_count).for_each(TrashData::<T>::remove);
296 }
297298Self::deposit_event(Event::PalletInitialized { reinit: witness_count.is_some() });
299 TrashDataCount::<T>::set(new_count);
300Ok(())
301 }
302303/// Set how much of the remaining `ref_time` weight should be consumed by `on_idle`.
304 ///
305 /// Only callable by Root or `AdminOrigin`.
306#[pallet::call_index(1)]
307pub fn set_compute(origin: OriginFor<T>, compute: FixedU64) -> DispatchResult {
308 T::AdminOrigin::ensure_origin_or_root(origin)?;
309310ensure!(compute <= RESOURCE_HARD_LIMIT, Error::<T>::InsaneLimit);
311 Compute::<T>::set(compute);
312313Self::deposit_event(Event::ComputationLimitSet { compute });
314Ok(())
315 }
316317/// Set how much of the remaining `proof_size` weight should be consumed by `on_idle`.
318 ///
319 /// `1.0` means that all remaining `proof_size` will be consumed. The PoV benchmarking
320 /// results that are used here are likely an over-estimation. 100% intended consumption will
321 /// therefore translate to less than 100% actual consumption.
322 ///
323 /// Only callable by Root or `AdminOrigin`.
324#[pallet::call_index(2)]
325pub fn set_storage(origin: OriginFor<T>, storage: FixedU64) -> DispatchResult {
326 T::AdminOrigin::ensure_origin_or_root(origin)?;
327328ensure!(storage <= RESOURCE_HARD_LIMIT, Error::<T>::InsaneLimit);
329 Storage::<T>::set(storage);
330331Self::deposit_event(Event::StorageLimitSet { storage });
332Ok(())
333 }
334335/// Increase the block size by including the specified garbage bytes.
336#[pallet::call_index(3)]
337 #[pallet::weight((0, DispatchClass::Mandatory))]
338pub fn bloat(_origin: OriginFor<T>, _garbage: Vec<[u8; VALUE_SIZE]>) -> DispatchResult {
339Ok(())
340 }
341342/// Set how much of the block length should be filled with trash data on each block.
343 ///
344 /// `1.0` means that all block should be filled. If set to `1.0`, storage proof size will
345 /// be close to zero.
346 ///
347 /// Only callable by Root or `AdminOrigin`.
348#[pallet::call_index(4)]
349 #[pallet::weight({1})]
350pub fn set_block_length(origin: OriginFor<T>, block_length: FixedU64) -> DispatchResult {
351 T::AdminOrigin::ensure_origin_or_root(origin)?;
352353ensure!(block_length <= RESOURCE_HARD_LIMIT, Error::<T>::InsaneLimit);
354 Length::<T>::set(block_length);
355356Self::deposit_event(Event::BlockLengthLimitSet { block_length });
357Ok(())
358 }
359 }
360361impl<T: Config> Pallet<T> {
362/// Waste at most the remaining proof size of `meter`.
363 ///
364 /// Tries to come as close to the limit as possible.
365pub(crate) fn waste_at_most_proof_size(meter: &mut WeightMeter) {
366let Ok(n) = Self::calculate_proof_size_iters(&meter) else { return };
367368 meter.consume(T::WeightInfo::waste_proof_size_some(n));
369370 (0..n).for_each(|i| {
371 TrashData::<T>::get(i);
372 });
373 }
374375/// Calculate how many times `waste_proof_size_some` should be called to fill up `meter`.
376fn calculate_proof_size_iters(meter: &WeightMeter) -> Result<u32, ()> {
377let base = T::WeightInfo::waste_proof_size_some(0);
378let slope = T::WeightInfo::waste_proof_size_some(1).saturating_sub(base);
379380let remaining = meter.remaining().saturating_sub(base);
381let iter_by_proof_size =
382 remaining.proof_size().checked_div(slope.proof_size()).ok_or(())?;
383let iter_by_ref_time = remaining.ref_time().checked_div(slope.ref_time()).ok_or(())?;
384385if iter_by_proof_size > 0 && iter_by_proof_size <= iter_by_ref_time {
386Ok(iter_by_proof_size as u32)
387 } else {
388Err(())
389 }
390 }
391392/// Waste at most the remaining ref time weight of `meter`.
393 ///
394 /// Tries to come as close to the limit as possible.
395pub(crate) fn waste_at_most_ref_time(meter: &mut WeightMeter) {
396let Ok(n) = Self::calculate_ref_time_iters(&meter) else { return };
397 meter.consume(T::WeightInfo::waste_ref_time_iter(n));
398399let clobber = Self::waste_ref_time_iter(vec![0u8; 64], n);
400401// By casting it into a vec we can hopefully prevent the compiler from optimizing it
402 // out. Note that `Blake2b512` produces 64 bytes, this is therefore impossible - but the
403 // compiler does not know that (hopefully).
404debug_assert!(clobber.len() == 64);
405if clobber.len() == 65 {
406 TrashData::<T>::insert(0, [clobber[0] as u8; VALUE_SIZE]);
407 }
408 }
409410/// Wastes some `ref_time`. Receives the previous result as an argument.
411 ///
412 /// The ref_time of one iteration should be in the order of 1-10 ms.
413pub(crate) fn waste_ref_time_iter(clobber: Vec<u8>, i: u32) -> Vec<u8> {
414let mut hasher = Blake2b512::new();
415416// Blake2 has a very high speed of hashing so we make multiple hashes with it to
417 // waste more `ref_time` at once.
418(0..i).for_each(|_| {
419 hasher.update(clobber.as_slice());
420 });
421422 hasher.finalize().to_vec()
423 }
424425/// Calculate how many times `waste_ref_time_iter` should be called to fill up `meter`.
426fn calculate_ref_time_iters(meter: &WeightMeter) -> Result<u32, ()> {
427let base = T::WeightInfo::waste_ref_time_iter(0);
428let slope = T::WeightInfo::waste_ref_time_iter(1).saturating_sub(base);
429if !slope.proof_size().is_zero() || !base.proof_size().is_zero() {
430return Err(())
431 }
432433match meter
434 .remaining()
435 .ref_time()
436 .saturating_sub(base.ref_time())
437 .checked_div(slope.ref_time())
438 {
439Some(0) | None => Err(()),
440Some(i) => Ok(i as u32),
441 }
442 }
443444/// Generate a pseudo-random deterministic value from a `seed`.
445pub(crate) fn gen_value(seed: u32) -> [u8; VALUE_SIZE] {
446let mut ret = [0u8; VALUE_SIZE];
447448for i in 0u32..(VALUE_SIZE as u32 / 32) {
449let hash = (seed, i).using_encoded(twox_256);
450 ret[i as usize * 32..(i + 1) as usize * 32].copy_from_slice(&hash);
451 }
452453 ret
454 }
455 }
456}