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

Generic slashing side-effects #5623

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();

}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions cumulus/parachains/runtimes/testing/penpal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type MaxFreezes = ConstU32<1>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down Expand Up @@ -1236,6 +1237,7 @@ impl pallet_balances::Config<NisCounterpartInstance> for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<1>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = VariantCountOf<RuntimeFreezeReason>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
23 changes: 23 additions & 0 deletions prdoc/pr_5623.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Generic slashing side-effects

doc:
- audience: Runtime Dev
description: |
What?
Make it possible for other pallets to implement their own logic when a slash on a balance occurs.

How?
First we abstract the done_slash function of holds::Balanced to it's own trait that any pallet can implement.
Then we add a config type in pallet-balances that accepts a callback tuple of all the pallets that implement this trait.
Finally implement done_slash for pallet-balances such that it calls the config type.
Integration
The default implementation of done_slash is still an empty function, and the new config type of pallet-balances can be set to an empty tuple, so nothing changes by default.

crates:
- name: frame-support
bump: major
- name: pallet-balances
bump: major
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = VariantCountOf<RuntimeFreezeReason>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/assets-freezer/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl pallet_balances::Config for Test {
type MaxFreezes = ();
type RuntimeHoldReason = ();
type RuntimeFreezeReason = ();
type DoneSlashHandler = ();
}

impl pallet_assets::Config for Test {
Expand Down
6 changes: 6 additions & 0 deletions substrate/frame/balances/src/impl_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ impl<T: Config<I>, I: 'static> fungible::Balanced<T::AccountId> for Pallet<T, I>

impl<T: Config<I>, I: 'static> fungible::BalancedHold<T::AccountId> for Pallet<T, I> {}

impl<T: Config<I>, I: 'static> fungible::hold::DoneSlash<T::RuntimeHoldReason, T::AccountId, T::Balance> for Pallet<T, I> {
fn done_slash(reason: &T::RuntimeHoldReason, who: &T::AccountId, amount: T::Balance) {
T::DoneSlashHandler::done_slash(reason, who, amount);
}
}

impl<T: Config<I>, I: 'static> AccountTouch<(), T::AccountId> for Pallet<T, I> {
type Balance = T::Balance;
fn deposit_required(_: ()) -> Self::Balance {
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ pub mod pallet {
type MaxFreezes = VariantCountOf<Self::RuntimeFreezeReason>;

type WeightInfo = ();
type DoneSlashHandler = ();
}
}

Expand Down Expand Up @@ -312,6 +313,9 @@ pub mod pallet {
/// The maximum number of individual freeze locks that can exist on an account at any time.
#[pallet::constant]
type MaxFreezes: Get<u32>;

/// Allows callbacks to other pallets so they can update their bookkeeping when a slash occurs.
type DoneSlashHandler: fungible::hold::DoneSlash<Self::RuntimeHoldReason, Self::AccountId, Self::Balance>;
JuaniRios marked this conversation as resolved.
Show resolved Hide resolved
}

/// The in-code storage version.
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/contracts/mock-network/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type WeightInfo = ();
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/contracts/mock-network/src/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl pallet_balances::Config for Runtime {
type MaxFreezes = ConstU32<0>;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type DoneSlashHandler = ();
}

impl shared::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/nis/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl pallet_balances::Config<Instance1> for Test {
type MaxFreezes = ();
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type DoneSlashHandler = ();
}

