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 all 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 @@ -213,6 +213,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 @@ -213,6 +213,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 @@ -342,6 +342,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 @@ -327,6 +327,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 @@ -223,6 +223,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 @@ -225,6 +225,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
1 change: 1 addition & 0 deletions cumulus/test/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,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 @@ -400,6 +400,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 @@ -1234,6 +1235,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 @@ -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
1 change: 1 addition & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = VariantCountOf<RuntimeFreezeReason>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot/xcm/xcm-builder/src/tests/pay/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl pallet_balances::Config for Test {
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}

parameter_types! {
Expand Down
89 changes: 89 additions & 0 deletions prdoc/pr_5623.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# 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

- name: pallet-broker
bump: minor

- name: rococo-runtime
bump: minor

- name: pallet-nis
bump: minor

- name: westend-runtime
bump: minor

- name: pallet-assets-freezer
bump: minor

- name: pallet-contracts-mock-network
bump: minor

- name: pallet-revive-mock-network
bump: minor

- name: asset-hub-rococo-runtime
bump: minor

- name: asset-hub-westend-runtime
bump: minor

- name: bridge-hub-rococo-runtime
bump: minor

- name: bridge-hub-westend-runtime
bump: minor

- name: collectives-westend-runtime
bump: minor

- name: coretime-rococo-runtime
bump: minor

- name: coretime-westend-runtime
bump: minor

- name: people-rococo-runtime
bump: minor

- name: people-westend-runtime
bump: minor

- name: penpal-runtime
bump: minor

- name: contracts-rococo-runtime
bump: minor

- name: rococo-parachain-runtime
bump: minor

- name: staging-xcm-builder
bump: minor

- name: polkadot-sdk
bump: minor


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
8 changes: 8 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,14 @@ 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
9 changes: 9 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,14 @@ 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,
>;
}

/// The in-code storage version.
Expand Down
14 changes: 14 additions & 0 deletions substrate/frame/broker/src/test_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ where
{
}

impl<
Instance: Get<u32>,
AccountId: Encode,
AssetId: tokens::AssetId + Copy,
MinimumBalance: TypedGet,
HoldReason: Encode + Decode + TypeInfo + 'static,
Balance: tokens::Balance,
> fungibles::hold::DoneSlash<AssetId, HoldReason, AccountId, Balance>
for TestFungibles<Instance, AccountId, AssetId, MinimumBalance, HoldReason>
where
MinimumBalance::Type: tokens::Balance,
{
}

impl<
Instance: Get<u32>,
AccountId: Encode,
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
18 changes: 16 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,11 @@ 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 +453,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) {}
/// 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); )* );
}
}
Loading