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

Commit

Permalink
[FRAME Core] New pallets: safe-mode and tx-pause (#12092)
Browse files Browse the repository at this point in the history
* Add safe-mode

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Update pallet

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add to kitchensink-runtime

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Spelling

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Rename to tx-pause

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add SafeMode pallet

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* fmt

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Automatically disable safe-mode in on_init…

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add permissionless enable+extend

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add repay+slash stake methods

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add docs

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix stakes storage

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Genesis config for safe-mode pallet

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Genesis config for safe-mode pallet

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Rename ExtrinsicName to FunctionName

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Origin variable duration

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Rename FunctionName -> CallName

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Rename and docs

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Pallet safe mode tests (#12148)

* Add safe-mode mock runtime
* Add safe-mode tests
* Add ForceEnable- and ForceExtendOrigin
* Start dummy benchmarks
Co-authored-by: Oliver Tale-Yazdi <[email protected]>

* Tests for `pallet-tx-pause` (#12259)

* mock added
* tests added
* dummy benchmarks started

* rename to active/inactive
tests broken, in progress

* Runtime types, fix tests

* WIP safe mode and tx pause {continued} (#12371)

* test coverage on safe mode and tx pause
* benchmarks & tests
* tests passing, use FullNameOf to check tx-pause unfilterables
* naming updates

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: Oliver Tale-Yazdi <[email protected]>

* Set block number

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* dummy weights generated, safe mode

* add repay_reservation call with RepaymentDelay per #10033 feature requirements

* make call name optional to allow pausing pallets, handle `Contains` correctly for this throughout, doc comments started

* move to full_name notation for all interfaces, last commit introduced 1 more storage read.

* refactor is_paused

* safe math on safe mode

* Make stuff compile

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Compile

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Cleanup & renames

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Cleanup TxPause pallet

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix benches

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* fmt

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Refactor to fungibles::* and rename stuf

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Make compile

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix node config

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Typos

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Remove CausalHoldReason

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Refactor benchmarks and runtime configs

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add traits

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Remove old code

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Cleanup safe-mode benches

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Update frame/safe-mode/Cargo.toml

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/safe-mode/Cargo.toml

Co-authored-by: Liam Aharon <[email protected]>

* Docs

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Remove getters

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Update Cargo.lock

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Remove phantom

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix test

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Remove phantom

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Apply suggestions from code review

Co-authored-by: Liam Aharon <[email protected]>

* Use new as Origin benchmarking syntax

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Docs

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix node

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix tx-pause benches

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Renames

* Remove duplicate test

* Add docs

* docs

* Apply suggestions from code review

Co-authored-by: Liam Aharon <[email protected]>
Co-authored-by: Muharem Ismailov <[email protected]>
Co-authored-by: Gonçalo Pestana <[email protected]>

* Cleanup tests

* docs

* Cleanup GenesisConfigs

* Doc traits

* Remove PauseTooLongNames

* docs

* Use V2 benchmarking

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Use RuntimeHoldReason

* Fix kitchensink runtime

* Fix CI

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix CI

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Review

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Rename Stake to Deposit

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add docs

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add Notify and test it

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix kitchensink

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Update frame/safe-mode/src/tests.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/safe-mode/src/tests.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/tx_pause.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/tx-pause/src/lib.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/tx-pause/src/lib.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/tx-pause/src/mock.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Simplify code

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Update frame/support/src/traits/safe_mode.rs

Co-authored-by: Liam Aharon <[email protected]>

* Fixup merge

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Make stuff compile

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Make tx-pause compile again

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix features

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix more features

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* ".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime=dev --target_dir=substrate --pallet=pallet_safe_mode

* Update weights

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

---------

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: Dan Shields <[email protected]>
Co-authored-by: Dan Shields <[email protected]>
Co-authored-by: Liam Aharon <[email protected]>
Co-authored-by: Muharem Ismailov <[email protected]>
Co-authored-by: Gonçalo Pestana <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
6 people authored Aug 25, 2023
1 parent 46c78d5 commit 8dac0ab
Show file tree
Hide file tree
Showing 22 changed files with 3,339 additions and 6 deletions.
39 changes: 39 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ members = [
"frame/recovery",
"frame/referenda",
"frame/remark",
"frame/tx-pause",
"frame/safe-mode",
"frame/salary",
"frame/scheduler",
"frame/scored-pool",
Expand Down
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 @@ -358,6 +358,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
8 changes: 8 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pallet-transaction-storage = { version = "4.0.0-dev", default-features = false,
pallet-uniques = { version = "4.0.0-dev", default-features = false, path = "../../../frame/uniques" }
pallet-vesting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/vesting" }
pallet-whitelist = { version = "4.0.0-dev", default-features = false, path = "../../../frame/whitelist" }
pallet-tx-pause = { version = "4.0.0-dev", default-features = false, path = "../../../frame/tx-pause" }
pallet-safe-mode = { version = "4.0.0-dev", default-features = false, path = "../../../frame/safe-mode" }

[build-dependencies]
substrate-wasm-builder = { version = "5.0.0-dev", path = "../../../utils/wasm-builder", optional = true }
Expand Down Expand Up @@ -200,6 +202,7 @@ std = [
"pallet-referenda/std",
"pallet-remark/std",
"pallet-root-testing/std",
"pallet-safe-mode/std",
"pallet-salary/std",
"pallet-scheduler/std",
"pallet-session-benchmarking?/std",
Expand All @@ -216,6 +219,7 @@ std = [
"pallet-transaction-payment/std",
"pallet-transaction-storage/std",
"pallet-treasury/std",
"pallet-tx-pause/std",
"pallet-uniques/std",
"pallet-utility/std",
"pallet-vesting/std",
Expand Down Expand Up @@ -290,6 +294,7 @@ runtime-benchmarks = [
"pallet-recovery/runtime-benchmarks",
"pallet-referenda/runtime-benchmarks",
"pallet-remark/runtime-benchmarks",
"pallet-safe-mode/runtime-benchmarks",
"pallet-salary/runtime-benchmarks",
"pallet-scheduler/runtime-benchmarks",
"pallet-session-benchmarking/runtime-benchmarks",
Expand All @@ -301,6 +306,7 @@ runtime-benchmarks = [
"pallet-tips/runtime-benchmarks",
"pallet-transaction-storage/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-tx-pause/runtime-benchmarks",
"pallet-uniques/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
Expand Down Expand Up @@ -360,6 +366,7 @@ try-runtime = [
"pallet-referenda/try-runtime",
"pallet-remark/try-runtime",
"pallet-root-testing/try-runtime",
"pallet-safe-mode/try-runtime",
"pallet-salary/try-runtime",
"pallet-scheduler/try-runtime",
"pallet-session/try-runtime",
Expand All @@ -373,6 +380,7 @@ try-runtime = [
"pallet-transaction-payment/try-runtime",
"pallet-transaction-storage/try-runtime",
"pallet-treasury/try-runtime",
"pallet-tx-pause/try-runtime",
"pallet-uniques/try-runtime",
"pallet-utility/try-runtime",
"pallet-vesting/try-runtime",
Expand Down
76 changes: 70 additions & 6 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use frame_support::{
traits::{
fungible::{Balanced, Credit, ItemOf},
tokens::{nonfungibles_v2::Inspect, GetSalary, PayFromAccount},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Currency, EitherOfDiverse,
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
LockIdentifier, Nothing, OnUnbalanced, WithdrawReasons,
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Contains, Currency,
EitherOfDiverse, EqualPrivilegeOnly, Imbalance, InsideBoth, InstanceFilter,
KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, WithdrawReasons,
},
weights::{
constants::{
Expand All @@ -64,6 +64,7 @@ use pallet_nis::WithMaximumOf;
use pallet_session::historical as pallet_session_historical;
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use pallet_tx_pause::RuntimeCallNameOf;
use sp_api::impl_runtime_apis;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
Expand Down Expand Up @@ -220,8 +221,67 @@ parameter_types! {

const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct());

/// Calls that can bypass the safe-mode pallet.
pub struct SafeModeWhitelistedCalls;
impl Contains<RuntimeCall> for SafeModeWhitelistedCalls {
fn contains(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::System(_) | RuntimeCall::SafeMode(_) | RuntimeCall::TxPause(_) => true,
_ => false,
}
}
}

/// Calls that cannot be paused by the tx-pause pallet.
pub struct TxPauseWhitelistedCalls;
/// Whitelist `Balances::transfer_keep_alive`, all others are pauseable.
impl Contains<RuntimeCallNameOf<Runtime>> for TxPauseWhitelistedCalls {
fn contains(full_name: &RuntimeCallNameOf<Runtime>) -> bool {
match (full_name.0.as_slice(), full_name.1.as_slice()) {
(b"Balances", b"transfer_keep_alive") => true,
_ => false,
}
}
}

impl pallet_tx_pause::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PauseOrigin = EnsureRoot<AccountId>;
type UnpauseOrigin = EnsureRoot<AccountId>;
type WhitelistedCalls = TxPauseWhitelistedCalls;
type MaxNameLen = ConstU32<256>;
type WeightInfo = pallet_tx_pause::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const EnterDuration: BlockNumber = 4 * HOURS;
pub const EnterDepositAmount: Balance = 2_000_000 * DOLLARS;
pub const ExtendDuration: BlockNumber = 2 * HOURS;
pub const ExtendDepositAmount: Balance = 1_000_000 * DOLLARS;
pub const ReleaseDelay: u32 = 2 * DAYS;
}

impl pallet_safe_mode::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type RuntimeHoldReason = RuntimeHoldReason;
type WhitelistedCalls = SafeModeWhitelistedCalls;
type EnterDuration = EnterDuration;
type EnterDepositAmount = EnterDepositAmount;
type ExtendDuration = ExtendDuration;
type ExtendDepositAmount = ExtendDepositAmount;
type ForceEnterOrigin = EnsureRootWithSuccess<AccountId, ConstU32<9>>;
type ForceExtendOrigin = EnsureRootWithSuccess<AccountId, ConstU32<11>>;
type ForceExitOrigin = EnsureRoot<AccountId>;
type ForceDepositOrigin = EnsureRoot<AccountId>;
type ReleaseDelay = ReleaseDelay;
type Notify = ();
type WeightInfo = pallet_safe_mode::weights::SubstrateWeight<Runtime>;
}

impl frame_system::Config for Runtime {
type BaseCallFilter = Everything;
type BaseCallFilter = InsideBoth<SafeMode, TxPause>;
type BlockWeights = RuntimeBlockWeights;
type BlockLength = RuntimeBlockLength;
type DbWeight = RocksDbWeight;
Expand Down Expand Up @@ -1682,8 +1742,8 @@ impl pallet_core_fellowship::Config for Runtime {
type Balance = Balance;
type ParamsOrigin = frame_system::EnsureRoot<AccountId>;
type InductOrigin = pallet_core_fellowship::EnsureInducted<Runtime, (), 1>;
type ApproveOrigin = frame_system::EnsureRootWithSuccess<AccountId, ConstU16<9>>;
type PromoteOrigin = frame_system::EnsureRootWithSuccess<AccountId, ConstU16<9>>;
type ApproveOrigin = EnsureRootWithSuccess<AccountId, ConstU16<9>>;
type PromoteOrigin = EnsureRootWithSuccess<AccountId, ConstU16<9>>;
type EvidenceSize = ConstU32<16_384>;
}

Expand Down Expand Up @@ -2021,6 +2081,8 @@ construct_runtime!(
FastUnstake: pallet_fast_unstake,
MessageQueue: pallet_message_queue,
Pov: frame_benchmarking_pallet_pov,
TxPause: pallet_tx_pause,
SafeMode: pallet_safe_mode,
Statement: pallet_statement,
Broker: pallet_broker,
}
Expand Down Expand Up @@ -2153,6 +2215,8 @@ mod benches {
[pallet_utility, Utility]
[pallet_vesting, Vesting]
[pallet_whitelist, Whitelist]
[pallet_tx_pause, TxPause]
[pallet_safe_mode, SafeMode]
);
}

Expand Down
2 changes: 2 additions & 0 deletions bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Run
alliance: Default::default(),
alliance_motion: Default::default(),
nomination_pools: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
glutton: GluttonConfig {
compute: Default::default(),
storage: Default::default(),
Expand Down
67 changes: 67 additions & 0 deletions frame/safe-mode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[package]
name = "pallet-safe-mode"
version = "4.0.0-dev"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME safe-mode pallet"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", 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.5.0", default-features = false, features = ["derive"] }
sp-arithmetic = { version = "16.0.0", default-features = false, path = "../../primitives/arithmetic" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
pallet-balances = { version = "4.0.0-dev", path = "../balances", default-features = false, optional = true }
pallet-utility = { version = "4.0.0-dev", path = "../utility", default-features = false, optional = true }
pallet-proxy = { version = "4.0.0-dev", path = "../proxy", default-features = false, optional = true }

[dev-dependencies]
sp-core = { version = "21.0.0", path = "../../primitives/core" }
sp-io = { version = "23.0.0", path = "../../primitives/io" }
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
pallet-utility = { version = "4.0.0-dev", path = "../utility" }
pallet-proxy = { version = "4.0.0-dev", path = "../proxy" }

[features]
default = [ "std" ]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/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",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/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",
"pallet-balances?/try-runtime",
"pallet-proxy?/try-runtime",
"pallet-utility?/try-runtime",
"sp-runtime/try-runtime",
]
Loading

0 comments on commit 8dac0ab

Please sign in to comment.