Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v16: Expose the unstable metadata v16 #5732

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7e8baee
metadata-ir: Introduce PalletAssociatedTypeMetadata
lexnv Aug 7, 2024
1c12f15
frame/config: Add associated types to parsed config
lexnv Aug 7, 2024
20b22d8
frame/expand: Implement associated types expansion
lexnv Aug 7, 2024
350efce
frame/expand: Use provided cfgs for the associated types
lexnv Aug 7, 2024
3e97971
frame/construct_runtime: Extract associated types from pallet config
lexnv Aug 7, 2024
5d8021e
frame/pallet: Introduce `config(without_metadata)`
lexnv Aug 7, 2024
5679244
frame/pallet: Introduce `#[pallet::include_metadata]` for associated
lexnv Aug 7, 2024
958611e
frame/pallet: Include associated type iff bounds contain TypeInfo
lexnv Aug 7, 2024
7f26b67
frame/pallet: Proper flag for metdata collection
lexnv Aug 7, 2024
cc001f3
frame/tests/ui: Fix type in test
lexnv Aug 8, 2024
b2803e2
frame/tests/ui: Check config without metadata
lexnv Aug 8, 2024
94007d8
frame/tests/ui: Check config with multiple attributes
lexnv Aug 8, 2024
75d2697
frame/tests/ui: Add negative test for duplicate attributes in config
lexnv Aug 8, 2024
ebcb4a0
frame/tests/ui: Add negative test for collecting metadata from constants
lexnv Aug 8, 2024
5115021
frame/tests/ui: Add negative test for metadata collection on events
lexnv Aug 8, 2024
aba029a
frame/tests: Check PalletAssociatedTypeMetadataIR collection
lexnv Aug 8, 2024
6502c83
frame/support: Add documentation
lexnv Aug 8, 2024
e9571cb
Add PRdoc
lexnv Aug 8, 2024
31bf284
Merge remote-tracking branch 'origin/master' into lexnv/metadata-v16-…
lexnv Aug 8, 2024
7429eb7
Update prdoc
lexnv Aug 8, 2024
8a0c138
prdoc: Remove unneeded crate
lexnv Aug 8, 2024
c6c1800
prdoc: Include frame-support
lexnv Aug 8, 2024
90c100c
Prupdate
lexnv Aug 8, 2024
fc87031
metadata-ir: Add v16 wrappers in preparation for frame-md release
lexnv Sep 13, 2024
f627476
Merge remote-tracking branch 'origin/master' into lexnv/metadata-v16-…
lexnv Sep 13, 2024
b533f60
metadata-ir: Add conversion methods
lexnv Sep 13, 2024
e298259
metadata-ir: Enable v16 version as unstable
lexnv Sep 13, 2024
d0ff8f4
metadata-ir: Fix into stable conversion
lexnv Sep 13, 2024
4060637
frame-md: Use git dependency instead
lexnv Sep 16, 2024
adf7508
metadata-ir: Adjust for new transaction format
lexnv Sep 16, 2024
2982c59
Merge remote-tracking branch 'origin/master' into lexnv/metadata-v16-…
lexnv Sep 16, 2024
4ee1d7f
Merge branch 'lexnv/metadata-v16-associated-types' into lexnv/metadat…
lexnv Sep 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ frame-benchmarking-pallet-pov = { default-features = false, path = "substrate/fr
frame-election-provider-solution-type = { path = "substrate/frame/election-provider-support/solution-type", default-features = false }
frame-election-provider-support = { path = "substrate/frame/election-provider-support", default-features = false }
frame-executive = { path = "substrate/frame/executive", default-features = false }
frame-metadata = { version = "16.0.0", default-features = false }
frame-metadata = { git = "https://github.com/paritytech/frame-metadata.git", branch = "lexnv/metadata-v16-associated-types", default-features = false }
frame-metadata-hash-extension = { path = "substrate/frame/metadata-hash-extension", default-features = false }
frame-support = { path = "substrate/frame/support", default-features = false }
frame-support-procedural = { path = "substrate/frame/support/procedural", default-features = false }
Expand Down
24 changes: 24 additions & 0 deletions prdoc/pr_5274.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
title: Enrich metadata IR with associated types of config traits

doc:
- audience: Runtime Dev
description: |
This feature is part of the upcoming metadata V16. The associated types of the `Config` trait that require the `TypeInfo`
or `Parameter` bounds are included in the metadata of the pallet. The metadata is not yet exposed to the end-user, however
the metadata intermediate representation (IR) contains these types.

Developers can opt out of metadata collection of the associated types by specifying `without_metadata` optional attribute
to the `#[pallet::config]`.

Furthermore, the `without_metadata` argument can be used in combination with the newly added `#[pallet::include_metadata]`
attribute to selectively include only certain associated types in the metadata collection.

crates:
- name: frame-support
bump: patch
- name: frame-support-procedural
bump: patch
- name: frame-support-procedural-tools
bump: patch
- name: sp-metadata-ir
bump: major
2 changes: 1 addition & 1 deletion substrate/frame/metadata-hash-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ substrate-test-runtime-client = { workspace = true }
sp-api = { workspace = true, default-features = true }
sp-transaction-pool = { workspace = true, default-features = true }
merkleized-metadata = { workspace = true }
frame-metadata = { features = ["current"], workspace = true, default-features = true }
frame-metadata = { features = ["current","unstable"], workspace = true, default-features = true }
sp-tracing = { workspace = true, default-features = true }

[features]
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ scale-info = { features = [
], workspace = true }
frame-metadata = { features = [
"current",
"unstable",
], workspace = true }
sp-api = { features = [
"frame-metadata",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn expand_runtime_metadata(
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);
let associated_types = expand_pallet_metadata_associated_types(runtime, decl);
let docs = expand_pallet_metadata_docs(runtime, decl);
let attr = decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
Expand All @@ -70,6 +71,7 @@ pub fn expand_runtime_metadata(
constants: #constants,
error: #errors,
docs: #docs,
associated_types: #associated_types,
deprecation_info: #deprecation_info,
}
}
Expand Down Expand Up @@ -261,3 +263,12 @@ fn expand_pallet_metadata_docs(runtime: &Ident, decl: &Pallet) -> TokenStream {
#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_documentation_metadata()
}
}

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

quote! {
#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_associated_types_metadata()
}
}
9 changes: 9 additions & 0 deletions substrate/frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,15 @@ pub fn event(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
}

