Skip to content

Commit

Permalink
change metadata for view fns
Browse files Browse the repository at this point in the history
  • Loading branch information
re-gius committed Jan 31, 2025
1 parent 698d9ae commit 0678c94
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn expand_runtime_metadata(
let index = &decl.index;
let storage = expand_pallet_metadata_storage(&filtered_names, runtime, decl);
let calls = expand_pallet_metadata_calls(&filtered_names, runtime, decl);
let view_functions = expand_pallet_metadata_view_functions(runtime, decl);
let event = expand_pallet_metadata_events(&filtered_names, runtime, decl);
let constants = expand_pallet_metadata_constants(runtime, decl);
let errors = expand_pallet_metadata_errors(runtime, decl);
Expand All @@ -59,6 +60,7 @@ pub fn expand_runtime_metadata(
index: #index,
storage: #storage,
calls: #calls,
view_functions: #view_functions,
event: #event,
constants: #constants,
error: #errors,
Expand All @@ -70,20 +72,6 @@ pub fn expand_runtime_metadata(
})
.collect::<Vec<_>>();

let view_functions = pallet_declarations.iter().map(|decl| {
let name = &decl.name;
let path = &decl.path;
let instance = decl.instance.as_ref().into_iter();
let attr = decl.get_attributes();

quote! {
#attr
#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_view_functions_metadata(
::core::stringify!(#name)
)
}
});

quote! {
impl #runtime {
fn metadata_ir() -> #scrate::__private::metadata_ir::MetadataIR {
Expand Down Expand Up @@ -156,10 +144,6 @@ pub fn expand_runtime_metadata(
event_enum_ty: #scrate::__private::scale_info::meta_type::<RuntimeEvent>(),
error_enum_ty: #scrate::__private::scale_info::meta_type::<RuntimeError>(),
},
view_functions: #scrate::__private::metadata_ir::RuntimeViewFunctionsIR {
ty: #scrate::__private::scale_info::meta_type::<RuntimeViewFunction>(),
groups: #scrate::__private::sp_std::vec![ #(#view_functions),* ],
}
}
}

Expand Down Expand Up @@ -216,6 +200,15 @@ fn expand_pallet_metadata_calls(
}
}

fn expand_pallet_metadata_view_functions(runtime: &Ident, decl: &Pallet) -> TokenStream {
let path = &decl.path;
let instance = decl.instance.as_ref().into_iter();

quote! {
#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_view_functions_metadata()
}
}

