#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "std")]
use std::fmt;
#[cfg(feature = "std")]
use std::collections::HashSet;
use codec::{Encode, Decode};
use tp_runtime::RuntimeString;
pub use tp_runtime::create_runtime_str;
#[doc(hidden)]
pub use tetcore_std;
#[cfg(feature = "std")]
use tp_runtime::{traits::Block as BlockT, generic::BlockId};
pub type ApiId = [u8; 8];
pub type ApisVec = tetcore_std::borrow::Cow<'static, [(ApiId, u32)]>;
#[macro_export]
macro_rules! create_apis_vec {
( $y:expr ) => { $crate::tetcore_std::borrow::Cow::Borrowed(& $y) }
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, Default, tp_runtime::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct RuntimeVersion {
pub spec_name: RuntimeString,
pub impl_name: RuntimeString,
pub authoring_version: u32,
pub spec_version: u32,
pub impl_version: u32,
#[cfg_attr(
feature = "std",
serde(
serialize_with = "apis_serialize::serialize",
deserialize_with = "apis_serialize::deserialize",
)
)]
pub apis: ApisVec,
pub transaction_version: u32,
}
#[cfg(feature = "std")]
impl fmt::Display for RuntimeVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}-{} ({}-{}.tx{}.au{})",
self.spec_name,
self.spec_version,
self.impl_name,
self.impl_version,
self.transaction_version,
self.authoring_version,
)
}
}
#[cfg(feature = "std")]
impl RuntimeVersion {
pub fn can_call_with(&self, other: &RuntimeVersion) -> bool {
self.spec_version == other.spec_version &&
self.spec_name == other.spec_name &&
self.authoring_version == other.authoring_version
}
pub fn has_api_with<P: Fn(u32) -> bool>(
&self,
id: &ApiId,
predicate: P,
) -> bool {
self.apis.iter().any(|(s, v)| s == id && predicate(*v))
}
}
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct NativeVersion {
pub runtime_version: RuntimeVersion,
pub can_author_with: HashSet<u32>,
}
#[cfg(feature = "std")]
impl NativeVersion {
pub fn can_author_with(&self, other: &RuntimeVersion) -> Result<(), String> {
if self.runtime_version.spec_name != other.spec_name {
Err(format!(
"`spec_name` does not match `{}` vs `{}`",
self.runtime_version.spec_name,
other.spec_name,
))
} else if self.runtime_version.authoring_version != other.authoring_version
&& !self.can_author_with.contains(&other.authoring_version)
{
Err(format!(
"`authoring_version` does not match `{version}` vs `{other_version}` and \
`can_author_with` not contains `{other_version}`",
version = self.runtime_version.authoring_version,
other_version = other.authoring_version,
))
} else {
Ok(())
}
}
}
#[cfg(feature = "std")]
pub trait GetRuntimeVersion<Block: BlockT> {
fn native_version(&self) -> &NativeVersion;
fn runtime_version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, String>;
}
#[cfg(feature = "std")]
impl<T: GetRuntimeVersion<Block>, Block: BlockT> GetRuntimeVersion<Block> for std::sync::Arc<T> {
fn native_version(&self) -> &NativeVersion {
(&**self).native_version()
}
fn runtime_version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, String> {
(&**self).runtime_version(at)
}
}
#[cfg(feature = "std")]
mod apis_serialize {
use super::*;
use impl_serde::serialize as bytes;
use serde::{Serializer, de, ser::SerializeTuple};
#[derive(Serialize)]
struct ApiId<'a>(
#[serde(serialize_with="serialize_bytesref")] &'a super::ApiId,
&'a u32,
);
pub fn serialize<S>(apis: &ApisVec, ser: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{
let len = apis.len();
let mut seq = ser.serialize_tuple(len)?;
for (api, ver) in &**apis {
seq.serialize_element(&ApiId(api, ver))?;
}
seq.end()
}
pub fn serialize_bytesref<S>(&apis: &&super::ApiId, ser: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{
bytes::serialize(apis, ser)
}
#[derive(Deserialize)]
struct ApiIdOwned(
#[serde(deserialize_with="deserialize_bytes")]
super::ApiId,
u32,
);
pub fn deserialize<'de, D>(deserializer: D) -> Result<ApisVec, D::Error> where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = ApisVec;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a sequence of api id and version tuples")
}
fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where
V: de::SeqAccess<'de>,
{
let mut apis = Vec::new();
while let Some(value) = visitor.next_element::<ApiIdOwned>()? {
apis.push((value.0, value.1));
}
Ok(apis.into())
}
}
deserializer.deserialize_seq(Visitor)
}
pub fn deserialize_bytes<'de, D>(d: D) -> Result<super::ApiId, D::Error> where
D: de::Deserializer<'de>
{
let mut arr = [0; 8];
bytes::deserialize_check_len(d, bytes::ExpectedLen::Exact(&mut arr[..]))?;
Ok(arr)
}
}