Skip to content

Commit eec29a6

Browse files
committed
staking: redirect slash to DAP for WAH and staking-async testnet
1 parent bad793f commit eec29a6

File tree

7 files changed

+48
-4
lines changed

7 files changed

+48
-4
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ cumulus-primitives-aura = { workspace = true }
120120
cumulus-primitives-core = { workspace = true }
121121
cumulus-primitives-utility = { workspace = true }
122122
pallet-collator-selection = { workspace = true }
123+
pallet-dap = { workspace = true }
123124
pallet-message-queue = { workspace = true }
124125
parachain-info = { workspace = true }
125126
parachains-common = { workspace = true }
@@ -173,6 +174,7 @@ runtime-benchmarks = [
173174
"pallet-balances/runtime-benchmarks",
174175
"pallet-collator-selection/runtime-benchmarks",
175176
"pallet-conviction-voting/runtime-benchmarks",
177+
"pallet-dap/runtime-benchmarks",
176178
"pallet-delegated-staking/runtime-benchmarks",
177179
"pallet-election-provider-multi-block/runtime-benchmarks",
178180
"pallet-fast-unstake/runtime-benchmarks",
@@ -246,6 +248,7 @@ try-runtime = [
246248
"pallet-balances/try-runtime",
247249
"pallet-collator-selection/try-runtime",
248250
"pallet-conviction-voting/try-runtime",
251+
"pallet-dap/try-runtime",
249252
"pallet-delegated-staking/try-runtime",
250253
"pallet-election-provider-multi-block/try-runtime",
251254
"pallet-fast-unstake/try-runtime",
@@ -326,6 +329,7 @@ std = [
326329
"pallet-balances/std",
327330
"pallet-collator-selection/std",
328331
"pallet-conviction-voting/std",
332+
"pallet-dap/std",
329333
"pallet-delegated-staking/std",
330334
"pallet-election-provider-multi-block/std",
331335
"pallet-fast-unstake/std",

cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,9 @@ construct_runtime!(
13971397
Treasury: pallet_treasury = 94,
13981398
AssetRate: pallet_asset_rate = 95,
13991399

1400+
// Dynamic Allocation Pool / Issuance Buffer
1401+
Dap: pallet_dap = 100,
1402+
14001403
// TODO: the pallet instance should be removed once all pools have migrated
14011404
// to the new account IDs.
14021405
AssetConversionMigration: pallet_asset_conversion_ops = 200,

cumulus/parachains/runtimes/assets/asset-hub-westend/src/staking.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl pallet_staking_async::Config for Runtime {
277277
type RuntimeHoldReason = RuntimeHoldReason;
278278
type CurrencyToVote = sp_staking::currency_to_vote::SaturatingCurrencyToVote;
279279
type RewardRemainder = ();
280-
type Slash = ();
280+
type Slash = pallet_dap::SlashToDap<Runtime>;
281281
type Reward = ();
282282
type SessionsPerEra = SessionsPerEra;
283283
type BondingDuration = BondingDuration;
@@ -309,6 +309,10 @@ impl pallet_staking_async_rc_client::Config for Runtime {
309309
type MaxValidatorSetRetries = ConstU32<64>;
310310
}
311311

312+
impl pallet_dap::Config for Runtime {
313+
type Currency = Balances;
314+
}
315+
312316
#[derive(Encode, Decode)]
313317
// Call indices taken from westend-next runtime.
314318
pub enum RelayChainRuntimePallets {

substrate/frame/dap/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ extern crate alloc;
3232
use frame_support::{
3333
pallet_prelude::*,
3434
traits::{
35-
fungible::{Inspect, Mutate},
35+
fungible::{Balanced, Credit, Inspect, Mutate},
3636
tokens::{FundingSink, FundingSource, Preservation},
37+
Imbalance, OnUnbalanced,
3738
},
3839
PalletId,
3940
};
@@ -60,7 +61,9 @@ pub mod pallet {
6061
#[pallet::config]
6162
pub trait Config: frame_system::Config {
6263
/// The currency type.
63-
type Currency: Inspect<Self::AccountId> + Mutate<Self::AccountId>;
64+
type Currency: Inspect<Self::AccountId>
65+
+ Mutate<Self::AccountId>
66+
+ Balanced<Self::AccountId>;
6467
}
6568

6669
impl<T: Config> Pallet<T> {
@@ -118,6 +121,28 @@ impl<T: Config> FundingSink<T::AccountId, BalanceOf<T>> for ReturnToDap<T> {
118121
}
119122
}
120123

124+
/// Type alias for credit (negative imbalance - funds that were slashed/removed).
125+
pub type CreditOf<T> = Credit<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;
126+
127+
/// Implementation of OnUnbalanced that deposits slashed funds into the DAP buffer.
128+
/// Use this as `type Slash = SlashToDap<Runtime>` in staking config.
129+
pub struct SlashToDap<T>(core::marker::PhantomData<T>);
130+
131+
impl<T: Config> OnUnbalanced<CreditOf<T>> for SlashToDap<T> {
132+
fn on_nonzero_unbalanced(amount: CreditOf<T>) {
133+
let buffer = Pallet::<T>::buffer_account();
134+
let numeric_amount = amount.peek();
135+
136+
// Resolve the imbalance by depositing into the buffer account
137+
let _ = T::Currency::resolve(&buffer, amount);
138+
139+
log::debug!(
140+
target: LOG_TARGET,
141+
"Deposited slash of {numeric_amount:?} to DAP buffer"
142+
);
143+
}
144+
}
145+
121146
#[cfg(test)]
122147
mod tests {
123148
use super::*;

substrate/frame/staking-async/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ frame-benchmarking = { workspace = true, default-features = true }
4242
frame-support = { features = ["experimental"], workspace = true, default-features = true }
4343
pallet-bags-list = { workspace = true, default-features = true }
4444
pallet-balances = { workspace = true, default-features = true }
45+
pallet-dap = { workspace = true, default-features = true }
4546
rand_chacha = { workspace = true, default-features = true }
4647
sp-tracing = { workspace = true, default-features = true }
4748
substrate-test-utils = { workspace = true }

substrate/frame/staking-async/src/mock.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ frame_support::construct_runtime!(
5252
Balances: pallet_balances,
5353
Staking: pallet_staking_async,
5454
VoterBagsList: pallet_bags_list::<Instance1>,
55+
Dap: pallet_dap,
5556
}
5657
);
5758

@@ -110,6 +111,10 @@ impl pallet_balances::Config for Test {
110111
type AccountStore = System;
111112
}
112113

114+
impl pallet_dap::Config for Test {
115+
type Currency = Balances;
116+
}
117+
113118
parameter_types! {
114119
pub static RewardRemainderUnbalanced: u128 = 0;
115120
}
@@ -445,7 +450,7 @@ impl crate::pallet::pallet::Config for Test {
445450
type RcClientInterface = session_mock::Session;
446451
type CurrencyBalance = Balance;
447452
type CurrencyToVote = SaturatingCurrencyToVote;
448-
type Slash = ();
453+
type Slash = pallet_dap::SlashToDap<Test>;
449454
type WeightInfo = ();
450455
}
451456

0 commit comments

Comments
 (0)