mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 05:11:09 +00:00
Check for invalid modules when registering a pallet in construct_runtime (#4520)
This commit is contained in:
@@ -251,7 +251,7 @@ construct_runtime!(
|
|||||||
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
|
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
|
||||||
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
|
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
|
||||||
Indices: indices,
|
Indices: indices,
|
||||||
Balances: balances::{default, Error},
|
Balances: balances,
|
||||||
TransactionPayment: transaction_payment::{Module, Storage},
|
TransactionPayment: transaction_payment::{Module, Storage},
|
||||||
Sudo: sudo,
|
Sudo: sudo,
|
||||||
// Used for the module template in `./template.rs`
|
// Used for the module template in `./template.rs`
|
||||||
|
|||||||
@@ -527,14 +527,14 @@ construct_runtime!(
|
|||||||
UncheckedExtrinsic = UncheckedExtrinsic
|
UncheckedExtrinsic = UncheckedExtrinsic
|
||||||
{
|
{
|
||||||
System: frame_system::{Module, Call, Storage, Config, Event},
|
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)},
|
Babe: pallet_babe::{Module, Call, Storage, Config, Inherent(Timestamp)},
|
||||||
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
|
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
|
||||||
Authorship: pallet_authorship::{Module, Call, Storage, Inherent},
|
Authorship: pallet_authorship::{Module, Call, Storage, Inherent},
|
||||||
Indices: pallet_indices,
|
Indices: pallet_indices,
|
||||||
Balances: pallet_balances::{default, Error},
|
Balances: pallet_balances,
|
||||||
TransactionPayment: pallet_transaction_payment::{Module, Storage},
|
TransactionPayment: pallet_transaction_payment::{Module, Storage},
|
||||||
Staking: pallet_staking::{default, OfflineWorker},
|
Staking: pallet_staking,
|
||||||
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
|
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
|
||||||
Democracy: pallet_democracy::{Module, Call, Storage, Config, Event<T>},
|
Democracy: pallet_democracy::{Module, Call, Storage, Config, Event<T>},
|
||||||
Council: pallet_collective::<Instance1>::{Module, Call, Storage, Origin<T>, Event<T>, Config<T>},
|
Council: pallet_collective::<Instance1>::{Module, Call, Storage, Origin<T>, Event<T>, Config<T>},
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ frame_support::construct_runtime!(
|
|||||||
UncheckedExtrinsic = UncheckedExtrinsic
|
UncheckedExtrinsic = UncheckedExtrinsic
|
||||||
{
|
{
|
||||||
System: system::{Module, Call, Event},
|
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>},
|
Elections: elections::{Module, Call, Event<T>, Config<T>},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ proc-macro = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
frame-support-procedural-tools = { version = "2.0.0", path = "./tools" }
|
frame-support-procedural-tools = { version = "2.0.0", path = "./tools" }
|
||||||
|
|
||||||
proc-macro2 = "1.0.6"
|
proc-macro2 = "1.0.6"
|
||||||
quote = "1.0.2"
|
quote = "1.0.2"
|
||||||
syn = { version = "1.0.7", features = ["full"] }
|
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());
|
let has_default = parts.into_iter().any(|m| m.is_default());
|
||||||
for entry in parts {
|
for entry in parts {
|
||||||
match entry {
|
match entry {
|
||||||
ModuleEntry::Part(part) if has_default => {
|
ModuleEntry::Part(part) => {
|
||||||
if part.is_included_in_default() {
|
if has_default && part.is_included_in_default() {
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"`{}` is already included in `default`. Either remove `default` or remove `{}`",
|
"`{}` is already included in `default`. Either remove `default` or remove `{}`",
|
||||||
part.name,
|
part.name,
|
||||||
@@ -189,8 +189,7 @@ impl Parse for ModuleDeclaration {
|
|||||||
);
|
);
|
||||||
return Err(Error::new(part.name.span(), msg));
|
return Err(Error::new(part.name.span(), msg));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
ModuleEntry::Part(part) => {
|
|
||||||
if !resolved.insert(part.name.clone()) {
|
if !resolved.insert(part.name.clone()) {
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"`{}` was already declared before. Please remove the duplicate declaration",
|
"`{}` was already declared before. Please remove the duplicate declaration",
|
||||||
@@ -287,7 +286,18 @@ pub struct ModulePart {
|
|||||||
|
|
||||||
impl Parse for ModulePart {
|
impl Parse for ModulePart {
|
||||||
fn parse(input: ParseStream) -> Result<Self> {
|
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()?;
|
let generics: syn::Generics = input.parse()?;
|
||||||
if !generics.params.is_empty() && !Self::is_allowed_generic(&name) {
|
if !generics.params.is_empty() && !Self::is_allowed_generic(&name) {
|
||||||
let valid_generics = ModulePart::format_names(ModulePart::allowed_generics());
|
let valid_generics = ModulePart::format_names(ModulePart::allowed_generics());
|
||||||
@@ -313,6 +323,7 @@ impl Parse for ModulePart {
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
name,
|
name,
|
||||||
generics,
|
generics,
|
||||||
@@ -330,15 +341,20 @@ impl ModulePart {
|
|||||||
Self::allowed_args().into_iter().any(|n| ident == n)
|
Self::allowed_args().into_iter().any(|n| ident == n)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allowed_generics() -> Vec<&'static str> {
|
pub fn allowed_generics() -> &'static [&'static str] {
|
||||||
vec!["Event", "Origin", "Config"]
|
&["Event", "Origin", "Config"]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allowed_args() -> Vec<&'static str> {
|
pub fn allowed_args() -> &'static [&'static str] {
|
||||||
vec!["Inherent"]
|
&["Inherent"]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_names(names: Vec<&'static str>) -> String {
|
/// 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: &[&'static str]) -> String {
|
||||||
let res: Vec<_> = names.into_iter().map(|s| format!("`{}`", s)).collect();
|
let res: Vec<_> = names.into_iter().map(|s| format!("`{}`", s)).collect();
|
||||||
res.join(", ")
|
res.join(", ")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user