referrerpolicy=no-referrer-when-downgrade

sp_runtime/traits/
mod.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//! Primitives for the runtime modules.
19
20use crate::{
21	generic::Digest,
22	scale_info::{StaticTypeInfo, TypeInfo},
23	transaction_validity::{
24		TransactionSource, TransactionValidity, TransactionValidityError, UnknownTransaction,
25		ValidTransaction,
26	},
27	DispatchResult, KeyTypeId, OpaqueExtrinsic,
28};
29use alloc::vec::Vec;
30use codec::{
31	Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, FullCodec, HasCompact, MaxEncodedLen,
32};
33#[doc(hidden)]
34pub use core::{fmt::Debug, marker::PhantomData};
35#[cfg(feature = "serde")]
36use serde::{de::DeserializeOwned, Deserialize, Serialize};
37use sp_application_crypto::AppCrypto;
38pub use sp_arithmetic::traits::{
39	checked_pow, ensure_pow, AtLeast32Bit, AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedDiv,
40	CheckedMul, CheckedShl, CheckedShr, CheckedSub, Ensure, EnsureAdd, EnsureAddAssign, EnsureDiv,
41	EnsureDivAssign, EnsureFixedPointNumber, EnsureFrom, EnsureInto, EnsureMul, EnsureMulAssign,
42	EnsureOp, EnsureOpAssign, EnsureSub, EnsureSubAssign, IntegerSquareRoot, One,
43	SaturatedConversion, Saturating, UniqueSaturatedFrom, UniqueSaturatedInto, Zero,
44};
45use sp_core::{self, storage::StateVersion, Hasher, TypeId, U256};
46#[doc(hidden)]
47pub use sp_core::{
48	parameter_types, ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstInt,
49	ConstU128, ConstU16, ConstU32, ConstU64, ConstU8, ConstUint, Get, GetDefault, TryCollect,
50	TypedGet,
51};
52#[cfg(feature = "std")]
53use std::fmt::Display;
54#[cfg(feature = "std")]
55use std::str::FromStr;
56
57pub mod transaction_extension;
58pub mod vers_tx_ext;
59pub use transaction_extension::{
60	DispatchTransaction, Implication, ImplicationParts, TransactionExtension,
61	TransactionExtensionMetadata, TxBaseImplication, ValidateResult,
62};
63pub use vers_tx_ext::{
64	DecodeWithVersion, DecodeWithVersionWithMemTracking, ExtensionVariant, InvalidVersion,
65	MultiVersion, Pipeline, PipelineAtVers, PipelineMetadataBuilder, PipelineVersion,
66};
67
68/// A lazy value.
69pub trait Lazy<T: ?Sized> {
70	/// Get a reference to the underlying value.
71	///
72	/// This will compute the value if the function is invoked for the first time.
73	fn get(&mut self) -> &T;
74}
75
76impl<'a> Lazy<[u8]> for &'a [u8] {
77	fn get(&mut self) -> &[u8] {
78		self
79	}
80}
81
82/// Some type that is able to be collapsed into an account ID. It is not possible to recreate the
83/// original value from the account ID.
84pub trait IdentifyAccount {
85	/// The account ID that this can be transformed into.
86	type AccountId;
87	/// Transform into an account.
88	fn into_account(self) -> Self::AccountId;
89}
90
91impl IdentifyAccount for sp_core::ed25519::Public {
92	type AccountId = Self;
93	fn into_account(self) -> Self {
94		self
95	}
96}
97
98impl IdentifyAccount for sp_core::sr25519::Public {
99	type AccountId = Self;
100	fn into_account(self) -> Self {
101		self
102	}
103}
104
105impl IdentifyAccount for sp_core::ecdsa::Public {
106	type AccountId = Self;
107	fn into_account(self) -> Self {
108		self
109	}
110}
111
112#[cfg(feature = "bls-experimental")]
113impl IdentifyAccount for sp_core::ecdsa_bls381::Public {
114	type AccountId = Self;
115	fn into_account(self) -> Self {
116		self
117	}
118}
119
120/// Means of signature verification.
121pub trait Verify {
122	/// Type of the signer.
123	type Signer: IdentifyAccount;
124	/// Verify a signature.
125	///
126	/// Return `true` if signature is valid for the value.
127	fn verify<L: Lazy<[u8]>>(
128		&self,
129		msg: L,
130		signer: &<Self::Signer as IdentifyAccount>::AccountId,
131	) -> bool;
132}
133
134impl Verify for sp_core::ed25519::Signature {
135	type Signer = sp_core::ed25519::Public;
136
137	fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &sp_core::ed25519::Public) -> bool {
138		sp_io::crypto::ed25519_verify(self, msg.get(), signer)
139	}
140}
141
142impl Verify for sp_core::sr25519::Signature {
143	type Signer = sp_core::sr25519::Public;
144
145	fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &sp_core::sr25519::Public) -> bool {
146		sp_io::crypto::sr25519_verify(self, msg.get(), signer)
147	}
148}
149
150impl Verify for sp_core::ecdsa::Signature {
151	type Signer = sp_core::ecdsa::Public;
152	fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &sp_core::ecdsa::Public) -> bool {
153		if !sp_core::ecdsa::is_signature_normalized(self.as_ref()) {
154			return false;
155		}
156		match sp_io::crypto::secp256k1_ecdsa_recover_compressed(
157			self.as_ref(),
158			&sp_io::hashing::blake2_256(msg.get()),
159		) {
160			Ok(pubkey) => signer.0 == pubkey,
161			_ => false,
162		}
163	}
164}
165
166#[cfg(feature = "bls-experimental")]
167impl Verify for sp_core::ecdsa_bls381::Signature {
168	type Signer = sp_core::ecdsa_bls381::Public;
169	fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &sp_core::ecdsa_bls381::Public) -> bool {
170		<sp_core::ecdsa_bls381::Pair as sp_core::Pair>::verify(self, msg.get(), signer)
171	}
172}
173
174/// Means of signature verification of an application key.
175pub trait AppVerify {
176	/// Type of the signer.
177	type AccountId;
178	/// Verify a signature. Return `true` if signature is valid for the value.
179	fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &Self::AccountId) -> bool;
180}
181
182impl<
183		S: Verify<Signer = <<T as AppCrypto>::Public as sp_application_crypto::AppPublic>::Generic>
184			+ From<T>,
185		T: sp_application_crypto::Wraps<Inner = S>
186			+ sp_application_crypto::AppCrypto
187			+ sp_application_crypto::AppSignature
188			+ AsRef<S>
189			+ AsMut<S>
190			+ From<S>,
191	> AppVerify for T
192where
193	<S as Verify>::Signer: IdentifyAccount<AccountId = <S as Verify>::Signer>,
194	<<T as AppCrypto>::Public as sp_application_crypto::AppPublic>::Generic: IdentifyAccount<
195		AccountId = <<T as AppCrypto>::Public as sp_application_crypto::AppPublic>::Generic,
196	>,
197{
198	type AccountId = <T as AppCrypto>::Public;
199	fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &<T as AppCrypto>::Public) -> bool {
200		use sp_application_crypto::IsWrappedBy;
201		let inner: &S = self.as_ref();
202		let inner_pubkey =
203			<<T as AppCrypto>::Public as sp_application_crypto::AppPublic>::Generic::from_ref(
204				signer,
205			);
206		Verify::verify(inner, msg, inner_pubkey)
207	}
208}
209
210/// An error type that indicates that the origin is invalid.
211#[derive(Encode, Decode, Debug)]
212pub struct BadOrigin;
213
214impl From<BadOrigin> for &'static str {
215	fn from(_: BadOrigin) -> &'static str {
216		"Bad origin"
217	}
218}
219
220/// An error that indicates that a lookup failed.
221#[derive(Encode, Decode, Debug)]
222pub struct LookupError;
223
224impl From<LookupError> for &'static str {
225	fn from(_: LookupError) -> &'static str {
226		"Can not lookup"
227	}
228}
229
230impl From<LookupError> for TransactionValidityError {
231	fn from(_: LookupError) -> Self {
232		UnknownTransaction::CannotLookup.into()
233	}
234}
235
236/// Means of changing one type into another in a manner dependent on the source type.
237pub trait Lookup {
238	/// Type to lookup from.
239	type Source;
240	/// Type to lookup into.
241	type Target;
242	/// Attempt a lookup.
243	fn lookup(&self, s: Self::Source) -> Result<Self::Target, LookupError>;
244}
245
246/// Means of changing one type into another in a manner dependent on the source type.
247/// This variant is different to `Lookup` in that it doesn't (can cannot) require any
248/// context.
249pub trait StaticLookup {
250	/// Type to lookup from.
251	type Source: Codec + Clone + PartialEq + Debug + TypeInfo;
252	/// Type to lookup into.
253	type Target;
254	/// Attempt a lookup.
255	fn lookup(s: Self::Source) -> Result<Self::Target, LookupError>;
256	/// Convert from Target back to Source.
257	fn unlookup(t: Self::Target) -> Self::Source;
258}
259
260/// A lookup implementation returning the input value.
261#[derive(Clone, Copy, PartialEq, Eq)]
262pub struct IdentityLookup<T>(PhantomData<T>);
263impl<T> Default for IdentityLookup<T> {
264	fn default() -> Self {
265		Self(PhantomData::<T>::default())
266	}
267}
268
269impl<T: Codec + Clone + PartialEq + Debug + TypeInfo> StaticLookup for IdentityLookup<T> {
270	type Source = T;
271	type Target = T;
272	fn lookup(x: T) -> Result<T, LookupError> {
273		Ok(x)
274	}
275	fn unlookup(x: T) -> T {
276		x
277	}
278}
279
280impl<T> Lookup for IdentityLookup<T> {
281	type Source = T;
282	type Target = T;
283	fn lookup(&self, x: T) -> Result<T, LookupError> {
284		Ok(x)
285	}
286}
287
288/// A lookup implementation returning the `AccountId` from a `MultiAddress`.
289pub struct AccountIdLookup<AccountId, AccountIndex>(PhantomData<(AccountId, AccountIndex)>);
290impl<AccountId, AccountIndex> StaticLookup for AccountIdLookup<AccountId, AccountIndex>
291where
292	AccountId: Codec + Clone + PartialEq + Debug,
293	AccountIndex: Codec + Clone + PartialEq + Debug,
294	crate::MultiAddress<AccountId, AccountIndex>: Codec + StaticTypeInfo,
295{
296	type Source = crate::MultiAddress<AccountId, AccountIndex>;
297	type Target = AccountId;
298	fn lookup(x: Self::Source) -> Result<Self::Target, LookupError> {
299		match x {
300			crate::MultiAddress::Id(i) => Ok(i),
301			_ => Err(LookupError),
302		}
303	}
304	fn unlookup(x: Self::Target) -> Self::Source {
305		crate::MultiAddress::Id(x)
306	}
307}
308
309/// Perform a StaticLookup where there are multiple lookup sources of the same type.
310impl<A, B> StaticLookup for (A, B)
311where
312	A: StaticLookup,
313	B: StaticLookup<Source = A::Source, Target = A::Target>,
314{
315	type Source = A::Source;
316	type Target = A::Target;
317
318	fn lookup(x: Self::Source) -> Result<Self::Target, LookupError> {
319		A::lookup(x.clone()).or_else(|_| B::lookup(x))
320	}
321	fn unlookup(x: Self::Target) -> Self::Source {
322		A::unlookup(x)
323	}
324}
325
326/// Extensible conversion trait. Generic over only source type, with destination type being
327/// associated.
328pub trait Morph<A> {
329	/// The type into which `A` is mutated.
330	type Outcome;
331
332	/// Make conversion.
333	fn morph(a: A) -> Self::Outcome;
334}
335
336/// A structure that performs identity conversion.
337impl<T> Morph<T> for Identity {
338	type Outcome = T;
339	fn morph(a: T) -> T {
340		a
341	}
342}
343
344/// Extensible conversion trait. Generic over only source type, with destination type being
345/// associated.
346pub trait TryMorph<A> {
347	/// The type into which `A` is mutated.
348	type Outcome;
349
350	/// Make conversion.
351	fn try_morph(a: A) -> Result<Self::Outcome, ()>;
352}
353
354/// A structure that performs identity conversion.
355impl<T> TryMorph<T> for Identity {
356	type Outcome = T;
357	fn try_morph(a: T) -> Result<T, ()> {
358		Ok(a)
359	}
360}
361
362/// Implementation of `Morph` which converts between types using `Into`.
363pub struct MorphInto<T>(core::marker::PhantomData<T>);
364impl<T, A: Into<T>> Morph<A> for MorphInto<T> {
365	type Outcome = T;
366	fn morph(a: A) -> T {
367		a.into()
368	}
369}
370
371/// Implementation of `TryMorph` which attempts to convert between types using `TryInto`.
372pub struct TryMorphInto<T>(core::marker::PhantomData<T>);
373impl<T, A: TryInto<T>> TryMorph<A> for TryMorphInto<T> {
374	type Outcome = T;
375	fn try_morph(a: A) -> Result<T, ()> {
376		a.try_into().map_err(|_| ())
377	}
378}
379
380/// Implementation of `Morph` to retrieve just the first element of a tuple.
381pub struct TakeFirst;
382impl<T1> Morph<(T1,)> for TakeFirst {
383	type Outcome = T1;
384	fn morph(a: (T1,)) -> T1 {
385		a.0
386	}
387}
388impl<T1, T2> Morph<(T1, T2)> for TakeFirst {
389	type Outcome = T1;
390	fn morph(a: (T1, T2)) -> T1 {
391		a.0
392	}
393}
394impl<T1, T2, T3> Morph<(T1, T2, T3)> for TakeFirst {
395	type Outcome = T1;
396	fn morph(a: (T1, T2, T3)) -> T1 {
397		a.0
398	}
399}
400impl<T1, T2, T3, T4> Morph<(T1, T2, T3, T4)> for TakeFirst {
401	type Outcome = T1;
402	fn morph(a: (T1, T2, T3, T4)) -> T1 {
403		a.0
404	}
405}
406
407/// Create a `Morph` and/or `TryMorph` impls with a simple closure-like expression.
408///
409/// # Examples
410///
411/// ```
412/// # use sp_runtime::{morph_types, traits::{Morph, TryMorph, TypedGet, ConstU32}};
413/// # use sp_arithmetic::traits::CheckedSub;
414///
415/// morph_types! {
416///    /// Replace by some other value; produce both `Morph` and `TryMorph` implementations
417///    pub type Replace<V: TypedGet> = |_| -> V::Type { V::get() };
418///    /// A private `Morph` implementation to reduce a `u32` by 10.
419///    type ReduceU32ByTen: Morph = |r: u32| -> u32 { r - 10 };
420///    /// A `TryMorph` implementation to reduce a scalar by a particular amount, checking for
421///    /// underflow.
422///    pub type CheckedReduceBy<N: TypedGet>: TryMorph = |r: N::Type| -> Result<N::Type, ()> {
423///        r.checked_sub(&N::get()).ok_or(())
424///    } where N::Type: CheckedSub;
425/// }
426///
427/// trait Config {
428///    type TestMorph1: Morph<u32>;
429///    type TestTryMorph1: TryMorph<u32>;
430///    type TestMorph2: Morph<u32>;
431///    type TestTryMorph2: TryMorph<u32>;
432/// }
433///
434/// struct Runtime;
435/// impl Config for Runtime {
436///    type TestMorph1 = Replace<ConstU32<42>>;
437///    type TestTryMorph1 = Replace<ConstU32<42>>;
438///    type TestMorph2 = ReduceU32ByTen;
439///    type TestTryMorph2 = CheckedReduceBy<ConstU32<10>>;
440/// }
441/// ```
442#[macro_export]
443macro_rules! morph_types {
444	(
445		@DECL $( #[doc = $doc:expr] )* $vq:vis $name:ident ()
446	) => {
447		$( #[doc = $doc] )* $vq struct $name;
448	};
449	(
450		@DECL $( #[doc = $doc:expr] )* $vq:vis $name:ident ( $( $bound_id:ident ),+ )
451	) => {
452		$( #[doc = $doc] )*
453		$vq struct $name < $($bound_id,)* > ( $crate::traits::PhantomData< ( $($bound_id,)* ) > ) ;
454	};
455	(
456		@IMPL $name:ty : ( $( $bounds:tt )* ) ( $( $where:tt )* )
457		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
458	) => {
459		impl<$($bounds)*> $crate::traits::Morph<$var_type> for $name $( $where )? {
460			type Outcome = $outcome;
461			fn morph($var: $var_type) -> Self::Outcome { $( $ex )* }
462		}
463	};
464	(
465		@IMPL_TRY $name:ty : ( $( $bounds:tt )* ) ( $( $where:tt )* )
466		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
467	) => {
468		impl<$($bounds)*> $crate::traits::TryMorph<$var_type> for $name $( $where )? {
469			type Outcome = $outcome;
470			fn try_morph($var: $var_type) -> Result<Self::Outcome, ()> { $( $ex )* }
471		}
472	};
473	(
474		@IMPL $name:ty : () ( $( $where:tt )* )
475		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
476	) => {
477		impl $crate::traits::Morph<$var_type> for $name $( $where )? {
478			type Outcome = $outcome;
479			fn morph($var: $var_type) -> Self::Outcome { $( $ex )* }
480		}
481	};
482	(
483		@IMPL_TRY $name:ty : () ( $( $where:tt )* )
484		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
485	) => {
486		impl $crate::traits::TryMorph<$var_type> for $name $( $where )? {
487			type Outcome = $outcome;
488			fn try_morph($var: $var_type) -> Result<Self::Outcome, ()> { $( $ex )* }
489		}
490	};
491	(
492		@IMPL_BOTH $name:ty : ( $( $bounds:tt )* ) ( $( $where:tt )* )
493		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
494	) => {
495		morph_types! {
496			@IMPL $name : ($($bounds)*) ($($where)*)
497			= |$var: $var_type| -> $outcome { $( $ex )* }
498		}
499		morph_types! {
500			@IMPL_TRY $name : ($($bounds)*) ($($where)*)
501			= |$var: $var_type| -> $outcome { Ok({$( $ex )*}) }
502		}
503	};
504
505	(
506		$( #[doc = $doc:expr] )* $vq:vis type $name:ident
507		$( < $( $bound_id:ident $( : $bound_head:path $( | $bound_tail:path )* )? ),+ > )?
508		$(: $type:tt)?
509		= |_| -> $outcome:ty { $( $ex:expr )* };
510		$( $rest:tt )*
511	) => {
512		morph_types! {
513			$( #[doc = $doc] )* $vq type $name
514			$( < $( $bound_id $( : $bound_head $( | $bound_tail )* )? ),+ > )?
515			EXTRA_GENERIC(X)
516			$(: $type)?
517			= |_x: X| -> $outcome { $( $ex )* };
518			$( $rest )*
519		}
520	};
521	(
522		$( #[doc = $doc:expr] )* $vq:vis type $name:ident
523		$( < $( $bound_id:ident $( : $bound_head:path $( | $bound_tail:path )* )? ),+ > )?
524		$( EXTRA_GENERIC ($extra:ident) )?
525		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
526		$( where $( $where_path:ty : $where_bound_head:path $( | $where_bound_tail:path )* ),* )?;
527		$( $rest:tt )*
528	) => {
529		morph_types! { @DECL $( #[doc = $doc] )* $vq $name ( $( $( $bound_id ),+ )? ) }
530		morph_types! {
531			@IMPL_BOTH $name $( < $( $bound_id ),* > )? :
532			( $( $( $bound_id $( : $bound_head $( + $bound_tail )* )? , )+ )? $( $extra )? )
533			( $( where $( $where_path : $where_bound_head $( + $where_bound_tail )* ),* )? )
534			= |$var: $var_type| -> $outcome { $( $ex )* }
535		}
536		morph_types!{ $($rest)* }
537	};
538	(
539		$( #[doc = $doc:expr] )* $vq:vis type $name:ident
540		$( < $( $bound_id:ident $( : $bound_head:path $( | $bound_tail:path )* )? ),+ > )?
541		$( EXTRA_GENERIC ($extra:ident) )?
542		: Morph
543		= |$var:ident: $var_type:ty| -> $outcome:ty { $( $ex:expr )* }
544		$( where $( $where_path:ty : $where_bound_head:path $( | $where_bound_tail:path )* ),* )?;
545		$( $rest:tt )*
546	) => {
547		morph_types! { @DECL $( #[doc = $doc] )* $vq $name ( $( $( $bound_id ),+ )? ) }
548		morph_types! {
549			@IMPL $name $( < $( $bound_id ),* > )? :
550			( $( $( $bound_id $( : $bound_head $( + $bound_tail )* )? , )+ )? $( $extra )? )
551			( $( where $( $where_path : $where_bound_head $( + $where_bound_tail )* ),* )? )
552			= |$var: $var_type| -> $outcome { $( $ex )* }
553		}
554		morph_types!{ $($rest)* }
555	};
556	(
557		$( #[doc = $doc:expr] )* $vq:vis type $name:ident
558		$( < $( $bound_id:ident $( : $bound_head:path $( | $bound_tail:path )* )? ),+ > )?
559		$( EXTRA_GENERIC ($extra:ident) )?
560		: TryMorph
561		= |$var:ident: $var_type:ty| -> Result<$outcome:ty, ()> { $( $ex:expr )* }
562		$( where $( $where_path:ty : $where_bound_head:path $( | $where_bound_tail:path )* ),* )?;
563		$( $rest:tt )*
564	) => {
565		morph_types! { @DECL $( #[doc = $doc] )* $vq $name ( $( $( $bound_id ),+ )? ) }
566		morph_types! {
567			@IMPL_TRY $name $( < $( $bound_id ),* > )? :
568			( $( $( $bound_id $( : $bound_head $( + $bound_tail )* )? , )+ )? $( $extra )? )
569			( $( where $( $where_path : $where_bound_head $( + $where_bound_tail )* ),* )? )
570			= |$var: $var_type| -> $outcome { $( $ex )* }
571		}
572		morph_types!{ $($rest)* }
573	};
574	() => {}
575}
576
577morph_types! {
578	/// Morpher to disregard the source value and replace with another.
579	pub type Replace<V: TypedGet> = |_| -> V::Type { V::get() };
580
581	/// Morpher to disregard the source value and replace with the default of `V`.
582	pub type ReplaceWithDefault<V: Default> = |_| -> V { Default::default() };
583
584	/// Mutator which reduces a scalar by a particular amount.
585	pub type ReduceBy<N: TypedGet> = |r: N::Type| -> N::Type {
586		r.checked_sub(&N::get()).unwrap_or(Zero::zero())
587	} where N::Type: CheckedSub | Zero;
588
589	/// A `TryMorph` implementation to reduce a scalar by a particular amount, checking for
590	/// underflow.
591	pub type CheckedReduceBy<N: TypedGet>: TryMorph = |r: N::Type| -> Result<N::Type, ()> {
592		r.checked_sub(&N::get()).ok_or(())
593	} where N::Type: CheckedSub;
594
595	/// A `TryMorph` implementation to enforce an upper limit for a result of the outer morphed type.
596	pub type MorphWithUpperLimit<L: TypedGet, M>: TryMorph = |r: L::Type| -> Result<L::Type, ()> {
597		M::try_morph(r).map(|m| m.min(L::get()))
598	} where L::Type: Ord, M: TryMorph<L::Type, Outcome = L::Type>;
599}
600
601/// Infallible conversion trait. Generic over both source and destination types.
602pub trait Convert<A, B> {
603	/// Make conversion.
604	fn convert(a: A) -> B;
605}
606
607impl<A, B: Default> Convert<A, B> for () {
608	fn convert(_: A) -> B {
609		Default::default()
610	}
611}
612
613/// Reversing infallible conversion trait. Generic over both source and destination types.
614///
615/// This specifically reverses the conversion.
616pub trait ConvertBack<A, B>: Convert<A, B> {
617	/// Make conversion back.
618	fn convert_back(b: B) -> A;
619}
620
621/// Fallible conversion trait returning an [Option]. Generic over both source and destination types.
622pub trait MaybeConvert<A, B> {
623	/// Attempt to make conversion.
624	fn maybe_convert(a: A) -> Option<B>;
625}
626
627#[impl_trait_for_tuples::impl_for_tuples(30)]
628impl<A: Clone, B> MaybeConvert<A, B> for Tuple {
629	fn maybe_convert(a: A) -> Option<B> {
630		for_tuples!( #(
631			match Tuple::maybe_convert(a.clone()) {
632				Some(b) => return Some(b),
633				None => {},
634			}
635		)* );
636		None
637	}
638}
639
640/// Reversing fallible conversion trait returning an [Option]. Generic over both source and
641/// destination types.
642pub trait MaybeConvertBack<A, B>: MaybeConvert<A, B> {
643	/// Attempt to make conversion back.
644	fn maybe_convert_back(b: B) -> Option<A>;
645}
646
647#[impl_trait_for_tuples::impl_for_tuples(30)]
648impl<A: Clone, B: Clone> MaybeConvertBack<A, B> for Tuple {
649	fn maybe_convert_back(b: B) -> Option<A> {
650		for_tuples!( #(
651			match Tuple::maybe_convert_back(b.clone()) {
652				Some(a) => return Some(a),
653				None => {},
654			}
655		)* );
656		None
657	}
658}
659
660/// Fallible conversion trait which returns the argument in the case of being unable to convert.
661/// Generic over both source and destination types.
662pub trait TryConvert<A, B> {
663	/// Attempt to make conversion. If returning [Result::Err], the inner must always be `a`.
664	fn try_convert(a: A) -> Result<B, A>;
665}
666
667#[impl_trait_for_tuples::impl_for_tuples(30)]
668impl<A, B> TryConvert<A, B> for Tuple {
669	fn try_convert(a: A) -> Result<B, A> {
670		for_tuples!( #(
671			let a = match Tuple::try_convert(a) {
672				Ok(b) => return Ok(b),
673				Err(a) => a,
674			};
675		)* );
676		Err(a)
677	}
678}
679
680/// Reversing fallible conversion trait which returns the argument in the case of being unable to
681/// convert back. Generic over both source and destination types.
682pub trait TryConvertBack<A, B>: TryConvert<A, B> {
683	/// Attempt to make conversion back. If returning [Result::Err], the inner must always be `b`.
684
685	fn try_convert_back(b: B) -> Result<A, B>;
686}
687
688#[impl_trait_for_tuples::impl_for_tuples(30)]
689impl<A, B> TryConvertBack<A, B> for Tuple {
690	fn try_convert_back(b: B) -> Result<A, B> {
691		for_tuples!( #(
692			let b = match Tuple::try_convert_back(b) {
693				Ok(a) => return Ok(a),
694				Err(b) => b,
695			};
696		)* );
697		Err(b)
698	}
699}
700
701/// Definition for a bi-directional, fallible conversion between two types.
702pub trait MaybeEquivalence<A, B> {
703	/// Attempt to convert reference of `A` into value of `B`, returning `None` if not possible.
704	fn convert(a: &A) -> Option<B>;
705	/// Attempt to convert reference of `B` into value of `A`, returning `None` if not possible.
706	fn convert_back(b: &B) -> Option<A>;
707}
708
709#[impl_trait_for_tuples::impl_for_tuples(30)]
710impl<A, B> MaybeEquivalence<A, B> for Tuple {
711	fn convert(a: &A) -> Option<B> {
712		for_tuples!( #(
713			match Tuple::convert(a) {
714				Some(b) => return Some(b),
715				None => {},
716			}
717		)* );
718		None
719	}
720	fn convert_back(b: &B) -> Option<A> {
721		for_tuples!( #(
722			match Tuple::convert_back(b) {
723				Some(a) => return Some(a),
724				None => {},
725			}
726		)* );
727		None
728	}
729}
730
731/// Adapter which turns a [Get] implementation into a [Convert] implementation which always returns
732/// in the same value no matter the input.
733pub struct ConvertToValue<T>(core::marker::PhantomData<T>);
734impl<X, Y, T: Get<Y>> Convert<X, Y> for ConvertToValue<T> {
735	fn convert(_: X) -> Y {
736		T::get()
737	}
738}
739impl<X, Y, T: Get<Y>> MaybeConvert<X, Y> for ConvertToValue<T> {
740	fn maybe_convert(_: X) -> Option<Y> {
741		Some(T::get())
742	}
743}
744impl<X, Y, T: Get<Y>> MaybeConvertBack<X, Y> for ConvertToValue<T> {
745	fn maybe_convert_back(_: Y) -> Option<X> {
746		None
747	}
748}
749impl<X, Y, T: Get<Y>> TryConvert<X, Y> for ConvertToValue<T> {
750	fn try_convert(_: X) -> Result<Y, X> {
751		Ok(T::get())
752	}
753}
754impl<X, Y, T: Get<Y>> TryConvertBack<X, Y> for ConvertToValue<T> {
755	fn try_convert_back(y: Y) -> Result<X, Y> {
756		Err(y)
757	}
758}
759impl<X, Y, T: Get<Y>> MaybeEquivalence<X, Y> for ConvertToValue<T> {
760	fn convert(_: &X) -> Option<Y> {
761		Some(T::get())
762	}
763	fn convert_back(_: &Y) -> Option<X> {
764		None
765	}
766}
767
768/// A structure that performs identity conversion.
769pub struct Identity;
770impl<T> Convert<T, T> for Identity {
771	fn convert(a: T) -> T {
772		a
773	}
774}
775impl<T> ConvertBack<T, T> for Identity {
776	fn convert_back(a: T) -> T {
777		a
778	}
779}
780impl<T> MaybeConvert<T, T> for Identity {
781	fn maybe_convert(a: T) -> Option<T> {
782		Some(a)
783	}
784}
785impl<T> MaybeConvertBack<T, T> for Identity {
786	fn maybe_convert_back(a: T) -> Option<T> {
787		Some(a)
788	}
789}
790impl<T> TryConvert<T, T> for Identity {
791	fn try_convert(a: T) -> Result<T, T> {
792		Ok(a)
793	}
794}
795impl<T> TryConvertBack<T, T> for Identity {
796	fn try_convert_back(a: T) -> Result<T, T> {
797		Ok(a)
798	}
799}
800impl<T: Clone> MaybeEquivalence<T, T> for Identity {
801	fn convert(a: &T) -> Option<T> {
802		Some(a.clone())
803	}
804	fn convert_back(a: &T) -> Option<T> {
805		Some(a.clone())
806	}
807}
808
809/// A structure that performs standard conversion using the standard Rust conversion traits.
810pub struct ConvertInto;
811impl<A: Into<B>, B> Convert<A, B> for ConvertInto {
812	fn convert(a: A) -> B {
813		a.into()
814	}
815}
816impl<A: Into<B>, B> MaybeConvert<A, B> for ConvertInto {
817	fn maybe_convert(a: A) -> Option<B> {
818		Some(a.into())
819	}
820}
821impl<A: Into<B>, B: Into<A>> MaybeConvertBack<A, B> for ConvertInto {
822	fn maybe_convert_back(b: B) -> Option<A> {
823		Some(b.into())
824	}
825}
826impl<A: Into<B>, B> TryConvert<A, B> for ConvertInto {
827	fn try_convert(a: A) -> Result<B, A> {
828		Ok(a.into())
829	}
830}
831impl<A: Into<B>, B: Into<A>> TryConvertBack<A, B> for ConvertInto {
832	fn try_convert_back(b: B) -> Result<A, B> {
833		Ok(b.into())
834	}
835}
836impl<A: Clone + Into<B>, B: Clone + Into<A>> MaybeEquivalence<A, B> for ConvertInto {
837	fn convert(a: &A) -> Option<B> {
838		Some(a.clone().into())
839	}
840	fn convert_back(b: &B) -> Option<A> {
841		Some(b.clone().into())
842	}
843}
844
845/// A structure that performs standard conversion using the standard Rust conversion traits.
846pub struct TryConvertInto;
847impl<A: Clone + TryInto<B>, B> MaybeConvert<A, B> for TryConvertInto {
848	fn maybe_convert(a: A) -> Option<B> {
849		a.clone().try_into().ok()
850	}
851}
852impl<A: Clone + TryInto<B>, B: Clone + TryInto<A>> MaybeConvertBack<A, B> for TryConvertInto {
853	fn maybe_convert_back(b: B) -> Option<A> {
854		b.clone().try_into().ok()
855	}
856}
857impl<A: Clone + TryInto<B>, B> TryConvert<A, B> for TryConvertInto {
858	fn try_convert(a: A) -> Result<B, A> {
859		a.clone().try_into().map_err(|_| a)
860	}
861}
862impl<A: Clone + TryInto<B>, B: Clone + TryInto<A>> TryConvertBack<A, B> for TryConvertInto {
863	fn try_convert_back(b: B) -> Result<A, B> {
864		b.clone().try_into().map_err(|_| b)
865	}
866}
867impl<A: Clone + TryInto<B>, B: Clone + TryInto<A>> MaybeEquivalence<A, B> for TryConvertInto {
868	fn convert(a: &A) -> Option<B> {
869		a.clone().try_into().ok()
870	}
871	fn convert_back(b: &B) -> Option<A> {
872		b.clone().try_into().ok()
873	}
874}
875
876/// Convenience type to work around the highly unergonomic syntax needed
877/// to invoke the functions of overloaded generic traits, in this case
878/// `TryFrom` and `TryInto`.
879pub trait CheckedConversion {
880	/// Convert from a value of `T` into an equivalent instance of `Option<Self>`.
881	///
882	/// This just uses `TryFrom` internally but with this
883	/// variant you can provide the destination type using turbofish syntax
884	/// in case Rust happens not to assume the correct type.
885	fn checked_from<T>(t: T) -> Option<Self>
886	where
887		Self: TryFrom<T>,
888	{
889		<Self as TryFrom<T>>::try_from(t).ok()
890	}
891	/// Consume self to return `Some` equivalent value of `Option<T>`.
892	///
893	/// This just uses `TryInto` internally but with this
894	/// variant you can provide the destination type using turbofish syntax
895	/// in case Rust happens not to assume the correct type.
896	fn checked_into<T>(self) -> Option<T>
897	where
898		Self: TryInto<T>,
899	{
900		<Self as TryInto<T>>::try_into(self).ok()
901	}
902}
903impl<T: Sized> CheckedConversion for T {}
904
905/// Multiply and divide by a number that isn't necessarily the same type. Basically just the same
906/// as `Mul` and `Div` except it can be used for all basic numeric types.
907pub trait Scale<Other> {
908	/// The output type of the product of `self` and `Other`.
909	type Output;
910
911	/// @return the product of `self` and `other`.
912	fn mul(self, other: Other) -> Self::Output;
913
914	/// @return the integer division of `self` and `other`.
915	fn div(self, other: Other) -> Self::Output;
916
917	/// @return the modulo remainder of `self` and `other`.
918	fn rem(self, other: Other) -> Self::Output;
919}
920macro_rules! impl_scale {
921	($self:ty, $other:ty) => {
922		impl Scale<$other> for $self {
923			type Output = Self;
924			fn mul(self, other: $other) -> Self::Output {
925				self * (other as Self)
926			}
927			fn div(self, other: $other) -> Self::Output {
928				self / (other as Self)
929			}
930			fn rem(self, other: $other) -> Self::Output {
931				self % (other as Self)
932			}
933		}
934	};
935}
936impl_scale!(u128, u128);
937impl_scale!(u128, u64);
938impl_scale!(u128, u32);
939impl_scale!(u128, u16);
940impl_scale!(u128, u8);
941impl_scale!(u64, u64);
942impl_scale!(u64, u32);
943impl_scale!(u64, u16);
944impl_scale!(u64, u8);
945impl_scale!(u32, u32);
946impl_scale!(u32, u16);
947impl_scale!(u32, u8);
948impl_scale!(u16, u16);
949impl_scale!(u16, u8);
950impl_scale!(u8, u8);
951
952/// Trait for things that can be clear (have no bits set). For numeric types, essentially the same
953/// as `Zero`.
954pub trait Clear {
955	/// True iff no bits are set.
956	fn is_clear(&self) -> bool;
957
958	/// Return the value of Self that is clear.
959	fn clear() -> Self;
960}
961
962impl<T: Default + Eq + PartialEq> Clear for T {
963	fn is_clear(&self) -> bool {
964		*self == Self::clear()
965	}
966	fn clear() -> Self {
967		Default::default()
968	}
969}
970
971/// A meta trait for all bit ops.
972pub trait SimpleBitOps:
973	Sized
974	+ Clear
975	+ core::ops::BitOr<Self, Output = Self>
976	+ core::ops::BitXor<Self, Output = Self>
977	+ core::ops::BitAnd<Self, Output = Self>
978{
979}
980impl<
981		T: Sized
982			+ Clear
983			+ core::ops::BitOr<Self, Output = Self>
984			+ core::ops::BitXor<Self, Output = Self>
985			+ core::ops::BitAnd<Self, Output = Self>,
986	> SimpleBitOps for T
987{
988}
989
990/// Abstraction around hashing
991// Stupid bug in the Rust compiler believes derived
992// traits must be fulfilled by all type parameters.
993pub trait Hash:
994	'static
995	+ MaybeSerializeDeserialize
996	+ Debug
997	+ Clone
998	+ Eq
999	+ PartialEq
1000	+ Hasher<Out = <Self as Hash>::Output>
1001{
1002	/// The hash type produced.
1003	type Output: HashOutput;
1004
1005	/// Produce the hash of some byte-slice.
1006	fn hash(s: &[u8]) -> Self::Output {
1007		<Self as Hasher>::hash(s)
1008	}
1009
1010	/// Produce the hash of some codec-encodable value.
1011	fn hash_of<S: Encode>(s: &S) -> Self::Output {
1012		Encode::using_encoded(s, <Self as Hasher>::hash)
1013	}
1014
1015	/// The ordered Patricia tree root of the given `input`.
1016	fn ordered_trie_root(input: Vec<Vec<u8>>, state_version: StateVersion) -> Self::Output;
1017
1018	/// The Patricia tree root of the given mapping.
1019	fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, state_version: StateVersion) -> Self::Output;
1020}
1021
1022/// Super trait with all the attributes for a hashing output.
1023pub trait HashOutput:
1024	Member
1025	+ MaybeSerializeDeserialize
1026	+ MaybeDisplay
1027	+ MaybeFromStr
1028	+ Debug
1029	+ core::hash::Hash
1030	+ AsRef<[u8]>
1031	+ AsMut<[u8]>
1032	+ Copy
1033	+ Ord
1034	+ Default
1035	+ Encode
1036	+ Decode
1037	+ DecodeWithMemTracking
1038	+ EncodeLike
1039	+ MaxEncodedLen
1040	+ TypeInfo
1041{
1042}
1043
1044impl<T> HashOutput for T where
1045	T: Member
1046		+ MaybeSerializeDeserialize
1047		+ MaybeDisplay
1048		+ MaybeFromStr
1049		+ Debug
1050		+ core::hash::Hash
1051		+ AsRef<[u8]>
1052		+ AsMut<[u8]>
1053		+ Copy
1054		+ Ord
1055		+ Default
1056		+ Encode
1057		+ Decode
1058		+ DecodeWithMemTracking
1059		+ EncodeLike
1060		+ MaxEncodedLen
1061		+ TypeInfo
1062{
1063}
1064
1065/// Blake2-256 Hash implementation.
1066#[derive(PartialEq, Eq, Clone, Debug, TypeInfo)]
1067#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1068pub struct BlakeTwo256;
1069
1070impl Hasher for BlakeTwo256 {
1071	type Out = sp_core::H256;
1072	type StdHasher = hash256_std_hasher::Hash256StdHasher;
1073	const LENGTH: usize = 32;
1074
1075	fn hash(s: &[u8]) -> Self::Out {
1076		sp_io::hashing::blake2_256(s).into()
1077	}
1078}
1079
1080impl Hash for BlakeTwo256 {
1081	type Output = sp_core::H256;
1082
1083	fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
1084		sp_io::trie::blake2_256_ordered_root(input, version)
1085	}
1086
1087	fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
1088		sp_io::trie::blake2_256_root(input, version)
1089	}
1090}
1091
1092/// Keccak-256 Hash implementation.
1093#[derive(PartialEq, Eq, Clone, Debug, TypeInfo)]
1094#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1095pub struct Keccak256;
1096
1097impl Hasher for Keccak256 {
1098	type Out = sp_core::H256;
1099	type StdHasher = hash256_std_hasher::Hash256StdHasher;
1100	const LENGTH: usize = 32;
1101
1102	fn hash(s: &[u8]) -> Self::Out {
1103		sp_io::hashing::keccak_256(s).into()
1104	}
1105}
1106
1107impl Hash for Keccak256 {
1108	type Output = sp_core::H256;
1109
1110	fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
1111		sp_io::trie::keccak_256_ordered_root(input, version)
1112	}
1113
1114	fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
1115		sp_io::trie::keccak_256_root(input, version)
1116	}
1117}
1118
1119/// Something that can be checked for equality and printed out to a debug channel if bad.
1120pub trait CheckEqual {
1121	/// Perform the equality check.
1122	fn check_equal(&self, other: &Self);
1123}
1124
1125impl CheckEqual for sp_core::H256 {
1126	#[cfg(feature = "std")]
1127	fn check_equal(&self, other: &Self) {
1128		use sp_core::hexdisplay::HexDisplay;
1129		if self != other {
1130			println!(
1131				"Hash: given={}, expected={}",
1132				HexDisplay::from(self.as_fixed_bytes()),
1133				HexDisplay::from(other.as_fixed_bytes()),
1134			);
1135		}
1136	}
1137
1138	#[cfg(not(feature = "std"))]
1139	fn check_equal(&self, other: &Self) {
1140		if self != other {
1141			log::error!("Hash not equal: given={:?}, expected={:?}", self, other);
1142		}
1143	}
1144}
1145
1146impl CheckEqual for super::generic::DigestItem {
1147	#[cfg(feature = "std")]
1148	fn check_equal(&self, other: &Self) {
1149		if self != other {
1150			println!("DigestItem: given={:?}, expected={:?}", self, other);
1151		}
1152	}
1153
1154	#[cfg(not(feature = "std"))]
1155	fn check_equal(&self, other: &Self) {
1156		if self != other {
1157			log::error!("DigestItem not equal: given={:?}, expected={:?}", self, other);
1158		}
1159	}
1160}
1161
1162sp_core::impl_maybe_marker!(
1163	/// A type that implements Display when in std environment.
1164	trait MaybeDisplay: Display;
1165
1166	/// A type that implements FromStr when in std environment.
1167	trait MaybeFromStr: FromStr;
1168
1169	/// A type that implements Hash when in std environment.
1170	trait MaybeHash: core::hash::Hash;
1171);
1172
1173sp_core::impl_maybe_marker_std_or_serde!(
1174	/// A type that implements Serialize when in std environment or serde feature is activated.
1175	trait MaybeSerialize: Serialize;
1176
1177	/// A type that implements Serialize, DeserializeOwned and Debug when in std environment or serde feature is activated.
1178	trait MaybeSerializeDeserialize: DeserializeOwned, Serialize;
1179);
1180
1181/// A type that can be used in runtime structures.
1182pub trait Member: Send + Sync + Sized + Debug + Eq + PartialEq + Clone + 'static {}
1183impl<T: Send + Sync + Sized + Debug + Eq + PartialEq + Clone + 'static> Member for T {}
1184
1185/// Determine if a `MemberId` is a valid member.
1186pub trait IsMember<MemberId> {
1187	/// Is the given `MemberId` a valid member?
1188	fn is_member(member_id: &MemberId) -> bool;
1189}
1190
1191/// Super trait with all the attributes for a block number.
1192pub trait BlockNumber:
1193	Member
1194	+ MaybeSerializeDeserialize
1195	+ MaybeFromStr
1196	+ Debug
1197	+ core::hash::Hash
1198	+ Copy
1199	+ MaybeDisplay
1200	+ AtLeast32BitUnsigned
1201	+ Into<U256>
1202	+ TryFrom<U256>
1203	+ Default
1204	+ TypeInfo
1205	+ MaxEncodedLen
1206	+ FullCodec
1207	+ DecodeWithMemTracking
1208	+ HasCompact<Type: DecodeWithMemTracking>
1209{
1210}
1211
1212impl<
1213		T: Member
1214			+ MaybeSerializeDeserialize
1215			+ MaybeFromStr
1216			+ Debug
1217			+ core::hash::Hash
1218			+ Copy
1219			+ MaybeDisplay
1220			+ AtLeast32BitUnsigned
1221			+ Into<U256>
1222			+ TryFrom<U256>
1223			+ Default
1224			+ TypeInfo
1225			+ MaxEncodedLen
1226			+ FullCodec
1227			+ DecodeWithMemTracking
1228			+ HasCompact<Type: DecodeWithMemTracking>,
1229	> BlockNumber for T
1230{
1231}
1232
1233/// Something which fulfills the abstract idea of a Substrate header. It has types for a `Number`,
1234/// a `Hash` and a `Hashing`. It provides access to an `extrinsics_root`, `state_root` and
1235/// `parent_hash`, as well as a `digest` and a block `number`.
1236///
1237/// You can also create a `new` one from those fields.
1238pub trait Header:
1239	Clone
1240	+ Send
1241	+ Sync
1242	+ Codec
1243	+ DecodeWithMemTracking
1244	+ Eq
1245	+ MaybeSerialize
1246	+ Debug
1247	+ TypeInfo
1248	+ 'static
1249{
1250	/// Header number.
1251	type Number: BlockNumber;
1252	/// Header hash type
1253	type Hash: HashOutput;
1254	/// Hashing algorithm
1255	type Hashing: Hash<Output = Self::Hash>;
1256
1257	/// Creates new header.
1258	fn new(
1259		number: Self::Number,
1260		extrinsics_root: Self::Hash,
1261		state_root: Self::Hash,
1262		parent_hash: Self::Hash,
1263		digest: Digest,
1264	) -> Self;
1265
1266	/// Returns a reference to the header number.
1267	fn number(&self) -> &Self::Number;
1268	/// Sets the header number.
1269	fn set_number(&mut self, number: Self::Number);
1270
1271	/// Returns a reference to the extrinsics root.
1272	fn extrinsics_root(&self) -> &Self::Hash;
1273	/// Sets the extrinsic root.
1274	fn set_extrinsics_root(&mut self, root: Self::Hash);
1275
1276	/// Returns a reference to the state root.
1277	fn state_root(&self) -> &Self::Hash;
1278	/// Sets the state root.
1279	fn set_state_root(&mut self, root: Self::Hash);
1280
1281	/// Returns a reference to the parent hash.
1282	fn parent_hash(&self) -> &Self::Hash;
1283	/// Sets the parent hash.
1284	fn set_parent_hash(&mut self, hash: Self::Hash);
1285
1286	/// Returns a reference to the digest.
1287	fn digest(&self) -> &Digest;
1288	/// Get a mutable reference to the digest.
1289	fn digest_mut(&mut self) -> &mut Digest;
1290
1291	/// Returns the hash of the header.
1292	fn hash(&self) -> Self::Hash {
1293		<Self::Hashing as Hash>::hash_of(self)
1294	}
1295}
1296
1297// Something that provides the Header Type. Only for internal usage and should only be used
1298// via `HeaderFor` or `BlockNumberFor`.
1299//
1300// This is needed to fix the "cyclical" issue in loading Header/BlockNumber as part of a
1301// `pallet::call`. Essentially, `construct_runtime` aggregates all calls to create a `RuntimeCall`
1302// that is then used to define `UncheckedExtrinsic`.
1303// ```ignore
1304// pub type UncheckedExtrinsic =
1305// 	generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
1306// ```
1307// This `UncheckedExtrinsic` is supplied to the `Block`.
1308// ```ignore
1309// pub type Block = generic::Block<Header, UncheckedExtrinsic>;
1310// ```
1311// So, if we do not create a trait outside of `Block` that doesn't have `Extrinsic`, we go into a
1312// recursive loop leading to a build error.
1313//
1314// Note that this is a workaround for a compiler bug and should be removed when the compiler
1315// bug is fixed.
1316#[doc(hidden)]
1317pub trait HeaderProvider {
1318	/// Header type.
1319	type HeaderT: Header;
1320}
1321
1322/// An extrinsic that can be lazily decoded.
1323pub trait LazyExtrinsic: Sized {
1324	/// Try to decode the lazy extrinsic.
1325	///
1326	/// Usually an encoded extrinsic is composed of 2 parts:
1327	/// - a `Compact<u32>` prefix (`len)`
1328	/// - a blob of size `len`
1329	/// This method expects to receive just the blob as a byte slice.
1330	/// The size of the blob is the `len`.
1331	fn decode_unprefixed(data: &[u8]) -> Result<Self, codec::Error>;
1332}
1333
1334/// A Substrate block that allows us to lazily decode its extrinsics.
1335pub trait LazyBlock: Debug + Encode + Decode + Sized {
1336	/// Type for the decoded extrinsics.
1337	type Extrinsic: LazyExtrinsic;
1338	/// Header type.
1339	type Header: Header;
1340
1341	/// Returns a reference to the header.
1342	fn header(&self) -> &Self::Header;
1343
1344	/// Returns a mut reference to the header.
1345	fn header_mut(&mut self) -> &mut Self::Header;
1346
1347	/// Returns an iterator over all extrinsics.
1348	///
1349	/// The extrinsics are lazily decoded (if possible) as they are pulled by the iterator.
1350	fn extrinsics(&self) -> impl Iterator<Item = Result<Self::Extrinsic, codec::Error>>;
1351}
1352
1353/// Something which fulfills the abstract idea of a Substrate block. It has types for
1354/// `Extrinsic` pieces of information as well as a `Header`.
1355///
1356/// You can get an iterator over each of the `extrinsics` and retrieve the `header`.
1357pub trait Block:
1358	HeaderProvider<HeaderT = Self::Header>
1359	+ Into<Self::LazyBlock>
1360	+ EncodeLike<Self::LazyBlock>
1361	+ Clone
1362	+ Send
1363	+ Sync
1364	+ Codec
1365	+ DecodeWithMemTracking
1366	+ Eq
1367	+ MaybeSerialize
1368	+ Debug
1369	+ 'static
1370{
1371	/// Type for extrinsics.
1372	type Extrinsic: Member + Codec + ExtrinsicLike + MaybeSerialize + Into<OpaqueExtrinsic>;
1373	/// Header type.
1374	type Header: Header<Hash = Self::Hash> + MaybeSerializeDeserialize;
1375	/// Block hash type.
1376	type Hash: HashOutput;
1377
1378	/// A shadow structure which allows us to lazily decode the extrinsics.
1379	/// The `LazyBlock` must have the same encoded representation as the `Block`.
1380	type LazyBlock: LazyBlock<Extrinsic = Self::Extrinsic, Header = Self::Header> + EncodeLike<Self>;
1381
1382	/// Returns a reference to the header.
1383	fn header(&self) -> &Self::Header;
1384	/// Returns a reference to the list of extrinsics.
1385	fn extrinsics(&self) -> &[Self::Extrinsic];
1386	/// Split the block into header and list of extrinsics.
1387	fn deconstruct(self) -> (Self::Header, Vec<Self::Extrinsic>);
1388	/// Creates new block from header and extrinsics.
1389	fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self;
1390	/// Returns the hash of the block.
1391	fn hash(&self) -> Self::Hash {
1392		<<Self::Header as Header>::Hashing as Hash>::hash_of(self.header())
1393	}
1394}
1395
1396/// Something that acts like an `Extrinsic`.
1397#[deprecated = "Use `ExtrinsicLike` along with the `CreateTransaction` trait family instead"]
1398pub trait Extrinsic: Sized {
1399	/// The function call.
1400	type Call: TypeInfo;
1401
1402	/// The payload we carry for signed extrinsics.
1403	///
1404	/// Usually it will contain a `Signature` and
1405	/// may include some additional data that are specific to signed
1406	/// extrinsics.
1407	type SignaturePayload: SignaturePayload;
1408
1409	/// Is this `Extrinsic` signed?
1410	/// If no information are available about signed/unsigned, `None` should be returned.
1411	fn is_signed(&self) -> Option<bool> {
1412		None
1413	}
1414
1415	/// Returns `true` if this `Extrinsic` is bare.
1416	fn is_bare(&self) -> bool {
1417		!self.is_signed().unwrap_or(true)
1418	}
1419
1420	/// Create a new old-school extrinsic, either a bare extrinsic if `_signed_data` is `None` or
1421	/// a signed transaction is it is `Some`.
1422	fn new(_call: Self::Call, _signed_data: Option<Self::SignaturePayload>) -> Option<Self> {
1423		None
1424	}
1425}
1426
1427/// Something that acts like an `Extrinsic`.
1428pub trait ExtrinsicLike: Sized {
1429	/// Is this `Extrinsic` signed?
1430	/// If no information are available about signed/unsigned, `None` should be returned.
1431	#[deprecated = "Use and implement `!is_bare()` instead"]
1432	fn is_signed(&self) -> Option<bool> {
1433		None
1434	}
1435
1436	/// Returns `true` if this `Extrinsic` is bare.
1437	fn is_bare(&self) -> bool {
1438		#[allow(deprecated)]
1439		!self.is_signed().unwrap_or(true)
1440	}
1441}
1442
1443#[allow(deprecated)]
1444impl<T> ExtrinsicLike for T
1445where
1446	T: Extrinsic,
1447{
1448	fn is_signed(&self) -> Option<bool> {
1449		#[allow(deprecated)]
1450		<Self as Extrinsic>::is_signed(&self)
1451	}
1452
1453	fn is_bare(&self) -> bool {
1454		<Self as Extrinsic>::is_bare(&self)
1455	}
1456}
1457
1458/// An extrinsic on which we can get access to call.
1459pub trait ExtrinsicCall: ExtrinsicLike {
1460	/// The type of the call.
1461	type Call;
1462
1463	/// Get a reference to the call of the extrinsic.
1464	fn call(&self) -> &Self::Call;
1465
1466	/// Convert the extrinsic into its call.
1467	fn into_call(self) -> Self::Call;
1468}
1469
1470/// Something that acts like a [`SignaturePayload`](Extrinsic::SignaturePayload) of an
1471/// [`Extrinsic`].
1472pub trait SignaturePayload {
1473	/// The type of the address that signed the extrinsic.
1474	///
1475	/// Particular to a signed extrinsic.
1476	type SignatureAddress: TypeInfo;
1477
1478	/// The signature type of the extrinsic.
1479	///
1480	/// Particular to a signed extrinsic.
1481	type Signature: TypeInfo;
1482
1483	/// The additional data that is specific to the signed extrinsic.
1484	///
1485	/// Particular to a signed extrinsic.
1486	type SignatureExtra: TypeInfo;
1487}
1488
1489impl SignaturePayload for () {
1490	type SignatureAddress = ();
1491	type Signature = ();
1492	type SignatureExtra = ();
1493}
1494
1495/// Implementor is an [`Extrinsic`] and provides metadata about this extrinsic.
1496pub trait ExtrinsicMetadata {
1497	/// The format versions of the `Extrinsic`.
1498	///
1499	/// By format we mean the encoded representation of the `Extrinsic`.
1500	const VERSIONS: &'static [u8];
1501
1502	/// All version of transaction extensions attached to this `Extrinsic`.
1503	///
1504	/// For extrinsic version 4, extrinsics don't specify any version, the pipeline version 0 is
1505	/// used.
1506	/// For extrinsic version 5, bare extrinsics don't specify any version, the pipeline version 0
1507	/// is used.
1508	type TransactionExtensionPipelines;
1509}
1510
1511/// Extract the hashing type for a block.
1512pub type HashingFor<B> = <<B as Block>::Header as Header>::Hashing;
1513/// Extract the number type for a block.
1514pub type NumberFor<B> = <<B as Block>::Header as Header>::Number;
1515/// Extract the digest type for a block.
1516
1517/// A "checkable" piece of information, used by the standard Substrate Executive in order to
1518/// check the validity of a piece of extrinsic information, usually by verifying the signature.
1519/// Implement for pieces of information that require some additional context `Context` in order to
1520/// be checked.
1521pub trait Checkable<Context>: Sized {
1522	/// Returned if `check` succeeds.
1523	type Checked;
1524
1525	/// Check self, given an instance of Context.
1526	fn check(self, c: &Context) -> Result<Self::Checked, TransactionValidityError>;
1527
1528	/// Blindly check self.
1529	///
1530	/// ## WARNING
1531	///
1532	/// DO NOT USE IN PRODUCTION. This is only meant to be used in testing environments. A runtime
1533	/// compiled with `try-runtime` should never be in production. Moreover, the name of this
1534	/// function is deliberately chosen to prevent developers from ever calling it in consensus
1535	/// code-paths.
1536	#[cfg(feature = "try-runtime")]
1537	fn unchecked_into_checked_i_know_what_i_am_doing(
1538		self,
1539		c: &Context,
1540	) -> Result<Self::Checked, TransactionValidityError>;
1541}
1542
1543/// A "checkable" piece of information, used by the standard Substrate Executive in order to
1544/// check the validity of a piece of extrinsic information, usually by verifying the signature.
1545/// Implement for pieces of information that don't require additional context in order to be
1546/// checked.
1547pub trait BlindCheckable: Sized {
1548	/// Returned if `check` succeeds.
1549	type Checked;
1550
1551	/// Check self.
1552	fn check(self) -> Result<Self::Checked, TransactionValidityError>;
1553}
1554
1555// Every `BlindCheckable` is also a `StaticCheckable` for arbitrary `Context`.
1556impl<T: BlindCheckable, Context> Checkable<Context> for T {
1557	type Checked = <Self as BlindCheckable>::Checked;
1558
1559	fn check(self, _c: &Context) -> Result<Self::Checked, TransactionValidityError> {
1560		BlindCheckable::check(self)
1561	}
1562
1563	#[cfg(feature = "try-runtime")]
1564	fn unchecked_into_checked_i_know_what_i_am_doing(
1565		self,
1566		_: &Context,
1567	) -> Result<Self::Checked, TransactionValidityError> {
1568		unreachable!();
1569	}
1570}
1571
1572/// A type that can handle weight refunds.
1573pub trait RefundWeight {
1574	/// Refund some unspent weight.
1575	fn refund(&mut self, weight: sp_weights::Weight);
1576}
1577
1578/// A type that can handle weight refunds and incorporate extension weights into the call weight
1579/// after dispatch.
1580pub trait ExtensionPostDispatchWeightHandler<DispatchInfo>: RefundWeight {
1581	/// Accrue some weight pertaining to the extension.
1582	fn set_extension_weight(&mut self, info: &DispatchInfo);
1583}
1584
1585impl RefundWeight for () {
1586	fn refund(&mut self, _weight: sp_weights::Weight) {}
1587}
1588
1589impl ExtensionPostDispatchWeightHandler<()> for () {
1590	fn set_extension_weight(&mut self, _info: &()) {}
1591}
1592
1593/// A lazy call (module function and argument values) that can be executed via its `dispatch`
1594/// method.
1595pub trait Dispatchable {
1596	/// Every function call from your runtime has an origin, which specifies where the extrinsic was
1597	/// generated from. In the case of a signed extrinsic (transaction), the origin contains an
1598	/// identifier for the caller. The origin can be empty in the case of an inherent extrinsic.
1599	type RuntimeOrigin: Debug;
1600	/// ...
1601	type Config;
1602	/// An opaque set of information attached to the transaction. This could be constructed anywhere
1603	/// down the line in a runtime. The current Substrate runtime uses a struct with the same name
1604	/// to represent the dispatch class and weight.
1605	type Info;
1606	/// Additional information that is returned by `dispatch`. Can be used to supply the caller
1607	/// with information about a `Dispatchable` that is only known post dispatch.
1608	type PostInfo: Eq
1609		+ PartialEq
1610		+ Clone
1611		+ Copy
1612		+ Encode
1613		+ Decode
1614		+ ExtensionPostDispatchWeightHandler<Self::Info>;
1615	/// Actually dispatch this call and return the result of it.
1616	fn dispatch(self, origin: Self::RuntimeOrigin)
1617		-> crate::DispatchResultWithInfo<Self::PostInfo>;
1618}
1619
1620/// Shortcut to reference the `RuntimeOrigin` type of a `Dispatchable`.
1621pub type DispatchOriginOf<T> = <T as Dispatchable>::RuntimeOrigin;
1622/// Shortcut to reference the `Info` type of a `Dispatchable`.
1623pub type DispatchInfoOf<T> = <T as Dispatchable>::Info;
1624/// Shortcut to reference the `PostInfo` type of a `Dispatchable`.
1625pub type PostDispatchInfoOf<T> = <T as Dispatchable>::PostInfo;
1626
1627impl Dispatchable for () {
1628	type RuntimeOrigin = ();
1629	type Config = ();
1630	type Info = ();
1631	type PostInfo = ();
1632	fn dispatch(
1633		self,
1634		_origin: Self::RuntimeOrigin,
1635	) -> crate::DispatchResultWithInfo<Self::PostInfo> {
1636		panic!("This implementation should not be used for actual dispatch.");
1637	}
1638}
1639
1640/// Dispatchable impl containing an arbitrary value which panics if it actually is dispatched.
1641#[derive(Clone, Eq, PartialEq, Encode, Decode, DecodeWithMemTracking, Debug, TypeInfo)]
1642pub struct FakeDispatchable<Inner>(pub Inner);
1643impl<Inner> From<Inner> for FakeDispatchable<Inner> {
1644	fn from(inner: Inner) -> Self {
1645		Self(inner)
1646	}
1647}
1648impl<Inner> FakeDispatchable<Inner> {
1649	/// Take `self` and return the underlying inner value.
1650	pub fn deconstruct(self) -> Inner {
1651		self.0
1652	}
1653}
1654impl<Inner> AsRef<Inner> for FakeDispatchable<Inner> {
1655	fn as_ref(&self) -> &Inner {
1656		&self.0
1657	}
1658}
1659
1660impl<Inner> Dispatchable for FakeDispatchable<Inner> {
1661	type RuntimeOrigin = ();
1662	type Config = ();
1663	type Info = ();
1664	type PostInfo = ();
1665	fn dispatch(
1666		self,
1667		_origin: Self::RuntimeOrigin,
1668	) -> crate::DispatchResultWithInfo<Self::PostInfo> {
1669		panic!("This implementation should not be used for actual dispatch.");
1670	}
1671}
1672
1673/// Runtime Origin which includes a System Origin variant whose `AccountId` is the parameter.
1674pub trait AsSystemOriginSigner<AccountId> {
1675	/// Extract a reference of the inner value of the System `Origin::Signed` variant, if self has
1676	/// that variant.
1677	fn as_system_origin_signer(&self) -> Option<&AccountId>;
1678}
1679
1680/// Interface to differentiate between Runtime Origins authorized to include a transaction into the
1681/// block and dispatch it, and those who aren't.
1682///
1683/// This trait targets transactions, by which we mean extrinsics which are validated through a
1684/// [`TransactionExtension`]. This excludes bare extrinsics (i.e. inherents), which have their call,
1685/// not their origin, validated and authorized.
1686///
1687/// Typically, upon validation or application of a transaction, the origin resulting from the
1688/// transaction extension (see [`TransactionExtension`]) is checked for authorization. The
1689/// transaction is then rejected or applied.
1690///
1691/// In FRAME, an authorized origin is either an `Origin::Signed` System origin or a custom origin
1692/// authorized in a [`TransactionExtension`].
1693pub trait AsTransactionAuthorizedOrigin {
1694	/// Whether the origin is authorized to include a transaction in a block.
1695	///
1696	/// In typical FRAME chains, this function returns `false` if the origin is a System
1697	/// `Origin::None` variant, `true` otherwise, meaning only signed or custom origin resulting
1698	/// from the transaction extension pipeline are authorized.
1699	///
1700	/// NOTE: This function should not be used in the context of bare extrinsics (i.e. inherents),
1701	/// as bare extrinsics do not authorize the origin but rather the call itself, and are not
1702	/// validated through the [`TransactionExtension`] pipeline.
1703	fn is_transaction_authorized(&self) -> bool;
1704}
1705
1706/// Means by which a transaction may be extended. This type embodies both the data and the logic
1707/// that should be additionally associated with the transaction. It should be plain old data.
1708#[deprecated = "Use `TransactionExtension` instead."]
1709pub trait SignedExtension:
1710	Codec + DecodeWithMemTracking + Debug + Sync + Send + Clone + Eq + PartialEq + StaticTypeInfo
1711{
1712	/// Unique identifier of this signed extension.
1713	///
1714	/// This will be exposed in the metadata to identify the signed extension used
1715	/// in an extrinsic.
1716	const IDENTIFIER: &'static str;
1717
1718	/// The type which encodes the sender identity.
1719	type AccountId;
1720
1721	/// The type which encodes the call to be dispatched.
1722	type Call: Dispatchable;
1723
1724	/// Any additional data that will go into the signed payload. This may be created dynamically
1725	/// from the transaction using the `additional_signed` function.
1726	type AdditionalSigned: Codec + TypeInfo;
1727
1728	/// The type that encodes information that can be passed from pre_dispatch to post-dispatch.
1729	type Pre;
1730
1731	/// Construct any additional data that should be in the signed payload of the transaction. Can
1732	/// also perform any pre-signature-verification checks and return an error if needed.
1733	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError>;
1734
1735	/// Validate a signed transaction for the transaction queue.
1736	///
1737	/// This function can be called frequently by the transaction queue,
1738	/// to obtain transaction validity against current state.
1739	/// It should perform all checks that determine a valid transaction,
1740	/// that can pay for its execution and quickly eliminate ones
1741	/// that are stale or incorrect.
1742	///
1743	/// Make sure to perform the same checks in `pre_dispatch` function.
1744	fn validate(
1745		&self,
1746		_who: &Self::AccountId,
1747		_call: &Self::Call,
1748		_info: &DispatchInfoOf<Self::Call>,
1749		_len: usize,
1750	) -> TransactionValidity {
1751		Ok(ValidTransaction::default())
1752	}
1753
1754	/// Do any pre-flight stuff for a signed transaction.
1755	///
1756	/// Make sure to perform the same checks as in [`Self::validate`].
1757	fn pre_dispatch(
1758		self,
1759		who: &Self::AccountId,
1760		call: &Self::Call,
1761		info: &DispatchInfoOf<Self::Call>,
1762		len: usize,
1763	) -> Result<Self::Pre, TransactionValidityError>;
1764
1765	/// Do any post-flight stuff for an extrinsic.
1766	///
1767	/// If the transaction is signed, then `_pre` will contain the output of `pre_dispatch`,
1768	/// and `None` otherwise.
1769	///
1770	/// This gets given the `DispatchResult` `_result` from the extrinsic and can, if desired,
1771	/// introduce a `TransactionValidityError`, causing the block to become invalid for including
1772	/// it.
1773	///
1774	/// WARNING: It is dangerous to return an error here. To do so will fundamentally invalidate the
1775	/// transaction and any block that it is included in, causing the block author to not be
1776	/// compensated for their work in validating the transaction or producing the block so far.
1777	///
1778	/// It can only be used safely when you *know* that the extrinsic is one that can only be
1779	/// introduced by the current block author; generally this implies that it is an inherent and
1780	/// will come from either an offchain-worker or via `InherentData`.
1781	fn post_dispatch(
1782		_pre: Option<Self::Pre>,
1783		_info: &DispatchInfoOf<Self::Call>,
1784		_post_info: &PostDispatchInfoOf<Self::Call>,
1785		_len: usize,
1786		_result: &DispatchResult,
1787	) -> Result<(), TransactionValidityError> {
1788		Ok(())
1789	}
1790
1791	/// Returns the metadata for this signed extension.
1792	///
1793	/// As a [`SignedExtension`] can be a tuple of [`SignedExtension`]s we need to return a `Vec`
1794	/// that holds the metadata of each one. Each individual `SignedExtension` must return
1795	/// *exactly* one [`TransactionExtensionMetadata`].
1796	///
1797	/// This method provides a default implementation that returns a vec containing a single
1798	/// [`TransactionExtensionMetadata`].
1799	fn metadata() -> Vec<TransactionExtensionMetadata> {
1800		alloc::vec![TransactionExtensionMetadata {
1801			identifier: Self::IDENTIFIER,
1802			ty: scale_info::meta_type::<Self>(),
1803			implicit: scale_info::meta_type::<Self::AdditionalSigned>()
1804		}]
1805	}
1806
1807	/// Validate an unsigned transaction for the transaction queue.
1808	///
1809	/// This function can be called frequently by the transaction queue
1810	/// to obtain transaction validity against current state.
1811	/// It should perform all checks that determine a valid unsigned transaction,
1812	/// and quickly eliminate ones that are stale or incorrect.
1813	fn validate_unsigned(
1814		_call: &Self::Call,
1815		_info: &DispatchInfoOf<Self::Call>,
1816		_len: usize,
1817	) -> TransactionValidity {
1818		Ok(ValidTransaction::default())
1819	}
1820
1821	/// Do any pre-flight stuff for an unsigned transaction.
1822	///
1823	/// Note this function by default delegates to `validate_unsigned`, so that
1824	/// all checks performed for the transaction queue are also performed during
1825	/// the dispatch phase (applying the extrinsic).
1826	///
1827	/// If you ever override this function, you need not perform the same validation as in
1828	/// `validate_unsigned`.
1829	fn pre_dispatch_unsigned(
1830		call: &Self::Call,
1831		info: &DispatchInfoOf<Self::Call>,
1832		len: usize,
1833	) -> Result<(), TransactionValidityError> {
1834		Self::validate_unsigned(call, info, len).map(|_| ()).map_err(Into::into)
1835	}
1836}
1837
1838/// An "executable" piece of information, used by the standard Substrate Executive in order to
1839/// enact a piece of extrinsic information by marshalling and dispatching to a named function
1840/// call.
1841///
1842/// Also provides information on to whom this information is attributable and an index that allows
1843/// each piece of attributable information to be disambiguated.
1844///
1845/// IMPORTANT: After validation, in both [validate](Applyable::validate) and
1846/// [apply](Applyable::apply), all transactions should have *some* authorized origin, except for
1847/// inherents. This is necessary in order to protect the chain against spam. If no extension in the
1848/// transaction extension pipeline authorized the transaction with an origin, either a system signed
1849/// origin or a custom origin, then the transaction must be rejected, as the extensions provided in
1850/// substrate which protect the chain, such as `CheckNonce`, `ChargeTransactionPayment` etc., rely
1851/// on the assumption that the system handles system signed transactions, and the pallets handle the
1852/// custom origin that they authorized.
1853pub trait Applyable: Sized + Send + Sync {
1854	/// Type by which we can dispatch. Restricts the `UnsignedValidator` type.
1855	type Call: Dispatchable;
1856
1857	/// Checks to see if this is a valid *transaction*. It returns information on it if so.
1858	///
1859	/// IMPORTANT: Ensure that *some* origin has been authorized after validating the transaction.
1860	/// If no origin was authorized, the transaction must be rejected.
1861	#[allow(deprecated)]
1862	fn validate<V: ValidateUnsigned<Call = Self::Call>>(
1863		&self,
1864		source: TransactionSource,
1865		info: &DispatchInfoOf<Self::Call>,
1866		len: usize,
1867	) -> TransactionValidity;
1868
1869	/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
1870	/// index and sender.
1871	///
1872	/// IMPORTANT: Ensure that *some* origin has been authorized after validating the
1873	/// transaction. If no origin was authorized, the transaction must be rejected.
1874	#[allow(deprecated)]
1875	fn apply<V: ValidateUnsigned<Call = Self::Call>>(
1876		self,
1877		info: &DispatchInfoOf<Self::Call>,
1878		len: usize,
1879	) -> crate::ApplyExtrinsicResultWithInfo<PostDispatchInfoOf<Self::Call>>;
1880}
1881
1882/// A marker trait for something that knows the type of the runtime block.
1883pub trait GetRuntimeBlockType {
1884	/// The `RuntimeBlock` type.
1885	type RuntimeBlock: self::Block;
1886}
1887
1888/// A marker trait for something that knows the type of the node block.
1889pub trait GetNodeBlockType {
1890	/// The `NodeBlock` type.
1891	type NodeBlock: self::Block;
1892}
1893
1894/// Provide validation for unsigned extrinsics.
1895///
1896/// This trait provides two functions [`pre_dispatch`](Self::pre_dispatch) and
1897/// [`validate_unsigned`](Self::validate_unsigned). The [`pre_dispatch`](Self::pre_dispatch)
1898/// function is called right before dispatching the call wrapped by an unsigned extrinsic. The
1899/// [`validate_unsigned`](Self::validate_unsigned) function is mainly being used in the context of
1900/// the transaction pool to check the validity of the call wrapped by an unsigned extrinsic.
1901///
1902/// # Deprecation Notice
1903///
1904/// This trait is deprecated and will be removed after April 2027. Use
1905/// `#[pallet::authorize]` with `frame_system::AuthorizeCall` transaction extension instead.
1906///
1907/// For more information, see: <https://github.com/paritytech/polkadot-sdk/issues/2415>
1908#[deprecated(
1909	note = "`ValidateUnsigned` will be removed after April 2027. Use `#[pallet::authorize]` with `frame_system::AuthorizeCall` instead. See https://github.com/paritytech/polkadot-sdk/issues/2415"
1910)]
1911pub trait ValidateUnsigned {
1912	/// The call to validate
1913	type Call;
1914
1915	/// Validate the call right before dispatch.
1916	///
1917	/// This method should be used to prevent transactions already in the pool
1918	/// (i.e. passing [`validate_unsigned`](Self::validate_unsigned)) from being included in blocks
1919	/// in case they became invalid since being added to the pool.
1920	///
1921	/// By default it's a good idea to call [`validate_unsigned`](Self::validate_unsigned) from
1922	/// within this function again to make sure we never include an invalid transaction. Otherwise
1923	/// the implementation of the call or this method will need to provide proper validation to
1924	/// ensure that the transaction is valid.
1925	///
1926	/// Changes made to storage *WILL* be persisted if the call returns `Ok`.
1927	fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
1928		Self::validate_unsigned(TransactionSource::InBlock, call)
1929			.map(|_| ())
1930			.map_err(Into::into)
1931	}
1932
1933	/// Return the validity of the call
1934	///
1935	/// This method has no side-effects. It merely checks whether the call would be rejected
1936	/// by the runtime in an unsigned extrinsic.
1937	///
1938	/// The validity checks should be as lightweight as possible because every node will execute
1939	/// this code before the unsigned extrinsic enters the transaction pool and also periodically
1940	/// afterwards to ensure the validity. To prevent dos-ing a network with unsigned
1941	/// extrinsics, these validity checks should include some checks around uniqueness, for example,
1942	/// checking that the unsigned extrinsic was sent by an authority in the active set.
1943	///
1944	/// Changes made to storage should be discarded by caller.
1945	fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity;
1946}
1947
1948/// Opaque data type that may be destructured into a series of raw byte slices (which represent
1949/// individual keys).
1950pub trait OpaqueKeys: Clone {
1951	/// The types that are bound to the [`KeyTypeId`]s.
1952	///
1953	/// They can be seen as the ones working with the keys associated to the [`KeyTypeId`]s.
1954	type KeyTypeIdProviders;
1955
1956	/// Return the key-type IDs supported by this set.
1957	fn key_ids() -> &'static [KeyTypeId];
1958
1959	/// Get the raw bytes of key with key-type ID `i`.
1960	fn get_raw(&self, i: KeyTypeId) -> &[u8];
1961
1962	/// Get the decoded key with key-type ID `i`.
1963	fn get<T: Decode>(&self, i: KeyTypeId) -> Option<T> {
1964		T::decode(&mut self.get_raw(i)).ok()
1965	}
1966
1967	/// Proof the ownership of `owner` over the keys using `proof`.
1968	#[must_use]
1969	fn ownership_proof_is_valid(&self, owner: &[u8], proof: &[u8]) -> bool;
1970}
1971
1972/// Input that adds infinite number of zero after wrapped input.
1973///
1974/// This can add an infinite stream of zeros onto any input, not just a slice as with
1975/// `TrailingZerosInput`.
1976pub struct AppendZerosInput<'a, T>(&'a mut T);
1977
1978impl<'a, T> AppendZerosInput<'a, T> {
1979	/// Create a new instance from the given byte array.
1980	pub fn new(input: &'a mut T) -> Self {
1981		Self(input)
1982	}
1983}
1984
1985impl<'a, T: codec::Input> codec::Input for AppendZerosInput<'a, T> {
1986	fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
1987		Ok(None)
1988	}
1989
1990	fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
1991		let remaining = self.0.remaining_len()?;
1992		let completed = if let Some(n) = remaining {
1993			let readable = into.len().min(n);
1994			// this should never fail if `remaining_len` API is implemented correctly.
1995			self.0.read(&mut into[..readable])?;
1996			readable
1997		} else {
1998			// Fill it byte-by-byte.
1999			let mut i = 0;
2000			while i < into.len() {
2001				if let Ok(b) = self.0.read_byte() {
2002					into[i] = b;
2003					i += 1;
2004				} else {
2005					break;
2006				}
2007			}
2008			i
2009		};
2010		// Fill the rest with zeros.
2011		for i in &mut into[completed..] {
2012			*i = 0;
2013		}
2014		Ok(())
2015	}
2016}
2017
2018/// Input that adds infinite number of zero after wrapped input.
2019pub struct TrailingZeroInput<'a>(&'a [u8]);
2020
2021impl<'a> TrailingZeroInput<'a> {
2022	/// Create a new instance from the given byte array.
2023	pub fn new(data: &'a [u8]) -> Self {
2024		Self(data)
2025	}
2026
2027	/// Create a new instance which only contains zeroes as input.
2028	pub fn zeroes() -> Self {
2029		Self::new(&[][..])
2030	}
2031}
2032
2033impl<'a> codec::Input for TrailingZeroInput<'a> {
2034	fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
2035		Ok(None)
2036	}
2037
2038	fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
2039		let len_from_inner = into.len().min(self.0.len());
2040		into[..len_from_inner].copy_from_slice(&self.0[..len_from_inner]);
2041		for i in &mut into[len_from_inner..] {
2042			*i = 0;
2043		}
2044		self.0 = &self.0[len_from_inner..];
2045
2046		Ok(())
2047	}
2048}
2049
2050/// This type can be converted into and possibly from an AccountId (which itself is generic).
2051pub trait AccountIdConversion<AccountId>: Sized {
2052	/// Convert into an account ID. This is infallible, and may truncate bytes to provide a result.
2053	/// This may lead to duplicate accounts if the size of `AccountId` is less than the seed.
2054	fn into_account_truncating(&self) -> AccountId {
2055		self.into_sub_account_truncating(&())
2056	}
2057
2058	/// Convert into an account ID, checking that all bytes of the seed are being used in the final
2059	/// `AccountId` generated. If any bytes are dropped, this returns `None`.
2060	fn try_into_account(&self) -> Option<AccountId> {
2061		self.try_into_sub_account(&())
2062	}
2063
2064	/// Try to convert an account ID into this type. Might not succeed.
2065	fn try_from_account(a: &AccountId) -> Option<Self> {
2066		Self::try_from_sub_account::<()>(a).map(|x| x.0)
2067	}
2068
2069	/// Convert this value amalgamated with a secondary "sub" value into an account ID,
2070	/// truncating any unused bytes. This is infallible.
2071	///
2072	/// NOTE: The account IDs from this and from `into_account` are *not* guaranteed to be distinct
2073	/// for any given value of `self`, nor are different invocations to this with different types
2074	/// `T`. For example, the following will all encode to the same account ID value:
2075	/// - `self.into_sub_account(0u32)`
2076	/// - `self.into_sub_account(vec![0u8; 0])`
2077	/// - `self.into_account()`
2078	///
2079	/// Also, if the seed provided to this function is greater than the number of bytes which fit
2080	/// into this `AccountId` type, then it will lead to truncation of the seed, and potentially
2081	/// non-unique accounts.
2082	fn into_sub_account_truncating<S: Encode>(&self, sub: S) -> AccountId;
2083
2084	/// Same as `into_sub_account_truncating`, but ensuring that all bytes of the account's seed are
2085	/// used when generating an account. This can help guarantee that different accounts are unique,
2086	/// besides types which encode the same as noted above.
2087	fn try_into_sub_account<S: Encode>(&self, sub: S) -> Option<AccountId>;
2088
2089	/// Try to convert an account ID into this type. Might not succeed.
2090	fn try_from_sub_account<S: Decode>(x: &AccountId) -> Option<(Self, S)>;
2091}
2092
2093/// Format is TYPE_ID ++ encode(sub-seed) ++ 00.... where 00... is indefinite trailing zeroes to
2094/// fill AccountId.
2095impl<T: Encode + Decode, Id: Encode + Decode + TypeId> AccountIdConversion<T> for Id {
2096	// Take the `sub` seed, and put as much of it as possible into the generated account, but
2097	// allowing truncation of the seed if it would not fit into the account id in full. This can
2098	// lead to two different `sub` seeds with the same account generated.
2099	fn into_sub_account_truncating<S: Encode>(&self, sub: S) -> T {
2100		(Id::TYPE_ID, self, sub)
2101			.using_encoded(|b| T::decode(&mut TrailingZeroInput(b)))
2102			.expect("All byte sequences are valid `AccountIds`; qed")
2103	}
2104
2105	// Same as `into_sub_account_truncating`, but returns `None` if any bytes would be truncated.
2106	fn try_into_sub_account<S: Encode>(&self, sub: S) -> Option<T> {
2107		let encoded_seed = (Id::TYPE_ID, self, sub).encode();
2108		let account = T::decode(&mut TrailingZeroInput(&encoded_seed))
2109			.expect("All byte sequences are valid `AccountIds`; qed");
2110		// If the `account` generated has less bytes than the `encoded_seed`, then we know that
2111		// bytes were truncated, and we return `None`.
2112		if encoded_seed.len() <= account.encoded_size() {
2113			Some(account)
2114		} else {
2115			None
2116		}
2117	}
2118
2119	fn try_from_sub_account<S: Decode>(x: &T) -> Option<(Self, S)> {
2120		x.using_encoded(|d| {
2121			if d[0..4] != Id::TYPE_ID {
2122				return None;
2123			}
2124			let mut cursor = &d[4..];
2125			let result = Decode::decode(&mut cursor).ok()?;
2126			if cursor.iter().all(|x| *x == 0) {
2127				Some(result)
2128			} else {
2129				None
2130			}
2131		})
2132	}
2133}
2134
2135/// Calls a given macro a number of times with a set of fixed params and an incrementing numeral.
2136/// e.g.
2137/// ```nocompile
2138/// count!(println ("{}",) foo, bar, baz);
2139/// // Will result in three `println!`s: "0", "1" and "2".
2140/// ```
2141#[macro_export]
2142macro_rules! count {
2143	($f:ident ($($x:tt)*) ) => ();
2144	($f:ident ($($x:tt)*) $x1:tt) => { $f!($($x)* 0); };
2145	($f:ident ($($x:tt)*) $x1:tt, $x2:tt) => { $f!($($x)* 0); $f!($($x)* 1); };
2146	($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt) => { $f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); };
2147	($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt, $x4:tt) => {
2148		$f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); $f!($($x)* 3);
2149	};
2150	($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt, $x4:tt, $x5:tt) => {
2151		$f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); $f!($($x)* 3); $f!($($x)* 4);
2152	};
2153}
2154
2155#[doc(hidden)]
2156#[macro_export]
2157macro_rules! impl_opaque_keys_inner {
2158	(
2159		$( #[ $attr:meta ] )*
2160		pub struct $name:ident {
2161			$(
2162				$( #[ $inner_attr:meta ] )*
2163				pub $field:ident: $type:ty,
2164			)*
2165		},
2166		$crate_path:path,
2167	) => {
2168		$( #[ $attr ] )*
2169		///
2170		#[doc = concat!("Generated by [`impl_opaque_keys!`](", stringify!($crate_path),"::impl_opaque_keys).")]
2171		#[derive(
2172			Clone, PartialEq, Eq,
2173			$crate::codec::Encode,
2174			$crate::codec::Decode,
2175			$crate::codec::DecodeWithMemTracking,
2176			$crate::scale_info::TypeInfo,
2177			Debug,
2178		)]
2179		pub struct $name {
2180			$(
2181				$( #[ $inner_attr ] )*
2182				pub $field: <$type as $crate::BoundToRuntimeAppPublic>::Public,
2183			)*
2184		}
2185
2186		impl $name {
2187			/// Generate a set of keys with optionally using the given seed.
2188			///
2189			/// The generated key pairs are stored in the keystore.
2190			///
2191			/// - `owner`: Some bytes that will be signed by the generated private keys.
2192			/// These signatures are put into a tuple in the same order as the public keys.
2193			/// The SCALE encoded signature tuple corresponds to the `proof` returned by this
2194			/// function.
2195			///
2196			/// - `seed`: Optional `seed` for seeding the private key generation.
2197			///
2198			/// Returns the generated public session keys and proof.
2199			#[allow(dead_code)]
2200			pub fn generate(
2201				owner: &[u8],
2202				seed: Option<$crate::sp_std::vec::Vec<u8>>,
2203			) -> $crate::traits::GeneratedSessionKeys<
2204				Self,
2205				(
2206					$(
2207						<
2208							<$type as $crate::BoundToRuntimeAppPublic>::Public
2209								as $crate::RuntimeAppPublic
2210						>::ProofOfPossession
2211					),*
2212				)
2213			> {
2214				let mut keys = Self {
2215					$(
2216						$field: <
2217							<
2218								$type as $crate::BoundToRuntimeAppPublic
2219							>::Public as $crate::RuntimeAppPublic
2220						>::generate_pair(seed.clone()),
2221					)*
2222				};
2223
2224				let proof = keys.create_ownership_proof(owner)
2225					.expect("Private key that was generated a moment ago, should exist; qed");
2226
2227				$crate::traits::GeneratedSessionKeys {
2228					keys,
2229					proof
2230				}
2231			}
2232
2233			/// Converts `Self` into a `Vec` of `(raw public key, KeyTypeId)`.
2234			#[allow(dead_code)]
2235			pub fn into_raw_public_keys(
2236				self,
2237			) -> $crate::Vec<($crate::Vec<u8>, $crate::KeyTypeId)> {
2238				let mut keys = Vec::new();
2239				$(
2240					keys.push((
2241						$crate::RuntimeAppPublic::to_raw_vec(&self.$field),
2242						<
2243							<
2244								$type as $crate::BoundToRuntimeAppPublic
2245							>::Public as $crate::RuntimeAppPublic
2246						>::ID,
2247					));
2248				)*
2249
2250				keys
2251			}
2252
2253			/// Decode `Self` from the given `encoded` slice and convert `Self` into the raw public
2254			/// keys (see [`Self::into_raw_public_keys`]).
2255			///
2256			/// Returns `None` when the decoding failed, otherwise `Some(_)`.
2257			#[allow(dead_code)]
2258			pub fn decode_into_raw_public_keys(
2259				encoded: &[u8],
2260			) -> Option<$crate::Vec<($crate::Vec<u8>, $crate::KeyTypeId)>> {
2261				<Self as $crate::codec::Decode>::decode(&mut &encoded[..])
2262					.ok()
2263					.map(|s| s.into_raw_public_keys())
2264			}
2265
2266			/// Create the ownership proof.
2267			///
2268			/// - `owner`: Some bytes that will be signed by the private keys associated to the
2269			/// public keys in this session key object. These signatures are put into a tuple in
2270			/// the same order as the public keys. The SCALE encoded signature tuple corresponds
2271			/// to the `proof` returned by this function.
2272			///
2273			/// Returns the SCALE encoded proof that will proof the ownership of the keys for `user`.
2274			/// An error is returned if the signing of `user` failed, e.g. a private key isn't present in the keystore.
2275			#[allow(dead_code)]
2276			pub fn create_ownership_proof(
2277				&mut self,
2278				owner: &[u8],
2279			) -> $crate::sp_std::result::Result<
2280				(
2281					$(
2282						<
2283							<$type as $crate::BoundToRuntimeAppPublic>::Public
2284								as $crate::RuntimeAppPublic
2285						>::ProofOfPossession
2286					),*
2287				),
2288				()
2289			> {
2290				let res = ($(
2291					$crate::RuntimeAppPublic::generate_proof_of_possession(&mut self.$field, &owner).ok_or(())?
2292				),*);
2293
2294				Ok(res)
2295			}
2296		}
2297
2298		impl $crate::traits::OpaqueKeys for $name {
2299			type KeyTypeIdProviders = ( $( $type, )* );
2300
2301			fn key_ids() -> &'static [$crate::KeyTypeId] {
2302				&[
2303					$(
2304						<
2305							<
2306								$type as $crate::BoundToRuntimeAppPublic
2307							>::Public as $crate::RuntimeAppPublic
2308						>::ID
2309					),*
2310				]
2311			}
2312
2313			fn get_raw(&self, i: $crate::KeyTypeId) -> &[u8] {
2314				match i {
2315					$(
2316						i if i == <
2317							<
2318								$type as $crate::BoundToRuntimeAppPublic
2319							>::Public as $crate::RuntimeAppPublic
2320						>::ID =>
2321							self.$field.as_ref(),
2322					)*
2323					_ => &[],
2324				}
2325			}
2326
2327			fn ownership_proof_is_valid(&self, owner: &[u8], proof: &[u8]) -> bool {
2328				// The proof is expected to be a tuple of all the signatures.
2329				let Ok(proof) = <($(
2330						<
2331							<
2332								$type as $crate::BoundToRuntimeAppPublic
2333							>::Public as $crate::RuntimeAppPublic
2334						>::ProofOfPossession
2335				),*) as $crate::codec::DecodeAll>::decode_all(&mut &proof[..]) else {
2336					return false
2337				};
2338
2339				// "unpack" the proof so that we can access the individual signatures.
2340				let ( $( $field ),* ) = proof;
2341
2342				// Verify that all the signatures signed `owner`.
2343				$(
2344					let valid = $crate::RuntimeAppPublic::verify_proof_of_possession(&self.$field, &owner, &$field);
2345
2346					if !valid {
2347						// We found an invalid signature.
2348						return false
2349					}
2350				)*
2351
2352				true
2353			}
2354		}
2355	};
2356}
2357
2358/// The output of generating session keys.
2359///
2360/// Contains the public session keys and a `proof` to verify the ownership of these keys.
2361///
2362/// To generate session keys the [`impl_opaque_keys!`](crate::impl_opaque_keys) needs to be used
2363/// first to create the session keys type and this type provides the `generate` function.
2364#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
2365pub struct GeneratedSessionKeys<Keys, Proof> {
2366	/// The opaque public session keys for registering on-chain.
2367	pub keys: Keys,
2368	/// The opaque proof to verify the ownership of the keys.
2369	pub proof: Proof,
2370}
2371
2372/// Implement [`OpaqueKeys`] for a described struct.
2373///
2374/// Every field type must implement [`BoundToRuntimeAppPublic`](crate::BoundToRuntimeAppPublic).
2375/// The [`KeyTypeIdProviders`](OpaqueKeys::KeyTypeIdProviders) type is set to tuple of all field
2376/// types passed to the macro.
2377///
2378/// The `proof` type used by the generated session keys for
2379/// [`ownership_proof_is_valid`](OpaqueKeys::ownership_proof_is_valid) is the SCALE encoded tuple of
2380/// all signatures. The order of the signatures is the same as the order of the fields in the
2381/// struct. Each signature is created by signing the `owner` given to the `generate` function.
2382///
2383/// ```rust
2384/// use sp_runtime::{
2385/// 	impl_opaque_keys, KeyTypeId, BoundToRuntimeAppPublic, app_crypto::{sr25519, ed25519}
2386/// };
2387///
2388/// pub struct KeyModule;
2389/// impl BoundToRuntimeAppPublic for KeyModule { type Public = ed25519::AppPublic; }
2390///
2391/// pub struct KeyModule2;
2392/// impl BoundToRuntimeAppPublic for KeyModule2 { type Public = sr25519::AppPublic; }
2393///
2394/// impl_opaque_keys! {
2395/// 	pub struct Keys {
2396/// 		pub key_module: KeyModule,
2397/// 		pub key_module2: KeyModule2,
2398/// 	}
2399/// }
2400/// ```
2401#[macro_export]
2402#[cfg(any(feature = "serde", feature = "std"))]
2403macro_rules! impl_opaque_keys {
2404	{
2405		$( #[ $attr:meta ] )*
2406		pub struct $name:ident {
2407			$(
2408				$( #[ $inner_attr:meta ] )*
2409				pub $field:ident: $type:ty,
2410			)*
2411		}
2412	} => {
2413		$crate::paste::paste! {
2414			use $crate::serde as [< __opaque_keys_serde_import__ $name >];
2415
2416			$crate::impl_opaque_keys_inner! {
2417				$( #[ $attr ] )*
2418				#[derive($crate::serde::Serialize, $crate::serde::Deserialize)]
2419				#[serde(crate = "__opaque_keys_serde_import__" $name)]
2420				pub struct $name {
2421					$(
2422						$( #[ $inner_attr ] )*
2423						pub $field: $type,
2424					)*
2425				},
2426				$crate,
2427			}
2428		}
2429	}
2430}
2431
2432#[macro_export]
2433#[cfg(all(not(feature = "std"), not(feature = "serde")))]
2434#[doc(hidden)]
2435macro_rules! impl_opaque_keys {
2436	{
2437		$( #[ $attr:meta ] )*
2438		pub struct $name:ident {
2439			$(
2440				$( #[ $inner_attr:meta ] )*
2441				pub $field:ident: $type:ty,
2442			)*
2443		}
2444	} => {
2445		$crate::impl_opaque_keys_inner! {
2446			$( #[ $attr ] )*
2447			pub struct $name {
2448				$(
2449					$( #[ $inner_attr ] )*
2450					pub $field: $type,
2451				)*
2452			},
2453			$crate,
2454		}
2455	}
2456}
2457
2458/// Something that can convert a [`BlockId`](crate::generic::BlockId) to a number or a hash.
2459#[cfg(feature = "std")]
2460pub trait BlockIdTo<Block: self::Block> {
2461	/// The error type that will be returned by the functions.
2462	type Error: std::error::Error;
2463
2464	/// Convert the given `block_id` to the corresponding block hash.
2465	fn to_hash(
2466		&self,
2467		block_id: &crate::generic::BlockId<Block>,
2468	) -> Result<Option<Block::Hash>, Self::Error>;
2469
2470	/// Convert the given `block_id` to the corresponding block number.
2471	fn to_number(
2472		&self,
2473		block_id: &crate::generic::BlockId<Block>,
2474	) -> Result<Option<NumberFor<Block>>, Self::Error>;
2475}
2476
2477/// Get current block number
2478pub trait BlockNumberProvider {
2479	/// Type of `BlockNumber` to provide.
2480	type BlockNumber: Codec
2481		+ DecodeWithMemTracking
2482		+ Clone
2483		+ Ord
2484		+ Eq
2485		+ AtLeast32BitUnsigned
2486		+ TypeInfo
2487		+ Debug
2488		+ MaxEncodedLen
2489		+ Copy
2490		+ EncodeLike
2491		+ Default;
2492
2493	/// Returns the current block number.
2494	///
2495	/// Provides an abstraction over an arbitrary way of providing the
2496	/// current block number.
2497	///
2498	/// In case of using crate `sp_runtime` with the crate `frame-system`,
2499	/// it is already implemented for
2500	/// `frame_system::Pallet<T: Config>` as:
2501	///
2502	/// ```ignore
2503	/// fn current_block_number() -> Self {
2504	///     frame_system::Pallet<Config>::block_number()
2505	/// }
2506	/// ```
2507	/// .
2508	fn current_block_number() -> Self::BlockNumber;
2509
2510	/// Utility function only to be used in benchmarking scenarios or tests, to be implemented
2511	/// optionally, else a noop.
2512	///
2513	/// It allows for setting the block number that will later be fetched
2514	/// This is useful in case the block number provider is different than System
2515	#[cfg(any(feature = "std", feature = "runtime-benchmarks"))]
2516	fn set_block_number(_block: Self::BlockNumber) {}
2517}
2518
2519impl BlockNumberProvider for () {
2520	type BlockNumber = u32;
2521	fn current_block_number() -> Self::BlockNumber {
2522		0
2523	}
2524}
2525
2526#[cfg(test)]
2527mod tests {
2528	use super::*;
2529	use crate::codec::{Decode, Encode, Input};
2530	#[cfg(feature = "bls-experimental")]
2531	use sp_core::ecdsa_bls381;
2532	use sp_core::{
2533		crypto::{Pair, UncheckedFrom},
2534		ecdsa, ed25519,
2535		proof_of_possession::ProofOfPossessionGenerator,
2536		sr25519,
2537	};
2538	use std::sync::Arc;
2539
2540	macro_rules! signature_verify_test {
2541		($algorithm:ident) => {
2542			let msg = &b"test-message"[..];
2543			let wrong_msg = &b"test-msg"[..];
2544			let (pair, _) = $algorithm::Pair::generate();
2545
2546			let signature = pair.sign(&msg);
2547			assert!($algorithm::Pair::verify(&signature, msg, &pair.public()));
2548
2549			assert!(signature.verify(msg, &pair.public()));
2550			assert!(!signature.verify(wrong_msg, &pair.public()));
2551		};
2552	}
2553
2554	mod t {
2555		use sp_application_crypto::{app_crypto, sr25519};
2556		use sp_core::crypto::KeyTypeId;
2557		app_crypto!(sr25519, KeyTypeId(*b"test"));
2558	}
2559
2560	#[test]
2561	fn app_verify_works() {
2562		use super::AppVerify;
2563		use t::*;
2564
2565		let s = Signature::try_from(vec![0; 64]).unwrap();
2566		let _ = s.verify(&[0u8; 100][..], &Public::unchecked_from([0; 32]));
2567	}
2568
2569	#[derive(Encode, Decode, Default, PartialEq, Debug)]
2570	struct U128Value(u128);
2571	impl super::TypeId for U128Value {
2572		const TYPE_ID: [u8; 4] = [0x0d, 0xf0, 0x0d, 0xf0];
2573	}
2574	// f00df00d
2575
2576	#[derive(Encode, Decode, Default, PartialEq, Debug)]
2577	struct U32Value(u32);
2578	impl super::TypeId for U32Value {
2579		const TYPE_ID: [u8; 4] = [0x0d, 0xf0, 0xfe, 0xca];
2580	}
2581	// cafef00d
2582
2583	#[derive(Encode, Decode, Default, PartialEq, Debug)]
2584	struct U16Value(u16);
2585	impl super::TypeId for U16Value {
2586		const TYPE_ID: [u8; 4] = [0xfe, 0xca, 0x0d, 0xf0];
2587	}
2588	// f00dcafe
2589
2590	type AccountId = u64;
2591
2592	#[test]
2593	fn into_account_truncating_should_work() {
2594		let r: AccountId = U32Value::into_account_truncating(&U32Value(0xdeadbeef));
2595		assert_eq!(r, 0x_deadbeef_cafef00d);
2596	}
2597
2598	#[test]
2599	fn try_into_account_should_work() {
2600		let r: AccountId = U32Value::try_into_account(&U32Value(0xdeadbeef)).unwrap();
2601		assert_eq!(r, 0x_deadbeef_cafef00d);
2602
2603		// u128 is bigger than u64 would fit
2604		let maybe: Option<AccountId> = U128Value::try_into_account(&U128Value(u128::MAX));
2605		assert!(maybe.is_none());
2606	}
2607
2608	#[test]
2609	fn try_from_account_should_work() {
2610		let r = U32Value::try_from_account(&0x_deadbeef_cafef00d_u64);
2611		assert_eq!(r.unwrap(), U32Value(0xdeadbeef));
2612	}
2613
2614	#[test]
2615	fn into_account_truncating_with_fill_should_work() {
2616		let r: AccountId = U16Value::into_account_truncating(&U16Value(0xc0da));
2617		assert_eq!(r, 0x_0000_c0da_f00dcafe);
2618	}
2619
2620	#[test]
2621	fn try_into_sub_account_should_work() {
2622		let r: AccountId = U16Value::try_into_account(&U16Value(0xc0da)).unwrap();
2623		assert_eq!(r, 0x_0000_c0da_f00dcafe);
2624
2625		let maybe: Option<AccountId> = U16Value::try_into_sub_account(
2626			&U16Value(0xc0da),
2627			"a really large amount of additional encoded information which will certainly overflow the account id type ;)"
2628		);
2629
2630		assert!(maybe.is_none())
2631	}
2632
2633	#[test]
2634	fn try_from_account_with_fill_should_work() {
2635		let r = U16Value::try_from_account(&0x0000_c0da_f00dcafe_u64);
2636		assert_eq!(r.unwrap(), U16Value(0xc0da));
2637	}
2638
2639	#[test]
2640	fn bad_try_from_account_should_fail() {
2641		let r = U16Value::try_from_account(&0x0000_c0de_baadcafe_u64);
2642		assert!(r.is_none());
2643		let r = U16Value::try_from_account(&0x0100_c0da_f00dcafe_u64);
2644		assert!(r.is_none());
2645	}
2646
2647	#[test]
2648	fn trailing_zero_should_work() {
2649		let mut t = super::TrailingZeroInput(&[1, 2, 3]);
2650		assert_eq!(t.remaining_len(), Ok(None));
2651		let mut buffer = [0u8; 2];
2652		assert_eq!(t.read(&mut buffer), Ok(()));
2653		assert_eq!(t.remaining_len(), Ok(None));
2654		assert_eq!(buffer, [1, 2]);
2655		assert_eq!(t.read(&mut buffer), Ok(()));
2656		assert_eq!(t.remaining_len(), Ok(None));
2657		assert_eq!(buffer, [3, 0]);
2658		assert_eq!(t.read(&mut buffer), Ok(()));
2659		assert_eq!(t.remaining_len(), Ok(None));
2660		assert_eq!(buffer, [0, 0]);
2661	}
2662
2663	#[test]
2664	fn ed25519_verify_works() {
2665		signature_verify_test!(ed25519);
2666	}
2667
2668	#[test]
2669	fn sr25519_verify_works() {
2670		signature_verify_test!(sr25519);
2671	}
2672
2673	#[test]
2674	fn ecdsa_verify_works() {
2675		signature_verify_test!(ecdsa);
2676
2677		let msg = &b"test-message"[..];
2678		let (pair, _) = ecdsa::Pair::generate();
2679		let signature = pair.sign(msg);
2680		let high_s_signature =
2681			ecdsa::Signature::from_raw(crate::tests::make_high_s_signature(signature.as_ref()));
2682		assert!(!high_s_signature.verify(msg, &pair.public()));
2683	}
2684
2685	#[test]
2686	#[cfg(feature = "bls-experimental")]
2687	fn ecdsa_bls381_verify_works() {
2688		signature_verify_test!(ecdsa_bls381);
2689	}
2690
2691	pub struct Sr25519Key;
2692	impl crate::BoundToRuntimeAppPublic for Sr25519Key {
2693		type Public = sp_application_crypto::sr25519::AppPublic;
2694	}
2695
2696	pub struct Ed25519Key;
2697	impl crate::BoundToRuntimeAppPublic for Ed25519Key {
2698		type Public = sp_application_crypto::ed25519::AppPublic;
2699	}
2700
2701	pub struct EcdsaKey;
2702	impl crate::BoundToRuntimeAppPublic for EcdsaKey {
2703		type Public = sp_application_crypto::ecdsa::AppPublic;
2704	}
2705
2706	impl_opaque_keys! {
2707		/// Some comment
2708		pub struct SessionKeys {
2709			pub sr25519: Sr25519Key,
2710			pub ed25519: Ed25519Key,
2711			pub ecdsa: EcdsaKey,
2712		}
2713	}
2714
2715	#[test]
2716	fn opaque_keys_ownership_proof_works() {
2717		let mut sr25519 = sp_core::sr25519::Pair::generate().0;
2718		let mut ed25519 = sp_core::ed25519::Pair::generate().0;
2719		let mut ecdsa = sp_core::ecdsa::Pair::generate().0;
2720
2721		let session_keys = SessionKeys {
2722			sr25519: sr25519.public().into(),
2723			ed25519: ed25519.public().into(),
2724			ecdsa: ecdsa.public().into(),
2725		};
2726
2727		let owner = &b"owner"[..];
2728
2729		let sr25519_sig = sr25519.generate_proof_of_possession(&owner);
2730		let ed25519_sig = ed25519.generate_proof_of_possession(&owner);
2731		let ecdsa_sig = ecdsa.generate_proof_of_possession(&owner);
2732
2733		for invalidate in [None, Some(0), Some(1), Some(2)] {
2734			let proof = if let Some(invalidate) = invalidate {
2735				match invalidate {
2736					0 => (
2737						sr25519.generate_proof_of_possession(&b"invalid"[..]),
2738						&ed25519_sig,
2739						&ecdsa_sig,
2740					)
2741						.encode(),
2742					1 => (
2743						&sr25519_sig,
2744						ed25519.generate_proof_of_possession(&b"invalid"[..]),
2745						&ecdsa_sig,
2746					)
2747						.encode(),
2748					2 => (
2749						&sr25519_sig,
2750						&ed25519_sig,
2751						ecdsa.generate_proof_of_possession(&b"invalid"[..]),
2752					)
2753						.encode(),
2754					_ => unreachable!(),
2755				}
2756			} else {
2757				(&sr25519_sig, &ed25519_sig, &ecdsa_sig).encode()
2758			};
2759
2760			assert_eq!(session_keys.ownership_proof_is_valid(owner, &proof), invalidate.is_none());
2761		}
2762
2763		// Ensure that a `proof` with extra junk data is rejected.
2764		let proof = (&sr25519_sig, &ed25519_sig, &ecdsa_sig, "hello").encode();
2765		assert!(!session_keys.ownership_proof_is_valid(owner, &proof));
2766
2767		let mut ext = sp_io::TestExternalities::default();
2768		ext.register_extension(sp_keystore::KeystoreExt(Arc::new(
2769			sp_keystore::testing::MemoryKeystore::new(),
2770		)));
2771
2772		ext.execute_with(|| {
2773			let session_keys = SessionKeys::generate(&owner, None);
2774
2775			assert!(session_keys
2776				.keys
2777				.ownership_proof_is_valid(&owner, &session_keys.proof.encode()));
2778		});
2779	}
2780}