Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frame] #[pallet::composite_enum] improved variant count handling + removed pallet_balances's MaxHolds config #2657

Merged
merged 63 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
746732c
Test for MaxHolds
bkontur Dec 7, 2023
d1dba07
wip
bkontur Dec 13, 2023
535a883
Dedicated test for `composite_enum` and variant count
bkontur Dec 22, 2023
2e8a925
Fix `HoldReason` and `FreezeReason` variant count
bkontur Jan 4, 2024
b752f74
Fix macro for no composite enums
bkontur Jan 5, 2024
80c685c
impl `Get<u32>` for RuntimeFreezeReason/RuntimeHoldReason
bkontur Jan 5, 2024
a2b272a
Set all `MaxHolds = RuntimeHoldReason` and `MaxFreezes = RuntimeFreez…
bkontur Jan 5, 2024
694d230
".git/.scripts/commands/fmt/fmt.sh"
Jan 5, 2024
a0dd0a8
Docs fix
bkontur Jan 5, 2024
71156bb
Update substrate/frame/support/procedural/src/lib.rs
bkontur Jan 8, 2024
2f3c31c
Fix other places with Basti's suggestion
bkontur Jan 8, 2024
6d6e75c
Update substrate/frame/support/procedural/src/lib.rs
bkontur Jan 8, 2024
b84cc1b
Update substrate/frame/support/procedural/src/lib.rs
bkontur Jan 8, 2024
4f5c9c8
Update substrate/frame/support/procedural/src/lib.rs
bkontur Jan 8, 2024
b30e17e
fix
bkontur Jan 8, 2024
b38ddb2
Update substrate/frame/support/procedural/src/construct_runtime/expan…
bkontur Jan 8, 2024
8d9bdf0
Update substrate/frame/support/procedural/src/construct_runtime/expan…
bkontur Jan 8, 2024
9bde28f
`pallet::balances` replace `MaxHolds` with `VariantCountOf<T::Runtime…
bkontur Jan 8, 2024
f759563
Removed unnecessery `impl #scrate::traits::Get<u32> for Runtime*Reason`
bkontur Jan 16, 2024
635724e
Removed `MaxHolds` from everywhere
bkontur Jan 17, 2024
a32b0b0
Fix after removed `Get` impl
bkontur Jan 17, 2024
f43412c
Removed derive macro `CompositeEnumVariantCount` and replace with exp…
bkontur Jan 17, 2024
248790a
fmt
bkontur Jan 17, 2024
9faefd8
One more revert for `MaxFreezes`
bkontur Jan 17, 2024
3c81275
Add additional check for composite enums
bkontur Jan 17, 2024
9b8aa8e
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 17, 2024
32cf3ff
Fix for multi-instance pallets when `InstanceXYZ` is not specified in…
bkontur Jan 18, 2024
3a4a1ce
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 18, 2024
bccc074
Fix preimage tests
bkontur Jan 18, 2024
ba7df0b
prdoc
bkontur Jan 19, 2024
6778282
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 19, 2024
0959bea
prdoc - change audience
bkontur Jan 19, 2024
c637145
prdoc one more time
bkontur Jan 19, 2024
9eca694
removed empty line
bkontur Jan 19, 2024
44b0827
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 19, 2024
f4cfc2c
Hit me, baby, one more time...
bkontur Jan 19, 2024
5a1b161
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 24, 2024
d557601
Update substrate/frame/support/procedural/src/pallet/expand/mod.rs
bkontur Jan 24, 2024
48c3d37
Update substrate/frame/support/procedural/src/pallet/expand/mod.rs
bkontur Jan 24, 2024
442f3fd
Update prdoc/pr_2657.prdoc
bkontur Jan 24, 2024
abaf487
Update substrate/frame/support/test/tests/composite_enum.rs
bkontur Jan 24, 2024
2358636
Merge remote-tracking branch 'origin/bko-max-holds-integrity-test' in…
bkontur Jan 24, 2024
7d113e9
Removed unnecessary indexes from `construct_runtime`
bkontur Jan 24, 2024
6a0289a
".git/.scripts/commands/fmt/fmt.sh"
Jan 24, 2024
0cb363a
Fmt? struct vs enum?
bkontur Jan 24, 2024
83e64fe
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Jan 24, 2024
4342247
".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtim…
Jan 24, 2024
71c55c7
".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtim…
Jan 24, 2024
5af90e7
Merge branch 'master' into bko-max-holds-integrity-test
bkontur Jan 24, 2024
464af0c
Update prdoc/pr_2657.prdoc
bkontur Jan 26, 2024
4015ea0
PR review fixes
bkontur Jan 26, 2024
c869313
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 26, 2024
77066c8
Merge branch 'master' into bko-max-holds-integrity-test
bkontur Jan 30, 2024
5380371
Merge branch 'master' into bko-max-holds-integrity-test
bkontur Jan 30, 2024
01c39bf
Merge remote-tracking branch 'origin/master' into bko-max-holds-integ…
bkontur Jan 30, 2024
0ce9eca
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
3aeb0f8
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Jan 31, 2024
998ed0f
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
53b2f44
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
e6552cc
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
2d6a3c4
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
27a5723
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
adbcace
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
Jan 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
// We allow each account to have holds on it from:
// - `NftFractionalization`: 1
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
// We allow each account to have holds on it from:
// - `NftFractionalization`: 1
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/people/people-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/people/people-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/testing/penpal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down
8 changes: 4 additions & 4 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ impl pallet_balances::Config for Runtime {
type ReserveIdentifier = [u8; 8];
type WeightInfo = weights::pallet_balances::WeightInfo<Runtime>;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<1>;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type MaxHolds = ConstU32<2>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down Expand Up @@ -1163,8 +1163,8 @@ impl pallet_balances::Config<NisCounterpartInstance> for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<2>;
type MaxFreezes = ConstU32<1>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = ConstU32<1>;
type MaxHolds = ConstU32<1>;
type MaxHolds = RuntimeHoldReason;
type MaxFreezes = RuntimeFreezeReason;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,21 @@ pub(crate) fn expand_variant(
}
}
}

