Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet safe mode to umbrella crate #6905

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 7 additions & 25 deletions substrate/frame/safe-mode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,36 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { features = ["derive"], workspace = true }
docify = { workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
scale-info = { features = ["derive"], workspace = true }
pallet-balances = { optional = true, workspace = true }
pallet-proxy = { optional = true, workspace = true }
pallet-utility = { optional = true, workspace = true }
scale-info = { features = ["derive"], workspace = true }
sp-arithmetic = { workspace = true }
sp-runtime = { workspace = true }
pallet-proxy = { optional = true, workspace = true }

[dev-dependencies]
frame-support = { features = ["experimental"], workspace = true, default-features = true }
pallet-balances = { workspace = true, default-features = true }
pallet-proxy = { workspace = true, default-features = true }
pallet-utility = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
pallet-proxy = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"pallet-balances?/std",
"pallet-proxy?/std",
"pallet-utility?/std",
"scale-info/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-balances?/try-runtime",
"pallet-proxy?/try-runtime",
"pallet-utility?/try-runtime",
"sp-runtime/try-runtime",
]
35 changes: 18 additions & 17 deletions substrate/frame/safe-mode/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
#![cfg(feature = "runtime-benchmarks")]

use super::{Pallet as SafeMode, *};

use frame_benchmarking::v2::*;
use frame_support::traits::{fungible, UnfilteredDispatchable};
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::{Bounded, One, Zero};
use frame::{benchmarking::prelude::*, traits::UnfilteredDispatchable};

