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

Allow construct_runtime to take cfg attributes for pallets #11818

Merged
merged 15 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions frame/support/procedural/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ proc-macro = true

[dependencies]
Inflector = "0.11.4"
cfg-expr = "0.10.3"
itertools = "0.10.3"
proc-macro2 = "1.0.37"
quote = "1.0.10"
syn = { version = "1.0.98", features = ["full"] }
Expand Down
32 changes: 28 additions & 4 deletions frame/support/procedural/src/construct_runtime/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::construct_runtime::Pallet;
use proc_macro2::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::Ident;

pub fn expand_outer_dispatch(
Expand All @@ -30,6 +31,7 @@ pub fn expand_outer_dispatch(
let mut variant_patterns = Vec::new();
let mut query_call_part_macros = Vec::new();
let mut pallet_names = Vec::new();
let mut pallet_attrs = Vec::new();
let system_path = &system_pallet.path;

let pallets_with_call = pallet_decls.iter().filter(|decl| decl.exists_part("Call"));
Expand All @@ -38,12 +40,24 @@ pub fn expand_outer_dispatch(
let name = &pallet_declaration.name;
let path = &pallet_declaration.path;
let index = pallet_declaration.index;
let attr =
pallet_declaration.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});

variant_defs.extend(
quote!(#[codec(index = #index)] #name( #scrate::dispatch::CallableCallFor<#name, #runtime> ),),
);
variant_defs.extend(quote! {
#attr
#[codec(index = #index)]
#name( #scrate::dispatch::CallableCallFor<#name, #runtime> ),
});
variant_patterns.push(quote!(Call::#name(call)));
pallet_names.push(name);
pallet_attrs.push(attr);
query_call_part_macros.push(quote! {
#path::__substrate_call_check::is_call_part_defined!(#name);
});
Expand All @@ -69,6 +83,7 @@ pub fn expand_outer_dispatch(
use #scrate::dispatch::Callable;
use core::mem::size_of;
&[#(
#pallet_attrs
(
stringify!(#pallet_names),
size_of::< <#pallet_names as Callable<#runtime>>::Call >(),
Expand Down Expand Up @@ -101,7 +116,10 @@ pub fn expand_outer_dispatch(
impl #scrate::dispatch::GetDispatchInfo for Call {
fn get_dispatch_info(&self) -> #scrate::dispatch::DispatchInfo {
match self {
#( #variant_patterns => call.get_dispatch_info(), )*
#(
#pallet_attrs
#variant_patterns => call.get_dispatch_info(),
)*
}
}
}
Expand All @@ -110,6 +128,7 @@ pub fn expand_outer_dispatch(
use #scrate::dispatch::GetCallName;
match self {
#(
#pallet_attrs
#variant_patterns => {
let function_name = call.get_call_name();
let pallet_name = stringify!(#pallet_names);
Expand All @@ -121,6 +140,7 @@ pub fn expand_outer_dispatch(

fn get_module_names() -> &'static [&'static str] {
&[#(
#pallet_attrs
stringify!(#pallet_names),
)*]
}
Expand All @@ -129,6 +149,7 @@ pub fn expand_outer_dispatch(
use #scrate::dispatch::{Callable, GetCallName};
match module {
#(
#pallet_attrs
stringify!(#pallet_names) =>
<<#pallet_names as Callable<#runtime>>::Call
as GetCallName>::get_call_names(),
Expand Down Expand Up @@ -157,6 +178,7 @@ pub fn expand_outer_dispatch(
fn dispatch_bypass_filter(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
match self {
#(
#pallet_attrs
#variant_patterns =>
#scrate::traits::UnfilteredDispatchable::dispatch_bypass_filter(call, origin),
)*
Expand All @@ -165,6 +187,7 @@ pub fn expand_outer_dispatch(
}

#(
#pallet_attrs
impl #scrate::traits::IsSubType<#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> for Call {
#[allow(unreachable_patterns)]
fn is_sub_type(&self) -> Option<&#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> {
Expand All @@ -176,6 +199,7 @@ pub fn expand_outer_dispatch(
}
}

#pallet_attrs
impl From<#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> for Call {
fn from(call: #scrate::dispatch::CallableCallFor<#pallet_names, #runtime>) -> Self {
#variant_patterns
Expand Down
21 changes: 18 additions & 3 deletions frame/support/procedural/src/construct_runtime/expand/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::construct_runtime::Pallet;
use inflector::Inflector;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use std::str::FromStr;
use syn::Ident;

pub fn expand_outer_config(
Expand All @@ -40,11 +41,19 @@ pub fn expand_outer_config(
let field_name =
&Ident::new(&pallet_name.to_string().to_snake_case(), decl.name.span());
let part_is_generic = !pallet_entry.generics.params.is_empty();
let attr = &decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});

types.extend(expand_config_types(runtime, decl, &config, part_is_generic));
fields.extend(quote!(pub #field_name: #config,));
types.extend(expand_config_types(attr, runtime, decl, &config, part_is_generic));
fields.extend(quote!(#attr pub #field_name: #config,));
build_storage_calls
.extend(expand_config_build_storage_call(scrate, runtime, decl, field_name));
.extend(expand_config_build_storage_call(scrate, attr, runtime, decl, field_name));
query_genesis_config_part_macros.push(quote! {
#path::__substrate_genesis_config_check::is_genesis_config_defined!(#pallet_name);
#[cfg(feature = "std")]
Expand Down Expand Up @@ -88,6 +97,7 @@ pub fn expand_outer_config(
}

fn expand_config_types(
attr: &TokenStream,
runtime: &Ident,
decl: &Pallet,
config: &Ident,
Expand All @@ -97,14 +107,17 @@ fn expand_config_types(

match (decl.instance.as_ref(), part_is_generic) {
(Some(inst), true) => quote! {
#attr
#[cfg(any(feature = "std", test))]
pub type #config = #path::GenesisConfig<#runtime, #path::#inst>;
},
(None, true) => quote! {
#attr
#[cfg(any(feature = "std", test))]
pub type #config = #path::GenesisConfig<#runtime>;
},
(_, false) => quote! {
#attr
#[cfg(any(feature = "std", test))]
pub type #config = #path::GenesisConfig;
},
Expand All @@ -113,6 +126,7 @@ fn expand_config_types(

fn expand_config_build_storage_call(
scrate: &TokenStream,
attr: &TokenStream,
runtime: &Ident,
decl: &Pallet,
field_name: &Ident,
Expand All @@ -125,6 +139,7 @@ fn expand_config_build_storage_call(
};

quote! {
#attr
#scrate::sp_runtime::BuildModuleGenesisStorage::
<#runtime, #instance>::build_module_genesis_storage(&self.#field_name, storage)?;
}
Expand Down
43 changes: 35 additions & 8 deletions frame/support/procedural/src/construct_runtime/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::construct_runtime::Pallet;
use proc_macro2::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::{Generics, Ident};

pub fn expand_outer_event(
Expand Down Expand Up @@ -97,19 +98,35 @@ fn expand_event_variant(
let path = &pallet.path;
let variant_name = &pallet.name;
let part_is_generic = !generics.params.is_empty();
let attr = pallet.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});

match instance {
Some(inst) if part_is_generic => {
quote!(#[codec(index = #index)] #variant_name(#path::Event<#runtime, #path::#inst>),)
Some(inst) if part_is_generic => quote! {
#attr
#[codec(index = #index)]
#variant_name(#path::Event<#runtime, #path::#inst>),
},
Some(inst) => {
quote!(#[codec(index = #index)] #variant_name(#path::Event<#path::#inst>),)
Some(inst) => quote! {
#attr
#[codec(index = #index)]
#variant_name(#path::Event<#path::#inst>),
},
None if part_is_generic => {
quote!(#[codec(index = #index)] #variant_name(#path::Event<#runtime>),)
None if part_is_generic => quote! {
#attr
#[codec(index = #index)]
#variant_name(#path::Event<#runtime>),
},
None => {
quote!(#[codec(index = #index)] #variant_name(#path::Event),)
None => quote! {
#attr
#[codec(index = #index)]
#variant_name(#path::Event),
},
}
}
Expand All @@ -120,13 +137,23 @@ fn expand_event_conversion(
pallet_event: &TokenStream,
) -> TokenStream {
let variant_name = &pallet.name;
let attr = pallet.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});

quote! {
#attr
impl From<#pallet_event> for Event {
fn from(x: #pallet_event) -> Self {
Event::#variant_name(x)
}
}
#attr
impl TryInto<#pallet_event> for Event {
type Error = ();

Expand Down
Loading