pub(crate) fn expand_variant_count(
composite_name: &str,
path: &PalletPath,
instance: Option<&Ident>,
) -> TokenStream {
let composite_name = quote::format_ident!("{}", composite_name);

if let Some(inst) = instance {
quote! {
#path::#composite_name::<#path::#inst>::VARIANT_COUNT
}
} else {
quote! {
#path::#composite_name::VARIANT_COUNT
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use quote::quote;
pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -> TokenStream {
let mut conversion_fns = Vec::new();
let mut freeze_reason_variants = Vec::new();
let mut freeze_reason_variants_count = Vec::new();
for decl in pallet_decls {
if let Some(_) = decl.find_part("FreezeReason") {
let variant_name = &decl.name;
Expand All @@ -44,9 +45,18 @@ pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream)
instance,
variant_name,
));

freeze_reason_variants_count.push(composite_helper::expand_variant_count(
"FreezeReason",
path,
instance,
));
}
}
let freeze_reason_variants_count = freeze_reason_variants.len() as u32;

if freeze_reason_variants_count.is_empty() {
freeze_reason_variants_count.push(quote! { 0 })
}

quote! {
/// A reason for placing a freeze on funds.
Expand All @@ -61,7 +71,14 @@ pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream)
}

impl #scrate::traits::VariantCount for RuntimeFreezeReason {
const VARIANT_COUNT: u32 = #freeze_reason_variants_count;
const VARIANT_COUNT: u32 = #( #freeze_reason_variants_count )+*;
}

/// Implementation of `Get<u32>` which returns `RuntimeFreezeReason::VARIANT_COUNT`.
impl #scrate::traits::Get<u32> for RuntimeFreezeReason {
fn get() -> u32 {
<RuntimeFreezeReason as #scrate::traits::VariantCount>::VARIANT_COUNT
}
}

#( #conversion_fns )*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use quote::quote;
pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -> TokenStream {
let mut conversion_fns = Vec::new();
let mut hold_reason_variants = Vec::new();
let mut hold_reason_variants_count = Vec::new();
for decl in pallet_decls {
if let Some(_) = decl.find_part("HoldReason") {
let variant_name = &decl.name;
Expand All @@ -44,9 +45,18 @@ pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -
instance,
variant_name,
));

hold_reason_variants_count.push(composite_helper::expand_variant_count(
"HoldReason",
path,
instance,
));
}
}
let hold_reason_variants_count = hold_reason_variants.len() as u32;

