Skip to content

Commit

Permalink
Fix macro for no composite enums
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Jan 5, 2024
1 parent 2e8a925 commit b752f74
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 44 deletions.
38 changes: 0 additions & 38 deletions polkadot/runtime/rococo/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,42 +60,4 @@ mod encoding_tests {
fn nis_hold_reason_encoding_is_correct() {
assert_eq!(RuntimeHoldReason::Nis(pallet_nis::HoldReason::NftReceipt).encode(), [38, 0]);
}
}

#[test]
fn runtime_hold_reason_vs_max_holds() {
use frame_support::assert_ok;
use frame_support::traits::Get;
use frame_support::traits::fungible::MutateHold;
use frame_support::traits::VariantCount;

sp_io::TestExternalities::default().execute_with(|| {
let account = AccountId::from([0; 32]);
let u = ExistentialDeposit::get();
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), account.clone().into(), u * 20));

let assert_holds = |a, expected_count| {
assert_eq!(expected_count, pallet_balances::Holds::<Runtime, ()>::get(a).len());
};

let balances_max_holds: u32 = <Runtime as pallet_balances::Config<()>>::MaxHolds::get();
let nis_max_holds: u32 = <Runtime as pallet_balances::Config<NisCounterpartInstance>>::MaxHolds::get();
let runtime_hold_reason_variant_count = <RuntimeHoldReason as VariantCount>::VARIANT_COUNT;

println!("Balances max_holds: {:?}", balances_max_holds);
println!("Nis max_holds: {:?}", nis_max_holds);
println!("RuntimeHoldReason variant_count: {:?}", runtime_hold_reason_variant_count);

// hold for all reasons
assert_ok!(Balances::hold(&RuntimeHoldReason::Nis(pallet_nis::HoldReason::NftReceipt).into(), &account, u));
assert_holds(&account, 1);
assert_ok!(Balances::hold(&RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage).into(), &account, u));
assert_holds(&account, 2);
assert_ok!(Balances::hold(&RuntimeHoldReason::StateTrieMigration(pallet_state_trie_migration::HoldReason::SlashForContinueMigrate).into(), &account, u));
assert_holds(&account, 3);
assert_ok!(Balances::hold(&RuntimeHoldReason::StateTrieMigration(pallet_state_trie_migration::HoldReason::SlashForMigrateCustomTop).into(), &account, u));
assert_holds(&account, 4);
assert_ok!(Balances::hold(&RuntimeHoldReason::StateTrieMigration(pallet_state_trie_migration::HoldReason::SlashForMigrateCustomChild).into(), &account, u));
assert_holds(&account, 5);
})
}
2 changes: 0 additions & 2 deletions substrate/frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ frame-support = { path = "../support", default-features = false }
frame-system = { path = "../system", default-features = false }
sp-runtime = { path = "../../primitives/runtime", default-features = false }
sp-std = { path = "../../primitives/std", default-features = false }
sp-version = { path = "../../primitives/version", default-features = false }

[dev-dependencies]
pallet-transaction-payment = { path = "../transaction-payment" }
Expand All @@ -46,7 +45,6 @@ std = [
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"sp-version/std",
]
# Enable support for setting the existential deposit to zero.
insecure_zero_ed = []
Expand Down
4 changes: 0 additions & 4 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,6 @@ pub mod pallet {
"The existential deposit must be greater than zero!"
);

let runtime: sp_version::RuntimeVersion = <T as frame_system::Config>::Version::get();
let max_holds: u32 = T::MaxHolds::get();
let hold_reason_variant_count: u32 = <T::RuntimeHoldReason as VariantCount>::VARIANT_COUNT;
log::error!("Runtime: {:?} - T::MaxHolds({:?} - hold_reason_variant_count({:?})", runtime, max_holds, hold_reason_variant_count);
assert!(
T::MaxHolds::get() >= <T::RuntimeHoldReason as VariantCount>::VARIANT_COUNT,
"MaxHolds should be greater than or equal to the number of hold reasons: {} < {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream)
}
}

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

quote! {
/// A reason for placing a freeze on funds.
#[derive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -
}
}

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

quote! {
/// A reason for placing a hold on funds.
#[derive(
Expand Down

0 comments on commit b752f74

Please sign in to comment.