Skip to content

Commit

Permalink
Quick and dirty fixes to get rid of the compilation errors (TODOs added)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdimitrov committed Feb 28, 2025
1 parent 2de795b commit 146d877
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 52 deletions.
2 changes: 1 addition & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ impl pallet_staking::Config for Runtime {
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
type AdminOrigin = EitherOf<EnsureRoot<AccountId>, StakingAdmin>;
type SessionInterface = Self;
type SessionInterface = (); // Should be pallet_staking_rc_client on ah-next
type EraPayout = EraPayout;
type MaxExposurePageSize = MaxExposurePageSize;
type NextNewSession = Session;
Expand Down
16 changes: 13 additions & 3 deletions substrate/frame/staking/ah-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ pub mod pallet {
fn new_session(
_: sp_staking::SessionIndex,
) -> Option<Vec<(<T as frame_system::Config>::AccountId, ())>> {
// If there is a new validator set - return it. Otherwise return `None`.
ValidatorSet::<T>::take()
.map(|validators| validators.into_iter().map(|v| (v, ())).collect())
let maybe_new_validator_set = ValidatorSet::<T>::take()
.map(|validators| validators.into_iter().map(|v| (v, ())).collect());

// A new validator set is an indication for a new era. Clear
if maybe_new_validator_set.is_none() {
// TODO: historical sessions should be pruned. This used to happen after the bonding
// period for the session but it would be nice to avoid XCM messages for prunning
// and trigger it from RC directly.

// <pallet_session::historical::Pallet<T>>::prune_up_to(up_to); // TODO!!!
}

return maybe_new_validator_set
}

fn new_session_genesis(
Expand Down
22 changes: 22 additions & 0 deletions substrate/frame/staking/rc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ pub trait StakingApi {
fn reward_by_ids(validators_points: Vec<(AccountId32, u32)>);
}

// TODO: update the comment
/// Means for interacting with a specialized version of the `session` trait.
///
/// This is needed because `Staking` sets the `ValidatorIdOf` of the `pallet_session::Config`
pub trait SessionInterface<AccountId> {
/// Get the validators from session.
fn validators() -> Vec<AccountId>;
}

impl<AccountId> SessionInterface<AccountId> for () {
fn validators() -> Vec<AccountId> {
Vec::new()
}
}

/// `pallet-staking-ah-client` pallet index on Relay chain. Used to construct remote calls.
///
/// The codec index must correspond to the index of `pallet-staking-ah-client` in the
Expand Down Expand Up @@ -188,6 +203,13 @@ pub mod pallet {
}
}

impl<T: Config> SessionInterface<T::AccountId> for Pallet<T> {
fn validators() -> Vec<T::AccountId> {
// TODO: cache the last applied validator set and return it here
vec![]
}
}

fn mk_relay_chain_call(call: SessionCalls) -> Instruction<()> {
Instruction::Transact {
origin_kind: OriginKind::Superuser,
Expand Down
25 changes: 0 additions & 25 deletions substrate/frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,31 +976,6 @@ impl<Balance, const MAX: u32> NominationsQuota<Balance> for FixedNominationsQuot
}
}

/// Means for interacting with a specialized version of the `session` trait.
///
/// This is needed because `Staking` sets the `ValidatorIdOf` of the `pallet_session::Config`
pub trait SessionInterface<AccountId> {
/// Report an offending validator.
fn report_offence(validator: AccountId, severity: OffenceSeverity);
/// Get the validators from session.
fn validators() -> Vec<AccountId>;
/// Prune historical session tries up to but not including the given index.
fn prune_historical_up_to(up_to: SessionIndex);
}

// TODO: Why we need this?
impl<AccountId> SessionInterface<AccountId> for () {
fn report_offence(_validator: AccountId, _severity: OffenceSeverity) {
()
}
fn validators() -> Vec<AccountId> {
Vec::new()
}
fn prune_historical_up_to(_: SessionIndex) {
()
}
}

/// Handler for determining how much of a balance should be paid out on the current era.
pub trait EraPayout<Balance> {
/// Determine the payout for this era.
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub mod v16 {
/// Migrating `OffendingValidators` from `Vec<(u32, bool)>` to `Vec<u32>`
pub mod v15 {
use super::*;
use pallet_staking_rc_client::SessionInterface;

// The disabling strategy used by staking pallet
type DefaultDisablingStrategy = pallet_session::disabling::UpToLimitDisablingStrategy;
Expand Down
42 changes: 20 additions & 22 deletions substrate/frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
weights::Weight,
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use pallet_staking_rc_client::{Offence, StakingApi};
use pallet_staking_rc_client::{Offence, SessionInterface, StakingApi};
use sp_core::crypto::AccountId32;
use sp_runtime::{
traits::{Bounded, CheckedAdd, Convert, SaturatedConversion, Saturating, StaticLookup, Zero},
Expand All @@ -50,8 +50,8 @@ use crate::{
asset, election_size_tracker::StaticTracker, log, slashing, weights::WeightInfo, ActiveEraInfo,
BalanceOf, BoundedExposuresOf, EraInfo, EraPayout, Exposure, Forcing, IndividualExposure,
LedgerIntegrityState, MaxNominationsOf, MaxWinnersOf, MaxWinnersPerPageOf, Nominations,
NominationsQuota, PositiveImbalanceOf, RewardDestination, SessionInterface, SnapshotStatus,
StakingLedger, ValidatorPrefs, STAKING_ID,
NominationsQuota, PositiveImbalanceOf, RewardDestination, SnapshotStatus, StakingLedger,
ValidatorPrefs, STAKING_ID,
};
use alloc::{boxed::Box, vec, vec::Vec};

Expand Down Expand Up @@ -573,9 +573,10 @@ impl<T: Config> Pallet<T> {
slashing::clear_era_metadata::<T>(pruned_era);
}

if let Some(&(_, first_session)) = bonded.first() {
T::SessionInterface::prune_historical_up_to(first_session);
}
// TODO: should be handled in ah_client (see the comment there)
// if let Some(&(_, first_session)) = bonded.first() {
// T::SessionInterface::prune_historical_up_to(first_session);
// }
}
});
}
Expand Down Expand Up @@ -1713,8 +1714,7 @@ where
}

add_db_reads_writes(1, 0);
let Some(exposure_overview) =
<ErasStakersOverview<T>>::get(&offence_era, &validator)
let Some(exposure_overview) = <ErasStakersOverview<T>>::get(&offence_era, &validator)
else {
// defensive: this implies offence is for a discarded era, and should already be
// filtered out.
Expand All @@ -1733,19 +1733,19 @@ where
offence_era,
});

