Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
remove OnStakerSlash replace with OnStakingEvents (#14527)
Browse files Browse the repository at this point in the history
* remove 'OnStakerSlash', replace with 'OnStakingEvents'

* fix other features in pallets

* small fixes

* fix docs

* fix docs

* fix docs

* Update primitives/staking/src/lib.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

---------

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
  • Loading branch information
kianenigma and gpestana committed Jul 9, 2023
1 parent 70c0547 commit 3fee5c7
Show file tree
Hide file tree
Showing 28 changed files with 262 additions and 178 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use frame_support::{
tokens::{nonfungibles_v2::Inspect, GetSalary, PayFromAccount},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Currency, EitherOfDiverse,
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote, WithdrawReasons,
LockIdentifier, Nothing, OnUnbalanced, WithdrawReasons,
},
weights::{
constants::{
Expand Down Expand Up @@ -575,7 +575,7 @@ impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = Timestamp;
type CurrencyToVote = U128CurrencyToVote;
type CurrencyToVote = sp_staking::currency_to_vote::U128CurrencyToVote;
type RewardRemainder = Treasury;
type RuntimeEvent = RuntimeEvent;
type Slash = Treasury; // send the slashed funds to the treasury.
Expand All @@ -600,7 +600,7 @@ impl pallet_staking::Config for Runtime {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = HistoryDepth;
type OnStakerSlash = NominationPools;
type EventListeners = NominationPools;
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = StakingBenchmarkingConfig;
}
Expand Down Expand Up @@ -1047,7 +1047,7 @@ impl pallet_elections_phragmen::Config for Runtime {
// NOTE: this implies that council's genesis members cannot be set directly and must come from
// this module.
type InitializeMembers = Council;
type CurrencyToVote = U128CurrencyToVote;
type CurrencyToVote = sp_staking::currency_to_vote::U128CurrencyToVote;
type CandidacyBond = CandidacyBond;
type VotingBondBase = VotingBondBase;
type VotingBondFactor = VotingBondFactor;
Expand Down
4 changes: 2 additions & 2 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl onchain::Config for OnChainSeqPhragmen {
impl pallet_staking::Config for Test {
type MaxNominations = ConstU32<16>;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
Expand All @@ -204,7 +204,7 @@ impl pallet_staking::Config for Test {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type OnStakerSlash = ();
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions frame/beefy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl onchain::Config for OnChainSeqPhragmen {
impl pallet_staking::Config for Test {
type MaxNominations = ConstU32<16>;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
Expand All @@ -228,7 +228,7 @@ impl pallet_staking::Config for Test {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type OnStakerSlash = ();
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = Timestamp;
type CurrencyToVote = traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = (); // burn slashes
Expand All @@ -285,7 +285,7 @@ impl pallet_staking::Config for Runtime {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = HistoryDepth;
type OnStakerSlash = ();
type EventListeners = ();
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
}
Expand Down
1 change: 1 addition & 0 deletions frame/elections-phragmen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sp-io = { version = "23.0.0", default-features = false, path = "../../primitives
sp-npos-elections = { version = "4.0.0-dev", default-features = false, path = "../../primitives/npos-elections" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
sp-staking = { default-features = false, path = "../../primitives/staking" }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
Expand Down
9 changes: 5 additions & 4 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
use codec::{Decode, Encode};
use frame_support::{
traits::{
defensive_prelude::*, ChangeMembers, Contains, ContainsLengthBound, Currency,
CurrencyToVote, Get, InitializeMembers, LockIdentifier, LockableCurrency, OnUnbalanced,
ReservableCurrency, SortedMembers, WithdrawReasons,
defensive_prelude::*, ChangeMembers, Contains, ContainsLengthBound, Currency, Get,
InitializeMembers, LockIdentifier, LockableCurrency, OnUnbalanced, ReservableCurrency,
SortedMembers, WithdrawReasons,
},
weights::Weight,
};
Expand All @@ -113,6 +113,7 @@ use sp_runtime::{
traits::{Saturating, StaticLookup, Zero},
DispatchError, Perbill, RuntimeDebug,
};
use sp_staking::currency_to_vote::CurrencyToVote;
use sp_std::{cmp::Ordering, prelude::*};

#[cfg(any(feature = "try-runtime", test))]
Expand Down Expand Up @@ -1424,7 +1425,7 @@ mod tests {
type PalletId = ElectionsPhragmenPalletId;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type ChangeMembers = TestChangeMembers;
type InitializeMembers = ();
type CandidacyBond = CandidacyBond;
Expand Down
4 changes: 2 additions & 2 deletions frame/fast-unstake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
Expand All @@ -157,7 +157,7 @@ impl pallet_staking::Config for Runtime {
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = ();
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl onchain::Config for OnChainSeqPhragmen {
impl pallet_staking::Config for Test {
type MaxNominations = ConstU32<16>;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
Expand All @@ -209,7 +209,7 @@ impl pallet_staking::Config for Test {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type OnStakerSlash = ();
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions frame/nomination-pools/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
Expand All @@ -120,7 +120,7 @@ impl pallet_staking::Config for Runtime {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type OnStakerSlash = Pools;
type EventListeners = Pools;
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
10 changes: 3 additions & 7 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
//!
//! This section assumes that the slash computation is executed by
//! `pallet_staking::StakingLedger::slash`, which passes the information to this pallet via
//! [`sp_staking::OnStakerSlash::on_slash`].
//! [`sp_staking::OnStakingUpdate::on_slash`].
//!
//! Unbonding pools need to be slashed to ensure all nominators whom where in the bonded pool while
//! it was backing a validator that equivocated are punished. Without these measures a member could
Expand All @@ -341,10 +341,6 @@
//! in addition to the unbonding pools. For maintenance simplicity these are not implemented.
//! Related: <https://github.com/paritytech/substrate/issues/10860>
//!
//! **Relevant methods:**
//!
//! * [`Pallet::on_slash`]
//!
//! ### Limitations
//!
//! * PoolMembers cannot vote with their staked funds because they are transferred into the pools
Expand Down Expand Up @@ -375,7 +371,7 @@ use sp_runtime::{
},
FixedPointNumber, Perbill,
};
use sp_staking::{EraIndex, OnStakerSlash, StakingInterface};
use sp_staking::{EraIndex, StakingInterface};
use sp_std::{collections::btree_map::BTreeMap, fmt::Debug, ops::Div, vec::Vec};

#[cfg(any(feature = "try-runtime", feature = "fuzzing", test, debug_assertions))]
Expand Down Expand Up @@ -3265,7 +3261,7 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> OnStakerSlash<T::AccountId, BalanceOf<T>> for Pallet<T> {
impl<T: Config> sp_staking::OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
fn on_slash(
pool_account: &T::AccountId,
// Bonded balance is always read directly from staking, therefore we don't need to update
Expand Down
1 change: 1 addition & 0 deletions frame/nomination-pools/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl StakingMock {
impl sp_staking::StakingInterface for StakingMock {
type Balance = Balance;
type AccountId = AccountId;
type CurrencyToVote = ();

fn minimum_nominator_bond() -> Self::Balance {
StakingMinBond::get()
Expand Down
4 changes: 2 additions & 2 deletions frame/nomination-pools/test-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
Expand All @@ -134,7 +134,7 @@ impl pallet_staking::Config for Runtime {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type OnStakerSlash = Pools;
type EventListeners = Pools;
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl pallet_staking::Config for Test {
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
Expand All @@ -182,7 +182,7 @@ impl pallet_staking::Config for Test {
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type OnStakerSlash = ();
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
15 changes: 2 additions & 13 deletions frame/root-offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,6 @@ impl onchain::Config for OnChainSeqPhragmen {
type TargetsBound = ConstU32<{ u32::MAX }>;
}

pub struct OnStakerSlashMock<T: Config>(core::marker::PhantomData<T>);
impl<T: Config> sp_staking::OnStakerSlash<AccountId, Balance> for OnStakerSlashMock<T> {
fn on_slash(
_pool_account: &AccountId,
slashed_bonded: Balance,
slashed_chunks: &BTreeMap<EraIndex, Balance>,
) {
LedgerSlashPerEra::set((slashed_bonded, slashed_chunks.clone()));
}
}

parameter_types! {
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub static Offset: BlockNumber = 0;
Expand All @@ -176,7 +165,7 @@ impl pallet_staking::Config for Test {
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
type UnixTime = Timestamp;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
Expand All @@ -196,7 +185,7 @@ impl pallet_staking::Config for Test {
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = ConstU32<84>;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type OnStakerSlash = OnStakerSlashMock<Test>;
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions frame/session/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl pallet_staking::Config for Test {
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
Expand All @@ -184,7 +184,7 @@ impl pallet_staking::Config for Test {
type HistoryDepth = ConstU32<84>;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type OnStakerSlash = ();
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use frame_election_provider_support::SortedListProvider;
use frame_support::{
dispatch::UnfilteredDispatchable,
pallet_prelude::*,
traits::{Currency, CurrencyToVote, Get, Imbalance},
traits::{Currency, Get, Imbalance},
};
use sp_runtime::{
traits::{Bounded, One, StaticLookup, TrailingZeroInput, Zero},
Perbill, Percent,
};
use sp_staking::SessionIndex;
use sp_staking::{currency_to_vote::CurrencyToVote, SessionIndex};
use sp_std::prelude::*;

pub use frame_benchmarking::v1::{
Expand Down
7 changes: 3 additions & 4 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ use sp_runtime::{
pub use sp_staking::StakerStatus;
use sp_staking::{
offence::{Offence, OffenceError, ReportOffence},
EraIndex, SessionIndex,
EraIndex, OnStakingUpdate, SessionIndex,
};
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
pub use weights::WeightInfo;
Expand Down Expand Up @@ -549,7 +549,7 @@ impl<T: Config> StakingLedger<T> {
///
/// `slash_era` is the era in which the slash (which is being enacted now) actually happened.
///
/// This calls `Config::OnStakerSlash::on_slash` with information as to how the slash was
/// This calls `Config::OnStakingUpdate::on_slash` with information as to how the slash was
/// applied.
pub fn slash(
&mut self,
Expand All @@ -562,7 +562,6 @@ impl<T: Config> StakingLedger<T> {
}

use sp_runtime::PerThing as _;
use sp_staking::OnStakerSlash as _;
let mut remaining_slash = slash_amount;
let pre_slash_total = self.total;

Expand Down Expand Up @@ -667,7 +666,7 @@ impl<T: Config> StakingLedger<T> {
// clean unlocking chunks that are set to zero.
self.unlocking.retain(|c| !c.value.is_zero());

T::OnStakerSlash::on_slash(&self.stash, self.active, &slashed_unlocking);
T::EventListeners::on_slash(&self.stash, self.active, &slashed_unlocking);
pre_slash_total.saturating_sub(self.total)
}
}
Expand Down

0 comments on commit 3fee5c7

Please sign in to comment.