From 857a896d55be8c1cb1a650ed37029861e2d0deea Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 5 Apr 2023 13:23:22 +0800 Subject: [PATCH 1/4] Add HoldReason to the NIS pallet --- frame/nis/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/frame/nis/src/lib.rs b/frame/nis/src/lib.rs index 539011c5149d0..34ccce004f022 100644 --- a/frame/nis/src/lib.rs +++ b/frame/nis/src/lib.rs @@ -482,6 +482,14 @@ pub mod pallet { AlreadyPrivate, } + /// A reason for the NIS pallet placing a hold on funds. + #[pallet::composable_enum] + pub enum HoldReason { + /// The NIS Pallet has reserved it for a non-fungible receipt. + #[codec(index = 0)] + NftReceipt, + } + pub(crate) struct WeightCounter { pub(crate) used: Weight, pub(crate) limit: Weight, From 1e1aa87867fed579384738fd08568908b288e639 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 5 Apr 2023 13:30:35 +0800 Subject: [PATCH 2/4] Rename composable_enum to composite_enum --- frame/nis/src/lib.rs | 2 +- frame/support/procedural/src/lib.rs | 6 +++--- frame/support/src/lib.rs | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/frame/nis/src/lib.rs b/frame/nis/src/lib.rs index 34ccce004f022..9cac5f4b616e7 100644 --- a/frame/nis/src/lib.rs +++ b/frame/nis/src/lib.rs @@ -483,7 +483,7 @@ pub mod pallet { } /// A reason for the NIS pallet placing a hold on funds. - #[pallet::composable_enum] + #[pallet::composite_enum] pub enum HoldReason { /// The NIS Pallet has reserved it for a non-fungible receipt. #[codec(index = 0)] diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index dea2d4943de00..ea997752c3a53 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -1412,7 +1412,7 @@ pub fn origin(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } -/// The `#[pallet::composable_enum]` attribute allows you to define an enum that gets composed as an +/// The `#[pallet::composite_enum]` attribute allows you to define an enum that gets composed as an /// aggregate enum by `construct_runtime`. This is similar in principle with `#[pallet::event]` and /// `#[pallet::error]`. /// @@ -1431,10 +1431,10 @@ pub fn origin(_: TokenStream, _: TokenStream) -> TokenStream { /// ``` /// /// For ease of usage, when no `#[derive]` attributes are found for the enum under -/// `#[pallet::composable_enum]`, the aforementioned traits are automatically derived for it. The +/// `#[pallet::composite_enum]`, the aforementioned traits are automatically derived for it. The /// inverse is also true: if there are any `#[derive]` attributes found for the enum, then no traits /// will automatically be derived for it. #[proc_macro_attribute] -pub fn composable_enum(_: TokenStream, _: TokenStream) -> TokenStream { +pub fn composite_enum(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index e0ebeb68a3eaf..916a8696ed678 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -1552,7 +1552,7 @@ pub mod pallet_prelude { /// * [`pallet::inherent`](#inherent-palletinherent-optional) /// * [`pallet::validate_unsigned`](#validate-unsigned-palletvalidate_unsigned-optional) /// * [`pallet::origin`](#origin-palletorigin-optional) -/// * [`pallet::composable_enum`](#composable-enum-palletcomposable_enum-optional) +/// * [`pallet::composite_enum`](#composite-enum-palletcomposite_enum-optional) /// /// Note that at compile-time, the `#[pallet]` macro will analyze and expand all of these /// attributes, ultimately removing their AST nodes before they can be parsed as real @@ -2278,19 +2278,19 @@ pub mod pallet_prelude { /// /// Also see [`pallet::origin`](`frame_support::pallet_macros::origin`) /// -/// # Composable enum `#[pallet::composable_enum]` (optional) +/// # Composite enum `#[pallet::composite_enum]` (optional) /// -/// The `#[pallet::composable_enum]` attribute allows you to define an enum on the pallet which +/// The `#[pallet::composite_enum]` attribute allows you to define an enum on the pallet which /// will then instruct `construct_runtime` to amalgamate all similarly-named enums from other /// pallets into an aggregate enum. This is similar in principle with how the aggregate enum is /// generated for `#[pallet::event]` or `#[pallet::error]`. /// -/// The item tagged with `#[pallet::composable_enum]` MUST be an enum declaration, and can ONLY +/// The item tagged with `#[pallet::composite_enum]` MUST be an enum declaration, and can ONLY /// be the following identifiers: `FreezeReason`, `HoldReason`, `LockId` or `SlashReason`. /// Custom identifiers are not supported. /// /// NOTE: For ease of usage, when no `#[derive]` attributes are detected, the -/// `#[pallet::composable_enum]` attribute will automatically derive the following traits for +/// `#[pallet::composite_enum]` attribute will automatically derive the following traits for /// the enum: /// /// ```ignore @@ -2832,7 +2832,7 @@ pub use frame_support_procedural::pallet; /// Contains macro stubs for all of the pallet:: macros pub mod pallet_macros { pub use frame_support_procedural::{ - call_index, compact, composable_enum, config, constant, + call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, generate_storage_info, generate_store, genesis_build, genesis_config, getter, hooks, inherent, origin, storage, storage_prefix, storage_version, type_value, unbounded, From cbab7a2cc8f58e37edfd3c73c8b083d2c8448758 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 5 Apr 2023 13:33:55 +0800 Subject: [PATCH 3/4] Add encoding test --- frame/support/test/tests/pallet.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 37f1219a800f5..11f82c4c0d266 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -991,6 +991,8 @@ fn validate_unsigned_expand() { #[test] fn composite_expand() { + use codec::Encode; + let hold_reason: RuntimeHoldReason = pallet::HoldReason::Staking.into(); let hold_reason2: RuntimeHoldReason = pallet2::HoldReason::Governance.into(); let slash_reason: RuntimeSlashReason = pallet2::SlashReason::Equivocation.into(); @@ -998,6 +1000,10 @@ fn composite_expand() { assert_eq!(hold_reason, RuntimeHoldReason::Example(pallet::HoldReason::Staking)); assert_eq!(hold_reason2, RuntimeHoldReason::Example2(pallet2::HoldReason::Governance)); assert_eq!(slash_reason, RuntimeSlashReason::Example2(pallet2::SlashReason::Equivocation)); + + assert_eq!(hold_reason.encode(), [1, 0]); + assert_eq!(hold_reason2.encode(), [2, 0]); + assert_eq!(slash_reason.encode(), [2, 0]); } #[test] From 2ee4a9050bbdfc731c5785784db78b24c9c63a7d Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 5 Apr 2023 13:47:22 +0800 Subject: [PATCH 4/4] Add more doc comments --- .../procedural/src/construct_runtime/expand/freeze_reason.rs | 1 + .../procedural/src/construct_runtime/expand/hold_reason.rs | 1 + frame/support/procedural/src/construct_runtime/expand/lock_id.rs | 1 + .../procedural/src/construct_runtime/expand/slash_reason.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/frame/support/procedural/src/construct_runtime/expand/freeze_reason.rs b/frame/support/procedural/src/construct_runtime/expand/freeze_reason.rs index 889168bf6c215..d357fab72115b 100644 --- a/frame/support/procedural/src/construct_runtime/expand/freeze_reason.rs +++ b/frame/support/procedural/src/construct_runtime/expand/freeze_reason.rs @@ -35,6 +35,7 @@ pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream) } quote! { + /// A reason for placing a freeze on funds. #[derive( Copy, Clone, Eq, PartialEq, Ord, PartialOrd, #scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen, diff --git a/frame/support/procedural/src/construct_runtime/expand/hold_reason.rs b/frame/support/procedural/src/construct_runtime/expand/hold_reason.rs index 60fc094a4d5e4..6acfe5382f18a 100644 --- a/frame/support/procedural/src/construct_runtime/expand/hold_reason.rs +++ b/frame/support/procedural/src/construct_runtime/expand/hold_reason.rs @@ -35,6 +35,7 @@ pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) - } quote! { + /// A reason for placing a hold on funds. #[derive( Copy, Clone, Eq, PartialEq, Ord, PartialOrd, #scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen, diff --git a/frame/support/procedural/src/construct_runtime/expand/lock_id.rs b/frame/support/procedural/src/construct_runtime/expand/lock_id.rs index 77c6fefa0aabd..d7031dcaf552b 100644 --- a/frame/support/procedural/src/construct_runtime/expand/lock_id.rs +++ b/frame/support/procedural/src/construct_runtime/expand/lock_id.rs @@ -35,6 +35,7 @@ pub fn expand_outer_lock_id(pallet_decls: &[Pallet], scrate: &TokenStream) -> To } quote! { + /// An identifier for each lock placed on funds. #[derive( Copy, Clone, Eq, PartialEq, Ord, PartialOrd, #scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen, diff --git a/frame/support/procedural/src/construct_runtime/expand/slash_reason.rs b/frame/support/procedural/src/construct_runtime/expand/slash_reason.rs index 2bf647aa7c1f4..abffb97d43981 100644 --- a/frame/support/procedural/src/construct_runtime/expand/slash_reason.rs +++ b/frame/support/procedural/src/construct_runtime/expand/slash_reason.rs @@ -35,6 +35,7 @@ pub fn expand_outer_slash_reason(pallet_decls: &[Pallet], scrate: &TokenStream) } quote! { + /// A reason for slashing funds. #[derive( Copy, Clone, Eq, PartialEq, Ord, PartialOrd, #scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen,