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