Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staking Migration Tests #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
54 changes: 35 additions & 19 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use super::*;
use crate::Module as Staking;
use testing_utils::*;

use sp_runtime::traits::One;
pub use frame_benchmarking::{account, benchmarks};
use frame_system::RawOrigin;
pub use frame_benchmarking::{benchmarks, account};
use sp_runtime::traits::One;
const SEED: u32 = 0;
const MAX_SPANS: u32 = 100;
const MAX_VALIDATORS: u32 = 1000;
Expand All @@ -32,13 +32,15 @@ const MAX_SLASHES: u32 = 1000;
// Add slashing spans to a user account. Not relevant for actual use, only to benchmark
// read and write operations.
fn add_slashing_spans<T: Trait>(who: &T::AccountId, spans: u32) {
if spans == 0 { return }
if spans == 0 {
return;
}

// For the first slashing span, we initialize
let mut slashing_spans = crate::slashing::SlashingSpans::new(0);
SpanSlash::<T>::insert((who, 0), crate::slashing::SpanRecord::default());

for i in 1 .. spans {
for i in 1..spans {
assert!(slashing_spans.end_span(i));
SpanSlash::<T>::insert((who, i), crate::slashing::SpanRecord::default());
}
Expand All @@ -47,7 +49,10 @@ fn add_slashing_spans<T: Trait>(who: &T::AccountId, spans: u32) {

// This function generates one validator being nominated by n nominators, and returns the validator
// stash account. It also starts an era and creates pending payouts.
pub fn create_validator_with_nominators<T: Trait>(n: u32, upper_bound: u32) -> Result<T::AccountId, &'static str> {
pub fn create_validator_with_nominators<T: Trait>(
n: u32,
upper_bound: u32,
) -> Result<T::AccountId, &'static str> {
let mut points_total = 0;
let mut points_individual = Vec::new();

Expand All @@ -64,10 +69,13 @@ pub fn create_validator_with_nominators<T: Trait>(n: u32, upper_bound: u32) -> R
points_individual.push((v_stash.clone(), 10));

// Give the validator n nominators, but keep total users in the system the same.
for i in 0 .. upper_bound {
for i in 0..upper_bound {
let (_n_stash, n_controller) = create_stash_controller::<T>(u32::max_value() - i, 100)?;
if i < n {
Staking::<T>::nominate(RawOrigin::Signed(n_controller.clone()).into(), vec![stash_lookup.clone()])?;
Staking::<T>::nominate(
RawOrigin::Signed(n_controller.clone()).into(),
vec![stash_lookup.clone()],
)?;
}
}

Expand Down Expand Up @@ -602,7 +610,7 @@ benchmarks! {
#[cfg(test)]
mod tests {
use super::*;
use crate::mock::{ExtBuilder, Test, Balances, Staking, Origin};
use crate::mock::{Balances, ExtBuilder, Origin, Staking, Test};
use frame_support::assert_ok;

#[test]
Expand All @@ -611,8 +619,7 @@ mod tests {
let v = 10;
let n = 100;

create_validators_with_nominators_for_era::<Test>(v, n, MAX_NOMINATIONS, false, None)
.unwrap();
create_validators_with_nominators_for_era::<Test>(v, n, MAX_NOMINATIONS, false, None).unwrap();

let count_validators = Validators::<Test>::iter().count();
let count_nominators = Nominators::<Test>::iter().count();
Expand All @@ -630,12 +637,17 @@ mod tests {
let validator_stash = create_validator_with_nominators::<Test>(
n,
<Test as Trait>::MaxNominatorRewardedPerValidator::get() as u32,
).unwrap();
)
.unwrap();

let current_era = CurrentEra::get().unwrap();

let original_free_balance = Balances::free_balance(&validator_stash);
assert_ok!(Staking::payout_stakers(Origin::signed(1337), validator_stash, current_era));
assert_ok!(Staking::payout_stakers(
Origin::signed(1337),
validator_stash,
current_era
));
let new_free_balance = Balances::free_balance(&validator_stash);

assert!(original_free_balance < new_free_balance);
Expand All @@ -650,22 +662,23 @@ mod tests {
let validator_stash = create_validator_with_nominators::<Test>(
n,
<Test as Trait>::MaxNominatorRewardedPerValidator::get() as u32,
).unwrap();
)
.unwrap();

// Add 20 slashing spans
let num_of_slashing_spans = 20;
add_slashing_spans::<Test>(&validator_stash, num_of_slashing_spans);

let slashing_spans = SlashingSpans::<Test>::get(&validator_stash).unwrap();
assert_eq!(slashing_spans.iter().count(), num_of_slashing_spans as usize);
for i in 0 .. num_of_slashing_spans {
for i in 0..num_of_slashing_spans {
assert!(SpanSlash::<Test>::contains_key((&validator_stash, i)));
}

// Test everything is cleaned up
assert_ok!(Staking::kill_stash(&validator_stash, num_of_slashing_spans));
assert!(SlashingSpans::<Test>::get(&validator_stash).is_none());
for i in 0 .. num_of_slashing_spans {
for i in 0..num_of_slashing_spans {
assert!(!SpanSlash::<Test>::contains_key((&validator_stash, i)));
}
});
Expand All @@ -678,12 +691,16 @@ mod tests {
let n = 100;

let selected_benchmark = SelectedBenchmark::payout_all;
let c = vec![(frame_benchmarking::BenchmarkParameter::v, v), (frame_benchmarking::BenchmarkParameter::n, n)];
let c = vec![
(frame_benchmarking::BenchmarkParameter::v, v),
(frame_benchmarking::BenchmarkParameter::n, n),
];
let closure_to_benchmark =
<SelectedBenchmark as frame_benchmarking::BenchmarkingSetup<Test>>::instance(
&selected_benchmark,
&c
).unwrap();
&c,
)
.unwrap();

assert_ok!(closure_to_benchmark());
});
Expand Down Expand Up @@ -729,5 +746,4 @@ mod tests {
assert_ok!(test_benchmark_submit_solution_weaker::<Test>());
});
}

}
83 changes: 62 additions & 21 deletions frame/staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! The staking rate in NPoS is the total amount of tokens staked by nominators and validators,
//! divided by the total token supply.

use sp_runtime::{Perbill, traits::AtLeast32Bit, curve::PiecewiseLinear};
use sp_runtime::{curve::PiecewiseLinear, traits::AtLeast32Bit, Perbill};

/// The total payout to all validators (and their nominators) per era and maximum payout.
///
Expand All @@ -33,16 +33,17 @@ pub fn compute_total_payout<N>(
yearly_inflation: &PiecewiseLinear<'static>,
npos_token_staked: N,
total_tokens: N,
era_duration: u64
) -> (N, N) where N: AtLeast32Bit + Clone {
era_duration: u64,
) -> (N, N)
where
N: AtLeast32Bit + Clone,
{
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;

let portion = Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR);
let payout = portion * yearly_inflation.calculate_for_fraction_times_denominator(
npos_token_staked,
total_tokens.clone(),
);
let payout = portion
* yearly_inflation.calculate_for_fraction_times_denominator(npos_token_staked, total_tokens.clone());
let maximum = portion * (yearly_inflation.maximum * total_tokens);
(payout, maximum)
}
Expand Down Expand Up @@ -72,24 +73,63 @@ mod test {

//super::I_NPOS.calculate_for_fraction_times_denominator(25, 100)
assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).0, 2_498);
assert_eq!(super::compute_total_payout(&I_NPOS, 5_000, 100_000u64, YEAR).0, 3_248);
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, YEAR).0, 6_246);
assert_eq!(super::compute_total_payout(&I_NPOS, 40_000, 100_000u64, YEAR).0, 8_494);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, YEAR).0, 9_993);
assert_eq!(super::compute_total_payout(&I_NPOS, 60_000, 100_000u64, YEAR).0, 4_379);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, YEAR).0, 2_733);
assert_eq!(super::compute_total_payout(&I_NPOS, 95_000, 100_000u64, YEAR).0, 2_513);
assert_eq!(super::compute_total_payout(&I_NPOS, 100_000, 100_000u64, YEAR).0, 2_505);
assert_eq!(
super::compute_total_payout(&I_NPOS, 5_000, 100_000u64, YEAR).0,
3_248
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, YEAR).0,
6_246
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 40_000, 100_000u64, YEAR).0,
8_494
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, YEAR).0,
9_993
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 60_000, 100_000u64, YEAR).0,
4_379
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, YEAR).0,
2_733
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 95_000, 100_000u64, YEAR).0,
2_513
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 100_000, 100_000u64, YEAR).0,
2_505
);

const DAY: u64 = 24 * 60 * 60 * 1000;
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, DAY).0, 17);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, DAY).0, 27);
assert_eq!(
super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, DAY).0,
17
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, DAY).0,
27
);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, DAY).0, 7);

const SIX_HOURS: u64 = 6 * 60 * 60 * 1000;
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, SIX_HOURS).0, 4);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, SIX_HOURS).0, 7);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, SIX_HOURS).0, 2);
assert_eq!(
super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, SIX_HOURS).0,
4
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, SIX_HOURS).0,
7
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, SIX_HOURS).0,
2
);

const HOUR: u64 = 60 * 60 * 1000;
assert_eq!(
Expand All @@ -98,7 +138,8 @@ mod test {
2_500_000_000_000_000_000_000_000_000u128,
5_000_000_000_000_000_000_000_000_000u128,
HOUR
).0,
)
.0,
57_038_500_000_000_000_000_000
);
}
Expand Down
Loading