sp_api_proc_macro/
runtime_metadata.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use proc_macro2::TokenStream as TokenStream2;
19use quote::quote;
20use syn::{parse_quote, ItemImpl, ItemTrait, Result};
21
22use crate::{
23	common::CHANGED_IN_ATTRIBUTE,
24	utils::{
25		extract_impl_trait, filter_cfg_attributes, generate_crate_access,
26		generate_runtime_mod_name_for_trait, get_doc_literals, RequireQualifiedTraitPath,
27	},
28};
29
30/// Get the type parameter argument without lifetime or mutability
31/// of a runtime metadata function.
32///
33/// In the following example, both the `AccountId` and `Nonce` generic
34/// type parameters must implement `scale_info::TypeInfo` because they
35/// are added into the metadata using `scale_info::meta_type`.
36///
37/// ```ignore
38/// trait ExampleAccountNonceApi<AccountId, Nonce> {
39///   fn account_nonce<'a>(account: &'a AccountId) -> Nonce;
40/// }
41/// ```
42///
43/// Instead of returning `&'a AccountId` for the first parameter, this function
44/// returns `AccountId` to place bounds around it.
45fn get_type_param(ty: &syn::Type) -> syn::Type {
46	// Remove the lifetime and mutability of the type T to
47	// place bounds around it.
48	let ty_elem = match &ty {
49		syn::Type::Reference(reference) => &reference.elem,
50		syn::Type::Ptr(ptr) => &ptr.elem,
51		syn::Type::Slice(slice) => &slice.elem,
52		syn::Type::Array(arr) => &arr.elem,
53		_ => ty,
54	};
55
56	ty_elem.clone()
57}
58
59/// Extract the documentation from the provided attributes.
60///
61/// It takes into account the `no-metadata-docs` feature.
62fn collect_docs(attrs: &[syn::Attribute], crate_: &TokenStream2) -> TokenStream2 {
63	if cfg!(feature = "no-metadata-docs") {
64		quote!(#crate_::vec![])
65	} else {
66		let docs = get_doc_literals(&attrs);
67		quote!(#crate_::vec![ #( #docs, )* ])
68	}
69}
70
71/// Generate the runtime metadata of the provided trait.
72///
73/// The metadata is exposed as a generic function on the hidden module
74/// of the trait generated by the `decl_runtime_apis`.
75pub fn generate_decl_runtime_metadata(decl: &ItemTrait) -> TokenStream2 {
76	let crate_ = generate_crate_access();
77	let mut methods = Vec::new();
78
79	// Ensure that any function parameter that relies on the `BlockT` bounds
80	// also has `TypeInfo + 'static` bounds (required by `scale_info::meta_type`).
81	//
82	// For example, if a runtime API defines a method that has an input:
83	// `fn func(input: <Block as BlockT>::Header)`
84	// then the runtime metadata will imply `<Block as BlockT>::Header: TypeInfo + 'static`.
85	//
86	// This restricts the bounds at the metadata level, without needing to modify the `BlockT`
87	// itself, since the concrete implementations are already satisfying `TypeInfo`.
88	let mut where_clause = Vec::new();
89	for item in &decl.items {
90		// Collect metadata for methods only.
91		let syn::TraitItem::Fn(method) = item else { continue };
92
93		// Collect metadata only for the latest methods.
94		let is_changed_in =
95			method.attrs.iter().any(|attr| attr.path().is_ident(CHANGED_IN_ATTRIBUTE));
96		if is_changed_in {
97			continue;
98		}
99
100		let mut inputs = Vec::new();
101		let signature = &method.sig;
102		for input in &signature.inputs {
103			// Exclude `self` from metadata collection.
104			let syn::FnArg::Typed(typed) = input else { continue };
105
106			let pat = &typed.pat;
107			let name = quote!(#pat).to_string();
108			let ty = &typed.ty;
109
110			where_clause.push(get_type_param(ty));
111
112			inputs.push(quote!(
113				#crate_::metadata_ir::RuntimeApiMethodParamMetadataIR {
114					name: #name,
115					ty: #crate_::scale_info::meta_type::<#ty>(),
116				}
117			));
118		}
119
120		let output = match &signature.output {
121			syn::ReturnType::Default => quote!(#crate_::scale_info::meta_type::<()>()),
122			syn::ReturnType::Type(_, ty) => {
123				where_clause.push(get_type_param(ty));
124				quote!(#crate_::scale_info::meta_type::<#ty>())
125			},
126		};
127
128		// String method name including quotes for constructing `v15::RuntimeApiMethodMetadata`.
129		let method_name = signature.ident.to_string();
130		let docs = collect_docs(&method.attrs, &crate_);
131
132		// Include the method metadata only if its `cfg` features are enabled.
133		let attrs = filter_cfg_attributes(&method.attrs);
134		let deprecation = match crate::utils::get_deprecation(&crate_, &method.attrs) {
135			Ok(deprecation) => deprecation,
136			Err(e) => return e.into_compile_error(),
137		};
138		methods.push(quote!(
139			#( #attrs )*
140			#crate_::metadata_ir::RuntimeApiMethodMetadataIR {
141				name: #method_name,
142				inputs: #crate_::vec![ #( #inputs, )* ],
143				output: #output,
144				docs: #docs,
145				deprecation_info: #deprecation,
146			}
147		));
148	}
149
150	let trait_name_ident = &decl.ident;
151	let trait_name = trait_name_ident.to_string();
152	let docs = collect_docs(&decl.attrs, &crate_);
153	let deprecation = match crate::utils::get_deprecation(&crate_, &decl.attrs) {
154		Ok(deprecation) => deprecation,
155		Err(e) => return e.into_compile_error(),
156	};
157	let attrs = filter_cfg_attributes(&decl.attrs);
158	// The trait generics where already extended with `Block: BlockT`.
159	let mut generics = decl.generics.clone();
160	for generic_param in generics.params.iter_mut() {
161		let syn::GenericParam::Type(ty) = generic_param else { continue };
162
163		// Default type parameters are not allowed in functions.
164		ty.eq_token = None;
165		ty.default = None;
166	}
167
168	where_clause
169		.into_iter()
170		.map(|ty| parse_quote!(#ty: #crate_::scale_info::TypeInfo + 'static))
171		.for_each(|w| generics.make_where_clause().predicates.push(w));
172
173	let (impl_generics, _, where_clause) = generics.split_for_impl();
174
175	quote!(
176		#crate_::frame_metadata_enabled! {
177			#( #attrs )*
178			#[inline(always)]
179			pub fn runtime_metadata #impl_generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR
180				#where_clause
181			{
182				#crate_::metadata_ir::RuntimeApiMetadataIR {
183					name: #trait_name,
184					methods: #crate_::vec![ #( #methods, )* ],
185					docs: #docs,
186					deprecation_info: #deprecation,
187				}
188			}
189		}
190	)
191}
192
193/// Implement the `runtime_metadata` function on the runtime that
194/// generates the metadata for the given traits.
195///
196/// The metadata of each trait is extracted from the generic function
197/// exposed by `generate_decl_runtime_metadata`.
198pub fn generate_impl_runtime_metadata(impls: &[ItemImpl]) -> Result<TokenStream2> {
199	if impls.is_empty() {
200		return Ok(quote!());
201	}
202
203	let crate_ = generate_crate_access();
204
205	// Get the name of the runtime for which the traits are implemented.
206	let runtime_name = &impls
207		.get(0)
208		.expect("Traits should contain at least one implementation; qed")
209		.self_ty;
210
211	let mut metadata = Vec::new();
212
213	for impl_ in impls {
214		let mut trait_ = extract_impl_trait(&impl_, RequireQualifiedTraitPath::Yes)?.clone();
215
216		// Implementation traits are always references with a path `impl client::Core<generics> ...`
217		// The trait name is the last segment of this path.
218		let trait_name_ident = &trait_
219			.segments
220			.last()
221			.as_ref()
222			.expect("Trait path should always contain at least one item; qed")
223			.ident;
224
225		// Extract the generics from the trait to pass to the `runtime_metadata`
226		// function on the hidden module.
227		let generics = trait_
228			.segments
229			.iter()
230			.find_map(|segment| {
231				if let syn::PathArguments::AngleBracketed(generics) = &segment.arguments {
232					Some(generics.clone())
233				} else {
234					None
235				}
236			})
237			.expect("Trait path should always contain at least one generic parameter; qed");
238
239		let mod_name = generate_runtime_mod_name_for_trait(&trait_name_ident);
240		// Get absolute path to the `runtime_decl_for_` module by replacing the last segment.
241		if let Some(segment) = trait_.segments.last_mut() {
242			*segment = parse_quote!(#mod_name);
243		}
244
245		let attrs = filter_cfg_attributes(&impl_.attrs);
246		metadata.push(quote!(
247			#( #attrs )*
248			#trait_::runtime_metadata::#generics()
249		));
250	}
251
252	// Each runtime must expose the `runtime_metadata()` to fetch the runtime API metadata.
253	// The function is implemented by calling `impl_runtime_apis!`.
254	//
255	// However, the `construct_runtime!` may be called without calling `impl_runtime_apis!`.
256	// Rely on the `Deref` trait to differentiate between a runtime that implements
257	// APIs (by macro impl_runtime_apis!) and a runtime that is simply created (by macro
258	// construct_runtime!).
259	//
260	// Both `InternalConstructRuntime` and `InternalImplRuntimeApis` expose a `runtime_metadata()`
261	// function. `InternalConstructRuntime` is implemented by the `construct_runtime!` for Runtime
262	// references (`& Runtime`), while `InternalImplRuntimeApis` is implemented by the
263	// `impl_runtime_apis!` for Runtime (`Runtime`).
264	//
265	// Therefore, the `Deref` trait will resolve the `runtime_metadata` from `impl_runtime_apis!`
266	// when both macros are called; and will resolve an empty `runtime_metadata` when only the
267	// `construct_runtime!` is called.
268
269	Ok(quote!(
270		#crate_::frame_metadata_enabled! {
271			#[doc(hidden)]
272			trait InternalImplRuntimeApis {
273				#[inline(always)]
274				fn runtime_metadata(&self) -> #crate_::vec::Vec<#crate_::metadata_ir::RuntimeApiMetadataIR> {
275					#crate_::vec![ #( #metadata, )* ]
276				}
277			}
278			#[doc(hidden)]
279			impl InternalImplRuntimeApis for #runtime_name {}
280		}
281	))
282}