From ac5d44b10ab79914b8d1e3f053d3f9d695733e90 Mon Sep 17 00:00:00 2001 From: Igor Papandinas <26460174+ipapandinas@users.noreply.github.com> Date: Thu, 28 Nov 2024 13:54:28 +0100 Subject: [PATCH] config updated and safe-mode coupled to DappStaking via Notify --- pallets/dapp-staking/src/lib.rs | 27 ++++++++++-- pallets/dapp-staking/src/test/tests.rs | 24 ++++++++++- runtime/local/src/lib.rs | 52 +++++++--------------- runtime/shibuya/src/lib.rs | 60 ++++++++++---------------- 4 files changed, 84 insertions(+), 79 deletions(-) diff --git a/pallets/dapp-staking/src/lib.rs b/pallets/dapp-staking/src/lib.rs index c82b163d29..d96ff175bc 100644 --- a/pallets/dapp-staking/src/lib.rs +++ b/pallets/dapp-staking/src/lib.rs @@ -39,7 +39,7 @@ use frame_support::{ pallet_prelude::*, traits::{ fungible::{Inspect as FunInspect, MutateFreeze as FunMutateFreeze}, - StorageVersion, + SafeModeNotify, StorageVersion, }, weights::Weight, }; @@ -682,9 +682,7 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::maintenance_mode())] pub fn maintenance_mode(origin: OriginFor, enabled: bool) -> DispatchResult { T::ManagerOrigin::ensure_origin(origin)?; - ActiveProtocolState::::mutate(|state| state.maintenance = enabled); - - Self::deposit_event(Event::::MaintenanceMode { enabled }); + Self::set_maintenance_mode(enabled); Ok(()) } @@ -2210,6 +2208,14 @@ pub mod pallet { Ok(()) } + /// Internal function to transition the dApp staking protocol maintenance mode. + /// Ensure this method is **not exposed publicly** and is only used for legitimate maintenance mode transitions invoked by privileged or trusted logic, + /// such as `T::ManagerOrigin` or a safe-mode enter/exit notification. + fn set_maintenance_mode(enabled: bool) { + ActiveProtocolState::::mutate(|state| state.maintenance = enabled); + Self::deposit_event(Event::::MaintenanceMode { enabled }); + } + /// Ensure the correctness of the state of this pallet. #[cfg(any(feature = "try-runtime", test))] pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> { @@ -2470,4 +2476,17 @@ pub mod pallet { Ok(()) } } + + /// Implementation of the `SafeModeNotify` trait for the `DappStaking` pallet. + /// This integration ensures that the dApp staking protocol transitions to and from + /// maintenance mode when the runtime enters or exits safe mode. + impl SafeModeNotify for Pallet { + fn entered() { + Self::set_maintenance_mode(true); + } + + fn exited() { + Self::set_maintenance_mode(false); + } + } } diff --git a/pallets/dapp-staking/src/test/tests.rs b/pallets/dapp-staking/src/test/tests.rs index c3ffcbe732..f3014e326f 100644 --- a/pallets/dapp-staking/src/test/tests.rs +++ b/pallets/dapp-staking/src/test/tests.rs @@ -29,7 +29,7 @@ use frame_support::{ error::BadOrigin, traits::{ fungible::Unbalanced as FunUnbalanced, Currency, Get, OnFinalize, OnInitialize, - ReservableCurrency, + ReservableCurrency, SafeModeNotify, }, BoundedVec, }; @@ -185,6 +185,28 @@ fn maintenance_mode_call_filtering_works() { }) } +#[test] +fn maintenance_safe_mode_entered_exited_works() { + ExtBuilder::default().build_and_execute(|| { + // Check that maintenance mode is disabled by default + assert!(!ActiveProtocolState::::get().maintenance); + + // Call entered and check post-state and event + DappStaking::entered(); + assert!(ActiveProtocolState::::get().maintenance); + System::assert_last_event(RuntimeEvent::DappStaking(Event::MaintenanceMode { + enabled: true, + })); + + // Call exited and check post-state and event + DappStaking::exited(); + assert!(!ActiveProtocolState::::get().maintenance); + System::assert_last_event(RuntimeEvent::DappStaking(Event::MaintenanceMode { + enabled: false, + })); + }) +} + #[test] fn on_initialize_is_noop_if_no_era_change() { ExtBuilder::default().build_and_execute(|| { diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index a5de502dab..04c3cf02be 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -49,7 +49,6 @@ use pallet_evm::{FeeCalculator, GasWeightMapping, Runner}; use pallet_evm_precompile_assets_erc20::AddressToAssetId; use pallet_grandpa::{fg_primitives, AuthorityList as GrandpaAuthorityList}; use pallet_transaction_payment::{FungibleAdapter, Multiplier, TargetedFeeAdjustment}; -use pallet_tx_pause::RuntimeCallNameOf; use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen}; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, sr25519, ConstBool, OpaqueMetadata, H160, H256, U256}; @@ -1128,57 +1127,39 @@ impl pallet_collective_proxy::Config for Runtime { type WeightInfo = pallet_collective_proxy::weights::SubstrateWeight; } -parameter_types! { - pub const EnterDuration: BlockNumber = 10 * MINUTES; - pub const EnterDepositAmount: Option = None; - pub const ExtendDuration: BlockNumber = 5 * MINUTES; - pub const ExtendDepositAmount: Option = None; - pub const ReleaseDelay: Option = None; -} - /// Calls that can bypass the safe-mode pallet. pub struct SafeModeWhitelistedCalls; impl Contains for SafeModeWhitelistedCalls { fn contains(call: &RuntimeCall) -> bool { match call { - RuntimeCall::Sudo(_) // System and Timestamp are required for block production - | RuntimeCall::System(_) + RuntimeCall::System(_) | RuntimeCall::Timestamp(_) - | RuntimeCall::SafeMode(_) + | RuntimeCall::Sudo(_) | RuntimeCall::TxPause(_) => true, _ => false, } } } -/// Calls that cannot be paused by the tx-pause pallet. -pub struct TxPauseWhitelistedCalls; -/// All calls can be paused, except Sudo calls. -impl Contains> for TxPauseWhitelistedCalls { - fn contains(full_name: &pallet_tx_pause::RuntimeCallNameOf) -> bool { - matches!(full_name.0.as_slice(), b"Sudo") - } -} - 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; - // The 'Success' value below represents the number of blocks that the origin may induce safe mode + type EnterDuration = ConstU32<{ 5 * MINUTES }>; + type EnterDepositAmount = (); + type ExtendDuration = ConstU32<{ 2 * MINUTES }>; + type ExtendDepositAmount = (); + // The 'Success' values below represent the number of blocks that the origin may induce safe mode type ForceEnterOrigin = - EnsureWithSuccess; + EnsureWithSuccess>; type ForceExtendOrigin = - EnsureWithSuccess; - type ForceExitOrigin = EnsureRootOrAllTechnicalCommittee; - type ForceDepositOrigin = EnsureRootOrAllTechnicalCommittee; - type ReleaseDelay = ReleaseDelay; - type Notify = (); + EnsureWithSuccess>; + type ForceExitOrigin = EnsureRootOrTwoThirdsTechnicalCommittee; + type ForceDepositOrigin = EnsureRootOrTwoThirdsTechnicalCommittee; + type ReleaseDelay = (); + type Notify = DappStaking; type WeightInfo = pallet_safe_mode::weights::SubstrateWeight; } @@ -1187,7 +1168,7 @@ impl pallet_tx_pause::Config for Runtime { type RuntimeCall = RuntimeCall; type PauseOrigin = EnsureRootOrHalfTechnicalCommittee; type UnpauseOrigin = EnsureRootOrHalfTechnicalCommittee; - type WhitelistedCalls = TxPauseWhitelistedCalls; + type WhitelistedCalls = (); type MaxNameLen = ConstU32<256>; type WeightInfo = pallet_tx_pause::weights::SubstrateWeight; } @@ -1236,9 +1217,8 @@ construct_runtime!( Treasury: pallet_treasury:: = 107, CommunityTreasury: pallet_treasury:: = 108, CollectiveProxy: pallet_collective_proxy = 109, - - SafeMode: pallet_safe_mode = 130, - TxPause: pallet_tx_pause = 131, + SafeMode: pallet_safe_mode = 110, + TxPause: pallet_tx_pause = 111, } ); diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index 334137e1df..3463018ce8 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -53,7 +53,6 @@ use pallet_identity::legacy::IdentityInfo; use pallet_transaction_payment::{ FeeDetails, Multiplier, RuntimeDispatchInfo, TargetedFeeAdjustment, }; -use pallet_tx_pause::RuntimeCallNameOf; use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen}; use polkadot_runtime_common::BlockHashCount; use sp_api::impl_runtime_apis; @@ -89,7 +88,8 @@ use astar_primitives::{ CommunityCouncilCollectiveInst, CommunityCouncilMembershipInst, CommunityTreasuryInst, EnsureRootOrAllMainCouncil, EnsureRootOrAllTechnicalCommittee, EnsureRootOrFourFifthsCommunityCouncil, EnsureRootOrHalfCommunityCouncil, - EnsureRootOrHalfMainCouncil, EnsureRootOrHalfTechnicalCommittee, MainCouncilCollectiveInst, + EnsureRootOrHalfMainCouncil, EnsureRootOrHalfTechnicalCommittee, + EnsureRootOrTwoThirdsTechnicalCommittee, MainCouncilCollectiveInst, MainCouncilMembershipInst, MainTreasuryInst, OracleMembershipInst, TechnicalCommitteeCollectiveInst, TechnicalCommitteeMembershipInst, }, @@ -249,7 +249,9 @@ parameter_types! { pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder() .base_block(BlockExecutionWeight::get()) .for_class(DispatchClass::all(), |weights| { - weights.base_extrinsic = ExtrinsicBaseWeight::get(); + // Adjusting the base extrinsic weight to account for the additional database + // read introduced by the `tx-pause` pallet during extrinsic filtering. + weights.base_extrinsic = ExtrinsicBaseWeight::get().saturating_add(RocksDbWeight::get().reads(1)); }) .for_class(DispatchClass::Normal, |weights| { weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT); @@ -1539,57 +1541,40 @@ impl pallet_migrations::Config for Runtime { type WeightInfo = pallet_migrations::weights::SubstrateWeight; } -parameter_types! { - pub const EnterDuration: BlockNumber = 4 * HOURS; - pub const EnterDepositAmount: Option = None; - pub const ExtendDuration: BlockNumber = 2 * HOURS; - pub const ExtendDepositAmount: Option = None; - pub const ReleaseDelay: Option = None; -} - /// Calls that can bypass the safe-mode pallet. pub struct SafeModeWhitelistedCalls; impl Contains for SafeModeWhitelistedCalls { fn contains(call: &RuntimeCall) -> bool { match call { - RuntimeCall::Sudo(_) // System and Timestamp are required for block production - | RuntimeCall::System(_) + RuntimeCall::System(_) | RuntimeCall::Timestamp(_) - | RuntimeCall::SafeMode(_) + | RuntimeCall::ParachainSystem(_) + | RuntimeCall::Sudo(_) | RuntimeCall::TxPause(_) => true, _ => false, } } } -/// Calls that cannot be paused by the tx-pause pallet. -pub struct TxPauseWhitelistedCalls; -/// All calls can be paused, except Sudo calls. -impl Contains> for TxPauseWhitelistedCalls { - fn contains(full_name: &pallet_tx_pause::RuntimeCallNameOf) -> bool { - matches!(full_name.0.as_slice(), b"Sudo") - } -} - 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; - // The 'Success' value below represents the number of blocks that the origin may induce safe mode + type EnterDuration = ConstU32<{ 4 * HOURS }>; + type EnterDepositAmount = (); + type ExtendDuration = ConstU32<{ 2 * HOURS }>; + type ExtendDepositAmount = (); + // The 'Success' values below represent the number of blocks that the origin may induce safe mode type ForceEnterOrigin = - EnsureWithSuccess; + EnsureWithSuccess>; type ForceExtendOrigin = - EnsureWithSuccess; - type ForceExitOrigin = EnsureRootOrAllTechnicalCommittee; - type ForceDepositOrigin = EnsureRootOrAllTechnicalCommittee; - type ReleaseDelay = ReleaseDelay; - type Notify = (); + EnsureWithSuccess>; + type ForceExitOrigin = EnsureRootOrTwoThirdsTechnicalCommittee; + type ForceDepositOrigin = EnsureRootOrTwoThirdsTechnicalCommittee; + type ReleaseDelay = (); + type Notify = DappStaking; type WeightInfo = pallet_safe_mode::weights::SubstrateWeight; } @@ -1598,7 +1583,7 @@ impl pallet_tx_pause::Config for Runtime { type RuntimeCall = RuntimeCall; type PauseOrigin = EnsureRootOrHalfTechnicalCommittee; type UnpauseOrigin = EnsureRootOrHalfTechnicalCommittee; - type WhitelistedCalls = TxPauseWhitelistedCalls; + type WhitelistedCalls = (); type MaxNameLen = ConstU32<256>; type WeightInfo = pallet_tx_pause::weights::SubstrateWeight; } @@ -1675,12 +1660,11 @@ construct_runtime!( Treasury: pallet_treasury:: = 107, CommunityTreasury: pallet_treasury:: = 108, CollectiveProxy: pallet_collective_proxy = 109, + SafeMode: pallet_safe_mode = 110, + TxPause: pallet_tx_pause = 111, MultiBlockMigrations: pallet_migrations = 120, - SafeMode: pallet_safe_mode = 130, - TxPause: pallet_tx_pause = 131, - #[cfg(feature = "runtime-benchmarks")] VestingMBM: vesting_mbm = 250, }