From 3dc0fc71ded92885868bb7ff5893e029040475fd Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:50:01 +0530 Subject: [PATCH 01/19] Use real rust for pallet type in macro --- Cargo.lock | 23 ++++ .../procedural/src/runtime/expand/mod.rs | 10 +- .../procedural/src/runtime/parse/pallet.rs | 27 +++- .../src/runtime/parse/pallet_decl.rs | 120 +++++++++++++++++- templates/minimal/runtime/src/lib.rs | 12 +- 5 files changed, 176 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a96bb680b750b..a1cb9b328e202 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5924,6 +5924,7 @@ dependencies = [ "macro_magic", "proc-macro-warning", "proc-macro2 1.0.82", + "proc-utils", "quote 1.0.35", "regex", "sp-crypto-hashing", @@ -15166,6 +15167,28 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proc-util-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45e6073edb3261de7793bbde3002e69e6a4baaa34f5e321e299dd30e3c7489ad" +dependencies = [ + "syn 2.0.61", +] + +[[package]] +name = "proc-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "842fd9aaf81ebd6351f2419bd99dd4dae9d97ae2824243e82c69323df4fd1e6b" +dependencies = [ + "prettyplease 0.2.12", + "proc-macro2 1.0.82", + "proc-util-macros", + "quote 1.0.35", + "syn 2.0.61", +] + [[package]] name = "procfs" version = "0.16.0" diff --git a/substrate/frame/support/procedural/src/runtime/expand/mod.rs b/substrate/frame/support/procedural/src/runtime/expand/mod.rs index 43f11896808c7..3cdfb06cb6eaa 100644 --- a/substrate/frame/support/procedural/src/runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/runtime/expand/mod.rs @@ -99,14 +99,20 @@ fn construct_runtime_implicit_to_explicit( for pallet in definition.pallet_decls.iter() { let pallet_path = &pallet.path; let pallet_name = &pallet.name; - let pallet_instance = pallet.instance.as_ref().map(|instance| quote::quote!(<#instance>)); + let runtime_param = &pallet.runtime_param; + let pallet_segment_and_instance = match (&pallet.pallet_segment, &pallet.instance) { + (Some(segment), Some(instance)) => quote::quote!(::#segment<#runtime_param, #instance>), + (Some(segment), None) => quote::quote!(::#segment<#runtime_param>), + (None, Some(instance)) => quote::quote!(<#instance>), + (None, None) => quote::quote!(), + }; expansion = quote::quote!( #frame_support::__private::tt_call! { macro = [{ #pallet_path::tt_default_parts_v2 }] your_tt_return = [{ #frame_support::__private::tt_return }] ~~> #frame_support::match_and_insert! { target = [{ #expansion }] - pattern = [{ #pallet_name = #pallet_path #pallet_instance }] + pattern = [{ #pallet_name = #pallet_path #pallet_segment_and_instance }] } } ); diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs index 09f5290541d3a..818cc6ed14678 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs @@ -55,6 +55,7 @@ impl Pallet { "Invalid pallet declaration, expected a path or a trait object", ))?; + let mut pallet_segment = None; let mut instance = None; if let Some(segment) = path.inner.segments.iter_mut().find(|seg| !seg.arguments.is_empty()) { @@ -62,14 +63,34 @@ impl Pallet { args, .. }) = segment.arguments.clone() { - if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args.first() { - instance = - Some(Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span())); + if segment.ident == "Pallet" { + let mut segment = segment.clone(); segment.arguments = PathArguments::None; + pallet_segment = Some(segment.clone()); + } + let mut args_iter = args.iter(); + if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { + let ident = Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); + if segment.ident == "Pallet" { + if let Some(arg_path) = args_iter.next() { + instance = Some(Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span())); + segment.arguments = PathArguments::None; + } + } else { + instance = Some(ident); + segment.arguments = PathArguments::None; + } } } } + if pallet_segment.is_some() { + path = PalletPath { inner: syn::Path { + leading_colon: None, + segments: path.inner.segments.first().cloned().into_iter().collect(), + }}; + } + pallet_parts = pallet_parts .into_iter() .filter(|part| { diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index e167d37d5f140..9d05d97e1cc5a 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -27,6 +27,11 @@ pub struct PalletDeclaration { pub attrs: Vec, /// The path of the pallet, e.g. `frame_system` in `pub type System = frame_system`. pub path: syn::Path, + /// The segment of the pallet, e.g. `Pallet` in `pub type System = frame_system::Pallet`. + pub pallet_segment: Option, + /// The runtime parameter of the pallet, e.g. `Runtime` in + /// `pub type System = frame_system::Pallet`. + pub runtime_param: Option, /// The instance of the pallet, e.g. `Instance1` in `pub type Council = /// pallet_collective`. pub instance: Option, @@ -42,20 +47,125 @@ impl PalletDeclaration { let mut path = path.path.clone(); + let mut pallet_segment = None; + let mut runtime_param = None; let mut instance = None; if let Some(segment) = path.segments.iter_mut().find(|seg| !seg.arguments.is_empty()) { - if let PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { + if let PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { args, .. }) = segment.arguments.clone() { - if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args.first() { - instance = - Some(Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span())); + if segment.ident == "Pallet" { + let mut segment = segment.clone(); segment.arguments = PathArguments::None; + pallet_segment = Some(segment.clone()); + } + let mut args_iter = args.iter(); + if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { + let ident = Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); + if segment.ident == "Pallet" { + runtime_param = Some(ident.clone()); + if let Some(arg_path) = args_iter.next() { + instance = Some(Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span())); + segment.arguments = PathArguments::None; + } + } else { + instance = Some(ident); + segment.arguments = PathArguments::None; + } } } } - Ok(Self { name, path, instance, attrs: item.attrs.clone() }) + if pallet_segment.is_some() { + path = syn::Path { + leading_colon: None, + segments: path.segments.first().cloned().into_iter().collect(), + }; + } + + Ok(Self { name, path, pallet_segment, runtime_param, instance, attrs: item.attrs.clone() }) } } + +#[test] +fn declaration_works() { + use syn::parse_quote; + + let decl: PalletDeclaration = PalletDeclaration::try_from( + proc_macro2::Span::call_site(), + &parse_quote! { pub type System = frame_system; }, + &parse_quote! { frame_system }, + ) + .expect("Failed to parse pallet declaration"); + + assert_eq!(decl.name, "System"); + assert_eq!(decl.path, parse_quote! { frame_system }); + assert_eq!(decl.pallet_segment, None); + assert_eq!(decl.runtime_param, None); + assert_eq!(decl.instance, None); +} + +#[test] +fn declaration_works_with_instance() { + use syn::parse_quote; + + let decl: PalletDeclaration = PalletDeclaration::try_from( + proc_macro2::Span::call_site(), + &parse_quote! { pub type System = frame_system; }, + &parse_quote! { frame_system }, + ) + .expect("Failed to parse pallet declaration"); + + assert_eq!(decl.name, "System"); + assert_eq!(decl.path, parse_quote! { frame_system }); + assert_eq!(decl.pallet_segment, None); + assert_eq!(decl.runtime_param, None); + assert_eq!(decl.instance, Some(parse_quote! { Instance1 })); +} + +#[test] +fn declaration_works_with_pallet() { + use syn::parse_quote; + + let decl: PalletDeclaration = PalletDeclaration::try_from( + proc_macro2::Span::call_site(), + &parse_quote! { pub type System = frame_system::Pallet; }, + &parse_quote! { frame_system::Pallet }, + ) + .expect("Failed to parse pallet declaration"); + + assert_eq!(decl.name, "System"); + assert_eq!(decl.path, parse_quote! { frame_system }); + + let segment: syn::PathSegment = syn::PathSegment { + ident: parse_quote! { Pallet }, + arguments: PathArguments::None, + }; + assert_eq!(decl.pallet_segment, Some(segment)); + assert_eq!(decl.runtime_param, Some(parse_quote! { Runtime })); + assert_eq!(decl.instance, None); +} + +#[test] +fn declaration_works_with_pallet_and_instance() { + use syn::parse_quote; + + let decl: PalletDeclaration = PalletDeclaration::try_from( + proc_macro2::Span::call_site(), + &parse_quote! { pub type System = frame_system::Pallet; }, + &parse_quote! { frame_system::Pallet }, + ) + .expect("Failed to parse pallet declaration"); + + assert_eq!(decl.name, "System"); + assert_eq!(decl.path, parse_quote! { frame_system }); + + let segment: syn::PathSegment = syn::PathSegment { + ident: parse_quote! { Pallet }, + arguments: PathArguments::None, + }; + assert_eq!(decl.pallet_segment, Some(segment)); + assert_eq!(decl.runtime_param, Some(parse_quote! { Runtime })); + assert_eq!(decl.instance, Some(parse_quote! { Instance1 })); +} diff --git a/templates/minimal/runtime/src/lib.rs b/templates/minimal/runtime/src/lib.rs index d2debbf5689fd..8c7867f4cc8cd 100644 --- a/templates/minimal/runtime/src/lib.rs +++ b/templates/minimal/runtime/src/lib.rs @@ -99,27 +99,27 @@ mod runtime { /// Mandatory system pallet that should always be included in a FRAME runtime. #[runtime::pallet_index(0)] - pub type System = frame_system; + pub type System = frame_system::Pallet; /// Provides a way for consensus systems to set and check the onchain time. #[runtime::pallet_index(1)] - pub type Timestamp = pallet_timestamp; + pub type Timestamp = pallet_timestamp::Pallet; /// Provides the ability to keep track of balances. #[runtime::pallet_index(2)] - pub type Balances = pallet_balances; + pub type Balances = pallet_balances::Pallet; /// Provides a way to execute privileged functions. #[runtime::pallet_index(3)] - pub type Sudo = pallet_sudo; + pub type Sudo = pallet_sudo::Pallet; /// Provides the ability to charge for extrinsic execution. #[runtime::pallet_index(4)] - pub type TransactionPayment = pallet_transaction_payment; + pub type TransactionPayment = pallet_transaction_payment::Pallet; /// A minimal pallet template. #[runtime::pallet_index(5)] - pub type Template = pallet_minimal_template; + pub type Template = pallet_minimal_template::Pallet; } parameter_types! { From bcd976414e91096daa70b90cdb895119ab6db375 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:58:29 +0530 Subject: [PATCH 02/19] Removes Cargo.lock changes --- Cargo.lock | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1cb9b328e202..a96bb680b750b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5924,7 +5924,6 @@ dependencies = [ "macro_magic", "proc-macro-warning", "proc-macro2 1.0.82", - "proc-utils", "quote 1.0.35", "regex", "sp-crypto-hashing", @@ -15167,28 +15166,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "proc-util-macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45e6073edb3261de7793bbde3002e69e6a4baaa34f5e321e299dd30e3c7489ad" -dependencies = [ - "syn 2.0.61", -] - -[[package]] -name = "proc-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "842fd9aaf81ebd6351f2419bd99dd4dae9d97ae2824243e82c69323df4fd1e6b" -dependencies = [ - "prettyplease 0.2.12", - "proc-macro2 1.0.82", - "proc-util-macros", - "quote 1.0.35", - "syn 2.0.61", -] - [[package]] name = "procfs" version = "0.16.0" From 4e75f143b584b12efd5d6c2a9708f818740f721a Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 17 Jun 2024 17:02:33 +0530 Subject: [PATCH 03/19] Moves docs --- substrate/frame/support/procedural/src/lib.rs | 62 +------------------ substrate/frame/support/src/lib.rs | 61 ++++++++++++++++++ 2 files changed, 64 insertions(+), 59 deletions(-) diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index e812ac071b2c9..ca1b9e7053943 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -1188,67 +1188,11 @@ pub fn import_section(attr: TokenStream, tokens: TokenStream) -> TokenStream { .into() } -/// Construct a runtime, with the given name and the given pallets. -/// -/// # Example: -/// -/// ```ignore -/// #[frame_support::runtime] -/// mod runtime { -/// // The main runtime -/// #[runtime::runtime] -/// // Runtime Types to be generated -/// #[runtime::derive( -/// RuntimeCall, -/// RuntimeEvent, -/// RuntimeError, -/// RuntimeOrigin, -/// RuntimeFreezeReason, -/// RuntimeHoldReason, -/// RuntimeSlashReason, -/// RuntimeLockId, -/// RuntimeTask, -/// )] -/// pub struct Runtime; -/// -/// #[runtime::pallet_index(0)] -/// pub type System = frame_system; -/// -/// #[runtime::pallet_index(1)] -/// pub type Test = path::to::test; -/// -/// // Pallet with instance. -/// #[runtime::pallet_index(2)] -/// pub type Test2_Instance1 = test2; -/// -/// // Pallet with calls disabled. -/// #[runtime::pallet_index(3)] -/// #[runtime::disable_call] -/// pub type Test3 = test3; -/// -/// // Pallet with unsigned extrinsics disabled. -/// #[runtime::pallet_index(4)] -/// #[runtime::disable_unsigned] -/// pub type Test4 = test4; -/// } -/// ``` -/// -/// # Legacy Ordering -/// -/// An optional attribute can be defined as #[frame_support::runtime(legacy_ordering)] to -/// ensure that the order of hooks is same as the order of pallets (and not based on the -/// pallet_index). This is to support legacy runtimes and should be avoided for new ones. -/// -/// # Note -/// -/// The population of the genesis storage depends on the order of pallets. So, if one of your -/// pallets depends on another pallet, the pallet that is depended upon needs to come before -/// the pallet depending on it. /// -/// # Type definitions +/// --- /// -/// * The macro generates a type alias for each pallet to their `Pallet`. E.g. `type System = -/// frame_system::Pallet` +/// **Rust-Analyzer users**: See the documentation of the Rust item in +/// `frame_support::runtime`. #[proc_macro_attribute] pub fn runtime(attr: TokenStream, item: TokenStream) -> TokenStream { runtime::runtime(attr, item) diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 8ae1f56b4d686..6404c22fc285b 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -508,6 +508,67 @@ pub use frame_support_procedural::{ construct_runtime, match_and_insert, transactional, PalletError, RuntimeDebugNoBound, }; +/// Construct a runtime, with the given name and the given pallets. +/// +/// # Example: +/// +/// ```ignore +/// #[frame_support::runtime] +/// mod runtime { +/// // The main runtime +/// #[runtime::runtime] +/// // Runtime Types to be generated +/// #[runtime::derive( +/// RuntimeCall, +/// RuntimeEvent, +/// RuntimeError, +/// RuntimeOrigin, +/// RuntimeFreezeReason, +/// RuntimeHoldReason, +/// RuntimeSlashReason, +/// RuntimeLockId, +/// RuntimeTask, +/// )] +/// pub struct Runtime; +/// +/// #[runtime::pallet_index(0)] +/// pub type System = frame_system; +/// +/// #[runtime::pallet_index(1)] +/// pub type Test = path::to::test; +/// +/// // Pallet with instance. +/// #[runtime::pallet_index(2)] +/// pub type Test2_Instance1 = test2; +/// +/// // Pallet with calls disabled. +/// #[runtime::pallet_index(3)] +/// #[runtime::disable_call] +/// pub type Test3 = test3; +/// +/// // Pallet with unsigned extrinsics disabled. +/// #[runtime::pallet_index(4)] +/// #[runtime::disable_unsigned] +/// pub type Test4 = test4; +/// } +/// ``` +/// +/// # Legacy Ordering +/// +/// An optional attribute can be defined as #[frame_support::runtime(legacy_ordering)] to +/// ensure that the order of hooks is same as the order of pallets (and not based on the +/// pallet_index). This is to support legacy runtimes and should be avoided for new ones. +/// +/// # Note +/// +/// The population of the genesis storage depends on the order of pallets. So, if one of your +/// pallets depends on another pallet, the pallet that is depended upon needs to come before +/// the pallet depending on it. +/// +/// # Type definitions +/// +/// * The macro generates a type alias for each pallet to their `Pallet`. E.g. `type System = +/// frame_system::Pallet` pub use frame_support_procedural::runtime; #[doc(hidden)] From b4c7a671d1944ddde52948e702e2c53f3982b5b1 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:03:13 +0530 Subject: [PATCH 04/19] Uses docify for runtime macro docs --- substrate/frame/support/procedural/src/lib.rs | 3 + substrate/frame/support/src/lib.rs | 42 +----- substrate/frame/support/src/tests/mod.rs | 26 +++- substrate/frame/support/src/tests/runtime.rs | 130 ++++++++++++++++++ 4 files changed, 154 insertions(+), 47 deletions(-) create mode 100644 substrate/frame/support/src/tests/runtime.rs diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index ca1b9e7053943..bed301ef2d69c 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -80,6 +80,9 @@ fn counter_prefix(prefix: &str) -> String { } /// Construct a runtime, with the given name and the given pallets. +/// +/// NOTE: A new version of this macro is available at `frame_support::runtime`. This macro will +/// soon be deprecated. Please use the new macro instead. /// /// The parameters here are specific types for `Block`, `NodeBlock`, and `UncheckedExtrinsic` /// and the pallets that are used by the runtime. diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 6404c22fc285b..437c8c23a2673 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -511,47 +511,7 @@ pub use frame_support_procedural::{ /// Construct a runtime, with the given name and the given pallets. /// /// # Example: -/// -/// ```ignore -/// #[frame_support::runtime] -/// mod runtime { -/// // The main runtime -/// #[runtime::runtime] -/// // Runtime Types to be generated -/// #[runtime::derive( -/// RuntimeCall, -/// RuntimeEvent, -/// RuntimeError, -/// RuntimeOrigin, -/// RuntimeFreezeReason, -/// RuntimeHoldReason, -/// RuntimeSlashReason, -/// RuntimeLockId, -/// RuntimeTask, -/// )] -/// pub struct Runtime; -/// -/// #[runtime::pallet_index(0)] -/// pub type System = frame_system; -/// -/// #[runtime::pallet_index(1)] -/// pub type Test = path::to::test; -/// -/// // Pallet with instance. -/// #[runtime::pallet_index(2)] -/// pub type Test2_Instance1 = test2; -/// -/// // Pallet with calls disabled. -/// #[runtime::pallet_index(3)] -/// #[runtime::disable_call] -/// pub type Test3 = test3; -/// -/// // Pallet with unsigned extrinsics disabled. -/// #[runtime::pallet_index(4)] -/// #[runtime::disable_unsigned] -/// pub type Test4 = test4; -/// } -/// ``` +#[doc = docify::embed!("src/tests/runtime.rs", runtime_macro)] /// /// # Legacy Ordering /// diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 88afa243f0932..80c8211a741b1 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -29,6 +29,7 @@ pub use self::frame_system::{pallet_prelude::*, Config, Pallet}; mod inject_runtime_type; mod storage_alias; mod tasks; +mod runtime; #[import_section(tasks::tasks_example)] #[pallet] @@ -220,12 +221,25 @@ type Header = generic::Header; type UncheckedExtrinsic = generic::UncheckedExtrinsic; type Block = generic::Block; -crate::construct_runtime!( - pub enum Runtime - { - System: self::frame_system, - } -); +#[crate::runtime] +mod runtime { + #[runtime::runtime] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeFreezeReason, + RuntimeHoldReason, + RuntimeSlashReason, + RuntimeLockId, + RuntimeTask, + )] + pub struct Runtime; + + #[runtime::pallet_index(0)] + pub type System = self::frame_system; +} #[crate::derive_impl(self::frame_system::config_preludes::TestDefaultConfig as self::frame_system::DefaultConfig)] impl Config for Runtime { diff --git a/substrate/frame/support/src/tests/runtime.rs b/substrate/frame/support/src/tests/runtime.rs new file mode 100644 index 0000000000000..616bc12e599ce --- /dev/null +++ b/substrate/frame/support/src/tests/runtime.rs @@ -0,0 +1,130 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::{Block, frame_system}; +use crate::derive_impl; + +#[crate::pallet(dev_mode)] +mod pallet_basic { + use super::frame_system; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} +} + +impl pallet_basic::Config for Runtime {} + +#[crate::pallet(dev_mode)] +mod pallet_with_disabled_call { + use super::frame_system; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} +} + +impl pallet_with_disabled_call::Config for Runtime {} + +#[crate::pallet(dev_mode)] +mod pallet_with_disabled_unsigned { + use super::frame_system; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} +} + +impl pallet_with_disabled_unsigned::Config for Runtime {} + +#[crate::pallet] +mod pallet_with_instance { + use super::frame_system; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} +} + +#[allow(unused)] +type Instance1 = pallet_with_instance::Pallet; + +impl pallet_with_instance::Config for Runtime {} + +#[allow(unused)] +type Instance2 = pallet_with_instance::Pallet; + +impl pallet_with_instance::Config for Runtime {} + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Runtime { + type Block = Block; +} + +#[docify::export(runtime_macro)] +#[crate::runtime] +mod runtime { + // The main runtime + #[runtime::runtime] + // Runtime Types to be generated + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeFreezeReason, + RuntimeHoldReason, + RuntimeSlashReason, + RuntimeLockId, + RuntimeTask, + )] + pub struct Runtime; + + // Use the concrete pallet type + #[runtime::pallet_index(0)] + pub type System = frame_system::Pallet; + + // Use path to the pallet + #[runtime::pallet_index(1)] + pub type Basic = pallet_basic; + + // Use the concrete pallet type with instance + #[runtime::pallet_index(2)] + pub type PalletWithInstance1 = pallet_with_instance::Pallet; + + // Use path to the pallet with instance + #[runtime::pallet_index(3)] + pub type PalletWithInstance2 = pallet_with_instance; + + // Ensure that the runtime does not export the calls from the pallet + #[runtime::pallet_index(4)] + #[runtime::disable_call] + pub type PalletWithDisabledCall = pallet_with_disabled_call::Pallet; + + // Ensure that the runtime does not export the unsigned calls from the pallet + #[runtime::pallet_index(5)] + #[runtime::disable_unsigned] + pub type PalletWithDisabledUnsigned = pallet_with_disabled_unsigned::Pallet; +} From c5d95df419d199ce4b2e3f2d1b42e353b733e834 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:08:36 +0530 Subject: [PATCH 05/19] Adds PrDoc --- prdoc/pr_4769.prdoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 prdoc/pr_4769.prdoc diff --git a/prdoc/pr_4769.prdoc b/prdoc/pr_4769.prdoc new file mode 100644 index 0000000000000..b796db6749b67 --- /dev/null +++ b/prdoc/pr_4769.prdoc @@ -0,0 +1,18 @@ +title: CheckWeight - account for extrinsic len as proof size + +doc: + - audience: Runtime Dev + description: | + This PR adds the ability to use a real rust type for pallet alias in the new `runtime` macro: + ```rust + #[runtime::pallet_index(0)] + pub type System = frame_system::Pallet; + ``` + + Please note that the current syntax still continues to be supported. + +crates: + - name: frame-support-procedural + bump: patch + - name: minimal-template-runtime + bump: patch From 5243881ff529352cf7e9911a10176b8a0cd4cc73 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Wed, 19 Jun 2024 05:41:16 +0000 Subject: [PATCH 06/19] ".git/.scripts/commands/fmt/fmt.sh" --- substrate/frame/support/procedural/src/lib.rs | 2 +- .../procedural/src/runtime/parse/pallet.rs | 24 ++++-- .../src/runtime/parse/pallet_decl.rs | 40 ++++----- substrate/frame/support/src/tests/mod.rs | 4 +- substrate/frame/support/src/tests/runtime.rs | 84 +++++++++---------- 5 files changed, 82 insertions(+), 72 deletions(-) diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index bed301ef2d69c..51e5657a2e8be 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -80,7 +80,7 @@ fn counter_prefix(prefix: &str) -> String { } /// Construct a runtime, with the given name and the given pallets. -/// +/// /// NOTE: A new version of this macro is available at `frame_support::runtime`. This macro will /// soon be deprecated. Please use the new macro instead. /// diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs index 818cc6ed14678..8cd6e3b75a84e 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs @@ -67,13 +67,19 @@ impl Pallet { let mut segment = segment.clone(); segment.arguments = PathArguments::None; pallet_segment = Some(segment.clone()); - } + } let mut args_iter = args.iter(); - if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { - let ident = Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); + if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = + args_iter.next() + { + let ident = + Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); if segment.ident == "Pallet" { if let Some(arg_path) = args_iter.next() { - instance = Some(Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span())); + instance = Some(Ident::new( + &arg_path.to_token_stream().to_string(), + arg_path.span(), + )); segment.arguments = PathArguments::None; } } else { @@ -85,10 +91,12 @@ impl Pallet { } if pallet_segment.is_some() { - path = PalletPath { inner: syn::Path { - leading_colon: None, - segments: path.inner.segments.first().cloned().into_iter().collect(), - }}; + path = PalletPath { + inner: syn::Path { + leading_colon: None, + segments: path.inner.segments.first().cloned().into_iter().collect(), + }, + }; } pallet_parts = pallet_parts diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 9d05d97e1cc5a..7656fec2a7e74 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -29,7 +29,7 @@ pub struct PalletDeclaration { pub path: syn::Path, /// The segment of the pallet, e.g. `Pallet` in `pub type System = frame_system::Pallet`. pub pallet_segment: Option, - /// The runtime parameter of the pallet, e.g. `Runtime` in + /// The runtime parameter of the pallet, e.g. `Runtime` in /// `pub type System = frame_system::Pallet`. pub runtime_param: Option, /// The instance of the pallet, e.g. `Instance1` in `pub type Council = @@ -51,7 +51,7 @@ impl PalletDeclaration { let mut runtime_param = None; let mut instance = None; if let Some(segment) = path.segments.iter_mut().find(|seg| !seg.arguments.is_empty()) { - if let PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { + if let PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { args, .. }) = segment.arguments.clone() { @@ -61,12 +61,18 @@ impl PalletDeclaration { pallet_segment = Some(segment.clone()); } let mut args_iter = args.iter(); - if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { - let ident = Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); + if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = + args_iter.next() + { + let ident = + Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); if segment.ident == "Pallet" { runtime_param = Some(ident.clone()); if let Some(arg_path) = args_iter.next() { - instance = Some(Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span())); + instance = Some(Ident::new( + &arg_path.to_token_stream().to_string(), + arg_path.span(), + )); segment.arguments = PathArguments::None; } } else { @@ -83,7 +89,7 @@ impl PalletDeclaration { segments: path.segments.first().cloned().into_iter().collect(), }; } - + Ok(Self { name, path, pallet_segment, runtime_param, instance, attrs: item.attrs.clone() }) } } @@ -104,7 +110,7 @@ fn declaration_works() { assert_eq!(decl.pallet_segment, None); assert_eq!(decl.runtime_param, None); assert_eq!(decl.instance, None); -} +} #[test] fn declaration_works_with_instance() { @@ -122,7 +128,7 @@ fn declaration_works_with_instance() { assert_eq!(decl.pallet_segment, None); assert_eq!(decl.runtime_param, None); assert_eq!(decl.instance, Some(parse_quote! { Instance1 })); -} +} #[test] fn declaration_works_with_pallet() { @@ -138,14 +144,12 @@ fn declaration_works_with_pallet() { assert_eq!(decl.name, "System"); assert_eq!(decl.path, parse_quote! { frame_system }); - let segment: syn::PathSegment = syn::PathSegment { - ident: parse_quote! { Pallet }, - arguments: PathArguments::None, - }; + let segment: syn::PathSegment = + syn::PathSegment { ident: parse_quote! { Pallet }, arguments: PathArguments::None }; assert_eq!(decl.pallet_segment, Some(segment)); - assert_eq!(decl.runtime_param, Some(parse_quote! { Runtime })); + assert_eq!(decl.runtime_param, Some(parse_quote! { Runtime })); assert_eq!(decl.instance, None); -} +} #[test] fn declaration_works_with_pallet_and_instance() { @@ -161,11 +165,9 @@ fn declaration_works_with_pallet_and_instance() { assert_eq!(decl.name, "System"); assert_eq!(decl.path, parse_quote! { frame_system }); - let segment: syn::PathSegment = syn::PathSegment { - ident: parse_quote! { Pallet }, - arguments: PathArguments::None, - }; + let segment: syn::PathSegment = + syn::PathSegment { ident: parse_quote! { Pallet }, arguments: PathArguments::None }; assert_eq!(decl.pallet_segment, Some(segment)); - assert_eq!(decl.runtime_param, Some(parse_quote! { Runtime })); + assert_eq!(decl.runtime_param, Some(parse_quote! { Runtime })); assert_eq!(decl.instance, Some(parse_quote! { Instance1 })); } diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 80c8211a741b1..34652231e3bce 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -27,9 +27,9 @@ use sp_runtime::{generic, traits::BlakeTwo256, BuildStorage}; pub use self::frame_system::{pallet_prelude::*, Config, Pallet}; mod inject_runtime_type; +mod runtime; mod storage_alias; mod tasks; -mod runtime; #[import_section(tasks::tasks_example)] #[pallet] @@ -233,7 +233,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, + RuntimeTask )] pub struct Runtime; diff --git a/substrate/frame/support/src/tests/runtime.rs b/substrate/frame/support/src/tests/runtime.rs index 616bc12e599ce..a9d9281f50da3 100644 --- a/substrate/frame/support/src/tests/runtime.rs +++ b/substrate/frame/support/src/tests/runtime.rs @@ -15,57 +15,57 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::{Block, frame_system}; +use super::{frame_system, Block}; use crate::derive_impl; #[crate::pallet(dev_mode)] mod pallet_basic { - use super::frame_system; + use super::frame_system; - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); - #[pallet::config] - pub trait Config: frame_system::Config {} + #[pallet::config] + pub trait Config: frame_system::Config {} } impl pallet_basic::Config for Runtime {} #[crate::pallet(dev_mode)] mod pallet_with_disabled_call { - use super::frame_system; + use super::frame_system; - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); - #[pallet::config] - pub trait Config: frame_system::Config {} + #[pallet::config] + pub trait Config: frame_system::Config {} } impl pallet_with_disabled_call::Config for Runtime {} #[crate::pallet(dev_mode)] mod pallet_with_disabled_unsigned { - use super::frame_system; + use super::frame_system; - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); - #[pallet::config] - pub trait Config: frame_system::Config {} + #[pallet::config] + pub trait Config: frame_system::Config {} } impl pallet_with_disabled_unsigned::Config for Runtime {} #[crate::pallet] mod pallet_with_instance { - use super::frame_system; + use super::frame_system; - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); - #[pallet::config] - pub trait Config: frame_system::Config {} + #[pallet::config] + pub trait Config: frame_system::Config {} } #[allow(unused)] @@ -86,9 +86,9 @@ impl frame_system::Config for Runtime { #[docify::export(runtime_macro)] #[crate::runtime] mod runtime { - // The main runtime + // The main runtime #[runtime::runtime] - // Runtime Types to be generated + // Runtime Types to be generated #[runtime::derive( RuntimeCall, RuntimeEvent, @@ -98,33 +98,33 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, + RuntimeTask )] pub struct Runtime; - // Use the concrete pallet type + // Use the concrete pallet type #[runtime::pallet_index(0)] pub type System = frame_system::Pallet; - // Use path to the pallet - #[runtime::pallet_index(1)] - pub type Basic = pallet_basic; + // Use path to the pallet + #[runtime::pallet_index(1)] + pub type Basic = pallet_basic; - // Use the concrete pallet type with instance - #[runtime::pallet_index(2)] - pub type PalletWithInstance1 = pallet_with_instance::Pallet; + // Use the concrete pallet type with instance + #[runtime::pallet_index(2)] + pub type PalletWithInstance1 = pallet_with_instance::Pallet; - // Use path to the pallet with instance - #[runtime::pallet_index(3)] - pub type PalletWithInstance2 = pallet_with_instance; + // Use path to the pallet with instance + #[runtime::pallet_index(3)] + pub type PalletWithInstance2 = pallet_with_instance; - // Ensure that the runtime does not export the calls from the pallet - #[runtime::pallet_index(4)] - #[runtime::disable_call] - pub type PalletWithDisabledCall = pallet_with_disabled_call::Pallet; + // Ensure that the runtime does not export the calls from the pallet + #[runtime::pallet_index(4)] + #[runtime::disable_call] + pub type PalletWithDisabledCall = pallet_with_disabled_call::Pallet; - // Ensure that the runtime does not export the unsigned calls from the pallet - #[runtime::pallet_index(5)] - #[runtime::disable_unsigned] - pub type PalletWithDisabledUnsigned = pallet_with_disabled_unsigned::Pallet; + // Ensure that the runtime does not export the unsigned calls from the pallet + #[runtime::pallet_index(5)] + #[runtime::disable_unsigned] + pub type PalletWithDisabledUnsigned = pallet_with_disabled_unsigned::Pallet; } From 639d37a25adda7035db1647928f1fa6ff5a6d43e Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:24:03 +0530 Subject: [PATCH 07/19] Updates PrDoc --- prdoc/pr_4769.prdoc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prdoc/pr_4769.prdoc b/prdoc/pr_4769.prdoc index b796db6749b67..0326daf038053 100644 --- a/prdoc/pr_4769.prdoc +++ b/prdoc/pr_4769.prdoc @@ -14,5 +14,7 @@ doc: crates: - name: frame-support-procedural bump: patch + - name: frame-support + bump: patch - name: minimal-template-runtime bump: patch From c2319ec5421f7b91f6631da9742a537144529ff8 Mon Sep 17 00:00:00 2001 From: gupnik Date: Thu, 20 Jun 2024 08:25:57 +0200 Subject: [PATCH 08/19] Update prdoc/pr_4769.prdoc --- prdoc/pr_4769.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4769.prdoc b/prdoc/pr_4769.prdoc index 0326daf038053..e9691ba6f8974 100644 --- a/prdoc/pr_4769.prdoc +++ b/prdoc/pr_4769.prdoc @@ -1,4 +1,4 @@ -title: CheckWeight - account for extrinsic len as proof size +title: Use real rust type for pallet alias in `runtime` macro doc: - audience: Runtime Dev From 217ff38723899f680d71a32c3f3c0035a49508fd Mon Sep 17 00:00:00 2001 From: gupnik Date: Fri, 21 Jun 2024 15:20:31 +0200 Subject: [PATCH 09/19] Update substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- .../frame/support/procedural/src/runtime/parse/pallet_decl.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 7656fec2a7e74..f6cba949075af 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -64,8 +64,7 @@ impl PalletDeclaration { if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { - let ident = - Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); + let ident = arg_path.require_ident()?.clone(); if segment.ident == "Pallet" { runtime_param = Some(ident.clone()); if let Some(arg_path) = args_iter.next() { From 0d364d0dc11e09b7aba3e4a8285e3bb76b86967d Mon Sep 17 00:00:00 2001 From: gupnik Date: Fri, 21 Jun 2024 15:20:42 +0200 Subject: [PATCH 10/19] Update substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- .../frame/support/procedural/src/runtime/parse/pallet_decl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index f6cba949075af..897a60a70485f 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -66,7 +66,7 @@ impl PalletDeclaration { { let ident = arg_path.require_ident()?.clone(); if segment.ident == "Pallet" { - runtime_param = Some(ident.clone()); + runtime_param = Some(ident); if let Some(arg_path) = args_iter.next() { instance = Some(Ident::new( &arg_path.to_token_stream().to_string(), From 342612239905f8d8a2b276b28a01af5f0b654749 Mon Sep 17 00:00:00 2001 From: gupnik Date: Fri, 21 Jun 2024 15:20:49 +0200 Subject: [PATCH 11/19] Update substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- .../support/procedural/src/runtime/parse/pallet_decl.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 897a60a70485f..482c6de2257f7 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -68,10 +68,7 @@ impl PalletDeclaration { if segment.ident == "Pallet" { runtime_param = Some(ident); if let Some(arg_path) = args_iter.next() { - instance = Some(Ident::new( - &arg_path.to_token_stream().to_string(), - arg_path.span(), - )); + instance = Some(arg_path.require_ident()?.clone()); segment.arguments = PathArguments::None; } } else { From 059cbb9267e1f95062c468c048dc27dc2b1d5fd7 Mon Sep 17 00:00:00 2001 From: gupnik Date: Fri, 21 Jun 2024 15:20:56 +0200 Subject: [PATCH 12/19] Update substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- .../frame/support/procedural/src/runtime/parse/pallet_decl.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 482c6de2257f7..69795c5e523d1 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -69,7 +69,6 @@ impl PalletDeclaration { runtime_param = Some(ident); if let Some(arg_path) = args_iter.next() { instance = Some(arg_path.require_ident()?.clone()); - segment.arguments = PathArguments::None; } } else { instance = Some(ident); From 80afc50746848397c43a1e656f5faca363e9d4eb Mon Sep 17 00:00:00 2001 From: gupnik Date: Fri, 21 Jun 2024 15:21:31 +0200 Subject: [PATCH 13/19] Update substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- .../frame/support/procedural/src/runtime/parse/pallet_decl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 69795c5e523d1..269352276123b 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -81,7 +81,7 @@ impl PalletDeclaration { if pallet_segment.is_some() { path = syn::Path { leading_colon: None, - segments: path.segments.first().cloned().into_iter().collect(), + segments: path.segments.iter().filter(|seg| seg.arguments.is_empty()).cloned().iter() }; } From 465b58eeeb7486a3f4af99edf2fd754e4ec75c27 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:01:00 +0530 Subject: [PATCH 14/19] Addresses review comments --- substrate/bin/node/runtime/src/lib.rs | 160 +++++++++--------- substrate/frame/support/procedural/Cargo.toml | 2 +- .../procedural/src/runtime/parse/mod.rs | 2 +- .../procedural/src/runtime/parse/pallet.rs | 38 +---- .../src/runtime/parse/pallet_decl.rs | 15 +- 5 files changed, 92 insertions(+), 125 deletions(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 2bddb3a1adef4..517f86c39c51c 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2248,248 +2248,248 @@ mod runtime { pub struct Runtime; #[runtime::pallet_index(0)] - pub type System = frame_system; + pub type System = frame_system::Pallet; #[runtime::pallet_index(1)] - pub type Utility = pallet_utility; + pub type Utility = pallet_utility::Pallet; #[runtime::pallet_index(2)] - pub type Babe = pallet_babe; + pub type Babe = pallet_babe::Pallet; #[runtime::pallet_index(3)] - pub type Timestamp = pallet_timestamp; + pub type Timestamp = pallet_timestamp::Pallet; // Authorship must be before session in order to note author in the correct session and era // for im-online and staking. #[runtime::pallet_index(4)] - pub type Authorship = pallet_authorship; + pub type Authorship = pallet_authorship::Pallet; #[runtime::pallet_index(5)] - pub type Indices = pallet_indices; + pub type Indices = pallet_indices::Pallet; #[runtime::pallet_index(6)] - pub type Balances = pallet_balances; + pub type Balances = pallet_balances::Pallet; #[runtime::pallet_index(7)] - pub type TransactionPayment = pallet_transaction_payment; + pub type TransactionPayment = pallet_transaction_payment::Pallet; #[runtime::pallet_index(8)] - pub type AssetTxPayment = pallet_asset_tx_payment; + pub type AssetTxPayment = pallet_asset_tx_payment::Pallet; #[runtime::pallet_index(9)] - pub type AssetConversionTxPayment = pallet_asset_conversion_tx_payment; + pub type AssetConversionTxPayment = pallet_asset_conversion_tx_payment::Pallet; #[runtime::pallet_index(10)] - pub type ElectionProviderMultiPhase = pallet_election_provider_multi_phase; + pub type ElectionProviderMultiPhase = pallet_election_provider_multi_phase::Pallet; #[runtime::pallet_index(11)] - pub type Staking = pallet_staking; + pub type Staking = pallet_staking::Pallet; #[runtime::pallet_index(12)] - pub type Session = pallet_session; + pub type Session = pallet_session::Pallet; #[runtime::pallet_index(13)] - pub type Democracy = pallet_democracy; + pub type Democracy = pallet_democracy::Pallet; #[runtime::pallet_index(14)] - pub type Council = pallet_collective; + pub type Council = pallet_collective::Pallet; #[runtime::pallet_index(15)] - pub type TechnicalCommittee = pallet_collective; + pub type TechnicalCommittee = pallet_collective::Pallet; #[runtime::pallet_index(16)] - pub type Elections = pallet_elections_phragmen; + pub type Elections = pallet_elections_phragmen::Pallet; #[runtime::pallet_index(17)] - pub type TechnicalMembership = pallet_membership; + pub type TechnicalMembership = pallet_membership::Pallet; #[runtime::pallet_index(18)] - pub type Grandpa = pallet_grandpa; + pub type Grandpa = pallet_grandpa::Pallet; #[runtime::pallet_index(19)] - pub type Treasury = pallet_treasury; + pub type Treasury = pallet_treasury::Pallet; #[runtime::pallet_index(20)] - pub type AssetRate = pallet_asset_rate; + pub type AssetRate = pallet_asset_rate::Pallet; #[runtime::pallet_index(21)] - pub type Contracts = pallet_contracts; + pub type Contracts = pallet_contracts::Pallet; #[runtime::pallet_index(22)] - pub type Sudo = pallet_sudo; + pub type Sudo = pallet_sudo::Pallet; #[runtime::pallet_index(23)] - pub type ImOnline = pallet_im_online; + pub type ImOnline = pallet_im_online::Pallet; #[runtime::pallet_index(24)] - pub type AuthorityDiscovery = pallet_authority_discovery; + pub type AuthorityDiscovery = pallet_authority_discovery::Pallet; #[runtime::pallet_index(25)] - pub type Offences = pallet_offences; + pub type Offences = pallet_offences::Pallet; #[runtime::pallet_index(26)] - pub type Historical = pallet_session_historical; + pub type Historical = pallet_session_historical::Pallet; #[runtime::pallet_index(27)] - pub type RandomnessCollectiveFlip = pallet_insecure_randomness_collective_flip; + pub type RandomnessCollectiveFlip = pallet_insecure_randomness_collective_flip::Pallet; #[runtime::pallet_index(28)] - pub type Identity = pallet_identity; + pub type Identity = pallet_identity::Pallet; #[runtime::pallet_index(29)] - pub type Society = pallet_society; + pub type Society = pallet_society::Pallet; #[runtime::pallet_index(30)] - pub type Recovery = pallet_recovery; + pub type Recovery = pallet_recovery::Pallet; #[runtime::pallet_index(31)] - pub type Vesting = pallet_vesting; + pub type Vesting = pallet_vesting::Pallet; #[runtime::pallet_index(32)] - pub type Scheduler = pallet_scheduler; + pub type Scheduler = pallet_scheduler::Pallet; #[runtime::pallet_index(33)] - pub type Glutton = pallet_glutton; + pub type Glutton = pallet_glutton::Pallet; #[runtime::pallet_index(34)] - pub type Preimage = pallet_preimage; + pub type Preimage = pallet_preimage::Pallet; #[runtime::pallet_index(35)] - pub type Proxy = pallet_proxy; + pub type Proxy = pallet_proxy::Pallet; #[runtime::pallet_index(36)] - pub type Multisig = pallet_multisig; + pub type Multisig = pallet_multisig::Pallet; #[runtime::pallet_index(37)] - pub type Bounties = pallet_bounties; + pub type Bounties = pallet_bounties::Pallet; #[runtime::pallet_index(38)] - pub type Tips = pallet_tips; + pub type Tips = pallet_tips::Pallet; #[runtime::pallet_index(39)] - pub type Assets = pallet_assets; + pub type Assets = pallet_assets::Pallet; #[runtime::pallet_index(40)] - pub type PoolAssets = pallet_assets; + pub type PoolAssets = pallet_assets::Pallet; #[runtime::pallet_index(41)] - pub type Beefy = pallet_beefy; + pub type Beefy = pallet_beefy::Pallet; // MMR leaf construction must be after session in order to have a leaf's next_auth_set // refer to block. See issue polkadot-fellows/runtimes#160 for details. #[runtime::pallet_index(42)] - pub type Mmr = pallet_mmr; + pub type Mmr = pallet_mmr::Pallet; #[runtime::pallet_index(43)] - pub type MmrLeaf = pallet_beefy_mmr; + pub type MmrLeaf = pallet_beefy_mmr::Pallet; #[runtime::pallet_index(44)] - pub type Lottery = pallet_lottery; + pub type Lottery = pallet_lottery::Pallet; #[runtime::pallet_index(45)] - pub type Nis = pallet_nis; + pub type Nis = pallet_nis::Pallet; #[runtime::pallet_index(46)] - pub type Uniques = pallet_uniques; + pub type Uniques = pallet_uniques::Pallet; #[runtime::pallet_index(47)] - pub type Nfts = pallet_nfts; + pub type Nfts = pallet_nfts::Pallet; #[runtime::pallet_index(48)] - pub type NftFractionalization = pallet_nft_fractionalization; + pub type NftFractionalization = pallet_nft_fractionalization::Pallet; #[runtime::pallet_index(49)] - pub type Salary = pallet_salary; + pub type Salary = pallet_salary::Pallet; #[runtime::pallet_index(50)] - pub type CoreFellowship = pallet_core_fellowship; + pub type CoreFellowship = pallet_core_fellowship::Pallet; #[runtime::pallet_index(51)] - pub type TransactionStorage = pallet_transaction_storage; + pub type TransactionStorage = pallet_transaction_storage::Pallet; #[runtime::pallet_index(52)] - pub type VoterList = pallet_bags_list; + pub type VoterList = pallet_bags_list::Pallet; #[runtime::pallet_index(53)] - pub type StateTrieMigration = pallet_state_trie_migration; + pub type StateTrieMigration = pallet_state_trie_migration::Pallet; #[runtime::pallet_index(54)] - pub type ChildBounties = pallet_child_bounties; + pub type ChildBounties = pallet_child_bounties::Pallet; #[runtime::pallet_index(55)] - pub type Referenda = pallet_referenda; + pub type Referenda = pallet_referenda::Pallet; #[runtime::pallet_index(56)] - pub type Remark = pallet_remark; + pub type Remark = pallet_remark::Pallet; #[runtime::pallet_index(57)] - pub type RootTesting = pallet_root_testing; + pub type RootTesting = pallet_root_testing::Pallet; #[runtime::pallet_index(58)] - pub type ConvictionVoting = pallet_conviction_voting; + pub type ConvictionVoting = pallet_conviction_voting::Pallet; #[runtime::pallet_index(59)] - pub type Whitelist = pallet_whitelist; + pub type Whitelist = pallet_whitelist::Pallet; #[runtime::pallet_index(60)] - pub type AllianceMotion = pallet_collective; + pub type AllianceMotion = pallet_collective::Pallet; #[runtime::pallet_index(61)] - pub type Alliance = pallet_alliance; + pub type Alliance = pallet_alliance::Pallet; #[runtime::pallet_index(62)] - pub type NominationPools = pallet_nomination_pools; + pub type NominationPools = pallet_nomination_pools::Pallet; #[runtime::pallet_index(63)] - pub type RankedPolls = pallet_referenda; + pub type RankedPolls = pallet_referenda::Pallet; #[runtime::pallet_index(64)] - pub type RankedCollective = pallet_ranked_collective; + pub type RankedCollective = pallet_ranked_collective::Pallet; #[runtime::pallet_index(65)] - pub type AssetConversion = pallet_asset_conversion; + pub type AssetConversion = pallet_asset_conversion::Pallet; #[runtime::pallet_index(66)] - pub type FastUnstake = pallet_fast_unstake; + pub type FastUnstake = pallet_fast_unstake::Pallet; #[runtime::pallet_index(67)] - pub type MessageQueue = pallet_message_queue; + pub type MessageQueue = pallet_message_queue::Pallet; #[runtime::pallet_index(68)] - pub type Pov = frame_benchmarking_pallet_pov; + pub type Pov = frame_benchmarking_pallet_pov::Pallet; #[runtime::pallet_index(69)] - pub type TxPause = pallet_tx_pause; + pub type TxPause = pallet_tx_pause::Pallet; #[runtime::pallet_index(70)] - pub type SafeMode = pallet_safe_mode; + pub type SafeMode = pallet_safe_mode::Pallet; #[runtime::pallet_index(71)] - pub type Statement = pallet_statement; + pub type Statement = pallet_statement::Pallet; #[runtime::pallet_index(72)] - pub type MultiBlockMigrations = pallet_migrations; + pub type MultiBlockMigrations = pallet_migrations::Pallet; #[runtime::pallet_index(73)] - pub type Broker = pallet_broker; + pub type Broker = pallet_broker::Pallet; #[runtime::pallet_index(74)] - pub type TasksExample = pallet_example_tasks; + pub type TasksExample = pallet_example_tasks::Pallet; #[runtime::pallet_index(75)] - pub type Mixnet = pallet_mixnet; + pub type Mixnet = pallet_mixnet::Pallet; #[runtime::pallet_index(76)] - pub type Parameters = pallet_parameters; + pub type Parameters = pallet_parameters::Pallet; #[runtime::pallet_index(77)] - pub type SkipFeelessPayment = pallet_skip_feeless_payment; + pub type SkipFeelessPayment = pallet_skip_feeless_payment::Pallet; #[runtime::pallet_index(78)] - pub type PalletExampleMbms = pallet_example_mbm; + pub type PalletExampleMbms = pallet_example_mbm::Pallet; #[runtime::pallet_index(79)] - pub type AssetConversionMigration = pallet_asset_conversion_ops; + pub type AssetConversionMigration = pallet_asset_conversion_ops::Pallet; } /// The address format for describing accounts. diff --git a/substrate/frame/support/procedural/Cargo.toml b/substrate/frame/support/procedural/Cargo.toml index b04af63de8117..23d50e94f02c1 100644 --- a/substrate/frame/support/procedural/Cargo.toml +++ b/substrate/frame/support/procedural/Cargo.toml @@ -24,7 +24,7 @@ cfg-expr = "0.15.5" itertools = "0.11" proc-macro2 = "1.0.56" quote = { workspace = true } -syn = { features = ["full", "visit-mut"], workspace = true } +syn = { features = ["full", "visit-mut", "parsing" ], workspace = true } frame-support-procedural-tools = { path = "tools" } macro_magic = { version = "0.5.0", features = ["proc_support"] } proc-macro-warning = { version = "1.0.0", default-features = false } diff --git a/substrate/frame/support/procedural/src/runtime/parse/mod.rs b/substrate/frame/support/procedural/src/runtime/parse/mod.rs index dd83cd0da90a2..32093ceedfb5a 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/mod.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/mod.rs @@ -189,7 +189,7 @@ impl Def { match *pallet_item.ty.clone() { syn::Type::Path(ref path) => { let pallet_decl = - PalletDeclaration::try_from(item.span(), &pallet_item, path)?; + PalletDeclaration::try_from(item.span(), &pallet_item, &path.path)?; if let Some(used_pallet) = names.insert(pallet_decl.name.clone(), pallet_decl.name.span()) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs index 8cd6e3b75a84e..d5a9e84a313b3 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs @@ -16,9 +16,10 @@ // limitations under the License. use crate::construct_runtime::parse::{Pallet, PalletPart, PalletPartKeyword, PalletPath}; +use crate::runtime::parse::PalletDeclaration; use frame_support_procedural_tools::get_doc_literals; use quote::ToTokens; -use syn::{punctuated::Punctuated, spanned::Spanned, token, Error, Ident, PathArguments}; +use syn::{punctuated::Punctuated, token, Error}; impl Pallet { pub fn try_from( @@ -55,40 +56,7 @@ impl Pallet { "Invalid pallet declaration, expected a path or a trait object", ))?; - let mut pallet_segment = None; - let mut instance = None; - if let Some(segment) = path.inner.segments.iter_mut().find(|seg| !seg.arguments.is_empty()) - { - if let PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { - args, .. - }) = segment.arguments.clone() - { - if segment.ident == "Pallet" { - let mut segment = segment.clone(); - segment.arguments = PathArguments::None; - pallet_segment = Some(segment.clone()); - } - let mut args_iter = args.iter(); - if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = - args_iter.next() - { - let ident = - Ident::new(&arg_path.to_token_stream().to_string(), arg_path.span()); - if segment.ident == "Pallet" { - if let Some(arg_path) = args_iter.next() { - instance = Some(Ident::new( - &arg_path.to_token_stream().to_string(), - arg_path.span(), - )); - segment.arguments = PathArguments::None; - } - } else { - instance = Some(ident); - segment.arguments = PathArguments::None; - } - } - } - } + let PalletDeclaration { pallet_segment, instance, .. } = PalletDeclaration::try_from(attr_span, item, &path.inner)?; if pallet_segment.is_some() { path = PalletPath { diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 269352276123b..8dc31c6a439cd 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -15,8 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use quote::ToTokens; -use syn::{spanned::Spanned, Attribute, Ident, PathArguments}; +use syn::{Attribute, Ident, PathArguments}; /// The declaration of a pallet. #[derive(Debug, Clone)] @@ -41,11 +40,11 @@ impl PalletDeclaration { pub fn try_from( _attr_span: proc_macro2::Span, item: &syn::ItemType, - path: &syn::TypePath, + path: &syn::Path, ) -> syn::Result { let name = item.ident.clone(); - let mut path = path.path.clone(); + let mut path = path.clone(); let mut pallet_segment = None; let mut runtime_param = None; @@ -64,11 +63,11 @@ impl PalletDeclaration { if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { - let ident = arg_path.require_ident()?.clone(); + let ident = arg_path.path.require_ident()?.clone(); if segment.ident == "Pallet" { runtime_param = Some(ident); - if let Some(arg_path) = args_iter.next() { - instance = Some(arg_path.require_ident()?.clone()); + if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { + instance = Some(arg_path.path.require_ident()?.clone()); } } else { instance = Some(ident); @@ -81,7 +80,7 @@ impl PalletDeclaration { if pallet_segment.is_some() { path = syn::Path { leading_colon: None, - segments: path.segments.iter().filter(|seg| seg.arguments.is_empty()).cloned().iter() + segments: path.segments.iter().filter(|seg| seg.arguments.is_empty()).cloned().collect() }; } From 46171175c4f2a89ff7fd681c845f4b59e3f4bcbc Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:03:10 +0530 Subject: [PATCH 15/19] FMT --- .../support/procedural/src/runtime/parse/pallet.rs | 9 ++++++--- .../procedural/src/runtime/parse/pallet_decl.rs | 11 +++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs index d5a9e84a313b3..0514415a8d9e1 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs @@ -15,8 +15,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::construct_runtime::parse::{Pallet, PalletPart, PalletPartKeyword, PalletPath}; -use crate::runtime::parse::PalletDeclaration; +use crate::{ + construct_runtime::parse::{Pallet, PalletPart, PalletPartKeyword, PalletPath}, + runtime::parse::PalletDeclaration, +}; use frame_support_procedural_tools::get_doc_literals; use quote::ToTokens; use syn::{punctuated::Punctuated, token, Error}; @@ -56,7 +58,8 @@ impl Pallet { "Invalid pallet declaration, expected a path or a trait object", ))?; - let PalletDeclaration { pallet_segment, instance, .. } = PalletDeclaration::try_from(attr_span, item, &path.inner)?; + let PalletDeclaration { pallet_segment, instance, .. } = + PalletDeclaration::try_from(attr_span, item, &path.inner)?; if pallet_segment.is_some() { path = PalletPath { diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs index 8dc31c6a439cd..fbac75336f255 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet_decl.rs @@ -66,7 +66,9 @@ impl PalletDeclaration { let ident = arg_path.path.require_ident()?.clone(); if segment.ident == "Pallet" { runtime_param = Some(ident); - if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = args_iter.next() { + if let Some(syn::GenericArgument::Type(syn::Type::Path(arg_path))) = + args_iter.next() + { instance = Some(arg_path.path.require_ident()?.clone()); } } else { @@ -80,7 +82,12 @@ impl PalletDeclaration { if pallet_segment.is_some() { path = syn::Path { leading_colon: None, - segments: path.segments.iter().filter(|seg| seg.arguments.is_empty()).cloned().collect() + segments: path + .segments + .iter() + .filter(|seg| seg.arguments.is_empty()) + .cloned() + .collect(), }; } From 4e859b01335a0782f19fff8af864eda985060d43 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:14:45 +0530 Subject: [PATCH 16/19] Updates docs --- substrate/frame/support/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 437c8c23a2673..8093a2d112bdb 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -513,7 +513,9 @@ pub use frame_support_procedural::{ /// # Example: #[doc = docify::embed!("src/tests/runtime.rs", runtime_macro)] /// -/// # Legacy Ordering +/// # Supported Attributes: +/// +/// ## Legacy Ordering /// /// An optional attribute can be defined as #[frame_support::runtime(legacy_ordering)] to /// ensure that the order of hooks is same as the order of pallets (and not based on the From 05b9f414533b5d2fa425ac48b908cd98b613f776 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:36:19 +0530 Subject: [PATCH 17/19] Reuses code and adds tests --- .../procedural/src/runtime/parse/mod.rs | 21 ++++ .../procedural/src/runtime/parse/pallet.rs | 103 ++++++++++++++++-- 2 files changed, 115 insertions(+), 9 deletions(-) diff --git a/substrate/frame/support/procedural/src/runtime/parse/mod.rs b/substrate/frame/support/procedural/src/runtime/parse/mod.rs index 32093ceedfb5a..49647993aac72 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/mod.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/mod.rs @@ -267,3 +267,24 @@ impl Def { Ok(def) } } + +#[test] +fn runtime_parsing_works() { + let def = Def::try_from(syn::parse_quote! { + #[runtime::runtime] + mod runtime { + #[runtime::derive(RuntimeCall, RuntimeEvent)] + #[runtime::runtime] + pub struct Runtime; + + #[runtime::pallet_index(0)] + pub type System = frame_system::Pallet; + + #[runtime::pallet_index(1)] + pub type Pallet1 = pallet1; + } + }) + .expect("Failed to parse runtime definition"); + + assert_eq!(def.runtime_struct.ident, "Runtime"); +} diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs index 0514415a8d9e1..ebfd0c9ccceed 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs @@ -58,17 +58,10 @@ impl Pallet { "Invalid pallet declaration, expected a path or a trait object", ))?; - let PalletDeclaration { pallet_segment, instance, .. } = + let PalletDeclaration { path: inner, instance, .. } = PalletDeclaration::try_from(attr_span, item, &path.inner)?; - if pallet_segment.is_some() { - path = PalletPath { - inner: syn::Path { - leading_colon: None, - segments: path.inner.segments.first().cloned().into_iter().collect(), - }, - }; - } + path = PalletPath { inner }; pallet_parts = pallet_parts .into_iter() @@ -101,3 +94,95 @@ impl Pallet { }) } } + +#[test] +fn pallet_parsing_works() { + use syn::{parse_quote, ItemType}; + + let item: ItemType = parse_quote! { + pub type System = frame_system + Call; + }; + let ItemType { ty, .. } = item.clone(); + let syn::Type::TraitObject(syn::TypeTraitObject { bounds, .. }) = *ty else { + panic!("Expected a trait object"); + }; + + let index = 0; + let pallet = + Pallet::try_from(proc_macro2::Span::call_site(), &item, index, false, false, &bounds) + .unwrap(); + + assert_eq!(pallet.name.to_string(), "System"); + assert_eq!(pallet.index, index); + assert_eq!(pallet.path.to_token_stream().to_string(), "frame_system"); + assert_eq!(pallet.instance, None); +} + +#[test] +fn pallet_parsing_works_with_instance() { + use syn::{parse_quote, ItemType}; + + let item: ItemType = parse_quote! { + pub type System = frame_system + Call; + }; + let ItemType { ty, .. } = item.clone(); + let syn::Type::TraitObject(syn::TypeTraitObject { bounds, .. }) = *ty else { + panic!("Expected a trait object"); + }; + + let index = 0; + let pallet = + Pallet::try_from(proc_macro2::Span::call_site(), &item, index, false, false, &bounds) + .unwrap(); + + assert_eq!(pallet.name.to_string(), "System"); + assert_eq!(pallet.index, index); + assert_eq!(pallet.path.to_token_stream().to_string(), "frame_system"); + assert_eq!(pallet.instance, Some(parse_quote! { Instance1 })); +} + +#[test] +fn pallet_parsing_works_with_pallet() { + use syn::{parse_quote, ItemType}; + + let item: ItemType = parse_quote! { + pub type System = frame_system::Pallet + Call; + }; + let ItemType { ty, .. } = item.clone(); + let syn::Type::TraitObject(syn::TypeTraitObject { bounds, .. }) = *ty else { + panic!("Expected a trait object"); + }; + + let index = 0; + let pallet = + Pallet::try_from(proc_macro2::Span::call_site(), &item, index, false, false, &bounds) + .unwrap(); + + assert_eq!(pallet.name.to_string(), "System"); + assert_eq!(pallet.index, index); + assert_eq!(pallet.path.to_token_stream().to_string(), "frame_system"); + assert_eq!(pallet.instance, None); +} + +#[test] +fn pallet_parsing_works_with_instance_and_pallet() { + use syn::{parse_quote, ItemType}; + + let item: ItemType = parse_quote! { + pub type System = frame_system::Pallet + Call; + }; + let ItemType { ty, .. } = item.clone(); + let syn::Type::TraitObject(syn::TypeTraitObject { bounds, .. }) = *ty else { + panic!("Expected a trait object"); + }; + + let index = 0; + let pallet = + Pallet::try_from(proc_macro2::Span::call_site(), &item, index, false, false, &bounds) + .unwrap(); + + assert_eq!(pallet.name.to_string(), "System"); + assert_eq!(pallet.index, index); + assert_eq!(pallet.path.to_token_stream().to_string(), "frame_system"); + assert_eq!(pallet.instance, Some(parse_quote! { Instance1 })); +} From 91fb52eb73f5e478a33cd4045a691f79b581bfee Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:56:30 +0530 Subject: [PATCH 18/19] Makes taplo happy --- substrate/frame/support/procedural/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/procedural/Cargo.toml b/substrate/frame/support/procedural/Cargo.toml index cb803856aae25..fbb4da0177a4d 100644 --- a/substrate/frame/support/procedural/Cargo.toml +++ b/substrate/frame/support/procedural/Cargo.toml @@ -24,7 +24,7 @@ cfg-expr = { workspace = true } itertools = { workspace = true } proc-macro2 = { workspace = true } quote = { workspace = true } -syn = { features = ["full", "visit-mut", "parsing"], workspace = true } +syn = { features = ["full", "parsing", "visit-mut"], workspace = true } frame-support-procedural-tools = { workspace = true, default-features = true } macro_magic = { features = ["proc_support"], workspace = true } proc-macro-warning = { workspace = true } From df004886fd48073fab983c9809e4734bcaf7be44 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:57:37 +0530 Subject: [PATCH 19/19] FMT --- substrate/frame/support/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 8093a2d112bdb..138091689a59e 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -514,7 +514,7 @@ pub use frame_support_procedural::{ #[doc = docify::embed!("src/tests/runtime.rs", runtime_macro)] /// /// # Supported Attributes: -/// +/// /// ## Legacy Ordering /// /// An optional attribute can be defined as #[frame_support::runtime(legacy_ordering)] to