mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 07:31:02 +00:00
Bridge: slash destination may be an explicit account (#4106)
Extracted to a separate PR as requested here: https://github.com/paritytech/parity-bridges-common/pull/2873#discussion_r1562459573
This commit is contained in:
committed by
GitHub
parent
2bc4ed1153
commit
6acf4787e1
@@ -23,7 +23,7 @@ use crate::messages_call_ext::{
|
||||
CallHelper as MessagesCallHelper, CallInfo as MessagesCallInfo, MessagesCallSubType,
|
||||
};
|
||||
use bp_messages::{LaneId, MessageNonce};
|
||||
use bp_relayers::{RewardsAccountOwner, RewardsAccountParams};
|
||||
use bp_relayers::{ExplicitOrAccountParams, RewardsAccountOwner, RewardsAccountParams};
|
||||
use bp_runtime::{Chain, Parachain, ParachainIdOf, RangeInclusiveExt, StaticStrProvider};
|
||||
use codec::{Codec, Decode, Encode};
|
||||
use frame_support::{
|
||||
@@ -589,7 +589,10 @@ where
|
||||
);
|
||||
},
|
||||
RelayerAccountAction::Slash(relayer, slash_account) =>
|
||||
RelayersPallet::<T::Runtime>::slash_and_deregister(&relayer, slash_account),
|
||||
RelayersPallet::<T::Runtime>::slash_and_deregister(
|
||||
&relayer,
|
||||
ExplicitOrAccountParams::Params(slash_account),
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -106,7 +106,7 @@ benchmarks! {
|
||||
let slash_destination = RewardsAccountParams::new(lane, *b"test", RewardsAccountOwner::ThisChain);
|
||||
T::prepare_rewards_account(slash_destination, Zero::zero());
|
||||
}: {
|
||||
crate::Pallet::<T>::slash_and_deregister(&relayer, slash_destination)
|
||||
crate::Pallet::<T>::slash_and_deregister(&relayer, slash_destination.into())
|
||||
}
|
||||
verify {
|
||||
assert!(!crate::Pallet::<T>::is_registration_active(&relayer));
|
||||
|
||||
@@ -21,7 +21,8 @@
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use bp_relayers::{
|
||||
PaymentProcedure, Registration, RelayerRewardsKeyProvider, RewardsAccountParams, StakeAndSlash,
|
||||
ExplicitOrAccountParams, PaymentProcedure, Registration, RelayerRewardsKeyProvider,
|
||||
RewardsAccountParams, StakeAndSlash,
|
||||
};
|
||||
use bp_runtime::StorageDoubleMapKeyProvider;
|
||||
use frame_support::fail;
|
||||
@@ -242,7 +243,7 @@ pub mod pallet {
|
||||
/// It may fail inside, but error is swallowed and we only log it.
|
||||
pub fn slash_and_deregister(
|
||||
relayer: &T::AccountId,
|
||||
slash_destination: RewardsAccountParams,
|
||||
slash_destination: ExplicitOrAccountParams<T::AccountId>,
|
||||
) {
|
||||
let registration = match RegisteredRelayers::<T>::take(relayer) {
|
||||
Some(registration) => registration,
|
||||
@@ -259,7 +260,7 @@ pub mod pallet {
|
||||
|
||||
match T::StakeAndSlash::repatriate_reserved(
|
||||
relayer,
|
||||
slash_destination,
|
||||
slash_destination.clone(),
|
||||
registration.stake,
|
||||
) {
|
||||
Ok(failed_to_slash) if failed_to_slash.is_zero() => {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
//! Code that allows `NamedReservableCurrency` to be used as a `StakeAndSlash`
|
||||
//! mechanism of the relayers pallet.
|
||||
|
||||
use bp_relayers::{PayRewardFromAccount, RewardsAccountParams, StakeAndSlash};
|
||||
use bp_relayers::{ExplicitOrAccountParams, PayRewardFromAccount, StakeAndSlash};
|
||||
use codec::Codec;
|
||||
use frame_support::traits::{tokens::BalanceStatus, NamedReservableCurrency};
|
||||
use sp_runtime::{traits::Get, DispatchError, DispatchResult};
|
||||
@@ -55,11 +55,14 @@ where
|
||||
|
||||
fn repatriate_reserved(
|
||||
relayer: &AccountId,
|
||||
beneficiary: RewardsAccountParams,
|
||||
beneficiary: ExplicitOrAccountParams<AccountId>,
|
||||
amount: Currency::Balance,
|
||||
) -> Result<Currency::Balance, DispatchError> {
|
||||
let beneficiary_account =
|
||||
PayRewardFromAccount::<(), AccountId>::rewards_account(beneficiary);
|
||||
let beneficiary_account = match beneficiary {
|
||||
ExplicitOrAccountParams::Explicit(account) => account,
|
||||
ExplicitOrAccountParams::Params(params) =>
|
||||
PayRewardFromAccount::<(), AccountId>::rewards_account(params),
|
||||
};
|
||||
Currency::repatriate_reserved_named(
|
||||
&ReserveId::get(),
|
||||
relayer,
|
||||
@@ -134,7 +137,11 @@ mod tests {
|
||||
Balances::mint_into(&beneficiary_account, expected_balance).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
TestStakeAndSlash::repatriate_reserved(&1, beneficiary, test_stake()),
|
||||
TestStakeAndSlash::repatriate_reserved(
|
||||
&1,
|
||||
ExplicitOrAccountParams::Params(beneficiary),
|
||||
test_stake()
|
||||
),
|
||||
Ok(test_stake())
|
||||
);
|
||||
assert_eq!(Balances::free_balance(1), 0);
|
||||
@@ -146,7 +153,11 @@ mod tests {
|
||||
Balances::mint_into(&2, test_stake() * 2).unwrap();
|
||||
TestStakeAndSlash::reserve(&2, test_stake() / 3).unwrap();
|
||||
assert_eq!(
|
||||
TestStakeAndSlash::repatriate_reserved(&2, beneficiary, test_stake()),
|
||||
TestStakeAndSlash::repatriate_reserved(
|
||||
&2,
|
||||
ExplicitOrAccountParams::Params(beneficiary),
|
||||
test_stake()
|
||||
),
|
||||
Ok(test_stake() - test_stake() / 3)
|
||||
);
|
||||
assert_eq!(Balances::free_balance(2), test_stake() * 2 - test_stake() / 3);
|
||||
@@ -158,7 +169,11 @@ mod tests {
|
||||
Balances::mint_into(&3, test_stake() * 2).unwrap();
|
||||
TestStakeAndSlash::reserve(&3, test_stake()).unwrap();
|
||||
assert_eq!(
|
||||
TestStakeAndSlash::repatriate_reserved(&3, beneficiary, test_stake()),
|
||||
TestStakeAndSlash::repatriate_reserved(
|
||||
&3,
|
||||
ExplicitOrAccountParams::Params(beneficiary),
|
||||
test_stake()
|
||||
),
|
||||
Ok(0)
|
||||
);
|
||||
assert_eq!(Balances::free_balance(3), test_stake());
|
||||
@@ -176,7 +191,12 @@ mod tests {
|
||||
|
||||
Balances::mint_into(&3, test_stake() * 2).unwrap();
|
||||
TestStakeAndSlash::reserve(&3, test_stake()).unwrap();
|
||||
assert!(TestStakeAndSlash::repatriate_reserved(&3, beneficiary, test_stake()).is_err());
|
||||
assert!(TestStakeAndSlash::repatriate_reserved(
|
||||
&3,
|
||||
ExplicitOrAccountParams::Params(beneficiary),
|
||||
test_stake()
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(Balances::free_balance(3), test_stake());
|
||||
assert_eq!(Balances::reserved_balance(3), test_stake());
|
||||
assert_eq!(Balances::free_balance(beneficiary_account), 0);
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub use registration::{Registration, StakeAndSlash};
|
||||
pub use registration::{ExplicitOrAccountParams, Registration, StakeAndSlash};
|
||||
|
||||
use bp_messages::LaneId;
|
||||
use bp_runtime::{ChainId, StorageDoubleMapKeyProvider};
|
||||
|
||||
@@ -46,6 +46,21 @@ use sp_runtime::{
|
||||
DispatchError, DispatchResult,
|
||||
};
|
||||
|
||||
/// Either explicit account reference or `RewardsAccountParams`.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ExplicitOrAccountParams<AccountId> {
|
||||
/// Explicit account reference.
|
||||
Explicit(AccountId),
|
||||
/// Account, referenced using `RewardsAccountParams`.
|
||||
Params(RewardsAccountParams),
|
||||
}
|
||||
|
||||
impl<AccountId> From<RewardsAccountParams> for ExplicitOrAccountParams<AccountId> {
|
||||
fn from(params: RewardsAccountParams) -> Self {
|
||||
ExplicitOrAccountParams::Params(params)
|
||||
}
|
||||
}
|
||||
|
||||
/// Relayer registration.
|
||||
#[derive(Copy, Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
|
||||
pub struct Registration<BlockNumber, Balance> {
|
||||
@@ -90,7 +105,7 @@ pub trait StakeAndSlash<AccountId, BlockNumber, Balance> {
|
||||
/// Returns `Ok(_)` with non-zero balance if we have failed to repatriate some portion of stake.
|
||||
fn repatriate_reserved(
|
||||
relayer: &AccountId,
|
||||
beneficiary: RewardsAccountParams,
|
||||
beneficiary: ExplicitOrAccountParams<AccountId>,
|
||||
amount: Balance,
|
||||
) -> Result<Balance, DispatchError>;
|
||||
}
|
||||
@@ -113,7 +128,7 @@ where
|
||||
|
||||
fn repatriate_reserved(
|
||||
_relayer: &AccountId,
|
||||
_beneficiary: RewardsAccountParams,
|
||||
_beneficiary: ExplicitOrAccountParams<AccountId>,
|
||||
_amount: Balance,
|
||||
) -> Result<Balance, DispatchError> {
|
||||
Ok(Zero::zero())
|
||||
|
||||
Reference in New Issue
Block a user