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

Merged
merged 32 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
871cc1a
modify frame_support trait and pallet-balances impl
JuaniRios Sep 6, 2024
329a590
add new pallet-balances config type to dependencies
JuaniRios Sep 6, 2024
e9c7ea0
add prdoc
JuaniRios Sep 6, 2024
1c81a18
add prdoc
JuaniRios Sep 6, 2024
d7875fb
add prdoc
JuaniRios Sep 6, 2024
c9954f9
Merge remote-tracking branch 'origin/slashing-trait' into slashing-trait
JuaniRios Sep 6, 2024
def735f
prdoc fixes
JuaniRios Sep 6, 2024
bb335c6
prdoc fixes
JuaniRios Sep 6, 2024
a9ddbfd
Merge branch 'master' into slashing-trait
JuaniRios Sep 6, 2024
d9e01f8
prdoc fixes
JuaniRios Sep 6, 2024
18a957b
Merge remote-tracking branch 'origin/slashing-trait' into slashing-trait
JuaniRios Sep 6, 2024
199a285
prdoc simplification, and documentation
JuaniRios Sep 18, 2024
8722bc3
Merge branch 'paritytech:master' into slashing-trait
JuaniRios Sep 18, 2024
ef640fc
Update substrate/frame/support/src/traits/tokens/fungible/hold.rs
kianenigma Sep 18, 2024
b79d2aa
Update substrate/frame/balances/src/lib.rs
JuaniRios Sep 18, 2024
fdc7388
Merge branch 'master' into slashing-trait
JuaniRios Sep 19, 2024
56426f1
Merge branch 'paritytech:master' into slashing-trait
JuaniRios Sep 20, 2024
dbf6cff
".git/.scripts/commands/fmt/fmt.sh"
Sep 20, 2024
9fd830c
Merge branch 'paritytech:master' into slashing-trait
JuaniRios Sep 23, 2024
ad9834a
fix pallet-broker, add lines to prdoc.
JuaniRios Sep 23, 2024
dd82cc0
Merge branch 'paritytech:master' into slashing-trait
JuaniRios Sep 24, 2024
cb87afa
".git/.scripts/commands/fmt/fmt.sh"
Sep 24, 2024
875f365
run `python3 ./scripts/generate-umbrella.py --sdk . --version 0.1.0`
JuaniRios Sep 24, 2024
2c2c594
fixes
JuaniRios Sep 24, 2024
50ffe3b
fixes
JuaniRios Sep 25, 2024
0ba4f51
Merge branch 'master' into slashing-trait
JuaniRios Sep 25, 2024
a1dec12
fixes
JuaniRios Sep 25, 2024
52ef51e
Merge remote-tracking branch 'origin/slashing-trait' into slashing-trait
JuaniRios Sep 25, 2024
2bd1c27
".git/.scripts/commands/fmt/fmt.sh"
Sep 30, 2024
bf81c85
Merge branch 'master' into slashing-trait
JuaniRios Oct 1, 2024
672d743
Merge branch 'master' into slashing-trait
JuaniRios Oct 2, 2024
3ac23ee
Merge branch 'master' into slashing-trait
franciscoaguirre Oct 2, 2024
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 @@ -246,6 +246,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 @@ -246,6 +246,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 @@ -222,6 +222,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 @@ -222,6 +222,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
32 changes: 32 additions & 0 deletions prdoc/pr_5623.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.

Why?
In the introduction of holds @gavofyork said:

Since Holds are designed to be infallibly slashed, this means that any logic using a Freeze must handle the possibility of the frozen amount being reduced, potentially to zero. A permissionless function should be provided in order to allow bookkeeping to be updated in this instance.

At Polimec we needed to find a way to reduce the vesting schedules of our users after a slash was made, and after talking to @kianenigma at the Web3Summit, we realized there was no easy way to implement this with the current traits, so we came up with this solution.
JuaniRios marked this conversation as resolved.
Show resolved Hide resolved

I also have a working implementation of done_slash for pallet_vesting which I can add to this PR or another one.
JuaniRios marked this conversation as resolved.
Show resolved Hide resolved

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
3 changes: 3 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,8 @@ 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>;

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
14 changes: 12 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,16 @@ 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) {}
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
17 changes: 10 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,16 @@ 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,
) {
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