This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Allow usage of path in construct_runtime! #8801
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
17d65f9
Allow usage of path in construct_runtime!
KiChjang 99ba3ce
Fix whitespace
KiChjang 9bc6c1a
Fix whitespace
KiChjang 87cd699
Make expand_runtime_metadata accept slice instead of Iterator
KiChjang 4d8683f
Include Call and Event in construct_runtime for testing
KiChjang 8642437
Migrate impl_outer_event to proc macro
KiChjang 4862445
Fix integrity_test_works
KiChjang fc99427
Update UI test expectations
KiChjang 0c4abde
Factor in module path while generating enum variant or fn names
KiChjang 4f6d4bf
Use ParseStream::lookahead for more helpful error messages
KiChjang 94af5e4
Remove generating outer_event_metadata
KiChjang b9724e8
Ensure pallets with different paths but same last path segment can co…
KiChjang 0ff81f1
Remove unnecessary generated function
KiChjang 5b39dd9
Migrate decl_outer_config to proc macro
KiChjang efec624
Add default_filter test for expand_outer_origin
KiChjang ba2badc
Merge remote-tracking branch 'origin/master' into kckyeung/allow-path…
KiChjang ebb5ae2
Allow crate, self and super keywords to appear in pallet path
KiChjang af7e40a
Add UI test for specifying empty pallet paths in construct_runtime
KiChjang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
frame/support/procedural/src/construct_runtime/expand/event.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
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
187
frame/support/procedural/src/construct_runtime/expand/metadata.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
frame/support/procedural/src/construct_runtime/expand/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.