fn expand_pallet_metadata_events(
filtered_names: &[&'static str],
runtime: &Ident,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ use proc_macro2::{Span, TokenStream};
use syn::spanned::Spanned;

pub fn expand_view_functions(def: &Def) -> TokenStream {
let (span, where_clause, view_fns, docs) = match def.view_functions.as_ref() {
Some(view_fns) => (
view_fns.attr_span,
view_fns.where_clause.clone(),
view_fns.view_functions.clone(),
view_fns.docs.clone(),
),
None => (def.item.span(), def.config.where_clause.clone(), Vec::new(), Vec::new()),
let (span, where_clause, view_fns) = match def.view_functions.as_ref() {
Some(view_fns) =>
(view_fns.attr_span, view_fns.where_clause.clone(), view_fns.view_functions.clone()),
None => (def.item.span(), def.config.where_clause.clone(), Vec::new()),
};

let view_function_prefix_impl =
Expand All @@ -39,7 +35,7 @@ pub fn expand_view_functions(def: &Def) -> TokenStream {
let impl_dispatch_view_function =
impl_dispatch_view_function(def, span, where_clause.as_ref(), &view_fns);
let impl_view_function_metadata =
impl_view_function_metadata(def, span, where_clause.as_ref(), &view_fns, &docs);
impl_view_function_metadata(def, span, where_clause.as_ref(), &view_fns);

quote::quote! {
#view_function_prefix_impl
Expand Down Expand Up @@ -201,7 +197,6 @@ fn impl_view_function_metadata(
span: Span,
where_clause: Option<&syn::WhereClause>,
view_fns: &[ViewFunctionDef],
docs: &[syn::Expr],
) -> TokenStream {
let frame_support = &def.frame_support;
let pallet_ident = &def.pallet_struct.pallet;
Expand All @@ -211,14 +206,14 @@ fn impl_view_function_metadata(
let view_functions = view_fns.iter().map(|view_fn| {
let view_function_struct_ident = view_fn.view_function_struct_ident();
let name = &view_fn.name;
let args = view_fn.args.iter().filter_map(|fn_arg| {
let inputs = view_fn.args.iter().filter_map(|fn_arg| {
match fn_arg {
syn::FnArg::Receiver(_) => None,
syn::FnArg::Typed(typed) => {
let pat = &typed.pat;
let ty = &typed.ty;
Some(quote::quote! {
#frame_support::__private::metadata_ir::ViewFunctionArgMetadataIR {
#frame_support::__private::metadata_ir::PalletViewFunctionMethodParamMetadataIR {
name: ::core::stringify!(#pat),
ty: #frame_support::__private::scale_info::meta_type::<#ty>(),
}
Expand All @@ -230,33 +225,34 @@ fn impl_view_function_metadata(
let no_docs = vec![];
let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &view_fn.docs };

let deprecation = match crate::deprecation::get_deprecation(
&quote::quote! { #frame_support },
&def.item.attrs,
) {
Ok(deprecation) => deprecation,
Err(e) => return e.into_compile_error(),
};

quote::quote! {
#frame_support::__private::metadata_ir::ViewFunctionMetadataIR {
#frame_support::__private::metadata_ir::PalletViewFunctionMethodMetadata {
name: ::core::stringify!(#name),
id: <#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunction>::id().into(),
args: #frame_support::__private::sp_std::vec![ #( #args ),* ],
inputs: #frame_support::__private::sp_std::vec![ #( #inputs ),* ],
output: #frame_support::__private::scale_info::meta_type::<
<#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunction>::ReturnType
>(),
docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ],
deprecation_info: #deprecation,
}
}
});

let no_docs = vec![];
let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { docs };

quote::quote! {
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause {
#[doc(hidden)]
pub fn pallet_view_functions_metadata(name: &'static ::core::primitive::str)
-> #frame_support::__private::metadata_ir::ViewFunctionGroupIR
{
#frame_support::__private::metadata_ir::ViewFunctionGroupIR {
name,
view_functions: #frame_support::__private::sp_std::vec![ #( #view_functions ),* ],
docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ],
}
pub fn pallet_view_functions_metadata()
-> #frame_support::__private::Vec<#frame_support::__private::metadata_ir::PalletViewFunctionMethodMetadataIR> {
#frame_support::__private::vec![ #( #view_functions ),* ]
}
}
}
Expand Down
1 change: 0 additions & 1 deletion substrate/primitives/metadata-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ mod test {
event_enum_ty: meta_type::<()>(),
error_enum_ty: meta_type::<()>(),
},
view_functions: RuntimeViewFunctionsIR { ty: meta_type::<()>(), groups: vec![] },
}
}

Expand Down
84 changes: 29 additions & 55 deletions substrate/primitives/metadata-ir/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ pub struct MetadataIR<T: Form = MetaForm> {
pub apis: Vec<RuntimeApiMetadataIR<T>>,
/// The outer enums types as found in the runtime.
pub outer_enums: OuterEnumsIR<T>,
/// Metadata of view function queries
pub view_functions: RuntimeViewFunctionsIR<T>,
}

/// Metadata of a runtime trait.
Expand Down Expand Up @@ -120,83 +118,52 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR {
}
}

/// Metadata of the top level runtime view function dispatch.
/// Metadata of a pallet view function method.
#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)]
pub struct RuntimeViewFunctionsIR<T: Form = MetaForm> {
/// The type implementing the runtime query dispatch.
pub ty: T::Type,
/// The view function groupings metadata.
pub groups: Vec<ViewFunctionGroupIR<T>>,
}

/// Metadata of a runtime view function group.
///
/// For example, view functions associated with a pallet would form a view function group.
#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)]
pub struct ViewFunctionGroupIR<T: Form = MetaForm> {
/// Name of the view function group.
pub name: T::String,
/// View functions belonging to the group.
pub view_functions: Vec<ViewFunctionMetadataIR<T>>,
/// View function group documentation.
pub docs: Vec<T::String>,
}

impl IntoPortable for ViewFunctionGroupIR {
type Output = ViewFunctionGroupIR<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ViewFunctionGroupIR {
name: self.name.into_portable(registry),
view_functions: registry.map_into_portable(self.view_functions),
docs: registry.map_into_portable(self.docs),
}
}
}

/// Metadata of a runtime view function.
#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)]
pub struct ViewFunctionMetadataIR<T: Form = MetaForm> {
/// Query name.
pub struct PalletViewFunctionMethodMetadataIR<T: Form = MetaForm> {
/// Method name.
pub name: T::String,
/// Query id.
/// Method id.
pub id: [u8; 32],
/// Query args.
pub args: Vec<ViewFunctionArgMetadataIR<T>>,
/// Query output.
/// Method parameters.
pub inputs: Vec<PalletViewFunctionMethodParamMetadataIR<T>>,
/// Method output.
pub output: T::Type,
/// Query documentation.
/// Method documentation.
pub docs: Vec<T::String>,
/// Deprecation info
pub deprecation_info: DeprecationStatusIR<T>,
}

impl IntoPortable for ViewFunctionMetadataIR {
type Output = ViewFunctionMetadataIR<PortableForm>;
impl IntoPortable for PalletViewFunctionMethodMetadataIR {
type Output = PalletViewFunctionMethodMetadataIR<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ViewFunctionMetadataIR {
PalletViewFunctionMethodMetadataIR {
name: self.name.into_portable(registry),
id: self.id,
args: registry.map_into_portable(self.args),
inputs: registry.map_into_portable(self.inputs),
output: registry.register_type(&self.output),
docs: registry.map_into_portable(self.docs),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}

/// Metadata of a runtime method argument.
/// Metadata of a pallet view function method argument.
#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)]
pub struct ViewFunctionArgMetadataIR<T: Form = MetaForm> {
/// Query argument name.
pub struct PalletViewFunctionMethodParamMetadataIR<T: Form = MetaForm> {
/// Parameter name.
pub name: T::String,
/// Query argument type.
/// Parameter type.
pub ty: T::Type,
}

impl IntoPortable for ViewFunctionArgMetadataIR {
type Output = ViewFunctionArgMetadataIR<PortableForm>;
impl IntoPortable for PalletViewFunctionMethodParamMetadataIR {
type Output = PalletViewFunctionMethodParamMetadataIR<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ViewFunctionArgMetadataIR {
PalletViewFunctionMethodParamMetadataIR {
name: self.name.into_portable(registry),
ty: registry.register_type(&self.ty),
}
Expand All @@ -212,6 +179,8 @@ pub struct PalletMetadataIR<T: Form = MetaForm> {
pub storage: Option<PalletStorageMetadataIR<T>>,
/// Pallet calls metadata.
pub calls: Option<PalletCallMetadataIR<T>>,
/// Pallet view functions metadata.
pub view_functions: Vec<PalletViewFunctionMethodMetadataIR<T>>,
/// Pallet event metadata.
pub event: Option<PalletEventMetadataIR<T>>,
/// Pallet constants metadata.
Expand All @@ -237,6 +206,11 @@ impl IntoPortable for PalletMetadataIR {
name: self.name.into_portable(registry),
storage: self.storage.map(|storage| storage.into_portable(registry)),
calls: self.calls.map(|calls| calls.into_portable(registry)),
view_functions: self
.view_functions
.into_iter()
.map(|view_functions| view_functions.into_portable(registry))
.collect(),
event: self.event.map(|event| event.into_portable(registry)),
constants: registry.map_into_portable(self.constants),
error: self.error.map(|error| error.into_portable(registry)),
Expand Down
2 changes: 2 additions & 0 deletions substrate/primitives/metadata-ir/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ impl From<PalletMetadataIR> for PalletMetadata {
name: ir.name,
storage: ir.storage.map(Into::into),
calls: ir.calls.map(Into::into),
// TODO: add with the new release of frame-metadata
// view_functions: ir.view_functions.into_iter().map(Into::into).collect(),
event: ir.event.map(Into::into),
constants: ir.constants.into_iter().map(Into::into).collect(),
error: ir.error.map(Into::into),
Expand Down
16 changes: 4 additions & 12 deletions substrate/primitives/metadata-ir/src/v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use super::types::{
};

use frame_metadata::v15::{
CustomMetadata, CustomValueMetadata, ExtrinsicMetadata, OuterEnums, PalletMetadata,
RuntimeApiMetadata, RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata,
RuntimeMetadataV15, SignedExtensionMetadata,
CustomMetadata, ExtrinsicMetadata, OuterEnums, PalletMetadata, RuntimeApiMetadata,
RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, RuntimeMetadataV15,
SignedExtensionMetadata,
};
use scale_info::{IntoPortable, Registry};

Expand All @@ -39,15 +39,7 @@ impl From<MetadataIR> for RuntimeMetadataV15 {
let apis =
registry.map_into_portable(ir.apis.into_iter().map(Into::<RuntimeApiMetadata>::into));
let outer_enums = Into::<OuterEnums>::into(ir.outer_enums).into_portable(&mut registry);

let view_function_groups = registry.map_into_portable(ir.view_functions.groups.into_iter());
let view_functions_custom_metadata = CustomValueMetadata {
ty: ir.view_functions.ty,
value: codec::Encode::encode(&view_function_groups),
};
let mut custom_map = alloc::collections::BTreeMap::new();
custom_map.insert("view_functions_experimental", view_functions_custom_metadata);
let custom = CustomMetadata { map: custom_map }.into_portable(&mut registry);
let custom = CustomMetadata { map: Default::default() };

Self { types: registry.into(), pallets, extrinsic, ty, apis, outer_enums, custom }
}
Expand Down

0 comments on commit 0678c94

Please sign in to comment.