///
/// ---
///
/// Documentation for this macro can be found at `frame_support::pallet_macros::include_metadata`.
#[proc_macro_attribute]
pub fn include_metadata(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
}

///
/// ---
///
Expand Down
47 changes: 47 additions & 0 deletions substrate/frame/support/procedural/src/pallet/expand/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,50 @@ Consequently, a runtime that wants to include this pallet must implement this tr
_ => Default::default(),
}
}

/// Generate the metadata for the associated types of the config trait.
///
/// Implements the `pallet_associated_types_metadata` function for the pallet.
pub fn expand_config_metadata(def: &Def) -> proc_macro2::TokenStream {
let frame_support = &def.frame_support;
let type_impl_gen = &def.type_impl_generics(proc_macro2::Span::call_site());
let type_use_gen = &def.type_use_generics(proc_macro2::Span::call_site());
let pallet_ident = &def.pallet_struct.pallet;
let trait_use_gen = &def.trait_use_generics(proc_macro2::Span::call_site());

let mut where_clauses = vec![&def.config.where_clause];
where_clauses.extend(def.extra_constants.iter().map(|d| &d.where_clause));
let completed_where_clause = super::merge_where_clauses(&where_clauses);

let types = def.config.associated_types_metadata.iter().map(|metadata| {
let ident = &metadata.ident;
let ident_str = format!("{}", ident);
let cfgs = &metadata.cfg;

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

quote::quote!({
#( #cfgs ) *
#frame_support::__private::metadata_ir::PalletAssociatedTypeMetadataIR {
name: #ident_str,
ty: #frame_support::__private::scale_info::meta_type::<
<T as Config #trait_use_gen>::#ident
>(),
docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ],
}
})
});

quote::quote!(
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause {

#[doc(hidden)]
pub fn pallet_associated_types_metadata()
-> #frame_support::__private::sp_std::vec::Vec<#frame_support::__private::metadata_ir::PalletAssociatedTypeMetadataIR>
{
#frame_support::__private::sp_std::vec![ #( #types ),* ]
}
}
)
}
2 changes: 2 additions & 0 deletions substrate/frame/support/procedural/src/pallet/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn expand(mut def: Def) -> proc_macro2::TokenStream {
let constants = constants::expand_constants(&mut def);
let pallet_struct = pallet_struct::expand_pallet_struct(&mut def);
let config = config::expand_config(&mut def);
let associated_types = config::expand_config_metadata(&def);
let call = call::expand_call(&mut def);
let tasks = tasks::expand_tasks(&mut def);
let error = error::expand_error(&mut def);
Expand Down Expand Up @@ -101,6 +102,7 @@ storage item. Otherwise, all storage items are listed among [*Type Definitions*]
#constants
#pallet_struct
#config
#associated_types
#call
#tasks
#error
Expand Down
Loading
Loading