mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 14:01:02 +00:00
Rename Palette to FRAME (#4182)
* palette -> frame * PALETTE, Palette -> FRAME * Move folder pallete -> frame * Update docs/Structure.adoc Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com> * Update docs/README.adoc Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com> * Update README.adoc
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
[package]
|
||||
name = "pallet-staking"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.101", optional = true }
|
||||
safe-mix = { version = "1.0.0", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
|
||||
substrate-keyring = { path = "../../primitives/keyring", optional = true }
|
||||
rstd = { package = "sr-std", path = "../../primitives/sr-std", default-features = false }
|
||||
phragmen = { package = "substrate-phragmen", path = "../../primitives/phragmen", default-features = false }
|
||||
runtime-io ={ package = "sr-io", path = "../../primitives/sr-io", default-features = false }
|
||||
sr-primitives = { path = "../../primitives/sr-primitives", default-features = false }
|
||||
sr-staking-primitives = { path = "../../primitives/sr-staking-primitives", default-features = false }
|
||||
support = { package = "frame-support", path = "../support", default-features = false }
|
||||
system = { package = "frame-system", path = "../system", default-features = false }
|
||||
session = { package = "pallet-session", path = "../session", default-features = false, features = ["historical"] }
|
||||
authorship = { package = "pallet-authorship", path = "../authorship", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
primitives = { package = "substrate-primitives", path = "../../primitives/core" }
|
||||
balances = { package = "pallet-balances", path = "../balances" }
|
||||
timestamp = { package = "pallet-timestamp", path = "../timestamp" }
|
||||
pallet-staking-reward-curve = { path = "../staking/reward-curve"}
|
||||
|
||||
[features]
|
||||
equalize = []
|
||||
default = ["std", "equalize"]
|
||||
std = [
|
||||
"serde",
|
||||
"safe-mix/std",
|
||||
"substrate-keyring",
|
||||
"codec/std",
|
||||
"rstd/std",
|
||||
"phragmen/std",
|
||||
"runtime-io/std",
|
||||
"support/std",
|
||||
"sr-primitives/std",
|
||||
"sr-staking-primitives/std",
|
||||
"session/std",
|
||||
"system/std",
|
||||
"authorship/std",
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "pallet-staking-reward-curve"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "1.0.7", features = [ "full", "visit" ] }
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0.6"
|
||||
proc-macro-crate = "0.1.4"
|
||||
|
||||
[dev-dependencies]
|
||||
sr-primitives = { path = "../../../primitives/sr-primitives" }
|
||||
@@ -0,0 +1,425 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
mod log;
|
||||
|
||||
use log::log2;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{TokenStream as TokenStream2, Span};
|
||||
use proc_macro_crate::crate_name;
|
||||
use quote::{quote, ToTokens};
|
||||
use std::convert::TryInto;
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
|
||||
/// Accepts a number of expressions to create a instance of PiecewiseLinear which represents the
|
||||
/// NPoS curve (as detailed
|
||||
/// [here](http://research.web3.foundation/en/latest/polkadot/Token%20Economics/#inflation-model))
|
||||
/// for those parameters. Parameters are:
|
||||
/// - `min_inflation`: the minimal amount to be rewarded between validators, expressed as a fraction
|
||||
/// of total issuance. Known as `I_0` in the literature.
|
||||
/// Expressed in millionth, must be between 0 and 1_000_000.
|
||||
///
|
||||
/// - `max_inflation`: the maximum amount to be rewarded between validators, expressed as a fraction
|
||||
/// of total issuance. This is attained only when `ideal_stake` is achieved.
|
||||
/// Expressed in millionth, must be between min_inflation and 1_000_000.
|
||||
///
|
||||
/// - `ideal_stake`: the fraction of total issued tokens that should be actively staked behind
|
||||
/// validators. Known as `x_ideal` in the literature.
|
||||
/// Expressed in millionth, must be between 0_100_000 and 0_900_000.
|
||||
///
|
||||
/// - `falloff`: Known as `decay_rate` in the literature. A co-efficient dictating the strength of
|
||||
/// the global incentivisation to get the `ideal_stake`. A higher number results in less typical
|
||||
/// inflation at the cost of greater volatility for validators.
|
||||
/// Expressed in millionth, must be between 0 and 1_000_000.
|
||||
///
|
||||
/// - `max_piece_count`: The maximum number of pieces in the curve. A greater number uses more
|
||||
/// resources but results in higher accuracy.
|
||||
/// Must be between 2 and 1_000.
|
||||
///
|
||||
/// - `test_precision`: The maximum error allowed in the generated test.
|
||||
/// Expressed in millionth, must be between 0 and 1_000_000.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() {}
|
||||
/// use sr_primitives::curve::PiecewiseLinear;
|
||||
///
|
||||
/// pallet_staking_reward_curve::build! {
|
||||
/// const I_NPOS: PiecewiseLinear<'static> = curve!(
|
||||
/// min_inflation: 0_025_000,
|
||||
/// max_inflation: 0_100_000,
|
||||
/// ideal_stake: 0_500_000,
|
||||
/// falloff: 0_050_000,
|
||||
/// max_piece_count: 40,
|
||||
/// test_precision: 0_005_000,
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro]
|
||||
pub fn build(input: TokenStream) -> TokenStream {
|
||||
let input = syn::parse_macro_input!(input as INposInput);
|
||||
|
||||
let points = compute_points(&input);
|
||||
|
||||
let declaration = generate_piecewise_linear(points);
|
||||
let test_module = generate_test_module(&input);
|
||||
|
||||
let imports = match crate_name("sr-primitives") {
|
||||
Ok(sr_primitives) => {
|
||||
let ident = syn::Ident::new(&sr_primitives, Span::call_site());
|
||||
quote!( extern crate #ident as _sr_primitives; )
|
||||
},
|
||||
Err(e) => syn::Error::new(Span::call_site(), &e).to_compile_error(),
|
||||
};
|
||||
|
||||
let const_name = input.ident;
|
||||
let const_type = input.typ;
|
||||
|
||||
quote!(
|
||||
const #const_name: #const_type = {
|
||||
#imports
|
||||
#declaration
|
||||
};
|
||||
#test_module
|
||||
).into()
|
||||
}
|
||||
|
||||
const MILLION: u32 = 1_000_000;
|
||||
|
||||
mod keyword {
|
||||
syn::custom_keyword!(curve);
|
||||
syn::custom_keyword!(min_inflation);
|
||||
syn::custom_keyword!(max_inflation);
|
||||
syn::custom_keyword!(ideal_stake);
|
||||
syn::custom_keyword!(falloff);
|
||||
syn::custom_keyword!(max_piece_count);
|
||||
syn::custom_keyword!(test_precision);
|
||||
}
|
||||
|
||||
struct INposInput {
|
||||
ident: syn::Ident,
|
||||
typ: syn::Type,
|
||||
min_inflation: u32,
|
||||
ideal_stake: u32,
|
||||
max_inflation: u32,
|
||||
falloff: u32,
|
||||
max_piece_count: u32,
|
||||
test_precision: u32,
|
||||
}
|
||||
|
||||
struct Bounds {
|
||||
min: u32,
|
||||
min_strict: bool,
|
||||
max: u32,
|
||||
max_strict: bool,
|
||||
}
|
||||
|
||||
impl Bounds {
|
||||
fn check(&self, value: u32) -> bool {
|
||||
let wrong = (self.min_strict && value <= self.min)
|
||||
|| (!self.min_strict && value < self.min)
|
||||
|| (self.max_strict && value >= self.max)
|
||||
|| (!self.max_strict && value > self.max);
|
||||
|
||||
!wrong
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Display for Bounds {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}{:07}; {:07}{}",
|
||||
if self.min_strict { "]" } else { "[" },
|
||||
self.min,
|
||||
self.max,
|
||||
if self.max_strict { "[" } else { "]" },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_field<Token: Parse + Default + ToTokens>(input: ParseStream, bounds: Bounds)
|
||||
-> syn::Result<u32>
|
||||
{
|
||||
<Token>::parse(&input)?;
|
||||
<syn::Token![:]>::parse(&input)?;
|
||||
let value_lit = syn::LitInt::parse(&input)?;
|
||||
let value: u32 = value_lit.base10_parse()?;
|
||||
if !bounds.check(value) {
|
||||
return Err(syn::Error::new(value_lit.span(), format!(
|
||||
"Invalid {}: {}, must be in {}", Token::default().to_token_stream(), value, bounds,
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
impl Parse for INposInput {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
let args_input;
|
||||
|
||||
<syn::Token![const]>::parse(&input)?;
|
||||
let ident = <syn::Ident>::parse(&input)?;
|
||||
<syn::Token![:]>::parse(&input)?;
|
||||
let typ = <syn::Type>::parse(&input)?;
|
||||
<syn::Token![=]>::parse(&input)?;
|
||||
<keyword::curve>::parse(&input)?;
|
||||
<syn::Token![!]>::parse(&input)?;
|
||||
syn::parenthesized!(args_input in input);
|
||||
<syn::Token![;]>::parse(&input)?;
|
||||
|
||||
if !input.is_empty() {
|
||||
return Err(input.error("expected end of input stream, no token expected"));
|
||||
}
|
||||
|
||||
let min_inflation = parse_field::<keyword::min_inflation>(&args_input, Bounds {
|
||||
min: 0,
|
||||
min_strict: true,
|
||||
max: 1_000_000,
|
||||
max_strict: false,
|
||||
})?;
|
||||
<syn::Token![,]>::parse(&args_input)?;
|
||||
let max_inflation = parse_field::<keyword::max_inflation>(&args_input, Bounds {
|
||||
min: min_inflation,
|
||||
min_strict: true,
|
||||
max: 1_000_000,
|
||||
max_strict: false,
|
||||
})?;
|
||||
<syn::Token![,]>::parse(&args_input)?;
|
||||
let ideal_stake = parse_field::<keyword::ideal_stake>(&args_input, Bounds {
|
||||
min: 0_100_000,
|
||||
min_strict: false,
|
||||
max: 0_900_000,
|
||||
max_strict: false,
|
||||
})?;
|
||||
<syn::Token![,]>::parse(&args_input)?;
|
||||
let falloff = parse_field::<keyword::falloff>(&args_input, Bounds {
|
||||
min: 0_010_000,
|
||||
min_strict: false,
|
||||
max: 1_000_000,
|
||||
max_strict: false,
|
||||
})?;
|
||||
<syn::Token![,]>::parse(&args_input)?;
|
||||
let max_piece_count = parse_field::<keyword::max_piece_count>(&args_input, Bounds {
|
||||
min: 2,
|
||||
min_strict: false,
|
||||
max: 1_000,
|
||||
max_strict: false,
|
||||
})?;
|
||||
<syn::Token![,]>::parse(&args_input)?;
|
||||
let test_precision = parse_field::<keyword::test_precision>(&args_input, Bounds {
|
||||
min: 0,
|
||||
min_strict: false,
|
||||
max: 1_000_000,
|
||||
max_strict: false,
|
||||
})?;
|
||||
<Option<syn::Token![,]>>::parse(&args_input)?;
|
||||
|
||||
if !args_input.is_empty() {
|
||||
return Err(args_input.error("expected end of input stream, no token expected"));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
ident,
|
||||
typ,
|
||||
min_inflation,
|
||||
ideal_stake,
|
||||
max_inflation,
|
||||
falloff,
|
||||
max_piece_count,
|
||||
test_precision,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct INPoS {
|
||||
i_0: u32,
|
||||
i_ideal_times_x_ideal: u32,
|
||||
i_ideal: u32,
|
||||
x_ideal: u32,
|
||||
d: u32,
|
||||
}
|
||||
|
||||
impl INPoS {
|
||||
fn from_input(input: &INposInput) -> Self {
|
||||
INPoS {
|
||||
i_0: input.min_inflation,
|
||||
i_ideal: (input.max_inflation as u64 * MILLION as u64 / input.ideal_stake as u64)
|
||||
.try_into().unwrap(),
|
||||
i_ideal_times_x_ideal: input.max_inflation,
|
||||
x_ideal: input.ideal_stake,
|
||||
d: input.falloff,
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_opposite_after_x_ideal(&self, y: u32) -> u32 {
|
||||
if y == self.i_0 {
|
||||
return u32::max_value();
|
||||
}
|
||||
let log = log2(self.i_ideal_times_x_ideal - self.i_0, y - self.i_0);
|
||||
|
||||
let term: u32 = ((self.d as u64 * log as u64) / 1_000_000).try_into().unwrap();
|
||||
|
||||
self.x_ideal + term
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_points(input: &INposInput) -> Vec<(u32, u32)> {
|
||||
let inpos = INPoS::from_input(input);
|
||||
|
||||
let mut points = vec![];
|
||||
points.push((0, inpos.i_0));
|
||||
points.push((inpos.x_ideal, inpos.i_ideal_times_x_ideal));
|
||||
|
||||
// For each point p: (next_p.0 - p.0) < segment_lenght && (next_p.1 - p.1) < segment_lenght.
|
||||
// This ensures that the total number of segment doesn't overflow max_piece_count.
|
||||
let max_length = (input.max_inflation - input.min_inflation + 1_000_000 - inpos.x_ideal)
|
||||
/ (input.max_piece_count - 1);
|
||||
|
||||
let mut delta_y = max_length;
|
||||
let mut y = input.max_inflation;
|
||||
|
||||
// The algorithm divide the curve in segment with vertical len and horizontal len less
|
||||
// than `max_length`. This is not very accurate in case of very consequent steep.
|
||||
while delta_y != 0 {
|
||||
let next_y = y - delta_y;
|
||||
|
||||
if next_y <= input.min_inflation {
|
||||
delta_y = delta_y.saturating_sub(1);
|
||||
continue
|
||||
}
|
||||
|
||||
let next_x = inpos.compute_opposite_after_x_ideal(next_y);
|
||||
|
||||
if (next_x - points.last().unwrap().0) > max_length {
|
||||
delta_y = delta_y.saturating_sub(1);
|
||||
continue
|
||||
}
|
||||
|
||||
if next_x >= 1_000_000 {
|
||||
let prev = points.last().unwrap();
|
||||
// Compute the y corresponding to x=1_000_000 using the this point and the previous one.
|
||||
|
||||
let delta_y: u32 = (
|
||||
(next_x - 1_000_000) as u64
|
||||
* (prev.1 - next_y) as u64
|
||||
/ (next_x - prev.0) as u64
|
||||
).try_into().unwrap();
|
||||
|
||||
let y = next_y + delta_y;
|
||||
|
||||
points.push((1_000_000, y));
|
||||
return points;
|
||||
}
|
||||
points.push((next_x, next_y));
|
||||
y = next_y;
|
||||
}
|
||||
|
||||
points.push((1_000_000, inpos.i_0));
|
||||
|
||||
points
|
||||
}
|
||||
|
||||
fn generate_piecewise_linear(points: Vec<(u32, u32)>) -> TokenStream2 {
|
||||
let mut points_tokens = quote!();
|
||||
|
||||
let max = points.iter()
|
||||
.map(|&(_, x)| x)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
.checked_mul(1_000)
|
||||
// clip at 1.0 for sanity only since it'll panic later if too high.
|
||||
.unwrap_or(1_000_000_000);
|
||||
|
||||
for (x, y) in points {
|
||||
let error = || panic!(format!(
|
||||
"Generated reward curve approximation doesn't fit into [0, 1] -> [0, 1] \
|
||||
because of point:
|
||||
x = {:07} per million
|
||||
y = {:07} per million",
|
||||
x, y
|
||||
));
|
||||
|
||||
let x_perbill = x.checked_mul(1_000).unwrap_or_else(error);
|
||||
let y_perbill = y.checked_mul(1_000).unwrap_or_else(error);
|
||||
|
||||
points_tokens.extend(quote!(
|
||||
(
|
||||
_sr_primitives::Perbill::from_parts(#x_perbill),
|
||||
_sr_primitives::Perbill::from_parts(#y_perbill),
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
quote!(
|
||||
_sr_primitives::curve::PiecewiseLinear::<'static> {
|
||||
points: & [ #points_tokens ],
|
||||
maximum: _sr_primitives::Perbill::from_parts(#max),
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn generate_test_module(input: &INposInput) -> TokenStream2 {
|
||||
let inpos = INPoS::from_input(input);
|
||||
|
||||
let ident = &input.ident;
|
||||
let precision = input.test_precision;
|
||||
let i_0 = inpos.i_0 as f64/ MILLION as f64;
|
||||
let i_ideal_times_x_ideal = inpos.i_ideal_times_x_ideal as f64 / MILLION as f64;
|
||||
let i_ideal = inpos.i_ideal as f64 / MILLION as f64;
|
||||
let x_ideal = inpos.x_ideal as f64 / MILLION as f64;
|
||||
let d = inpos.d as f64 / MILLION as f64;
|
||||
let max_piece_count = input.max_piece_count;
|
||||
|
||||
quote!(
|
||||
#[cfg(test)]
|
||||
mod __pallet_staking_reward_curve_test_module {
|
||||
fn i_npos(x: f64) -> f64 {
|
||||
if x <= #x_ideal {
|
||||
#i_0 + x * (#i_ideal - #i_0 / #x_ideal)
|
||||
} else {
|
||||
#i_0 + (#i_ideal_times_x_ideal - #i_0) * 2_f64.powf((#x_ideal - x) / #d)
|
||||
}
|
||||
}
|
||||
|
||||
const MILLION: u32 = 1_000_000;
|
||||
|
||||
#[test]
|
||||
fn reward_curve_precision() {
|
||||
for &base in [MILLION, u32::max_value()].into_iter() {
|
||||
let number_of_check = 100_000.min(base);
|
||||
for check_index in 0..=number_of_check {
|
||||
let i = (check_index as u64 * base as u64 / number_of_check as u64) as u32;
|
||||
let x = i as f64 / base as f64;
|
||||
let float_res = (i_npos(x) * base as f64).round() as u32;
|
||||
let int_res = super::#ident.calculate_for_fraction_times_denominator(i, base);
|
||||
let err = (
|
||||
(float_res.max(int_res) - float_res.min(int_res)) as u64
|
||||
* MILLION as u64
|
||||
/ float_res as u64
|
||||
) as u32;
|
||||
if err > #precision {
|
||||
panic!(format!("\n\
|
||||
Generated reward curve approximation differ from real one:\n\t\
|
||||
for i = {} and base = {}, f(i/base) * base = {},\n\t\
|
||||
but approximation = {},\n\t\
|
||||
err = {:07} millionth,\n\t\
|
||||
try increase the number of segment: {} or the test_error: {}.\n",
|
||||
i, base, float_res, int_res, err, #max_piece_count, #precision
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reward_curve_piece_count() {
|
||||
assert!(
|
||||
super::#ident.points.len() as u32 - 1 <= #max_piece_count,
|
||||
"Generated reward curve approximation is invalid: \
|
||||
has more points than specified, please fill an issue."
|
||||
);
|
||||
}
|
||||
}
|
||||
).into()
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
/// Return Per-million value.
|
||||
pub fn log2(p: u32, q: u32) -> u32 {
|
||||
assert!(p >= q);
|
||||
assert!(p <= u32::max_value()/2);
|
||||
|
||||
// This restriction should not be mandatory. But function is only tested and used for this.
|
||||
assert!(p <= 1_000_000);
|
||||
assert!(q <= 1_000_000);
|
||||
|
||||
if p == q {
|
||||
return 0
|
||||
}
|
||||
|
||||
let mut n = 0u32;
|
||||
while !(p >= 2u32.pow(n)*q) || !(p < 2u32.pow(n+1)*q) {
|
||||
n += 1;
|
||||
}
|
||||
assert!(p < 2u32.pow(n+1) * q);
|
||||
|
||||
let y_num: u32 = (p - 2u32.pow(n) * q).try_into().unwrap();
|
||||
let y_den: u32 = (p + 2u32.pow(n) * q).try_into().unwrap();
|
||||
|
||||
let _2_div_ln_2 = 2_885_390u32;
|
||||
|
||||
let taylor_term = |k: u32| -> u32 {
|
||||
if k == 0 {
|
||||
(_2_div_ln_2 as u128 * (y_num as u128).pow(1) / (y_den as u128).pow(1))
|
||||
.try_into().unwrap()
|
||||
} else {
|
||||
let mut res = _2_div_ln_2 as u128 * (y_num as u128).pow(3) / (y_den as u128).pow(3);
|
||||
for _ in 1..k {
|
||||
res = res * (y_num as u128).pow(2) / (y_den as u128).pow(2);
|
||||
}
|
||||
res /= 2 * k as u128 + 1;
|
||||
|
||||
res.try_into().unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
let mut res = n * 1_000_000u32;
|
||||
let mut k = 0;
|
||||
loop {
|
||||
let term = taylor_term(k);
|
||||
if term == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
res += term;
|
||||
k += 1;
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_log() {
|
||||
let div = 1_000;
|
||||
for p in 0..=div {
|
||||
for q in 1..=p {
|
||||
let p: u32 = (1_000_000 as u64 * p as u64 / div as u64).try_into().unwrap();
|
||||
let q: u32 = (1_000_000 as u64 * q as u64 / div as u64).try_into().unwrap();
|
||||
|
||||
let res = - (log2(p, q) as i64);
|
||||
let expected = ((q as f64 / p as f64).log(2.0) * 1_000_000 as f64).round() as i64;
|
||||
assert!((res - expected).abs() <= 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test crate for pallet-staking-reward-curve. Allows to test for procedural macro.
|
||||
//! See tests directory.
|
||||
|
||||
mod test_small_falloff {
|
||||
pallet_staking_reward_curve::build! {
|
||||
const REWARD_CURVE: sr_primitives::curve::PiecewiseLinear<'static> = curve!(
|
||||
min_inflation: 0_020_000,
|
||||
max_inflation: 0_200_000,
|
||||
ideal_stake: 0_600_000,
|
||||
falloff: 0_010_000,
|
||||
max_piece_count: 200,
|
||||
test_precision: 0_005_000,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
mod test_big_falloff {
|
||||
pallet_staking_reward_curve::build! {
|
||||
const REWARD_CURVE: sr_primitives::curve::PiecewiseLinear<'static> = curve!(
|
||||
min_inflation: 0_100_000,
|
||||
max_inflation: 0_400_000,
|
||||
ideal_stake: 0_400_000,
|
||||
falloff: 1_000_000,
|
||||
max_piece_count: 40,
|
||||
test_precision: 0_005_000,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! This module expose one function `P_NPoS` (Payout NPoS) or `compute_total_payout` which returns
|
||||
//! the total payout for the era given the era duration and the staking rate in NPoS.
|
||||
//! The staking rate in NPoS is the total amount of tokens staked by nominators and validators,
|
||||
//! divided by the total token supply.
|
||||
|
||||
use sr_primitives::{Perbill, traits::SimpleArithmetic, curve::PiecewiseLinear};
|
||||
|
||||
/// The total payout to all validators (and their nominators) per era.
|
||||
///
|
||||
/// Defined as such:
|
||||
/// `payout = yearly_inflation(npos_token_staked / total_tokens) * total_tokans / era_per_year`
|
||||
///
|
||||
/// `era_duration` is expressed in millisecond.
|
||||
pub fn compute_total_payout<N>(
|
||||
yearly_inflation: &PiecewiseLinear<'static>,
|
||||
npos_token_staked: N,
|
||||
total_tokens: N,
|
||||
era_duration: u64
|
||||
) -> (N, N) where N: SimpleArithmetic + 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 maximum = portion * (yearly_inflation.maximum * total_tokens);
|
||||
(payout, maximum)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use sr_primitives::curve::PiecewiseLinear;
|
||||
|
||||
pallet_staking_reward_curve::build! {
|
||||
const I_NPOS: PiecewiseLinear<'static> = curve!(
|
||||
min_inflation: 0_025_000,
|
||||
max_inflation: 0_100_000,
|
||||
ideal_stake: 0_500_000,
|
||||
falloff: 0_050_000,
|
||||
max_piece_count: 40,
|
||||
test_precision: 0_005_000,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn npos_curve_is_sensible() {
|
||||
const YEAR: u64 = 365 * 24 * 60 * 60 * 1000;
|
||||
|
||||
// check maximum inflation.
|
||||
// not 10_000 due to rounding error.
|
||||
assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).1, 9_993);
|
||||
|
||||
//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);
|
||||
|
||||
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, 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);
|
||||
|
||||
const HOUR: u64 = 60 * 60 * 1000;
|
||||
assert_eq!(
|
||||
super::compute_total_payout(
|
||||
&I_NPOS,
|
||||
2_500_000_000_000_000_000_000_000_000u128,
|
||||
5_000_000_000_000_000_000_000_000_000u128,
|
||||
HOUR
|
||||
).0,
|
||||
57_038_500_000_000_000_000_000
|
||||
);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,453 @@
|
||||
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test utilities
|
||||
|
||||
use std::{collections::HashSet, cell::RefCell};
|
||||
use sr_primitives::{Perbill, KeyTypeId};
|
||||
use sr_primitives::curve::PiecewiseLinear;
|
||||
use sr_primitives::traits::{IdentityLookup, Convert, OpaqueKeys, OnInitialize, SaturatedConversion};
|
||||
use sr_primitives::testing::{Header, UintAuthorityId};
|
||||
use sr_staking_primitives::SessionIndex;
|
||||
use primitives::{H256, crypto::key_types};
|
||||
use runtime_io;
|
||||
use support::{assert_ok, impl_outer_origin, parameter_types, StorageLinkedMap};
|
||||
use support::traits::{Currency, Get, FindAuthor};
|
||||
use crate::{
|
||||
EraIndex, GenesisConfig, Module, Trait, StakerStatus, ValidatorPrefs, RewardDestination,
|
||||
Nominators, inflation
|
||||
};
|
||||
|
||||
/// The AccountId alias in this test module.
|
||||
pub type AccountId = u64;
|
||||
pub type BlockNumber = u64;
|
||||
pub type Balance = u64;
|
||||
|
||||
/// Simple structure that exposes how u64 currency can be represented as... u64.
|
||||
pub struct CurrencyToVoteHandler;
|
||||
impl Convert<u64, u64> for CurrencyToVoteHandler {
|
||||
fn convert(x: u64) -> u64 { x }
|
||||
}
|
||||
impl Convert<u128, u64> for CurrencyToVoteHandler {
|
||||
fn convert(x: u128) -> u64 { x.saturated_into() }
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static SESSION: RefCell<(Vec<AccountId>, HashSet<AccountId>)> = RefCell::new(Default::default());
|
||||
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
|
||||
}
|
||||
|
||||
pub struct TestSessionHandler;
|
||||
impl session::SessionHandler<AccountId> for TestSessionHandler {
|
||||
const KEY_TYPE_IDS: &'static [KeyTypeId] = &[key_types::DUMMY];
|
||||
|
||||
fn on_genesis_session<Ks: OpaqueKeys>(_validators: &[(AccountId, Ks)]) {}
|
||||
|
||||
fn on_new_session<Ks: OpaqueKeys>(
|
||||
_changed: bool,
|
||||
validators: &[(AccountId, Ks)],
|
||||
_queued_validators: &[(AccountId, Ks)],
|
||||
) {
|
||||
SESSION.with(|x|
|
||||
*x.borrow_mut() = (validators.iter().map(|x| x.0.clone()).collect(), HashSet::new())
|
||||
);
|
||||
}
|
||||
|
||||
fn on_disabled(validator_index: usize) {
|
||||
SESSION.with(|d| {
|
||||
let mut d = d.borrow_mut();
|
||||
let value = d.0[validator_index];
|
||||
d.1.insert(value);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_disabled(controller: AccountId) -> bool {
|
||||
let stash = Staking::ledger(&controller).unwrap().stash;
|
||||
SESSION.with(|d| d.borrow().1.contains(&stash))
|
||||
}
|
||||
|
||||
pub struct ExistentialDeposit;
|
||||
impl Get<u64> for ExistentialDeposit {
|
||||
fn get() -> u64 {
|
||||
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow())
|
||||
}
|
||||
}
|
||||
|
||||
impl_outer_origin!{
|
||||
pub enum Origin for Test {}
|
||||
}
|
||||
|
||||
/// Author of block is always 11
|
||||
pub struct Author11;
|
||||
impl FindAuthor<u64> for Author11 {
|
||||
fn find_author<'a, I>(_digests: I) -> Option<u64>
|
||||
where I: 'a + IntoIterator<Item=(support::ConsensusEngineId, &'a [u8])>
|
||||
{
|
||||
Some(11)
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct Test;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 250;
|
||||
pub const MaximumBlockWeight: u32 = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
}
|
||||
impl system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = BlockNumber;
|
||||
type Call = ();
|
||||
type Hash = H256;
|
||||
type Hashing = ::sr_primitives::traits::BlakeTwo256;
|
||||
type AccountId = AccountId;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const TransferFee: Balance = 0;
|
||||
pub const CreationFee: Balance = 0;
|
||||
}
|
||||
impl balances::Trait for Test {
|
||||
type Balance = Balance;
|
||||
type OnFreeBalanceZero = Staking;
|
||||
type OnNewAccount = ();
|
||||
type Event = ();
|
||||
type TransferPayment = ();
|
||||
type DustRemoval = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type TransferFee = TransferFee;
|
||||
type CreationFee = CreationFee;
|
||||
}
|
||||
parameter_types! {
|
||||
pub const Period: BlockNumber = 1;
|
||||
pub const Offset: BlockNumber = 0;
|
||||
pub const UncleGenerations: u64 = 0;
|
||||
pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(25);
|
||||
}
|
||||
impl session::Trait for Test {
|
||||
type OnSessionEnding = session::historical::NoteHistoricalRoot<Test, Staking>;
|
||||
type Keys = UintAuthorityId;
|
||||
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
|
||||
type SessionHandler = TestSessionHandler;
|
||||
type Event = ();
|
||||
type ValidatorId = AccountId;
|
||||
type ValidatorIdOf = crate::StashOf<Test>;
|
||||
type SelectInitialValidators = Staking;
|
||||
type DisabledValidatorsThreshold = DisabledValidatorsThreshold;
|
||||
}
|
||||
|
||||
impl session::historical::Trait for Test {
|
||||
type FullIdentification = crate::Exposure<AccountId, Balance>;
|
||||
type FullIdentificationOf = crate::ExposureOf<Test>;
|
||||
}
|
||||
impl authorship::Trait for Test {
|
||||
type FindAuthor = Author11;
|
||||
type UncleGenerations = UncleGenerations;
|
||||
type FilterUncle = ();
|
||||
type EventHandler = Module<Test>;
|
||||
}
|
||||
parameter_types! {
|
||||
pub const MinimumPeriod: u64 = 5;
|
||||
}
|
||||
impl timestamp::Trait for Test {
|
||||
type Moment = u64;
|
||||
type OnTimestampSet = ();
|
||||
type MinimumPeriod = MinimumPeriod;
|
||||
}
|
||||
pallet_staking_reward_curve::build! {
|
||||
const I_NPOS: PiecewiseLinear<'static> = curve!(
|
||||
min_inflation: 0_025_000,
|
||||
max_inflation: 0_100_000,
|
||||
ideal_stake: 0_500_000,
|
||||
falloff: 0_050_000,
|
||||
max_piece_count: 40,
|
||||
test_precision: 0_005_000,
|
||||
);
|
||||
}
|
||||
parameter_types! {
|
||||
pub const SessionsPerEra: SessionIndex = 3;
|
||||
pub const BondingDuration: EraIndex = 3;
|
||||
pub const RewardCurve: &'static PiecewiseLinear<'static> = &I_NPOS;
|
||||
}
|
||||
impl Trait for Test {
|
||||
type Currency = balances::Module<Self>;
|
||||
type Time = timestamp::Module<Self>;
|
||||
type CurrencyToVote = CurrencyToVoteHandler;
|
||||
type RewardRemainder = ();
|
||||
type Event = ();
|
||||
type Slash = ();
|
||||
type Reward = ();
|
||||
type SessionsPerEra = SessionsPerEra;
|
||||
type BondingDuration = BondingDuration;
|
||||
type SessionInterface = Self;
|
||||
type RewardCurve = RewardCurve;
|
||||
}
|
||||
|
||||
pub struct ExtBuilder {
|
||||
existential_deposit: u64,
|
||||
validator_pool: bool,
|
||||
nominate: bool,
|
||||
validator_count: u32,
|
||||
minimum_validator_count: u32,
|
||||
fair: bool,
|
||||
num_validators: Option<u32>,
|
||||
invulnerables: Vec<u64>,
|
||||
}
|
||||
|
||||
impl Default for ExtBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
existential_deposit: 0,
|
||||
validator_pool: false,
|
||||
nominate: true,
|
||||
validator_count: 2,
|
||||
minimum_validator_count: 0,
|
||||
fair: true,
|
||||
num_validators: None,
|
||||
invulnerables: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtBuilder {
|
||||
pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
|
||||
self.existential_deposit = existential_deposit;
|
||||
self
|
||||
}
|
||||
pub fn validator_pool(mut self, validator_pool: bool) -> Self {
|
||||
self.validator_pool = validator_pool;
|
||||
self
|
||||
}
|
||||
pub fn nominate(mut self, nominate: bool) -> Self {
|
||||
self.nominate = nominate;
|
||||
self
|
||||
}
|
||||
pub fn validator_count(mut self, count: u32) -> Self {
|
||||
self.validator_count = count;
|
||||
self
|
||||
}
|
||||
pub fn minimum_validator_count(mut self, count: u32) -> Self {
|
||||
self.minimum_validator_count = count;
|
||||
self
|
||||
}
|
||||
pub fn fair(mut self, is_fair: bool) -> Self {
|
||||
self.fair = is_fair;
|
||||
self
|
||||
}
|
||||
pub fn num_validators(mut self, num_validators: u32) -> Self {
|
||||
self.num_validators = Some(num_validators);
|
||||
self
|
||||
}
|
||||
pub fn invulnerables(mut self, invulnerables: Vec<u64>) -> Self {
|
||||
self.invulnerables = invulnerables;
|
||||
self
|
||||
}
|
||||
pub fn set_associated_consts(&self) {
|
||||
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
|
||||
}
|
||||
pub fn build(self) -> runtime_io::TestExternalities {
|
||||
self.set_associated_consts();
|
||||
let mut storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
let balance_factor = if self.existential_deposit > 0 {
|
||||
256
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
let num_validators = self.num_validators.unwrap_or(self.validator_count);
|
||||
let validators = (0..num_validators)
|
||||
.map(|x| ((x + 1) * 10 + 1) as u64)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let _ = balances::GenesisConfig::<Test>{
|
||||
balances: vec![
|
||||
(1, 10 * balance_factor),
|
||||
(2, 20 * balance_factor),
|
||||
(3, 300 * balance_factor),
|
||||
(4, 400 * balance_factor),
|
||||
(10, balance_factor),
|
||||
(11, balance_factor * 1000),
|
||||
(20, balance_factor),
|
||||
(21, balance_factor * 2000),
|
||||
(30, balance_factor),
|
||||
(31, balance_factor * 2000),
|
||||
(40, balance_factor),
|
||||
(41, balance_factor * 2000),
|
||||
(100, 2000 * balance_factor),
|
||||
(101, 2000 * balance_factor),
|
||||
// This allow us to have a total_payout different from 0.
|
||||
(999, 1_000_000_000_000),
|
||||
],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut storage);
|
||||
|
||||
let stake_21 = if self.fair { 1000 } else { 2000 };
|
||||
let stake_31 = if self.validator_pool { balance_factor * 1000 } else { 1 };
|
||||
let status_41 = if self.validator_pool {
|
||||
StakerStatus::<AccountId>::Validator
|
||||
} else {
|
||||
StakerStatus::<AccountId>::Idle
|
||||
};
|
||||
let nominated = if self.nominate { vec![11, 21] } else { vec![] };
|
||||
let _ = GenesisConfig::<Test>{
|
||||
current_era: 0,
|
||||
stakers: vec![
|
||||
// (stash, controller, staked_amount, status)
|
||||
(11, 10, balance_factor * 1000, StakerStatus::<AccountId>::Validator),
|
||||
(21, 20, stake_21, StakerStatus::<AccountId>::Validator),
|
||||
(31, 30, stake_31, StakerStatus::<AccountId>::Validator),
|
||||
(41, 40, balance_factor * 1000, status_41),
|
||||
// nominator
|
||||
(101, 100, balance_factor * 500, StakerStatus::<AccountId>::Nominator(nominated))
|
||||
],
|
||||
validator_count: self.validator_count,
|
||||
minimum_validator_count: self.minimum_validator_count,
|
||||
invulnerables: self.invulnerables,
|
||||
slash_reward_fraction: Perbill::from_percent(10),
|
||||
..Default::default()
|
||||
}.assimilate_storage(&mut storage);
|
||||
|
||||
let _ = session::GenesisConfig::<Test> {
|
||||
keys: validators.iter().map(|x| (*x, UintAuthorityId(*x))).collect(),
|
||||
}.assimilate_storage(&mut storage);
|
||||
|
||||
let mut ext = runtime_io::TestExternalities::from(storage);
|
||||
ext.execute_with(|| {
|
||||
let validators = Session::validators();
|
||||
SESSION.with(|x|
|
||||
*x.borrow_mut() = (validators.clone(), HashSet::new())
|
||||
);
|
||||
});
|
||||
ext
|
||||
}
|
||||
}
|
||||
|
||||
pub type System = system::Module<Test>;
|
||||
pub type Balances = balances::Module<Test>;
|
||||
pub type Session = session::Module<Test>;
|
||||
pub type Timestamp = timestamp::Module<Test>;
|
||||
pub type Staking = Module<Test>;
|
||||
|
||||
pub fn check_exposure_all() {
|
||||
Staking::current_elected().into_iter().for_each(|acc| check_exposure(acc));
|
||||
}
|
||||
|
||||
pub fn check_nominator_all() {
|
||||
<Nominators<Test>>::enumerate().for_each(|(acc, _)| check_nominator_exposure(acc));
|
||||
}
|
||||
|
||||
/// Check for each selected validator: expo.total = Sum(expo.other) + expo.own
|
||||
pub fn check_exposure(stash: u64) {
|
||||
assert_is_stash(stash);
|
||||
let expo = Staking::stakers(&stash);
|
||||
assert_eq!(
|
||||
expo.total as u128, expo.own as u128 + expo.others.iter().map(|e| e.value as u128).sum::<u128>(),
|
||||
"wrong total exposure for {:?}: {:?}", stash, expo,
|
||||
);
|
||||
}
|
||||
|
||||
/// Check that for each nominator: slashable_balance > sum(used_balance)
|
||||
/// Note: we might not consume all of a nominator's balance, but we MUST NOT over spend it.
|
||||
pub fn check_nominator_exposure(stash: u64) {
|
||||
assert_is_stash(stash);
|
||||
let mut sum = 0;
|
||||
Staking::current_elected()
|
||||
.iter()
|
||||
.map(|v| Staking::stakers(v))
|
||||
.for_each(|e| e.others.iter()
|
||||
.filter(|i| i.who == stash)
|
||||
.for_each(|i| sum += i.value));
|
||||
let nominator_stake = Staking::slashable_balance_of(&stash);
|
||||
// a nominator cannot over-spend.
|
||||
assert!(
|
||||
nominator_stake >= sum,
|
||||
"failed: Nominator({}) stake({}) >= sum divided({})", stash, nominator_stake, sum,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn assert_is_stash(acc: u64) {
|
||||
assert!(Staking::bonded(&acc).is_some(), "Not a stash.");
|
||||
}
|
||||
|
||||
pub fn bond_validator(acc: u64, val: u64) {
|
||||
// a = controller
|
||||
// a + 1 = stash
|
||||
let _ = Balances::make_free_balance_be(&(acc + 1), val);
|
||||
assert_ok!(Staking::bond(Origin::signed(acc + 1), acc, val, RewardDestination::Controller));
|
||||
assert_ok!(Staking::validate(Origin::signed(acc), ValidatorPrefs::default()));
|
||||
}
|
||||
|
||||
pub fn bond_nominator(acc: u64, val: u64, target: Vec<u64>) {
|
||||
// a = controller
|
||||
// a + 1 = stash
|
||||
let _ = Balances::make_free_balance_be(&(acc + 1), val);
|
||||
assert_ok!(Staking::bond(Origin::signed(acc + 1), acc, val, RewardDestination::Controller));
|
||||
assert_ok!(Staking::nominate(Origin::signed(acc), target));
|
||||
}
|
||||
|
||||
pub fn advance_session() {
|
||||
let current_index = Session::current_index();
|
||||
start_session(current_index + 1);
|
||||
}
|
||||
|
||||
pub fn start_session(session_index: SessionIndex) {
|
||||
// Compensate for session delay
|
||||
let session_index = session_index + 1;
|
||||
for i in Session::current_index()..session_index {
|
||||
System::set_block_number((i + 1).into());
|
||||
Timestamp::set_timestamp(System::block_number() * 1000);
|
||||
Session::on_initialize(System::block_number());
|
||||
}
|
||||
|
||||
assert_eq!(Session::current_index(), session_index);
|
||||
}
|
||||
|
||||
pub fn start_era(era_index: EraIndex) {
|
||||
start_session((era_index * 3).into());
|
||||
assert_eq!(Staking::current_era(), era_index);
|
||||
}
|
||||
|
||||
pub fn current_total_payout_for_duration(duration: u64) -> u64 {
|
||||
inflation::compute_total_payout(
|
||||
<Test as Trait>::RewardCurve::get(),
|
||||
<Module<Test>>::slot_stake() * 2,
|
||||
Balances::total_issuance(),
|
||||
duration,
|
||||
).0
|
||||
}
|
||||
|
||||
pub fn reward_all_elected() {
|
||||
let rewards = <Module<Test>>::current_elected().iter()
|
||||
.map(|v| (*v, 1))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
<Module<Test>>::reward_by_ids(rewards)
|
||||
}
|
||||
|
||||
pub fn validator_controllers() -> Vec<AccountId> {
|
||||
Session::validators().into_iter().map(|s| Staking::bonded(&s).expect("no controller for validator")).collect()
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user