Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Move Staking Weights to T::WeightInfo #7007

Merged
22 commits merged into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 100 additions & 75 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,14 @@ pub fn create_validator_with_nominators<T: Trait>(
Ok(v_stash)
}

const USER_SEED: u32 = 999666;

benchmarks! {
_{
// User account seed
let u in 0 .. 1000 => ();
}
_{}

bond {
let u in ...;
let stash = create_funded_user::<T>("stash", u, 100);
let controller = create_funded_user::<T>("controller", u, 100);
let stash = create_funded_user::<T>("stash", USER_SEED, 100);
let controller = create_funded_user::<T>("controller", USER_SEED, 100);
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
let reward_destination = RewardDestination::Staked;
let amount = T::Currency::minimum_balance() * 10.into();
Expand All @@ -120,8 +118,7 @@ benchmarks! {
}

bond_extra {
let u in ...;
let (stash, controller) = create_stash_controller::<T>(u, 100)?;
let (stash, controller) = create_stash_controller::<T>(USER_SEED, 100)?;
let max_additional = T::Currency::minimum_balance() * 10.into();
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
let original_bonded: BalanceOf<T> = ledger.active;
Expand All @@ -133,8 +130,7 @@ benchmarks! {
}

unbond {
let u in ...;
let (_, controller) = create_stash_controller::<T>(u, 100)?;
let (_, controller) = create_stash_controller::<T>(USER_SEED, 100)?;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
let amount = T::Currency::minimum_balance() * 10.into();
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
let original_bonded: BalanceOf<T> = ledger.active;
Expand Down Expand Up @@ -180,8 +176,7 @@ benchmarks! {
}

validate {
let u in ...;
let (stash, controller) = create_stash_controller::<T>(u, 100)?;
let (stash, controller) = create_stash_controller::<T>(USER_SEED, 100)?;
let prefs = ValidatorPrefs::default();
}: _(RawOrigin::Signed(controller), prefs)
verify {
Expand All @@ -199,23 +194,20 @@ benchmarks! {
}

chill {
let u in ...;
let (_, controller) = create_stash_controller::<T>(u, 100)?;
let (_, controller) = create_stash_controller::<T>(USER_SEED, 100)?;
}: _(RawOrigin::Signed(controller))

set_payee {
let u in ...;
let (stash, controller) = create_stash_controller::<T>(u, 100)?;
let (stash, controller) = create_stash_controller::<T>(USER_SEED, 100)?;
assert_eq!(Payee::<T>::get(&stash), RewardDestination::Staked);
}: _(RawOrigin::Signed(controller), RewardDestination::Controller)
verify {
assert_eq!(Payee::<T>::get(&stash), RewardDestination::Controller);
}

set_controller {
let u in ...;
let (stash, _) = create_stash_controller::<T>(u, 100)?;
let new_controller = create_funded_user::<T>("new_controller", u, 100);
let (stash, _) = create_stash_controller::<T>(USER_SEED, 100)?;
let new_controller = create_funded_user::<T>("new_controller", USER_SEED, 100);
let new_controller_lookup = T::Lookup::unlookup(new_controller.clone());
}: _(RawOrigin::Signed(stash), new_controller_lookup)
verify {
Expand All @@ -229,13 +221,13 @@ benchmarks! {
assert_eq!(ValidatorCount::get(), c);
}

force_no_eras { let i in 0 .. 1; }: _(RawOrigin::Root)
force_no_eras {}: _(RawOrigin::Root)
verify { assert_eq!(ForceEra::get(), Forcing::ForceNone); }

force_new_era {let i in 0 .. 1; }: _(RawOrigin::Root)
force_new_era {}: _(RawOrigin::Root)
verify { assert_eq!(ForceEra::get(), Forcing::ForceNew); }

force_new_era_always { let i in 0 .. 1; }: _(RawOrigin::Root)
force_new_era_always {}: _(RawOrigin::Root)
verify { assert_eq!(ForceEra::get(), Forcing::ForceAlways); }

// Worst case scenario, the list of invulnerables is very long.
Expand Down Expand Up @@ -280,32 +272,42 @@ benchmarks! {
let validator = create_validator_with_nominators::<T>(n, T::MaxNominatorRewardedPerValidator::get() as u32, true)?;

let current_era = CurrentEra::get().unwrap();
// set the commission for this particular era as well.
<ErasValidatorPrefs<T>>::insert(current_era, validator.clone(), <Staking<T>>::validators(&validator));

let caller = whitelisted_caller();
let balance_before = T::Currency::free_balance(&validator);
}: _(RawOrigin::Signed(caller), validator.clone(), current_era)
verify {
// Validator has been paid!
let balance_after = T::Currency::free_balance(&validator);
assert!(balance_before < balance_after);
assert!(
balance_before < balance_after,
"Balance of stash {:?} should have increased after payout.", validator,
);
}

payout_stakers_alive_controller {
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32;
let validator = create_validator_with_nominators::<T>(n, T::MaxNominatorRewardedPerValidator::get() as u32, false)?;

let current_era = CurrentEra::get().unwrap();
// set the commission for this particular era as well.
<ErasValidatorPrefs<T>>::insert(current_era, validator.clone(), <Staking<T>>::validators(&validator));

let caller = whitelisted_caller();
let balance_before = T::Currency::free_balance(&validator);
}: payout_stakers(RawOrigin::Signed(caller), validator.clone(), current_era)
verify {
// Validator has been paid!
let balance_after = T::Currency::free_balance(&validator);
assert!(balance_before < balance_after);
assert!(
balance_before < balance_after,
"Balance of stash {:?} should have increased after payout.", validator,
);
}

rebond {
let l in 1 .. MAX_UNLOCKING_CHUNKS as u32;
let (_, controller) = create_stash_controller::<T>(u, 100)?;
let (_, controller) = create_stash_controller::<T>(USER_SEED, 100)?;
let mut staking_ledger = Ledger::<T>::get(controller.clone()).unwrap();
let unlock_chunk = UnlockChunk::<BalanceOf<T>> {
value: 1.into(),
Expand Down Expand Up @@ -362,32 +364,6 @@ benchmarks! {
assert!(validators.len() == v as usize);
}

do_slash {
let l in 1 .. MAX_UNLOCKING_CHUNKS as u32;
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
let mut staking_ledger = Ledger::<T>::get(controller.clone()).unwrap();
let unlock_chunk = UnlockChunk::<BalanceOf<T>> {
value: 1.into(),
era: EraIndex::zero(),
};
for _ in 0 .. l {
staking_ledger.unlocking.push(unlock_chunk.clone())
}
Ledger::<T>::insert(controller, staking_ledger);
let slash_amount = T::Currency::minimum_balance() * 10.into();
let balance_before = T::Currency::free_balance(&stash);
}: {
crate::slashing::do_slash::<T>(
&stash,
slash_amount,
&mut BalanceOf::<T>::zero(),
&mut NegativeImbalanceOf::<T>::zero()
);
} verify {
let balance_after = T::Currency::free_balance(&stash);
assert!(balance_before > balance_after);
}

payout_all {
let v in 1 .. 10;
let n in 1 .. 100;
Expand Down Expand Up @@ -426,18 +402,45 @@ benchmarks! {
}
}

// This benchmark create `v` validators intent, `n` nominators intent, each nominator nominate
// MAX_NOMINATIONS in the set of the first `w` validators.
// It builds a solution with `w` winners composed of nominated validators randomly nominated,
// `a` assignment with MAX_NOMINATIONS.
#[extra]
do_slash {
let l in 1 .. MAX_UNLOCKING_CHUNKS as u32;
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
let mut staking_ledger = Ledger::<T>::get(controller.clone()).unwrap();
let unlock_chunk = UnlockChunk::<BalanceOf<T>> {
value: 1.into(),
era: EraIndex::zero(),
};
for _ in 0 .. l {
staking_ledger.unlocking.push(unlock_chunk.clone())
}
Ledger::<T>::insert(controller, staking_ledger);
let slash_amount = T::Currency::minimum_balance() * 10.into();
let balance_before = T::Currency::free_balance(&stash);
}: {
crate::slashing::do_slash::<T>(
&stash,
slash_amount,
&mut BalanceOf::<T>::zero(),
&mut NegativeImbalanceOf::<T>::zero()
);
} verify {
let balance_after = T::Currency::free_balance(&stash);
assert!(balance_before > balance_after);
}

// This benchmark create `v` validators intent, `n` nominators intent, in total creating `e`
// edges.
#[extra]
submit_solution_initial {
// number of validator intent
let v in 1000 .. 2000;
// number of nominator intent
let n in 1000 .. 2000;
// number of assignments. Basically, number of active nominators.
let a in 200 .. 500;
// number of winners, also ValidatorCount
// number of validator intention. This will be equal to `ElectionSize::validators`.
let v in 200 .. 400;
// number of nominator intention. This will be equal to `ElectionSize::nominators`.
let n in 500 .. 1000;
// number of assignments. Basically, number of active nominators. This will be equal to
// `compact.len()`.
let a in 200 .. 400;
// number of winners, also ValidatorCount. This will be equal to `winner.len()`.
let w in 16 .. 100;

ensure!(w as usize >= MAX_NOMINATIONS, "doesn't support lower value");
Expand Down Expand Up @@ -466,6 +469,13 @@ benchmarks! {
size
) = offchain_election::prepare_submission::<T>(assignments, winners, false).unwrap();

assert_eq!(
winners.len(), compact.unique_targets().len(),
"unique targets ({}) and winners ({}) count not same. This solution is not valid.",
compact.unique_targets().len(),
winners.len(),
);

// needed for the solution to be accepted
<EraElectionStatus<T>>::put(ElectionStatus::Open(T::BlockNumber::from(1u32)));

Expand All @@ -492,14 +502,15 @@ benchmarks! {
}

// same as submit_solution_initial but we place a very weak solution on chian first.
#[extra]
submit_solution_better {
// number of validator intent
let v in 1000 .. 2000;
// number of nominator intent
let n in 1000 .. 2000;
// number of validator intention.
let v in 200 .. 400;
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
// number of nominator intention.
let n in 500 .. 1000;
// number of assignments. Basically, number of active nominators.
let a in 200 .. 500;
// number of winners, also ValidatorCount
let a in 200 .. 400;
// number of winners, also ValidatorCount.
let w in 16 .. 100;

ensure!(w as usize >= MAX_NOMINATIONS, "doesn't support lower value");
Expand Down Expand Up @@ -530,6 +541,13 @@ benchmarks! {
size
) = offchain_election::prepare_submission::<T>(assignments, winners, false).unwrap();

assert_eq!(
winners.len(), compact.unique_targets().len(),
"unique targets ({}) and winners ({}) count not same. This solution is not valid.",
compact.unique_targets().len(),
winners.len(),
);

// needed for the solution to be accepted
<EraElectionStatus<T>>::put(ElectionStatus::Open(T::BlockNumber::from(1u32)));

Expand Down Expand Up @@ -576,11 +594,12 @@ benchmarks! {
}

// This will be early rejected based on the score.
#[extra]
submit_solution_weaker {
// number of validator intent
let v in 1000 .. 2000;
// number of nominator intent
let n in 1000 .. 2000;
// number of validator intention.
let v in 200 .. 400;
// number of nominator intention.
let n in 500 .. 1000;

create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;

Expand All @@ -599,6 +618,12 @@ benchmarks! {
// submit a seq-phragmen with all the good stuff on chain.
{
let (winners, compact, score, size) = get_seq_phragmen_solution::<T>(true);
assert_eq!(
winners.len(), compact.unique_targets().len(),
"unique targets ({}) and winners ({}) count not same. This solution is not valid.",
compact.unique_targets().len(),
winners.len(),
);
assert!(
<Staking<T>>::submit_election_solution(
RawOrigin::Signed(caller.clone()).into(),
Expand Down
Loading