mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 08:07:58 +00:00
Versioning for #[runtime-interface] (#5328)
* versionned runtime-interface * use only one additional wasm blob * alter docs * formatting, naming and docs * add comment for test * version duplicate err * RuntimeInterfaceItem -> RuntimeInterfaceFunction< * test naming * version checking * remove spaces * Update primitives/runtime-interface/proc-macro/src/runtime_interface/bare_function_interface.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * remove sanity checks and reduce coverage * add doc comment Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -20,17 +20,60 @@ use proc_macro2::{TokenStream, Span};
|
||||
|
||||
use syn::{
|
||||
Ident, Error, Signature, Pat, PatType, FnArg, Type, token, TraitItemMethod, ItemTrait,
|
||||
TraitItem, parse_quote, spanned::Spanned,
|
||||
TraitItem, parse_quote, spanned::Spanned, Result, Meta, NestedMeta, Lit, Attribute,
|
||||
};
|
||||
|
||||
use proc_macro_crate::crate_name;
|
||||
|
||||
use std::env;
|
||||
use std::collections::{BTreeMap, btree_map::Entry};
|
||||
|
||||
use quote::quote;
|
||||
|
||||
use inflector::Inflector;
|
||||
|
||||
/// Runtime interface function with all associated versions of this function.
|
||||
pub struct RuntimeInterfaceFunction<'a> {
|
||||
latest_version: u32,
|
||||
versions: BTreeMap<u32, &'a TraitItemMethod>,
|
||||
}
|
||||
|
||||
impl<'a> RuntimeInterfaceFunction<'a> {
|
||||
fn new(version: u32, trait_item: &'a TraitItemMethod) -> Self {
|
||||
Self {
|
||||
latest_version: version,
|
||||
versions: {
|
||||
let mut res = BTreeMap::new();
|
||||
res.insert(version, trait_item);
|
||||
res
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn latest_version(&self) -> (u32, &TraitItemMethod) {
|
||||
(
|
||||
self.latest_version,
|
||||
self.versions.get(&self.latest_version)
|
||||
.expect("If latest_version has a value, the key with this value is in the versions; qed")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// All functions of a runtime interface grouped by the function names.
|
||||
pub struct RuntimeInterface<'a> {
|
||||
items: BTreeMap<syn::Ident, RuntimeInterfaceFunction<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> RuntimeInterface<'a> {
|
||||
pub fn latest_versions(&self) -> impl Iterator<Item = (u32, &TraitItemMethod)> {
|
||||
self.items.iter().map(|(_, item)| item.latest_version())
|
||||
}
|
||||
|
||||
pub fn all_versions(&self) -> impl Iterator<Item = (u32, &TraitItemMethod)> {
|
||||
self.items.iter().flat_map(|(_, item)| item.versions.iter()).map(|(v, i)| (*v, *i))
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the include for the runtime-interface crate.
|
||||
pub fn generate_runtime_interface_include() -> TokenStream {
|
||||
if env::var("CARGO_PKG_NAME").unwrap() == "sp-runtime-interface" {
|
||||
@@ -67,12 +110,25 @@ pub fn create_exchangeable_host_function_ident(name: &Ident) -> Ident {
|
||||
}
|
||||
|
||||
/// Create the host function identifier for the given function name.
|
||||
pub fn create_host_function_ident(name: &Ident, trait_name: &Ident) -> Ident {
|
||||
pub fn create_host_function_ident(name: &Ident, version: u32, trait_name: &Ident) -> Ident {
|
||||
Ident::new(
|
||||
&format!(
|
||||
"ext_{}_{}_version_1",
|
||||
"ext_{}_{}_version_{}",
|
||||
trait_name.to_string().to_snake_case(),
|
||||
name,
|
||||
version,
|
||||
),
|
||||
Span::call_site(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Create the host function identifier for the given function name.
|
||||
pub fn create_function_ident_with_version(name: &Ident, version: u32) -> Ident {
|
||||
Ident::new(
|
||||
&format!(
|
||||
"{}_version_{}",
|
||||
name,
|
||||
version,
|
||||
),
|
||||
Span::call_site(),
|
||||
)
|
||||
@@ -151,7 +207,7 @@ pub fn get_function_argument_types_ref_and_mut<'a>(
|
||||
}
|
||||
|
||||
/// Returns an iterator over all trait methods for the given trait definition.
|
||||
pub fn get_trait_methods<'a>(trait_def: &'a ItemTrait) -> impl Iterator<Item = &'a TraitItemMethod> {
|
||||
fn get_trait_methods<'a>(trait_def: &'a ItemTrait) -> impl Iterator<Item = &'a TraitItemMethod> {
|
||||
trait_def
|
||||
.items
|
||||
.iter()
|
||||
@@ -160,3 +216,85 @@ pub fn get_trait_methods<'a>(trait_def: &'a ItemTrait) -> impl Iterator<Item = &
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse version attribute.
|
||||
///
|
||||
/// Returns error if it is in incorrent format. Correct format is only `#[version(X)]`.
|
||||
fn parse_version_attribute(version: &Attribute) -> Result<u32> {
|
||||
let meta = version.parse_meta()?;
|
||||
|
||||
let err = Err(Error::new(
|
||||
meta.span(),
|
||||
"Unexpected `version` attribute. The supported format is `#[version(1)]`",
|
||||
)
|
||||
);
|
||||
|
||||
match meta {
|
||||
Meta::List(list) => {
|
||||
if list.nested.len() != 1 {
|
||||
err
|
||||
} else if let Some(NestedMeta::Lit(Lit::Int(i))) = list.nested.first() {
|
||||
i.base10_parse()
|
||||
} else {
|
||||
err
|
||||
}
|
||||
},
|
||||
_ => err,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return item version (`#[version(X)]`) attribute, if present.
|
||||
fn get_item_version(item: &TraitItemMethod) -> Result<Option<u32>> {
|
||||
item.attrs.iter().find(|attr| attr.path.is_ident("version"))
|
||||
.map(|attr| parse_version_attribute(attr))
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Returns all runtime interface members, with versions.
|
||||
pub fn get_runtime_interface<'a>(trait_def: &'a ItemTrait)
|
||||
-> Result<RuntimeInterface<'a>>
|
||||
{
|
||||
let mut functions: BTreeMap<syn::Ident, RuntimeInterfaceFunction<'a>> = BTreeMap::new();
|
||||
|
||||
for item in get_trait_methods(trait_def) {
|
||||
let name = item.sig.ident.clone();
|
||||
let version = get_item_version(item)?.unwrap_or(1);
|
||||
|
||||
match functions.entry(name.clone()) {
|
||||
Entry::Vacant(entry) => { entry.insert(RuntimeInterfaceFunction::new(version, item)); },
|
||||
Entry::Occupied(mut entry) => {
|
||||
if let Some(existing_item) = entry.get().versions.get(&version) {
|
||||
let mut err = Error::new(
|
||||
item.span(),
|
||||
"Duplicated version attribute",
|
||||
);
|
||||
err.combine(Error::new(
|
||||
existing_item.span(),
|
||||
"Previous version with the same number defined here",
|
||||
));
|
||||
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let interface_item = entry.get_mut();
|
||||
if interface_item.latest_version < version { interface_item.latest_version = version; }
|
||||
interface_item.versions.insert(version, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for function in functions.values() {
|
||||
let mut next_expected = 1;
|
||||
for (version, item) in function.versions.iter() {
|
||||
if next_expected != *version {
|
||||
return Err(Error::new(
|
||||
item.span(),
|
||||
format!("Unexpected version attribute: missing version '{}' for this function", next_expected),
|
||||
));
|
||||
}
|
||||
next_expected += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(RuntimeInterface { items: functions })
|
||||
}
|
||||
Reference in New Issue
Block a user