impl pallet_balances::Config<Instance2> for Test {
Expand All @@ -84,6 +85,7 @@ impl pallet_balances::Config<Instance2> for Test {
type MaxFreezes = ();
type RuntimeHoldReason = ();
type RuntimeFreezeReason = ();
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/revive/mock-network/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type WeightInfo = ();
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/revive/mock-network/src/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl pallet_balances::Config for Runtime {
type MaxFreezes = ConstU32<0>;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type DoneSlashHandler = ();
}

impl shared::Config for Runtime {
Expand Down
15 changes: 13 additions & 2 deletions substrate/frame/support/src/traits/tokens/fungible/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ pub trait Mutate<AccountId>:
}

/// Trait for slashing a fungible asset which can be place on hold.
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> {
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> + DoneSlash<Self::Reason, AccountId, Self::Balance> {
/// Reduce the balance of some funds on hold in an account.
///
/// The resulting imbalance is the first item of the tuple returned.
Expand All @@ -449,6 +449,17 @@ pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId
Self::done_slash(reason, who, decrease);
(credit, amount.saturating_sub(decrease))
}
}

fn done_slash(_reason: &Self::Reason, _who: &AccountId, _amount: Self::Balance) {}
/// Trait for optional bookkeeping callbacks after a slash.
pub trait DoneSlash<Reason, AccountId, Balance> {
JuaniRios marked this conversation as resolved.
Show resolved Hide resolved
JuaniRios marked this conversation as resolved.
Show resolved Hide resolved
fn done_slash(_reason: &Reason, _who: &AccountId, _amount: Balance) {}
}

#[impl_trait_for_tuples::impl_for_tuples(30)]
impl<Reason, AccountId, Balance: Copy> DoneSlash<Reason, AccountId, Balance> for Tuple {
fn done_slash(reason: &Reason, who: &AccountId, amount: Balance) {
for_tuples!( #( Tuple::done_slash(reason, who, amount); )* );
}
}

12 changes: 12 additions & 0 deletions substrate/frame/support/src/traits/tokens/fungible/item_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//!
//! See the [`crate::traits::fungible`] doc for more information about fungible traits.

use frame_support::traits::fungible::hold::DoneSlash;
use super::*;
use crate::traits::{
fungible::imbalance,
Expand Down Expand Up @@ -467,5 +468,16 @@ impl<
}
}

impl<
F: fungibles::BalancedHold<AccountId>,
A: Get<<F as fungibles::Inspect<AccountId>>::AssetId>,
AccountId,
> DoneSlash<F::Reason, AccountId, F::Balance> for ItemOf<F, A, AccountId>
{
fn done_slash(reason: &F::Reason, who: &AccountId, amount: F::Balance) {
<F as fungibles::hold::DoneSlash<F::AssetId, F::Reason, AccountId, F::Balance>>::done_slash(A::get(), reason, who, amount)
}
}

#[test]
fn test() {}
24 changes: 22 additions & 2 deletions substrate/frame/support/src/traits/tokens/fungible/union_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ impl<
}

impl<
Left: fungible::BalancedHold<AccountId>,
Right: fungibles::BalancedHold<AccountId, Balance = Left::Balance, Reason = Left::Reason>,
Left: fungible::BalancedHold<AccountId> + fungible::hold::DoneSlash<Self::Reason, AccountId, Self::Balance>,
Right: fungibles::BalancedHold<AccountId, Balance = Left::Balance, Reason = Left::Reason> + fungibles::hold::DoneSlash<AssetKind, Left::Reason, AccountId, Left::Balance>,
Criterion: Convert<AssetKind, Either<(), Right::AssetId>>,
AssetKind: AssetId,
AccountId,
Expand All @@ -871,6 +871,26 @@ impl<
}
}
}
impl<
Reason,
Balance,
Left: fungible::hold::DoneSlash<Reason, AccountId, Balance>,
Right: fungibles::hold::DoneSlash<Right::AssetId, Reason, AccountId, Balance> + fungibles::Inspect<AccountId>,
Criterion: Convert<AssetKind, Either<(), Right::AssetId>>,
AssetKind: AssetId,
AccountId,
> fungibles::hold::DoneSlash<AssetKind, Reason, AccountId, Balance> for UnionOf<Left, Right, Criterion, AssetKind, AccountId> {
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
fn done_slash(asset: AssetKind, reason: &Reason, who: &AccountId, amount: Balance) {
match Criterion::convert(asset.clone()) {
Left(()) => {
Left::done_slash(reason, who, amount);
},
Right(a) => {
Right::done_slash(a, reason, who, amount);
},
}
}
}

impl<
Left: fungible::Inspect<AccountId>,
Expand Down
18 changes: 11 additions & 7 deletions substrate/frame/support/src/traits/tokens/fungibles/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
}

/// Trait for slashing a fungible asset which can be place on hold.
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> {
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> + DoneSlash<Self::AssetId, Self::Reason, AccountId, Self::Balance> {
/// Reduce the balance of some funds on hold in an account.
///
/// The resulting imbalance is the first item of the tuple returned.
Expand All @@ -238,13 +238,17 @@ pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId
Self::done_slash(asset, reason, who, decrease);
(credit, amount.saturating_sub(decrease))
}
}

fn done_slash(
_asset: Self::AssetId,
_reason: &Self::Reason,
_who: &AccountId,
_amount: Self::Balance,
) {
/// Trait for optional bookkeeping callbacks after a slash
pub trait DoneSlash<AssetId, Reason, AccountId, Balance> {
fn done_slash(_asset: AssetId, _reason: &Reason, _who: &AccountId, _amount: Balance) {}
}

#[impl_trait_for_tuples::impl_for_tuples(30)]
impl<AssetId: Copy, Reason, AccountId, Balance: Copy> DoneSlash<AssetId, Reason, AccountId, Balance> for Tuple {
fn done_slash(asset_id: AssetId, reason: &Reason, who: &AccountId, amount: Balance) {
for_tuples!( #( Tuple::done_slash(asset_id, reason, who, amount); )* );
}
}

Expand Down
Loading
Loading