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

Add pallet dev mode #12536

Merged
merged 33 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8e171a1
stub for construct_dev_runtime!
sam0x17 Oct 17, 2022
ba45d0b
revert
sam0x17 Oct 18, 2022
e1883c9
stub for dev_mode proc macro
sam0x17 Oct 19, 2022
1f3eb5a
preliminary docs for pallet::dev_mode (attribute) proc macro
sam0x17 Oct 19, 2022
ef886e2
add dev_mode to pallet_macros module
sam0x17 Oct 19, 2022
a62437a
add docs item for dev_mode to frame_support
sam0x17 Oct 19, 2022
89337a6
parsing of #[pallet(dev_mode)]
sam0x17 Oct 20, 2022
4d4f235
strip out dev_mode stub since it will be an arg for pallet instead
sam0x17 Oct 20, 2022
76eb78b
make pallet Def struct aware of dev mode
sam0x17 Oct 20, 2022
7ac54de
WIP
sam0x17 Oct 22, 2022
2a5857e
revert changes to call.rs
sam0x17 Oct 25, 2022
fce8e41
pass dev_mode to pallet parsing code
sam0x17 Oct 25, 2022
011f1e0
auto-specify default weights when in dev mode if not specified
sam0x17 Oct 25, 2022
41f159c
add proof / expect for syn::parse in dev mode weight processing
sam0x17 Oct 25, 2022
a7c85b3
set all storages to unbounded when in dev mode
sam0x17 Oct 25, 2022
bb71c7f
just use 0
sam0x17 Oct 25, 2022
45f9932
add invalid pallet arg test
sam0x17 Oct 27, 2022
0470808
add passing dev mode pallet test
sam0x17 Oct 27, 2022
b0cfc6a
add test confirming that dev mode features only work in dev mode
sam0x17 Oct 27, 2022
b5a82cb
cargo fmt + clean up
sam0x17 Oct 27, 2022
450f09d
bump CI
sam0x17 Oct 27, 2022
e765c42
fix pallet ui test
sam0x17 Oct 28, 2022
755a62c
add docs for dev mode
sam0x17 Oct 28, 2022
69411ab
add warning about using dev mode in production circumstances
sam0x17 Nov 1, 2022
4f8b58f
remove comment about no other attributes being supported
sam0x17 Nov 1, 2022
da619d7
fix unneeded assignment
sam0x17 Nov 1, 2022
d350781
make warning more explicit
sam0x17 Nov 7, 2022
6ac7643
more explicit warning about using dev mode in production
sam0x17 Nov 7, 2022
dc038d4
simpler assignment for dev_mode boolean
sam0x17 Nov 7, 2022
555516d
add note about MEL requirement
sam0x17 Nov 7, 2022
386a8c1
add comment specifying why weights can be omitted in example
sam0x17 Nov 7, 2022
b2d988a
tweak wording of comments
sam0x17 Nov 7, 2022
0aea2c8
bump ci
sam0x17 Nov 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions frame/support/procedural/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,35 @@ mod parse;
pub use parse::Def;
use syn::spanned::Spanned;

mod keyword {
syn::custom_keyword!(dev_mode);
}

