Skip to content

Commit

Permalink
".git/.scripts/commands/fmt/fmt.sh"
Browse files Browse the repository at this point in the history
  • Loading branch information
command-bot committed Nov 26, 2023
1 parent b7b6413 commit 9f5efa1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
4 changes: 3 additions & 1 deletion substrate/frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,9 @@ pub(crate) fn stake_tracker_sanity_tests() -> Result<(), &'static str> {

assert_eq!(
Nominators::<Test>::count() + Validators::<Test>::count(),
VoterBagsList::iter().filter(|v| Staking::status(&v) != Ok(StakerStatus::Idle)).count() as u32,
VoterBagsList::iter()
.filter(|v| Staking::status(&v) != Ok(StakerStatus::Idle))
.count() as u32,
);

// recalculate the target's stake based on voter's nominations and compare with the score in the
Expand Down
53 changes: 34 additions & 19 deletions substrate/frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,8 @@ impl<T: Config> Pallet<T> {
let mut min_active_stake = u64::MAX;

// voter list also contains chilled/idle voters, filter those.
let mut sorted_voters = T::VoterList::iter()
.filter(|v| Self::status(&v) != Ok(StakerStatus::Idle));
let mut sorted_voters =
T::VoterList::iter().filter(|v| Self::status(&v) != Ok(StakerStatus::Idle));
while all_voters.len() < final_predicted_len as usize &&
voters_seen < (NPOS_MAX_ITERATIONS_COEFFICIENT * final_predicted_len as u32)
{
Expand Down Expand Up @@ -944,8 +944,8 @@ impl<T: Config> Pallet<T> {
let mut targets_seen = 0;

// target list also contains chilled/idle validators, filter those.
let mut targets_iter = T::TargetList::iter()
.filter(|t| Self::status(&t) != Ok(StakerStatus::Idle));
let mut targets_iter =
T::TargetList::iter().filter(|t| Self::status(&t) != Ok(StakerStatus::Idle));
while all_targets.len() < final_predicted_len as usize &&
targets_seen < (NPOS_MAX_ITERATIONS_COEFFICIENT * final_predicted_len as u32)
{
Expand Down Expand Up @@ -985,7 +985,6 @@ impl<T: Config> Pallet<T> {
/// to `Nominators` or `VoterList` outside of this function is almost certainly
/// wrong.
pub fn do_add_nominator(who: &T::AccountId, nominations: Nominations<T>) {

match (Nominators::<T>::contains_key(who), T::VoterList::contains(who)) {
(false, false) => {
// new nomination
Expand All @@ -1005,12 +1004,14 @@ impl<T: Config> Pallet<T> {
},
(true, false) => {
defensive!("unexpected state.");
}
},
};

debug_assert_eq!(
Nominators::<T>::count() + Validators::<T>::count(),
T::VoterList::iter().filter(|v| Self::status(&v) != Ok(StakerStatus::Idle)).count() as u32,
T::VoterList::iter()
.filter(|v| Self::status(&v) != Ok(StakerStatus::Idle))
.count() as u32,
);
}

Expand Down Expand Up @@ -1063,16 +1064,20 @@ impl<T: Config> Pallet<T> {
true
},
(true, false) => {
defensive!("inconsistent state: staker is in nominators list but not in voter list");
defensive!(
"inconsistent state: staker is in nominators list but not in voter list"
);
false
}
},
// nominator has been already removed.
(false, false) => false,
};

debug_assert_eq!(
Nominators::<T>::count() + Validators::<T>::count(),
T::VoterList::iter().filter(|v| Self::status(&v) != Ok(StakerStatus::Idle)).count() as u32,
T::VoterList::iter()
.filter(|v| Self::status(&v) != Ok(StakerStatus::Idle))
.count() as u32,
);

outcome
Expand All @@ -1097,7 +1102,9 @@ impl<T: Config> Pallet<T> {

debug_assert_eq!(
Nominators::<T>::count() + Validators::<T>::count(),
T::VoterList::iter().filter(|v| Self::status(&v) != Ok(StakerStatus::Idle)).count() as u32,
T::VoterList::iter()
.filter(|v| Self::status(&v) != Ok(StakerStatus::Idle))
.count() as u32,
);
}

Expand Down Expand Up @@ -1135,9 +1142,11 @@ impl<T: Config> Pallet<T> {
who,
);
true
}
},
(true, false) => {
defensive!("inconsistent state: staker is in validators list but not in targets list");
defensive!(
"inconsistent state: staker is in validators list but not in targets list"
);
false
},
// validator has been already removed.
Expand All @@ -1146,7 +1155,9 @@ impl<T: Config> Pallet<T> {

debug_assert_eq!(
Nominators::<T>::count() + Validators::<T>::count(),
T::VoterList::iter().filter(|v| Self::status(&v) != Ok(StakerStatus::Idle)).count() as u32,
T::VoterList::iter()
.filter(|v| Self::status(&v) != Ok(StakerStatus::Idle))
.count() as u32,
);

outcome
Expand Down Expand Up @@ -1897,8 +1908,9 @@ impl<T: Config> StakingInterface for Pallet<T> {
impl<T: Config> Pallet<T> {
pub(crate) fn do_try_state(_: BlockNumberFor<T>) -> Result<(), TryRuntimeError> {
ensure!(
T::VoterList::iter()
.all(|x| <Nominators<T>>::contains_key(&x) || <Validators<T>>::contains_key(&x) || Self::status(&x) == Ok(StakerStatus::Idle)),
T::VoterList::iter().all(|x| <Nominators<T>>::contains_key(&x) ||
<Validators<T>>::contains_key(&x) ||
Self::status(&x) == Ok(StakerStatus::Idle)),
"VoterList contains non-staker"
);

Expand All @@ -1910,12 +1922,15 @@ impl<T: Config> Pallet<T> {

fn check_count() -> Result<(), TryRuntimeError> {
ensure!(
<T as Config>::VoterList::iter().filter(|v| Self::status(&v) != Ok(StakerStatus::Idle)).count() as u32 ==
Nominators::<T>::count() + Validators::<T>::count(),
<T as Config>::VoterList::iter()
.filter(|v| Self::status(&v) != Ok(StakerStatus::Idle))
.count() as u32 == Nominators::<T>::count() + Validators::<T>::count(),
"wrong external count (VoterList.count != Nominators.count + Validators.count)"
);
ensure!(
<T as Config>::TargetList::iter().filter(|t| Self::status(&t) != Ok(StakerStatus::Idle)).count() as u32 == Validators::<T>::count(),
<T as Config>::TargetList::iter()
.filter(|t| Self::status(&t) != Ok(StakerStatus::Idle))
.count() as u32 == Validators::<T>::count(),
"wrong external count (TargetList.count != Validators.count)"
);
ensure!(
Expand Down
9 changes: 7 additions & 2 deletions substrate/frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7568,7 +7568,10 @@ mod stake_tracker {

// the target list has been updated accordingly and an indirect rebag of 21 happened.
// 11, althoug chilled, is still part of the target list.
assert_eq!(voters_and_targets().1, [(11, score_11_after), (21, score_21_after), (31, 500)]);
assert_eq!(
voters_and_targets().1,
[(11, score_11_after), (21, score_21_after), (31, 500)]
);
assert_eq!(
target_bags_events(),
[
Expand All @@ -7584,7 +7587,9 @@ mod stake_tracker {

// fetching targets sorted and filtered by status works.
assert_eq!(
TargetBagsList::iter().filter(|t| Staking::status(&t).unwrap() != StakerStatus::Idle).collect::<Vec<_>>(),
TargetBagsList::iter()
.filter(|t| Staking::status(&t).unwrap() != StakerStatus::Idle)
.collect::<Vec<_>>(),
[21, 31],
);
})
Expand Down
5 changes: 2 additions & 3 deletions substrate/frame/staking/stake-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ impl<T: Config> OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
//
// Note: it is assumed that who's staking state is updated *before* this method is called.
fn on_stake_update(who: &T::AccountId, prev_stake: Option<Stake<BalanceOf<T>>>) {

// closure to calculate the stake imbalance of a staker.
let stake_imbalance_of = |prev_stake: Option<Stake<BalanceOf<T>>>,
voter_weight: ExtendedBalance| {
Expand Down Expand Up @@ -408,7 +407,6 @@ impl<T: Config> OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
//
// Note: it is assumed that who's staking state is updated *before* calling this method.
fn on_validator_add(who: &T::AccountId) {

// target may exist in the list in case of re-enabling a chilled validator;
if !T::TargetList::contains(who) {
let _ = T::TargetList::on_insert(who.clone(), Self::active_vote_of(who))
Expand Down Expand Up @@ -527,7 +525,8 @@ impl<T: Config> OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
let stake_imbalance = StakeImbalance::Negative(Self::to_vote_extended(slashed_total));

match T::Staking::status(stash).defensive_proof("called on_slash on a unbonded stash") {
Ok(StakerStatus::Idle) | Ok(StakerStatus::Validator) => Self::update_score::<T::TargetList>(stash, stake_imbalance),
Ok(StakerStatus::Idle) | Ok(StakerStatus::Validator) =>
Self::update_score::<T::TargetList>(stash, stake_imbalance),
// score of target impacted by nominators will be updated through ledger.update.
Ok(StakerStatus::Nominator(_)) => (),
Err(_) => (), // nothing to see here.
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/staking/stake-tracker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use crate::{mock::*, StakeImbalance};

use frame_election_provider_support::{SortedListProvider, ScoreProvider};
use frame_election_provider_support::{ScoreProvider, SortedListProvider};
use frame_support::assert_ok;
use sp_staking::{OnStakingUpdate, Stake, StakingInterface};

Expand Down

0 comments on commit 9f5efa1

Please sign in to comment.