// Copyright 2018-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see .
use crate::utils::{
generate_crate_access, generate_hidden_includes, generate_runtime_mod_name_for_trait,
fold_fn_decl_for_client_side, extract_parameter_names_types_and_borrows,
generate_native_call_generator_fn_name, return_type_extract_type,
generate_method_runtime_api_impl_name, generate_call_api_at_fn_name, prefix_function_with_trait,
replace_wild_card_parameter_names,
};
use proc_macro2::{TokenStream, Span};
use quote::quote;
use syn::{
spanned::Spanned, parse_macro_input, parse::{Parse, ParseStream, Result, Error}, ReturnType,
fold::{self, Fold}, parse_quote, ItemTrait, Generics, GenericParam, Attribute, FnArg, Type,
visit::{Visit, self}, TraitBound, Meta, NestedMeta, Lit, TraitItem, Ident, TraitItemMethod,
};
use std::collections::HashMap;
use blake2_rfc;
/// The ident used for the block generic parameter.
const BLOCK_GENERIC_IDENT: &str = "Block";
/// Unique identifier used to make the hidden includes unique for this macro.
const HIDDEN_INCLUDES_ID: &str = "DECL_RUNTIME_APIS";
/// The `core_trait` attribute.
const CORE_TRAIT_ATTRIBUTE: &str = "core_trait";
/// The `api_version` attribute.
///
/// Is used to set the current version of the trait.
const API_VERSION_ATTRIBUTE: &str = "api_version";
/// The `changed_in` attribute.
///
/// Is used when the function signature changed between different versions of a trait.
/// This attribute should be placed on the old signature of the function.
const CHANGED_IN_ATTRIBUTE: &str = "changed_in";
/// The `renamed` attribute.
///
/// Is used when a trait method was renamed.
const RENAMED_ATTRIBUTE: &str = "renamed";
/// The `skip_initialize_block` attribute.
///
/// Is used when a trait method does not require that the block is initialized
/// before being called.
const SKIP_INITIALIZE_BLOCK_ATTRIBUTE: &str = "skip_initialize_block";
/// The `initialize_block` attribute.
///
/// A trait method tagged with this attribute, initializes the runtime at
/// certain block.
const INITIALIZE_BLOCK_ATTRIBUTE: &str = "initialize_block";
/// All attributes that we support in the declaration of a runtime api trait.
const SUPPORTED_ATTRIBUTE_NAMES: &[&str] = &[
CORE_TRAIT_ATTRIBUTE, API_VERSION_ATTRIBUTE, CHANGED_IN_ATTRIBUTE,
RENAMED_ATTRIBUTE, SKIP_INITIALIZE_BLOCK_ATTRIBUTE,
INITIALIZE_BLOCK_ATTRIBUTE,
];
/// The structure used for parsing the runtime api declarations.
struct RuntimeApiDecls {
decls: Vec,
}
impl Parse for RuntimeApiDecls {
fn parse(input: ParseStream) -> Result {
let mut decls = Vec::new();
while !input.is_empty() {
decls.push(ItemTrait::parse(input)?);
}
Ok(Self { decls })
}
}
/// Extend the given generics with `Block: BlockT` as first generic parameter.
fn extend_generics_with_block(generics: &mut Generics) {
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
generics.lt_token = Some(Default::default());
generics.params.insert(0, parse_quote!( Block: #c::BlockT ));
generics.gt_token = Some(Default::default());
}
/// Remove all attributes from the vector that are supported by us in the declaration of a runtime
/// api trait. The returned hashmap contains all found attribute names as keys and the rest of the
/// attribute body as `TokenStream`.
fn remove_supported_attributes(attrs: &mut Vec) -> HashMap<&'static str, Attribute> {
let mut result = HashMap::new();
attrs.retain(|v| {
match SUPPORTED_ATTRIBUTE_NAMES.iter().find(|a| v.path.is_ident(a)) {
Some(attribute) => {
result.insert(*attribute, v.clone());
false
},
None => true,
}
});
result
}
/// Visits the ast and checks if `Block` ident is used somewhere.
struct IsUsingBlock {
result: bool,
}
impl<'ast> Visit<'ast> for IsUsingBlock {
fn visit_ident(&mut self, i: &'ast Ident) {
if i == BLOCK_GENERIC_IDENT {
self.result = true;
}
}
}
/// Visits the ast and checks if `Block` ident is used somewhere.
fn type_is_using_block(ty: &Type) -> bool {
let mut visitor = IsUsingBlock { result: false };
visitor.visit_type(ty);
visitor.result
}
/// Visits the ast and checks if `Block` ident is used somewhere.
fn return_type_is_using_block(ty: &ReturnType) -> bool {
let mut visitor = IsUsingBlock { result: false };
visitor.visit_return_type(ty);
visitor.result
}
/// Replace all occurrences of `Block` with `NodeBlock`
struct ReplaceBlockWithNodeBlock {}
impl Fold for ReplaceBlockWithNodeBlock {
fn fold_ident(&mut self, input: Ident) -> Ident {
if input == BLOCK_GENERIC_IDENT {
Ident::new("NodeBlock", Span::call_site())
} else {
input
}
}
}
/// Replace all occurrences of `Block` with `NodeBlock`
fn fn_arg_replace_block_with_node_block(fn_arg: FnArg) -> FnArg {
let mut replace = ReplaceBlockWithNodeBlock {};
fold::fold_fn_arg(&mut replace, fn_arg)
}
/// Replace all occurrences of `Block` with `NodeBlock`
fn return_type_replace_block_with_node_block(return_type: ReturnType) -> ReturnType {
let mut replace = ReplaceBlockWithNodeBlock {};
fold::fold_return_type(&mut replace, return_type)
}
/// Generate the functions that generate the native call closure for each trait method.
fn generate_native_call_generators(decl: &ItemTrait) -> Result {
let fns = decl.items.iter().filter_map(|i| match i {
TraitItem::Method(ref m) => Some(&m.sig),
_ => None,
});
let mut result = Vec::new();
let trait_ = &decl.ident;
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
// Auxiliary function that is used to convert between types that use different block types.
// The function expects that both are convertible by encoding the one and decoding the other.
result.push(quote!(
#[cfg(any(feature = "std", test))]
fn convert_between_block_types
(
input: &I, error_desc: &'static str,
) -> std::result::Result
{
::decode(
&mut crate_::Encode::encode(input)[..],
).map_err(|e| format!("{} {}", error_desc, e.what()))
}
));
// Generate a native call generator for each function of the given trait.
for fn_ in fns {
let params = extract_parameter_names_types_and_borrows(&fn_)?;
let trait_fn_name = &fn_.ident;
let fn_name = generate_native_call_generator_fn_name(&fn_.ident);
let output = return_type_replace_block_with_node_block(fn_.output.clone());
let output_ty = return_type_extract_type(&output);
let output = quote!( std::result::Result<#output_ty, String> );
// Every type that is using the `Block` generic parameter, we need to encode/decode,
// to make it compatible between the runtime/node.
let conversions = params.iter().filter(|v| type_is_using_block(&v.1)).map(|(n, t, _)| {
let name_str = format!(
"Could not convert parameter `{}` between node and runtime:", quote!(#n)
);
quote!(
let #n: #t = convert_between_block_types(n, #name_str)?;
)
});
// Same as for the input types, we need to check if we also need to convert the output,
// before returning it.
let output_conversion = if return_type_is_using_block(&fn_.output) {
quote!(
convert_between_block_types(
&res,
"Could not convert return value from runtime to node!"
)
)
} else {
quote!( Ok(res) )
};
let input_names = params.iter().map(|v| &v.0);
// If the type is using the block generic type, we will encode/decode it to make it
// compatible. To ensure that we forward it by ref/value, we use the value given by the
// the user. Otherwise if it is not using the block, we don't need to add anything.
let input_borrows = params
.iter()
.map(|v| if type_is_using_block(&v.1) { v.2.clone() } else { None });
// Replace all `Block` with `NodeBlock`, add `'a` lifetime to references and collect
// all the function inputs.
let fn_inputs = fn_
.inputs
.iter()
.map(|v| fn_arg_replace_block_with_node_block(v.clone()))
.map(|v| match v {
FnArg::Typed(ref arg) => {
let mut arg = arg.clone();
if let Type::Reference(ref mut r) = *arg.ty {
r.lifetime = Some(parse_quote!( 'a ));
}
FnArg::Typed(arg)
},
r => r.clone(),
});
let (impl_generics, ty_generics, where_clause) = decl.generics.split_for_impl();
// We need to parse them again, to get an easy access to the actual parameters.
let impl_generics: Generics = parse_quote!( #impl_generics );
let impl_generics_params = impl_generics.params.iter().map(|p| {
match p {
GenericParam::Type(ref ty) => {
let mut ty = ty.clone();
ty.bounds.push(parse_quote!( 'a ));
GenericParam::Type(ty)
},
// We should not see anything different than type params here.
r => r.clone(),
}
});
// Generate the generator function
result.push(quote!(
#[cfg(any(feature = "std", test))]
pub fn #fn_name<
'a, ApiImpl: #trait_ #ty_generics, NodeBlock: #crate_::BlockT
#(, #impl_generics_params)*
>(
#( #fn_inputs ),*
) -> impl FnOnce() -> #output + 'a #where_clause {
move || {
#( #conversions )*
let res = ApiImpl::#trait_fn_name(#( #input_borrows #input_names ),*);
#output_conversion
}
}
));
}
Ok(quote!( #( #result )* ))
}
/// Try to parse the given `Attribute` as `renamed` attribute.
fn parse_renamed_attribute(renamed: &Attribute) -> Result<(String, u32)> {
let meta = renamed.parse_meta()?;
let err = Err(Error::new(
meta.span(),
&format!(
"Unexpected `{renamed}` attribute. The supported format is `{renamed}(\"old_name\", version_it_was_renamed)`",
renamed = RENAMED_ATTRIBUTE,
)
)
);
match meta {
Meta::List(list) => {
if list.nested.len() > 2 && list.nested.is_empty() {
err
} else {
let mut itr = list.nested.iter();
let old_name = match itr.next() {
Some(NestedMeta::Lit(Lit::Str(i))) => {
i.value()
},
_ => return err,
};
let version = match itr.next() {
Some(NestedMeta::Lit(Lit::Int(i))) => {
i.base10_parse()?
},
_ => return err,
};
Ok((old_name, version))
}
},
_ => err,
}
}
/// Generate the functions that call the api at a given block for a given trait method.
fn generate_call_api_at_calls(decl: &ItemTrait) -> Result {
let fns = decl.items.iter().filter_map(|i| match i {
TraitItem::Method(ref m) => Some((&m.attrs, &m.sig)),
_ => None,
});
let mut result = Vec::new();
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
// Generate a native call generator for each function of the given trait.
for (attrs, fn_) in fns {
let trait_name = &decl.ident;
let trait_fn_name = prefix_function_with_trait(&trait_name, &fn_.ident);
let fn_name = generate_call_api_at_fn_name(&fn_.ident);
let attrs = remove_supported_attributes(&mut attrs.clone());
if attrs.contains_key(RENAMED_ATTRIBUTE) && attrs.contains_key(CHANGED_IN_ATTRIBUTE) {
return Err(Error::new(
fn_.span(),
format!(
"`{}` and `{}` are not supported at once.",
RENAMED_ATTRIBUTE,
CHANGED_IN_ATTRIBUTE
)
));
}
// We do not need to generate this function for a method that signature was changed.
if attrs.contains_key(CHANGED_IN_ATTRIBUTE) {
continue;
}
let skip_initialize_block = attrs.contains_key(SKIP_INITIALIZE_BLOCK_ATTRIBUTE);
let update_initialized_block = if attrs.contains_key(INITIALIZE_BLOCK_ATTRIBUTE) {
quote!(
|| *initialized_block.borrow_mut() = Some(*at)
)
} else {
quote!(|| ())
};
// Parse the renamed attributes.
let mut renames = Vec::new();
if let Some((_, a)) = attrs
.iter()
.find(|a| a.0 == &RENAMED_ATTRIBUTE)
{
let (old_name, version) = parse_renamed_attribute(a)?;
renames.push((version, prefix_function_with_trait(&trait_name, &old_name)));
}
renames.sort_unstable_by(|l, r| r.cmp(l));
let (versions, old_names) = renames.into_iter().fold(
(Vec::new(), Vec::new()),
|(mut versions, mut old_names), (version, old_name)| {
versions.push(version);
old_names.push(old_name);
(versions, old_names)
}
);
// Generate the generator function
result.push(quote!(
#[cfg(any(feature = "std", test))]
pub fn #fn_name<
R: #crate_::Encode + #crate_::Decode + PartialEq,
NC: FnOnce() -> std::result::Result + std::panic::UnwindSafe,
Block: #crate_::BlockT,
T: #crate_::CallApiAt,
C: #crate_::Core,
>(
call_runtime_at: &T,
core_api: &C,
at: crate_::BlockId,
args: Vec,
changes: &std::cell::RefCell<#crate_::OverlayedChanges>,
storage_transaction_cache: &std::cell::RefCell<
#crate_::StorageTransactionCache
>,
initialized_block: &std::cell::RefCell