Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
sp-api: Put frame-metadata behind some feature (#14398)
Browse files Browse the repository at this point in the history
* sp-api: Put `frame-metadata` behind some feature

Closes: #14296

* ".git/.scripts/commands/fmt/fmt.sh"

* Review feedback

---------

Co-authored-by: command-bot <>
  • Loading branch information
bkchr committed Jul 3, 2023
1 parent 26692ff commit 1b00cba
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
10 changes: 7 additions & 3 deletions primitives/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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"
]
1 change: 1 addition & 0 deletions primitives/api/proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ assert_matches = "1.3.0"
default = ["std"]
std = []
no-metadata-docs = []
frame-metadata = []
7 changes: 5 additions & 2 deletions primitives/api/proc-macro/src/decl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -188,13 +187,17 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
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<u64, Vec<TraitItemFn>> = BTreeMap::new();
Expand Down
7 changes: 5 additions & 2 deletions primitives/api/proc-macro/src/impl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -694,7 +693,11 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result<TokenStream> {
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
Expand Down
1 change: 1 addition & 0 deletions primitives/api/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion primitives/api/proc-macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<syn::Lit> {
use quote::ToTokens;

attrs
.iter()
.filter_map(|attr| {
Expand All @@ -275,6 +278,7 @@ 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 primitives/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 1b00cba

Please sign in to comment.