Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge: slash destination may be an explicit account #4106

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bridges/bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -588,7 +588,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(())
Expand Down
7 changes: 4 additions & 3 deletions bridges/modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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() => {
Expand Down
36 changes: 28 additions & 8 deletions bridges/modules/relayers/src/stake_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bridges/primitives/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
19 changes: 17 additions & 2 deletions bridges/primitives/relayers/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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>;
}
Expand All @@ -113,7 +128,7 @@ where

fn repatriate_reserved(
_relayer: &AccountId,
_beneficiary: RewardsAccountParams,
_beneficiary: ExplicitOrAccountParams<AccountId>,
_amount: Balance,
) -> Result<Balance, DispatchError> {
Ok(Zero::zero())
Expand Down
Loading