Skip to content

Commit

Permalink
sp-api: Use macro to detect if frame-metadata is enabled (#4117)
Browse files Browse the repository at this point in the history
While `sp-api-proc-macro` isn't used directly and thus, it should have
the same features enabled as `sp-api`. However, I have seen issues
around `frame-metadata` not being enabled for `sp-api`, but for
`sp-api-proc-macro`. This can be prevented by using the
`frame_metadata_enabled` macro from `sp-api` that ensures we have the
same feature set between both crates.
  • Loading branch information
bkchr committed Jul 18, 2024
1 parent a56caf1 commit 0edc4fc
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion substrate/primitives/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ std = [
disable-logging = ["log/max_level_off"]
# Do not report the documentation in the metadata.
no-metadata-docs = ["sp-api-proc-macro/no-metadata-docs"]
frame-metadata = ["sp-api-proc-macro/frame-metadata" ]
frame-metadata = []
1 change: 0 additions & 1 deletion substrate/primitives/api/proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ assert_matches = "1.3.0"
default = ["std"]
std = ["blake2/std"]
no-metadata-docs = []
frame-metadata = []
3 changes: 0 additions & 3 deletions substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
get_api_version(&found_attributes).map(|v| generate_runtime_api_version(v as u32))?;
let id = generate_runtime_api_id(&decl.ident.to_string());

#[cfg(feature = "frame-metadata")]
let metadata = crate::runtime_metadata::generate_decl_runtime_metadata(&decl);
#[cfg(not(feature = "frame-metadata"))]
let metadata = quote!();

let trait_api_version = get_api_version(&found_attributes)?;

Expand Down
3 changes: 0 additions & 3 deletions substrate/primitives/api/proc-macro/src/impl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,7 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result<TokenStream> {
let wasm_interface = generate_wasm_interface(api_impls)?;
let api_impls_for_runtime_api = generate_api_impl_for_runtime_api(api_impls)?;

#[cfg(feature = "frame-metadata")]
let runtime_metadata = crate::runtime_metadata::generate_impl_runtime_metadata(api_impls)?;
#[cfg(not(feature = "frame-metadata"))]
let runtime_metadata = quote!();

let impl_ = quote!(
#base_runtime_api
Expand Down
1 change: 0 additions & 1 deletion substrate/primitives/api/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ mod common;
mod decl_runtime_apis;
mod impl_runtime_apis;
mod mock_impl_runtime_apis;
#[cfg(feature = "frame-metadata")]
mod runtime_metadata;
mod utils;

Expand Down
36 changes: 20 additions & 16 deletions substrate/primitives/api/proc-macro/src/runtime_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,17 @@ pub fn generate_decl_runtime_metadata(decl: &ItemTrait) -> TokenStream2 {
let (impl_generics, _, where_clause) = generics.split_for_impl();

quote!(
#( #attrs )*
#[inline(always)]
pub fn runtime_metadata #impl_generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR
#where_clause
{
#crate_::metadata_ir::RuntimeApiMetadataIR {
name: #trait_name,
methods: #crate_::vec![ #( #methods, )* ],
docs: #docs,
#crate_::frame_metadata_enabled! {
#( #attrs )*
#[inline(always)]
pub fn runtime_metadata #impl_generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR
#where_clause
{
#crate_::metadata_ir::RuntimeApiMetadataIR {
name: #trait_name,
methods: #crate_::vec![ #( #methods, )* ],
docs: #docs,
}
}
}
)
Expand Down Expand Up @@ -255,14 +257,16 @@ pub fn generate_impl_runtime_metadata(impls: &[ItemImpl]) -> Result<TokenStream2
// `construct_runtime!` is called.

Ok(quote!(
#[doc(hidden)]
trait InternalImplRuntimeApis {
#[inline(always)]
fn runtime_metadata(&self) -> #crate_::vec::Vec<#crate_::metadata_ir::RuntimeApiMetadataIR> {
#crate_::vec![ #( #metadata, )* ]
#crate_::frame_metadata_enabled! {
#[doc(hidden)]
trait InternalImplRuntimeApis {
#[inline(always)]
fn runtime_metadata(&self) -> #crate_::vec::Vec<#crate_::metadata_ir::RuntimeApiMetadataIR> {
#crate_::vec![ #( #metadata, )* ]
}
}
#[doc(hidden)]
impl InternalImplRuntimeApis for #runtime_name {}
}
#[doc(hidden)]
impl InternalImplRuntimeApis for #runtime_name {}
))
}
2 changes: 0 additions & 2 deletions substrate/primitives/api/proc-macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ pub fn versioned_trait_name(trait_ident: &Ident, version: u64) -> Ident {
}

/// Extract the documentation from the provided attributes.
#[cfg(feature = "frame-metadata")]
pub fn get_doc_literals(attrs: &[syn::Attribute]) -> Vec<syn::Lit> {
attrs
.iter()
Expand All @@ -274,7 +273,6 @@ pub fn get_doc_literals(attrs: &[syn::Attribute]) -> Vec<syn::Lit> {
}

/// Filters all attributes except the cfg ones.
#[cfg(feature = "frame-metadata")]
pub fn filter_cfg_attributes(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
attrs.iter().filter(|a| a.path().is_ident("cfg")).cloned().collect()
}
Expand Down
1 change: 1 addition & 0 deletions substrate/primitives/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,4 @@ decl_runtime_apis! {

sp_core::generate_feature_enabled_macro!(std_enabled, feature = "std", $);
sp_core::generate_feature_enabled_macro!(std_disabled, not(feature = "std"), $);
sp_core::generate_feature_enabled_macro!(frame_metadata_enabled, feature = "frame-metadata", $);

0 comments on commit 0edc4fc

Please sign in to comment.