Skip to main content

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