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

Allow usage of path in construct_runtime! #8801

Merged
18 commits merged into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
173 changes: 173 additions & 0 deletions frame/support/procedural/src/construct_runtime/expand/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// This file is part of Substrate.

// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

use crate::construct_runtime::{Pallet, parse::PalletPath};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Generics, Ident};

pub fn expand_outer_event(
runtime: &Ident,
pallet_decls: &[Pallet],
scrate: &TokenStream,
) -> syn::Result<TokenStream> {
let mut event_variants = TokenStream::new();
let mut event_conversions = TokenStream::new();
let mut events_metadata = TokenStream::new();
let mut pallet_event_fns = TokenStream::new();

for pallet_decl in pallet_decls {
if let Some(pallet_entry) = pallet_decl.find_part("Event") {
let path = &pallet_decl.pallet;
let index = pallet_decl.index;
let instance = pallet_decl.instance.as_ref();
let generics = &pallet_entry.generics;

if instance.is_some() && generics.params.is_empty() {
let msg = format!(
"Instantiable pallet with no generic `Event` cannot \
be constructed: pallet `{}` must have generic `Event`",
pallet_decl.name,
);
return Err(syn::Error::new(pallet_decl.name.span(), msg));
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
}

let pallet_is_generic = !generics.params.is_empty();
let pallet_event = match (instance, pallet_is_generic) {
(Some(inst), true) => quote!(#path::Event::<#runtime, #path::#inst>),
(Some(inst), false) => quote!(#path::Event::<#path::#inst>),
(None, true) => quote!(#path::Event::<#runtime>),
(None, false) => quote!(#path::Event),
};

event_variants.extend(expand_event_variant(runtime, path, index, instance, generics));
event_conversions.extend(expand_event_conversion(scrate, path, instance, &pallet_event));
events_metadata.extend(expand_event_metadata(scrate, path, &pallet_event));
pallet_event_fns.extend(expand_pallet_event_fn(scrate, path, instance, &pallet_event));
}
}

Ok(quote!{
#[derive(
Clone, PartialEq, Eq,
#scrate::codec::Encode,
#scrate::codec::Decode,
#scrate::RuntimeDebug,
)]
#[allow(non_camel_case_types)]
pub enum Event {
#event_variants
}

#event_conversions

impl #runtime {
#pallet_event_fns
}
})
}

fn expand_event_variant(
runtime: &Ident,
path: &PalletPath,
index: u8,
instance: Option<&Ident>,
generics: &Generics,
) -> TokenStream {
let pallet_is_generic = !generics.params.is_empty();
let mod_name = &path.mod_name();

match (instance, pallet_is_generic) {
(Some(inst), true) => {
let variant = format_ident!("{}_{}", mod_name, inst);
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
quote!(#[codec(index = #index)] #variant(#path::Event<#runtime, #path::#inst>),)
}
(Some(inst), false) => {
let variant = format_ident!("{}_{}", mod_name, inst);
quote!(#[codec(index = #index)] #variant(#path::Event<#path::#inst>),)
}
(None, true) => {
quote!(#[codec(index = #index)] #mod_name(#path::Event<#runtime>),)
}
(None, false) => {
quote!(#[codec(index = #index)] #mod_name(#path::Event),)
}
}
}

fn expand_event_conversion(
scrate: &TokenStream,
path: &PalletPath,
instance: Option<&Ident>,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = path.mod_name();
let variant = if let Some(inst) = instance {
format_ident!("{}_{}", mod_name, inst)
} else {
mod_name
};

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

fn try_into(self) -> #scrate::sp_std::result::Result<#pallet_event, Self::Error> {
match self {
Self::#variant(evt) => Ok(evt),
_ => Err(()),
}
}
}
}
}

fn expand_event_metadata(
scrate: &TokenStream,
path: &PalletPath,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = path.mod_name();

quote!{(stringify!(#mod_name), #scrate::event::FnEncode(#pallet_event::metadata)),}
}

fn expand_pallet_event_fn(
scrate: &TokenStream,
path: &PalletPath,
instance: Option<&Ident>,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = path.mod_name();
let fn_name = if let Some(inst) = instance {
format_ident!("__module_events_{}_{}", mod_name, inst)
} else {
format_ident!("__module_events_{}", mod_name)
};

quote!{
#[allow(dead_code, non_snake_case)]
pub fn #fn_name() -> &'static [#scrate::event::EventMetadata] {
#pallet_event::metadata()
}
}
}
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
187 changes: 187 additions & 0 deletions frame/support/procedural/src/construct_runtime/expand/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// This file is part of Substrate.

// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

use proc_macro2::TokenStream;
use crate::construct_runtime::Pallet;
use syn::{Ident, TypePath};
use quote::{format_ident, quote};

pub fn expand_runtime_metadata(
runtime: &Ident,
pallet_declarations: &[Pallet],
scrate: &TokenStream,
extrinsic: &TypePath,
) -> TokenStream {
let modules = pallet_declarations
.iter()
.filter_map(|pallet_declaration| {
pallet_declaration.find_part("Pallet").map(|_| {
let filtered_names: Vec<_> = pallet_declaration
.pallet_parts()
.iter()
.filter(|part| part.name() != "Pallet")
.map(|part| part.name())
.collect();
(pallet_declaration, filtered_names)
})
})
.map(|(decl, filtered_names)| {
let name = &decl.name;
let index = &decl.index;
let storage = expand_pallet_metadata_storage(&filtered_names, runtime, scrate, decl);
let calls = expand_pallet_metadata_calls(&filtered_names, runtime, scrate, decl);
let event = expand_pallet_metadata_events(&filtered_names, runtime, scrate, decl);
let constants = expand_pallet_metadata_constants(runtime, scrate, decl);
let errors = expand_pallet_metadata_errors(runtime, scrate, decl);

quote!{
#scrate::metadata::ModuleMetadata {
name: #scrate::metadata::DecodeDifferent::Encode(stringify!(#name)),
index: #index,
storage: #storage,
calls: #calls,
event: #event,
constants: #constants,
errors: #errors,
}
}
})
.collect::<Vec<_>>();

quote!{
impl #runtime {
pub fn metadata() -> #scrate::metadata::RuntimeMetadataPrefixed {
#scrate::metadata::RuntimeMetadataLastVersion {
modules: #scrate::metadata::DecodeDifferent::Encode(&[ #(#modules),* ]),
extrinsic: #scrate::metadata::ExtrinsicMetadata {
version: <#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata>::VERSION,
signed_extensions: <
<
#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata
>::SignedExtensions as #scrate::sp_runtime::traits::SignedExtension
>::identifier()
.into_iter()
.map(#scrate::metadata::DecodeDifferent::Encode)
.collect(),
},
}.into()
}
}
}
}

