staking: Flexible generation of reward curve and associated tweaks (#8327)

* Initial abstraction

* Alter rest of APIs

* Fixes

* Some extra getters in Gilt pallet.

* Refactor Gilt to avoid u128 conversions

* Simplify and improve pow in per_things

* Add scalar division to per_things

* Renaming from_fraction -> from_float, drop _approximation

* Fixes

* Fixes

* Fixes

* Fixes

* Make stuff build

* Fixes

* Fixes

* Fixes

* Fixes

* Update .gitignore

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/gilt/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/gilt/src/mock.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fixes

* Fixes

* Fixes

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2021-03-16 13:03:58 +01:00
committed by GitHub
parent b6c626399e
commit 363db4f086
32 changed files with 342 additions and 226 deletions
@@ -95,15 +95,15 @@ mod tests {
Assignment {
who: 1u32,
distribution: vec![
(10u32, Perbill::from_fraction(0.5)),
(20, Perbill::from_fraction(0.5)),
(10u32, Perbill::from_float(0.5)),
(20, Perbill::from_float(0.5)),
],
},
Assignment {
who: 2u32,
distribution: vec![
(10, Perbill::from_fraction(0.33)),
(20, Perbill::from_fraction(0.67)),
(10, Perbill::from_float(0.33)),
(20, Perbill::from_float(0.67)),
],
},
];
@@ -371,7 +371,7 @@ impl<AccountId: IdentifierT> Voter<AccountId> {
.edges
.into_iter()
.filter_map(|e| {
let per_thing = P::from_rational_approximation(e.weight, budget);
let per_thing = P::from_rational(e.weight, budget);
// trim zero edges.
if per_thing.is_zero() { None } else { Some((e.who, per_thing)) }
}).collect::<Vec<_>>();
@@ -551,7 +551,7 @@ impl<AccountId> StakedAssignment<AccountId> {
let distribution = self.distribution
.into_iter()
.filter_map(|(target, w)| {
let per_thing = P::from_rational_approximation(w, stake);
let per_thing = P::from_rational(w, stake);
if per_thing == Bounded::min_value() {
None
} else {
@@ -345,7 +345,7 @@ pub(crate) fn run_and_compare<Output: PerThing128>(
for (candidate, per_thingy) in distribution {
if let Some(float_assignment) = float_assignments.1.iter().find(|x| x.0 == candidate ) {
assert_eq_error_rate!(
Output::from_fraction(float_assignment.1).deconstruct(),
Output::from_float(float_assignment.1).deconstruct(),
per_thingy.deconstruct(),
Output::Inner::one(),
);
@@ -101,7 +101,7 @@ pub(crate) fn calculate_max_score<AccountId: IdentifierT, P: PerThing>(
for edge in voter.edges.iter() {
let edge_candidate = edge.candidate.borrow();
if edge_candidate.elected {
let edge_contribution: ExtendedBalance = P::from_rational_approximation(
let edge_contribution: ExtendedBalance = P::from_rational(
edge.weight,
edge_candidate.backed_stake,
).deconstruct().into();
@@ -37,7 +37,6 @@ use crate::{
use sp_std::{rc::Rc, vec::Vec};
use sp_std::collections::btree_map::BTreeMap;
use sp_arithmetic::{traits::Zero, Perbill};
/// The type used as the threshold.
///
/// Just some reading sugar; Must always be same as [`ExtendedBalance`];
@@ -364,7 +363,7 @@ fn slack<AccountId: IdentifierT>(voter: &Voter<AccountId>, t: Threshold) -> Exte
let candidate = edge.candidate.borrow();
if candidate.elected {
let extra =
Perbill::one().min(Perbill::from_rational_approximation(t, candidate.backed_stake))
Perbill::one().min(Perbill::from_rational(t, candidate.backed_stake))
* edge.weight;
acc.saturating_add(extra)
} else {
@@ -1095,7 +1095,7 @@ mod score {
is_score_better(
claim.clone(),
initial.clone(),
Perbill::from_rational_approximation(1u32, 10_000),
Perbill::from_rational(1u32, 10_000),
),
true,
);
@@ -1104,7 +1104,7 @@ mod score {
is_score_better(
claim.clone(),
initial.clone(),
Perbill::from_rational_approximation(2u32, 10_000),
Perbill::from_rational(2u32, 10_000),
),
true,
);
@@ -1113,7 +1113,7 @@ mod score {
is_score_better(
claim.clone(),
initial.clone(),
Perbill::from_rational_approximation(3u32, 10_000),
Perbill::from_rational(3u32, 10_000),
),
true,
);
@@ -1122,7 +1122,7 @@ mod score {
is_score_better(
claim.clone(),
initial.clone(),
Perbill::from_rational_approximation(4u32, 10_000),
Perbill::from_rational(4u32, 10_000),
),
true,
);
@@ -1131,7 +1131,7 @@ mod score {
is_score_better(
claim.clone(),
initial.clone(),
Perbill::from_rational_approximation(5u32, 10_000),
Perbill::from_rational(5u32, 10_000),
),
false,
);