sp_version/
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 version
19//!
20//! Each runtime that should be executed by a Substrate based node needs to have a runtime version.
21//! The runtime version is defined by [`RuntimeVersion`]. The runtime version is used to
22//! distinguish different runtimes. The most important field is the
23//! [`spec_version`](RuntimeVersion::spec_version). The `spec_version` should be increased in a
24//! runtime when a new runtime build includes breaking changes that would make other runtimes unable
25//! to import blocks built by this runtime or vice-versa, where the new runtime could not import
26//! blocks built by the old runtime. The runtime version also carries other version information
27//! about the runtime, see [`RuntimeVersion`] for more information on this.
28//!
29//! Substrate will fetch the runtime version from a `wasm` blob by first checking the
30//! `runtime_version` link section or calling the `Core::version` runtime api. The link section can
31//! be generated in the runtime using the [`runtime_version`] attribute. The `Core` runtime api also
32//! needs to be implemented for the runtime using `impl_runtime_apis!`.
33
34#![cfg_attr(not(feature = "std"), no_std)]
35
36extern crate alloc;
37
38#[cfg(any(feature = "std", feature = "serde"))]
39use alloc::fmt;
40#[cfg(feature = "serde")]
41use serde::{Deserialize, Serialize};
42#[cfg(feature = "std")]
43use std::collections::HashSet;
44
45#[doc(hidden)]
46pub use alloc::borrow::Cow;
47use codec::{Decode, Encode, Input};
48use scale_info::TypeInfo;
49use sp_runtime::RuntimeString;
50pub use sp_runtime::{create_runtime_str, StateVersion};
51#[doc(hidden)]
52pub use sp_std;
53
54#[cfg(feature = "std")]
55use sp_runtime::traits::Block as BlockT;
56
57#[cfg(feature = "std")]
58pub mod embed;
59
60/// An attribute that accepts a version declaration of a runtime and generates a custom wasm
61/// section with the equivalent contents.
62///
63/// The custom section allows to read the version of the runtime without having to execute any
64/// code. Instead, the generated custom section can be relatively easily parsed from the wasm
65/// binary. The identifier of the custom section is "runtime_version".
66///
67/// A shortcoming of this macro is that it is unable to embed information regarding supported
68/// APIs. This is supported by the `construct_runtime!` macro.
69///
70/// # Usage
71///
72/// This macro accepts a const item like the following:
73///
74/// ```rust
75/// use sp_version::{create_runtime_str, RuntimeVersion};
76///
77/// #[sp_version::runtime_version]
78/// pub const VERSION: RuntimeVersion = RuntimeVersion {
79/// 	spec_name: create_runtime_str!("test"),
80/// 	impl_name: create_runtime_str!("test"),
81/// 	authoring_version: 10,
82/// 	spec_version: 265,
83/// 	impl_version: 1,
84/// 	apis: RUNTIME_API_VERSIONS,
85/// 	transaction_version: 2,
86/// 	system_version: 1,
87/// };
88///
89/// # const RUNTIME_API_VERSIONS: sp_version::ApisVec = sp_version::create_apis_vec!([]);
90/// ```
91///
92/// It will pass it through and add code required for emitting a custom section. The
93/// information that will go into the custom section is parsed from the item declaration. Due
94/// to that, the macro is somewhat rigid in terms of the code it accepts. There are the
95/// following considerations:
96///
97/// - The `spec_name` and `impl_name` must be set by a macro-like expression. The name of the
98///   macro doesn't matter though.
99///
100/// - `authoring_version`, `spec_version`, `impl_version` and `transaction_version` must be set
101///   by a literal. Literal must be an integer. No other expressions are allowed there. In
102///   particular, you can't supply a constant variable.
103///
104/// - `apis` doesn't have any specific constraints. This is because this information doesn't
105///   get into the custom section and is not parsed.
106///
107/// # Compilation Target & "std" feature
108///
109/// This macro assumes it will be used within a runtime. By convention, a runtime crate defines
110/// a feature named "std". This feature is enabled when the runtime is compiled to native code
111/// and disabled when it is compiled to the wasm code.
112///
113/// The custom section can only be emitted while compiling to wasm. In order to detect the
114/// compilation target we use the "std" feature. This macro will emit the custom section only
115/// if the "std" feature is **not** enabled.
116///
117/// Including this macro in the context where there is no "std" feature and the code is not
118/// compiled to wasm can lead to cryptic linking errors.
119pub use sp_version_proc_macro::runtime_version;
120
121/// The identity of a particular API interface that the runtime might provide.
122///
123/// The id is generated by hashing the name of the runtime api with BLAKE2 using a hash size
124/// of 8 bytes.
125///
126/// The name of the runtime api is the name of the trait when using `decl_runtime_apis!` macro. So,
127/// in the following runtime api declaration:
128///
129/// ```nocompile
130/// decl_runtime_apis! {
131///     trait TestApi {
132///         fn do_test();
133///     }
134/// }
135/// ```
136///
137/// The name of the trait would be `TestApi` and would be taken as input to the BLAKE2 hash
138/// function.
139///
140/// As Rust supports renaming of traits, the name of a runtime api given to `impl_runtime_apis!`
141/// doesn't need to be the same as in `decl_runtime_apis!`, but only the name in
142/// `decl_runtime_apis!` is the important one!
143pub type ApiId = [u8; 8];
144
145/// A vector of pairs of `ApiId` and a `u32` for version.
146pub type ApisVec = alloc::borrow::Cow<'static, [(ApiId, u32)]>;
147
148/// Create a vector of Api declarations.
149#[macro_export]
150macro_rules! create_apis_vec {
151	( $y:expr ) => {
152		$crate::Cow::Borrowed(&$y)
153	};
154}
155
156/// Runtime version.
157/// This should not be thought of as classic Semver (major/minor/tiny).
158/// This triplet have different semantics and mis-interpretation could cause problems.
159/// In particular: bug fixes should result in an increment of `spec_version` and possibly
160/// `authoring_version`, absolutely not `impl_version` since they change the semantics of the
161/// runtime.
162#[derive(Clone, PartialEq, Eq, Encode, Default, sp_runtime::RuntimeDebug, TypeInfo)]
163pub struct RuntimeVersion {
164	/// Identifies the different Substrate runtimes. There'll be at least polkadot and node.
165	/// A different on-chain spec_name to that of the native runtime would normally result
166	/// in node not attempting to sync or author blocks.
167	pub spec_name: RuntimeString,
168
169	/// Name of the implementation of the spec. This is of little consequence for the node
170	/// and serves only to differentiate code of different implementation teams. For this
171	/// codebase, it will be parity-polkadot. If there were a non-Rust implementation of the
172	/// Polkadot runtime (e.g. C++), then it would identify itself with an accordingly different
173	/// `impl_name`.
174	pub impl_name: RuntimeString,
175
176	/// `authoring_version` is the version of the authorship interface. An authoring node
177	/// will not attempt to author blocks unless this is equal to its native runtime.
178	pub authoring_version: u32,
179
180	/// Version of the runtime specification.
181	///
182	/// A full-node will not attempt to use its native runtime in substitute for the on-chain
183	/// Wasm runtime unless all of `spec_name`, `spec_version` and `authoring_version` are the same
184	/// between Wasm and native.
185	///
186	/// This number should never decrease.
187	pub spec_version: u32,
188
189	/// Version of the implementation of the specification.
190	///
191	/// Nodes are free to ignore this; it serves only as an indication that the code is different;
192	/// as long as the other two versions are the same then while the actual code may be different,
193	/// it is nonetheless required to do the same thing. Non-consensus-breaking optimizations are
194	/// about the only changes that could be made which would result in only the `impl_version`
195	/// changing.
196	///
197	/// This number can be reverted to `0` after a [`spec_version`](Self::spec_version) bump.
198	pub impl_version: u32,
199
200	/// List of supported API "features" along with their versions.
201	pub apis: ApisVec,
202
203	/// All existing calls (dispatchables) are fully compatible when this number doesn't change. If
204	/// this number changes, then [`spec_version`](Self::spec_version) must change, also.
205	///
206	/// This number must change when an existing call (pallet index, call index) is changed,
207	/// either through an alteration in its user-level semantics, a parameter
208	/// added/removed, a parameter type changed, or a call/pallet changing its index. An alteration
209	/// of the user level semantics is for example when the call was before `transfer` and now is
210	/// `transfer_all`, the semantics of the call changed completely.
211	///
212	/// Removing a pallet or a call doesn't require a *bump* as long as no pallet or call is put at
213	/// the same index. Removing doesn't require a bump as the chain will reject a transaction
214	/// referencing this removed call/pallet while decoding and thus, the user isn't at risk to
215	/// execute any unknown call. FRAME runtime devs have control over the index of a call/pallet
216	/// to prevent that an index gets reused.
217	///
218	/// Adding a new pallet or call also doesn't require a *bump* as long as they also don't reuse
219	/// any previously used index.
220	///
221	/// This number should never decrease.
222	pub transaction_version: u32,
223
224	/// Version of the system implementation used by this runtime.
225	/// Use of an incorrect version is consensus breaking.
226	pub system_version: u8,
227}
228
229// Manual implementation in order to sprinkle `stateVersion` at the end for migration purposes
230// after the field was renamed from `state_version` to `system_version`
231#[cfg(feature = "serde")]
232impl serde::Serialize for RuntimeVersion {
233	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
234	where
235		S: serde::Serializer,
236	{
237		use serde::ser::SerializeStruct;
238
239		let mut state = serializer.serialize_struct("RuntimeVersion", 9)?;
240		state.serialize_field("specName", &self.spec_name)?;
241		state.serialize_field("implName", &self.impl_name)?;
242		state.serialize_field("authoringVersion", &self.authoring_version)?;
243		state.serialize_field("specVersion", &self.spec_version)?;
244		state.serialize_field("implVersion", &self.impl_version)?;
245		state.serialize_field("apis", {
246			struct SerializeWith<'a>(&'a ApisVec);
247
248			impl<'a> serde::Serialize for SerializeWith<'a> {
249				fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
250				where
251					S: serde::Serializer,
252				{
253					apis_serialize::serialize(self.0, serializer)
254				}
255			}
256
257			&SerializeWith(&self.apis)
258		})?;
259		state.serialize_field("transactionVersion", &self.transaction_version)?;
260		state.serialize_field("systemVersion", &self.system_version)?;
261		state.serialize_field("stateVersion", &self.system_version)?;
262		state.end()
263	}
264}
265
266// Manual implementation in order to allow both old `stateVersion` and new `systemVersion` to be
267// present at the same time
268#[cfg(feature = "serde")]
269impl<'de> serde::Deserialize<'de> for RuntimeVersion {
270	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
271	where
272		D: serde::Deserializer<'de>,
273	{
274		use core::marker::PhantomData;
275
276		enum Field {
277			SpecName,
278			ImplName,
279			AuthoringVersion,
280			SpecVersion,
281			ImplVersion,
282			Apis,
283			TransactionVersion,
284			SystemVersion,
285			Ignore,
286		}
287
288		struct FieldVisitor;
289
290		impl<'de> serde::de::Visitor<'de> for FieldVisitor {
291			type Value = Field;
292
293			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
294				formatter.write_str("field identifier")
295			}
296
297			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
298			where
299				E: serde::de::Error,
300			{
301				match value {
302					0 => Ok(Field::SpecName),
303					1 => Ok(Field::ImplName),
304					2 => Ok(Field::AuthoringVersion),
305					3 => Ok(Field::SpecVersion),
306					4 => Ok(Field::ImplVersion),
307					5 => Ok(Field::Apis),
308					6 => Ok(Field::TransactionVersion),
309					7 => Ok(Field::SystemVersion),
310					_ => Ok(Field::Ignore),
311				}
312			}
313
314			fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
315			where
316				E: serde::de::Error,
317			{
318				match value {
319					"specName" => Ok(Field::SpecName),
320					"implName" => Ok(Field::ImplName),
321					"authoringVersion" => Ok(Field::AuthoringVersion),
322					"specVersion" => Ok(Field::SpecVersion),
323					"implVersion" => Ok(Field::ImplVersion),
324					"apis" => Ok(Field::Apis),
325					"transactionVersion" => Ok(Field::TransactionVersion),
326					"systemVersion" | "stateVersion" => Ok(Field::SystemVersion),
327					_ => Ok(Field::Ignore),
328				}
329			}
330
331			fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
332			where
333				E: serde::de::Error,
334			{
335				match value {
336					b"specName" => Ok(Field::SpecName),
337					b"implName" => Ok(Field::ImplName),
338					b"authoringVersion" => Ok(Field::AuthoringVersion),
339					b"specVersion" => Ok(Field::SpecVersion),
340					b"implVersion" => Ok(Field::ImplVersion),
341					b"apis" => Ok(Field::Apis),
342					b"transactionVersion" => Ok(Field::TransactionVersion),
343					b"systemVersion" | b"stateVersion" => Ok(Field::SystemVersion),
344					_ => Ok(Field::Ignore),
345				}
346			}
347		}
348
349		impl<'de> serde::Deserialize<'de> for Field {
350			#[inline]
351			fn deserialize<E>(deserializer: E) -> Result<Self, E::Error>
352			where
353				E: serde::Deserializer<'de>,
354			{
355				deserializer.deserialize_identifier(FieldVisitor)
356			}
357		}
358
359		struct Visitor<'de> {
360			lifetime: PhantomData<&'de ()>,
361		}
362		impl<'de> serde::de::Visitor<'de> for Visitor<'de> {
363			type Value = RuntimeVersion;
364
365			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
366				formatter.write_str("struct RuntimeVersion")
367			}
368
369			#[inline]
370			fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
371			where
372				A: serde::de::SeqAccess<'de>,
373			{
374				let spec_name = match seq.next_element()? {
375					Some(spec_name) => spec_name,
376					None =>
377						return Err(serde::de::Error::invalid_length(
378							0usize,
379							&"struct RuntimeVersion with 8 elements",
380						)),
381				};
382				let impl_name = match seq.next_element()? {
383					Some(impl_name) => impl_name,
384					None =>
385						return Err(serde::de::Error::invalid_length(
386							1usize,
387							&"struct RuntimeVersion with 8 elements",
388						)),
389				};
390				let authoring_version = match seq.next_element()? {
391					Some(authoring_version) => authoring_version,
392					None =>
393						return Err(serde::de::Error::invalid_length(
394							2usize,
395							&"struct RuntimeVersion with 8 elements",
396						)),
397				};
398				let spec_version = match seq.next_element()? {
399					Some(spec_version) => spec_version,
400					None =>
401						return Err(serde::de::Error::invalid_length(
402							3usize,
403							&"struct RuntimeVersion with 8 elements",
404						)),
405				};
406				let impl_version = match seq.next_element()? {
407					Some(impl_version) => impl_version,
408					None =>
409						return Err(serde::de::Error::invalid_length(
410							4usize,
411							&"struct RuntimeVersion with 8 elements",
412						)),
413				};
414				let apis = match {
415					struct DeserializeWith<'de> {
416						value: ApisVec,
417
418						phantom: PhantomData<RuntimeVersion>,
419						lifetime: PhantomData<&'de ()>,
420					}
421					impl<'de> serde::Deserialize<'de> for DeserializeWith<'de> {
422						fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
423						where
424							D: serde::Deserializer<'de>,
425						{
426							Ok(DeserializeWith {
427								value: apis_serialize::deserialize(deserializer)?,
428								phantom: PhantomData,
429								lifetime: PhantomData,
430							})
431						}
432					}
433					seq.next_element::<DeserializeWith<'de>>()?.map(|wrap| wrap.value)
434				} {
435					Some(apis) => apis,
436					None =>
437						return Err(serde::de::Error::invalid_length(
438							5usize,
439							&"struct RuntimeVersion with 8 elements",
440						)),
441				};
442				let transaction_version = match seq.next_element()? {
443					Some(transaction_version) => transaction_version,
444					None =>
445						return Err(serde::de::Error::invalid_length(
446							6usize,
447							&"struct RuntimeVersion with 8 elements",
448						)),
449				};
450				let system_version = match seq.next_element()? {
451					Some(system_version) => system_version,
452					None =>
453						return Err(serde::de::Error::invalid_length(
454							7usize,
455							&"struct RuntimeVersion with 8 elements",
456						)),
457				};
458				Ok(RuntimeVersion {
459					spec_name,
460					impl_name,
461					authoring_version,
462					spec_version,
463					impl_version,
464					apis,
465					transaction_version,
466					system_version,
467				})
468			}
469
470			#[inline]
471			fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
472			where
473				A: serde::de::MapAccess<'de>,
474			{
475				let mut spec_name: Option<RuntimeString> = None;
476				let mut impl_name: Option<RuntimeString> = None;
477				let mut authoring_version: Option<u32> = None;
478				let mut spec_version: Option<u32> = None;
479				let mut impl_version: Option<u32> = None;
480				let mut apis: Option<ApisVec> = None;
481				let mut transaction_version: Option<u32> = None;
482				let mut system_version: Option<u8> = None;
483
484				while let Some(key) = map.next_key()? {
485					match key {
486						Field::SpecName => {
487							if spec_name.is_some() {
488								return Err(<A::Error as serde::de::Error>::duplicate_field(
489									"specName",
490								));
491							}
492							spec_name = Some(map.next_value()?);
493						},
494						Field::ImplName => {
495							if impl_name.is_some() {
496								return Err(<A::Error as serde::de::Error>::duplicate_field(
497									"implName",
498								));
499							}
500							impl_name = Some(map.next_value()?);
501						},
502						Field::AuthoringVersion => {
503							if authoring_version.is_some() {
504								return Err(<A::Error as serde::de::Error>::duplicate_field(
505									"authoringVersion",
506								));
507							}
508							authoring_version = Some(map.next_value()?);
509						},
510						Field::SpecVersion => {
511							if spec_version.is_some() {
512								return Err(<A::Error as serde::de::Error>::duplicate_field(
513									"specVersion",
514								));
515							}
516							spec_version = Some(map.next_value()?);
517						},
518						Field::ImplVersion => {
519							if impl_version.is_some() {
520								return Err(<A::Error as serde::de::Error>::duplicate_field(
521									"implVersion",
522								));
523							}
524							impl_version = Some(map.next_value()?);
525						},
526						Field::Apis => {
527							if apis.is_some() {
528								return Err(<A::Error as serde::de::Error>::duplicate_field("apis"));
529							}
530							apis = Some({
531								struct DeserializeWith<'de> {
532									value: ApisVec,
533									lifetime: PhantomData<&'de ()>,
534								}
535								impl<'de> serde::Deserialize<'de> for DeserializeWith<'de> {
536									fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
537									where
538										D: serde::Deserializer<'de>,
539									{
540										Ok(DeserializeWith {
541											value: apis_serialize::deserialize(deserializer)?,
542											lifetime: PhantomData,
543										})
544									}
545								}
546
547								map.next_value::<DeserializeWith<'de>>()?.value
548							});
549						},
550						Field::TransactionVersion => {
551							if transaction_version.is_some() {
552								return Err(<A::Error as serde::de::Error>::duplicate_field(
553									"transactionVersion",
554								));
555							}
556							transaction_version = Some(map.next_value()?);
557						},
558						Field::SystemVersion =>
559							if let Some(system_version) = system_version {
560								let new_value = map.next_value::<u8>()?;
561								if system_version != new_value {
562									return Err(<A::Error as serde::de::Error>::custom(
563										alloc::format!(
564											r#"Duplicated "stateVersion" and "systemVersion" \
565											fields must have the same value, but different values \
566											were provided: {system_version} vs {new_value}"#
567										),
568									));
569								}
570							} else {
571								system_version = Some(map.next_value()?);
572							},
573						_ => {
574							let _ = map.next_value::<serde::de::IgnoredAny>()?;
575						},
576					}
577				}
578				let spec_name = spec_name
579					.ok_or_else(|| <A::Error as serde::de::Error>::missing_field("specName"))?;
580				let impl_name = impl_name
581					.ok_or_else(|| <A::Error as serde::de::Error>::missing_field("implName"))?;
582				let authoring_version = authoring_version.ok_or_else(|| {
583					<A::Error as serde::de::Error>::missing_field("authoringVersion")
584				})?;
585				let spec_version = spec_version
586					.ok_or_else(|| <A::Error as serde::de::Error>::missing_field("specVersion"))?;
587				let impl_version = impl_version
588					.ok_or_else(|| <A::Error as serde::de::Error>::missing_field("implVersion"))?;
589				let apis =
590					apis.ok_or_else(|| <A::Error as serde::de::Error>::missing_field("apis"))?;
591				let transaction_version = transaction_version.ok_or_else(|| {
592					<A::Error as serde::de::Error>::missing_field("transactionVersion")
593				})?;
594				let system_version = system_version.ok_or_else(|| {
595					<A::Error as serde::de::Error>::missing_field("systemVersion")
596				})?;
597				Ok(RuntimeVersion {
598					spec_name,
599					impl_name,
600					authoring_version,
601					spec_version,
602					impl_version,
603					apis,
604					transaction_version,
605					system_version,
606				})
607			}
608		}
609
610		const FIELDS: &[&str] = &[
611			"specName",
612			"implName",
613			"authoringVersion",
614			"specVersion",
615			"implVersion",
616			"apis",
617			"transactionVersion",
618			"stateVersion",
619			"systemVersion",
620		];
621
622		deserializer.deserialize_struct("RuntimeVersion", FIELDS, Visitor { lifetime: PhantomData })
623	}
624}
625
626impl RuntimeVersion {
627	/// `Decode` while giving a "version hint"
628	///
629	/// There exists multiple versions of [`RuntimeVersion`] and they are versioned using the `Core`
630	/// runtime api:
631	/// - `Core` version < 3 is a runtime version without a transaction version and state version.
632	/// - `Core` version 3 is a runtime version without a state version.
633	/// - `Core` version 4 is the latest runtime version.
634	pub fn decode_with_version_hint<I: Input>(
635		input: &mut I,
636		core_version: Option<u32>,
637	) -> Result<RuntimeVersion, codec::Error> {
638		let spec_name = Decode::decode(input)?;
639		let impl_name = Decode::decode(input)?;
640		let authoring_version = Decode::decode(input)?;
641		let spec_version = Decode::decode(input)?;
642		let impl_version = Decode::decode(input)?;
643		let apis = Decode::decode(input)?;
644		let core_version =
645			if core_version.is_some() { core_version } else { core_version_from_apis(&apis) };
646		let transaction_version =
647			if core_version.map(|v| v >= 3).unwrap_or(false) { Decode::decode(input)? } else { 1 };
648		let system_version =
649			if core_version.map(|v| v >= 4).unwrap_or(false) { Decode::decode(input)? } else { 0 };
650		Ok(RuntimeVersion {
651			spec_name,
652			impl_name,
653			authoring_version,
654			spec_version,
655			impl_version,
656			apis,
657			transaction_version,
658			system_version,
659		})
660	}
661}
662
663impl Decode for RuntimeVersion {
664	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
665		Self::decode_with_version_hint(input, None)
666	}
667}
668
669#[cfg(feature = "std")]
670impl fmt::Display for RuntimeVersion {
671	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
672		write!(
673			f,
674			"{}-{} ({}-{}.tx{}.au{})",
675			self.spec_name,
676			self.spec_version,
677			self.impl_name,
678			self.impl_version,
679			self.transaction_version,
680			self.authoring_version,
681		)
682	}
683}
684
685#[cfg(feature = "std")]
686fn has_api_with<P: Fn(u32) -> bool>(apis: &ApisVec, id: &ApiId, predicate: P) -> bool {
687	apis.iter().any(|(s, v)| s == id && predicate(*v))
688}
689
690/// Returns the version of the `Core` runtime api.
691pub fn core_version_from_apis(apis: &ApisVec) -> Option<u32> {
692	let id = sp_crypto_hashing_proc_macro::blake2b_64!(b"Core");
693	apis.iter().find(|(s, _v)| s == &id).map(|(_s, v)| *v)
694}
695
696#[cfg(feature = "std")]
697impl RuntimeVersion {
698	/// Check if this version matches other version for calling into runtime.
699	pub fn can_call_with(&self, other: &RuntimeVersion) -> bool {
700		self.spec_version == other.spec_version &&
701			self.spec_name == other.spec_name &&
702			self.authoring_version == other.authoring_version
703	}
704
705	/// Check if the given api with `api_id` is implemented and the version passes the given
706	/// `predicate`.
707	pub fn has_api_with<P: Fn(u32) -> bool>(&self, id: &ApiId, predicate: P) -> bool {
708		has_api_with(&self.apis, id, predicate)
709	}
710
711	/// Returns the api version found for api with `id`.
712	pub fn api_version(&self, id: &ApiId) -> Option<u32> {
713		self.apis.iter().find_map(|a| (a.0 == *id).then(|| a.1))
714	}
715}
716
717impl RuntimeVersion {
718	/// Returns state version to use for update.
719	///
720	/// For runtime with core api version less than 4,
721	/// V0 trie version will be applied to state.
722	/// Otherwise, V1 trie version will be use.
723	pub fn state_version(&self) -> StateVersion {
724		// If version > than 1, keep using latest version.
725		self.system_version.try_into().unwrap_or(StateVersion::V1)
726	}
727
728	/// Returns the state version to use for Extrinsics root.
729	pub fn extrinsics_root_state_version(&self) -> StateVersion {
730		match self.system_version {
731			// for system version 0 and 1, return V0
732			0 | 1 => StateVersion::V0,
733			// anything above 1, return V1
734			_ => StateVersion::V1,
735		}
736	}
737}
738
739/// The version of the native runtime.
740///
741/// In contrast to the bare [`RuntimeVersion`] this also carries a list of `spec_version`s of
742/// runtimes this native runtime can be used to author blocks for.
743#[derive(Debug)]
744#[cfg(feature = "std")]
745pub struct NativeVersion {
746	/// Basic runtime version info.
747	pub runtime_version: RuntimeVersion,
748	/// Authoring runtimes (`spec_version`s) that this native runtime supports.
749	pub can_author_with: HashSet<u32>,
750}
751
752#[cfg(feature = "std")]
753impl NativeVersion {
754	/// Check if this version matches other version for authoring blocks.
755	///
756	/// # Return
757	///
758	/// - Returns `Ok(())` when authoring is supported.
759	/// - Returns `Err(_)` with a detailed error when authoring is not supported.
760	pub fn can_author_with(&self, other: &RuntimeVersion) -> Result<(), String> {
761		if self.runtime_version.spec_name != other.spec_name {
762			Err(format!(
763				"`spec_name` does not match `{}` vs `{}`",
764				self.runtime_version.spec_name, other.spec_name,
765			))
766		} else if self.runtime_version.authoring_version != other.authoring_version &&
767			!self.can_author_with.contains(&other.authoring_version)
768		{
769			Err(format!(
770				"`authoring_version` does not match `{version}` vs `{other_version}` and \
771				`can_author_with` not contains `{other_version}`",
772				version = self.runtime_version.authoring_version,
773				other_version = other.authoring_version,
774			))
775		} else {
776			Ok(())
777		}
778	}
779}
780
781#[cfg(feature = "std")]
782/// Returns the version of the native runtime.
783pub trait GetNativeVersion {
784	/// Returns the version of the native runtime.
785	fn native_version(&self) -> &NativeVersion;
786}
787
788/// Something that can provide the runtime version at a given block.
789#[cfg(feature = "std")]
790pub trait GetRuntimeVersionAt<Block: BlockT> {
791	/// Returns the version of runtime at the given block.
792	fn runtime_version(&self, at: <Block as BlockT>::Hash) -> Result<RuntimeVersion, String>;
793}
794
795#[cfg(feature = "std")]
796impl<T: GetRuntimeVersionAt<Block>, Block: BlockT> GetRuntimeVersionAt<Block>
797	for std::sync::Arc<T>
798{
799	fn runtime_version(&self, at: <Block as BlockT>::Hash) -> Result<RuntimeVersion, String> {
800		(&**self).runtime_version(at)
801	}
802}
803
804#[cfg(feature = "std")]
805impl<T: GetNativeVersion> GetNativeVersion for std::sync::Arc<T> {
806	fn native_version(&self) -> &NativeVersion {
807		(&**self).native_version()
808	}
809}
810
811#[cfg(feature = "serde")]
812mod apis_serialize {
813	use super::*;
814	use alloc::vec::Vec;
815	use impl_serde::serialize as bytes;
816	use serde::{de, ser::SerializeTuple, Serializer};
817
818	#[derive(Serialize)]
819	struct ApiId<'a>(#[serde(serialize_with = "serialize_bytesref")] &'a super::ApiId, &'a u32);
820
821	pub fn serialize<S>(apis: &ApisVec, ser: S) -> Result<S::Ok, S::Error>
822	where
823		S: Serializer,
824	{
825		let len = apis.len();
826		let mut seq = ser.serialize_tuple(len)?;
827		for (api, ver) in &**apis {
828			seq.serialize_element(&ApiId(api, ver))?;
829		}
830		seq.end()
831	}
832
833	pub fn serialize_bytesref<S>(&apis: &&super::ApiId, ser: S) -> Result<S::Ok, S::Error>
834	where
835		S: Serializer,
836	{
837		bytes::serialize(apis, ser)
838	}
839
840	#[derive(Deserialize)]
841	struct ApiIdOwned(#[serde(deserialize_with = "deserialize_bytes")] super::ApiId, u32);
842
843	pub fn deserialize<'de, D>(deserializer: D) -> Result<ApisVec, D::Error>
844	where
845		D: de::Deserializer<'de>,
846	{
847		struct Visitor;
848		impl<'de> de::Visitor<'de> for Visitor {
849			type Value = ApisVec;
850
851			fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
852				formatter.write_str("a sequence of api id and version tuples")
853			}
854
855			fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
856			where
857				V: de::SeqAccess<'de>,
858			{
859				let mut apis = Vec::new();
860				while let Some(value) = visitor.next_element::<ApiIdOwned>()? {
861					apis.push((value.0, value.1));
862				}
863				Ok(apis.into())
864			}
865		}
866		deserializer.deserialize_seq(Visitor)
867	}
868
869	pub fn deserialize_bytes<'de, D>(d: D) -> Result<super::ApiId, D::Error>
870	where
871		D: de::Deserializer<'de>,
872	{
873		let mut arr = [0; 8];
874		bytes::deserialize_check_len(d, bytes::ExpectedLen::Exact(&mut arr[..]))?;
875		Ok(arr)
876	}
877}