pezsp_version/
lib.rs

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