Skip to content

Commit e043ded

Browse files
committed
full identification backward compatible
1 parent 0be27ce commit e043ded

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

polkadot/runtime/westend/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ impl pallet_session::Config for Runtime {
537537
}
538538

539539
impl pallet_session::historical::Config for Runtime {
540-
type FullIdentification = pallet_staking::Exposure<AccountId, Balance>;
541-
type FullIdentificationOf = pallet_staking::ExposureOf<Runtime>;
540+
type FullIdentification = pallet_staking::ExistenceOrLegacyExposure<AccountId, Balance>;
541+
type FullIdentificationOf = pallet_staking::ExistenceOrLegacyExposureOf<Runtime>;
542542
}
543543

544544
pub struct MaybeSignedPhase;

substrate/frame/staking/ah-client/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub mod pallet {
7171
use core::result;
7272
use frame_system::pallet_prelude::*;
7373
use pallet_session::historical;
74-
use pallet_staking::ExposureOf;
74+
use pallet_staking::{ExistenceOrLegacyExposure, ExistenceOrLegacyExposureOf};
7575
use polkadot_primitives::Id as ParaId;
7676
use polkadot_runtime_parachains::origin::{ensure_parachain, Origin};
7777
use sp_runtime::Perbill;
@@ -238,8 +238,11 @@ pub mod pallet {
238238
where
239239
T: pallet_session::Config<ValidatorId = <T as frame_system::Config>::AccountId>,
240240
T: pallet_session::historical::Config<
241-
FullIdentification = Exposure<<T as frame_system::Config>::AccountId, BalanceOf<T>>,
242-
FullIdentificationOf = ExposureOf<T>,
241+
FullIdentification = ExistenceOrLegacyExposure<
242+
<T as frame_system::Config>::AccountId,
243+
BalanceOf<T>,
244+
>,
245+
FullIdentificationOf = ExistenceOrLegacyExposureOf<T>,
243246
>,
244247
T::SessionHandler: pallet_session::SessionHandler<<T as frame_system::Config>::AccountId>,
245248
T::SessionManager: pallet_session::SessionManager<<T as frame_system::Config>::AccountId>,

substrate/frame/staking/src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,10 @@ impl<T: Config> Convert<T::AccountId, Option<T::AccountId>> for StashOf<T> {
11181118
///
11191119
/// Active exposure is the exposure of the validator set currently validating, i.e. in
11201120
/// `active_era`. It can differ from the latest planned exposure in `current_era`.
1121+
#[deprecated(note = "Use `ExistenceOf` or `ExistenceOrLegacyExposureOf` instead")]
11211122
pub struct ExposureOf<T>(core::marker::PhantomData<T>);
11221123

1124+
#[allow(deprecated)]
11231125
impl<T: Config> Convert<T::AccountId, Option<Exposure<T::AccountId, BalanceOf<T>>>>
11241126
for ExposureOf<T>
11251127
{
@@ -1146,17 +1148,18 @@ impl<T: Config> Convert<T::AccountId, Option<Existence>> for ExistenceOf<T> {
11461148
}
11471149

11481150
/// A compatibility wrapper type used to represent the presence of a validator in the current era.
1149-
/// Encodes as a unit type but can decode from legacy `Exposure` values for backward compatibility.
1151+
/// Encodes as type [`Existence`] but can decode from legacy [`Exposure`] values for backward
1152+
/// compatibility.
1153+
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, RuntimeDebug, TypeInfo, DecodeWithMemTracking)]
11501154
pub enum ExistenceOrLegacyExposure<A, B: HasCompact> {
11511155
/// Validator exists in the current era.
11521156
Exists,
11531157
/// Legacy `Exposure` data, retained for decoding compatibility.
11541158
Exposure(Exposure<A, B>),
11551159
}
11561160

1157-
/// A converter type that returns `Some(ExistenceOrLegacyExposure::Exists)` if the validator exists
1158-
/// in the current active era, otherwise `None`. This allows decoding from older exposures while
1159-
/// encoding remains minimal.
1161+
/// Converts a validator account ID to a Some([`ExistenceOrLegacyExposure::Exists`]) if the
1162+
/// validator exists in the current era, otherwise `None`.
11601163
pub struct ExistenceOrLegacyExposureOf<T>(core::marker::PhantomData<T>);
11611164

11621165
impl<T: Config> Convert<T::AccountId, Option<ExistenceOrLegacyExposure<T::AccountId, BalanceOf<T>>>>

substrate/frame/staking/src/pallet/impls.rs

+38-27
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ use sp_staking::{
4747

4848
use crate::{
4949
asset, election_size_tracker::StaticTracker, log, slashing, weights::WeightInfo, ActiveEraInfo,
50-
BalanceOf, BoundedExposuresOf, EraInfo, EraPayout, Exposure, Forcing, IndividualExposure,
51-
LedgerIntegrityState, MaxNominationsOf, MaxWinnersOf, MaxWinnersPerPageOf, Nominations,
52-
NominationsQuota, PositiveImbalanceOf, RewardDestination, SessionInterface, SnapshotStatus,
53-
StakingLedger, ValidatorPrefs, STAKING_ID,
50+
BalanceOf, BoundedExposuresOf, EraInfo, EraPayout, ExistenceOrLegacyExposure, Exposure,
51+
Forcing, IndividualExposure, LedgerIntegrityState, MaxNominationsOf, MaxWinnersOf,
52+
MaxWinnersPerPageOf, Nominations, NominationsQuota, PositiveImbalanceOf, RewardDestination,
53+
SessionInterface, SnapshotStatus, StakingLedger, ValidatorPrefs, STAKING_ID,
5454
};
5555
use alloc::{boxed::Box, vec, vec::Vec};
5656

@@ -306,7 +306,7 @@ impl<T: Config> Pallet<T> {
306306

307307
if EraInfo::<T>::is_rewards_claimed(era, &stash, page) {
308308
return Err(Error::<T>::AlreadyClaimed
309-
.with_weight(T::WeightInfo::payout_stakers_alive_staked(0)))
309+
.with_weight(T::WeightInfo::payout_stakers_alive_staked(0)));
310310
}
311311

312312
EraInfo::<T>::set_rewards_as_claimed(era, &stash, page);
@@ -332,7 +332,7 @@ impl<T: Config> Pallet<T> {
332332

333333
// Nothing to do if they have no reward points.
334334
if validator_reward_points.is_zero() {
335-
return Ok(Some(T::WeightInfo::payout_stakers_alive_staked(0)).into())
335+
return Ok(Some(T::WeightInfo::payout_stakers_alive_staked(0)).into());
336336
}
337337

338338
// This is the fraction of the total reward that the validator and the
@@ -421,7 +421,7 @@ impl<T: Config> Pallet<T> {
421421
) -> Option<(PositiveImbalanceOf<T>, RewardDestination<T::AccountId>)> {
422422
// noop if amount is zero
423423
if amount.is_zero() {
424-
return None
424+
return None;
425425
}
426426
let dest = Self::payee(StakingAccount::Stash(stash.clone()))?;
427427

@@ -484,7 +484,7 @@ impl<T: Config> Pallet<T> {
484484
_ => {
485485
// Either `Forcing::ForceNone`,
486486
// or `Forcing::NotForcing if era_length >= T::SessionsPerEra::get()`.
487-
return None
487+
return None;
488488
},
489489
}
490490

@@ -1098,7 +1098,7 @@ impl<T: Config> Pallet<T> {
10981098
// if voter weight is zero, do not consider this voter for the snapshot.
10991099
if voter_weight.is_zero() {
11001100
log!(debug, "voter's active balance is 0. skip this voter.");
1101-
continue
1101+
continue;
11021102
}
11031103

11041104
if let Some(Nominations { targets, .. }) = <Nominators<T>>::get(&voter) {
@@ -1113,7 +1113,7 @@ impl<T: Config> Pallet<T> {
11131113
Self::deposit_event(Event::<T>::SnapshotVotersSizeExceeded {
11141114
size: voters_size_tracker.size as u32,
11151115
});
1116-
break
1116+
break;
11171117
}
11181118

11191119
all_voters.push(voter);
@@ -1139,7 +1139,7 @@ impl<T: Config> Pallet<T> {
11391139
Self::deposit_event(Event::<T>::SnapshotVotersSizeExceeded {
11401140
size: voters_size_tracker.size as u32,
11411141
});
1142-
break
1142+
break;
11431143
}
11441144
all_voters.push(self_vote);
11451145
validators_taken.saturating_inc();
@@ -1205,7 +1205,7 @@ impl<T: Config> Pallet<T> {
12051205
Self::deposit_event(Event::<T>::SnapshotTargetsSizeExceeded {
12061206
size: targets_size_tracker.size as u32,
12071207
});
1208-
break
1208+
break;
12091209
}
12101210

12111211
if Validators::<T>::contains_key(&target) {
@@ -1536,7 +1536,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
15361536
// We can't handle this case yet -- return an error. WIP to improve handling this case in
15371537
// <https://github.com/paritytech/substrate/pull/13195>.
15381538
if bounds.exhausted(None, CountBound(targets.len() as u32).into()) {
1539-
return Err("Target snapshot too big")
1539+
return Err("Target snapshot too big");
15401540
}
15411541

15421542
debug_assert!(!bounds.slice_exhausted(&targets));
@@ -1546,7 +1546,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
15461546

15471547
fn next_election_prediction(now: BlockNumberFor<T>) -> BlockNumberFor<T> {
15481548
if let Some(override_value) = BenchmarkNextElection::get() {
1549-
return override_value.into()
1549+
return override_value.into();
15501550
}
15511551

15521552
let current_era = CurrentEra::<T>::get().unwrap_or(0);
@@ -1587,7 +1587,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
15871587
log!(debug, "we are mid-election, pointing to next era as election prediction.");
15881588
return now.saturating_add(
15891589
BlockNumberFor::<T>::from(T::SessionsPerEra::get()) * session_length,
1590-
)
1590+
);
15911591
}
15921592

15931593
now.saturating_add(
@@ -1775,14 +1775,25 @@ impl<T: Config> historical::SessionManager<T::AccountId, Exposure<T::AccountId,
17751775
}
17761776
}
17771777

1778-
impl<T: Config> historical::SessionManager<T::AccountId, ()> for Pallet<T> {
1779-
fn new_session(new_index: SessionIndex) -> Option<Vec<(T::AccountId, ())>> {
1780-
<Self as pallet_session::SessionManager<_>>::new_session(new_index)
1781-
.map(|validators| validators.into_iter().map(|v| (v, ())).collect())
1778+
impl<T: Config>
1779+
historical::SessionManager<T::AccountId, ExistenceOrLegacyExposure<T::AccountId, BalanceOf<T>>>
1780+
for Pallet<T>
1781+
{
1782+
fn new_session(
1783+
new_index: SessionIndex,
1784+
) -> Option<Vec<(T::AccountId, ExistenceOrLegacyExposure<T::AccountId, BalanceOf<T>>)>> {
1785+
<Self as pallet_session::SessionManager<_>>::new_session(new_index).map(|validators| {
1786+
validators.into_iter().map(|v| (v, ExistenceOrLegacyExposure::Exists)).collect()
1787+
})
17821788
}
1783-
fn new_session_genesis(new_index: SessionIndex) -> Option<Vec<(T::AccountId, ())>> {
1784-
<Self as pallet_session::SessionManager<_>>::new_session_genesis(new_index)
1785-
.map(|validators| validators.into_iter().map(|v| (v, ())).collect())
1789+
fn new_session_genesis(
1790+
new_index: SessionIndex,
1791+
) -> Option<Vec<(T::AccountId, ExistenceOrLegacyExposure<T::AccountId, BalanceOf<T>>)>> {
1792+
<Self as pallet_session::SessionManager<_>>::new_session_genesis(new_index).map(
1793+
|validators| {
1794+
validators.into_iter().map(|v| (v, ExistenceOrLegacyExposure::Exists)).collect()
1795+
},
1796+
)
17861797
}
17871798
fn start_session(start_index: SessionIndex) {
17881799
<Self as pallet_session::SessionManager<_>>::start_session(start_index)
@@ -1861,7 +1872,7 @@ impl<T: Config> Pallet<T> {
18611872
add_db_reads_writes(1, 0);
18621873
let Some(active_era) = ActiveEra::<T>::get() else {
18631874
log!(warn, "🦹 on_offence: no active era; ignoring offence");
1864-
return consumed_weight
1875+
return consumed_weight;
18651876
};
18661877

18671878
add_db_reads_writes(1, 0);
@@ -1886,7 +1897,7 @@ impl<T: Config> Pallet<T> {
18861897
// defensive: this implies offence is for a discarded era, and should already be
18871898
// filtered out.
18881899
log!(warn, "🦹 on_offence: no era found for slash_session; ignoring offence");
1889-
return Weight::default()
1900+
return Weight::default();
18901901
},
18911902
}
18921903
};
@@ -1899,7 +1910,7 @@ impl<T: Config> Pallet<T> {
18991910
// Skip if the validator is invulnerable.
19001911
if invulnerables.contains(&validator) {
19011912
log!(debug, "🦹 on_offence: {:?} is invulnerable; ignoring offence", validator);
1902-
continue
1913+
continue;
19031914
}
19041915

19051916
add_db_reads_writes(1, 0);
@@ -2328,7 +2339,7 @@ impl<T: Config> StakingInterface for Pallet<T> {
23282339
who: &Self::AccountId,
23292340
) -> Result<sp_staking::StakerStatus<Self::AccountId>, DispatchError> {
23302341
if !StakingLedger::<T>::is_bonded(StakingAccount::Stash(who.clone())) {
2331-
return Err(Error::<T>::NotStash.into())
2342+
return Err(Error::<T>::NotStash.into());
23322343
}
23332344

23342345
let is_validator = Validators::<T>::contains_key(&who);
@@ -2405,7 +2416,7 @@ impl<T: Config> sp_staking::StakingUnchecked for Pallet<T> {
24052416
payee: &Self::AccountId,
24062417
) -> DispatchResult {
24072418
if StakingLedger::<T>::is_bonded(StakingAccount::Stash(keyless_who.clone())) {
2408-
return Err(Error::<T>::AlreadyBonded.into())
2419+
return Err(Error::<T>::AlreadyBonded.into());
24092420
}
24102421

24112422
// check if payee not same as who.

0 commit comments

Comments
 (0)