Don't require module name in inherents (#6576)

* Start

* Cleanup `construct_runtime!`

* Add tests

* Fix after merge

* Update the docs
This commit is contained in:
Bastian Köcher
2020-07-06 12:29:17 +02:00
committed by GitHub
parent 2019f70768
commit ad2e832289
15 changed files with 279 additions and 161 deletions
@@ -87,7 +87,12 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
let dispatch = decl_outer_dispatch(&name, modules.iter(), &scrate);
let metadata = decl_runtime_metadata(&name, modules.iter(), &scrate, &unchecked_extrinsic);
let outer_config = decl_outer_config(&name, modules.iter(), &scrate);
let inherent = decl_outer_inherent(&block, &unchecked_extrinsic, modules.iter(), &scrate);
let inherent = decl_outer_inherent(
&block,
&unchecked_extrinsic,
modules.iter(),
&scrate,
);
let validate_unsigned = decl_validate_unsigned(&name, modules.iter(), &scrate);
let integrity_test = decl_integrity_test(&scrate);
@@ -153,19 +158,17 @@ fn decl_outer_inherent<'a>(
) -> TokenStream2 {
let modules_tokens = module_declarations.filter_map(|module_declaration| {
let maybe_config_part = module_declaration.find_part("Inherent");
maybe_config_part.map(|config_part| {
let arg = config_part
.args
.as_ref()
.and_then(|parens| parens.content.inner.iter().next())
.unwrap_or(&module_declaration.name);
maybe_config_part.map(|_| {
let name = &module_declaration.name;
quote!(#name : #arg,)
quote!(#name,)
})
});
quote!(
#scrate::impl_outer_inherent!(
impl Inherents where Block = #block, UncheckedExtrinsic = #unchecked_extrinsic {
impl Inherents where
Block = #block,
UncheckedExtrinsic = #unchecked_extrinsic
{
#(#modules_tokens)*
}
);
@@ -279,18 +279,6 @@ impl ModulePartKeyword {
Ident::new(self.name(), self.span())
}
/// Returns `true` if this module part allows to have an argument.
///
/// For example `Inherent(Timestamp)`.
fn allows_arg(&self) -> bool {
Self::all_allow_arg().iter().any(|n| *n == self.name())
}
/// Returns the names of all module parts that allow to have an argument.
fn all_allow_arg() -> &'static [&'static str] {
&["Inherent"]
}
/// Returns `true` if this module part is allowed to have generic arguments.
fn allows_generic(&self) -> bool {
Self::all_generic_arg().iter().any(|n| *n == self.name())
@@ -321,7 +309,6 @@ impl Spanned for ModulePartKeyword {
pub struct ModulePart {
pub keyword: ModulePartKeyword,
pub generics: syn::Generics,
pub args: Option<ext::Parens<ext::Punctuated<Ident, Token![,]>>>,
}
impl Parse for ModulePart {
@@ -339,27 +326,10 @@ impl Parse for ModulePart {
);
return Err(syn::Error::new(keyword.span(), msg));
}
let args = if input.peek(token::Paren) {
if !keyword.allows_arg() {
let syn::group::Parens { token: parens, .. } = syn::group::parse_parens(input)?;
let valid_names = ModulePart::format_names(ModulePartKeyword::all_allow_arg());
let msg = format!(
"`{}` is not allowed to have arguments in parens. \
Only the following modules are allowed to have arguments in parens: {}.",
keyword.name(),
valid_names,
);
return Err(syn::Error::new(parens.span, msg));
}
Some(input.parse()?)
} else {
None
};
Ok(Self {
keyword,
generics,
args,
})
}
}
@@ -277,10 +277,8 @@ pub fn decl_storage(input: TokenStream) -> TokenStream {
/// - `Event` or `Event<T>` (if the event is generic)
/// - `Origin` or `Origin<T>` (if the origin is generic)
/// - `Config` or `Config<T>` (if the config is generic)
/// - `Inherent ( $(CALL),* )` - If the module provides/can check inherents. The optional parameter
/// is for modules that use a `Call` from a different module as
/// inherent.
/// - `ValidateUnsigned` - If the module validates unsigned extrinsics.
/// - `Inherent` - If the module provides/can check inherents.
/// - `ValidateUnsigned` - If the module validates unsigned extrinsics.
///
/// # Note
///