Run integrity_test in Externalities (#14546)

* Run integrity_test in RO externalities

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* frame-support: Export RO externalities

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix bench tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename to __private

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Run in TestExternalities

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix other pallets

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fixes

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/src/dispatch.rs

Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>

* Fixup merge

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: parity-processbot <>
Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>
This commit is contained in:
Oliver Tale-Yazdi
2023-07-18 12:10:56 +02:00
committed by GitHub
parent 26d8e65910
commit 5b89f47df2
7 changed files with 37 additions and 47 deletions
+6 -10
View File
@@ -282,16 +282,12 @@ pub mod pallet {
} }
fn integrity_test() { fn integrity_test() {
sp_std::if_std! { // Ensure that the value of `ErasToCheckPerBlock` is less or equal to
sp_io::TestExternalities::new_empty().execute_with(|| { // `T::MaxErasToCheckPerBlock`.
// ensure that the value of `ErasToCheckPerBlock` is less than assert!(
// `T::MaxErasToCheckPerBlock`. ErasToCheckPerBlock::<T>::get() <= T::MaxErasToCheckPerBlock::get(),
assert!( "the value of `ErasToCheckPerBlock` is greater than `T::MaxErasToCheckPerBlock`",
ErasToCheckPerBlock::<T>::get() <= T::MaxErasToCheckPerBlock::get(), );
"the value of `ErasToCheckPerBlock` is greater than `T::MaxErasToCheckPerBlock`",
);
});
}
} }
#[cfg(feature = "try-runtime")] #[cfg(feature = "try-runtime")]
+6 -10
View File
@@ -794,16 +794,12 @@ pub mod pallet {
<T::GenesisElectionProvider as ElectionProviderBase>::MaxWinners::get() <T::GenesisElectionProvider as ElectionProviderBase>::MaxWinners::get()
); );
sp_std::if_std! { assert!(
sp_io::TestExternalities::new_empty().execute_with(|| T::SlashDeferDuration::get() < T::BondingDuration::get() || T::BondingDuration::get() == 0,
assert!( "As per documentation, slash defer duration ({}) should be less than bonding duration ({}).",
T::SlashDeferDuration::get() < T::BondingDuration::get() || T::BondingDuration::get() == 0, T::SlashDeferDuration::get(),
"As per documentation, slash defer duration ({}) should be less than bonding duration ({}).", T::BondingDuration::get(),
T::SlashDeferDuration::get(), )
T::BondingDuration::get(),
)
);
}
} }
#[cfg(feature = "try-runtime")] #[cfg(feature = "try-runtime")]
@@ -251,11 +251,13 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
for #pallet_ident<#type_use_gen> #where_clause for #pallet_ident<#type_use_gen> #where_clause
{ {
fn integrity_test() { fn integrity_test() {
< #frame_support::sp_io::TestExternalities::default().execute_with(|| {
Self as #frame_support::traits::Hooks< <
#frame_system::pallet_prelude::BlockNumberFor::<T> Self as #frame_support::traits::Hooks<
> #frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::integrity_test() >::integrity_test()
});
} }
} }
} }
+2 -2
View File
@@ -401,8 +401,8 @@ pub trait Hooks<BlockNumber> {
/// `type Bar: Get<u32>` where `Foo::get()` must always be greater than `Bar::get()`, such /// `type Bar: Get<u32>` where `Foo::get()` must always be greater than `Bar::get()`, such
/// checks can be asserted upon here. /// checks can be asserted upon here.
/// ///
/// Note that this hook is not executed in an externality environment, so if access to state is /// Note that this hook is executed in an externality environment, provided by
/// needed, the code should be wrapped in `sp_io::TestExternalities`. /// `sp_io::TestExternalities`. This makes it possible to access the storage.
fn integrity_test() {} fn integrity_test() {}
} }
@@ -18,6 +18,7 @@
// Benchmarks for Utility Pallet // Benchmarks for Utility Pallet
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
#![cfg(feature = "runtime-benchmarks")]
use codec::Encode; use codec::Encode;
use frame_benchmarking::{ use frame_benchmarking::{
+1 -3
View File
@@ -390,9 +390,7 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
#[cfg(feature = "std")] #[cfg(feature = "std")]
fn integrity_test() { fn integrity_test() {
sp_io::TestExternalities::default().execute_with(|| { T::BlockWeights::get().validate().expect("The weights are invalid.");
T::BlockWeights::get().validate().expect("The weights are invalid.");
});
} }
} }
+15 -18
View File
@@ -416,6 +416,7 @@ pub mod pallet {
}); });
} }
#[cfg(feature = "std")]
fn integrity_test() { fn integrity_test() {
// given weight == u64, we build multipliers from `diff` of two weight values, which can // given weight == u64, we build multipliers from `diff` of two weight values, which can
// at most be maximum block weight. Make sure that this can fit in a multiplier without // at most be maximum block weight. Make sure that this can fit in a multiplier without
@@ -441,25 +442,21 @@ pub mod pallet {
return return
} }
#[cfg(any(feature = "std", test))] // This is the minimum value of the multiplier. Make sure that if we collapse to this
sp_io::TestExternalities::new_empty().execute_with(|| { // value, we can recover with a reasonable amount of traffic. For this test we assert
// This is the minimum value of the multiplier. Make sure that if we collapse to // that if we collapse to minimum, the trend will be positive with a weight value which
// this value, we can recover with a reasonable amount of traffic. For this test we // is 1% more than the target.
// assert that if we collapse to minimum, the trend will be positive with a weight let min_value = T::FeeMultiplierUpdate::min();
// value which is 1% more than the target. let target = target + addition;
let min_value = T::FeeMultiplierUpdate::min();
let target = target + addition; <frame_system::Pallet<T>>::set_block_consumed_resources(target, 0);
let next = T::FeeMultiplierUpdate::convert(min_value);
<frame_system::Pallet<T>>::set_block_consumed_resources(target, 0); assert!(
let next = T::FeeMultiplierUpdate::convert(min_value); next > min_value,
assert!( "The minimum bound of the multiplier is too low. When \
next > min_value, block saturation is more than target by 1% and multiplier is minimal then \
"The minimum bound of the multiplier is too low. When \ the multiplier doesn't increase."
block saturation is more than target by 1% and multiplier is minimal then \ );
the multiplier doesn't increase."
);
});
} }
} }
} }