Skip to content
Snippets Groups Projects
Commit 4a9697db authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Check for invalid modules when registering a pallet in construct_runtime (#4520)

parent 508f94ac
No related merge requests found
......@@ -251,7 +251,7 @@ construct_runtime!(
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Indices: indices,
Balances: balances::{default, Error},
Balances: balances,
TransactionPayment: transaction_payment::{Module, Storage},
Sudo: sudo,
// Used for the module template in `./template.rs`
......
......@@ -527,14 +527,14 @@ construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic
{
System: frame_system::{Module, Call, Storage, Config, Event},
Utility: pallet_utility::{Module, Call, Storage, Event<T>, Error},
Utility: pallet_utility::{Module, Call, Storage, Event<T>},
Babe: pallet_babe::{Module, Call, Storage, Config, Inherent(Timestamp)},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
Authorship: pallet_authorship::{Module, Call, Storage, Inherent},
Indices: pallet_indices,
Balances: pallet_balances::{default, Error},
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment::{Module, Storage},
Staking: pallet_staking::{default, OfflineWorker},
Staking: pallet_staking,
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
Democracy: pallet_democracy::{Module, Call, Storage, Config, Event<T>},
Council: pallet_collective::<Instance1>::{Module, Call, Storage, Origin<T>, Event<T>, Config<T>},
......
......@@ -154,7 +154,7 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Module, Call, Event},
Balances: pallet_balances::{Module, Call, Event<T>, Config<T>, Error},
Balances: pallet_balances::{Module, Call, Event<T>, Config<T>},
Elections: elections::{Module, Call, Event<T>, Config<T>},
}
);
......
......@@ -9,7 +9,6 @@ proc-macro = true
[dependencies]
frame-support-procedural-tools = { version = "2.0.0", path = "./tools" }
proc-macro2 = "1.0.6"
quote = "1.0.2"
syn = { version = "1.0.7", features = ["full"] }
......@@ -180,8 +180,8 @@ impl Parse for ModuleDeclaration {
let has_default = parts.into_iter().any(|m| m.is_default());
for entry in parts {
match entry {
ModuleEntry::Part(part) if has_default => {
if part.is_included_in_default() {
ModuleEntry::Part(part) => {
if has_default && part.is_included_in_default() {
let msg = format!(
"`{}` is already included in `default`. Either remove `default` or remove `{}`",
part.name,
......@@ -189,8 +189,7 @@ impl Parse for ModuleDeclaration {
);
return Err(Error::new(part.name.span(), msg));
}
}
ModuleEntry::Part(part) => {
if !resolved.insert(part.name.clone()) {
let msg = format!(
"`{}` was already declared before. Please remove the duplicate declaration",
......@@ -287,7 +286,18 @@ pub struct ModulePart {
impl Parse for ModulePart {
fn parse(input: ParseStream) -> Result<Self> {
let name = input.parse()?;
let name: Ident = input.parse()?;
if !ModulePart::all_allowed().iter().any(|n| name == n) {
return Err(syn::Error::new(
name.span(),
format!(
"Only the following modules are allowed: {}",
ModulePart::format_names(ModulePart::all_allowed()),
),
))
}
let generics: syn::Generics = input.parse()?;
if !generics.params.is_empty() && !Self::is_allowed_generic(&name) {
let valid_generics = ModulePart::format_names(ModulePart::allowed_generics());
......@@ -313,6 +323,7 @@ impl Parse for ModulePart {
} else {
None
};
Ok(Self {
name,
generics,
......@@ -330,15 +341,20 @@ impl ModulePart {
Self::allowed_args().into_iter().any(|n| ident == n)
}
pub fn allowed_generics() -> Vec<&'static str> {
vec!["Event", "Origin", "Config"]
pub fn allowed_generics() -> &'static [&'static str] {
&["Event", "Origin", "Config"]
}
pub fn allowed_args() -> &'static [&'static str] {
&["Inherent"]
}
pub fn allowed_args() -> Vec<&'static str> {
vec!["Inherent"]
/// Returns all allowed names for module parts.
pub fn all_allowed() -> &'static [&'static str] {
&["Module", "Call", "Storage", "Event", "Config", "Origin", "Inherent", "ValidateUnsigned"]
}
pub fn format_names(names: Vec<&'static str>) -> String {
pub fn format_names(names: &[&'static str]) -> String {
let res: Vec<_> = names.into_iter().map(|s| format!("`{}`", s)).collect();
res.join(", ")
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment