Emit error when construct_runtime imports a non-existent pallet part (#8949)

* Emit error when construct_runtime imports a non-existent Call part

* Reword and display pallet name when emitting part not found error

* Migrate decl_outer_dispatch to a proc macro

* Rename calls.rs to call.rs

* Create new construct_runtime_v2 macro

* Add UI test for importing non-existent call part in construct_runtime

* Emit error when construct_runtime imports a non-existent Config part

* Emit error when construct_runtime imports a non-existent Event part

* Migrate decl_outer_inherent to a proc macro

* Emit error when construct_runtime imports a non-existent Inherent part

* Migrate decl_outer_validate_unsigned to a proc macro

* Emit error when construct_runtime imports a non-existent ValidateUnsigned part

* impl for old macro

* fix line width

* add doc

* hide macroes and use unique counter everywhere

* Remove construct_runtime_v2

* Encapsulate pallet part check macros in a module

* Fix macro definitions in dummy part checker

* Tag ProvideInherent impl with #[pallet::inherent] properly for authorship pallet

* Remove Call part from pallets that do not define it

* Add Call part unit tests

* Remove undefined Call part import from offences pallet

* Add tests for expand_outer_inherent

* Remove Call part from pallets that do not define them

* Remove Call part imports from pallets that do not have it defined

* Remove Call part import of the offences pallet from grandpa pallet mocks

* Update frame/support/test/tests/pallet.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Remove Call part imports for pallets that do not define them

* Move inherent tests to inherent_expand

* Add unit tests for expand_outer_validate_unsigned

* Add newline at the end of file

* fix ui test

* Small prayer to RNGsus for fixing CI

* Remove Call part from construct_runtime for randomness collective flip pallet

* Remove Call part import for randomness collective flip pallet

* Summon Laplace's demon instead of praying to RNGsus

* Update test expectations

* fix ui test and make sure it's flaky

* Revert "fix ui test and make sure it's flaky"

This reverts commit 362b6881389c911ef8d9ef85d71c9463f5694b20.

* Comment out test instead of putting it in conditional compilation

* Update UI test expectations

* Update UI test expectations

* Emit error when construct_runtime imports a non-existent Origin part

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Denis P <denis.pisarev@parity.io>
This commit is contained in:
Keith Yeung
2021-06-15 20:44:22 -07:00
committed by GitHub
parent 7dd38e3aec
commit 58e837fcd3
47 changed files with 1934 additions and 197 deletions
@@ -0,0 +1,145 @@
// 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;
use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;
pub fn expand_outer_dispatch(
runtime: &Ident,
pallet_decls: &[Pallet],
scrate: &TokenStream,
) -> TokenStream {
let mut variant_defs = TokenStream::new();
let mut variant_patterns = Vec::new();
let mut query_call_part_macros = Vec::new();
let mut pallet_names = Vec::new();
let pallets_with_call = pallet_decls
.iter()
.filter(|decl| decl.exists_part("Call"));
for pallet_declaration in pallets_with_call {
let name = &pallet_declaration.name;
let path = &pallet_declaration.path;
let index = pallet_declaration.index;
variant_defs.extend(quote!(#[codec(index = #index)] #name( #scrate::dispatch::CallableCallFor<#name, #runtime> ),));
variant_patterns.push(quote!(Call::#name(call)));
pallet_names.push(name);
query_call_part_macros.push(quote! {
#path::__substrate_call_check::is_call_part_defined!(#name);
});
}
quote! {
#( #query_call_part_macros )*
#[derive(
Clone, PartialEq, Eq,
#scrate::codec::Encode,
#scrate::codec::Decode,
#scrate::RuntimeDebug,
)]
pub enum Call {
#variant_defs
}
impl #scrate::dispatch::GetDispatchInfo for Call {
fn get_dispatch_info(&self) -> #scrate::dispatch::DispatchInfo {
match self {
#( #variant_patterns => call.get_dispatch_info(), )*
}
}
}
impl #scrate::dispatch::GetCallMetadata for Call {
fn get_call_metadata(&self) -> #scrate::dispatch::CallMetadata {
use #scrate::dispatch::GetCallName;
match self {
#(
#variant_patterns => {
let function_name = call.get_call_name();
let pallet_name = stringify!(#pallet_names);
#scrate::dispatch::CallMetadata { function_name, pallet_name }
}
)*
}
}
fn get_module_names() -> &'static [&'static str] {
&[#(
stringify!(#pallet_names),
)*]
}
fn get_call_names(module: &str) -> &'static [&'static str] {
use #scrate::dispatch::{Callable, GetCallName};
match module {
#(
stringify!(#pallet_names) =>
<<#pallet_names as Callable<#runtime>>::Call
as GetCallName>::get_call_names(),
)*
_ => unreachable!(),
}
}
}
impl #scrate::dispatch::Dispatchable for Call {
type Origin = Origin;
type Config = Call;
type Info = #scrate::weights::DispatchInfo;
type PostInfo = #scrate::weights::PostDispatchInfo;
fn dispatch(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
if !<Self::Origin as #scrate::traits::OriginTrait>::filter_call(&origin, &self) {
return #scrate::sp_std::result::Result::Err(#scrate::dispatch::DispatchError::BadOrigin.into());
}
#scrate::traits::UnfilteredDispatchable::dispatch_bypass_filter(self, origin)
}
}
impl #scrate::traits::UnfilteredDispatchable for Call {
type Origin = Origin;
fn dispatch_bypass_filter(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
match self {
#(
#variant_patterns =>
#scrate::traits::UnfilteredDispatchable::dispatch_bypass_filter(call, origin),
)*
}
}
}
#(
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>> {
match self {
#variant_patterns => Some(call),
// May be unreachable
_ => None,
}
}
}
impl From<#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> for Call {
fn from(call: #scrate::dispatch::CallableCallFor<#pallet_names, #runtime>) -> Self {
#variant_patterns
}
}
)*
}
}
@@ -29,21 +29,31 @@ pub fn expand_outer_config(
let mut types = TokenStream::new();
let mut fields = TokenStream::new();
let mut build_storage_calls = TokenStream::new();
let mut query_genesis_config_part_macros = Vec::new();
for decl in pallet_decls {
if let Some(pallet_entry) = decl.find_part("Config") {
let config = format_ident!("{}Config", decl.name);
let pallet_name = &decl.name.to_string().to_snake_case();
let field_name = &Ident::new(pallet_name, decl.name.span());
let path = &decl.path;
let pallet_name = &decl.name;
let config = format_ident!("{}Config", pallet_name);
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();
types.extend(expand_config_types(runtime, decl, &config, part_is_generic));
fields.extend(quote!(pub #field_name: #config,));
build_storage_calls.extend(expand_config_build_storage_call(scrate, runtime, decl, &field_name));
query_genesis_config_part_macros.push(quote! {
#path::__substrate_genesis_config_check::is_genesis_config_defined!(#pallet_name);
});
}
}
quote!{
quote! {
#( #query_genesis_config_part_macros )*
#types
#[cfg(any(feature = "std", test))]
@@ -27,10 +27,12 @@ pub fn expand_outer_event(
) -> syn::Result<TokenStream> {
let mut event_variants = TokenStream::new();
let mut event_conversions = TokenStream::new();
let mut query_event_part_macros = Vec::new();
for pallet_decl in pallet_decls {
if let Some(pallet_entry) = pallet_decl.find_part("Event") {
let path = &pallet_decl.path;
let pallet_name = &pallet_decl.name;
let index = pallet_decl.index;
let instance = pallet_decl.instance.as_ref();
let generics = &pallet_entry.generics;
@@ -39,9 +41,9 @@ pub fn expand_outer_event(
let msg = format!(
"Instantiable pallet with no generic `Event` cannot \
be constructed: pallet `{}` must have generic `Event`",
pallet_decl.name,
pallet_name,
);
return Err(syn::Error::new(pallet_decl.name.span(), msg));
return Err(syn::Error::new(pallet_name.span(), msg));
}
let part_is_generic = !generics.params.is_empty();
@@ -54,10 +56,15 @@ pub fn expand_outer_event(
event_variants.extend(expand_event_variant(runtime, pallet_decl, index, instance, generics));
event_conversions.extend(expand_event_conversion(scrate, pallet_decl, &pallet_event));
query_event_part_macros.push(quote! {
#path::__substrate_event_check::is_event_part_defined!(#pallet_name);
});
}
}
Ok(quote!{
Ok(quote! {
#( #query_event_part_macros )*
#[derive(
Clone, PartialEq, Eq,
#scrate::codec::Encode,
@@ -0,0 +1,204 @@
// 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;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Ident, TypePath};
pub fn expand_outer_inherent(
runtime: &Ident,
block: &TypePath,
unchecked_extrinsic: &TypePath,
pallet_decls: &[Pallet],
scrate: &TokenStream,
) -> TokenStream {
let mut pallet_names = Vec::new();
let mut query_inherent_part_macros = Vec::new();
for pallet_decl in pallet_decls {
if pallet_decl.exists_part("Inherent") {
let name = &pallet_decl.name;
let path = &pallet_decl.path;
pallet_names.push(name);
query_inherent_part_macros.push(quote! {
#path::__substrate_inherent_check::is_inherent_part_defined!(#name);
});
}
}
quote! {
#( #query_inherent_part_macros )*
trait InherentDataExt {
fn create_extrinsics(&self) ->
#scrate::inherent::Vec<<#block as #scrate::inherent::BlockT>::Extrinsic>;
fn check_extrinsics(&self, block: &#block) -> #scrate::inherent::CheckInherentsResult;
}
impl InherentDataExt for #scrate::inherent::InherentData {
fn create_extrinsics(&self) ->
#scrate::inherent::Vec<<#block as #scrate::inherent::BlockT>::Extrinsic>
{
use #scrate::inherent::ProvideInherent;
let mut inherents = Vec::new();
#(
if let Some(inherent) = #pallet_names::create_inherent(self) {
let inherent = <#unchecked_extrinsic as #scrate::inherent::Extrinsic>::new(
inherent.into(),
None,
).expect("Runtime UncheckedExtrinsic is not Opaque, so it has to return \
`Some`; qed");
inherents.push(inherent);
}
)*
inherents
}
fn check_extrinsics(&self, block: &#block) -> #scrate::inherent::CheckInherentsResult {
use #scrate::inherent::{ProvideInherent, IsFatalError};
use #scrate::traits::{IsSubType, ExtrinsicCall};
use #scrate::sp_runtime::traits::Block as _;
let mut result = #scrate::inherent::CheckInherentsResult::new();
for xt in block.extrinsics() {
// Inherents are before any other extrinsics.
// And signed extrinsics are not inherents.
if #scrate::inherent::Extrinsic::is_signed(xt).unwrap_or(false) {
break
}
let mut is_inherent = false;
#({
let call = <#unchecked_extrinsic as ExtrinsicCall>::call(xt);
if let Some(call) = IsSubType::<_>::is_sub_type(call) {
if #pallet_names::is_inherent(call) {
is_inherent = true;
if let Err(e) = #pallet_names::check_inherent(call, self) {
result.put_error(
#pallet_names::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
if e.is_fatal_error() {
return result;
}
}
}
}
})*
// Inherents are before any other extrinsics.
// No module marked it as inherent thus it is not.
if !is_inherent {
break
}
}
#(
match #pallet_names::is_inherent_required(self) {
Ok(Some(e)) => {
let found = block.extrinsics().iter().any(|xt| {
let is_signed = #scrate::inherent::Extrinsic::is_signed(xt)
.unwrap_or(false);
if !is_signed {
let call = <
#unchecked_extrinsic as ExtrinsicCall
>::call(xt);
if let Some(call) = IsSubType::<_>::is_sub_type(call) {
#pallet_names::is_inherent(&call)
} else {
false
}
} else {
// Signed extrinsics are not inherents.
false
}
});
if !found {
result.put_error(
#pallet_names::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
if e.is_fatal_error() {
return result;
}
}
},
Ok(None) => (),
Err(e) => {
result.put_error(
#pallet_names::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
if e.is_fatal_error() {
return result;
}
},
}
)*
result
}
}
impl #scrate::traits::EnsureInherentsAreFirst<#block> for #runtime {
fn ensure_inherents_are_first(block: &#block) -> Result<(), u32> {
use #scrate::inherent::ProvideInherent;
use #scrate::traits::{IsSubType, ExtrinsicCall};
use #scrate::sp_runtime::traits::Block as _;
let mut first_signed_observed = false;
for (i, xt) in block.extrinsics().iter().enumerate() {
let is_signed = #scrate::inherent::Extrinsic::is_signed(xt).unwrap_or(false);
let is_inherent = if is_signed {
// Signed extrinsics are not inherents.
false
} else {
let mut is_inherent = false;
#({
let call = <#unchecked_extrinsic as ExtrinsicCall>::call(xt);
if let Some(call) = IsSubType::<_>::is_sub_type(call) {
if #pallet_names::is_inherent(&call) {
is_inherent = true;
}
}
})*
is_inherent
};
if !is_inherent {
first_signed_observed = true;
}
if first_signed_observed && is_inherent {
return Err(i as u32)
}
}
Ok(())
}
}
}
}
@@ -15,12 +15,18 @@
// See the License for the specific language governing permissions and
// limitations under the License
mod call;
mod config;
mod event;
mod inherent;
mod metadata;
mod origin;
mod unsigned;
pub use call::expand_outer_dispatch;
pub use config::expand_outer_config;
pub use event::expand_outer_event;
pub use inherent::expand_outer_inherent;
pub use metadata::expand_runtime_metadata;
pub use origin::expand_outer_origin;
pub use unsigned::expand_outer_validate_unsigned;
@@ -36,20 +36,23 @@ pub fn expand_outer_origin(
let mut caller_variants = TokenStream::new();
let mut pallet_conversions = TokenStream::new();
let mut query_origin_part_macros = Vec::new();
for pallet_decl in pallets.iter().filter(|pallet| pallet.name != SYSTEM_PALLET_NAME) {
if let Some(pallet_entry) = pallet_decl.find_part("Origin") {
let instance = pallet_decl.instance.as_ref();
let index = pallet_decl.index;
let generics = &pallet_entry.generics;
let name = &pallet_decl.name;
let path = &pallet_decl.path;
if instance.is_some() && generics.params.is_empty() {
let msg = format!(
"Instantiable pallet with no generic `Origin` cannot \
be constructed: pallet `{}` must have generic `Origin`",
pallet_decl.name
name
);
return Err(syn::Error::new(pallet_decl.name.span(), msg));
return Err(syn::Error::new(name.span(), msg));
}
caller_variants.extend(
@@ -58,13 +61,18 @@ pub fn expand_outer_origin(
pallet_conversions.extend(
expand_origin_pallet_conversions(scrate, runtime, pallet_decl, instance, generics),
);
query_origin_part_macros.push(quote! {
#path::__substrate_origin_check::is_origin_part_defined!(#name);
});
}
}
let system_path = &system_pallet.path;
let system_index = system_pallet.index;
Ok(quote!{
Ok(quote! {
#( #query_origin_part_macros )*
// WARNING: All instance must hold the filter `frame_system::Config::BaseCallFilter`, except
// when caller is system Root. One can use `OriginTrait::reset_filter` to do so.
#[derive(Clone)]
@@ -0,0 +1,72 @@
// 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;
use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;
pub fn expand_outer_validate_unsigned(
runtime: &Ident,
pallet_decls: &[Pallet],
scrate: &TokenStream,
) -> TokenStream {
let mut pallet_names = Vec::new();
let mut query_validate_unsigned_part_macros = Vec::new();
for pallet_decl in pallet_decls {
if pallet_decl.exists_part("ValidateUnsigned") {
let name = &pallet_decl.name;
let path = &pallet_decl.path;
pallet_names.push(name);
query_validate_unsigned_part_macros.push(quote! {
#path::__substrate_validate_unsigned_check::is_validate_unsigned_part_defined!(#name);
});
}
}
quote! {
#( #query_validate_unsigned_part_macros )*
impl #scrate::unsigned::ValidateUnsigned for #runtime {
type Call = Call;
fn pre_dispatch(call: &Self::Call) -> Result<(), #scrate::unsigned::TransactionValidityError> {
#[allow(unreachable_patterns)]
match call {
#( Call::#pallet_names(inner_call) => #pallet_names::pre_dispatch(inner_call), )*
// pre-dispatch should not stop inherent extrinsics, validation should prevent
// including arbitrary (non-inherent) extrinsics to blocks.
_ => Ok(()),
}
}
fn validate_unsigned(
#[allow(unused_variables)]
source: #scrate::unsigned::TransactionSource,
call: &Self::Call,
) -> #scrate::unsigned::TransactionValidity {
#[allow(unreachable_patterns)]
match call {
#( Call::#pallet_names(inner_call) => #pallet_names::validate_unsigned(source, inner_call), )*
_ => #scrate::unsigned::UnknownTransaction::NoUnsignedValidator.into(),
}
}
}
}
}
@@ -145,17 +145,17 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
let all_pallets = decl_all_pallets(&name, pallets.iter());
let pallet_to_index = decl_pallet_runtime_setup(&pallets, &scrate);
let dispatch = decl_outer_dispatch(&name, pallets.iter(), &scrate);
let dispatch = expand::expand_outer_dispatch(&name, &pallets, &scrate);
let metadata = expand::expand_runtime_metadata(&name, &pallets, &scrate, &unchecked_extrinsic);
let outer_config = expand::expand_outer_config(&name, &pallets, &scrate);
let inherent = decl_outer_inherent(
let inherent = expand::expand_outer_inherent(
&name,
&block,
&unchecked_extrinsic,
pallets.iter(),
&pallets,
&scrate,
);
let validate_unsigned = decl_validate_unsigned(&name, pallets.iter(), &scrate);
let validate_unsigned = expand::expand_outer_validate_unsigned(&name, &pallets, &scrate);
let integrity_test = decl_integrity_test(&scrate);
let res = quote!(
@@ -200,73 +200,6 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
Ok(res)
}
fn decl_validate_unsigned<'a>(
runtime: &'a Ident,
pallet_declarations: impl Iterator<Item = &'a Pallet>,
scrate: &'a TokenStream2,
) -> TokenStream2 {
let pallets_tokens = pallet_declarations
.filter(|pallet_declaration| pallet_declaration.exists_part("ValidateUnsigned"))
.map(|pallet_declaration| &pallet_declaration.name);
quote!(
#scrate::impl_outer_validate_unsigned!(
impl ValidateUnsigned for #runtime {
#( #pallets_tokens )*
}
);
)
}
fn decl_outer_inherent<'a>(
runtime: &'a Ident,
block: &'a syn::TypePath,
unchecked_extrinsic: &'a syn::TypePath,
pallet_declarations: impl Iterator<Item = &'a Pallet>,
scrate: &'a TokenStream2,
) -> TokenStream2 {
let pallets_tokens = pallet_declarations.filter_map(|pallet_declaration| {
let maybe_config_part = pallet_declaration.find_part("Inherent");
maybe_config_part.map(|_| {
let name = &pallet_declaration.name;
quote!(#name,)
})
});
quote!(
#scrate::impl_outer_inherent!(
impl Inherents where
Block = #block,
UncheckedExtrinsic = #unchecked_extrinsic,
Runtime = #runtime,
{
#(#pallets_tokens)*
}
);
)
}
fn decl_outer_dispatch<'a>(
runtime: &'a Ident,
pallet_declarations: impl Iterator<Item = &'a Pallet>,
scrate: &'a TokenStream2,
) -> TokenStream2 {
let pallets_tokens = pallet_declarations
.filter(|pallet_declaration| pallet_declaration.exists_part("Call"))
.map(|pallet_declaration| {
let pallet = &pallet_declaration.path.inner.segments.last().unwrap();
let name = &pallet_declaration.name;
let index = pallet_declaration.index;
quote!(#[codec(index = #index)] #pallet::#name)
});
quote!(
#scrate::impl_outer_dispatch! {
pub enum Call for #runtime where origin: Origin {
#(#pallets_tokens,)*
}
}
)
}
fn decl_all_pallets<'a>(
runtime: &'a Ident,
pallet_declarations: impl Iterator<Item = &'a Pallet>,
@@ -0,0 +1,104 @@
use proc_macro::TokenStream;
use crate::COUNTER;
pub fn generate_dummy_part_checker(input: TokenStream) -> TokenStream {
if !input.is_empty() {
return syn::Error::new(proc_macro2::Span::call_site(), "No arguments expected")
.to_compile_error().into()
}
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let genesis_config_macro_ident = syn::Ident::new(
&format!("__is_genesis_config_defined_{}", count),
proc_macro2::Span::call_site(),
);
let event_macro_ident = syn::Ident::new(
&format!("__is_event_part_defined_{}", count),
proc_macro2::Span::call_site(),
);
let inherent_macro_ident = syn::Ident::new(
&format!("__is_inherent_part_defined_{}", count),
proc_macro2::Span::call_site(),
);
let validate_unsigned_macro_ident = syn::Ident::new(
&format!("__is_validate_unsigned_part_defined_{}", count),
proc_macro2::Span::call_site(),
);
let call_macro_ident = syn::Ident::new(
&format!("__is_call_part_defined_{}", count),
proc_macro2::Span::call_site(),
);
let origin_macro_ident = syn::Ident::new(
&format!("__is_origin_part_defined_{}", count),
proc_macro2::Span::call_site(),
);
quote::quote!(
#[doc(hidden)]
pub mod __substrate_genesis_config_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #genesis_config_macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #genesis_config_macro_ident as is_genesis_config_defined;
}
#[doc(hidden)]
pub mod __substrate_event_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #event_macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #event_macro_ident as is_event_part_defined;
}
#[doc(hidden)]
pub mod __substrate_inherent_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #inherent_macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #inherent_macro_ident as is_inherent_part_defined;
}
#[doc(hidden)]
pub mod __substrate_validate_unsigned_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #validate_unsigned_macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #validate_unsigned_macro_ident as is_validate_unsigned_part_defined;
}
#[doc(hidden)]
pub mod __substrate_call_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #call_macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #call_macro_ident as is_call_part_defined;
}
#[doc(hidden)]
pub mod __substrate_origin_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #origin_macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #origin_macro_ident as is_origin_part_defined;
}
).into()
}
@@ -29,9 +29,29 @@ mod clone_no_bound;
mod partial_eq_no_bound;
mod default_no_bound;
mod key_prefix;
mod dummy_part_checker;
pub(crate) use storage::INHERENT_INSTANCE_NAME;
use proc_macro::TokenStream;
use std::cell::RefCell;
thread_local! {
/// A global counter, can be used to generate a relatively unique identifier.
static COUNTER: RefCell<Counter> = RefCell::new(Counter(0));
}
/// Counter to generate a relatively unique identifier for macros querying for the existence of
/// pallet parts. This is necessary because declarative macros gets hoisted to the crate root,
/// which shares the namespace with other pallets containing the very same query macros.
struct Counter(u64);
impl Counter {
fn inc(&mut self) -> u64 {
let ret = self.0;
self.0 += 1;
ret
}
}
/// Declares strongly-typed wrappers around codec-compatible types in storage.
///
@@ -453,3 +473,9 @@ pub(crate) const NUMBER_OF_INSTANCE: u8 = 16;
pub fn impl_key_prefix_for_tuples(input: TokenStream) -> TokenStream {
key_prefix::impl_key_prefix_for_tuples(input).unwrap_or_else(syn::Error::into_compile_error).into()
}
/// Internal macro use by frame_support to generate dummy part checker for old pallet declaration
#[proc_macro]
pub fn __generate_dummy_part_checker(input: TokenStream) -> TokenStream {
dummy_part_checker::generate_dummy_part_checker(input)
}
@@ -17,6 +17,7 @@
use crate::pallet::Def;
use frame_support_procedural_tools::clean_type_string;
use crate::COUNTER;
use syn::spanned::Spanned;
/// * Generate enum call and implement various trait on it.
@@ -31,7 +32,7 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
(span, where_clause, methods, docs)
}
None => (def.pallet_struct.attr_span, None, Vec::new(), Vec::new()),
None => (def.item.span(), None, Vec::new(), Vec::new()),
};
let frame_support = &def.frame_support;
let frame_system = &def.frame_system;
@@ -89,7 +90,37 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
&docs[..]
};
let maybe_compile_error = if def.call.is_none() {
quote::quote!{
compile_error!(concat!(
"`",
stringify!($pallet_name),
"` does not have #[pallet::call] defined, perhaps you should remove `Call` from \
construct_runtime?",
));
}
} else {
proc_macro2::TokenStream::new()
};
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let macro_ident = syn::Ident::new(&format!("__is_call_part_defined_{}", count), span);
quote::quote_spanned!(span =>
#[doc(hidden)]
pub mod __substrate_call_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {
#maybe_compile_error
};
}
#[doc(hidden)]
pub use #macro_ident as is_call_part_defined;
}
#( #[doc = #docs] )*
#[derive(
#frame_support::RuntimeDebugNoBound,
@@ -16,15 +16,44 @@
// limitations under the License.
use crate::pallet::{Def, parse::helper::get_doc_literals};
use crate::COUNTER;
use syn::{spanned::Spanned, Ident};
/// * Add __Ignore variant on Event
/// * Impl various trait on Event including metadata
/// * if deposit_event is defined, implement deposit_event on module.
pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
let event = if let Some(event) = &def.event {
event
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let (event, macro_ident) = if let Some(event) = &def.event {
let ident = Ident::new(&format!("__is_event_part_defined_{}", count), event.attr_span);
(event, ident)
} else {
return Default::default()
let macro_ident = Ident::new(
&format!("__is_event_part_defined_{}", count),
def.item.span(),
);
return quote::quote! {
#[doc(hidden)]
pub mod __substrate_event_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {
compile_error!(concat!(
"`",
stringify!($pallet_name),
"` does not have #[pallet::event] defined, perhaps you should \
remove `Event` from construct_runtime?",
));
}
}
#[doc(hidden)]
pub use #macro_ident as is_event_part_defined;
}
};
};
let event_where_clause = &event.where_clause;
@@ -130,6 +159,18 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
};
quote::quote_spanned!(event.attr_span =>
#[doc(hidden)]
pub mod __substrate_event_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #macro_ident as is_event_part_defined;
}
#deposit_event
impl<#event_impl_gen> From<#event_ident<#event_use_gen>> for () #event_where_clause {
@@ -16,13 +16,45 @@
// limitations under the License.
use crate::pallet::{Def, parse::helper::get_doc_literals};
use crate::COUNTER;
use syn::{Ident, spanned::Spanned};
/// * add various derive trait on GenesisConfig struct.
pub fn expand_genesis_config(def: &mut Def) -> proc_macro2::TokenStream {
let genesis_config = if let Some(genesis_config) = &def.genesis_config {
genesis_config
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let (genesis_config, macro_ident) = if let Some(genesis_config) = &def.genesis_config {
let ident = Ident::new(
&format!("__is_genesis_config_defined_{}", count),
genesis_config.genesis_config.span(),
);
(genesis_config, ident)
} else {
return Default::default()
let macro_ident = Ident::new(
&format!("__is_genesis_config_defined_{}", count),
def.item.span(),
);
return quote::quote! {
#[doc(hidden)]
pub mod __substrate_genesis_config_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {
compile_error!(concat!(
"`",
stringify!($pallet_name),
"` does not have #[pallet::genesis_config] defined, perhaps you should \
remove `Config` from construct_runtime?",
));
}
}
#[doc(hidden)]
pub use #macro_ident as is_genesis_config_defined;
}
};
};
let frame_support = &def.frame_support;
@@ -57,5 +89,17 @@ pub fn expand_genesis_config(def: &mut Def) -> proc_macro2::TokenStream {
_ => unreachable!("Checked by genesis_config parser"),
}
Default::default()
quote::quote! {
#[doc(hidden)]
pub mod __substrate_genesis_config_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {};
}
#[doc(hidden)]
pub use #macro_ident as is_genesis_config_defined;
}
}
}
@@ -0,0 +1,56 @@
// 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::pallet::Def;
use proc_macro2::TokenStream;
use quote::quote;
use crate::COUNTER;
use syn::{Ident, spanned::Spanned};
pub fn expand_inherents(def: &mut Def) -> TokenStream {
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let macro_ident = Ident::new(&format!("__is_inherent_part_defined_{}", count), def.item.span());
let maybe_compile_error = if def.inherent.is_none() {
quote! {
compile_error!(concat!(
"`",
stringify!($pallet_name),
"` does not have #[pallet::inherent] defined, perhaps you should \
remove `Inherent` from construct_runtime?",
));
}
} else {
TokenStream::new()
};
quote! {
#[doc(hidden)]
pub mod __substrate_inherent_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {
#maybe_compile_error
}
}
#[doc(hidden)]
pub use #macro_ident as is_inherent_part_defined;
}
}
}
@@ -24,10 +24,13 @@ mod event;
mod storage;
mod hooks;
mod store_trait;
mod inherent;
mod instances;
mod genesis_build;
mod genesis_config;
mod type_value;
mod origin;
mod validate_unsigned;
use crate::pallet::{Def, parse::helper::get_doc_literals};
use quote::ToTokens;
@@ -54,12 +57,15 @@ pub fn expand(mut def: Def) -> proc_macro2::TokenStream {
let error = error::expand_error(&mut def);
let event = event::expand_event(&mut def);
let storages = storage::expand_storages(&mut def);
let inherents = inherent::expand_inherents(&mut def);
let instances = instances::expand_instances(&mut def);
let store_trait = store_trait::expand_store_trait(&mut def);
let hooks = hooks::expand_hooks(&mut def);
let genesis_build = genesis_build::expand_genesis_build(&mut def);
let genesis_config = genesis_config::expand_genesis_config(&mut def);
let type_values = type_value::expand_type_values(&mut def);
let origins = origin::expand_origins(&mut def);
let validate_unsigned = validate_unsigned::expand_validate_unsigned(&mut def);
if get_doc_literals(&def.item.attrs).is_empty() {
def.item.attrs.push(syn::parse_quote!(
@@ -80,12 +86,15 @@ pub fn expand(mut def: Def) -> proc_macro2::TokenStream {
#error
#event
#storages
#inherents
#instances
#store_trait
#hooks
#genesis_build
#genesis_config
#type_values
#origins
#validate_unsigned
);
def.item.content.as_mut().expect("This is checked by parsing").1
@@ -0,0 +1,55 @@
// This file is part of Substrate.
// Copyright (C) 2020-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::{pallet::Def, COUNTER};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Ident, spanned::Spanned};
pub fn expand_origins(def: &mut Def) -> TokenStream {
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let macro_ident = Ident::new(&format!("__is_origin_part_defined_{}", count), def.item.span());
let maybe_compile_error = if def.origin.is_none() {
quote! {
compile_error!(concat!(
"`",
stringify!($pallet_name),
"` does not have #[pallet::origin] defined, perhaps you should \
remove `Origin` from construct_runtime?",
));
}
} else {
TokenStream::new()
};
quote! {
#[doc(hidden)]
pub mod __substrate_origin_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {
#maybe_compile_error
}
}
#[doc(hidden)]
pub use #macro_ident as is_origin_part_defined;
}
}
}
@@ -0,0 +1,56 @@
// This file is part of Substrate.
// Copyright (C) 2020-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::pallet::Def;
use proc_macro2::TokenStream;
use quote::quote;
use crate::COUNTER;
use syn::{Ident, spanned::Spanned};
pub fn expand_validate_unsigned(def: &mut Def) -> TokenStream {
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
let macro_ident = Ident::new(&format!("__is_validate_unsigned_part_defined_{}", count), def.item.span());
let maybe_compile_error = if def.validate_unsigned.is_none() {
quote! {
compile_error!(concat!(
"`",
stringify!($pallet_name),
"` does not have #[pallet::validate_unsigned] defined, perhaps you should \
remove `ValidateUnsigned` from construct_runtime?",
));
}
} else {
TokenStream::new()
};
quote! {
#[doc(hidden)]
pub mod __substrate_validate_unsigned_check {
#[macro_export]
#[doc(hidden)]
macro_rules! #macro_ident {
($pallet_name:ident) => {
#maybe_compile_error
}
}
#[doc(hidden)]
pub use #macro_ident as is_validate_unsigned_part_defined;
}
}
}