#[benchmarks(where T::Currency: fungible::Mutate<T::AccountId>)]
mod benchmarks {
Expand Down Expand Up @@ -65,7 +61,7 @@ mod benchmarks {

assert_eq!(
EnteredUntil::<T>::get().unwrap(),
System::<T>::block_number() + T::EnterDuration::get()
frame_system::Pallet::<T>::block_number() + T::EnterDuration::get()
);
Ok(())
}
Expand All @@ -81,7 +77,10 @@ mod benchmarks {
#[extrinsic_call]
_(force_origin as T::RuntimeOrigin);

assert_eq!(EnteredUntil::<T>::get().unwrap(), System::<T>::block_number() + duration);
assert_eq!(
EnteredUntil::<T>::get().unwrap(),
frame_system::Pallet::<T>::block_number() + duration
);
Ok(())
}

Expand All @@ -93,15 +92,15 @@ mod benchmarks {
let alice: T::AccountId = whitelisted_caller();
<T::Currency as fungible::Mutate<_>>::set_balance(&alice, init_bal::<T>());

System::<T>::set_block_number(1u32.into());
frame_system::Pallet::<T>::set_block_number(1u32.into());
assert!(SafeMode::<T>::do_enter(None, 1u32.into()).is_ok());

#[extrinsic_call]
_(RawOrigin::Signed(alice));

assert_eq!(
EnteredUntil::<T>::get().unwrap(),
System::<T>::block_number() + 1u32.into() + T::ExtendDuration::get()
frame_system::Pallet::<T>::block_number() + 1u32.into() + T::ExtendDuration::get()
);
Ok(())
}
Expand All @@ -112,7 +111,7 @@ mod benchmarks {
let force_origin = T::ForceExtendOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;

System::<T>::set_block_number(1u32.into());
frame_system::Pallet::<T>::set_block_number(1u32.into());
assert!(SafeMode::<T>::do_enter(None, 1u32.into()).is_ok());

let duration = T::ForceExtendOrigin::ensure_origin(force_origin.clone()).unwrap();
Expand All @@ -125,7 +124,7 @@ mod benchmarks {

assert_eq!(
EnteredUntil::<T>::get().unwrap(),
System::<T>::block_number() + 1u32.into() + duration
frame_system::Pallet::<T>::block_number() + 1u32.into() + duration
);
Ok(())
}
Expand Down Expand Up @@ -161,9 +160,9 @@ mod benchmarks {
EnteredUntil::<T>::put(&block);
assert!(SafeMode::<T>::do_exit(ExitReason::Force).is_ok());

System::<T>::set_block_number(delay + One::one() + 2u32.into());
System::<T>::on_initialize(System::<T>::block_number());
SafeMode::<T>::on_initialize(System::<T>::block_number());
frame_system::Pallet::<T>::set_block_number(delay + One::one() + 2u32.into());
frame_system::Pallet::<T>::on_initialize(frame_system::Pallet::<T>::block_number());
SafeMode::<T>::on_initialize(frame_system::Pallet::<T>::block_number());

#[extrinsic_call]
_(origin, alice.clone(), 1u32.into());
Expand Down Expand Up @@ -195,9 +194,11 @@ mod benchmarks {
);
assert!(SafeMode::<T>::do_exit(ExitReason::Force).is_ok());

System::<T>::set_block_number(System::<T>::block_number() + One::one());
System::<T>::on_initialize(System::<T>::block_number());
SafeMode::<T>::on_initialize(System::<T>::block_number());
frame_system::Pallet::<T>::set_block_number(
frame_system::Pallet::<T>::block_number() + One::one(),
);
frame_system::Pallet::<T>::on_initialize(frame_system::Pallet::<T>::block_number());
SafeMode::<T>::on_initialize(frame_system::Pallet::<T>::block_number());

#[extrinsic_call]
_(force_origin as T::RuntimeOrigin, alice.clone(), block);
Expand Down
25 changes: 11 additions & 14 deletions substrate/frame/safe-mode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ pub mod mock;
mod tests;
pub mod weights;

use frame_support::{
defensive_assert,
pallet_prelude::*,
use frame::{
// This is here because of the defensive! macro, can be removed once the macro is changed to
// not require frame_support
re-gius marked this conversation as resolved.
Show resolved Hide resolved
Krayt78 marked this conversation as resolved.
Show resolved Hide resolved
deps::frame_support,
prelude::*,
traits::{
fungible::{
self,
Expand All @@ -86,20 +88,15 @@ use frame_support::{
tokens::{Fortitude, Precision},
CallMetadata, Contains, Defensive, GetCallMetadata, PalletInfoAccess, SafeModeNotify,
},
weights::Weight,
DefaultNoBound,
};
use frame_system::pallet_prelude::*;
use sp_arithmetic::traits::Zero;
use sp_runtime::traits::Saturating;

pub use pallet::*;
pub use weights::*;

type BalanceOf<T> =
<<T as Config>::Currency as fungible::Inspect<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;

Expand Down Expand Up @@ -609,7 +606,7 @@ where
}
}

impl<T: Config> frame_support::traits::SafeMode for Pallet<T> {
impl<T: Config> frame::traits::SafeMode for Pallet<T> {
type BlockNumber = BlockNumberFor<T>;

fn is_entered() -> bool {
Expand All @@ -623,20 +620,20 @@ impl<T: Config> frame_support::traits::SafeMode for Pallet<T> {
})
}

fn enter(duration: BlockNumberFor<T>) -> Result<(), frame_support::traits::SafeModeError> {
fn enter(duration: BlockNumberFor<T>) -> Result<(), frame::traits::SafeModeError> {
Self::do_enter(None, duration).map_err(Into::into)
}

fn extend(duration: BlockNumberFor<T>) -> Result<(), frame_support::traits::SafeModeError> {
fn extend(duration: BlockNumberFor<T>) -> Result<(), frame::traits::SafeModeError> {
Self::do_extend(None, duration).map_err(Into::into)
}

fn exit() -> Result<(), frame_support::traits::SafeModeError> {
fn exit() -> Result<(), frame::traits::SafeModeError> {
Self::do_exit(ExitReason::Force).map_err(Into::into)
}
}

impl<T: Config> From<Error<T>> for frame_support::traits::SafeModeError {
impl<T: Config> From<Error<T>> for frame::traits::SafeModeError {
fn from(err: Error<T>) -> Self {
match err {
Error::<T>::Entered => Self::AlreadyEntered,
Expand Down
29 changes: 18 additions & 11 deletions substrate/frame/safe-mode/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@
use super::*;
use crate as pallet_safe_mode;

use frame_support::{
derive_impl, parameter_types,
traits::{ConstU64, Everything, InsideBoth, InstanceFilter, IsInVec, SafeModeNotify},
};
use frame_system::EnsureSignedBy;
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
use frame::{
testing_prelude::*,
traits::{
BlakeTwo256, ConstU64, Everything, IdentityLookup, InsideBoth, InstanceFilter, IsInVec,
re-gius marked this conversation as resolved.
Show resolved Hide resolved
SafeModeNotify,
},
};
// use frame_support::{
// derive_impl, parameter_types,
// traits::{ConstU64, Everything, InsideBoth, InstanceFilter, IsInVec, SafeModeNotify},
// };
// use frame_system::EnsureSignedBy;
// use sp_core::H256;
// use sp_runtime::{
// traits::{BlakeTwo256, IdentityLookup},
// BuildStorage,
// };

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
Expand Down Expand Up @@ -227,7 +234,7 @@ frame_support::construct_runtime!(
pub const BAL_ACC0: u64 = 1234;
pub const BAL_ACC1: u64 = 5678;

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<Test> {
Expand All @@ -240,7 +247,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
let mut ext = TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
});
Expand Down
3 changes: 1 addition & 2 deletions substrate/frame/safe-mode/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
use super::*;
use crate::mock::{RuntimeCall, *};

use frame_support::{assert_err, assert_noop, assert_ok, hypothetically_ok, traits::Currency};
use sp_runtime::traits::Dispatchable;
use frame::{deps::frame_support::hypothetically_ok, testing_prelude::*, traits::Currency};

#[test]
fn fails_to_filter_calls_to_safe_mode_pallet() {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/safe-mode/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading