Skip to content

Commit

Permalink
nits, benchs passing
Browse files Browse the repository at this point in the history
  • Loading branch information
gpestana committed May 30, 2024
1 parent 153e06f commit 46a80f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 48 deletions.
32 changes: 4 additions & 28 deletions substrate/frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ mod benchmarks {

assert_eq!(
Staking::<T>::status(&voter),
Ok(StakerStatus::Nominator(vec![dangling_target.clone(), other_target.clone()]))
Ok(StakerStatus::Nominator(vec![other_target.clone(), dangling_target.clone()]))
);

#[extrinsic_call]
Expand Down Expand Up @@ -1228,7 +1228,7 @@ mod tests {

#[test]
fn create_validators_with_nominators_for_era_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().try_state(false).build_and_execute(|| {
let v = 10;
let n = 100;

Expand All @@ -1254,7 +1254,7 @@ mod tests {

#[test]
fn create_validator_with_nominators_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().try_state(false).build_and_execute(|| {
let n = 10;

let (validator_stash, nominators) = create_validator_with_nominators::<Test>(
Expand Down Expand Up @@ -1285,7 +1285,7 @@ mod tests {

#[test]
fn add_slashing_spans_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().try_state(false).build_and_execute(|| {
let n = 10;

let (validator_stash, _nominators) = create_validator_with_nominators::<Test>(
Expand Down Expand Up @@ -1315,28 +1315,4 @@ mod tests {
}
});
}

// TODO: SelectedBenchmark not exposed by v2?
/*
#[test]
fn test_payout_all() {
ExtBuilder::default().build_and_execute(|| {
let v = 10;
let n = 100;
let selected_benchmark = SelectedBenchmark::payout_all;
let c = vec![
(frame_benchmarking::BenchmarkParameter::v, v),
(frame_benchmarking::BenchmarkParameter::n, n),
];
assert_ok!(
<SelectedBenchmark as frame_benchmarking::BenchmarkingSetup<Test>>::unit_test_instance(
&selected_benchmark,
&c,
)
);
});
}
*/
}
32 changes: 12 additions & 20 deletions substrate/frame/staking/src/migrations/v13_stake_tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_election_provider_support::{SortedListProvider, VoteWeight};
use frame_support::{
ensure,
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
traits::Defensive,
traits::{Defensive, DefensiveSaturating},
};
use scale_info::TypeInfo;
use sp_staking::StakingInterface;
Expand Down Expand Up @@ -61,9 +61,7 @@ impl Default for Processing {
/// - Ensure the new targets in the list are sorted per total stake (as per the underlying
/// [`SortedListProvider`]).
pub struct MigrationV13<T: Config, W: weights::WeightInfo>(PhantomData<(T, W)>);
impl<T: Config<CurrencyBalance = u128>, W: weights::WeightInfo> SteppedMigration
for MigrationV13<T, W>
{
impl<T: Config, W: weights::WeightInfo> SteppedMigration for MigrationV13<T, W> {
// nominator cursor and validator cursor.
type Cursor = (Option<T::AccountId>, Processing);
type Identifier = MigrationId<18>;
Expand Down Expand Up @@ -142,7 +140,7 @@ impl<T: Config<CurrencyBalance = u128>, W: weights::WeightInfo> SteppedMigration
}
}

impl<T: Config<CurrencyBalance = u128>, W: weights::WeightInfo> MigrationV13<T, W> {
impl<T: Config, W: weights::WeightInfo> MigrationV13<T, W> {
fn process_nominator(
nominator: &T::AccountId,
nominations: Nominations<T>,
Expand Down Expand Up @@ -185,12 +183,8 @@ impl<T: Config<CurrencyBalance = u128>, W: weights::WeightInfo> MigrationV13<T,
let init_stake = match <Pallet<T> as StakingInterface>::status(&who) {
Ok(StakerStatus::Validator) => {
let self_stake = Pallet::<T>::weight_of(&who);
if let Some(total_stake) = self_stake.checked_add(nomination_stake) {
total_stake
} else {
log!(error, "target stake overflow. exit.");
return Err(SteppedMigrationError::Failed)
}
let total_stake = self_stake.defensive_saturating_add(nomination_stake);
total_stake
},
_ => nomination_stake,
};
Expand All @@ -215,15 +209,13 @@ impl<T: Config<CurrencyBalance = u128>, W: weights::WeightInfo> MigrationV13<T,
let current_stake =
<T as Config>::TargetList::get_score(&who).expect("node is in the list");

if let Some(total_stake) = current_stake.checked_add(nomination_stake.into()) {
let _ = <T as Config>::TargetList::on_update(&who, total_stake)
.map_err(|e| log!(error, "updating TL score of {:?}: {:?}", who, e))
.defensive();
Ok(())
} else {
log!(error, "target stake overflow. exit.");
Err(SteppedMigrationError::Failed)
}
let total_stake = current_stake.defensive_saturating_add(nomination_stake.into());
let _ = <T as Config>::TargetList::on_update(&who, total_stake.into()).map_err(|e| {
log!(error, "updating TL score of {:?}: {:?}", who, e);
SteppedMigrationError::Failed
})?;

Ok(())
}

/// Cleans up the nominations of `who`.
Expand Down

0 comments on commit 46a80f1

Please sign in to comment.