referrerpolicy=no-referrer-when-downgrade

sp_runtime/
lib.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//! # Substrate Runtime Primitives.
19//!
20//! This crate, among other things, contains a large library of types and utilities that are used in
21//! the Substrate runtime, but are not particularly `FRAME`-oriented.
22//!
23//! ## Block, Header and Extrinsics
24//!
25//! Most notable, this crate contains some of the types and trait that enable important
26//! communication between the client and the runtime. This includes:
27//!
28//! - A set of traits to declare what any block/header/extrinsic type should provide.
29//! 	- [`traits::Block`], [`traits::Header`], [`traits::ExtrinsicLike`]
30//! - A set of types that implement these traits, whilst still providing a high degree of
31//!   configurability via generics.
32//! 	- [`generic::Block`], [`generic::Header`], [`generic::UncheckedExtrinsic`] and
33//!    [`generic::CheckedExtrinsic`]
34//!
35//! ## Runtime API Types
36//!
37//! This crate also contains some types that are often used in conjuncture with Runtime APIs. Most
38//! notable:
39//!
40//! - [`ApplyExtrinsicResult`], and [`DispatchOutcome`], which dictate how the client and runtime
41//!   communicate about the success or failure of an extrinsic.
42//! - [`transaction_validity`], which dictates how the client and runtime communicate about the
43//!  validity of an extrinsic while still in the transaction-queue.
44
45#![warn(missing_docs)]
46#![cfg_attr(not(feature = "std"), no_std)]
47
48#[doc(hidden)]
49extern crate alloc;
50
51#[doc(hidden)]
52pub use alloc::vec::Vec;
53#[doc(hidden)]
54pub use codec;
55#[doc(hidden)]
56pub use scale_info;
57#[cfg(feature = "serde")]
58#[doc(hidden)]
59pub use serde;
60#[doc(hidden)]
61pub use sp_std;
62
63#[doc(hidden)]
64pub use paste;
65#[doc(hidden)]
66pub use sp_arithmetic::traits::Saturating;
67
68#[doc(hidden)]
69pub use sp_application_crypto as app_crypto;
70
71pub use sp_core::storage::StateVersion;
72#[cfg(feature = "std")]
73pub use sp_core::storage::{Storage, StorageChild};
74
75use sp_core::{
76	crypto::{self, ByteArray, FromEntropy},
77	ecdsa, ed25519,
78	hash::{H256, H512},
79	sr25519,
80};
81
82use alloc::vec;
83use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
84use scale_info::TypeInfo;
85
86pub mod curve;
87pub mod generic;
88pub mod legacy;
89mod multiaddress;
90pub mod offchain;
91pub mod proving_trie;
92pub mod runtime_logger;
93#[cfg(feature = "std")]
94pub mod testing;
95pub mod traits;
96pub mod transaction_validity;
97pub mod type_with_default;
98
99// Re-export Multiaddress
100pub use multiaddress::MultiAddress;
101
102use proving_trie::TrieError;
103
104/// Re-export these since they're only "kind of" generic.
105pub use generic::{Digest, DigestItem};
106
107pub use sp_application_crypto::{BoundToRuntimeAppPublic, RuntimeAppPublic};
108/// Re-export this since it's part of the API of this crate.
109pub use sp_core::{
110	bounded::{BoundedBTreeMap, BoundedBTreeSet, BoundedSlice, BoundedVec, WeakBoundedVec},
111	crypto::{key_types, AccountId32, CryptoType, CryptoTypeId, KeyTypeId},
112	TypeId,
113};
114/// Re-export bounded_vec and bounded_btree_map macros only when std is enabled.
115#[cfg(feature = "std")]
116pub use sp_core::{bounded_btree_map, bounded_vec};
117
118/// Re-export `RuntimeDebug`, to avoid dependency clutter.
119pub use sp_core::RuntimeDebug;
120
121/// Re-export big_uint stuff.
122pub use sp_arithmetic::biguint;
123/// Re-export 128 bit helpers.
124pub use sp_arithmetic::helpers_128bit;
125/// Re-export top-level arithmetic stuff.
126pub use sp_arithmetic::{
127	traits::SaturatedConversion, ArithmeticError, FixedI128, FixedI64, FixedPointNumber,
128	FixedPointOperand, FixedU128, FixedU64, InnerOf, PerThing, PerU16, Perbill, Percent, Permill,
129	Perquintill, Rational128, Rounding, UpperOf,
130};
131/// Re-export this since it's part of the API of this crate.
132pub use sp_weights::Weight;
133
134pub use either::Either;
135
136/// The number of bytes of the module-specific `error` field defined in [`ModuleError`].
137/// In FRAME, this is the maximum encoded size of a pallet error type.
138pub const MAX_MODULE_ERROR_ENCODED_SIZE: usize = 4;
139
140/// An abstraction over justification for a block's validity under a consensus algorithm.
141///
142/// Essentially a finality proof. The exact formulation will vary between consensus
143/// algorithms. In the case where there are multiple valid proofs, inclusion within
144/// the block itself would allow swapping justifications to change the block's hash
145/// (and thus fork the chain). Sending a `Justification` alongside a block instead
146/// bypasses this problem.
147///
148/// Each justification is provided as an encoded blob, and is tagged with an ID
149/// to identify the consensus engine that generated the proof (we might have
150/// multiple justifications from different engines for the same block).
151pub type Justification = (ConsensusEngineId, EncodedJustification);
152
153/// The encoded justification specific to a consensus engine.
154pub type EncodedJustification = Vec<u8>;
155
156/// Collection of justifications for a given block, multiple justifications may
157/// be provided by different consensus engines for the same block.
158#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
159#[derive(Default, Debug, Clone, PartialEq, Eq, Encode, Decode)]
160pub struct Justifications(Vec<Justification>);
161
162impl Justifications {
163	/// Create a new `Justifications` instance with the given justifications.
164	pub fn new(justifications: Vec<Justification>) -> Self {
165		Self(justifications)
166	}
167
168	/// Return an iterator over the justifications.
169	pub fn iter(&self) -> impl Iterator<Item = &Justification> {
170		self.0.iter()
171	}
172
173	/// Append a justification. Returns false if a justification with the same
174	/// `ConsensusEngineId` already exists, in which case the justification is
175	/// not inserted.
176	pub fn append(&mut self, justification: Justification) -> bool {
177		if self.get(justification.0).is_some() {
178			return false
179		}
180		self.0.push(justification);
181		true
182	}
183
184	/// Return the encoded justification for the given consensus engine, if it
185	/// exists.
186	pub fn get(&self, engine_id: ConsensusEngineId) -> Option<&EncodedJustification> {
187		self.iter().find(|j| j.0 == engine_id).map(|j| &j.1)
188	}
189
190	/// Remove the encoded justification for the given consensus engine, if it exists.
191	pub fn remove(&mut self, engine_id: ConsensusEngineId) {
192		self.0.retain(|j| j.0 != engine_id)
193	}
194
195	/// Return a copy of the encoded justification for the given consensus
196	/// engine, if it exists.
197	pub fn into_justification(self, engine_id: ConsensusEngineId) -> Option<EncodedJustification> {
198		self.into_iter().find(|j| j.0 == engine_id).map(|j| j.1)
199	}
200}
201
202impl IntoIterator for Justifications {
203	type Item = Justification;
204	type IntoIter = alloc::vec::IntoIter<Self::Item>;
205
206	fn into_iter(self) -> Self::IntoIter {
207		self.0.into_iter()
208	}
209}
210
211impl From<Justification> for Justifications {
212	fn from(justification: Justification) -> Self {
213		Self(vec![justification])
214	}
215}
216
217use traits::{Lazy, Verify};
218
219use crate::traits::IdentifyAccount;
220#[cfg(feature = "serde")]
221pub use serde::{de::DeserializeOwned, Deserialize, Serialize};
222
223/// Complex storage builder stuff.
224#[cfg(feature = "std")]
225pub trait BuildStorage {
226	/// Build the storage out of this builder.
227	fn build_storage(&self) -> Result<sp_core::storage::Storage, String> {
228		let mut storage = Default::default();
229		self.assimilate_storage(&mut storage)?;
230		Ok(storage)
231	}
232	/// Assimilate the storage for this module into pre-existing overlays.
233	fn assimilate_storage(&self, storage: &mut sp_core::storage::Storage) -> Result<(), String>;
234}
235
236/// Something that can build the genesis storage of a module.
237#[cfg(feature = "std")]
238#[deprecated(
239	note = "`BuildModuleGenesisStorage` is planned to be removed in December 2023. Use `BuildStorage` instead of it."
240)]
241pub trait BuildModuleGenesisStorage<T, I>: Sized {
242	/// Create the module genesis storage into the given `storage` and `child_storage`.
243	fn build_module_genesis_storage(
244		&self,
245		storage: &mut sp_core::storage::Storage,
246	) -> Result<(), String>;
247}
248
249#[cfg(feature = "std")]
250impl BuildStorage for sp_core::storage::Storage {
251	fn assimilate_storage(&self, storage: &mut sp_core::storage::Storage) -> Result<(), String> {
252		storage.top.extend(self.top.iter().map(|(k, v)| (k.clone(), v.clone())));
253		for (k, other_map) in self.children_default.iter() {
254			let k = k.clone();
255			if let Some(map) = storage.children_default.get_mut(&k) {
256				map.data.extend(other_map.data.iter().map(|(k, v)| (k.clone(), v.clone())));
257				if !map.child_info.try_update(&other_map.child_info) {
258					return Err("Incompatible child info update".to_string())
259				}
260			} else {
261				storage.children_default.insert(k, other_map.clone());
262			}
263		}
264		Ok(())
265	}
266}
267
268#[cfg(feature = "std")]
269impl BuildStorage for () {
270	fn assimilate_storage(&self, _: &mut sp_core::storage::Storage) -> Result<(), String> {
271		Err("`assimilate_storage` not implemented for `()`".into())
272	}
273}
274
275/// Consensus engine unique ID.
276pub type ConsensusEngineId = [u8; 4];
277
278/// Signature verify that can work with any known signature types.
279#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
280#[derive(
281	Eq,
282	PartialEq,
283	Clone,
284	Encode,
285	Decode,
286	DecodeWithMemTracking,
287	MaxEncodedLen,
288	RuntimeDebug,
289	TypeInfo,
290)]
291pub enum MultiSignature {
292	/// An Ed25519 signature.
293	Ed25519(ed25519::Signature),
294	/// An Sr25519 signature.
295	Sr25519(sr25519::Signature),
296	/// An ECDSA/SECP256k1 signature.
297	Ecdsa(ecdsa::Signature),
298}
299
300impl From<ed25519::Signature> for MultiSignature {
301	fn from(x: ed25519::Signature) -> Self {
302		Self::Ed25519(x)
303	}
304}
305
306impl TryFrom<MultiSignature> for ed25519::Signature {
307	type Error = ();
308	fn try_from(m: MultiSignature) -> Result<Self, Self::Error> {
309		if let MultiSignature::Ed25519(x) = m {
310			Ok(x)
311		} else {
312			Err(())
313		}
314	}
315}
316
317impl From<sr25519::Signature> for MultiSignature {
318	fn from(x: sr25519::Signature) -> Self {
319		Self::Sr25519(x)
320	}
321}
322
323impl TryFrom<MultiSignature> for sr25519::Signature {
324	type Error = ();
325	fn try_from(m: MultiSignature) -> Result<Self, Self::Error> {
326		if let MultiSignature::Sr25519(x) = m {
327			Ok(x)
328		} else {
329			Err(())
330		}
331	}
332}
333
334impl From<ecdsa::Signature> for MultiSignature {
335	fn from(x: ecdsa::Signature) -> Self {
336		Self::Ecdsa(x)
337	}
338}
339
340impl TryFrom<MultiSignature> for ecdsa::Signature {
341	type Error = ();
342	fn try_from(m: MultiSignature) -> Result<Self, Self::Error> {
343		if let MultiSignature::Ecdsa(x) = m {
344			Ok(x)
345		} else {
346			Err(())
347		}
348	}
349}
350
351/// Public key for any known crypto algorithm.
352#[derive(
353	Eq,
354	PartialEq,
355	Ord,
356	PartialOrd,
357	Clone,
358	Encode,
359	Decode,
360	DecodeWithMemTracking,
361	RuntimeDebug,
362	TypeInfo,
363)]
364#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
365pub enum MultiSigner {
366	/// An Ed25519 identity.
367	Ed25519(ed25519::Public),
368	/// An Sr25519 identity.
369	Sr25519(sr25519::Public),
370	/// An SECP256k1/ECDSA identity (actually, the Blake2 hash of the compressed pub key).
371	Ecdsa(ecdsa::Public),
372}
373
374impl FromEntropy for MultiSigner {
375	fn from_entropy(input: &mut impl codec::Input) -> Result<Self, codec::Error> {
376		Ok(match input.read_byte()? % 3 {
377			0 => Self::Ed25519(FromEntropy::from_entropy(input)?),
378			1 => Self::Sr25519(FromEntropy::from_entropy(input)?),
379			2.. => Self::Ecdsa(FromEntropy::from_entropy(input)?),
380		})
381	}
382}
383
384/// NOTE: This implementations is required by `SimpleAddressDeterminer`,
385/// we convert the hash into some AccountId, it's fine to use any scheme.
386impl<T: Into<H256>> crypto::UncheckedFrom<T> for MultiSigner {
387	fn unchecked_from(x: T) -> Self {
388		ed25519::Public::unchecked_from(x.into()).into()
389	}
390}
391
392impl AsRef<[u8]> for MultiSigner {
393	fn as_ref(&self) -> &[u8] {
394		match *self {
395			Self::Ed25519(ref who) => who.as_ref(),
396			Self::Sr25519(ref who) => who.as_ref(),
397			Self::Ecdsa(ref who) => who.as_ref(),
398		}
399	}
400}
401
402impl traits::IdentifyAccount for MultiSigner {
403	type AccountId = AccountId32;
404	fn into_account(self) -> AccountId32 {
405		match self {
406			Self::Ed25519(who) => <[u8; 32]>::from(who).into(),
407			Self::Sr25519(who) => <[u8; 32]>::from(who).into(),
408			Self::Ecdsa(who) => sp_io::hashing::blake2_256(who.as_ref()).into(),
409		}
410	}
411}
412
413impl From<ed25519::Public> for MultiSigner {
414	fn from(x: ed25519::Public) -> Self {
415		Self::Ed25519(x)
416	}
417}
418
419impl TryFrom<MultiSigner> for ed25519::Public {
420	type Error = ();
421	fn try_from(m: MultiSigner) -> Result<Self, Self::Error> {
422		if let MultiSigner::Ed25519(x) = m {
423			Ok(x)
424		} else {
425			Err(())
426		}
427	}
428}
429
430impl From<sr25519::Public> for MultiSigner {
431	fn from(x: sr25519::Public) -> Self {
432		Self::Sr25519(x)
433	}
434}
435
436impl TryFrom<MultiSigner> for sr25519::Public {
437	type Error = ();
438	fn try_from(m: MultiSigner) -> Result<Self, Self::Error> {
439		if let MultiSigner::Sr25519(x) = m {
440			Ok(x)
441		} else {
442			Err(())
443		}
444	}
445}
446
447impl From<ecdsa::Public> for MultiSigner {
448	fn from(x: ecdsa::Public) -> Self {
449		Self::Ecdsa(x)
450	}
451}
452
453impl TryFrom<MultiSigner> for ecdsa::Public {
454	type Error = ();
455	fn try_from(m: MultiSigner) -> Result<Self, Self::Error> {
456		if let MultiSigner::Ecdsa(x) = m {
457			Ok(x)
458		} else {
459			Err(())
460		}
461	}
462}
463
464#[cfg(feature = "std")]
465impl std::fmt::Display for MultiSigner {
466	fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
467		match self {
468			Self::Ed25519(who) => write!(fmt, "ed25519: {}", who),
469			Self::Sr25519(who) => write!(fmt, "sr25519: {}", who),
470			Self::Ecdsa(who) => write!(fmt, "ecdsa: {}", who),
471		}
472	}
473}
474
475impl Verify for MultiSignature {
476	type Signer = MultiSigner;
477	fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &AccountId32) -> bool {
478		let who: [u8; 32] = *signer.as_ref();
479		match self {
480			Self::Ed25519(sig) => sig.verify(msg, &who.into()),
481			Self::Sr25519(sig) => sig.verify(msg, &who.into()),
482			Self::Ecdsa(sig) => {
483				let m = sp_io::hashing::blake2_256(msg.get());
484				sp_io::crypto::secp256k1_ecdsa_recover_compressed(sig.as_ref(), &m)
485					.map_or(false, |pubkey| sp_io::hashing::blake2_256(&pubkey) == who)
486			},
487		}
488	}
489}
490
491/// Signature verify that can work with any known signature types..
492#[derive(Eq, PartialEq, Clone, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
493#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
494pub struct AnySignature(H512);
495
496impl Verify for AnySignature {
497	type Signer = sr25519::Public;
498	fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &sr25519::Public) -> bool {
499		let msg = msg.get();
500		sr25519::Signature::try_from(self.0.as_fixed_bytes().as_ref())
501			.map(|s| s.verify(msg, signer))
502			.unwrap_or(false) ||
503			ed25519::Signature::try_from(self.0.as_fixed_bytes().as_ref())
504				.map(|s| match ed25519::Public::from_slice(signer.as_ref()) {
505					Err(()) => false,
506					Ok(signer) => s.verify(msg, &signer),
507				})
508				.unwrap_or(false)
509	}
510}
511
512impl From<sr25519::Signature> for AnySignature {
513	fn from(s: sr25519::Signature) -> Self {
514		Self(s.into())
515	}
516}
517
518impl From<ed25519::Signature> for AnySignature {
519	fn from(s: ed25519::Signature) -> Self {
520		Self(s.into())
521	}
522}
523
524impl From<DispatchError> for DispatchOutcome {
525	fn from(err: DispatchError) -> Self {
526		Err(err)
527	}
528}
529
530/// This is the legacy return type of `Dispatchable`. It is still exposed for compatibility reasons.
531/// The new return type is `DispatchResultWithInfo`. FRAME runtimes should use
532/// `frame_support::dispatch::DispatchResult`.
533pub type DispatchResult = core::result::Result<(), DispatchError>;
534
535/// Return type of a `Dispatchable` which contains the `DispatchResult` and additional information
536/// about the `Dispatchable` that is only known post dispatch.
537pub type DispatchResultWithInfo<T> = core::result::Result<T, DispatchErrorWithPostInfo<T>>;
538
539/// Reason why a pallet call failed.
540#[derive(
541	Eq, Clone, Copy, Encode, Decode, DecodeWithMemTracking, Debug, TypeInfo, MaxEncodedLen,
542)]
543#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
544pub struct ModuleError {
545	/// Module index, matching the metadata module index.
546	pub index: u8,
547	/// Module specific error value.
548	pub error: [u8; MAX_MODULE_ERROR_ENCODED_SIZE],
549	/// Optional error message.
550	#[codec(skip)]
551	#[cfg_attr(feature = "serde", serde(skip_deserializing))]
552	pub message: Option<&'static str>,
553}
554
555impl PartialEq for ModuleError {
556	fn eq(&self, other: &Self) -> bool {
557		(self.index == other.index) && (self.error == other.error)
558	}
559}
560
561/// Errors related to transactional storage layers.
562#[derive(
563	Eq,
564	PartialEq,
565	Clone,
566	Copy,
567	Encode,
568	Decode,
569	DecodeWithMemTracking,
570	Debug,
571	TypeInfo,
572	MaxEncodedLen,
573)]
574#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
575pub enum TransactionalError {
576	/// Too many transactional layers have been spawned.
577	LimitReached,
578	/// A transactional layer was expected, but does not exist.
579	NoLayer,
580}
581
582impl From<TransactionalError> for &'static str {
583	fn from(e: TransactionalError) -> &'static str {
584		match e {
585			TransactionalError::LimitReached => "Too many transactional layers have been spawned",
586			TransactionalError::NoLayer => "A transactional layer was expected, but does not exist",
587		}
588	}
589}
590
591impl From<TransactionalError> for DispatchError {
592	fn from(e: TransactionalError) -> DispatchError {
593		Self::Transactional(e)
594	}
595}
596
597/// Reason why a dispatch call failed.
598#[derive(
599	Eq,
600	Clone,
601	Copy,
602	Encode,
603	Decode,
604	DecodeWithMemTracking,
605	Debug,
606	TypeInfo,
607	PartialEq,
608	MaxEncodedLen,
609)]
610#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
611pub enum DispatchError {
612	/// Some error occurred.
613	Other(
614		#[codec(skip)]
615		#[cfg_attr(feature = "serde", serde(skip_deserializing))]
616		&'static str,
617	),
618	/// Failed to lookup some data.
619	CannotLookup,
620	/// A bad origin.
621	BadOrigin,
622	/// A custom error in a module.
623	Module(ModuleError),
624	/// At least one consumer is remaining so the account cannot be destroyed.
625	ConsumerRemaining,
626	/// There are no providers so the account cannot be created.
627	NoProviders,
628	/// There are too many consumers so the account cannot be created.
629	TooManyConsumers,
630	/// An error to do with tokens.
631	Token(TokenError),
632	/// An arithmetic error.
633	Arithmetic(ArithmeticError),
634	/// The number of transactional layers has been reached, or we are not in a transactional
635	/// layer.
636	Transactional(TransactionalError),
637	/// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate.
638	Exhausted,
639	/// The state is corrupt; this is generally not going to fix itself.
640	Corruption,
641	/// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later.
642	Unavailable,
643	/// Root origin is not allowed.
644	RootNotAllowed,
645	/// An error with tries.
646	Trie(TrieError),
647}
648
649/// Result of a `Dispatchable` which contains the `DispatchResult` and additional information about
650/// the `Dispatchable` that is only known post dispatch.
651#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, DecodeWithMemTracking, Debug, TypeInfo)]
652pub struct DispatchErrorWithPostInfo<Info>
653where
654	Info: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable,
655{
656	/// Additional information about the `Dispatchable` which is only known post dispatch.
657	pub post_info: Info,
658	/// The actual `DispatchResult` indicating whether the dispatch was successful.
659	pub error: DispatchError,
660}
661
662impl DispatchError {
663	/// Return the same error but without the attached message.
664	pub fn stripped(self) -> Self {
665		match self {
666			DispatchError::Module(ModuleError { index, error, message: Some(_) }) =>
667				DispatchError::Module(ModuleError { index, error, message: None }),
668			m => m,
669		}
670	}
671}
672
673impl<T, E> From<E> for DispatchErrorWithPostInfo<T>
674where
675	T: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable + Default,
676	E: Into<DispatchError>,
677{
678	fn from(error: E) -> Self {
679		Self { post_info: Default::default(), error: error.into() }
680	}
681}
682
683impl From<crate::traits::LookupError> for DispatchError {
684	fn from(_: crate::traits::LookupError) -> Self {
685		Self::CannotLookup
686	}
687}
688
689impl From<crate::traits::BadOrigin> for DispatchError {
690	fn from(_: crate::traits::BadOrigin) -> Self {
691		Self::BadOrigin
692	}
693}
694
695/// Description of what went wrong when trying to complete an operation on a token.
696#[derive(
697	Eq,
698	PartialEq,
699	Clone,
700	Copy,
701	Encode,
702	Decode,
703	DecodeWithMemTracking,
704	Debug,
705	TypeInfo,
706	MaxEncodedLen,
707)]
708#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
709pub enum TokenError {
710	/// Funds are unavailable.
711	FundsUnavailable,
712	/// Some part of the balance gives the only provider reference to the account and thus cannot
713	/// be (re)moved.
714	OnlyProvider,
715	/// Account cannot exist with the funds that would be given.
716	BelowMinimum,
717	/// Account cannot be created.
718	CannotCreate,
719	/// The asset in question is unknown.
720	UnknownAsset,
721	/// Funds exist but are frozen.
722	Frozen,
723	/// Operation is not supported by the asset.
724	Unsupported,
725	/// Account cannot be created for a held balance.
726	CannotCreateHold,
727	/// Withdrawal would cause unwanted loss of account.
728	NotExpendable,
729	/// Account cannot receive the assets.
730	Blocked,
731}
732
733impl From<TokenError> for &'static str {
734	fn from(e: TokenError) -> &'static str {
735		match e {
736			TokenError::FundsUnavailable => "Funds are unavailable",
737			TokenError::OnlyProvider => "Account that must exist would die",
738			TokenError::BelowMinimum => "Account cannot exist with the funds that would be given",
739			TokenError::CannotCreate => "Account cannot be created",
740			TokenError::UnknownAsset => "The asset in question is unknown",
741			TokenError::Frozen => "Funds exist but are frozen",
742			TokenError::Unsupported => "Operation is not supported by the asset",
743			TokenError::CannotCreateHold =>
744				"Account cannot be created for recording amount on hold",
745			TokenError::NotExpendable => "Account that is desired to remain would die",
746			TokenError::Blocked => "Account cannot receive the assets",
747		}
748	}
749}
750
751impl From<TokenError> for DispatchError {
752	fn from(e: TokenError) -> DispatchError {
753		Self::Token(e)
754	}
755}
756
757impl From<ArithmeticError> for DispatchError {
758	fn from(e: ArithmeticError) -> DispatchError {
759		Self::Arithmetic(e)
760	}
761}
762
763impl From<TrieError> for DispatchError {
764	fn from(e: TrieError) -> DispatchError {
765		Self::Trie(e)
766	}
767}
768
769impl From<&'static str> for DispatchError {
770	fn from(err: &'static str) -> DispatchError {
771		Self::Other(err)
772	}
773}
774
775impl From<DispatchError> for &'static str {
776	fn from(err: DispatchError) -> &'static str {
777		use DispatchError::*;
778		match err {
779			Other(msg) => msg,
780			CannotLookup => "Cannot lookup",
781			BadOrigin => "Bad origin",
782			Module(ModuleError { message, .. }) => message.unwrap_or("Unknown module error"),
783			ConsumerRemaining => "Consumer remaining",
784			NoProviders => "No providers",
785			TooManyConsumers => "Too many consumers",
786			Token(e) => e.into(),
787			Arithmetic(e) => e.into(),
788			Transactional(e) => e.into(),
789			Exhausted => "Resources exhausted",
790			Corruption => "State corrupt",
791			Unavailable => "Resource unavailable",
792			RootNotAllowed => "Root not allowed",
793			Trie(e) => e.into(),
794		}
795	}
796}
797
798impl<T> From<DispatchErrorWithPostInfo<T>> for &'static str
799where
800	T: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable,
801{
802	fn from(err: DispatchErrorWithPostInfo<T>) -> &'static str {
803		err.error.into()
804	}
805}
806
807impl traits::Printable for DispatchError {
808	fn print(&self) {
809		use DispatchError::*;
810		"DispatchError".print();
811		match self {
812			Other(err) => err.print(),
813			CannotLookup => "Cannot lookup".print(),
814			BadOrigin => "Bad origin".print(),
815			Module(ModuleError { index, error, message }) => {
816				index.print();
817				error.print();
818				if let Some(msg) = message {
819					msg.print();
820				}
821			},
822			ConsumerRemaining => "Consumer remaining".print(),
823			NoProviders => "No providers".print(),
824			TooManyConsumers => "Too many consumers".print(),
825			Token(e) => {
826				"Token error: ".print();
827				<&'static str>::from(*e).print();
828			},
829			Arithmetic(e) => {
830				"Arithmetic error: ".print();
831				<&'static str>::from(*e).print();
832			},
833			Transactional(e) => {
834				"Transactional error: ".print();
835				<&'static str>::from(*e).print();
836			},
837			Exhausted => "Resources exhausted".print(),
838			Corruption => "State corrupt".print(),
839			Unavailable => "Resource unavailable".print(),
840			RootNotAllowed => "Root not allowed".print(),
841			Trie(e) => {
842				"Trie error: ".print();
843				<&'static str>::from(*e).print();
844			},
845		}
846	}
847}
848
849impl<T> traits::Printable for DispatchErrorWithPostInfo<T>
850where
851	T: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable,
852{
853	fn print(&self) {
854		self.error.print();
855		"PostInfo: ".print();
856		self.post_info.print();
857	}
858}
859
860/// This type specifies the outcome of dispatching a call to a module.
861///
862/// In case of failure an error specific to the module is returned.
863///
864/// Failure of the module call dispatching doesn't invalidate the extrinsic and it is still included
865/// in the block, therefore all state changes performed by the dispatched call are still persisted.
866///
867/// For example, if the dispatching of an extrinsic involves inclusion fee payment then these
868/// changes are going to be preserved even if the call dispatched failed.
869pub type DispatchOutcome = Result<(), DispatchError>;
870
871/// The result of applying of an extrinsic.
872///
873/// This type is typically used in the context of `BlockBuilder` to signal that the extrinsic
874/// in question cannot be included.
875///
876/// A block containing extrinsics that have a negative inclusion outcome is invalid. A negative
877/// result can only occur during the block production, where such extrinsics are detected and
878/// removed from the block that is being created and the transaction pool.
879///
880/// To rehash: every extrinsic in a valid block must return a positive `ApplyExtrinsicResult`.
881///
882/// Examples of reasons preventing inclusion in a block:
883/// - More block weight is required to process the extrinsic than is left in the block being built.
884///   This doesn't necessarily mean that the extrinsic is invalid, since it can still be included in
885///   the next block if it has enough spare weight available.
886/// - The sender doesn't have enough funds to pay the transaction inclusion fee. Including such a
887///   transaction in the block doesn't make sense.
888/// - The extrinsic supplied a bad signature. This transaction won't become valid ever.
889pub type ApplyExtrinsicResult =
890	Result<DispatchOutcome, transaction_validity::TransactionValidityError>;
891
892/// Same as `ApplyExtrinsicResult` but augmented with `PostDispatchInfo` on success.
893pub type ApplyExtrinsicResultWithInfo<T> =
894	Result<DispatchResultWithInfo<T>, transaction_validity::TransactionValidityError>;
895
896/// The error type used as return type in try runtime hooks.
897pub type TryRuntimeError = DispatchError;
898
899/// Verify a signature on an encoded value in a lazy manner. This can be
900/// an optimization if the signature scheme has an "unsigned" escape hash.
901pub fn verify_encoded_lazy<V: Verify, T: codec::Encode>(
902	sig: &V,
903	item: &T,
904	signer: &<V::Signer as IdentifyAccount>::AccountId,
905) -> bool {
906	// The `Lazy<T>` trait expresses something like `X: FnMut<Output = for<'a> &'a T>`.
907	// unfortunately this is a lifetime relationship that can't
908	// be expressed without generic associated types, better unification of HRTBs in type position,
909	// and some kind of integration into the Fn* traits.
910	struct LazyEncode<F> {
911		inner: F,
912		encoded: Option<Vec<u8>>,
913	}
914
915	impl<F: Fn() -> Vec<u8>> traits::Lazy<[u8]> for LazyEncode<F> {
916		fn get(&mut self) -> &[u8] {
917			self.encoded.get_or_insert_with(&self.inner).as_slice()
918		}
919	}
920
921	sig.verify(LazyEncode { inner: || item.encode(), encoded: None }, signer)
922}
923
924/// Checks that `$x` is equal to `$y` with an error rate of `$error`.
925///
926/// # Example
927///
928/// ```rust
929/// # fn main() {
930/// sp_runtime::assert_eq_error_rate!(10, 10, 0);
931/// sp_runtime::assert_eq_error_rate!(10, 11, 1);
932/// sp_runtime::assert_eq_error_rate!(12, 10, 2);
933/// # }
934/// ```
935///
936/// ```rust,should_panic
937/// # fn main() {
938/// sp_runtime::assert_eq_error_rate!(12, 10, 1);
939/// # }
940/// ```
941#[macro_export]
942#[cfg(feature = "std")]
943macro_rules! assert_eq_error_rate {
944	($x:expr, $y:expr, $error:expr $(,)?) => {
945		assert!(
946			($x >= $crate::Saturating::saturating_sub($y, $error)) &&
947				($x <= $crate::Saturating::saturating_add($y, $error)),
948			"{:?} != {:?} (with error rate {:?})",
949			$x,
950			$y,
951			$error,
952		);
953	};
954}
955
956/// Same as [`assert_eq_error_rate`], but intended to be used with floating point number, or
957/// generally those who do not have over/underflow potentials.
958#[macro_export]
959#[cfg(feature = "std")]
960macro_rules! assert_eq_error_rate_float {
961	($x:expr, $y:expr, $error:expr $(,)?) => {
962		assert!(
963			($x >= $y - $error) && ($x <= $y + $error),
964			"{:?} != {:?} (with error rate {:?})",
965			$x,
966			$y,
967			$error,
968		);
969	};
970}
971
972/// Simple blob to hold an extrinsic without committing to its format and ensure it is serialized
973/// correctly.
974#[derive(PartialEq, Eq, Clone, Default, Encode, Decode, DecodeWithMemTracking, TypeInfo)]
975pub struct OpaqueExtrinsic(Vec<u8>);
976
977impl OpaqueExtrinsic {
978	/// Convert an encoded extrinsic to an `OpaqueExtrinsic`.
979	pub fn from_bytes(mut bytes: &[u8]) -> Result<Self, codec::Error> {
980		Self::decode(&mut bytes)
981	}
982}
983
984impl core::fmt::Debug for OpaqueExtrinsic {
985	#[cfg(feature = "std")]
986	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
987		write!(fmt, "{}", sp_core::hexdisplay::HexDisplay::from(&self.0))
988	}
989
990	#[cfg(not(feature = "std"))]
991	fn fmt(&self, _fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
992		Ok(())
993	}
994}
995
996#[cfg(feature = "serde")]
997impl ::serde::Serialize for OpaqueExtrinsic {
998	fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
999	where
1000		S: ::serde::Serializer,
1001	{
1002		codec::Encode::using_encoded(&self.0, |bytes| ::sp_core::bytes::serialize(bytes, seq))
1003	}
1004}
1005
1006#[cfg(feature = "serde")]
1007impl<'a> ::serde::Deserialize<'a> for OpaqueExtrinsic {
1008	fn deserialize<D>(de: D) -> Result<Self, D::Error>
1009	where
1010		D: ::serde::Deserializer<'a>,
1011	{
1012		let r = ::sp_core::bytes::deserialize(de)?;
1013		Decode::decode(&mut &r[..])
1014			.map_err(|e| ::serde::de::Error::custom(alloc::format!("Decode error: {}", e)))
1015	}
1016}
1017
1018impl traits::ExtrinsicLike for OpaqueExtrinsic {
1019	fn is_bare(&self) -> bool {
1020		false
1021	}
1022}
1023
1024/// Print something that implements `Printable` from the runtime.
1025pub fn print(print: impl traits::Printable) {
1026	print.print();
1027}
1028
1029/// Utility function to declare string literals backed by an array of length N.
1030///
1031/// The input can be shorter than N, in that case the end of the array is padded with zeros.
1032///
1033/// [`str_array`] is useful when converting strings that end up in the storage as fixed size arrays
1034/// or in const contexts where static data types have strings that could also end up in the storage.
1035///
1036/// # Example
1037///
1038/// ```rust
1039/// # use sp_runtime::str_array;
1040/// const MY_STR: [u8; 6] = str_array("data");
1041/// assert_eq!(MY_STR, *b"data\0\0");
1042/// ```
1043pub const fn str_array<const N: usize>(s: &str) -> [u8; N] {
1044	debug_assert!(s.len() <= N, "String literal doesn't fit in array");
1045	let mut i = 0;
1046	let mut arr = [0; N];
1047	let s = s.as_bytes();
1048	while i < s.len() {
1049		arr[i] = s[i];
1050		i += 1;
1051	}
1052	arr
1053}
1054
1055/// Describes on what should happen with a storage transaction.
1056pub enum TransactionOutcome<R> {
1057	/// Commit the transaction.
1058	Commit(R),
1059	/// Rollback the transaction.
1060	Rollback(R),
1061}
1062
1063impl<R> TransactionOutcome<R> {
1064	/// Convert into the inner type.
1065	pub fn into_inner(self) -> R {
1066		match self {
1067			Self::Commit(r) => r,
1068			Self::Rollback(r) => r,
1069		}
1070	}
1071}
1072
1073/// Confines the kind of extrinsics that can be included in a block.
1074#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Encode, Decode, TypeInfo)]
1075pub enum ExtrinsicInclusionMode {
1076	/// All extrinsics are allowed to be included in this block.
1077	#[default]
1078	AllExtrinsics,
1079	/// Inherents are allowed to be included.
1080	OnlyInherents,
1081}
1082
1083/// Simple blob that hold a value in an encoded form without committing to its type.
1084#[derive(Decode, Encode, PartialEq, TypeInfo)]
1085pub struct OpaqueValue(Vec<u8>);
1086impl OpaqueValue {
1087	/// Create a new `OpaqueValue` using the given encoded representation.
1088	pub fn new(inner: Vec<u8>) -> OpaqueValue {
1089		OpaqueValue(inner)
1090	}
1091
1092	/// Try to decode this `OpaqueValue` into the given concrete type.
1093	pub fn decode<T: Decode>(&self) -> Option<T> {
1094		Decode::decode(&mut &self.0[..]).ok()
1095	}
1096}
1097
1098// TODO: Remove in future versions and clean up `parse_str_literal` in `sp-version-proc-macro`
1099/// Deprecated `Cow::Borrowed()` wrapper.
1100#[macro_export]
1101#[deprecated = "Use Cow::Borrowed() instead of create_runtime_str!()"]
1102macro_rules! create_runtime_str {
1103	( $y:expr ) => {{
1104		$crate::Cow::Borrowed($y)
1105	}};
1106}
1107// TODO: Re-export for ^ macro `create_runtime_str`, should be removed once macro is gone
1108#[doc(hidden)]
1109pub use alloc::borrow::Cow;
1110// TODO: Remove in future versions
1111/// Deprecated alias to improve upgrade experience
1112#[deprecated = "Use String or Cow<'static, str> instead"]
1113pub type RuntimeString = alloc::string::String;
1114
1115#[cfg(test)]
1116mod tests {
1117	use crate::traits::BlakeTwo256;
1118
1119	use super::*;
1120	use codec::{Decode, Encode};
1121	use sp_core::crypto::Pair;
1122	use sp_io::TestExternalities;
1123	use sp_state_machine::create_proof_check_backend;
1124
1125	#[test]
1126	fn opaque_extrinsic_serialization() {
1127		let ex = super::OpaqueExtrinsic(vec![1, 2, 3, 4]);
1128		assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x1001020304\"".to_owned());
1129	}
1130
1131	#[test]
1132	fn dispatch_error_encoding() {
1133		let error = DispatchError::Module(ModuleError {
1134			index: 1,
1135			error: [2, 0, 0, 0],
1136			message: Some("error message"),
1137		});
1138		let encoded = error.encode();
1139		let decoded = DispatchError::decode(&mut &encoded[..]).unwrap();
1140		assert_eq!(encoded, vec![3, 1, 2, 0, 0, 0]);
1141		assert_eq!(
1142			decoded,
1143			DispatchError::Module(ModuleError { index: 1, error: [2, 0, 0, 0], message: None })
1144		);
1145	}
1146
1147	#[test]
1148	fn dispatch_error_equality() {
1149		use DispatchError::*;
1150
1151		let variants = vec![
1152			Other("foo"),
1153			Other("bar"),
1154			CannotLookup,
1155			BadOrigin,
1156			Module(ModuleError { index: 1, error: [1, 0, 0, 0], message: None }),
1157			Module(ModuleError { index: 1, error: [2, 0, 0, 0], message: None }),
1158			Module(ModuleError { index: 2, error: [1, 0, 0, 0], message: None }),
1159			ConsumerRemaining,
1160			NoProviders,
1161			Token(TokenError::FundsUnavailable),
1162			Token(TokenError::OnlyProvider),
1163			Token(TokenError::BelowMinimum),
1164			Token(TokenError::CannotCreate),
1165			Token(TokenError::UnknownAsset),
1166			Token(TokenError::Frozen),
1167			Arithmetic(ArithmeticError::Overflow),
1168			Arithmetic(ArithmeticError::Underflow),
1169			Arithmetic(ArithmeticError::DivisionByZero),
1170		];
1171		for (i, variant) in variants.iter().enumerate() {
1172			for (j, other_variant) in variants.iter().enumerate() {
1173				if i == j {
1174					assert_eq!(variant, other_variant);
1175				} else {
1176					assert_ne!(variant, other_variant);
1177				}
1178			}
1179		}
1180
1181		// Ignores `message` field in `Module` variant.
1182		assert_eq!(
1183			Module(ModuleError { index: 1, error: [1, 0, 0, 0], message: Some("foo") }),
1184			Module(ModuleError { index: 1, error: [1, 0, 0, 0], message: None }),
1185		);
1186	}
1187
1188	#[test]
1189	fn multi_signature_ecdsa_verify_works() {
1190		let msg = &b"test-message"[..];
1191		let (pair, _) = ecdsa::Pair::generate();
1192
1193		let signature = pair.sign(&msg);
1194		assert!(ecdsa::Pair::verify(&signature, msg, &pair.public()));
1195
1196		let multi_sig = MultiSignature::from(signature);
1197		let multi_signer = MultiSigner::from(pair.public());
1198		assert!(multi_sig.verify(msg, &multi_signer.into_account()));
1199
1200		let multi_signer = MultiSigner::from(pair.public());
1201		assert!(multi_sig.verify(msg, &multi_signer.into_account()));
1202	}
1203
1204	#[test]
1205	fn execute_and_generate_proof_works() {
1206		use codec::Encode;
1207		use sp_state_machine::Backend;
1208		let mut ext = TestExternalities::default();
1209
1210		ext.insert(b"a".to_vec(), vec![1u8; 33]);
1211		ext.insert(b"b".to_vec(), vec![2u8; 33]);
1212		ext.insert(b"c".to_vec(), vec![3u8; 33]);
1213		ext.insert(b"d".to_vec(), vec![4u8; 33]);
1214
1215		let pre_root = *ext.backend.root();
1216		let (_, proof) = ext.execute_and_prove(|| {
1217			sp_io::storage::get(b"a");
1218			sp_io::storage::get(b"b");
1219			sp_io::storage::get(b"v");
1220			sp_io::storage::get(b"d");
1221		});
1222
1223		let compact_proof = proof.clone().into_compact_proof::<BlakeTwo256>(pre_root).unwrap();
1224		let compressed_proof = zstd::stream::encode_all(&compact_proof.encode()[..], 0).unwrap();
1225
1226		// just an example of how you'd inspect the size of the proof.
1227		println!("proof size: {:?}", proof.encoded_size());
1228		println!("compact proof size: {:?}", compact_proof.encoded_size());
1229		println!("zstd-compressed compact proof size: {:?}", &compressed_proof.len());
1230
1231		// create a new trie-backed from the proof and make sure it contains everything
1232		let proof_check = create_proof_check_backend::<BlakeTwo256>(pre_root, proof).unwrap();
1233		assert_eq!(proof_check.storage(b"a",).unwrap().unwrap(), vec![1u8; 33]);
1234
1235		let _ = ext.execute_and_prove(|| {
1236			sp_io::storage::set(b"a", &vec![1u8; 44]);
1237		});
1238
1239		// ensure that these changes are propagated to the backend.
1240
1241		ext.execute_with(|| {
1242			assert_eq!(sp_io::storage::get(b"a").unwrap(), vec![1u8; 44]);
1243			assert_eq!(sp_io::storage::get(b"b").unwrap(), vec![2u8; 33]);
1244		});
1245	}
1246}
1247
1248// NOTE: we have to test the sp_core stuff also from a different crate to check that the macro
1249// can access the sp_core crate.
1250#[cfg(test)]
1251mod sp_core_tests {
1252	use super::*;
1253
1254	sp_core::generate_feature_enabled_macro!(if_test, test, $);
1255	sp_core::generate_feature_enabled_macro!(if_not_test, not(test), $);
1256
1257	#[test]
1258	#[should_panic]
1259	fn generate_feature_enabled_macro_panics() {
1260		if_test!(panic!("This should panic"));
1261	}
1262
1263	#[test]
1264	fn generate_feature_enabled_macro_works() {
1265		if_not_test!(panic!("This should not panic"));
1266	}
1267}