pub fn pallet(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut dev_mode = false;
if !attr.is_empty() {
let msg =
"Invalid pallet macro call: expected no attributes, e.g. macro call must be just \
`#[frame_support::pallet]` or `#[pallet]`";
let span = proc_macro2::TokenStream::from(attr).span();
return syn::Error::new(span, msg).to_compile_error().into()
if let Ok(_) = syn::parse::<keyword::dev_mode>(attr.clone()) {
println!("dev mode detected!");
dev_mode = true;
} else {
let msg = "Invalid pallet macro call: unexpected attribute. Macro call must be \
bare, such as `#[frame_support::pallet]` or `#[pallet]`, or must specify the \
`dev_mode` attribute, such as `#[frame_support::pallet(dev_mode)]` or \
#[pallet(dev_mode)]. No other attributes are supported at this time.";
sam0x17 marked this conversation as resolved.
Show resolved Hide resolved
let span = proc_macro2::TokenStream::from(attr).span();
return syn::Error::new(span, msg).to_compile_error().into()
}
}

let item = syn::parse_macro_input!(item as syn::ItemMod);
match parse::Def::try_from(item) {
Ok(def) => expand::expand(def).into(),
match parse::Def::try_from(item, dev_mode) {
Ok(mut def) => {
def.dev_mode = dev_mode;
sam0x17 marked this conversation as resolved.
Show resolved Hide resolved
expand::expand(def).into()
},
Err(e) => e.to_compile_error().into(),
}
}
10 changes: 10 additions & 0 deletions frame/support/procedural/src/pallet/parse/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl CallDef {
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
dev_mode: bool,
) -> syn::Result<Self> {
let item_impl = if let syn::Item::Impl(item) = item {
item
Expand Down Expand Up @@ -213,6 +214,15 @@ impl CallDef {
},
);

if weight_attrs.is_empty() && dev_mode {
// inject a default O(1) weight when dev mode is enabled and no weight has
// been specified on the call
let empty_weight: syn::Expr =
syn::parse(quote::quote!(T::WeightInfo::create()).into())
sam0x17 marked this conversation as resolved.
Show resolved Hide resolved
.expect("we are parsing a quoted string; qed");
weight_attrs.push(FunctionAttr::Weight(empty_weight));
}

if weight_attrs.len() != 1 {
let msg = if weight_attrs.is_empty() {
"Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`"
Expand Down
8 changes: 5 additions & 3 deletions frame/support/procedural/src/pallet/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ pub struct Def {
pub type_values: Vec<type_value::TypeValueDef>,
pub frame_system: syn::Ident,
pub frame_support: syn::Ident,
pub dev_mode: bool,
}

impl Def {
pub fn try_from(mut item: syn::ItemMod) -> syn::Result<Self> {
pub fn try_from(mut item: syn::ItemMod, dev_mode: bool) -> syn::Result<Self> {
let frame_system = generate_crate_access_2018("frame-system")?;
let frame_support = generate_crate_access_2018("frame-support")?;

Expand Down Expand Up @@ -106,7 +107,7 @@ impl Def {
hooks = Some(m);
},
Some(PalletAttr::RuntimeCall(span)) if call.is_none() =>
call = Some(call::CallDef::try_from(span, index, item)?),
call = Some(call::CallDef::try_from(span, index, item, dev_mode)?),
Some(PalletAttr::Error(span)) if error.is_none() =>
error = Some(error::ErrorDef::try_from(span, index, item)?),
Some(PalletAttr::RuntimeEvent(span)) if event.is_none() =>
Expand All @@ -124,7 +125,7 @@ impl Def {
Some(PalletAttr::Inherent(_)) if inherent.is_none() =>
inherent = Some(inherent::InherentDef::try_from(index, item)?),
Some(PalletAttr::Storage(span)) =>
storages.push(storage::StorageDef::try_from(span, index, item)?),
storages.push(storage::StorageDef::try_from(span, index, item, dev_mode)?),
Some(PalletAttr::ValidateUnsigned(_)) if validate_unsigned.is_none() => {
let v = validate_unsigned::ValidateUnsignedDef::try_from(index, item)?;
validate_unsigned = Some(v);
Expand Down Expand Up @@ -173,6 +174,7 @@ impl Def {
type_values,
frame_system,
frame_support,
dev_mode,
};

def.check_instance_usage()?;
Expand Down
8 changes: 7 additions & 1 deletion frame/support/procedural/src/pallet/parse/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ impl StorageDef {
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
dev_mode: bool,
) -> syn::Result<Self> {
let item = if let syn::Item::Type(item) = item {
item
Expand All @@ -686,9 +687,14 @@ impl StorageDef {
};

let attrs: Vec<PalletStorageAttr> = helper::take_item_pallet_attrs(&mut item.attrs)?;
let PalletStorageAttrInfo { getter, rename_as, unbounded, whitelisted } =
let PalletStorageAttrInfo { getter, rename_as, mut unbounded, whitelisted } =
PalletStorageAttrInfo::from_attrs(attrs)?;

if dev_mode {
// set all storages to be unbounded if dev_mode is enabled
unbounded = true;
}

sam0x17 marked this conversation as resolved.
Show resolved Hide resolved
let cfg_attrs = helper::get_item_cfg_attrs(&item.attrs);

let instances = vec![helper::check_type_def_gen(&item.generics, item.ident.span())?];
Expand Down