if hold_reason_variants_count.is_empty() {
hold_reason_variants_count.push(quote! { 0 })
}
bkontur marked this conversation as resolved.
Show resolved Hide resolved

quote! {
/// A reason for placing a hold on funds.
Expand All @@ -61,7 +71,14 @@ pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -
}

impl #scrate::traits::VariantCount for RuntimeHoldReason {
const VARIANT_COUNT: u32 = #hold_reason_variants_count;
const VARIANT_COUNT: u32 = #( #hold_reason_variants_count )+*;
bkontur marked this conversation as resolved.
Show resolved Hide resolved
}

/// Implementation of `Get<u32>` which returns `RuntimeHoldReason::VARIANT_COUNT`.
impl #scrate::traits::Get<u32> for RuntimeHoldReason {
fn get() -> u32 {
<RuntimeHoldReason as #scrate::traits::VariantCount>::VARIANT_COUNT
}
}
bkontur marked this conversation as resolved.
Show resolved Hide resolved

#( #conversion_fns )*
Expand Down
67 changes: 65 additions & 2 deletions substrate/frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,18 +1506,81 @@ pub fn origin(_: TokenStream, _: TokenStream) -> TokenStream {
///
/// ```ignore
/// Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo,
/// RuntimeDebug
/// RuntimeDebug,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// RuntimeDebug,
/// RuntimeDebug

/// CompositeEnumVariantCount,
/// ```
///
/// For ease of usage, when no `#[derive]` attributes are found for the enum under
/// `#[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.
/// will automatically be derived for it (this implies that you need to provide the
/// `frame_support::traits::VariantCount` implementation).
#[proc_macro_attribute]
pub fn composite_enum(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
}

/// Derive macro for implementing `frame_support::traits::VariantCount` for enum type.
///
/// This macro works only for the enum which does not have variants or variants do not have fields.
///
/// This macro is automatically derived for `#[pallet::composite_enum]` enums.
#[proc_macro_derive(CompositeEnumVariantCount)]
bkontur marked this conversation as resolved.
Show resolved Hide resolved
pub fn derive_composite_enum_variant_count(input: TokenStream) -> TokenStream {
let input: syn::DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
bkontur marked this conversation as resolved.
Show resolved Hide resolved

// only `Enum` type is expected here (see also `CompositeDef::try_from`)
let data_enum = match &input.data {
syn::Data::Enum(ref data_enum) => data_enum,
_ =>
return syn::Error::new(input.ident.span(), "Invalid type: expected enum item")
.to_compile_error()
.into(),
};
bkontur marked this conversation as resolved.
Show resolved Hide resolved

// check variants: composite enums support only field-less enum variants. This is because fields
// can introduce too many possibilities, making it challenging to compute a fixed variant count.
// The only exception is `__Ignore` which can be added by `CompositeDef::try_from`.
for variant in &data_enum.variants {
match variant.fields {
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
let variant_name = variant.ident.to_string();
if variant_name != "__Ignore" {
bkontur marked this conversation as resolved.
Show resolved Hide resolved
return syn::Error::new(
variant.ident.span(),
"The composite enum does not support variants with fields!",
)
.to_compile_error()
.into()
}
},
syn::Fields::Unit => (),
}
}
// get variants count
let variants_count = data_enum.variants.len() as u32;

// add `VariantCount` implementation for composite enum
let frame_support = match generate_access_from_frame_or_crate("frame-support") {
Ok(c) => c,
Err(e) => return e.into_compile_error().into(),
};
let frame_support = &frame_support;
bkontur marked this conversation as resolved.
Show resolved Hide resolved

let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

quote! {
impl #impl_generics #frame_support::traits::VariantCount for #name #ty_generics #where_clause {
const VARIANT_COUNT: u32 = #variants_count;
}
}
.into()
}

///
/// ---
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl CompositeDef {
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
#scrate::CompositeEnumVariantCount,
)]
};
item.attrs.push(derive_attr);
Expand Down
Loading
Loading