Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit cca3707

Browse files
committed
fix some constrain, instance compat and unisgned
1 parent 5010b97 commit cca3707

File tree

8 files changed

+493
-54
lines changed

8 files changed

+493
-54
lines changed

frame/support/procedural/src/pallet/parse/event.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ impl EventDef {
149149
item.ident.span()
150150
)? {
151151
instances.push(u);
152+
} else {
153+
// construct_runtime only allow generic event for instantiable pallet.
154+
instances.push(helper::InstanceUsage {
155+
has_instance: false,
156+
span: item.ident.span(),
157+
})
152158
}
153159

154160
let event = syn::parse2::<keyword::Event>(item.ident.to_token_stream())?;

frame/support/procedural/src/pallet/parse/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod event;
3131
pub mod helper;
3232
pub mod genesis_config;
3333
pub mod genesis_build;
34+
pub mod validate_unsigned;
3435

3536
use syn::spanned::Spanned;
3637
use frame_support_procedural_tools::generate_crate_access;
@@ -53,6 +54,7 @@ pub struct Def {
5354
pub inherent: Option<inherent::InherentDef>,
5455
pub genesis_config: Option<genesis_config::GenesisConfigDef>,
5556
pub genesis_build: Option<genesis_build::GenesisBuildDef>,
57+
pub validate_unsigned: Option<validate_unsigned::ValidateUnsignedDef>,
5658
}
5759

5860
impl Def {
@@ -74,6 +76,7 @@ impl Def {
7476
let mut inherent = None;
7577
let mut genesis_config = None;
7678
let mut genesis_build = None;
79+
let mut validate_unsigned = None;
7780
let mut storages = vec![];
7881

7982
for (index, item) in items.iter_mut().enumerate() {
@@ -103,6 +106,10 @@ impl Def {
103106
inherent = Some(inherent::InherentDef::try_from(index, item)?),
104107
Some(PalletAttr::Storage) =>
105108
storages.push(storage::StorageDef::try_from(index, item)?),
109+
Some(PalletAttr::ValidateUnsigned) => {
110+
let v = validate_unsigned::ValidateUnsignedDef::try_from(index, item)?;
111+
validate_unsigned = Some(v);
112+
},
106113
None => (),
107114
}
108115
}
@@ -129,6 +136,7 @@ impl Def {
129136
call: call.ok_or_else(|| syn::Error::new(item_span, "Missing `#[pallet::call]"))?,
130137
genesis_config,
131138
genesis_build,
139+
validate_unsigned,
132140
error,
133141
event,
134142
origin,
@@ -296,6 +304,7 @@ mod keyword {
296304
syn::custom_keyword!(storage);
297305
syn::custom_keyword!(genesis_build);
298306
syn::custom_keyword!(genesis_config);
307+
syn::custom_keyword!(validate_unsigned);
299308
}
300309

301310
/// Parse attributes for item in pallet module
@@ -312,6 +321,7 @@ pub enum PalletAttr {
312321
Storage,
313322
GenesisConfig,
314323
GenesisBuild,
324+
ValidateUnsigned,
315325
}
316326

317327
impl syn::parse::Parse for PalletAttr {
@@ -356,6 +366,9 @@ impl syn::parse::Parse for PalletAttr {
356366
} else if lookahead.peek(keyword::genesis_build) {
357367
content.parse::<keyword::genesis_build>()?;
358368
Ok(PalletAttr::GenesisBuild)
369+
} else if lookahead.peek(keyword::validate_unsigned) {
370+
content.parse::<keyword::validate_unsigned>()?;
371+
Ok(PalletAttr::ValidateUnsigned)
359372
} else {
360373
Err(lookahead.error())
361374
}

frame/support/procedural/src/pallet/parse/origin.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ impl OriginDef {
5050
let is_generic = generics.params.len() > 0;
5151

5252
let mut instances = vec![];
53-
// TODO TODO: is it ok not to be generic over I ? maybe should be same as storage, at least it doesn't seem supported by construct_runtime right now
5453
if let Some(u) = helper::check_type_def_optional_generics(&generics, item.span())? {
5554
instances.push(u);
55+
} else {
56+
// construct_runtime only allow generic event for instantiable pallet.
57+
instances.push(helper::InstanceUsage {
58+
has_instance: false,
59+
span: ident.span(),
60+
})
5661
}
5762

5863
if !matches!(vis, syn::Visibility::Public(_)) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
use syn::spanned::Spanned;
19+
use super::helper;
20+
21+
/// The definition of the pallet validate unsigned implementation.
22+
pub struct ValidateUnsignedDef {
23+
/// The index of validate unsigned item in pallet module.
24+
pub index: usize,
25+
/// A set of usage of instance, must be check for consistency with trait.
26+
pub instances: Vec<helper::InstanceUsage>,
27+
}
28+
29+
impl ValidateUnsignedDef {
30+
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
31+
let item = if let syn::Item::Impl(item) = item {
32+
item
33+
} else {
34+
let msg = "Invalid pallet::validate_unsigned, expect item impl";
35+
return Err(syn::Error::new(item.span(), msg));
36+
};
37+
38+
if item.trait_.is_none() {
39+
let msg = "Invalid pallet::validate_unsigned, expect impl<..> ValidateUnsigned for \
40+
Module<..>";
41+
return Err(syn::Error::new(item.span(), msg));
42+
}
43+
44+
if let Some(last) = item.trait_.as_ref().unwrap().1.segments.last() {
45+
if last.ident != "ValidateUnsigned" {
46+
let msg = "Invalid pallet::validate_unsigned, expect trait ValidateUnsigned";
47+
return Err(syn::Error::new(last.span(), msg));
48+
}
49+
} else {
50+
let msg = "Invalid pallet::validate_unsigned, expect impl<..> ValidateUnsigned for \
51+
Module<..>";
52+
return Err(syn::Error::new(item.span(), msg));
53+
}
54+
55+
let mut instances = vec![];
56+
instances.push(helper::check_module_usage(&item.self_ty)?);
57+
instances.push(helper::check_impl_generics(&item.generics, item.impl_token.span())?);
58+
59+
Ok(ValidateUnsignedDef { index, instances })
60+
}
61+
}

0 commit comments

Comments
 (0)