Unify the operating mode for bridge pallets (#1483)

Unify the operating mode for bridge pallets

- define the OperationMode trait and BasicOperatingMode enum
- use the OperationMode trait in all the bridge pallets
- use BasicOperatingMode instead of IsHalted for the Grandpa pallet
- use BasicOperatingMode as part of MessagesOperatingMode

Signed-off-by: Serban Iorga <serban@parity.io>
This commit is contained in:
Serban Iorga
2022-06-28 17:39:07 +03:00
committed by Bastian Köcher
parent ff342fafa9
commit f8ff3c9142
11 changed files with 218 additions and 104 deletions
+3 -2
View File
@@ -19,6 +19,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use bp_runtime::BasicOperatingMode;
use codec::{Codec, Decode, Encode, EncodeLike};
use core::{clone::Clone, cmp::Eq, default::Default, fmt::Debug};
use scale_info::TypeInfo;
@@ -66,8 +67,8 @@ pub struct InitializationData<H: HeaderT> {
pub authority_list: AuthorityList,
/// The ID of the initial authority set.
pub set_id: SetId,
/// Should the pallet block transaction immediately after initialization.
pub is_halted: bool,
/// Pallet operating mode.
pub operating_mode: BasicOperatingMode,
}
/// base trait for verifying transaction inclusion proofs.
@@ -17,18 +17,18 @@
//! Storage keys of bridge GRANDPA pallet.
/// Name of the `IsHalted` storage value.
pub const IS_HALTED_VALUE_NAME: &str = "IsHalted";
pub const PALLET_OPERATING_MODE_VALUE_NAME: &str = "PalletOperatingMode";
/// Name of the `BestFinalized` storage value.
pub const BEST_FINALIZED_VALUE_NAME: &str = "BestFinalized";
use sp_core::storage::StorageKey;
/// Storage key of the `IsHalted` flag in the runtime storage.
pub fn is_halted_key(pallet_prefix: &str) -> StorageKey {
/// Storage key of the `PalletOperatingMode` variable in the runtime storage.
pub fn pallet_operating_mode_key(pallet_prefix: &str) -> StorageKey {
StorageKey(
bp_runtime::storage_value_final_key(
pallet_prefix.as_bytes(),
IS_HALTED_VALUE_NAME.as_bytes(),
PALLET_OPERATING_MODE_VALUE_NAME.as_bytes(),
)
.to_vec(),
)
@@ -51,13 +51,13 @@ mod tests {
use hex_literal::hex;
#[test]
fn is_halted_key_computed_properly() {
fn pallet_operating_mode_key_computed_properly() {
// If this test fails, then something has been changed in module storage that is breaking
// compatibility with previous pallet.
let storage_key = is_halted_key("BridgeGrandpa").0;
let storage_key = pallet_operating_mode_key("BridgeGrandpa").0;
assert_eq!(
storage_key,
hex!("0b06f475eddb98cf933a12262e0388de9611a984bbd04e2fd39f97bbc006115f").to_vec(),
hex!("0b06f475eddb98cf933a12262e0388de0f4cf0917788d791142ff6c1f216e7b3").to_vec(),
"Unexpected storage key: {}",
hex::encode(&storage_key),
);
+15 -7
View File
@@ -32,14 +32,15 @@ pub mod storage_keys;
pub mod target_chain;
// Weight is reexported to avoid additional frame-support dependencies in related crates.
use bp_runtime::{BasicOperatingMode, OperatingMode};
pub use frame_support::weights::Weight;
/// Messages pallet operating mode.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum OperatingMode {
/// Normal mode, when all operations are allowed.
Normal,
pub enum MessagesOperatingMode {
/// Basic operating mode (Normal/Halted)
Basic(BasicOperatingMode),
/// The pallet is not accepting outbound messages. Inbound messages and receiving proofs
/// are still accepted.
///
@@ -48,13 +49,20 @@ pub enum OperatingMode {
/// queued messages to the bridged chain. Once upgrade is completed, the mode may be switched
/// back to `Normal`.
RejectingOutboundMessages,
/// The pallet is halted. All operations (except operating mode change) are prohibited.
Halted,
}
impl Default for OperatingMode {
impl Default for MessagesOperatingMode {
fn default() -> Self {
OperatingMode::Normal
MessagesOperatingMode::Basic(BasicOperatingMode::Normal)
}
}
impl OperatingMode for MessagesOperatingMode {
fn is_halted(&self) -> bool {
match self {
Self::Basic(operating_mode) => operating_mode.is_halted(),
_ => false,
}
}
}
+2
View File
@@ -11,6 +11,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
hash-db = { version = "0.15.2", default-features = false }
num-traits = { version = "0.2", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
# Substrate Dependencies
@@ -35,6 +36,7 @@ std = [
"hash-db/std",
"num-traits/std",
"scale-info/std",
"serde",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
+34 -10
View File
@@ -269,18 +269,47 @@ pub enum OwnedBridgeModuleError {
Halted,
}
/// Operating mode for a bridge module.
pub trait OperatingMode: Send + Copy + Debug + FullCodec {
// Returns true if the bridge module is halted.
fn is_halted(&self) -> bool;
}
/// Basic operating modes for a bridges module (Normal/Halted).
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum BasicOperatingMode {
/// Normal mode, when all operations are allowed.
Normal,
/// The pallet is halted. All operations (except operating mode change) are prohibited.
Halted,
}
impl Default for BasicOperatingMode {
fn default() -> Self {
Self::Normal
}
}
impl OperatingMode for BasicOperatingMode {
fn is_halted(&self) -> bool {
*self == BasicOperatingMode::Halted
}
}
/// Bridge module that has owner and operating mode
pub trait OwnedBridgeModule<T: frame_system::Config> {
/// The target that will be used when publishing logs related to this module.
const LOG_TARGET: &'static str;
const OPERATING_MODE_KEY: &'static str;
type OwnerStorage: StorageValue<T::AccountId, Query = Option<T::AccountId>>;
type OperatingMode: Copy + Debug + FullCodec;
type OperatingModeStorage: StorageValue<Self::OperatingMode>;
type OperatingMode: OperatingMode;
type OperatingModeStorage: StorageValue<Self::OperatingMode, Query = Self::OperatingMode>;
/// Check if the module is halted.
fn is_halted() -> bool;
fn is_halted() -> bool {
Self::OperatingModeStorage::get().is_halted()
}
/// Ensure that the origin is either root, or `PalletOwner`.
fn ensure_owner_or_root(origin: T::Origin) -> Result<(), BadOrigin> {
@@ -325,12 +354,7 @@ pub trait OwnedBridgeModule<T: frame_system::Config> {
) -> DispatchResult {
Self::ensure_owner_or_root(origin)?;
Self::OperatingModeStorage::put(operating_mode);
log::info!(
target: Self::LOG_TARGET,
"Setting operating mode ( {} = {:?}).",
Self::OPERATING_MODE_KEY,
operating_mode
);
log::info!(target: Self::LOG_TARGET, "Setting operating mode to {:?}.", operating_mode);
Ok(())
}
}