Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Pallet safe mode tests (#12148)
Browse files Browse the repository at this point in the history
* Add safe-mode mock runtime
* Add safe-mode tests
* Add ForceEnable- and ForceExtendOrigin
* Start dummy benchmarks
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
nuke-web3 authored Sep 21, 2022
1 parent ace1f1c commit 379b82f
Show file tree
Hide file tree
Showing 10 changed files with 800 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ pub fn testnet_genesis(
transaction_storage: Default::default(),
transaction_payment: Default::default(),
alliance: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
alliance_motion: Default::default(),
nomination_pools: NominationPoolsConfig {
min_create_bond: 10 * DOLLARS,
Expand Down
19 changes: 16 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,26 @@ impl pallet_tx_pause::Config for Runtime {
type PauseTooLongNames = ConstBool<true>;
}

parameter_types! {
// signed config
pub const EnableStakeAmount: Balance = 1 * DOLLARS; //TODO This needs to be something sensible for the implications of enablement!
pub const ExtendStakeAmount: Balance = 1 * DOLLARS; //TODO This needs to be something sensible for the implications of enablement!
pub BlockHeight: BlockNumber = System::block_number(); // TODO ensure this plus config below is correct
}

impl pallet_safe_mode::Config for Runtime {
type Event = Event;
type Currency = Balances;
type SafeModeFilter = Nothing; // TODO add TxPause pallet
type EnableDuration = ConstU32<{ 2 * DAYS }>;
type ExtendDuration = ConstU32<{ 1 * DAYS }>;
type EnableOrigin = EnsureRoot<AccountId>;
type ExtendOrigin = EnsureRoot<AccountId>;
type PreemptiveDisableOrigin = EnsureRoot<AccountId>;
type EnableOrigin = EnsureRootWithSuccess<AccountId, BlockHeight>;
type ExtendOrigin = EnsureRootWithSuccess<AccountId, BlockHeight>;
type DisableOrigin = EnsureRoot<AccountId>;
type RepayOrigin = EnsureRoot<AccountId>;
type EnableStakeAmount = EnableStakeAmount;
type ExtendStakeAmount = ExtendStakeAmount;
type WeightInfo = pallet_safe_mode::weights::SubstrateWeight<Runtime>;
}

impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -1795,6 +1807,7 @@ mod benches {
[pallet_utility, Utility]
[pallet_vesting, Vesting]
[pallet_whitelist, Whitelist]
[pallet_safe_mode, SafeMode]
);
}

Expand Down
1 change: 0 additions & 1 deletion frame/remark/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use super::{Error, Event, Pallet as Remark};
use crate::mock::*;
use frame_support::{assert_noop, assert_ok};
use frame_system::RawOrigin;

#[test]
fn generates_event() {
Expand Down
9 changes: 8 additions & 1 deletion frame/safe-mode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,32 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "4.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
sp-core = { version = "6.0.0", path = "../../primitives/core" }
sp-std = { version = "4.0.0", path = "../../primitives/std" }
sp-io = { version = "6.0.0", path = "../../primitives/io" }
pallet-balances = { version = "4.0.0-dev", path = "../balances" }

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
Expand Down
77 changes: 77 additions & 0 deletions frame/safe-mode/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,80 @@
// limitations under the License.

#![cfg(feature = "runtime-benchmarks")]

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

use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_support::traits::Currency;
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::Bounded;

benchmarks! {
enable {
let caller: T::AccountId = whitelisted_caller();
let origin = RawOrigin::Signed(caller.clone());
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
}: enable(origin)
verify {
assert_eq!(
SafeMode::<T>::enabled().unwrap(),
System::<T>::block_number() + T::EnableDuration::get()
);
}

// force_enable {
// /* code to set the initial state */
// }: {
// /* code to test the function benchmarked */
// }
// verify {
// /* optional verification */
// }

// extend {
// /* code to set the initial state */
// }: {
// /* code to test the function benchmarked */
// }
// verify {
// /* optional verification */
// }

// force_extend {
// /* code to set the initial state */
// }: {
// /* code to test the function benchmarked */
// }
// verify {
// /* optional verification */
// }

// force_disable {
// /* code to set the initial state */
// }: {
// /* code to test the function benchmarked */
// }
// verify {
// /* optional verification */
// }

// repay_stake {
// /* code to set the initial state */
// }: {
// /* code to test the function benchmarked */
// }
// verify {
// /* optional verification */
// }

// slash_stake {
// /* code to set the initial state */
// }: {
// /* code to test the function benchmarked */
// }
// verify {
// /* optional verification */
// }

impl_benchmark_test_suite!(SafeMode, crate::mock::new_test_ext(), crate::mock::Test);
}
21 changes: 15 additions & 6 deletions frame/safe-mode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
pub mod mock;
#[cfg(test)]
mod tests;
pub mod weights;

use frame_support::{
pallet_prelude::*,
traits::{
CallMetadata, Contains, Currency, Defensive, GetCallMetadata, PalletInfoAccess,
ReservableCurrency,
},
weights::Weight,
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Saturating;
use sp_std::{convert::TryInto, prelude::*};

mod benchmarking;
mod mock;
mod tests;

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

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -97,7 +103,7 @@ pub mod pallet {
type RepayOrigin: EnsureOrigin<Self::Origin>;

// Weight information for extrinsics in this pallet.
//type WeightInfo: WeightInfo;
type WeightInfo: WeightInfo;
}

#[pallet::error]
Expand Down Expand Up @@ -149,10 +155,12 @@ pub mod pallet {
/// This is set to `None` if the safe-mode is disabled.
/// The safe-mode is automatically disabled when the current block number is greater than this.
#[pallet::storage]
#[pallet::getter(fn enabled)]
pub type Enabled<T: Config> = StorageValue<_, T::BlockNumber, OptionQuery>;

/// Holds the stake that was reserved from a user at a specific block number.
#[pallet::storage]
#[pallet::getter(fn stakes)]
pub type Stakes<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
Expand Down Expand Up @@ -196,7 +204,8 @@ pub mod pallet {
/// Reserves `EnableStakeAmount` from the caller's account.
/// Errors if the safe-mode is already enabled.
/// Can be permanently disabled by configuring `EnableStakeAmount` to `None`.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::enable())]
// #[pallet::weight(0)]
pub fn enable(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down
Loading

0 comments on commit 379b82f

Please sign in to comment.