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