Skip to main content

subsoil/version/
mod.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Substrate runtime version
8//!
9//! Each runtime that should be executed by a Substrate based node needs to have a runtime version.
10//! The runtime version is defined by [`RuntimeVersion`]. The runtime version is used to
11//! distinguish different runtimes. The most important field is the
12//! [`spec_version`](RuntimeVersion::spec_version). The `spec_version` should be increased in a
13//! runtime when a new runtime build includes breaking changes that would make other runtimes unable
14//! to import blocks built by this runtime or vice-versa, where the new runtime could not import
15//! blocks built by the old runtime. The runtime version also carries other version information
16//! about the runtime, see [`RuntimeVersion`] for more information on this.
17//!
18//! Substrate will fetch the runtime version from a `wasm` blob by first checking the
19//! `runtime_version` link section or calling the `Core::version` runtime api. The link section can
20//! be generated in the runtime using the [`runtime_version`] attribute. The `Core` runtime api also
21//! needs to be implemented for the runtime using `impl_runtime_apis!`.
22
23#[cfg(feature = "std")]
24use ::std::collections::HashSet;
25#[cfg(any(feature = "std", feature = "serde"))]
26use alloc::fmt;
27#[cfg(feature = "serde")]
28use serde::{Deserialize, Serialize};
29
30#[allow(deprecated)]
31pub use crate::create_runtime_str;
32pub use crate::runtime::StateVersion;
33#[doc(hidden)]
34pub use crate::std;
35#[doc(hidden)]
36pub use alloc::borrow::Cow;
37use codec::{Decode, Encode, Input};
38use scale_info::TypeInfo;
39
40#[cfg(feature = "std")]
41use crate::runtime::traits::Block as BlockT;
42
43#[cfg(feature = "std")]
44pub mod embed;
45
46/// An attribute that accepts a version declaration of a runtime and generates a custom wasm
47/// section with the equivalent contents.
48///
49/// The custom section allows to read the version of the runtime without having to execute any
50/// code. Instead, the generated custom section can be relatively easily parsed from the wasm
51/// binary. The identifier of the custom section is "runtime_version".
52///
53/// A shortcoming of this macro is that it is unable to embed information regarding supported
54/// APIs. This is supported by the `construct_runtime!` macro.
55///
56/// # Usage
57///
58/// This macro accepts a const item like the following:
59///
60/// ```rust
61/// extern crate alloc;
62///
63/// use alloc::borrow::Cow;
64/// use subsoil::version::RuntimeVersion;
65///
66/// #[subsoil::version::runtime_version]
67/// pub const VERSION: RuntimeVersion = RuntimeVersion {
68/// 	spec_name: Cow::Borrowed("test"),
69/// 	impl_name: Cow::Borrowed("test"),
70/// 	authoring_version: 10,
71/// 	spec_version: 265,
72/// 	impl_version: 1,
73/// 	apis: RUNTIME_API_VERSIONS,
74/// 	transaction_version: 2,
75/// 	system_version: 1,
76/// };
77///
78/// # const RUNTIME_API_VERSIONS: subsoil::version::ApisVec = subsoil::create_apis_vec!([]);
79/// ```
80///
81/// It will pass it through and add code required for emitting a custom section. The
82/// information that will go into the custom section is parsed from the item declaration. Due
83/// to that, the macro is somewhat rigid in terms of the code it accepts. There are the
84/// following considerations:
85///
86/// - The `spec_name` and `impl_name` must be set by a macro-like expression. The name of the
87///   macro doesn't matter though.
88///
89/// - `authoring_version`, `spec_version`, `impl_version` and `transaction_version` must be set
90///   by a literal. Literal must be an integer. No other expressions are allowed there. In
91///   particular, you can't supply a constant variable.
92///
93/// - `apis` doesn't have any specific constraints. This is because this information doesn't
94///   get into the custom section and is not parsed.
95///
96/// # Compilation Target & "std" feature
97///
98/// This macro assumes it will be used within a runtime. By convention, a runtime crate defines
99/// a feature named "std". This feature is enabled when the runtime is compiled to native code
100/// and disabled when it is compiled to the wasm code.
101///
102/// The custom section can only be emitted while compiling to wasm. In order to detect the
103/// compilation target we use the "std" feature. This macro will emit the custom section only
104/// if the "std" feature is **not** enabled.
105///
106/// Including this macro in the context where there is no "std" feature and the code is not
107/// compiled to wasm can lead to cryptic linking errors.
108pub use subsoil_macros::runtime_version;
109
110/// The identity of a particular API interface that the runtime might provide.
111///
112/// The id is generated by hashing the name of the runtime api with BLAKE2 using a hash size
113/// of 8 bytes.
114///
115/// The name of the runtime api is the name of the trait when using `decl_runtime_apis!` macro. So,
116/// in the following runtime api declaration:
117///
118/// ```nocompile
119/// decl_runtime_apis! {
120///     trait TestApi {
121///         fn do_test();
122///     }
123/// }
124/// ```
125///
126/// The name of the trait would be `TestApi` and would be taken as input to the BLAKE2 hash
127/// function.
128///
129/// As Rust supports renaming of traits, the name of a runtime api given to `impl_runtime_apis!`
130/// doesn't need to be the same as in `decl_runtime_apis!`, but only the name in
131/// `decl_runtime_apis!` is the important one!
132pub type ApiId = [u8; 8];
133
134/// A vector of pairs of `ApiId` and a `u32` for version.
135pub type ApisVec = alloc::borrow::Cow<'static, [(ApiId, u32)]>;
136
137/// Create a vector of Api declarations.
138#[macro_export]
139macro_rules! create_apis_vec {
140	( $y:expr ) => {
141		$crate::version::Cow::Borrowed(&$y)
142	};
143}
144
145/// Runtime version.
146/// This should not be thought of as classic Semver (major/minor/tiny).
147/// This triplet have different semantics and mis-interpretation could cause problems.
148/// In particular: bug fixes should result in an increment of `spec_version` and possibly
149/// `authoring_version`, absolutely not `impl_version` since they change the semantics of the
150/// runtime.
151#[derive(Clone, PartialEq, Eq, Encode, Default, Debug, TypeInfo)]
152pub struct RuntimeVersion {
153	/// Identifies the different Substrate runtimes. There'll be at least polkadot and node.
154	/// A different on-chain spec_name to that of the native runtime would normally result
155	/// in node not attempting to sync or author blocks.
156	pub spec_name: Cow<'static, str>,
157
158	/// Name of the implementation of the spec. This is of little consequence for the node
159	/// and serves only to differentiate code of different implementation teams. For this
160	/// codebase, it will be parity-polkadot. If there were a non-Rust implementation of the
161	/// Polkadot runtime (e.g. C++), then it would identify itself with an accordingly different
162	/// `impl_name`.
163	pub impl_name: Cow<'static, str>,
164
165	/// `authoring_version` is the version of the authorship interface. An authoring node
166	/// will not attempt to author blocks unless this is equal to its native runtime.
167	pub authoring_version: u32,
168
169	/// Version of the runtime specification.
170	///
171	/// A full-node will not attempt to use its native runtime in substitute for the on-chain
172	/// Wasm runtime unless all of `spec_name`, `spec_version` and `authoring_version` are the same
173	/// between Wasm and native.
174	///
175	/// This number should never decrease.
176	pub spec_version: u32,
177
178	/// Version of the implementation of the specification.
179	///
180	/// Nodes are free to ignore this; it serves only as an indication that the code is different;
181	/// as long as the other two versions are the same then while the actual code may be different,
182	/// it is nonetheless required to do the same thing. Non-consensus-breaking optimizations are
183	/// about the only changes that could be made which would result in only the `impl_version`
184	/// changing.
185	///
186	/// This number can be reverted to `0` after a [`spec_version`](Self::spec_version) bump.
187	pub impl_version: u32,
188
189	/// List of supported API "features" along with their versions.
190	pub apis: ApisVec,
191
192	/// All existing calls (dispatchables) are fully compatible when this number doesn't change. If
193	/// this number changes, then [`spec_version`](Self::spec_version) must change, also.
194	///
195	/// This number must change when an existing call (pallet index, call index) is changed,
196	/// either through an alteration in its user-level semantics, a parameter
197	/// added/removed, a parameter type changed, or a call/pallet changing its index. An alteration
198	/// of the user level semantics is for example when the call was before `transfer` and now is
199	/// `transfer_all`, the semantics of the call changed completely.
200	///
201	/// Removing a pallet or a call doesn't require a *bump* as long as no pallet or call is put at
202	/// the same index. Removing doesn't require a bump as the chain will reject a transaction
203	/// referencing this removed call/pallet while decoding and thus, the user isn't at risk to
204	/// execute any unknown call. FRAME runtime devs have control over the index of a call/pallet
205	/// to prevent that an index gets reused.
206	///
207	/// Adding a new pallet or call also doesn't require a *bump* as long as they also don't reuse
208	/// any previously used index.
209	///
210	/// This number should never decrease.
211	pub transaction_version: u32,
212
213	/// Version of the system implementation used by this runtime.
214	/// Use of an incorrect version is consensus breaking.
215	pub system_version: u8,
216}
217
218// Manual implementation in order to sprinkle `stateVersion` at the end for migration purposes
219// after the field was renamed from `state_version` to `system_version`
220#[cfg(feature = "serde")]
221impl serde::Serialize for RuntimeVersion {
222	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
223	where
224		S: serde::Serializer,
225	{
226		use serde::ser::SerializeStruct;
227
228		let mut state = serializer.serialize_struct("RuntimeVersion", 9)?;
229		state.serialize_field("specName", &self.spec_name)?;
230		state.serialize_field("implName", &self.impl_name)?;
231		state.serialize_field("authoringVersion", &self.authoring_version)?;
232		state.serialize_field("specVersion", &self.spec_version)?;
233		state.serialize_field("implVersion", &self.impl_version)?;
234		state.serialize_field("apis", {
235			struct SerializeWith<'a>(&'a ApisVec);
236
237			impl<'a> serde::Serialize for SerializeWith<'a> {
238				fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
239				where
240					S: serde::Serializer,
241				{
242					apis_serialize::serialize(self.0, serializer)
243				}
244			}
245
246			&SerializeWith(&self.apis)
247		})?;
248		state.serialize_field("transactionVersion", &self.transaction_version)?;
249		state.serialize_field("systemVersion", &self.system_version)?;
250		state.serialize_field("stateVersion", &self.system_version)?;
251		state.end()
252	}
253}
254
255// Manual implementation in order to allow both old `stateVersion` and new `systemVersion` to be
256// present at the same time
257#[cfg(feature = "serde")]
258impl<'de> serde::Deserialize<'de> for RuntimeVersion {
259	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
260	where
261		D: serde::Deserializer<'de>,
262	{
263		use ::core::marker::PhantomData;
264
265		enum Field {
266			SpecName,
267			ImplName,
268			AuthoringVersion,
269			SpecVersion,
270			ImplVersion,
271			Apis,
272			TransactionVersion,
273			SystemVersion,
274			Ignore,
275		}
276
277		struct FieldVisitor;
278
279		impl<'de> serde::de::Visitor<'de> for FieldVisitor {
280			type Value = Field;
281
282			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
283				formatter.write_str("field identifier")
284			}
285
286			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
287			where
288				E: serde::de::Error,
289			{
290				match value {
291					0 => Ok(Field::SpecName),
292					1 => Ok(Field::ImplName),
293					2 => Ok(Field::AuthoringVersion),
294					3 => Ok(Field::SpecVersion),
295					4 => Ok(Field::ImplVersion),
296					5 => Ok(Field::Apis),
297					6 => Ok(Field::TransactionVersion),
298					7 => Ok(Field::SystemVersion),
299					_ => Ok(Field::Ignore),
300				}
301			}
302
303			fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
304			where
305				E: serde::de::Error,
306			{
307				match value {
308					"specName" => Ok(Field::SpecName),
309					"implName" => Ok(Field::ImplName),
310					"authoringVersion" => Ok(Field::AuthoringVersion),
311					"specVersion" => Ok(Field::SpecVersion),
312					"implVersion" => Ok(Field::ImplVersion),
313					"apis" => Ok(Field::Apis),
314					"transactionVersion" => Ok(Field::TransactionVersion),
315					"systemVersion" | "stateVersion" => Ok(Field::SystemVersion),
316					_ => Ok(Field::Ignore),
317				}
318			}
319
320			fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
321			where
322				E: serde::de::Error,
323			{
324				match value {
325					b"specName" => Ok(Field::SpecName),
326					b"implName" => Ok(Field::ImplName),
327					b"authoringVersion" => Ok(Field::AuthoringVersion),
328					b"specVersion" => Ok(Field::SpecVersion),
329					b"implVersion" => Ok(Field::ImplVersion),
330					b"apis" => Ok(Field::Apis),
331					b"transactionVersion" => Ok(Field::TransactionVersion),
332					b"systemVersion" | b"stateVersion" => Ok(Field::SystemVersion),
333					_ => Ok(Field::Ignore),
334				}
335			}
336		}
337
338		impl<'de> serde::Deserialize<'de> for Field {
339			#[inline]
340			fn deserialize<E>(deserializer: E) -> Result<Self, E::Error>
341			where
342				E: serde::Deserializer<'de>,
343			{
344				deserializer.deserialize_identifier(FieldVisitor)
345			}
346		}
347
348		struct Visitor<'de> {
349			lifetime: PhantomData<&'de ()>,
350		}
351		impl<'de> serde::de::Visitor<'de> for Visitor<'de> {
352			type Value = RuntimeVersion;
353
354			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
355				formatter.write_str("struct RuntimeVersion")
356			}
357
358			#[inline]
359			fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
360			where
361				A: serde::de::SeqAccess<'de>,
362			{
363				let spec_name = match seq.next_element()? {
364					Some(spec_name) => spec_name,
365					None => {
366						return Err(serde::de::Error::invalid_length(
367							0usize,
368							&"struct RuntimeVersion with 8 elements",
369						))
370					},
371				};
372				let impl_name = match seq.next_element()? {
373					Some(impl_name) => impl_name,
374					None => {
375						return Err(serde::de::Error::invalid_length(
376							1usize,
377							&"struct RuntimeVersion with 8 elements",
378						))
379					},
380				};
381				let authoring_version = match seq.next_element()? {
382					Some(authoring_version) => authoring_version,
383					None => {
384						return Err(serde::de::Error::invalid_length(
385							2usize,
386							&"struct RuntimeVersion with 8 elements",
387						))
388					},
389				};
390				let spec_version = match seq.next_element()? {
391					Some(spec_version) => spec_version,
392					None => {
393						return Err(serde::de::Error::invalid_length(
394							3usize,
395							&"struct RuntimeVersion with 8 elements",
396						))
397					},
398				};
399				let impl_version = match seq.next_element()? {
400					Some(impl_version) => impl_version,
401					None => {
402						return Err(serde::de::Error::invalid_length(
403							4usize,
404							&"struct RuntimeVersion with 8 elements",
405						))
406					},
407				};
408				let apis = match {
409					struct DeserializeWith<'de> {
410						value: ApisVec,
411
412						phantom: PhantomData<RuntimeVersion>,
413						lifetime: PhantomData<&'de ()>,
414					}
415					impl<'de> serde::Deserialize<'de> for DeserializeWith<'de> {
416						fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
417						where
418							D: serde::Deserializer<'de>,
419						{
420							Ok(DeserializeWith {
421								value: apis_serialize::deserialize(deserializer)?,
422								phantom: PhantomData,
423								lifetime: PhantomData,
424							})
425						}
426					}
427					seq.next_element::<DeserializeWith<'de>>()?.map(|wrap| wrap.value)
428				} {
429					Some(apis) => apis,
430					None => {
431						return Err(serde::de::Error::invalid_length(
432							5usize,
433							&"struct RuntimeVersion with 8 elements",
434						))
435					},
436				};
437				let transaction_version = match seq.next_element()? {
438					Some(transaction_version) => transaction_version,
439					None => {
440						return Err(serde::de::Error::invalid_length(
441							6usize,
442							&"struct RuntimeVersion with 8 elements",
443						))
444					},
445				};
446				let system_version = match seq.next_element()? {
447					Some(system_version) => system_version,
448					None => {
449						return Err(serde::de::Error::invalid_length(
450							7usize,
451							&"struct RuntimeVersion with 8 elements",
452						))
453					},
454				};
455				Ok(RuntimeVersion {
456					spec_name,
457					impl_name,
458					authoring_version,
459					spec_version,
460					impl_version,
461					apis,
462					transaction_version,
463					system_version,
464				})
465			}
466
467			#[inline]
468			fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
469			where
470				A: serde::de::MapAccess<'de>,
471			{
472				let mut spec_name: Option<Cow<'static, str>> = None;
473				let mut impl_name: Option<Cow<'static, str>> = None;
474				let mut authoring_version: Option<u32> = None;
475				let mut spec_version: Option<u32> = None;
476				let mut impl_version: Option<u32> = None;
477				let mut apis: Option<ApisVec> = None;
478				let mut transaction_version: Option<u32> = None;
479				let mut system_version: Option<u8> = None;
480
481				while let Some(key) = map.next_key()? {
482					match key {
483						Field::SpecName => {
484							if spec_name.is_some() {
485								return Err(<A::Error as serde::de::Error>::duplicate_field(
486									"specName",
487								));
488							}
489							spec_name = Some(map.next_value()?);
490						},
491						Field::ImplName => {
492							if impl_name.is_some() {
493								return Err(<A::Error as serde::de::Error>::duplicate_field(
494									"implName",
495								));
496							}
497							impl_name = Some(map.next_value()?);
498						},
499						Field::AuthoringVersion => {
500							if authoring_version.is_some() {
501								return Err(<A::Error as serde::de::Error>::duplicate_field(
502									"authoringVersion",
503								));
504							}
505							authoring_version = Some(map.next_value()?);
506						},
507						Field::SpecVersion => {
508							if spec_version.is_some() {
509								return Err(<A::Error as serde::de::Error>::duplicate_field(
510									"specVersion",
511								));
512							}
513							spec_version = Some(map.next_value()?);
514						},
515						Field::ImplVersion => {
516							if impl_version.is_some() {
517								return Err(<A::Error as serde::de::Error>::duplicate_field(
518									"implVersion",
519								));
520							}
521							impl_version = Some(map.next_value()?);
522						},
523						Field::Apis => {
524							if apis.is_some() {
525								return Err(<A::Error as serde::de::Error>::duplicate_field(
526									"apis",
527								));
528							}
529							apis = Some({
530								struct DeserializeWith<'de> {
531									value: ApisVec,
532									lifetime: PhantomData<&'de ()>,
533								}
534								impl<'de> serde::Deserialize<'de> for DeserializeWith<'de> {
535									fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
536									where
537										D: serde::Deserializer<'de>,
538									{
539										Ok(DeserializeWith {
540											value: apis_serialize::deserialize(deserializer)?,
541											lifetime: PhantomData,
542										})
543									}
544								}
545
546								map.next_value::<DeserializeWith<'de>>()?.value
547							});
548						},
549						Field::TransactionVersion => {
550							if transaction_version.is_some() {
551								return Err(<A::Error as serde::de::Error>::duplicate_field(
552									"transactionVersion",
553								));
554							}
555							transaction_version = Some(map.next_value()?);
556						},
557						Field::SystemVersion => {
558							if let Some(system_version) = system_version {
559								let new_value = map.next_value::<u8>()?;
560								if system_version != new_value {
561									return Err(<A::Error as serde::de::Error>::custom(
562										alloc::format!(
563											r#"Duplicated "stateVersion" and "systemVersion" \
564											fields must have the same value, but different values \
565											were provided: {system_version} vs {new_value}"#
566										),
567									));
568								}
569							} else {
570								system_version = Some(map.next_value()?);
571							}
572						},
573						_ => {
574							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 = subsoil_macros::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}