Remove multiply_by_rational (#11598)

* Removed multiply_by_rational
Replaced with multiply_by_rational_with_rounding

* fixes

* Test Fixes

* nightly fmt

* Test Fix

* Fixed fuzzer.
This commit is contained in:
Boluwatife Bakre
2022-06-17 04:57:29 +01:00
committed by GitHub
parent c47431118b
commit 0108d216d2
6 changed files with 162 additions and 147 deletions
@@ -25,9 +25,9 @@ use crate::{
IdentifierT, PerThing128, VoteWeight, Voter,
};
use sp_arithmetic::{
helpers_128bit::multiply_by_rational,
helpers_128bit::multiply_by_rational_with_rounding,
traits::{Bounded, Zero},
Rational128,
Rational128, Rounding,
};
use sp_std::prelude::*;
@@ -143,10 +143,11 @@ pub fn seq_phragmen_core<AccountId: IdentifierT>(
for edge in &voter.edges {
let mut candidate = edge.candidate.borrow_mut();
if !candidate.elected && !candidate.approval_stake.is_zero() {
let temp_n = multiply_by_rational(
let temp_n = multiply_by_rational_with_rounding(
voter.load.n(),
voter.budget,
candidate.approval_stake,
Rounding::Down,
)
.unwrap_or(Bounded::max_value());
let temp_d = voter.load.d();
@@ -184,9 +185,14 @@ pub fn seq_phragmen_core<AccountId: IdentifierT>(
for edge in &mut voter.edges {
if edge.candidate.borrow().elected {
// update internal state.
edge.weight = multiply_by_rational(voter.budget, edge.load.n(), voter.load.n())
// If result cannot fit in u128. Not much we can do about it.
.unwrap_or(Bounded::max_value());
edge.weight = multiply_by_rational_with_rounding(
voter.budget,
edge.load.n(),
voter.load.n(),
Rounding::Down,
)
// If result cannot fit in u128. Not much we can do about it.
.unwrap_or(Bounded::max_value());
} else {
edge.weight = 0
}