fn expand_pallet_metadata_storage(
filtered_names: &[&'static str],
runtime: &Ident,
scrate: &TokenStream,
decl: &Pallet,
) -> TokenStream {
if filtered_names.contains(&"Storage") {
let instance = decl.instance.as_ref().into_iter();
let path = &decl.pallet;

quote!{
Some(#scrate::metadata::DecodeDifferent::Encode(
#scrate::metadata::FnEncode(
#path::Pallet::<#runtime #(, #path::#instance)*>::storage_metadata
)
))
}
} else {
quote!(None)
}
}

fn expand_pallet_metadata_calls(
filtered_names: &[&'static str],
runtime: &Ident,
scrate: &TokenStream,
decl: &Pallet,
) -> TokenStream {
if filtered_names.contains(&"Call") {
let instance = decl.instance.as_ref().into_iter();
let path = &decl.pallet;

quote!{
Some(#scrate::metadata::DecodeDifferent::Encode(
#scrate::metadata::FnEncode(
#path::Pallet::<#runtime #(, #path::#instance)*>::call_functions
)
))
}
} else {
quote!(None)
}
}

fn expand_pallet_metadata_events(
filtered_names: &[&'static str],
runtime: &Ident,
scrate: &TokenStream,
decl: &Pallet,
) -> TokenStream {
if filtered_names.contains(&"Event") {
let mod_name = decl.pallet.mod_name();
let event = if let Some(instance) = decl.instance.as_ref() {
format_ident!("__module_events_{}_{}", mod_name, instance)
} else {
format_ident!("__module_events_{}", mod_name)
};

quote!{
Some(#scrate::metadata::DecodeDifferent::Encode(
#scrate::metadata::FnEncode(#runtime::#event)
))
}
} else {
quote!(None)
}
}

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

quote!{
#scrate::metadata::DecodeDifferent::Encode(
#scrate::metadata::FnEncode(
#path::Pallet::<#runtime #(, #path::#instance)*>::module_constants_metadata
)
)
}
}

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

quote!{
#scrate::metadata::DecodeDifferent::Encode(
#scrate::metadata::FnEncode(
<#path::Pallet::<#runtime #(, #path::#instance)*> as #scrate::metadata::ModuleErrorMetadata>::metadata
)
)
}
}
24 changes: 24 additions & 0 deletions frame/support/procedural/src/construct_runtime/expand/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is part of Substrate.

// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

mod event;
mod metadata;
mod origin;

pub use event::expand_outer_event;
pub use metadata::expand_runtime_metadata;
pub use origin::expand_outer_origin;
Loading