Skip to content

Commit

Permalink
fix staking mock runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Ank4n committed Feb 5, 2025
1 parent be7b5c3 commit 61824f9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 81 deletions.
3 changes: 2 additions & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub use pallet_balances::Call as BalancesCall;
pub use pallet_election_provider_multi_phase::{Call as EPMCall, GeometricDepositBase};
use pallet_staking::UseValidatorsMap;
pub use pallet_timestamp::Call as TimestampCall;
use sp_runtime::traits::{Convert, Get};
use sp_runtime::traits::Get;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;

Expand Down Expand Up @@ -528,6 +528,7 @@ impl pallet_session::Config for Runtime {
type ValidatorIdOf = pallet_staking::StashOf<Self>;
type ShouldEndSession = Babe;
type NextSessionRotation = Babe;
// todo(ank4n): Recheck if this is correct. We probably need a modified historical root.
type SessionManager = Staking;
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
type Keys = SessionKeys;
Expand Down
3 changes: 1 addition & 2 deletions substrate/frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ use frame_support::{
ConstU32, Defensive, DefensiveMax, DefensiveSaturating, Get, LockIdentifier,
},
weights::Weight,
BoundedVec, CloneNoBound, DebugNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
BoundedVec, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use scale_info::TypeInfo;
use sp_runtime::{
Expand All @@ -370,7 +370,6 @@ pub use sp_staking::{Exposure, IndividualExposure, StakerStatus};
pub use weights::WeightInfo;

pub use pallet::{pallet::*, UseNominatorsAndValidatorsMap, UseValidatorsMap};
use sp_runtime::traits::ConvertInto;

pub(crate) const STAKING_ID: LockIdentifier = *b"staking ";
pub(crate) const LOG_TARGET: &str = "runtime::staking";
Expand Down
23 changes: 16 additions & 7 deletions substrate/frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ sp_runtime::impl_opaque_keys! {
}
}
impl pallet_session::Config for Test {
type SessionManager = pallet_session::historical::NoteHistoricalRoot<Test, Staking>;
type SessionManager = Staking;
type Keys = SessionKeys;
type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;
type SessionHandler = (OtherSessionHandler,);
Expand All @@ -152,8 +152,8 @@ impl pallet_session::Config for Test {
}

impl pallet_session::historical::Config for Test {
type FullIdentification = crate::Exposure<AccountId, Balance>;
type FullIdentificationOf = crate::ExposureOf<Test>;
type FullIdentification = AccountId;
type FullIdentificationOf = IdentityOf<Test>;
}
impl pallet_authorship::Config for Test {
type FindAuthor = Author11;
Expand Down Expand Up @@ -816,14 +816,14 @@ pub(crate) fn era_exposures(era: u32) -> Vec<(AccountId, Exposure<AccountId, Bal
}

pub(crate) fn on_offence_in_era(
offenders: &[OffenceDetails<AccountId, AccountId>],
offenders: &[OffenceDetails<AccountId, pallet_session::historical::IdentificationTuple<Test>>],
slash_fraction: &[Perbill],
era: EraIndex,
) {
// counter to keep track of how many blocks we need to advance to process all the offences.
let mut process_blocks = 0u32;
for detail in offenders {
process_blocks += EraInfo::<Test>::get_page_count(era, &detail.offender);
process_blocks += EraInfo::<Test>::get_page_count(era, &detail.offender.0);
}

let bonded_eras = crate::BondedEras::<Test>::get();
Expand All @@ -850,16 +850,25 @@ pub(crate) fn on_offence_in_era(
}

pub(crate) fn on_offence_now(
offenders: &[OffenceDetails<AccountId, AccountId>],
offenders: &[OffenceDetails<AccountId, pallet_session::historical::IdentificationTuple<Test>>],
slash_fraction: &[Perbill],
) {
let now = pallet_staking::ActiveEra::<Test>::get().unwrap().index;
on_offence_in_era(offenders, slash_fraction, now);
}
pub(crate) fn offence_from(
offender: AccountId,
reporter: Option<AccountId>,
) -> OffenceDetails<AccountId, pallet_session::historical::IdentificationTuple<Test>> {
OffenceDetails {
offender: (offender, offender),
reporters: reporter.map(|r| vec![(r)]).unwrap_or_default(),
}
}

pub(crate) fn add_slash(who: &AccountId) {
on_offence_now(
&[OffenceDetails { offender: *who, reporters: vec![] }],
&[offence_from(*who, None)],
&[Perbill::from_percent(10)],
);
}
Expand Down
3 changes: 1 addition & 2 deletions substrate/frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use sp_staking::{

use crate::{
asset, election_size_tracker::StaticTracker, log, slashing, weights::WeightInfo, ActiveEraInfo,
BalanceOf, BoundedExposuresOf, EraInfo, EraPayout, Exposure, ExposureOf, Forcing, IdentityOf,
BalanceOf, BoundedExposuresOf, EraInfo, EraPayout, Exposure, Forcing, IdentityOf,
IndividualExposure, LedgerIntegrityState, MaxNominationsOf, MaxWinnersOf, MaxWinnersPerPageOf,
Nominations, NominationsQuota, PositiveImbalanceOf, RewardDestination, SessionInterface,
SnapshotStatus, StakingLedger, ValidatorPrefs, STAKING_ID,
Expand All @@ -61,7 +61,6 @@ use super::pallet::*;
use crate::slashing::OffenceRecord;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
use frame_support::traits::tokens::Preservation::Protect;
#[cfg(any(test, feature = "try-runtime"))]
use sp_runtime::TryRuntimeError;

Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::cancel_deferred_slash(slash_indices.len() as u32))]
pub fn cancel_deferred_slash(
origin: OriginFor<T>,
era: EraIndex,
_era: EraIndex,
slash_indices: Vec<u32>,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;
Expand Down
7 changes: 2 additions & 5 deletions substrate/frame/staking/src/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ use crate::{
use alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
defensive,
dispatch::WithPostDispatchInfo,
ensure,
pallet_prelude::{One, Weight},
traits::{Defensive, DefensiveSaturating, Get, Imbalance, OnUnbalanced},
};
use scale_info::TypeInfo;
Expand All @@ -70,8 +67,8 @@ use sp_runtime::{
BoundedVec, DispatchResult, RuntimeDebug,
};
use sp_staking::{
offence::{OffenceDetails, OffenceSeverity},
EraIndex, Page, SessionIndex, StakingInterface,
offence::OffenceSeverity,
EraIndex, StakingInterface,
};

/// The proportion of the slashing reward to be paid out on the first slashing detection.
Expand Down
Loading

0 comments on commit 61824f9

Please sign in to comment.