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, ConstU128, ConstU16, ConstU32,
32	ConstU64, ConstU8, 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		frame_support::__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		frame_support::__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		frame_support::__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::Extrinsic {
923	/// Get the call of the extrinsic.
924	fn call(&self) -> &Self::Call;
925}
926
927#[cfg(feature = "std")]
928impl<Call, Extra> ExtrinsicCall for sp_runtime::testing::TestXt<Call, Extra>
929where
930	Call: codec::Codec + Sync + Send + TypeInfo,
931	Extra: TypeInfo,
932{
933	fn call(&self) -> &Self::Call {
934		&self.call
935	}
936}
937
938impl<Address, Call, Signature, Extra> ExtrinsicCall
939	for sp_runtime::generic::UncheckedExtrinsic<Address, Call, Signature, Extra>
940where
941	Address: TypeInfo,
942	Call: TypeInfo,
943	Signature: TypeInfo,
944	Extra: sp_runtime::traits::SignedExtension + TypeInfo,
945{
946	fn call(&self) -> &Self::Call {
947		&self.function
948	}
949}
950
951/// Something that can estimate the fee of a (frame-based) call.
952///
953/// Typically, the same pallet that will charge transaction fees will implement this.
954pub trait EstimateCallFee<Call, Balance> {
955	/// Estimate the fee of this call.
956	///
957	/// The dispatch info and the length is deduced from the call. The post info can optionally be
958	/// provided.
959	fn estimate_call_fee(call: &Call, post_info: crate::dispatch::PostDispatchInfo) -> Balance;
960}
961
962// Useful for building mocks.
963#[cfg(feature = "std")]
964impl<Call, Balance: From<u32>, const T: u32> EstimateCallFee<Call, Balance> for ConstU32<T> {
965	fn estimate_call_fee(_: &Call, _: crate::dispatch::PostDispatchInfo) -> Balance {
966		T.into()
967	}
968}
969
970/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
971///
972/// The encoding is the encoding of `T` prepended with the compact encoding of its size in bytes.
973/// Thus the encoded value can be decoded as a `Vec<u8>`.
974#[derive(Debug, Eq, PartialEq, Default, Clone)]
975#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
976pub struct WrapperOpaque<T>(pub T);
977
978impl<T: Encode> EncodeLike for WrapperOpaque<T> {}
979impl<T: Encode> EncodeLike<WrapperKeepOpaque<T>> for WrapperOpaque<T> {}
980
981impl<T: Encode> Encode for WrapperOpaque<T> {
982	fn size_hint(&self) -> usize {
983		self.0.size_hint().saturating_add(<codec::Compact<u32>>::max_encoded_len())
984	}
985
986	fn encode_to<O: codec::Output + ?Sized>(&self, dest: &mut O) {
987		self.0.encode().encode_to(dest);
988	}
989
990	fn encode(&self) -> Vec<u8> {
991		self.0.encode().encode()
992	}
993
994	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
995		self.0.encode().using_encoded(f)
996	}
997}
998
999impl<T: Decode> Decode for WrapperOpaque<T> {
1000	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
1001		Ok(Self(T::decode_all_with_depth_limit(
1002			sp_api::MAX_EXTRINSIC_DEPTH,
1003			&mut &<Vec<u8>>::decode(input)?[..],
1004		)?))
1005	}
1006
1007	fn skip<I: Input>(input: &mut I) -> Result<(), codec::Error> {
1008		<Vec<u8>>::skip(input)
1009	}
1010}
1011
1012impl<T> From<T> for WrapperOpaque<T> {
1013	fn from(t: T) -> Self {
1014		Self(t)
1015	}
1016}
1017
1018impl<T: MaxEncodedLen> MaxEncodedLen for WrapperOpaque<T> {
1019	fn max_encoded_len() -> usize {
1020		let t_max_len = T::max_encoded_len();
1021
1022		// See scale encoding: https://docs.substrate.io/reference/scale-codec/
1023		if t_max_len < 64 {
1024			t_max_len + 1
1025		} else if t_max_len < 2usize.pow(14) {
1026			t_max_len + 2
1027		} else if t_max_len < 2usize.pow(30) {
1028			t_max_len + 4
1029		} else {
1030			<codec::Compact<u32>>::max_encoded_len().saturating_add(T::max_encoded_len())
1031		}
1032	}
1033}
1034
1035impl<T: TypeInfo + 'static> TypeInfo for WrapperOpaque<T> {
1036	type Identity = Self;
1037	fn type_info() -> Type {
1038		Type::builder()
1039			.path(Path::new("WrapperOpaque", module_path!()))
1040			.type_params(vec![TypeParameter::new("T", Some(meta_type::<T>()))])
1041			.composite(
1042				Fields::unnamed()
1043					.field(|f| f.compact::<u32>())
1044					.field(|f| f.ty::<T>().type_name("T")),
1045			)
1046	}
1047}
1048
1049/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
1050///
1051/// This type is similar to [`WrapperOpaque`], but it differs in the way it stores the type `T`.
1052/// While [`WrapperOpaque`] stores the decoded type, the [`WrapperKeepOpaque`] stores the type only
1053/// in its opaque format, aka as a `Vec<u8>`. To access the real type `T` [`Self::try_decode`] needs
1054/// to be used.
1055#[derive(Debug, Eq, PartialEq, Default, Clone)]
1056pub struct WrapperKeepOpaque<T> {
1057	data: Vec<u8>,
1058	_phantom: core::marker::PhantomData<T>,
1059}
1060
1061impl<T: Decode> WrapperKeepOpaque<T> {
1062	/// Try to decode the wrapped type from the inner `data`.
1063	///
1064	/// Returns `None` if the decoding failed.
1065	pub fn try_decode(&self) -> Option<T> {
1066		T::decode_all_with_depth_limit(sp_api::MAX_EXTRINSIC_DEPTH, &mut &self.data[..]).ok()
1067	}
1068
1069	/// Returns the length of the encoded `T`.
1070	pub fn encoded_len(&self) -> usize {
1071		self.data.len()
1072	}
1073
1074	/// Returns the encoded data.
1075	pub fn encoded(&self) -> &[u8] {
1076		&self.data
1077	}
1078
1079	/// Create from the given encoded `data`.
1080	pub fn from_encoded(data: Vec<u8>) -> Self {
1081		Self { data, _phantom: core::marker::PhantomData }
1082	}
1083}
1084
1085impl<T: Encode> EncodeLike for WrapperKeepOpaque<T> {}
1086impl<T: Encode> EncodeLike<WrapperOpaque<T>> for WrapperKeepOpaque<T> {}
1087
1088impl<T: Encode> Encode for WrapperKeepOpaque<T> {
1089	fn size_hint(&self) -> usize {
1090		self.data.len() + codec::Compact::<u32>::compact_len(&(self.data.len() as u32))
1091	}
1092
1093	fn encode_to<O: codec::Output + ?Sized>(&self, dest: &mut O) {
1094		self.data.encode_to(dest);
1095	}
1096
1097	fn encode(&self) -> Vec<u8> {
1098		self.data.encode()
1099	}
1100
1101	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1102		self.data.using_encoded(f)
1103	}
1104}
1105
1106impl<T: Decode> Decode for WrapperKeepOpaque<T> {
1107	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
1108		Ok(Self { data: Vec::<u8>::decode(input)?, _phantom: core::marker::PhantomData })
1109	}
1110
1111	fn skip<I: Input>(input: &mut I) -> Result<(), codec::Error> {
1112		<Vec<u8>>::skip(input)
1113	}
1114}
1115
1116impl<T: MaxEncodedLen> MaxEncodedLen for WrapperKeepOpaque<T> {
1117	fn max_encoded_len() -> usize {
1118		WrapperOpaque::<T>::max_encoded_len()
1119	}
1120}
1121
1122impl<T: TypeInfo + 'static> TypeInfo for WrapperKeepOpaque<T> {
1123	type Identity = Self;
1124	fn type_info() -> Type {
1125		Type::builder()
1126			.path(Path::new("WrapperKeepOpaque", module_path!()))
1127			.type_params(vec![TypeParameter::new("T", Some(meta_type::<T>()))])
1128			.composite(
1129				Fields::unnamed()
1130					.field(|f| f.compact::<u32>())
1131					.field(|f| f.ty::<T>().type_name("T")),
1132			)
1133	}
1134}
1135
1136/// A interface for looking up preimages from their hash on chain.
1137pub trait PreimageProvider<Hash> {
1138	/// Returns whether a preimage exists for a given hash.
1139	///
1140	/// A value of `true` implies that `get_preimage` is `Some`.
1141	fn have_preimage(hash: &Hash) -> bool;
1142
1143	/// Returns the preimage for a given hash.
1144	fn get_preimage(hash: &Hash) -> Option<Vec<u8>>;
1145
1146	/// Returns whether a preimage request exists for a given hash.
1147	fn preimage_requested(hash: &Hash) -> bool;
1148
1149	/// Request that someone report a preimage. Providers use this to optimise the economics for
1150	/// preimage reporting.
1151	fn request_preimage(hash: &Hash);
1152
1153	/// Cancel a previous preimage request.
1154	fn unrequest_preimage(hash: &Hash);
1155}
1156
1157impl<Hash> PreimageProvider<Hash> for () {
1158	fn have_preimage(_: &Hash) -> bool {
1159		false
1160	}
1161	fn get_preimage(_: &Hash) -> Option<Vec<u8>> {
1162		None
1163	}
1164	fn preimage_requested(_: &Hash) -> bool {
1165		false
1166	}
1167	fn request_preimage(_: &Hash) {}
1168	fn unrequest_preimage(_: &Hash) {}
1169}
1170
1171/// A interface for managing preimages to hashes on chain.
1172///
1173/// Note that this API does not assume any underlying user is calling, and thus
1174/// does not handle any preimage ownership or fees. Other system level logic that
1175/// uses this API should implement that on their own side.
1176pub trait PreimageRecipient<Hash>: PreimageProvider<Hash> {
1177	/// Maximum size of a preimage.
1178	type MaxSize: Get<u32>;
1179
1180	/// Store the bytes of a preimage on chain infallible due to the bounded type.
1181	fn note_preimage(bytes: crate::BoundedVec<u8, Self::MaxSize>);
1182
1183	/// Clear a previously noted preimage. This is infallible and should be treated more like a
1184	/// hint - if it was not previously noted or if it is now requested, then this will not do
1185	/// anything.
1186	fn unnote_preimage(hash: &Hash);
1187}
1188
1189impl<Hash> PreimageRecipient<Hash> for () {
1190	type MaxSize = ();
1191	fn note_preimage(_: crate::BoundedVec<u8, Self::MaxSize>) {}
1192	fn unnote_preimage(_: &Hash) {}
1193}
1194
1195/// Trait for touching/creating an asset account with a deposit taken from a designated depositor
1196/// specified by the client.
1197///
1198/// Ensures that transfers to the touched account will succeed without being denied by the account
1199/// creation requirements. For example, it is useful for the account creation of non-sufficient
1200/// assets when its system account may not have the free consumer reference required for it. If
1201/// there is no risk of failing to meet those requirements, the touch operation can be a no-op, as
1202/// is common for native assets.
1203pub trait AccountTouch<AssetId, AccountId> {
1204	/// The type for currency units of the deposit.
1205	type Balance;
1206
1207	/// The deposit amount of a native currency required for touching an account of the `asset`.
1208	fn deposit_required(asset: AssetId) -> Self::Balance;
1209
1210	/// Check if an account for a given asset should be touched to meet the existence requirements.
1211	fn should_touch(asset: AssetId, who: &AccountId) -> bool;
1212
1213	/// Create an account for `who` of the `asset` with a deposit taken from the `depositor`.
1214	fn touch(asset: AssetId, who: &AccountId, depositor: &AccountId) -> DispatchResult;
1215}
1216
1217#[cfg(test)]
1218mod test {
1219	use super::*;
1220	use core::marker::PhantomData;
1221	use sp_core::bounded::{BoundedSlice, BoundedVec};
1222
1223	#[test]
1224	fn defensive_assert_works() {
1225		defensive_assert!(true);
1226		defensive_assert!(true,);
1227		defensive_assert!(true, "must work");
1228		defensive_assert!(true, "must work",);
1229	}
1230
1231	#[test]
1232	#[cfg(debug_assertions)]
1233	#[should_panic(expected = "Defensive failure has been triggered!: \"1 == 0\": \"Must fail\"")]
1234	fn defensive_assert_panics() {
1235		defensive_assert!(1 == 0, "Must fail");
1236	}
1237
1238	#[test]
1239	#[cfg(not(debug_assertions))]
1240	fn defensive_assert_does_not_panic() {
1241		defensive_assert!(1 == 0, "Must fail");
1242	}
1243
1244	#[test]
1245	#[cfg(not(debug_assertions))]
1246	fn defensive_saturating_accrue_works() {
1247		let mut v = 1_u32;
1248		v.defensive_saturating_accrue(2);
1249		assert_eq!(v, 3);
1250		v.defensive_saturating_accrue(u32::MAX);
1251		assert_eq!(v, u32::MAX);
1252		v.defensive_saturating_accrue(1);
1253		assert_eq!(v, u32::MAX);
1254	}
1255
1256	#[test]
1257	#[cfg(debug_assertions)]
1258	#[should_panic(expected = "Defensive")]
1259	fn defensive_saturating_accrue_panics() {
1260		let mut v = u32::MAX;
1261		v.defensive_saturating_accrue(1); // defensive failure
1262	}
1263
1264	#[test]
1265	#[cfg(not(debug_assertions))]
1266	fn defensive_saturating_reduce_works() {
1267		let mut v = u32::MAX;
1268		v.defensive_saturating_reduce(3);
1269		assert_eq!(v, u32::MAX - 3);
1270		v.defensive_saturating_reduce(u32::MAX);
1271		assert_eq!(v, 0);
1272		v.defensive_saturating_reduce(1);
1273		assert_eq!(v, 0);
1274	}
1275
1276	#[test]
1277	#[cfg(debug_assertions)]
1278	#[should_panic(expected = "Defensive")]
1279	fn defensive_saturating_reduce_panics() {
1280		let mut v = 0_u32;
1281		v.defensive_saturating_reduce(1); // defensive failure
1282	}
1283
1284	#[test]
1285	#[cfg(not(debug_assertions))]
1286	fn defensive_saturating_inc_works() {
1287		let mut v = 0_u32;
1288		for i in 1..10 {
1289			v.defensive_saturating_inc();
1290			assert_eq!(v, i);
1291		}
1292		v += u32::MAX - 10;
1293		v.defensive_saturating_inc();
1294		assert_eq!(v, u32::MAX);
1295		v.defensive_saturating_inc();
1296		assert_eq!(v, u32::MAX);
1297	}
1298
1299	#[test]
1300	#[cfg(debug_assertions)]
1301	#[should_panic(expected = "Defensive")]
1302	fn defensive_saturating_inc_panics() {
1303		let mut v = u32::MAX;
1304		v.defensive_saturating_inc(); // defensive failure
1305	}
1306
1307	#[test]
1308	#[cfg(not(debug_assertions))]
1309	fn defensive_saturating_dec_works() {
1310		let mut v = u32::MAX;
1311		for i in 1..10 {
1312			v.defensive_saturating_dec();
1313			assert_eq!(v, u32::MAX - i);
1314		}
1315		v -= u32::MAX - 10;
1316		v.defensive_saturating_dec();
1317		assert_eq!(v, 0);
1318		v.defensive_saturating_dec();
1319		assert_eq!(v, 0);
1320	}
1321
1322	#[test]
1323	#[cfg(debug_assertions)]
1324	#[should_panic(expected = "Defensive")]
1325	fn defensive_saturating_dec_panics() {
1326		let mut v = 0_u32;
1327		v.defensive_saturating_dec(); // defensive failure
1328	}
1329
1330	#[test]
1331	#[cfg(not(debug_assertions))]
1332	fn defensive_truncating_from_vec_defensive_works() {
1333		let unbound = vec![1u32, 2];
1334		let bound = BoundedVec::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1335		assert_eq!(bound, vec![1u32]);
1336	}
1337
1338	#[test]
1339	#[cfg(not(debug_assertions))]
1340	fn defensive_truncating_from_slice_defensive_works() {
1341		let unbound = &[1u32, 2];
1342		let bound = BoundedSlice::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1343		assert_eq!(bound, &[1u32][..]);
1344	}
1345
1346	#[test]
1347	#[cfg(debug_assertions)]
1348	#[should_panic(
1349		expected = "Defensive failure has been triggered!: \"DefensiveTruncateFrom truncating\""
1350	)]
1351	fn defensive_truncating_from_vec_defensive_panics() {
1352		let unbound = vec![1u32, 2];
1353		let _ = BoundedVec::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1354	}
1355
1356	#[test]
1357	#[cfg(debug_assertions)]
1358	#[should_panic(
1359		expected = "Defensive failure has been triggered!: \"DefensiveTruncateFrom truncating\""
1360	)]
1361	fn defensive_truncating_from_slice_defensive_panics() {
1362		let unbound = &[1u32, 2];
1363		let _ = BoundedSlice::<u32, ConstU32<1>>::defensive_truncate_from(unbound);
1364	}
1365
1366	#[test]
1367	fn defensive_truncate_from_vec_works() {
1368		let unbound = vec![1u32, 2, 3];
1369		let bound = BoundedVec::<u32, ConstU32<3>>::defensive_truncate_from(unbound.clone());
1370		assert_eq!(bound, unbound);
1371	}
1372
1373	#[test]
1374	fn defensive_truncate_from_slice_works() {
1375		let unbound = [1u32, 2, 3];
1376		let bound = BoundedSlice::<u32, ConstU32<3>>::defensive_truncate_from(&unbound);
1377		assert_eq!(bound, &unbound[..]);
1378	}
1379
1380	#[derive(Encode, Decode)]
1381	enum NestedType {
1382		Nested(Box<Self>),
1383		Done,
1384	}
1385
1386	#[test]
1387	fn test_opaque_wrapper_decode_limit() {
1388		let limit = sp_api::MAX_EXTRINSIC_DEPTH as usize;
1389		let mut ok_bytes = vec![0u8; limit];
1390		ok_bytes.push(1u8);
1391		let mut err_bytes = vec![0u8; limit + 1];
1392		err_bytes.push(1u8);
1393		assert!(<WrapperOpaque<NestedType>>::decode(&mut &ok_bytes.encode()[..]).is_ok());
1394		assert!(<WrapperOpaque<NestedType>>::decode(&mut &err_bytes.encode()[..]).is_err());
1395
1396		let ok_keep_opaque = WrapperKeepOpaque { data: ok_bytes, _phantom: PhantomData };
1397		let err_keep_opaque = WrapperKeepOpaque { data: err_bytes, _phantom: PhantomData };
1398
1399		assert!(<WrapperKeepOpaque<NestedType>>::try_decode(&ok_keep_opaque).is_some());
1400		assert!(<WrapperKeepOpaque<NestedType>>::try_decode(&err_keep_opaque).is_none());
1401	}
1402
1403	#[test]
1404	fn test_opaque_wrapper() {
1405		let encoded = WrapperOpaque(3u32).encode();
1406		assert_eq!(encoded, [codec::Compact(4u32).encode(), 3u32.to_le_bytes().to_vec()].concat());
1407		let vec_u8 = <Vec<u8>>::decode(&mut &encoded[..]).unwrap();
1408		let decoded_from_vec_u8 = u32::decode(&mut &vec_u8[..]).unwrap();
1409		assert_eq!(decoded_from_vec_u8, 3u32);
1410		let decoded = <WrapperOpaque<u32>>::decode(&mut &encoded[..]).unwrap();
1411		assert_eq!(decoded.0, 3u32);
1412
1413		assert_eq!(<WrapperOpaque<[u8; 63]>>::max_encoded_len(), 63 + 1);
1414		assert_eq!(
1415			<WrapperOpaque<[u8; 63]>>::max_encoded_len(),
1416			WrapperOpaque([0u8; 63]).encode().len()
1417		);
1418
1419		assert_eq!(<WrapperOpaque<[u8; 64]>>::max_encoded_len(), 64 + 2);
1420		assert_eq!(
1421			<WrapperOpaque<[u8; 64]>>::max_encoded_len(),
1422			WrapperOpaque([0u8; 64]).encode().len()
1423		);
1424
1425		assert_eq!(
1426			<WrapperOpaque<[u8; 2usize.pow(14) - 1]>>::max_encoded_len(),
1427			2usize.pow(14) - 1 + 2
1428		);
1429		assert_eq!(<WrapperOpaque<[u8; 2usize.pow(14)]>>::max_encoded_len(), 2usize.pow(14) + 4);
1430
1431		let data = 4u64;
1432		// Ensure that we check that the `Vec<u8>` is consumed completely on decode.
1433		assert!(WrapperOpaque::<u32>::decode(&mut &data.encode().encode()[..]).is_err());
1434	}
1435
1436	#[test]
1437	fn test_keep_opaque_wrapper() {
1438		let data = 3u32.encode().encode();
1439
1440		let keep_opaque = WrapperKeepOpaque::<u32>::decode(&mut &data[..]).unwrap();
1441		keep_opaque.try_decode().unwrap();
1442
1443		let data = WrapperOpaque(50u32).encode();
1444		let decoded = WrapperKeepOpaque::<u32>::decode(&mut &data[..]).unwrap();
1445		let data = decoded.encode();
1446		WrapperOpaque::<u32>::decode(&mut &data[..]).unwrap();
1447	}
1448
1449	#[test]
1450	fn defensive_min_works() {
1451		assert_eq!(10, 10_u32.defensive_min(11_u32));
1452		assert_eq!(10, 10_u32.defensive_min(10_u32));
1453	}
1454
1455	#[test]
1456	#[cfg(debug_assertions)]
1457	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMin\"")]
1458	fn defensive_min_panics() {
1459		10_u32.defensive_min(9_u32);
1460	}
1461
1462	#[test]
1463	fn defensive_strict_min_works() {
1464		assert_eq!(10, 10_u32.defensive_strict_min(11_u32));
1465		assert_eq!(9, 9_u32.defensive_strict_min(10_u32));
1466	}
1467
1468	#[test]
1469	#[cfg(debug_assertions)]
1470	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMin strict\"")]
1471	fn defensive_strict_min_panics() {
1472		9_u32.defensive_strict_min(9_u32);
1473	}
1474
1475	#[test]
1476	fn defensive_max_works() {
1477		assert_eq!(11, 11_u32.defensive_max(10_u32));
1478		assert_eq!(10, 10_u32.defensive_max(10_u32));
1479	}
1480
1481	#[test]
1482	#[cfg(debug_assertions)]
1483	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMax\"")]
1484	fn defensive_max_panics() {
1485		9_u32.defensive_max(10_u32);
1486	}
1487
1488	#[test]
1489	fn defensive_strict_max_works() {
1490		assert_eq!(11, 11_u32.defensive_strict_max(10_u32));
1491		assert_eq!(10, 10_u32.defensive_strict_max(9_u32));
1492	}
1493
1494	#[test]
1495	#[cfg(debug_assertions)]
1496	#[should_panic(expected = "Defensive failure has been triggered!: \"DefensiveMax strict\"")]
1497	fn defensive_strict_max_panics() {
1498		9_u32.defensive_strict_max(9_u32);
1499	}
1500}