Skip to content

Commit

Permalink
relayer may be slashed to explicit account
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik committed Mar 13, 2024
1 parent 0298318 commit 1541ba5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
7 changes: 5 additions & 2 deletions bin/runtime-common/src/extensions/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::{Parachain, ParachainIdOf, RangeInclusiveExt, StaticStrProvider};
use codec::{Codec, Decode, Encode};
use frame_support::{
Expand Down Expand Up @@ -569,7 +569,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 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 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 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
13 changes: 11 additions & 2 deletions primitives/relayers/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ 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),
}

/// Relayer registration.
#[derive(Copy, Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub struct Registration<BlockNumber, Balance> {
Expand Down Expand Up @@ -90,7 +99,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 +122,7 @@ where

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

0 comments on commit 1541ba5

Please sign in to comment.