if offence_era == active_era.index {
// offence is in the current active era. Report it to session to maybe disable the
// validator.
add_db_reads_writes(2, 2);
T::SessionInterface::report_offence(
validator.clone(),
OffenceSeverity(slash_fraction),
);
}
// TODO: handled in ah_client. Verify it works correctly before removing the commented
// out code. if offence_era == active_era.index {
// // offence is in the current active era. Report it to session to maybe disable the
// // validator.
// add_db_reads_writes(2, 2);
// T::SessionInterface::report_offence(
// validator.clone(),
// OffenceSeverity(slash_fraction),
// );
// }
add_db_reads_writes(1, 0);
let prior_slash_fraction =
ValidatorSlashInEra::<T>::get(offence_era, &validator)
.map_or(Zero::zero(), |(f, _)| f);
let prior_slash_fraction = ValidatorSlashInEra::<T>::get(offence_era, &validator)
.map_or(Zero::zero(), |(f, _)| f);

add_db_reads_writes(1, 0);
if let Some(existing) = OffenceQueue::<T>::get(offence_era, &validator) {
Expand Down Expand Up @@ -1873,8 +1873,6 @@ where
// }
// }



impl<T: Config> ScoreProvider<T::AccountId> for Pallet<T> {
type Score = VoteWeight;

Expand Down
3 changes: 2 additions & 1 deletion substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use frame_support::{
BoundedBTreeSet, BoundedVec,
};
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
use pallet_staking_rc_client::SessionInterface;
use rand::seq::SliceRandom;
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
Expand All @@ -60,7 +61,7 @@ use crate::{
asset, slashing, weights::WeightInfo, AccountIdLookupOf, ActiveEraInfo, BalanceOf, EraPayout,
EraRewardPoints, ExposurePage, Forcing, LedgerIntegrityState, MaxNominationsOf,
NegativeImbalanceOf, Nominations, NominationsQuota, PositiveImbalanceOf, RewardDestination,
SessionInterface, StakingLedger, UnappliedSlash, UnlockChunk, ValidatorPrefs,
StakingLedger, UnappliedSlash, UnlockChunk, ValidatorPrefs,
};

// The speculative number of spans are used as an input of the weight annotation of
Expand Down

0 comments on commit 146d877

Please sign in to comment.