mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 06:48:01 +00:00
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:
committed by
Bastian Köcher
parent
a97dedb50f
commit
ff342fafa9
@@ -61,17 +61,16 @@ use bp_messages::{
|
||||
OutboundMessageDetails, Parameter as MessagesParameter, UnrewardedRelayer,
|
||||
UnrewardedRelayersState,
|
||||
};
|
||||
use bp_runtime::{ChainId, Size};
|
||||
use bp_runtime::{ChainId, OwnedBridgeModule, Size};
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
fail,
|
||||
traits::Get,
|
||||
weights::{Pays, PostDispatchInfo},
|
||||
};
|
||||
use frame_system::RawOrigin;
|
||||
use num_traits::{SaturatingAdd, Zero};
|
||||
use sp_core::H256;
|
||||
use sp_runtime::traits::{BadOrigin, Convert};
|
||||
use sp_runtime::traits::Convert;
|
||||
use sp_std::{
|
||||
cell::RefCell, cmp::PartialOrd, collections::vec_deque::VecDeque, marker::PhantomData,
|
||||
ops::RangeInclusive, prelude::*,
|
||||
@@ -217,6 +216,18 @@ pub mod pallet {
|
||||
#[pallet::without_storage_info]
|
||||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
||||
|
||||
impl<T: Config<I>, I: 'static> OwnedBridgeModule<T> for Pallet<T, I> {
|
||||
const LOG_TARGET: &'static str = "runtime::bridge-messages";
|
||||
const OPERATING_MODE_KEY: &'static str = "PalletOperatingMode";
|
||||
type OwnerStorage = PalletOwner<T, I>;
|
||||
type OperatingMode = OperatingMode;
|
||||
type OperatingModeStorage = PalletOperatingMode<T, I>;
|
||||
|
||||
fn is_halted() -> bool {
|
||||
Self::OperatingModeStorage::get() == OperatingMode::Halted
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
/// Change `PalletOwner`.
|
||||
@@ -224,18 +235,7 @@ pub mod pallet {
|
||||
/// May only be called either by root, or by `PalletOwner`.
|
||||
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
|
||||
pub fn set_owner(origin: OriginFor<T>, new_owner: Option<T::AccountId>) -> DispatchResult {
|
||||
ensure_owner_or_root::<T, I>(origin)?;
|
||||
match new_owner {
|
||||
Some(new_owner) => {
|
||||
PalletOwner::<T, I>::put(&new_owner);
|
||||
log::info!(target: "runtime::bridge-messages", "Setting pallet Owner to: {:?}", new_owner);
|
||||
},
|
||||
None => {
|
||||
PalletOwner::<T, I>::kill();
|
||||
log::info!(target: "runtime::bridge-messages", "Removed Owner of pallet.");
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
<Self as OwnedBridgeModule<_>>::set_owner(origin, new_owner)
|
||||
}
|
||||
|
||||
/// Halt or resume all/some pallet operations.
|
||||
@@ -246,14 +246,7 @@ pub mod pallet {
|
||||
origin: OriginFor<T>,
|
||||
operating_mode: OperatingMode,
|
||||
) -> DispatchResult {
|
||||
ensure_owner_or_root::<T, I>(origin)?;
|
||||
PalletOperatingMode::<T, I>::put(operating_mode);
|
||||
log::info!(
|
||||
target: "runtime::bridge-messages",
|
||||
"Setting messages pallet operating mode to {:?}.",
|
||||
operating_mode,
|
||||
);
|
||||
Ok(())
|
||||
<Self as OwnedBridgeModule<_>>::set_operating_mode(origin, operating_mode)
|
||||
}
|
||||
|
||||
/// Update pallet parameter.
|
||||
@@ -267,7 +260,7 @@ pub mod pallet {
|
||||
origin: OriginFor<T>,
|
||||
parameter: T::Parameter,
|
||||
) -> DispatchResult {
|
||||
ensure_owner_or_root::<T, I>(origin)?;
|
||||
Self::ensure_owner_or_root(origin)?;
|
||||
parameter.save();
|
||||
Self::deposit_event(Event::ParameterUpdated(parameter));
|
||||
Ok(())
|
||||
@@ -297,7 +290,7 @@ pub mod pallet {
|
||||
nonce: MessageNonce,
|
||||
additional_fee: T::OutboundMessageFee,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_not_halted::<T, I>()?;
|
||||
Self::ensure_not_halted().map_err(Error::<T, I>::BridgeModule)?;
|
||||
// if someone tries to pay for already-delivered message, we're rejecting this intention
|
||||
// (otherwise this additional fee will be locked forever in relayers fund)
|
||||
//
|
||||
@@ -368,7 +361,7 @@ pub mod pallet {
|
||||
messages_count: u32,
|
||||
dispatch_weight: Weight,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_not_halted::<T, I>()?;
|
||||
Self::ensure_not_halted().map_err(Error::<T, I>::BridgeModule)?;
|
||||
let relayer_id_at_this_chain = ensure_signed(origin)?;
|
||||
|
||||
// reject transactions that are declaring too many messages
|
||||
@@ -512,7 +505,7 @@ pub mod pallet {
|
||||
proof: MessagesDeliveryProofOf<T, I>,
|
||||
relayers_state: UnrewardedRelayersState,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_not_halted::<T, I>()?;
|
||||
Self::ensure_not_halted().map_err(Error::<T, I>::BridgeModule)?;
|
||||
|
||||
// why do we need to know the weight of this (`receive_messages_delivery_proof`) call?
|
||||
// Because we may want to return some funds for messages that are not processed by the
|
||||
@@ -669,8 +662,8 @@ pub mod pallet {
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T, I = ()> {
|
||||
/// All pallet operations are halted.
|
||||
Halted,
|
||||
/// Pallet is not in Normal operating mode.
|
||||
NotOperatingNormally,
|
||||
/// Message has been treated as invalid by chain verifier.
|
||||
MessageRejectedByChainVerifier,
|
||||
/// Message has been treated as invalid by lane verifier.
|
||||
@@ -695,6 +688,8 @@ pub mod pallet {
|
||||
/// The number of actually confirmed messages is going to be larger than the number of
|
||||
/// messages in the proof. This may mean that this or bridged chain storage is corrupted.
|
||||
TryingToConfirmMoreMessagesThanExpected,
|
||||
/// Error generated by the `OwnedBridgeModule` trait.
|
||||
BridgeModule(bp_runtime::OwnedBridgeModuleError),
|
||||
}
|
||||
|
||||
/// Optional pallet owner.
|
||||
@@ -975,30 +970,10 @@ where
|
||||
relayers_rewards
|
||||
}
|
||||
|
||||
/// Ensure that the origin is either root, or `PalletOwner`.
|
||||
fn ensure_owner_or_root<T: Config<I>, I: 'static>(origin: T::Origin) -> Result<(), BadOrigin> {
|
||||
match origin.into() {
|
||||
Ok(RawOrigin::Root) => Ok(()),
|
||||
Ok(RawOrigin::Signed(ref signer))
|
||||
if Some(signer) == Pallet::<T, I>::module_owner().as_ref() =>
|
||||
Ok(()),
|
||||
_ => Err(BadOrigin),
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that the pallet is in normal operational mode.
|
||||
fn ensure_normal_operating_mode<T: Config<I>, I: 'static>() -> Result<(), Error<T, I>> {
|
||||
if PalletOperatingMode::<T, I>::get() != OperatingMode::Normal {
|
||||
Err(Error::<T, I>::Halted)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that the pallet is not halted.
|
||||
fn ensure_not_halted<T: Config<I>, I: 'static>() -> Result<(), Error<T, I>> {
|
||||
if PalletOperatingMode::<T, I>::get() == OperatingMode::Halted {
|
||||
Err(Error::<T, I>::Halted)
|
||||
Err(Error::<T, I>::NotOperatingNormally)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -1442,12 +1417,12 @@ mod tests {
|
||||
REGULAR_PAYLOAD,
|
||||
REGULAR_PAYLOAD.declared_weight,
|
||||
),
|
||||
Error::<TestRuntime, ()>::Halted,
|
||||
Error::<TestRuntime, ()>::NotOperatingNormally,
|
||||
);
|
||||
|
||||
assert_noop!(
|
||||
Pallet::<TestRuntime>::increase_message_fee(Origin::signed(1), TEST_LANE_ID, 1, 1,),
|
||||
Error::<TestRuntime, ()>::Halted,
|
||||
Error::<TestRuntime, ()>::BridgeModule(bp_runtime::OwnedBridgeModuleError::Halted),
|
||||
);
|
||||
|
||||
assert_noop!(
|
||||
@@ -1458,7 +1433,7 @@ mod tests {
|
||||
1,
|
||||
REGULAR_PAYLOAD.declared_weight,
|
||||
),
|
||||
Error::<TestRuntime, ()>::Halted,
|
||||
Error::<TestRuntime, ()>::BridgeModule(bp_runtime::OwnedBridgeModuleError::Halted),
|
||||
);
|
||||
|
||||
assert_noop!(
|
||||
@@ -1480,7 +1455,7 @@ mod tests {
|
||||
last_delivered_nonce: 1,
|
||||
},
|
||||
),
|
||||
Error::<TestRuntime, ()>::Halted,
|
||||
Error::<TestRuntime, ()>::BridgeModule(bp_runtime::OwnedBridgeModuleError::Halted),
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1500,7 +1475,7 @@ mod tests {
|
||||
REGULAR_PAYLOAD,
|
||||
REGULAR_PAYLOAD.declared_weight,
|
||||
),
|
||||
Error::<TestRuntime, ()>::Halted,
|
||||
Error::<TestRuntime, ()>::NotOperatingNormally,
|
||||
);
|
||||
|
||||
assert_ok!(Pallet::<TestRuntime>::increase_message_fee(
|
||||
|
||||
Reference in New Issue
Block a user