From 1b00cbada430fd9bcc8e798f855a534a1c673059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 3 Jul 2023 11:54:28 +0200 Subject: [PATCH] sp-api: Put `frame-metadata` behind some feature (#14398) * sp-api: Put `frame-metadata` behind some feature Closes: https://github.com/paritytech/substrate/issues/14296 * ".git/.scripts/commands/fmt/fmt.sh" * Review feedback --------- Co-authored-by: command-bot <> --- frame/support/Cargo.toml | 2 +- primitives/api/Cargo.toml | 10 +++++++--- primitives/api/proc-macro/Cargo.toml | 1 + primitives/api/proc-macro/src/decl_runtime_apis.rs | 7 +++++-- primitives/api/proc-macro/src/impl_runtime_apis.rs | 7 +++++-- primitives/api/proc-macro/src/lib.rs | 1 + primitives/api/proc-macro/src/utils.rs | 6 +++++- primitives/api/src/lib.rs | 1 + 8 files changed, 26 insertions(+), 9 deletions(-) diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index f95fbe161476a..201a5d42818d0 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -17,7 +17,7 @@ serde = { version = "1.0.163", default-features = false, features = ["alloc", "d codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive", "max-encoded-len"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } frame-metadata = { version = "16.0.0", default-features = false, features = ["current"] } -sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } +sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api", features = [ "frame-metadata" ] } sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" } sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" } diff --git a/primitives/api/Cargo.toml b/primitives/api/Cargo.toml index 7dc5cdcfc887d..ded866ed48193 100644 --- a/primitives/api/Cargo.toml +++ b/primitives/api/Cargo.toml @@ -24,7 +24,7 @@ sp-trie = { version = "22.0.0", default-features = false, optional = true, path hash-db = { version = "0.16.0", optional = true } thiserror = { version = "1.0.30", optional = true } scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } -sp-metadata-ir = { version = "0.1.0", default-features = false, path = "../metadata-ir" } +sp-metadata-ir = { version = "0.1.0", default-features = false, optional = true, path = "../metadata-ir" } log = { version = "0.4.17", default-features = false } [dev-dependencies] @@ -44,9 +44,9 @@ std = [ "thiserror", "log/std", "scale-info/std", - "sp-metadata-ir/std", + "sp-metadata-ir?/std", ] -# Special feature to disable logging completly. +# Special feature to disable logging completely. # # By default `sp-api` initializes the `RuntimeLogger` for each runtime api function. However, # logging functionality increases the code size. It is recommended to enable this feature when @@ -56,3 +56,7 @@ 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-metadata-ir", + "sp-api-proc-macro/frame-metadata" +] diff --git a/primitives/api/proc-macro/Cargo.toml b/primitives/api/proc-macro/Cargo.toml index 75291e06759b1..fb6513806c812 100644 --- a/primitives/api/proc-macro/Cargo.toml +++ b/primitives/api/proc-macro/Cargo.toml @@ -32,3 +32,4 @@ assert_matches = "1.3.0" default = ["std"] std = [] no-metadata-docs = [] +frame-metadata = [] diff --git a/primitives/api/proc-macro/src/decl_runtime_apis.rs b/primitives/api/proc-macro/src/decl_runtime_apis.rs index 827a3156060a9..052dc896f2cc3 100644 --- a/primitives/api/proc-macro/src/decl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/decl_runtime_apis.rs @@ -20,7 +20,6 @@ use crate::{ API_VERSION_ATTRIBUTE, BLOCK_GENERIC_IDENT, CHANGED_IN_ATTRIBUTE, CORE_TRAIT_ATTRIBUTE, RENAMED_ATTRIBUTE, SUPPORTED_ATTRIBUTE_NAMES, }, - runtime_metadata::generate_decl_runtime_metadata, utils::{ extract_parameter_names_types_and_borrows, fold_fn_decl_for_client_side, generate_crate_access, generate_runtime_mod_name_for_trait, parse_runtime_api_version, @@ -188,13 +187,17 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result { let mut decl = decl.clone(); let decl_span = decl.span(); extend_generics_with_block(&mut decl.generics); - let metadata = generate_decl_runtime_metadata(&decl); let mod_name = generate_runtime_mod_name_for_trait(&decl.ident); let found_attributes = remove_supported_attributes(&mut decl.attrs); let api_version = 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)?; let mut methods_by_version: BTreeMap> = BTreeMap::new(); diff --git a/primitives/api/proc-macro/src/impl_runtime_apis.rs b/primitives/api/proc-macro/src/impl_runtime_apis.rs index 3883dfa4925e6..028cc6a675ee5 100644 --- a/primitives/api/proc-macro/src/impl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/impl_runtime_apis.rs @@ -17,7 +17,6 @@ use crate::{ common::API_VERSION_ATTRIBUTE, - runtime_metadata::generate_impl_runtime_metadata, utils::{ extract_all_signature_types, extract_block_type_from_trait_path, extract_impl_trait, extract_parameter_names_types_and_borrows, generate_crate_access, @@ -694,7 +693,11 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result { let runtime_api_versions = generate_runtime_api_versions(api_impls)?; let wasm_interface = generate_wasm_interface(api_impls)?; let api_impls_for_runtime_api = generate_api_impl_for_runtime_api(api_impls)?; - let runtime_metadata = generate_impl_runtime_metadata(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 diff --git a/primitives/api/proc-macro/src/lib.rs b/primitives/api/proc-macro/src/lib.rs index d34f4b7f9cf6a..06e148880e975 100644 --- a/primitives/api/proc-macro/src/lib.rs +++ b/primitives/api/proc-macro/src/lib.rs @@ -25,6 +25,7 @@ mod common; mod decl_runtime_apis; mod impl_runtime_apis; mod mock_impl_runtime_apis; +#[cfg(feature = "frame-metadata")] mod runtime_metadata; mod utils; diff --git a/primitives/api/proc-macro/src/utils.rs b/primitives/api/proc-macro/src/utils.rs index 6716be142febc..7b580ee6f099e 100644 --- a/primitives/api/proc-macro/src/utils.rs +++ b/primitives/api/proc-macro/src/utils.rs @@ -22,7 +22,7 @@ use syn::{ ImplItem, ItemImpl, Pat, Path, PathArguments, Result, ReturnType, Signature, Type, TypePath, }; -use quote::{format_ident, quote, ToTokens}; +use quote::{format_ident, quote}; use proc_macro_crate::{crate_name, FoundCrate}; @@ -259,7 +259,10 @@ 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 { + use quote::ToTokens; + attrs .iter() .filter_map(|attr| { @@ -275,6 +278,7 @@ pub fn get_doc_literals(attrs: &[syn::Attribute]) -> Vec { } /// Filters all attributes except the cfg ones. +#[cfg(feature = "frame-metadata")] pub fn filter_cfg_attributes(attrs: &[syn::Attribute]) -> Vec { attrs.iter().filter(|a| a.path().is_ident("cfg")).cloned().collect() } diff --git a/primitives/api/src/lib.rs b/primitives/api/src/lib.rs index 1a286a927e6e8..78c4cd73a18ae 100644 --- a/primitives/api/src/lib.rs +++ b/primitives/api/src/lib.rs @@ -84,6 +84,7 @@ use sp_core::OpaqueMetadata; #[doc(hidden)] pub use sp_core::{offchain, ExecutionContext}; #[doc(hidden)] +#[cfg(feature = "frame-metadata")] pub use sp_metadata_ir::{self as metadata_ir, frame_metadata as metadata}; #[doc(hidden)] #[cfg(feature = "std")]