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};
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
62pub use sp_version_proc_macro::runtime_version;
125
126pub type ApiId = [u8; 8];
149
150pub type ApisVec = alloc::borrow::Cow<'static, [(ApiId, u32)]>;
152
153#[macro_export]
155macro_rules! create_apis_vec {
156 ( $y:expr ) => {
157 $crate::Cow::Borrowed(&$y)
158 };
159}
160
161#[derive(Clone, PartialEq, Eq, Encode, Default, Debug, TypeInfo)]
168pub struct RuntimeVersion {
169 pub spec_name: Cow<'static, str>,
173
174 pub impl_name: Cow<'static, str>,
180
181 pub authoring_version: u32,
184
185 pub spec_version: u32,
193
194 pub impl_version: u32,
204
205 pub apis: ApisVec,
207
208 pub transaction_version: u32,
228
229 pub system_version: u8,
232}
233
234#[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#[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 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
706pub 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 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 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 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 pub fn state_version(&self) -> StateVersion {
740 self.system_version.try_into().unwrap_or(StateVersion::V1)
742 }
743
744 pub fn extrinsics_root_state_version(&self) -> StateVersion {
746 match self.system_version {
747 0 | 1 => StateVersion::V0,
749 _ => StateVersion::V1,
751 }
752 }
753}
754
755#[derive(Debug)]
760#[cfg(feature = "std")]
761pub struct NativeVersion {
762 pub runtime_version: RuntimeVersion,
764 pub can_author_with: HashSet<u32>,
766}
767
768#[cfg(feature = "std")]
769impl NativeVersion {
770 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")]
798pub trait GetNativeVersion {
800 fn native_version(&self) -> &NativeVersion;
802}
803
804#[cfg(feature = "std")]
806pub trait GetRuntimeVersionAt<Block: BlockT> {
807 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}