Skip to content

Commit

Permalink
Replace global static mut with static parameter_types!
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed Jul 15, 2024
1 parent 56e91c7 commit a87c594
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions substrate/frame/executive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ const TEST_KEY: &[u8] = b":test:key:";

#[cfg(feature = "try-runtime")]
mod try_runtime {
use frame_support::traits::{IdentifiableTryStateLogic, TryStateLogic};
use frame_support::{
parameter_types,
traits::{IdentifiableTryStateLogic, TryStateLogic},
};
use sp_runtime::traits::AtLeast32BitUnsigned;

// Will contain `true` when the custom runtime logic is called.
pub(super) static mut CANARY_FLAG: bool = false;
parameter_types! {
pub(super) static CanaryFlag: bool = false;
}

const CUSTOM_TRY_STATE_ID: &[u8] = b"custom_try_state";

Expand All @@ -61,9 +66,7 @@ mod try_runtime {
BlockNumber: Clone + sp_std::fmt::Debug + AtLeast32BitUnsigned,
{
fn try_state(_: BlockNumber) -> Result<(), sp_runtime::TryRuntimeError> {
unsafe {
CANARY_FLAG = true;
}
CanaryFlag::set(true);
Ok(())
}
}
Expand Down Expand Up @@ -116,12 +119,10 @@ mod custom {
// Verify that `CustomTryState` has been called before.
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
unsafe {
assert!(
try_runtime::CANARY_FLAG,
"Custom `try-runtime` did not run before pallet `try-runtime`."
);
}
assert!(
try_runtime::CanaryFlag::get(),
"Custom `try-runtime` did not run before pallet `try-runtime`."
);
Ok(())
}
}
Expand Down Expand Up @@ -577,9 +578,7 @@ fn new_test_ext(balance_factor: Balance) -> sp_io::TestExternalities {
});

#[cfg(feature = "try-runtime")]
unsafe {
try_runtime::CANARY_FLAG = false;
}
try_runtime::CanaryFlag::set(false);

ext
}
Expand Down

0 comments on commit a87c594

Please sign in to comment.