From 82704319df7b5e9adfd2979e6cbfa5a144d6c938 Mon Sep 17 00:00:00 2001 From: Janislav Date: Mon, 6 Nov 2023 17:45:14 +0100 Subject: [PATCH] feature/PRO-898/instantiable-safe-mode (#4168) Co-authored-by: Daniel --- state-chain/pallets/cf-broadcast/src/lib.rs | 20 +++++++++++--- state-chain/pallets/cf-broadcast/src/mock.rs | 2 +- state-chain/pallets/cf-environment/src/lib.rs | 2 +- .../pallets/cf-environment/src/migrations.rs | 2 ++ .../cf-environment/src/migrations/v6.rs | 15 +++++++++++ state-chain/pallets/cf-vaults/src/lib.rs | 26 ++++++++++++++----- state-chain/pallets/cf-vaults/src/mock.rs | 2 +- state-chain/pallets/cf-vaults/src/tests.rs | 6 ++--- state-chain/runtime/src/safe_mode.rs | 10 ++++--- 9 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 state-chain/pallets/cf-environment/src/migrations/v6.rs diff --git a/state-chain/pallets/cf-broadcast/src/lib.rs b/state-chain/pallets/cf-broadcast/src/lib.rs index 7dab27ce22..136a560f79 100644 --- a/state-chain/pallets/cf-broadcast/src/lib.rs +++ b/state-chain/pallets/cf-broadcast/src/lib.rs @@ -9,10 +9,24 @@ mod tests; pub mod migrations; pub mod weights; use cf_primitives::{BroadcastId, ThresholdSignatureRequestId}; -use cf_traits::{impl_pallet_safe_mode, GetBlockHeight}; +use cf_traits::{GetBlockHeight, SafeMode}; +use frame_support::RuntimeDebug; +use sp_std::marker; pub use weights::WeightInfo; -impl_pallet_safe_mode!(PalletSafeMode; retry_enabled); +#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Copy, Clone, PartialEq, Eq, RuntimeDebug)] +#[scale_info(skip_type_params(I))] +pub struct PalletSafeMode { + pub retry_enabled: bool, + #[doc(hidden)] + #[codec(skip)] + _phantom: marker::PhantomData, +} + +impl SafeMode for PalletSafeMode { + const CODE_RED: Self = PalletSafeMode { retry_enabled: false, _phantom: marker::PhantomData }; + const CODE_GREEN: Self = PalletSafeMode { retry_enabled: true, _phantom: marker::PhantomData }; +} use cf_chains::{ ApiCall, Chain, ChainCrypto, FeeRefundCalculator, TransactionBuilder, TransactionMetadata as _, @@ -199,7 +213,7 @@ pub mod pallet { type KeyProvider: KeyProvider<::ChainCrypto>; /// Safe Mode access. - type SafeMode: Get; + type SafeMode: Get>; /// The save mode block margin type SafeModeBlockMargin: Get>; diff --git a/state-chain/pallets/cf-broadcast/src/mock.rs b/state-chain/pallets/cf-broadcast/src/mock.rs index 25d6383de8..0e0d05c0cf 100644 --- a/state-chain/pallets/cf-broadcast/src/mock.rs +++ b/state-chain/pallets/cf-broadcast/src/mock.rs @@ -135,7 +135,7 @@ impl OnBroadcastReady for MockBroadcastReadyProvider { type ApiCall = MockApiCall; } -impl_mock_runtime_safe_mode! { broadcast: PalletSafeMode } +impl_mock_runtime_safe_mode! { broadcast: PalletSafeMode } impl pallet_cf_broadcast::Config for Test { type RuntimeEvent = RuntimeEvent; diff --git a/state-chain/pallets/cf-environment/src/lib.rs b/state-chain/pallets/cf-environment/src/lib.rs index 08d7a9cfe0..c8e66772b7 100644 --- a/state-chain/pallets/cf-environment/src/lib.rs +++ b/state-chain/pallets/cf-environment/src/lib.rs @@ -31,7 +31,7 @@ pub mod weights; pub use weights::WeightInfo; mod migrations; -pub const PALLET_VERSION: StorageVersion = StorageVersion::new(5); +pub const PALLET_VERSION: StorageVersion = StorageVersion::new(6); type SignatureNonce = u64; diff --git a/state-chain/pallets/cf-environment/src/migrations.rs b/state-chain/pallets/cf-environment/src/migrations.rs index 49027375d6..c07ad8e95a 100644 --- a/state-chain/pallets/cf-environment/src/migrations.rs +++ b/state-chain/pallets/cf-environment/src/migrations.rs @@ -1,10 +1,12 @@ pub mod v3; pub mod v4; pub mod v5; +pub mod v6; use cf_runtime_upgrade_utilities::VersionedMigration; pub type PalletMigration = ( VersionedMigration, v4::Migration, 3, 4>, VersionedMigration, v5::Migration, 4, 5>, + VersionedMigration, v6::Migration, 5, 6>, ); diff --git a/state-chain/pallets/cf-environment/src/migrations/v6.rs b/state-chain/pallets/cf-environment/src/migrations/v6.rs new file mode 100644 index 0000000000..1b0816dfc9 --- /dev/null +++ b/state-chain/pallets/cf-environment/src/migrations/v6.rs @@ -0,0 +1,15 @@ +use crate::*; + +use cf_traits::SafeMode; +use frame_support::traits::OnRuntimeUpgrade; +use sp_std::marker::PhantomData; + +pub struct Migration(PhantomData); + +impl OnRuntimeUpgrade for Migration { + fn on_runtime_upgrade() -> frame_support::weights::Weight { + RuntimeSafeMode::::set(SafeMode::CODE_GREEN); + + Weight::zero() + } +} diff --git a/state-chain/pallets/cf-vaults/src/lib.rs b/state-chain/pallets/cf-vaults/src/lib.rs index e0f4200c2b..e80d344711 100644 --- a/state-chain/pallets/cf-vaults/src/lib.rs +++ b/state-chain/pallets/cf-vaults/src/lib.rs @@ -8,10 +8,9 @@ use cf_primitives::{ }; use cf_runtime_utilities::{EnumVariant, StorageDecodeVariant}; use cf_traits::{ - impl_pallet_safe_mode, offence_reporting::OffenceReporter, AccountRoleRegistry, AsyncResult, - Broadcaster, Chainflip, CurrentEpochIndex, EpochKey, GetBlockHeight, KeyProvider, KeyState, - SafeMode, SetSafeMode, Slashing, ThresholdSigner, VaultKeyWitnessedHandler, VaultRotator, - VaultStatus, + offence_reporting::OffenceReporter, AccountRoleRegistry, AsyncResult, Broadcaster, Chainflip, + CurrentEpochIndex, EpochKey, GetBlockHeight, KeyProvider, KeyState, SafeMode, SetSafeMode, + Slashing, ThresholdSigner, VaultKeyWitnessedHandler, VaultRotator, VaultStatus, }; use frame_support::{ pallet_prelude::*, @@ -23,6 +22,7 @@ pub use pallet::*; use sp_std::{ collections::{btree_map::BTreeMap, btree_set::BTreeSet}, iter::Iterator, + marker, prelude::*, }; @@ -63,7 +63,21 @@ pub type KeygenResponseStatus = pub type KeyHandoverResponseStatus = ResponseStatus, KeyHandoverFailureVoters, I>; -impl_pallet_safe_mode!(PalletSafeMode; slashing_enabled); +#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Copy, Clone, PartialEq, Eq, RuntimeDebug)] +#[scale_info(skip_type_params(I))] +pub struct PalletSafeMode { + pub slashing_enabled: bool, + #[doc(hidden)] + #[codec(skip)] + _phantom: marker::PhantomData, +} + +impl SafeMode for PalletSafeMode { + const CODE_RED: Self = + PalletSafeMode { slashing_enabled: false, _phantom: marker::PhantomData }; + const CODE_GREEN: Self = + PalletSafeMode { slashing_enabled: true, _phantom: marker::PhantomData }; +} /// The current status of a vault rotation. #[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebugNoBound, EnumVariant)] @@ -197,7 +211,7 @@ pub mod pallet { type Slasher: Slashing>; /// For activating Safe mode: CODE RED for the chain. - type SafeMode: Get + SafeMode + SetSafeMode; + type SafeMode: Get> + SafeMode + SetSafeMode; type ChainTracking: GetBlockHeight; diff --git a/state-chain/pallets/cf-vaults/src/mock.rs b/state-chain/pallets/cf-vaults/src/mock.rs index d67985ece8..aee43722cd 100644 --- a/state-chain/pallets/cf-vaults/src/mock.rs +++ b/state-chain/pallets/cf-vaults/src/mock.rs @@ -206,7 +206,7 @@ impl Slashing for MockSlasher { } } -impl_mock_runtime_safe_mode! { vault: PalletSafeMode } +impl_mock_runtime_safe_mode! { vault: PalletSafeMode<()> } impl pallet_cf_vaults::Config for Test { type RuntimeEvent = RuntimeEvent; diff --git a/state-chain/pallets/cf-vaults/src/tests.rs b/state-chain/pallets/cf-vaults/src/tests.rs index 77e3834357..7f108be598 100644 --- a/state-chain/pallets/cf-vaults/src/tests.rs +++ b/state-chain/pallets/cf-vaults/src/tests.rs @@ -1,6 +1,6 @@ #![cfg(test)] -use core::panic; +use core::{marker, panic}; use crate::{ mock::*, CeremonyId, Error, Event as PalletEvent, KeyHandoverResolutionPendingSince, @@ -1119,14 +1119,14 @@ fn when_set_agg_key_with_agg_key_not_required_we_skip_to_completion() { fn dont_slash_in_safe_mode() { new_test_ext().execute_with(|| { MockRuntimeSafeMode::set_safe_mode(MockRuntimeSafeMode { - vault: crate::PalletSafeMode { slashing_enabled: false }, + vault: crate::PalletSafeMode { slashing_enabled: false, _phantom: marker::PhantomData }, }); keygen_failure(&[BOB, CHARLIE]); assert!(MockSlasher::slash_count(BOB) == 0); assert!(MockSlasher::slash_count(CHARLIE) == 0); MockRuntimeSafeMode::set_safe_mode(MockRuntimeSafeMode { - vault: crate::PalletSafeMode { slashing_enabled: true }, + vault: crate::PalletSafeMode { slashing_enabled: true, _phantom: marker::PhantomData }, }); keygen_failure(&[BOB, CHARLIE]); assert!(MockSlasher::slash_count(BOB) == 1); diff --git a/state-chain/runtime/src/safe_mode.rs b/state-chain/runtime/src/safe_mode.rs index f9a6a76cdd..e782e3440d 100644 --- a/state-chain/runtime/src/safe_mode.rs +++ b/state-chain/runtime/src/safe_mode.rs @@ -1,6 +1,6 @@ //! For filtering runtime calls and other related utilities. -use crate::{Runtime, RuntimeCall}; +use crate::{BitcoinInstance, EthereumInstance, PolkadotInstance, Runtime, RuntimeCall}; use cf_traits::{impl_runtime_safe_mode, CallDispatchFilter}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -15,9 +15,13 @@ impl_runtime_safe_mode! { validator: pallet_cf_validator::PalletSafeMode, pools: pallet_cf_pools::PalletSafeMode, reputation: pallet_cf_reputation::PalletSafeMode, - vault: pallet_cf_vaults::PalletSafeMode, + vault_ethereum: pallet_cf_vaults::PalletSafeMode, + vault_bitcoin: pallet_cf_vaults::PalletSafeMode, + vault_polkadot: pallet_cf_vaults::PalletSafeMode, + broadcast_ethereum: pallet_cf_broadcast::PalletSafeMode, + broadcast_bitcoin: pallet_cf_broadcast::PalletSafeMode, + broadcast_polkadot: pallet_cf_broadcast::PalletSafeMode, witnesser: pallet_cf_witnesser::PalletSafeMode, - broadcast: pallet_cf_broadcast::PalletSafeMode, } /// Contains permissions for different Runtime calls.