1#![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
60pub use pezsp_version_proc_macro::runtime_version;
123
124pub type ApiId = [u8; 8];
147
148pub type ApisVec = alloc::borrow::Cow<'static, [(ApiId, u32)]>;
150
151#[macro_export]
153macro_rules! create_apis_vec {
154 ( $y:expr ) => {
155 $crate::Cow::Borrowed(&$y)
156 };
157}
158
159#[derive(Clone, PartialEq, Eq, Encode, Default, pezsp_runtime::RuntimeDebug, TypeInfo)]
166pub struct RuntimeVersion {
167 pub spec_name: Cow<'static, str>,
171
172 pub impl_name: Cow<'static, str>,
178
179 pub authoring_version: u32,
182
183 pub spec_version: u32,
191
192 pub impl_version: u32,
202
203 pub apis: ApisVec,
205
206 pub transaction_version: u32,
226
227 pub system_version: u8,
230}
231
232#[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#[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 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
704pub 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 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 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 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 pub fn state_version(&self) -> StateVersion {
738 self.system_version.try_into().unwrap_or(StateVersion::V1)
740 }
741
742 pub fn extrinsics_root_state_version(&self) -> StateVersion {
744 match self.system_version {
745 0 | 1 => StateVersion::V0,
747 _ => StateVersion::V1,
749 }
750 }
751}
752
753#[derive(Debug)]
758#[cfg(feature = "std")]
759pub struct NativeVersion {
760 pub runtime_version: RuntimeVersion,
762 pub can_author_with: HashSet<u32>,
764}
765
766#[cfg(feature = "std")]
767impl NativeVersion {
768 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")]
796pub trait GetNativeVersion {
798 fn native_version(&self) -> &NativeVersion;
800}
801
802#[cfg(feature = "std")]
804pub trait GetRuntimeVersionAt<Block: BlockT> {
805 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}