Avoid duplicate function definitions

Avoid duplicate function definitions for:
- ensure_owner_or_root()
- ensure_not_halted()
- set_owner()
- set_operating_mode() / set_operational()

Signed-off-by: Serban Iorga <serban@parity.io>
This commit is contained in:
Serban Iorga
2022-06-24 21:23:01 +03:00
committed by Bastian Köcher
parent a97dedb50f
commit ff342fafa9
5 changed files with 150 additions and 124 deletions
+81 -3
View File
@@ -18,11 +18,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use frame_support::{RuntimeDebug, StorageHasher};
use codec::{Decode, Encode, FullCodec};
use frame_support::{
log, pallet_prelude::DispatchResult, PalletError, RuntimeDebug, StorageHasher, StorageValue,
};
use frame_system::RawOrigin;
use scale_info::TypeInfo;
use sp_core::{hash::H256, storage::StorageKey};
use sp_io::hashing::blake2_256;
use sp_std::{convert::TryFrom, vec, vec::Vec};
use sp_runtime::traits::BadOrigin;
use sp_std::{convert::TryFrom, fmt::Debug, vec, vec::Vec};
pub use chain::{
AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, EncodedOrDecodedCall, HashOf,
@@ -257,6 +262,79 @@ pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey {
StorageKey(final_key)
}
/// Error generated by the `OwnedBridgeModule` trait.
#[derive(Encode, Decode, TypeInfo, PalletError)]
pub enum OwnedBridgeModuleError {
/// All pallet operations are halted.
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>;
/// Check if the module is halted.
fn is_halted() -> bool;
/// Ensure that the origin is either root, or `PalletOwner`.
fn ensure_owner_or_root(origin: T::Origin) -> Result<(), BadOrigin> {
match origin.into() {
Ok(RawOrigin::Root) => Ok(()),
Ok(RawOrigin::Signed(ref signer))
if Self::OwnerStorage::get().as_ref() == Some(signer) =>
Ok(()),
_ => Err(BadOrigin),
}
}
/// Ensure that the module is not halted.
fn ensure_not_halted() -> Result<(), OwnedBridgeModuleError> {
match Self::is_halted() {
true => Err(OwnedBridgeModuleError::Halted),
false => Ok(()),
}
}
/// Change the owner of the module.
fn set_owner(origin: T::Origin, maybe_owner: Option<T::AccountId>) -> DispatchResult {
Self::ensure_owner_or_root(origin)?;
match maybe_owner {
Some(owner) => {
Self::OwnerStorage::put(&owner);
log::info!(target: Self::LOG_TARGET, "Setting pallet Owner to: {:?}", owner);
},
None => {
Self::OwnerStorage::kill();
log::info!(target: Self::LOG_TARGET, "Removed Owner of pallet.");
},
}
Ok(())
}
/// Halt or resume all/some module operations.
fn set_operating_mode(
origin: T::Origin,
operating_mode: Self::OperatingMode,
) -> 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
);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;