Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Add lockdown mode activation to lockdown-mode pallet's GenesisConfig (
Browse files Browse the repository at this point in the history
#176)

* add activating lockdown mode to GenesisConfig

* add activating lockdown mode to test ext

* add tests for activating lockdown mode in GenesisConfig

* cargo fmt -p pallet-lockdown-mode

* fix linting

* add activated to benchmark

* removing, as this is already in another test

* setting initial state to true, therefore no longer need to manually change state

* rename activated in genesisConfig to initial_status
  • Loading branch information
brunopgalvao authored May 24, 2023
1 parent 5035ce3 commit 727749c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pallets/lockdown-mode/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ benchmarks! {
assert_eq!(LockdownModeStatus::<T>::get(), ACTIVATED);
}

deactivate_lockdown_mode {
deactivate_lockdown_mode {
LockdownModeStatus::<T>::put(ACTIVATED);
}: deactivate_lockdown_mode(RawOrigin::Root)
verify {
assert_eq!(LockdownModeStatus::<T>::get(), DEACTIVATED);
}

impl_benchmark_test_suite!(LockdownMode, crate::mock::new_test_ext(), crate::mock::Test);
impl_benchmark_test_suite!(LockdownMode, crate::mock::new_test_ext(true), crate::mock::Test);
}
15 changes: 11 additions & 4 deletions pallets/lockdown-mode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ pub mod pallet {
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[derive(Default)]
#[pallet::genesis_config]
/// Genesis config for lockdown mode pallet
pub struct GenesisConfig {}
pub struct GenesisConfig {
pub initial_status: bool,
}

#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self { initial_status: ACTIVATED }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
LockdownModeStatus::<T>::put(ACTIVATED);
LockdownModeStatus::<T>::put(&self.initial_status);
}
}

Expand Down
12 changes: 8 additions & 4 deletions pallets/lockdown-mode/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate as pallet_lockdown_mode;
use cumulus_primitives_core::{relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler};
use frame_support::{
traits::{ConstU16, ConstU64, Contains},
traits::{ConstU16, ConstU64, Contains, GenesisBuild},
weights::Weight,
};
use frame_system as system;
Expand Down Expand Up @@ -119,8 +119,12 @@ impl pallet_lockdown_mode::Config for Test {
type WeightInfo = pallet_lockdown_mode::weights::SubstrateWeight<Test>;
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
pub fn new_test_ext(initial_status: bool) -> sp_io::TestExternalities {
let mut storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisBuild::<Test>::assimilate_storage(
&pallet_lockdown_mode::GenesisConfig { initial_status },
&mut storage,
)
.unwrap();
storage.into()
}
32 changes: 22 additions & 10 deletions pallets/lockdown-mode/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
use super::*;
use super::{GenesisConfig, *};
use crate::{mock::*, Error, ACTIVATED, DEACTIVATED};
use frame_support::{assert_noop, assert_ok, traits::Contains};
use pallet_balances::{self, Call as BalancesCall};
use pallet_remark::{self, Call as RemarkCall};

#[test]
fn genesis_config_default() {
let default_genesis = GenesisConfig::default();
assert_eq!(default_genesis.initial_status, ACTIVATED);
}

#[test]
fn genesis_config_initialized() {
[true, false].into_iter().for_each(|expected| {
new_test_ext(expected).execute_with(|| {
let lockdown_mode = LockdownModeStatus::<Test>::get();
assert_eq!(lockdown_mode, expected);
});
});
}

#[test]
fn activate_lockdown_mode_works() {
new_test_ext().execute_with(|| {
assert_eq!(LockdownModeStatus::<Test>::get(), DEACTIVATED);
new_test_ext(false).execute_with(|| {
assert_ok!(LockdownMode::activate_lockdown_mode(RuntimeOrigin::root()));

let lockdown_mode = LockdownModeStatus::<Test>::get();
Expand All @@ -22,10 +37,7 @@ fn activate_lockdown_mode_works() {

#[test]
fn deactivate_lockdown_mode_works() {
new_test_ext().execute_with(|| {
// We activate lockdown mode first so we can deactivate it.
assert_ok!(LockdownMode::activate_lockdown_mode(RuntimeOrigin::root()));

new_test_ext(true).execute_with(|| {
assert_ok!(LockdownMode::deactivate_lockdown_mode(RuntimeOrigin::root()));

let lockdown_mode = LockdownModeStatus::<Test>::get();
Expand All @@ -40,7 +52,7 @@ fn deactivate_lockdown_mode_works() {

#[test]
fn call_not_filtered_in_lockdown_mode() {
new_test_ext().execute_with(|| {
new_test_ext(false).execute_with(|| {
assert_ok!(LockdownMode::activate_lockdown_mode(RuntimeOrigin::root()));
let remark_call = RuntimeCall::Remark(RemarkCall::store { remark: vec![1, 2, 3] });
let result: bool = LockdownMode::contains(&remark_call);
Expand All @@ -50,7 +62,7 @@ fn call_not_filtered_in_lockdown_mode() {

#[test]
fn call_filtered_in_lockdown_mode() {
new_test_ext().execute_with(|| {
new_test_ext(false).execute_with(|| {
assert_ok!(LockdownMode::activate_lockdown_mode(RuntimeOrigin::root()));
let balance_call = RuntimeCall::Balance(BalancesCall::transfer { dest: 1, value: 2 });

Expand All @@ -61,7 +73,7 @@ fn call_filtered_in_lockdown_mode() {

#[test]
fn call_not_filtered_in_normal_mode() {
new_test_ext().execute_with(|| {
new_test_ext(false).execute_with(|| {
let lockdown_mode = LockdownModeStatus::<Test>::get();
assert_eq!(lockdown_mode, DEACTIVATED);
let balance_call = RuntimeCall::Balance(BalancesCall::transfer { dest: 1, value: 2 });
Expand Down

0 comments on commit 727749c

Please sign in to comment.