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

Commit 8dac0ab

Browse files
ggwpeznuke-web3liamaharonmuharemgpestana
authored
[FRAME Core] New pallets: safe-mode and tx-pause (#12092)
* 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 <>
1 parent 46c78d5 commit 8dac0ab

File tree

22 files changed

+3339
-6
lines changed

22 files changed

+3339
-6
lines changed

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ members = [
188188
"frame/recovery",
189189
"frame/referenda",
190190
"frame/remark",
191+
"frame/tx-pause",
192+
"frame/safe-mode",
191193
"frame/salary",
192194
"frame/scheduler",
193195
"frame/scored-pool",

bin/node/cli/src/chain_spec.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ pub fn testnet_genesis(
358358
transaction_storage: Default::default(),
359359
transaction_payment: Default::default(),
360360
alliance: Default::default(),
361+
safe_mode: Default::default(),
362+
tx_pause: Default::default(),
361363
alliance_motion: Default::default(),
362364
nomination_pools: NominationPoolsConfig {
363365
min_create_bond: 10 * DOLLARS,

bin/node/runtime/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pallet-transaction-storage = { version = "4.0.0-dev", default-features = false,
129129
pallet-uniques = { version = "4.0.0-dev", default-features = false, path = "../../../frame/uniques" }
130130
pallet-vesting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/vesting" }
131131
pallet-whitelist = { version = "4.0.0-dev", default-features = false, path = "../../../frame/whitelist" }
132+
pallet-tx-pause = { version = "4.0.0-dev", default-features = false, path = "../../../frame/tx-pause" }
133+
pallet-safe-mode = { version = "4.0.0-dev", default-features = false, path = "../../../frame/safe-mode" }
132134

133135
[build-dependencies]
134136
substrate-wasm-builder = { version = "5.0.0-dev", path = "../../../utils/wasm-builder", optional = true }
@@ -200,6 +202,7 @@ std = [
200202
"pallet-referenda/std",
201203
"pallet-remark/std",
202204
"pallet-root-testing/std",
205+
"pallet-safe-mode/std",
203206
"pallet-salary/std",
204207
"pallet-scheduler/std",
205208
"pallet-session-benchmarking?/std",
@@ -216,6 +219,7 @@ std = [
216219
"pallet-transaction-payment/std",
217220
"pallet-transaction-storage/std",
218221
"pallet-treasury/std",
222+
"pallet-tx-pause/std",
219223
"pallet-uniques/std",
220224
"pallet-utility/std",
221225
"pallet-vesting/std",
@@ -290,6 +294,7 @@ runtime-benchmarks = [
290294
"pallet-recovery/runtime-benchmarks",
291295
"pallet-referenda/runtime-benchmarks",
292296
"pallet-remark/runtime-benchmarks",
297+
"pallet-safe-mode/runtime-benchmarks",
293298
"pallet-salary/runtime-benchmarks",
294299
"pallet-scheduler/runtime-benchmarks",
295300
"pallet-session-benchmarking/runtime-benchmarks",
@@ -301,6 +306,7 @@ runtime-benchmarks = [
301306
"pallet-tips/runtime-benchmarks",
302307
"pallet-transaction-storage/runtime-benchmarks",
303308
"pallet-treasury/runtime-benchmarks",
309+
"pallet-tx-pause/runtime-benchmarks",
304310
"pallet-uniques/runtime-benchmarks",
305311
"pallet-utility/runtime-benchmarks",
306312
"pallet-vesting/runtime-benchmarks",
@@ -360,6 +366,7 @@ try-runtime = [
360366
"pallet-referenda/try-runtime",
361367
"pallet-remark/try-runtime",
362368
"pallet-root-testing/try-runtime",
369+
"pallet-safe-mode/try-runtime",
363370
"pallet-salary/try-runtime",
364371
"pallet-scheduler/try-runtime",
365372
"pallet-session/try-runtime",
@@ -373,6 +380,7 @@ try-runtime = [
373380
"pallet-transaction-payment/try-runtime",
374381
"pallet-transaction-storage/try-runtime",
375382
"pallet-treasury/try-runtime",
383+
"pallet-tx-pause/try-runtime",
376384
"pallet-uniques/try-runtime",
377385
"pallet-utility/try-runtime",
378386
"pallet-vesting/try-runtime",

bin/node/runtime/src/lib.rs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ use frame_support::{
3737
traits::{
3838
fungible::{Balanced, Credit, ItemOf},
3939
tokens::{nonfungibles_v2::Inspect, GetSalary, PayFromAccount},
40-
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Currency, EitherOfDiverse,
41-
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
42-
LockIdentifier, Nothing, OnUnbalanced, WithdrawReasons,
40+
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Contains, Currency,
41+
EitherOfDiverse, EqualPrivilegeOnly, Imbalance, InsideBoth, InstanceFilter,
42+
KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, WithdrawReasons,
4343
},
4444
weights::{
4545
constants::{
@@ -64,6 +64,7 @@ use pallet_nis::WithMaximumOf;
6464
use pallet_session::historical as pallet_session_historical;
6565
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
6666
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
67+
use pallet_tx_pause::RuntimeCallNameOf;
6768
use sp_api::impl_runtime_apis;
6869
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
6970
use sp_consensus_grandpa::AuthorityId as GrandpaId;
@@ -220,8 +221,67 @@ parameter_types! {
220221

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

224+
/// Calls that can bypass the safe-mode pallet.
225+
pub struct SafeModeWhitelistedCalls;
226+
impl Contains<RuntimeCall> for SafeModeWhitelistedCalls {
227+
fn contains(call: &RuntimeCall) -> bool {
228+
match call {
229+
RuntimeCall::System(_) | RuntimeCall::SafeMode(_) | RuntimeCall::TxPause(_) => true,
230+
_ => false,
231+
}
232+
}
233+
}
234+
235+
/// Calls that cannot be paused by the tx-pause pallet.
236+
pub struct TxPauseWhitelistedCalls;
237+
/// Whitelist `Balances::transfer_keep_alive`, all others are pauseable.
238+
impl Contains<RuntimeCallNameOf<Runtime>> for TxPauseWhitelistedCalls {
239+
fn contains(full_name: &RuntimeCallNameOf<Runtime>) -> bool {
240+
match (full_name.0.as_slice(), full_name.1.as_slice()) {
241+
(b"Balances", b"transfer_keep_alive") => true,
242+
_ => false,
243+
}
244+
}
245+
}
246+
247+
impl pallet_tx_pause::Config for Runtime {
248+
type RuntimeEvent = RuntimeEvent;
249+
type RuntimeCall = RuntimeCall;
250+
type PauseOrigin = EnsureRoot<AccountId>;
251+
type UnpauseOrigin = EnsureRoot<AccountId>;
252+
type WhitelistedCalls = TxPauseWhitelistedCalls;
253+
type MaxNameLen = ConstU32<256>;
254+
type WeightInfo = pallet_tx_pause::weights::SubstrateWeight<Runtime>;
255+
}
256+
257+
parameter_types! {
258+
pub const EnterDuration: BlockNumber = 4 * HOURS;
259+
pub const EnterDepositAmount: Balance = 2_000_000 * DOLLARS;
260+
pub const ExtendDuration: BlockNumber = 2 * HOURS;
261+
pub const ExtendDepositAmount: Balance = 1_000_000 * DOLLARS;
262+
pub const ReleaseDelay: u32 = 2 * DAYS;
263+
}
264+
265+
impl pallet_safe_mode::Config for Runtime {
266+
type RuntimeEvent = RuntimeEvent;
267+
type Currency = Balances;
268+
type RuntimeHoldReason = RuntimeHoldReason;
269+
type WhitelistedCalls = SafeModeWhitelistedCalls;
270+
type EnterDuration = EnterDuration;
271+
type EnterDepositAmount = EnterDepositAmount;
272+
type ExtendDuration = ExtendDuration;
273+
type ExtendDepositAmount = ExtendDepositAmount;
274+
type ForceEnterOrigin = EnsureRootWithSuccess<AccountId, ConstU32<9>>;
275+
type ForceExtendOrigin = EnsureRootWithSuccess<AccountId, ConstU32<11>>;
276+
type ForceExitOrigin = EnsureRoot<AccountId>;
277+
type ForceDepositOrigin = EnsureRoot<AccountId>;
278+
type ReleaseDelay = ReleaseDelay;
279+
type Notify = ();
280+
type WeightInfo = pallet_safe_mode::weights::SubstrateWeight<Runtime>;
281+
}
282+
223283
impl frame_system::Config for Runtime {
224-
type BaseCallFilter = Everything;
284+
type BaseCallFilter = InsideBoth<SafeMode, TxPause>;
225285
type BlockWeights = RuntimeBlockWeights;
226286
type BlockLength = RuntimeBlockLength;
227287
type DbWeight = RocksDbWeight;
@@ -1682,8 +1742,8 @@ impl pallet_core_fellowship::Config for Runtime {
16821742
type Balance = Balance;
16831743
type ParamsOrigin = frame_system::EnsureRoot<AccountId>;
16841744
type InductOrigin = pallet_core_fellowship::EnsureInducted<Runtime, (), 1>;
1685-
type ApproveOrigin = frame_system::EnsureRootWithSuccess<AccountId, ConstU16<9>>;
1686-
type PromoteOrigin = frame_system::EnsureRootWithSuccess<AccountId, ConstU16<9>>;
1745+
type ApproveOrigin = EnsureRootWithSuccess<AccountId, ConstU16<9>>;
1746+
type PromoteOrigin = EnsureRootWithSuccess<AccountId, ConstU16<9>>;
16871747
type EvidenceSize = ConstU32<16_384>;
16881748
}
16891749

@@ -2021,6 +2081,8 @@ construct_runtime!(
20212081
FastUnstake: pallet_fast_unstake,
20222082
MessageQueue: pallet_message_queue,
20232083
Pov: frame_benchmarking_pallet_pov,
2084+
TxPause: pallet_tx_pause,
2085+
SafeMode: pallet_safe_mode,
20242086
Statement: pallet_statement,
20252087
Broker: pallet_broker,
20262088
}
@@ -2153,6 +2215,8 @@ mod benches {
21532215
[pallet_utility, Utility]
21542216
[pallet_vesting, Vesting]
21552217
[pallet_whitelist, Whitelist]
2218+
[pallet_tx_pause, TxPause]
2219+
[pallet_safe_mode, SafeMode]
21562220
);
21572221
}
21582222

bin/node/testing/src/genesis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Run
101101
alliance: Default::default(),
102102
alliance_motion: Default::default(),
103103
nomination_pools: Default::default(),
104+
safe_mode: Default::default(),
105+
tx_pause: Default::default(),
104106
glutton: GluttonConfig {
105107
compute: Default::default(),
106108
storage: Default::default(),

frame/safe-mode/Cargo.toml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[package]
2+
name = "pallet-safe-mode"
3+
version = "4.0.0-dev"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2021"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.io"
8+
repository = "https://github.com/paritytech/substrate/"
9+
description = "FRAME safe-mode pallet"
10+
11+
[package.metadata.docs.rs]
12+
targets = ["x86_64-unknown-linux-gnu"]
13+
14+
[dependencies]
15+
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
16+
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
17+
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
18+
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
19+
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
20+
sp-arithmetic = { version = "16.0.0", default-features = false, path = "../../primitives/arithmetic" }
21+
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
22+
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
23+
pallet-balances = { version = "4.0.0-dev", path = "../balances", default-features = false, optional = true }
24+
pallet-utility = { version = "4.0.0-dev", path = "../utility", default-features = false, optional = true }
25+
pallet-proxy = { version = "4.0.0-dev", path = "../proxy", default-features = false, optional = true }
26+
27+
[dev-dependencies]
28+
sp-core = { version = "21.0.0", path = "../../primitives/core" }
29+
sp-io = { version = "23.0.0", path = "../../primitives/io" }
30+
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
31+
pallet-utility = { version = "4.0.0-dev", path = "../utility" }
32+
pallet-proxy = { version = "4.0.0-dev", path = "../proxy" }
33+
34+
[features]
35+
default = [ "std" ]
36+
std = [
37+
"codec/std",
38+
"frame-benchmarking/std",
39+
"frame-support/std",
40+
"frame-system/std",
41+
"pallet-balances?/std",
42+
"pallet-proxy?/std",
43+
"pallet-utility?/std",
44+
"scale-info/std",
45+
"sp-arithmetic/std",
46+
"sp-core/std",
47+
"sp-io/std",
48+
"sp-runtime/std",
49+
"sp-std/std",
50+
]
51+
runtime-benchmarks = [
52+
"frame-benchmarking/runtime-benchmarks",
53+
"frame-support/runtime-benchmarks",
54+
"frame-system/runtime-benchmarks",
55+
"pallet-balances/runtime-benchmarks",
56+
"pallet-proxy/runtime-benchmarks",
57+
"pallet-utility/runtime-benchmarks",
58+
"sp-runtime/runtime-benchmarks",
59+
]
60+
try-runtime = [
61+
"frame-support/try-runtime",
62+
"frame-system/try-runtime",
63+
"pallet-balances?/try-runtime",
64+
"pallet-proxy?/try-runtime",
65+
"pallet-utility?/try-runtime",
66+
"sp-runtime/try-runtime",
67+
]

0 commit comments

Comments
 (0)