frame_support/traits/
misc.rs

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