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