Skip to content
Snippets Groups Projects
Unverified Commit ca0fb0d9 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

pallet_balances: Add `try_state` for checking `Holds` and `Freezes` (#4490)

Co-authored-by: command-bot <>
parent 2c48b9dd
No related merge requests found
Pipeline #475726 waiting for manual action with stages
in 7 minutes and 10 seconds
......@@ -542,8 +542,8 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
#[cfg(not(feature = "insecure_zero_ed"))]
fn integrity_test() {
#[cfg(not(feature = "insecure_zero_ed"))]
assert!(
!<T as Config<I>>::ExistentialDeposit::get().is_zero(),
"The existential deposit must be greater than zero!"
......@@ -555,6 +555,29 @@ pub mod pallet {
T::MaxFreezes::get(), <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
);
}
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
Holds::<T, I>::iter_keys().try_for_each(|k| {
if Holds::<T, I>::decode_len(k).unwrap_or(0) >
T::RuntimeHoldReason::VARIANT_COUNT as usize
{
Err("Found `Hold` with too many elements")
} else {
Ok(())
}
})?;
Freezes::<T, I>::iter_keys().try_for_each(|k| {
if Freezes::<T, I>::decode_len(k).unwrap_or(0) > T::MaxFreezes::get() as usize {
Err("Found `Freeze` with too many elements")
} else {
Ok(())
}
})?;
Ok(())
}
}
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
......
......@@ -109,3 +109,35 @@ fn regression_historic_acc_does_not_evaporate_reserve() {
});
});
}
#[cfg(feature = "try-runtime")]
#[test]
fn try_state_works() {
use crate::{Config, Freezes, Holds};
use frame_support::{
storage,
traits::{Get, Hooks, VariantCount},
};
ExtBuilder::default().build_and_execute_with(|| {
storage::unhashed::put(
&Holds::<Test>::hashed_key_for(1),
&vec![0u8; <Test as Config>::RuntimeHoldReason::VARIANT_COUNT as usize + 1],
);
assert!(format!("{:?}", Balances::try_state(0).unwrap_err())
.contains("Found `Hold` with too many elements"));
});
ExtBuilder::default().build_and_execute_with(|| {
let max_freezes: u32 = <Test as Config>::MaxFreezes::get();
storage::unhashed::put(
&Freezes::<Test>::hashed_key_for(1),
&vec![0u8; max_freezes as usize + 1],
);
assert!(format!("{:?}", Balances::try_state(0).unwrap_err())
.contains("Found `Freeze` with too many elements"));
});
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment