This repository has been 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
Add pallet
attribute macro to declare pallets
#6877
Merged
Merged
Changes from 37 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
b67c866
rename system Config to system Trait.
gui1117 336d3c9
make construct_runtime handle Pallet and Module
gui1117 942c161
introduce pallet attribute macro
gui1117 1e1e8a3
allow to print some upgrade helper from decl_storage
gui1117 0d9bb3e
Improved error msg, typo.
gui1117 b48cfc6
Improved error msg, typo.
gui1117 cc165a3
Improved error message on unexpected attributes + ui test
gui1117 460ccf9
add test for transactional
gui1117 5ced0a8
various typo
gui1117 688f044
some tips when spans are lost
gui1117 c5ef21f
allow pallet to depend on other pallet instances
gui1117 240a2a7
make event type metadata consistent with call and constant
gui1117 1229c33
error messages
gui1117 30000d0
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 c2148b0
ignore doc example
gui1117 e5485c8
fix pallet upgrade template
gui1117 1ff085e
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 d5b745f
fixup
gui1117 8e8ee8d
fix doc
gui1117 3ea926c
fix indentation
gui1117 bd99a8d
Apply suggestions code formatting
gui1117 0adbb30
some renames + fix compilation
gui1117 c96e1cf
remove unsupported genesis config type alias
gui1117 04a54d6
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 78789d6
merge fixup
gui1117 96845d8
fix ui tests
gui1117 1762c47
additional doc
gui1117 a03a4b4
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 4cb71d8
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 cf7ae7d
implement StorageInstance with new syntax
gui1117 88e6995
fix line width
gui1117 d1ec690
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 3a29683
fix doc: because pallet doc goes below reexport doc
gui1117 9430b34
Merge remote-tracking branch 'origin/master' into HEAD
gui1117 888d121
Update frame/support/procedural/src/pallet/parse/event.rs
gui1117 6fa68a9
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 0c6f916
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 52a654f
Update frame/system/src/lib.rs
gui1117 03c7fbf
Update frame/support/test/tests/pallet_ui.rs
gui1117 3f23d3c
improve doc as suggested
gui1117 d9d48c7
revert construct_runtime Pallet part.
gui1117 0d56f2b
refactor with less intricated code
gui1117 d9fdc4b
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 98cdc0e
fix ui test with new image
gui1117 ae38859
fix ui tests
gui1117 c158e5f
add minor tests
gui1117 10e8a92
Merge remote-tracking branch 'origin/master' into gui-macro-attribute
gui1117 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,201 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 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::pallet::Def; | ||
use frame_support_procedural_tools::clean_type_string; | ||
use syn::spanned::Spanned; | ||
|
||
/// * Generate enum call and implement various trait on it. | ||
/// * Implement Callable and call_function on `Pallet` | ||
pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream { | ||
let frame_support = &def.frame_support; | ||
let frame_system = &def.frame_system; | ||
let type_impl_gen = &def.type_impl_generics(); | ||
let type_decl_bounded_gen = &def.type_decl_bounded_generics(); | ||
let type_use_gen = &def.type_use_generics(); | ||
let call_ident = syn::Ident::new("Call", def.call.attr_span.clone()); | ||
let pallet_ident = &def.pallet_struct.pallet; | ||
let where_clause = &def.call.where_clause; | ||
|
||
let fn_name = def.call.methods.iter().map(|method| &method.name).collect::<Vec<_>>(); | ||
|
||
let fn_weight = def.call.methods.iter().map(|method| &method.weight); | ||
|
||
let fn_doc = def.call.methods.iter().map(|method| &method.docs).collect::<Vec<_>>(); | ||
|
||
let args_name = def.call.methods.iter() | ||
.map(|method| method.args.iter().map(|(_, name, _)| name.clone()).collect::<Vec<_>>()) | ||
.collect::<Vec<_>>(); | ||
|
||
let args_type = def.call.methods.iter() | ||
.map(|method| method.args.iter().map(|(_, _, type_)| type_.clone()).collect::<Vec<_>>()) | ||
.collect::<Vec<_>>(); | ||
|
||
let args_compact_attr = def.call.methods.iter().map(|method| { | ||
method.args.iter() | ||
.map(|(is_compact, _, type_)| { | ||
if *is_compact { | ||
quote::quote_spanned!(type_.span() => #[codec(compact)] ) | ||
} else { | ||
quote::quote!() | ||
} | ||
}) | ||
.collect::<Vec<_>>() | ||
}); | ||
|
||
let args_metadata_type = def.call.methods.iter().map(|method| { | ||
method.args.iter() | ||
.map(|(is_compact, _, type_)| { | ||
let final_type = if *is_compact { | ||
quote::quote!(Compact<#type_>) | ||
} else { | ||
quote::quote!(#type_) | ||
}; | ||
clean_type_string(&final_type.to_string()) | ||
}) | ||
.collect::<Vec<_>>() | ||
}); | ||
|
||
quote::quote_spanned!(def.call.attr_span => | ||
#[derive( | ||
#frame_support::RuntimeDebugNoBound, | ||
#frame_support::CloneNoBound, | ||
#frame_support::EqNoBound, | ||
#frame_support::PartialEqNoBound, | ||
#frame_support::codec::Encode, | ||
#frame_support::codec::Decode, | ||
)] | ||
#[allow(non_camel_case_types)] | ||
pub enum #call_ident<#type_decl_bounded_gen> #where_clause { | ||
#[doc(hidden)] | ||
#[codec(skip)] | ||
__Ignore( | ||
#frame_support::sp_std::marker::PhantomData<(#type_use_gen,)>, | ||
#frame_support::Never, | ||
), | ||
#( #fn_name( #( #args_compact_attr #args_type ),* ), )* | ||
} | ||
|
||
impl<#type_impl_gen> #frame_support::dispatch::GetDispatchInfo | ||
for #call_ident<#type_use_gen> | ||
#where_clause | ||
{ | ||
fn get_dispatch_info(&self) -> #frame_support::dispatch::DispatchInfo { | ||
match *self { | ||
#( | ||
Self::#fn_name ( #( ref #args_name, )* ) => { | ||
let base_weight = #fn_weight; | ||
|
||
let weight = < | ||
dyn #frame_support::dispatch::WeighData<( #( & #args_type, )* )> | ||
>::weigh_data(&base_weight, ( #( #args_name, )* )); | ||
|
||
let class = < | ||
dyn #frame_support::dispatch::ClassifyDispatch< | ||
( #( & #args_type, )* ) | ||
> | ||
>::classify_dispatch(&base_weight, ( #( #args_name, )* )); | ||
|
||
let pays_fee = < | ||
dyn #frame_support::dispatch::PaysFee<( #( & #args_type, )* )> | ||
>::pays_fee(&base_weight, ( #( #args_name, )* )); | ||
|
||
#frame_support::dispatch::DispatchInfo { | ||
weight, | ||
class, | ||
pays_fee, | ||
} | ||
}, | ||
)* | ||
Self::__Ignore(_, _) => unreachable!("__Ignore cannot be used"), | ||
} | ||
} | ||
} | ||
|
||
impl<#type_impl_gen> #frame_support::dispatch::GetCallName for #call_ident<#type_use_gen> | ||
#where_clause | ||
{ | ||
fn get_call_name(&self) -> &'static str { | ||
match *self { | ||
#( Self::#fn_name(..) => stringify!(#fn_name), )* | ||
Self::__Ignore(_, _) => unreachable!("__PhantomItem cannot be used."), | ||
} | ||
} | ||
|
||
fn get_call_names() -> &'static [&'static str] { | ||
&[ #( stringify!(#fn_name), )* ] | ||
} | ||
} | ||
|
||
impl<#type_impl_gen> #frame_support::traits::UnfilteredDispatchable | ||
for #call_ident<#type_use_gen> | ||
#where_clause | ||
{ | ||
type Origin = #frame_system::pallet_prelude::OriginFor<T>; | ||
fn dispatch_bypass_filter( | ||
self, | ||
origin: Self::Origin | ||
gui1117 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> #frame_support::dispatch::DispatchResultWithPostInfo { | ||
match self { | ||
#( | ||
Self::#fn_name( #( #args_name, )* ) => | ||
<#pallet_ident<#type_use_gen>>::#fn_name(origin, #( #args_name, )* ) | ||
.map(Into::into).map_err(Into::into), | ||
)* | ||
Self::__Ignore(_, _) => { | ||
let _ = origin; // Use origin for empty Call enum | ||
unreachable!("__PhantomItem cannot be used."); | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl<#type_impl_gen> #frame_support::dispatch::Callable<T> for #pallet_ident<#type_use_gen> | ||
#where_clause | ||
{ | ||
type Call = #call_ident<#type_use_gen>; | ||
} | ||
|
||
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { | ||
#[doc(hidden)] | ||
pub fn call_functions() -> &'static [#frame_support::dispatch::FunctionMetadata] { | ||
&[ #( | ||
#frame_support::dispatch::FunctionMetadata { | ||
name: #frame_support::dispatch::DecodeDifferent::Encode( | ||
stringify!(#fn_name) | ||
), | ||
arguments: #frame_support::dispatch::DecodeDifferent::Encode( | ||
&[ #( | ||
#frame_support::dispatch::FunctionArgumentMetadata { | ||
name: #frame_support::dispatch::DecodeDifferent::Encode( | ||
stringify!(#args_name) | ||
), | ||
ty: #frame_support::dispatch::DecodeDifferent::Encode( | ||
#args_metadata_type | ||
), | ||
}, | ||
)* ] | ||
), | ||
documentation: #frame_support::dispatch::DecodeDifferent::Encode( | ||
&[ #( #fn_doc ),* ] | ||
), | ||
}, | ||
)* ] | ||
} | ||
} | ||
) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this was no requirement, why is it now required to have this?
Actually it was a very bad idea back in the days to introduce
Module
, as we enforce this anyway. Aka, a pallet/module without the struct doesn't work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah I see, I used this part
Module
to identify if we should useModule
orPallet
. But actually it is not its usage.if part
Module
exist then just metadata will contains some module associated metadata.I think it is indeed better not to change this. Instead I should have new macro generating a type alias to
Module
. and Have construct_runtime useModule
as before.Later when we deprecate old macros we can factorize it but a type alias here is not an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I make pallet generating a type alias
Module
and revert all changes to construct_runtime.I think it is cleaner too, refactoring of construct_runtime can be done later and differently