Skip to main content

frame_support/traits/
misc.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//! Smaller traits used in FRAME which don't need their own file.
19
20use crate::dispatch::{DispatchResult, Parameter};
21use alloc::{vec, vec::Vec};
22use codec::{CompactLen, Decode, DecodeLimit, Encode, EncodeLike, Input, MaxEncodedLen};
23use impl_trait_for_tuples::impl_for_tuples;
24use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter};
25use sp_arithmetic::traits::{CheckedAdd, CheckedMul, CheckedSub, One, Saturating};
26use sp_core::bounded::bounded_vec::TruncateFrom;
27
28use core::cmp::Ordering;
29#[doc(hidden)]
30pub use sp_runtime::traits::{
31	ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstInt, ConstU128, ConstU16,
32	ConstU32, ConstU64, ConstU8, ConstUint, Get, GetDefault, TryCollect, TypedGet,
33};
34use sp_runtime::{
35	traits::{Block as BlockT, ExtrinsicCall},
36	DispatchError,
37};
38
39#[doc(hidden)]
40pub const DEFENSIVE_OP_PUBLIC_ERROR: &str = "a defensive failure has been triggered; please report the block number at https://github.com/paritytech/polkadot-sdk/issues";
41#[doc(hidden)]
42pub const DEFENSIVE_OP_INTERNAL_ERROR: &str = "Defensive failure has been triggered!";
43
44/// Trait to get the number of variants in any enum.
45pub trait VariantCount {
46	/// Get the number of variants.
47	const VARIANT_COUNT: u32;
48}
49
50impl VariantCount for () {
51	const VARIANT_COUNT: u32 = 0;
52}
53
54impl VariantCount for u8 {
55	const VARIANT_COUNT: u32 = 256;
56}
57
58/// Adapter for `Get<u32>` to access `VARIANT_COUNT` from `trait pub trait VariantCount {`.
59pub struct VariantCountOf<T: VariantCount>(core::marker::PhantomData<T>);
60impl<T: VariantCount> Get<u32> for VariantCountOf<T> {
61	fn get() -> u32 {
62		T::VARIANT_COUNT
63	}
64}
65
66/// Generic function to mark an execution path as ONLY defensive.
67///
68/// Similar to mark a match arm or `if/else` branch as `unreachable!`.
69#[macro_export]
70macro_rules! defensive {
71	() => {
72		$crate::__private::log::error!(
73			target: "runtime::defensive",
74			"{}",
75			$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR
76		);
77		debug_assert!(false, "{}", $crate::traits::DEFENSIVE_OP_INTERNAL_ERROR);
78	};
79	($error:expr $(,)?) => {
80		$crate::__private::log::error!(
81			target: "runtime::defensive",
82			"{}: {:?}",
83			$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR,
84			$error
85		);
86		debug_assert!(false, "{}: {:?}", $crate::traits::DEFENSIVE_OP_INTERNAL_ERROR, $error);
87	};
88	($error:expr, $proof:expr $(,)?) => {
89		$crate::__private::log::error!(
90			target: "runtime::defensive",
91			"{}: {:?}: {:?}",
92			$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR,
93			$error,
94			$proof,
95		);
96		debug_assert!(false, "{}: {:?}: {:?}", $crate::traits::DEFENSIVE_OP_INTERNAL_ERROR, $error, $proof);
97	}
98}
99
100/// Trigger a defensive failure if a condition is not met.
101///
102/// Similar to [`assert!`] but will print an error without `debug_assertions` instead of silently
103/// ignoring it. Only accepts one instead of variable formatting arguments.
104///
105/// # Example
106///
107/// ```should_panic
108/// frame_support::defensive_assert!(1 == 0, "Must fail")
109/// ```
110#[macro_export]
111macro_rules! defensive_assert {
112	($cond:expr $(, $proof:expr )? $(,)?) => {
113		if !($cond) {
114			$crate::defensive!(::core::stringify!($cond) $(, $proof )?);
115		}
116	};
117}
118
119/// Prelude module for all defensive traits to be imported at once.
120pub mod defensive_prelude {
121	pub use super::{Defensive, DefensiveOption, DefensiveResult};
122}
123
124/// A trait to handle errors and options when you are really sure that a condition must hold, but
125/// not brave enough to `expect` on it, or a default fallback value makes more sense.
126///
127/// This trait mostly focuses on methods that eventually unwrap the inner value. See
128/// [`DefensiveResult`] and [`DefensiveOption`] for methods that specifically apply to the
129/// respective types.
130///
131/// Each function in this trait will have two side effects, aside from behaving exactly as the name
132/// would suggest:
133///
134/// 1. It panics on `#[debug_assertions]`, so if the infallible code is reached in any of the tests,
135///    you realize.
136/// 2. It will log an error using the runtime logging system. This might help you detect such bugs
137///    in production as well. Note that the log message, as of now, are not super expressive. Your
138///    best shot of fully diagnosing the error would be to infer the block number of which the log
139///    message was emitted, then re-execute that block using `check-block` or `try-runtime`
140///    subcommands in substrate client.
141pub trait Defensive<T> {
142	/// Exactly the same as `unwrap_or`, but it does the defensive warnings explained in the trait
143	/// docs.
144	fn defensive_unwrap_or(self, other: T) -> T;
145
146	/// Exactly the same as `unwrap_or_else`, but it does the defensive warnings explained in the
147	/// trait docs.
148	fn defensive_unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T;
149
150	/// Exactly the same as `unwrap_or_default`, but it does the defensive warnings explained in the
151	/// trait docs.
152	fn defensive_unwrap_or_default(self) -> T
153	where
154		T: Default;
155
156	/// Does not alter the inner value at all, but it will log warnings if the inner value is `None`
157	/// or `Err`.
158	///
159	/// In some ways, this is like  `.defensive_map(|x| x)`.
160	///
161	/// This is useful as:
162	/// ```nocompile
163	/// if let Some(inner) = maybe_value().defensive() {
164	/// 	 	..
165	/// }
166	/// ```
167	fn defensive(self) -> Self;
168
169	/// Same as [`Defensive::defensive`], but it takes a proof as input, and displays it if the
170	/// defensive operation has been triggered.
171	fn defensive_proof(self, proof: &'static str) -> Self;
172}
173
174/// Subset of methods similar to [`Defensive`] that can only work for a `Result`.
175pub trait DefensiveResult<T, E> {
176	/// Defensively map the error into another return type, but you are really sure that this
177	/// conversion should never be needed.
178	fn defensive_map_err<F, O: FnOnce(E) -> F>(self, o: O) -> Result<T, F>;
179
180	/// Defensively map and unpack the value to something else (`U`), or call the default callback
181	/// if `Err`, which should never happen.
182	fn defensive_map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U;
183
184	/// Defensively transform this result into an option, discarding the `Err` variant if it
185	/// happens, which should never happen.
186	fn defensive_ok(self) -> Option<T>;
187
188	/// Exactly the same as `map`, but it prints the appropriate warnings if the value being mapped
189	/// is `Err`.
190	fn defensive_map<U, F: FnOnce(T) -> U>(self, f: F) -> Result<U, E>;
191}
192
193/// Subset of methods similar to [`Defensive`] that can only work for a `Option`.
194pub trait DefensiveOption<T> {
195	/// Potentially map and unpack the value to something else (`U`), or call the default callback
196	/// if `None`, which should never happen.
197	fn defensive_map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U;
198
199	/// Defensively transform this option to a result, mapping `None` to the return value of an
200	/// error closure.
201	fn defensive_ok_or_else<E: core::fmt::Debug, F: FnOnce() -> E>(self, err: F) -> Result<T, E>;
202
203	/// Defensively transform this option to a result, mapping `None` to a default value.
204	fn defensive_ok_or<E: core::fmt::Debug>(self, err: E) -> Result<T, E>;
205
206	/// Exactly the same as `map`, but it prints the appropriate warnings if the value being mapped
207	/// is `None`.
208	fn defensive_map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U>;
209}
210
211impl<T> Defensive<T> for Option<T> {
212	fn defensive_unwrap_or(self, or: T) -> T {
213		match self {
214			Some(inner) => inner,
215			None => {
216				defensive!();
217				or
218			},
219		}
220	}
221
222	fn defensive_unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
223		match self {
224			Some(inner) => inner,
225			None => {
226				defensive!();
227				f()
228			},
229		}
230	}
231
232	fn defensive_unwrap_or_default(self) -> T
233	where
234		T: Default,
235	{
236		match self {
237			Some(inner) => inner,
238			None => {
239				defensive!();
240				Default::default()
241			},
242		}
243	}
244
245	fn defensive(self) -> Self {
246		match self {
247			Some(inner) => Some(inner),
248			None => {
249				defensive!();
250				None
251			},
252		}
253	}
254
255	fn defensive_proof(self, proof: &'static str) -> Self {
256		if self.is_none() {
257			defensive!(proof);
258		}
259		self
260	}
261}
262
263impl<T, E: core::fmt::Debug> Defensive<T> for Result<T, E> {
264	fn defensive_unwrap_or(self, or: T) -> T {
265		match self {
266			Ok(inner) => inner,
267			Err(e) => {
268				defensive!(e);
269				or
270			},
271		}
272	}
273
274	fn defensive_unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
275		match self {
276			Ok(inner) => inner,
277			Err(e) => {
278				defensive!(e);
279				f()
280			},
281		}
282	}
283
284	fn defensive_unwrap_or_default(self) -> T
285	where
286		T: Default,
287	{
288		match self {
289			Ok(inner) => inner,
290			Err(e) => {
291				defensive!(e);
292				Default::default()
293			},
294		}
295	}
296
297	fn defensive(self) -> Self {
298		match self {
299			Ok(inner) => Ok(inner),
300			Err(e) => {
301				defensive!(e);
302				Err(e)
303			},
304		}
305	}
306
307	fn defensive_proof(self, proof: &'static str) -> Self {
308		match self {
309			Ok(inner) => Ok(inner),
310			Err(e) => {
311				defensive!(e, proof);
312				Err(e)
313			},
314		}
315	}
316}
317
318impl<T, E: core::fmt::Debug> DefensiveResult<T, E> for Result<T, E> {
319	fn defensive_map_err<F, O: FnOnce(E) -> F>(self, o: O) -> Result<T, F> {
320		self.map_err(|e| {
321			defensive!(e);
322			o(e)
323		})
324	}
325
326	fn defensive_map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
327		self.map_or_else(
328			|e| {
329				defensive!(e);
330				default(e)
331			},
332			f,
333		)
334	}
335
336	fn defensive_ok(self) -> Option<T> {
337		match self {
338			Ok(inner) => Some(inner),
339			Err(e) => {
340				defensive!(e);
341				None
342			},
343		}
344	}
345
346	fn defensive_map<U, F: FnOnce(T) -> U>(self, f: F) -> Result<U, E> {
347		match self {
348			Ok(inner) => Ok(f(inner)),
349			Err(e) => {
350				defensive!(e);
351				Err(e)
352			},
353		}
354	}
355}
356
357impl<T> DefensiveOption<T> for Option<T> {
358	fn defensive_map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
359		self.map_or_else(
360			|| {
361				defensive!();
362				default()
363			},
364			f,
365		)
366	}
367
368	fn defensive_ok_or_else<E: core::fmt::Debug, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
369		self.ok_or_else(|| {
370			let err_value = err();
371			defensive!(err_value);
372			err_value
373		})
374	}
375
376	fn defensive_ok_or<E: core::fmt::Debug>(self, err: E) -> Result<T, E> {
377		self.ok_or_else(|| {
378			defensive!(err);
379			err
380		})
381	}
382
383	fn defensive_map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
384		match self {
385			Some(inner) => Some(f(inner)),
386			None => {
387				defensive!();
388				None
389			},
390		}
391	}
392}
393
394/// A variant of [`Defensive`] with the same rationale, for the arithmetic operations where in
395/// case an infallible operation fails, it saturates.
396pub trait DefensiveSaturating {
397	/// Return `self` plus `other` defensively.
398	fn defensive_saturating_add(self, other: Self) -> Self;
399	/// Return `self` minus `other` defensively.
400	fn defensive_saturating_sub(self, other: Self) -> Self;
401	/// Return the product of `self` and `other` defensively.
402	fn defensive_saturating_mul(self, other: Self) -> Self;
403	/// Increase `self` by `other` defensively.
404	fn defensive_saturating_accrue(&mut self, other: Self);
405	/// Reduce `self` by `other` defensively.
406	fn defensive_saturating_reduce(&mut self, other: Self);
407	/// Increment `self` by one defensively.
408	fn defensive_saturating_inc(&mut self);
409	/// Decrement `self` by one defensively.
410	fn defensive_saturating_dec(&mut self);
411}
412
413// NOTE: A bit unfortunate, since T has to be bound by all the traits needed. Could make it
414// `DefensiveSaturating<T>` to mitigate.
415impl<T: Saturating + CheckedAdd + CheckedMul + CheckedSub + One> DefensiveSaturating for T {
416	fn defensive_saturating_add(self, other: Self) -> Self {
417		self.checked_add(&other).defensive_unwrap_or_else(|| self.saturating_add(other))
418	}
419	fn defensive_saturating_sub(self, other: Self) -> Self {
420		self.checked_sub(&other).defensive_unwrap_or_else(|| self.saturating_sub(other))
421	}
422	fn defensive_saturating_mul(self, other: Self) -> Self {
423		self.checked_mul(&other).defensive_unwrap_or_else(|| self.saturating_mul(other))
424	}
425	fn defensive_saturating_accrue(&mut self, other: Self) {
426		// Use `replace` here since `take` would require `T: Default`.
427		*self = core::mem::replace(self, One::one()).defensive_saturating_add(other);
428	}
429	fn defensive_saturating_reduce(&mut self, other: Self) {
430		// Use `replace` here since `take` would require `T: Default`.
431		*self = core::mem::replace(self, One::one()).defensive_saturating_sub(other);
432	}
433	fn defensive_saturating_inc(&mut self) {
434		self.defensive_saturating_accrue(One::one());
435	}
436	fn defensive_saturating_dec(&mut self) {
437		self.defensive_saturating_reduce(One::one());
438	}
439}
440
441/// Construct an object by defensively truncating an input if the `TryFrom` conversion fails.
442pub trait DefensiveTruncateFrom<T> {
443	/// Use `TryFrom` first and defensively fall back to truncating otherwise.
444	///
445	/// # Example
446	///
447	/// ```
448	/// use frame_support::{BoundedVec, traits::DefensiveTruncateFrom};
449	/// use sp_runtime::traits::ConstU32;
450	///
451	/// let unbound = vec![1, 2];
452	/// let bound = BoundedVec::<u8, ConstU32<2>>::defensive_truncate_from(unbound);
453	///
454	/// assert_eq!(bound, vec![1, 2]);
455	/// ```
456	fn defensive_truncate_from(unbound: T) -> Self;
457}
458
459impl<T, U> DefensiveTruncateFrom<U> for T
460where
461	// NOTE: We use the fact that `BoundedVec` and
462	// `BoundedSlice` use `Self` as error type. We could also
463	// require a `Clone` bound and use `unbound.clone()` in the
464	// error case.
465	T: TruncateFrom<U> + TryFrom<U, Error = U>,
466{
467	fn defensive_truncate_from(unbound: U) -> Self {
468		unbound.try_into().map_or_else(
469			|err| {
470				defensive!("DefensiveTruncateFrom truncating");
471				T::truncate_from(err)
472			},
473			|bound| bound,
474		)
475	}
476}
477
478/// Defensively truncate a value and convert it into its bounded form.
479pub trait DefensiveTruncateInto<T> {
480	/// Defensively truncate a value and convert it into its bounded form.
481	fn defensive_truncate_into(self) -> T;
482}
483
484impl<T, U: DefensiveTruncateFrom<T>> DefensiveTruncateInto<U> for T {
485	fn defensive_truncate_into(self) -> U {
486		U::defensive_truncate_from(self)
487	}
488}
489/// Defensively calculates the minimum of two values.
490///
491/// Can be used in contexts where we assume the receiver value to be (strictly) smaller.
492pub trait DefensiveMin<T> {
493	/// Returns the minimum and defensively checks that `self` is not larger than `other`.
494	///
495	/// # Example
496	///
497	/// ```
498	/// use frame_support::traits::DefensiveMin;
499	/// // min(3, 4) is 3.
500	/// assert_eq!(3, 3_u32.defensive_min(4_u32));
501	/// // min(4, 4) is 4.
502	/// assert_eq!(4, 4_u32.defensive_min(4_u32));
503	/// ```
504	///
505	/// ```should_panic
506	/// use frame_support::traits::DefensiveMin;
507	/// // min(4, 3) panics.
508	/// 4_u32.defensive_min(3_u32);
509	/// ```
510	fn defensive_min(self, other: T) -> Self;
511
512	/// Returns the minimum and defensively checks that `self` is smaller than `other`.
513	///
514	/// # Example
515	///
516	/// ```
517	/// use frame_support::traits::DefensiveMin;
518	/// // min(3, 4) is 3.
519	/// assert_eq!(3, 3_u32.defensive_strict_min(4_u32));
520	/// ```
521	///
522	/// ```should_panic
523	/// use frame_support::traits::DefensiveMin;
524	/// // min(4, 4) panics.
525	/// 4_u32.defensive_strict_min(4_u32);
526	/// ```
527	fn defensive_strict_min(self, other: T) -> Self;
528}
529
530impl<T> DefensiveMin<T> for T
531where
532	T: PartialOrd<T>,
533{
534	fn defensive_min(self, other: T) -> Self {
535		if self <= other {
536			self
537		} else {
538			defensive!("DefensiveMin");
539			other
540		}
541	}
542
543	fn defensive_strict_min(self, other: T) -> Self {
544		if self < other {
545			self
546		} else {
547			defensive!("DefensiveMin strict");
548			other
549		}
550	}
551}
552
553/// Defensively calculates the maximum of two values.
554///
555/// Can be used in contexts where we assume the receiver value to be (strictly) larger.
556pub trait DefensiveMax<T> {
557	/// Returns the maximum and defensively asserts that `other` is not larger than `self`.
558	///
559	/// # Example
560	///
561	/// ```
562	/// use frame_support::traits::DefensiveMax;
563	/// // max(4, 3) is 4.
564	/// assert_eq!(4, 4_u32.defensive_max(3_u32));
565	/// // max(4, 4) is 4.
566	/// assert_eq!(4, 4_u32.defensive_max(4_u32));
567	/// ```
568	///
569	/// ```should_panic
570	/// use frame_support::traits::DefensiveMax;
571	/// // max(4, 5) panics.
572	/// 4_u32.defensive_max(5_u32);
573	/// ```
574	fn defensive_max(self, other: T) -> Self;
575
576	/// Returns the maximum and defensively asserts that `other` is smaller than `self`.
577	///
578	/// # Example
579	///
580	/// ```
581	/// use frame_support::traits::DefensiveMax;
582	/// // y(4, 3) is 4.
583	/// assert_eq!(4, 4_u32.defensive_strict_max(3_u32));
584	/// ```
585	///
586	/// ```should_panic
587	/// use frame_support::traits::DefensiveMax;
588	/// // max(4, 4) panics.
589	/// 4_u32.defensive_strict_max(4_u32);
590	/// ```
591	fn defensive_strict_max(self, other: T) -> Self;
592}
593
594impl<T> DefensiveMax<T> for T
595where
596	T: PartialOrd<T>,
597{
598	fn defensive_max(self, other: T) -> Self {
599		if self >= other {
600			self
601		} else {
602			defensive!("DefensiveMax");
603			other
604		}
605	}
606
607	fn defensive_strict_max(self, other: T) -> Self {
608		if self > other {
609			self
610		} else {
611			defensive!("DefensiveMax strict");
612			other
613		}
614	}
615}
616
617/// Anything that can have a `::len()` method.
618pub trait Len {
619	/// Return the length of data type.
620	fn len(&self) -> usize;
621}
622
623impl<T: IntoIterator + Clone> Len for T
624where
625	<T as IntoIterator>::IntoIter: ExactSizeIterator,
626{
627	fn len(&self) -> usize {
628		self.clone().into_iter().len()
629	}
630}
631
632/// A type for which some values make sense to be able to drop without further consideration.
633pub trait TryDrop: Sized {
634	/// Drop an instance cleanly. Only works if its value represents "no-operation".
635	fn try_drop(self) -> Result<(), Self>;
636}
637
638impl TryDrop for () {
639	fn try_drop(self) -> Result<(), Self> {
640		Ok(())
641	}
642}
643
644/// Return type used when we need to return one of two items, each of the opposite direction or
645/// sign, with one (`Same`) being of the same type as the `self` or primary argument of the function
646/// that returned it.
647pub enum SameOrOther<A, B> {
648	/// No item.
649	None,
650	/// An item of the same type as the `Self` on which the return function was called.
651	Same(A),
652	/// An item of the opposite type to the `Self` on which the return function was called.
653	Other(B),
654}
655
656impl<A, B> TryDrop for SameOrOther<A, B> {
657	fn try_drop(self) -> Result<(), Self> {
658		if let SameOrOther::None = self {
659			Ok(())
660		} else {
661			Err(self)
662		}
663	}
664}
665
666impl<A, B> SameOrOther<A, B> {
667	/// Returns `Ok` with the inner value of `Same` if `self` is that, otherwise returns `Err` with
668	/// `self`.
669	pub fn try_same(self) -> Result<A, Self> {
670		match self {
671			SameOrOther::Same(a) => Ok(a),
672			x => Err(x),
673		}
674	}
675
676	/// Returns `Ok` with the inner value of `Other` if `self` is that, otherwise returns `Err` with
677	/// `self`.
678	pub fn try_other(self) -> Result<B, Self> {
679		match self {
680			SameOrOther::Other(b) => Ok(b),
681			x => Err(x),
682		}
683	}
684
685	/// Returns `Ok` if `self` is `None`, otherwise returns `Err` with `self`.
686	pub fn try_none(self) -> Result<(), Self> {
687		match self {
688			SameOrOther::None => Ok(()),
689			x => Err(x),
690		}
691	}
692
693	pub fn same(self) -> Result<A, B>
694	where
695		A: Default,
696	{
697		match self {
698			SameOrOther::Same(a) => Ok(a),
699			SameOrOther::None => Ok(A::default()),
700			SameOrOther::Other(b) => Err(b),
701		}
702	}
703
704	pub fn other(self) -> Result<B, A>
705	where
706		B: Default,
707	{
708		match self {
709			SameOrOther::Same(a) => Err(a),
710			SameOrOther::None => Ok(B::default()),
711			SameOrOther::Other(b) => Ok(b),
712		}
713	}
714}
715
716/// Handler for when a new account has been created.
717#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
718#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
719#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
720pub trait OnNewAccount<AccountId> {
721	/// A new account `who` has been registered.
722	fn on_new_account(who: &AccountId);
723}
724
725/// The account with the given id was reaped.
726#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
727#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
728#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
729pub trait OnKilledAccount<AccountId> {
730	/// The account with the given id was reaped.
731	fn on_killed_account(who: &AccountId);
732}
733
734/// A simple, generic one-parameter event notifier/handler.
735pub trait HandleLifetime<T> {
736	/// An account was created.
737	fn created(_t: &T) -> Result<(), DispatchError> {
738		Ok(())
739	}
740
741	/// An account was killed.
742	fn killed(_t: &T) -> Result<(), DispatchError> {
743		Ok(())
744	}
745}
746
747impl<T> HandleLifetime<T> for () {}
748
749pub trait Time {
750	type Moment: sp_arithmetic::traits::AtLeast32Bit + Parameter + Default + Copy + MaxEncodedLen;
751
752	fn now() -> Self::Moment;
753}
754
755/// Trait to deal with unix time.
756pub trait UnixTime {
757	/// Return duration since `SystemTime::UNIX_EPOCH`.
758	fn now() -> core::time::Duration;
759}
760
761/// Trait to be used when types are exactly same.
762///
763/// This allow to convert back and forth from type, a reference and a mutable reference.
764pub trait IsType<T>: Into<T> + From<T> {
765	/// Cast reference.
766	fn from_ref(t: &T) -> &Self;
767
768	/// Cast reference.
769	fn into_ref(&self) -> &T;
770
771	/// Cast mutable reference.
772	fn from_mut(t: &mut T) -> &mut Self;
773
774	/// Cast mutable reference.
775	fn into_mut(&mut self) -> &mut T;
776}
777
778impl<T> IsType<T> for T {
779	fn from_ref(t: &T) -> &Self {
780		t
781	}
782	fn into_ref(&self) -> &T {
783		self
784	}
785	fn from_mut(t: &mut T) -> &mut Self {
786		t
787	}
788	fn into_mut(&mut self) -> &mut T {
789		self
790	}
791}
792
793/// Something that can be checked to be a of sub type `T`.
794///
795/// This is useful for enums where each variant encapsulates a different sub type, and
796/// you need access to these sub types.
797///
798/// For example, in FRAME, this trait is implemented for the runtime `Call` enum. Pallets use this
799/// to check if a certain call is an instance of the local pallet's `Call` enum.
800///
801/// # Example
802///
803/// ```
804/// # use frame_support::traits::IsSubType;
805///
806/// enum Test {
807///     String(String),
808///     U32(u32),
809/// }
810///
811/// impl IsSubType<String> for Test {
812///     fn is_sub_type(&self) -> Option<&String> {
813///         match self {
814///             Self::String(ref r) => Some(r),
815///             _ => None,
816///         }
817///     }
818/// }
819///
820/// impl IsSubType<u32> for Test {
821///     fn is_sub_type(&self) -> Option<&u32> {
822///         match self {
823///             Self::U32(ref r) => Some(r),
824///             _ => None,
825///         }
826///     }
827/// }
828///
829/// fn main() {
830///     let data = Test::String("test".into());
831///
832///     assert_eq!("test", IsSubType::<String>::is_sub_type(&data).unwrap().as_str());
833/// }
834/// ```
835pub trait IsSubType<T> {
836	/// Returns `Some(_)` if `self` is an instance of sub type `T`.
837	fn is_sub_type(&self) -> Option<&T>;
838}
839
840/// Something that can execute a given block.
841///
842/// Executing a block means that all extrinsics in a given block will be executed and the resulting
843/// header will be checked against the header of the given block.
844pub trait ExecuteBlock<Block: BlockT> {
845	/// Execute the given `block`.
846	///
847	/// This will execute all extrinsics in the block and check that the resulting header is
848	/// correct.
849	///
850	/// This function is a wrapper around [`Self::verify_and_remove_seal`] and
851	/// [`Self::execute_verified_block`].
852	///
853	/// # Panic
854	///
855	/// Panics when an extrinsics panics or the resulting header doesn't match the expected header
856	/// or the seal is invalid.
857	fn execute_block(mut block: Block::LazyBlock) {
858		Self::verify_and_remove_seal(&mut block);
859		Self::execute_verified_block(block);
860	}
861
862	/// Verify and remove seal.
863	///
864	/// Verifies any seal meant for the consensus logic represented by the implementation. An
865	/// implementation may also chooses to not verify anything.
866	///
867	/// # Panic
868	///
869	/// Panics if a seal is invalid or if a seal is required, but not present.
870	fn verify_and_remove_seal(block: &mut Block::LazyBlock);
871
872	/// Executes the given `block` after it was verified by `[Self::verify_and_remove_seal]`.
873	///
874	/// # Panic
875	///
876	/// Panics when an extrinsics panics or the resulting header doesn't match the expected header.
877	fn execute_verified_block(block: Block::LazyBlock);
878}
879
880/// Something that can compare privileges of two origins.
881pub trait PrivilegeCmp<Origin> {
882	/// Compare the `left` to the `right` origin.
883	///
884	/// The returned ordering should be from the pov of the `left` origin.
885	///
886	/// Should return `None` when it can not compare the given origins.
887	fn cmp_privilege(left: &Origin, right: &Origin) -> Option<Ordering>;
888}
889
890/// Implementation of [`PrivilegeCmp`] that only checks for equal origins.
891///
892/// This means it will either return [`Ordering::Equal`] or `None`.
893pub struct EqualPrivilegeOnly;
894impl<Origin: PartialEq> PrivilegeCmp<Origin> for EqualPrivilegeOnly {
895	fn cmp_privilege(left: &Origin, right: &Origin) -> Option<Ordering> {
896		(left == right).then(|| Ordering::Equal)
897	}
898}
899
900/// Off-chain computation trait.
901///
902/// Implementing this trait on a module allows you to perform long-running tasks
903/// that make (by default) validators generate transactions that feed results
904/// of those long-running computations back on chain.
905///
906/// NOTE: This function runs off-chain, so it can access the block state,
907/// but cannot preform any alterations. More specifically alterations are
908/// not forbidden, but they are not persisted in any way after the worker
909/// has finished.
910#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
911#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
912#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
913pub trait OffchainWorker<BlockNumber> {
914	/// This function is being called after every block import (when fully synced).
915	///
916	/// Implement this and use any of the `Offchain` `sp_io` set of APIs
917	/// to perform off-chain computations, calls and submit transactions
918	/// with results to trigger any on-chain changes.
919	/// Any state alterations are lost and are not persisted.
920	fn offchain_worker(_n: BlockNumber) {}
921}
922
923/// Some amount of backing from a group. The precise definition of what it means to "back" something
924/// is left flexible.
925pub struct Backing {
926	/// The number of members of the group that back some motion.
927	pub approvals: u32,
928	/// The total count of group members.
929	pub eligible: u32,
930}
931
932/// Retrieve the backing from an object's ref.
933pub trait GetBacking {
934	/// Returns `Some` `Backing` if `self` represents a fractional/groupwise backing of some
935	/// implicit motion. `None` if it does not.
936	fn get_backing(&self) -> Option<Backing>;
937}
938
939/// A trait to check if an extrinsic is an inherent.
940pub trait IsInherent<Extrinsic> {
941	/// Whether this extrinsic is an inherent.
942	fn is_inherent(ext: &Extrinsic) -> bool;
943}
944
945/// Interface for types capable of constructing an inherent extrinsic.
946pub trait InherentBuilder: ExtrinsicCall {
947	/// Create a new inherent from a given call.
948	fn new_inherent(call: Self::Call) -> Self;
949}
950
951impl<Address, Call, Signature, ExtensionV0, ExtensionOtherVersions> InherentBuilder
952	for sp_runtime::generic::UncheckedExtrinsic<
953		Address,
954		Call,
955		Signature,
956		ExtensionV0,
957		ExtensionOtherVersions,
958	>
959{
960	fn new_inherent(call: Self::Call) -> Self {
961		Self::new_bare(call)
962	}
963}
964
965/// Interface for types capable of constructing a signed transaction.
966pub trait SignedTransactionBuilder: ExtrinsicCall {
967	type Address;
968	type Signature;
969	type Extension;
970
971	/// Create a new signed transaction from a given call and extension using the provided signature
972	/// data.
973	fn new_signed_transaction(
974		call: Self::Call,
975		signed: Self::Address,
976		signature: Self::Signature,
977		tx_ext: Self::Extension,
978	) -> Self;
979}
980
981impl<Address, Call, Signature, ExtensionV0, ExtensionOtherVersions> SignedTransactionBuilder
982	for sp_runtime::generic::UncheckedExtrinsic<
983		Address,
984		Call,
985		Signature,
986		ExtensionV0,
987		ExtensionOtherVersions,
988	>
989{
990	type Address = Address;
991	type Signature = Signature;
992	type Extension = ExtensionV0;
993
994	fn new_signed_transaction(
995		call: Self::Call,
996		signed: Address,
997		signature: Signature,
998		tx_ext: ExtensionV0,
999	) -> Self {
1000		Self::new_signed(call, signed, signature, tx_ext)
1001	}
1002}
1003
1004/// Something that can estimate the fee of a (frame-based) call.
1005///
1006/// Typically, the same pallet that will charge transaction fees will implement this.
1007pub trait EstimateCallFee<Call, Balance> {
1008	/// Estimate the fee of this call.
1009	///
1010	/// The dispatch info and the length is deduced from the call. The post info can optionally be
1011	/// provided.
1012	fn estimate_call_fee(call: &Call, post_info: crate::dispatch::PostDispatchInfo) -> Balance;
1013}
1014
1015// Useful for building mocks.
1016#[cfg(feature = "std")]
1017impl<Call, Balance: From<u32>, const T: u32> EstimateCallFee<Call, Balance> for ConstU32<T> {
1018	fn estimate_call_fee(_: &Call, _: crate::dispatch::PostDispatchInfo) -> Balance {
1019		T.into()
1020	}
1021}
1022
1023#[cfg(feature = "std")]
1024impl<Call, Balance: From<u32>, const T: u64> EstimateCallFee<Call, Balance> for ConstU64<T> {
1025	fn estimate_call_fee(_: &Call, _: crate::dispatch::PostDispatchInfo) -> Balance {
1026		(T as u32).into()
1027	}
1028}
1029
1030/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
1031///
1032/// The encoding is the encoding of `T` prepended with the compact encoding of its size in bytes.
1033/// Thus the encoded value can be decoded as a `Vec<u8>`.
1034#[derive(Debug, Eq, PartialEq, Default, Clone)]
1035#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
1036pub struct WrapperOpaque<T>(pub T);
1037
1038impl<T: Encode> EncodeLike for WrapperOpaque<T> {}
1039impl<T: Encode> EncodeLike<WrapperKeepOpaque<T>> for WrapperOpaque<T> {}
1040
1041impl<T: Encode> Encode for WrapperOpaque<T> {
1042	fn size_hint(&self) -> usize {
1043		self.0.size_hint().saturating_add(<codec::Compact<u32>>::max_encoded_len())
1044	}
1045
1046	fn encode_to<O: codec::Output + ?Sized>(&self, dest: &mut O) {
1047		self.0.encode().encode_to(dest);
1048	}
1049
1050	fn encode(&self) -> Vec<u8> {
1051		self.0.encode().encode()
1052	}
1053
1054	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1055		self.0.encode().using_encoded(f)
1056	}
1057}
1058
1059impl<T: Decode> Decode for WrapperOpaque<T> {
1060	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
1061		Ok(Self(T::decode_all_with_depth_limit(
1062			crate::MAX_EXTRINSIC_DEPTH,
1063			&mut &<Vec<u8>>::decode(input)?[..],
1064		)?))
1065	}
1066
1067	fn skip<I: Input>(input: &mut I) -> Result<(), codec::Error> {
1068		<Vec<u8>>::skip(input)
1069	}
1070}
1071
1072impl<T> From<T> for WrapperOpaque<T> {
1073	fn from(t: T) -> Self {
1074		Self(t)
1075	}
1076}
1077
1078impl<T: MaxEncodedLen> MaxEncodedLen for WrapperOpaque<T> {
1079	fn max_encoded_len() -> usize {
1080		let t_max_len = T::max_encoded_len();
1081
1082		// See scale encoding: https://docs.substrate.io/reference/scale-codec/
1083		if t_max_len < 64 {
1084			t_max_len + 1
1085		} else if t_max_len < 2usize.pow(14) {
1086			t_max_len + 2
1087		} else if t_max_len < 2usize.pow(30) {
1088			t_max_len + 4
1089		} else {
1090			<codec::Compact<u32>>::max_encoded_len().saturating_add(T::max_encoded_len())
1091		}
1092	}
1093}
1094
1095impl<T: TypeInfo + 'static> TypeInfo for WrapperOpaque<T> {
1096	type Identity = Self;
1097	fn type_info() -> Type {
1098		Type::builder()
1099			.path(Path::new("WrapperOpaque", module_path!()))
1100			.type_params(vec![TypeParameter::new("T", Some(meta_type::<T>()))])
1101			.composite(
1102				Fields::unnamed()
1103					.field(|f| f.compact::<u32>())
1104					.field(|f| f.ty::<T>().type_name("T")),
1105			)
1106	}
1107}
1108
1109/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
1110///
1111/// This type is similar to [`WrapperOpaque`], but it differs in the way it stores the type `T`.
1112/// While [`WrapperOpaque`] stores the decoded type, the [`WrapperKeepOpaque`] stores the type only
1113/// in its opaque format, aka as a `Vec<u8>`. To access the real type `T` [`Self::try_decode`] needs
1114/// to be used.
1115#[derive(Debug, Eq, PartialEq, Default, Clone)]
1116pub struct WrapperKeepOpaque<T> {
1117	data: Vec<u8>,
1118	_phantom: core::marker::PhantomData<T>,
1119}
1120
1121impl<T: Decode> WrapperKeepOpaque<T> {
1122	/// Try to decode the wrapped type from the inner `data`.
1123	///
1124	/// Returns `None` if the decoding failed.
1125	pub fn try_decode(&self) -> Option<T> {
1126		T::decode_all_with_depth_limit(crate::MAX_EXTRINSIC_DEPTH, &mut &self.data[..]).ok()
1127	}
1128
1129	/// Returns the length of the encoded `T`.
1130	pub fn encoded_len(&self) -> usize {
1131		self.data.len()
1132	}
1133
1134	/// Returns the encoded data.
1135	pub fn encoded(&self) -> &[u8] {
1136		&self.data
1137	}
1138
1139	/// Create from the given encoded `data`.
1140	pub fn from_encoded(data: Vec<u8>) -> Self {
1141		Self { data, _phantom: core::marker::PhantomData }
1142	}
1143}
1144
1145impl<T: Encode> EncodeLike for WrapperKeepOpaque<T> {}
1146impl<T: Encode> EncodeLike<WrapperOpaque<T>> for WrapperKeepOpaque<T> {}
1147
1148impl<T: Encode> Encode for WrapperKeepOpaque<T> {
1149	fn size_hint(&self) -> usize {
1150		self.data.len() + codec::Compact::<u32>::compact_len(&(self.data.len() as u32))
1151	}
1152
1153	fn encode_to<O: codec::Output + ?Sized>(&self, dest: &mut O) {
1154		self.data.encode_to(dest);
1155	}
1156
1157	fn encode(&self) -> Vec<u8> {
1158		self.data.encode()
1159	}
1160
1161	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1162		self.data.using_encoded(f)
1163	}
1164}
1165
1166impl<T: Decode> Decode for WrapperKeepOpaque<T> {
1167	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
1168		Ok(Self { data: Vec::<u8>::decode(input)?, _phantom: core::marker::PhantomData })
1169	}
1170
1171	fn skip<I: Input>(input: &mut I) -> Result<(), codec::Error> {
1172		<Vec<u8>>::skip(input)
1173	}
1174}
1175
1176impl<T: MaxEncodedLen> MaxEncodedLen for WrapperKeepOpaque<T> {
1177	fn max_encoded_len() -> usize {
1178		WrapperOpaque::<T>::max_encoded_len()
1179	}
1180}
1181
1182impl<T: TypeInfo + 'static> TypeInfo for WrapperKeepOpaque<T> {
1183	type Identity = Self;
1184	fn type_info() -> Type {
1185		Type::builder()
1186			.path(Path::new("WrapperKeepOpaque", module_path!()))
1187			.type_params(vec![TypeParameter::new("T", Some(meta_type::<T>()))])
1188			.composite(
1189				Fields::unnamed()
1190					.field(|f| f.compact::<u32>())
1191					.field(|f| f.ty::<T>().type_name("T")),
1192			)
1193	}
1194}
1195
1196/// A interface for looking up preimages from their hash on chain.
1197pub trait PreimageProvider<Hash> {
1198	/// Returns whether a preimage exists for a given hash.
1199	///
1200	/// A value of `true` implies that `get_preimage` is `Some`.
1201	fn have_preimage(hash: &Hash) -> bool;
1202
1203	/// Returns the preimage for a given hash.
1204	fn get_preimage(hash: &Hash) -> Option<Vec<u8>>;
1205
1206	/// Returns whether a preimage request exists for a given hash.
1207	fn preimage_requested(hash: &Hash) -> bool;
1208
1209	/// Request that someone report a preimage. Providers use this to optimise the economics for
1210	/// preimage reporting.
1211	fn request_preimage(hash: &Hash);
1212
1213	/// Cancel a previous preimage request.
1214	fn unrequest_preimage(hash: &Hash);
1215}
1216
1217impl<Hash> PreimageProvider<Hash> for () {
1218	fn have_preimage(_: &Hash) -> bool {
1219		false
1220	}
1221	fn get_preimage(_: &Hash) -> Option<Vec<u8>> {
1222		None
1223	}
1224	fn preimage_requested(_: &Hash) -> bool {
1225		false
1226	}
1227	fn request_preimage(_: &Hash) {}
1228	fn unrequest_preimage(_: &Hash) {}
1229}
1230
1231/// A interface for managing preimages to hashes on chain.
1232///
1233/// Note that this API does not assume any underlying user is calling, and thus
1234/// does not handle any preimage ownership or fees. Other system level logic that
1235/// uses this API should implement that on their own side.
1236pub trait PreimageRecipient<Hash>: PreimageProvider<Hash> {
1237	/// Maximum size of a preimage.
1238	type MaxSize: Get<u32>;
1239
1240	/// Store the bytes of a preimage on chain infallible due to the bounded type.
1241	fn note_preimage(bytes: crate::BoundedVec<u8, Self::MaxSize>);
1242
1243	/// Clear a previously noted preimage. This is infallible and should be treated more like a
1244	/// hint - if it was not previously noted or if it is now requested, then this will not do
1245	/// anything.
1246	fn unnote_preimage(hash: &Hash);
1247}
1248
1249impl<Hash> PreimageRecipient<Hash> for () {
1250	type MaxSize = ();
1251	fn note_preimage(_: crate::BoundedVec<u8, Self::MaxSize>) {}
1252	fn unnote_preimage(_: &Hash) {}
1253}
1254
1255/// Trait for touching/creating an asset account with a deposit taken from a designated depositor
1256/// specified by the client.
1257///
1258/// Ensures that transfers to the touched account will succeed without being denied by the account
1259/// creation requirements. For example, it is useful for the account creation of non-sufficient
1260/// assets when its system account may not have the free consumer reference required for it. If
1261/// there is no risk of failing to meet those requirements, the touch operation can be a no-op, as
1262/// is common for native assets.
1263pub trait AccountTouch<AssetId, AccountId> {
1264	/// The type for currency units of the deposit.
1265	type Balance;
1266
1267	/// The deposit amount of a native currency required for touching an account of the `asset`.
1268	fn deposit_required(asset: AssetId) -> Self::Balance;
1269
1270	/// Check if an account for a given asset should be touched to meet the existence requirements.
1271	fn should_touch(asset: AssetId, who: &AccountId) -> bool;
1272
1273	/// Create an account for `who` of the `asset` with a deposit taken from the `depositor`.
1274	fn touch(asset: AssetId, who: &AccountId, depositor: &AccountId) -> DispatchResult;
1275}
1276
1277/// Trait for reporting additional validator reward points
1278pub trait RewardsReporter<ValidatorId> {
1279	/// The input is an iterator of tuples of validator account IDs and the amount of points they
1280	/// should be rewarded.
1281	fn reward_by_ids(validators_points: impl IntoIterator<Item = (ValidatorId, u32)>);
1282}
1283
1284#[cfg(test)]
1285mod test {
1286	use super::*;
1287	use core::marker::PhantomData;
1288	use sp_core::bounded::{BoundedSlice, BoundedVec};
1289
1290	#[test]
1291	fn defensive_assert_works() {
1292		defensive_assert!(true);
1293		defensive_assert!(true,);
1294		defensive_assert!(true, "must work");
1295		defensive_assert!(true, "must work",);
1296	}
1297
1298	#[test]
1299	#[cfg(debug_assertions)]
1300	#[should_panic(expected = "Defensive failure has been triggered!: \"1 == 0\": \"Must fail\"")]
1301	fn defensive_assert_panics() {
1302		defensive_assert!(1 == 0, "Must fail");
1303	}
1304
1305	#[test]
1306	#[cfg(not(debug_assertions))]
1307	fn defensive_assert_does_not_panic() {
1308		defensive_assert!(1 == 0, "Must fail");
1309	}
1310
1311	#[test]
1312	#[cfg(not(debug_assertions))]
1313	fn defensive_saturating_accrue_works() {
1314		let mut v = 1_u32;
1315		v.defensive_saturating_accrue(2);
1316		assert_eq!(v, 3);
1317		v.defensive_saturating_accrue(u32::MAX);
1318		assert_eq!(v, u32::MAX);
1319		v.defensive_saturating_accrue(1);
1320		assert_eq!(v, u32::MAX);
1321	}
1322
1323	#[test]
1324	#[cfg(debug_assertions)]
1325	#[should_panic(expected = "Defensive")]
1326	fn defensive_saturating_accrue_panics() {
1327		let mut v = u32::MAX;
1328		v.defensive_saturating_accrue(1); // defensive failure
1329	}
1330
1331	#[test]
1332	#[cfg(not(debug_assertions))]
1333	fn defensive_saturating_reduce_works() {
1334		let mut v = u32::MAX;
1335		v.defensive_saturating_reduce(3);
1336		assert_eq!(v, u32::MAX - 3);
1337		v.defensive_saturating_reduce(u32::MAX);
1338		assert_eq!(v, 0);
1339		v.defensive_saturating_reduce(1);
1340		assert_eq!(v, 0);
1341	}
1342
1343	#[test]
1344	#[cfg(debug_assertions)]
1345	#[should_panic(expected = "Defensive")]
1346	fn defensive_saturating_reduce_panics() {
1347		let mut v = 0_u32;
1348		v.defensive_saturating_reduce(1); // defensive failure
1349	}
1350
1351	#[test]
1352	#[cfg(not(debug_assertions))]
1353	fn defensive_saturating_inc_works() {
1354		let mut v = 0_u32;
1355		for i in 1..10 {
1356			v.defensive_saturating_inc();
1357			assert_eq!(v, i);
1358		}
1359		v += u32::MAX - 10;
1360		v.defensive_saturating_inc();
1361		assert_eq!(v, u32::MAX);
1362		v.defensive_saturating_inc();
1363		assert_eq!(v, u32::MAX);
1364	}
1365
1366	#[test]
1367	#[cfg(debug_assertions)]
1368	#[should_panic(expected = "Defensive")]
1369	fn defensive_saturating_inc_panics() {
1370		let mut v = u32::MAX;
1371		v.defensive_saturating_inc(); // defensive failure
1372	}
1373
1374	#[test]
1375	#[cfg(not(debug_assertions))]
1376	fn defensive_saturating_dec_works() {
1377		let mut v = u32::MAX;
1378		for i in 1..10 {
1379			v.defensive_saturating_dec();
1380			assert_eq!(v, u32::MAX - i);
1381		}
1382		v -= u32::MAX - 10;
1383		v.defensive_saturating_dec();
1384		assert_eq!(v, 0);
1385		v.defensive_saturating_dec();
1386		assert_eq!(v, 0);
1387	}
1388
1389	#[test]
1390	#[cfg(debug_assertions)]
1391	#[should_panic(expected = "Defensive")]
1392	fn defensive_saturating_dec_panics() {
1393		let mut v = 0_u32;
1394		v.defensive_saturating_dec(); // defensive failure
1395	}
1396
1397	#[test]
1398	#[cfg(not(debug_assertions))]
1399	fn defensive_truncating_from_vec_defensive_works() {
1400		let unbound = vec![1u32, 2];
1401		let bound = BoundedVec::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1402		assert_eq!(bound, vec![1u32]);
1403	}
1404
1405	#[test]
1406	#[cfg(not(debug_assertions))]
1407	fn defensive_truncating_from_slice_defensive_works() {
1408		let unbound = &[1u32, 2];
1409		let bound = BoundedSlice::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1410		assert_eq!(bound, &[1u32][..]);
1411	}
1412
1413	#[test]
1414	#[cfg(debug_assertions)]
1415	#[should_panic(
1416		expected = "Defensive failure has been triggered!: \"DefensiveTruncateFrom truncating\""
1417	)]
1418	fn defensive_truncating_from_vec_defensive_panics() {
1419		let unbound = vec![1u32, 2];
1420		let _ = BoundedVec::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1421	}
1422
1423	#[test]
1424	#[cfg(debug_assertions)]
1425	#[should_panic(
1426		expected = "Defensive failure has been triggered!: \"DefensiveTruncateFrom truncating\""
1427	)]
1428	fn defensive_truncating_from_slice_defensive_panics() {
1429		let unbound = &[1u32, 2];
1430		let _ = BoundedSlice::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1431	}
1432
1433	#[test]
1434	fn defensive_truncate_from_vec_works() {
1435		let unbound = vec![1u32, 2, 3];
1436		let bound = BoundedVec::<u32, ConstU32<3>>::defensive_truncate_from(unbound.clone());
1437		assert_eq!(bound, unbound);
1438	}
1439
1440	#[test]
1441	fn defensive_truncate_from_slice_works() {
1442		let unbound = [1u32, 2, 3];
1443		let bound = BoundedSlice::<u32, ConstU32<3>>::defensive_truncate_from(&unbound);
1444		assert_eq!(bound, &unbound[..]);
1445	}
1446
1447	#[derive(Encode, Decode)]
1448	enum NestedType {
1449		Nested(Box<Self>),
1450		Done,
1451	}
1452
1453	#[test]
1454	fn test_opaque_wrapper_decode_limit() {
1455		let limit = crate::MAX_EXTRINSIC_DEPTH as usize;
1456		let mut ok_bytes = vec![0u8; limit];
1457		ok_bytes.push(1u8);
1458		let mut err_bytes = vec![0u8; limit + 1];
1459		err_bytes.push(1u8);
1460		assert!(<WrapperOpaque<NestedType>>::decode(&mut &ok_bytes.encode()[..]).is_ok());
1461		assert!(<WrapperOpaque<NestedType>>::decode(&mut &err_bytes.encode()[..]).is_err());
1462
1463		let ok_keep_opaque = WrapperKeepOpaque { data: ok_bytes, _phantom: PhantomData };
1464		let err_keep_opaque = WrapperKeepOpaque { data: err_bytes, _phantom: PhantomData };
1465
1466		assert!(<WrapperKeepOpaque<NestedType>>::try_decode(&ok_keep_opaque).is_some());
1467		assert!(<WrapperKeepOpaque<NestedType>>::try_decode(&err_keep_opaque).is_none());
1468	}
1469
1470	#[test]
1471	fn test_opaque_wrapper() {
1472		let encoded = WrapperOpaque(3u32).encode();
1473		assert_eq!(encoded, [codec::Compact(4u32).encode(), 3u32.to_le_bytes().to_vec()].concat());
1474		let vec_u8 = <Vec<u8>>::decode(&mut &encoded[..]).unwrap();
1475		let decoded_from_vec_u8 = u32::decode(&mut &vec_u8[..]).unwrap();
1476		assert_eq!(decoded_from_vec_u8, 3u32);
1477		let decoded = <WrapperOpaque<u32>>::decode(&mut &encoded[..]).unwrap();
1478		assert_eq!(decoded.0, 3u32);
1479
1480		assert_eq!(<WrapperOpaque<[u8; 63]>>::max_encoded_len(), 63 + 1);
1481		assert_eq!(
1482			<WrapperOpaque<[u8; 63]>>::max_encoded_len(),
1483			WrapperOpaque([0u8; 63]).encode().len()
1484		);
1485
1486		assert_eq!(<WrapperOpaque<[u8; 64]>>::max_encoded_len(), 64 + 2);
1487		assert_eq!(
1488			<WrapperOpaque<[u8; 64]>>::max_encoded_len(),
1489			WrapperOpaque([0u8; 64]).encode().len()
1490		);
1491
1492		assert_eq!(
1493			<WrapperOpaque<[u8; 2usize.pow(14) - 1]>>::max_encoded_len(),
1494			2usize.pow(14) - 1 + 2
1495		);
1496		assert_eq!(<WrapperOpaque<[u8; 2usize.pow(14)]>>::max_encoded_len(), 2usize.pow(14) + 4);
1497
1498		let data = 4u64;
1499		// Ensure that we check that the `Vec<u8>` is consumed completely on decode.
1500		assert!(WrapperOpaque::<u32>::decode(&mut &data.encode().encode()[..]).is_err());
1501	}
1502
1503	#[test]
1504	fn test_keep_opaque_wrapper() {
1505		let data = 3u32.encode().encode();
1506
1507		let keep_opaque = WrapperKeepOpaque::<u32>::decode(&mut &data[..]).unwrap();
1508		keep_opaque.try_decode().unwrap();
1509
1510		let data = WrapperOpaque(50u32).encode();
1511		let decoded = WrapperKeepOpaque::<u32>::decode(&mut &data[..]).unwrap();
1512		let data = decoded.encode();
1513		WrapperOpaque::<u32>::decode(&mut &data[..]).unwrap();
1514	}
1515
1516	#[test]
1517	fn defensive_min_works() {
1518		assert_eq!(10, 10_u32.defensive_min(11_u32));
1519		assert_eq!(10, 10_u32.defensive_min(10_u32));
1520	}
1521
1522	#[test]
1523	#[cfg(debug_assertions)]
1524	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMin\"")]
1525	fn defensive_min_panics() {
1526		10_u32.defensive_min(9_u32);
1527	}
1528
1529	#[test]
1530	fn defensive_strict_min_works() {
1531		assert_eq!(10, 10_u32.defensive_strict_min(11_u32));
1532		assert_eq!(9, 9_u32.defensive_strict_min(10_u32));
1533	}
1534
1535	#[test]
1536	#[cfg(debug_assertions)]
1537	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMin strict\"")]
1538	fn defensive_strict_min_panics() {
1539		9_u32.defensive_strict_min(9_u32);
1540	}
1541
1542	#[test]
1543	fn defensive_max_works() {
1544		assert_eq!(11, 11_u32.defensive_max(10_u32));
1545		assert_eq!(10, 10_u32.defensive_max(10_u32));
1546	}
1547
1548	#[test]
1549	#[cfg(debug_assertions)]
1550	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMax\"")]
1551	fn defensive_max_panics() {
1552		9_u32.defensive_max(10_u32);
1553	}
1554
1555	#[test]
1556	fn defensive_strict_max_works() {
1557		assert_eq!(11, 11_u32.defensive_strict_max(10_u32));
1558		assert_eq!(10, 10_u32.defensive_strict_max(9_u32));
1559	}
1560
1561	#[test]
1562	#[cfg(debug_assertions)]
1563	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMax strict\"")]
1564	fn defensive_strict_max_panics() {
1565		9_u32.defensive_strict_max(9_u32);
1566	}
1567}