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

feat: add safe-mode and tx-pause pallets #94

Merged
merged 6 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 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 @@ -60,6 +60,8 @@ pallet-transaction-payment = { version = "23.0.0", default-features = false }
pallet-transaction-payment-rpc = { version = "25.0.0" }
pallet-transaction-payment-rpc-runtime-api = { version = "23.0.0", default-features = false }
pallet-utility = { version = "23.0.0", default-features = false }
pallet-safe-mode = { version = "4.0.0", default-features = false }
pallet-tx-pause = { version = "4.0.0", default-features = false }
sc-basic-authorship = { version = "0.29.0" }
sc-chain-spec = { version = "22.0.0" }
sc-cli = { version = "0.31.0" }
Expand Down
4 changes: 4 additions & 0 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ pub mod devnet {
..Default::default()
},
transaction_payment: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
}
}
}
Expand Down Expand Up @@ -511,6 +513,8 @@ pub mod mainnet {
..Default::default()
},
transaction_payment: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
}
}
}
7 changes: 7 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;

pub const UNITS: Balance = 1_000_000_000_000;
pub const QUID: Balance = UNITS / 30;
pub const CENTS: Balance = QUID / 100;
pub const GRAND: Balance = QUID * 1_000;
pub const MILLICENTS: Balance = CENTS / 1_000;
pub const DOLLARS: Balance = CENTS / 1_000;
weezy20 marked this conversation as resolved.
Show resolved Hide resolved

/// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
/// used to limit the maximal weight of a single extrinsic.
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);
Expand Down
8 changes: 8 additions & 0 deletions runtime/devnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pallet-transaction-payment-rpc-runtime-api = { workspace = true, default-feature
pallet-utility = { workspace = true, default-features = false }
pallet-multisig = { workspace = true, default-features = false }
pallet-preimage = { workspace = true, default-features = false }
pallet-safe-mode = { workspace = true, default-features = false }
pallet-tx-pause = { workspace = true, default-features = false }
sp-api = { workspace = true, default-features = false }
sp-block-builder = { workspace = true, default-features = false }
sp-consensus-aura = { workspace = true, default-features = false }
Expand Down Expand Up @@ -123,6 +125,8 @@ std = [
"pallet-transaction-payment/std",
"pallet-utility/std",
"pallet-xcm/std",
"pallet-safe-mode/std",
"pallet-tx-pause/std",
"parachain-info/std",
"polkadot-parachain-primitives/std",
"polkadot-runtime-common/std",
Expand Down Expand Up @@ -161,6 +165,8 @@ runtime-benchmarks = [
"pallet-timestamp/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-safe-mode/runtime-benchmarks",
"pallet-tx-pause/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"cumulus-pallet-session-benchmarking/runtime-benchmarks",
Expand Down Expand Up @@ -192,5 +198,7 @@ try-runtime = [
"pallet-transaction-payment/try-runtime",
"pallet-utility/try-runtime",
"pallet-xcm/try-runtime",
"pallet-safe-mode/try-runtime",
"pallet-tx-pause/try-runtime",
"parachain-info/try-runtime",
]
78 changes: 73 additions & 5 deletions runtime/devnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod xcm_config;
pub use fee::WeightToFee;

use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
use pallet_tx_pause::RuntimeCallNameOf;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, ConstBool, OpaqueMetadata};
use sp_runtime::{
Expand All @@ -29,18 +30,20 @@ use frame_support::{
construct_runtime,
dispatch::DispatchClass,
parameter_types,
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, EitherOfDiverse, Everything},
traits::{
AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, Contains, EitherOfDiverse, Everything,
},
weights::{ConstantMultiplier, Weight},
PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot, EnsureSigned,
EnsureRoot, EnsureRootWithSuccess, EnsureSigned,
};
use pallet_xcm::{EnsureXcm, IsVoiceOfBody};
pub use runtime_common::{
AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT, MILLISECS_PER_BLOCK, MINUTES,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
AVERAGE_ON_INITIALIZE_RATIO, DAYS, DOLLARS, HOURS, MAXIMUM_BLOCK_WEIGHT, MILLISECS_PER_BLOCK,
MINUTES, NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
pub use sp_runtime::{MultiAddress, Perbill, Permill};
Expand Down Expand Up @@ -348,7 +351,7 @@ impl pallet_balances::Config for Runtime {
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type ReserveIdentifier = [u8; 8];
type RuntimeHoldReason = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxLocks = ConstU32<50>;
type MaxReserves = ConstU32<50>;
Expand Down Expand Up @@ -611,6 +614,67 @@ impl pallet_collator_selection::Config for Runtime {
type WeightInfo = ();
}

pub struct SafeModeWhitelistedCalls;
impl Contains<RuntimeCall> for SafeModeWhitelistedCalls {
fn contains(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::System(_)
| RuntimeCall::SafeMode(_)
| RuntimeCall::TxPause(_)
| RuntimeCall::Balances(_) => true,
_ => false,
}
}
}

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 ExtendDuration = ExtendDuration;
type EnterDepositAmount = EnterDepositAmount;
type ExtendDepositAmount = ExtendDepositAmount;
type ForceEnterOrigin = EnsureRootWithSuccess<AccountId, ConstU32<9>>;
weezy20 marked this conversation as resolved.
Show resolved Hide resolved
type ForceExtendOrigin = EnsureRootWithSuccess<AccountId, ConstU32<11>>;
type ForceExitOrigin = EnsureRoot<AccountId>;
type ForceDepositOrigin = EnsureRoot<AccountId>;
type Notify = ();
type ReleaseDelay = ReleaseDelay;
type WeightInfo = pallet_safe_mode::weights::SubstrateWeight<Runtime>;
}

/// 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>;
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub struct Runtime {
Expand All @@ -625,6 +689,8 @@ construct_runtime!(
Multisig: pallet_multisig = 5,
Preimage: pallet_preimage = 6,
Scheduler: pallet_scheduler = 7,
SafeMode: pallet_safe_mode = 8,
TxPause: pallet_tx_pause = 9,

// Monetary stuff.
Balances: pallet_balances = 10,
Expand Down Expand Up @@ -663,6 +729,8 @@ mod benches {
[pallet_collator_selection, CollatorSelection]
[pallet_multisig, Multisig]
[pallet_preimage, Preimage]
[pallet_safe_mode, SafeMode]
[pallet_tx_pause, TxPause]
[cumulus_pallet_xcmp_queue, XcmpQueue]
);
}
Expand Down
8 changes: 8 additions & 0 deletions runtime/mainnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pallet-transaction-payment-rpc-runtime-api = { workspace = true, default-feature
pallet-utility = { workspace = true, default-features = false }
pallet-multisig = { workspace = true, default-features = false }
pallet-preimage = { workspace = true, default-features = false }
pallet-safe-mode = { workspace = true, default-features = false }
pallet-tx-pause = { workspace = true, default-features = false }
sp-api = { workspace = true, default-features = false }
sp-block-builder = { workspace = true, default-features = false }
sp-consensus-aura = { workspace = true, default-features = false }
Expand Down Expand Up @@ -123,6 +125,8 @@ std = [
"pallet-transaction-payment/std",
"pallet-utility/std",
"pallet-xcm/std",
"pallet-safe-mode/std",
"pallet-tx-pause/std",
"parachain-info/std",
"polkadot-parachain-primitives/std",
"polkadot-runtime-common/std",
Expand Down Expand Up @@ -161,6 +165,8 @@ runtime-benchmarks = [
"pallet-timestamp/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-safe-mode/runtime-benchmarks",
"pallet-tx-pause/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"cumulus-pallet-session-benchmarking/runtime-benchmarks",
Expand Down Expand Up @@ -192,5 +198,7 @@ try-runtime = [
"pallet-transaction-payment/try-runtime",
"pallet-utility/try-runtime",
"pallet-xcm/try-runtime",
"pallet-safe-mode/try-runtime",
"pallet-tx-pause/try-runtime",
"parachain-info/try-runtime",
]
Loading