mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 08:11:03 +00:00
Attempt to remove the where bounds in arithmetic. (#7933)
* Attempt to remove the where bounds. * Fix further and further. * Format better. * Update primitives/npos-elections/src/lib.rs * fix build * remove unused
This commit is contained in:
@@ -65,7 +65,6 @@ mod bench_closure_and_slice {
|
||||
) -> Vec<StakedAssignment<A>>
|
||||
where
|
||||
T: sp_std::ops::Mul<ExtendedBalance, Output = ExtendedBalance>,
|
||||
ExtendedBalance: From<<T as PerThing>::Inner>,
|
||||
{
|
||||
ratio
|
||||
.into_iter()
|
||||
|
||||
@@ -17,11 +17,8 @@
|
||||
|
||||
//! Helper methods for npos-elections.
|
||||
|
||||
use crate::{
|
||||
Assignment, Error, ExtendedBalance, IdentifierT, PerThing128, StakedAssignment, VoteWeight,
|
||||
WithApprovalOf,
|
||||
};
|
||||
use sp_arithmetic::{InnerOf, PerThing};
|
||||
use crate::{Assignment, Error, IdentifierT, PerThing128, StakedAssignment, VoteWeight, WithApprovalOf};
|
||||
use sp_arithmetic::PerThing;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
/// Converts a vector of ratio assignments into ones with absolute budget value.
|
||||
@@ -33,7 +30,6 @@ pub fn assignment_ratio_to_staked<A: IdentifierT, P: PerThing128, FS>(
|
||||
) -> Vec<StakedAssignment<A>>
|
||||
where
|
||||
for<'r> FS: Fn(&'r A) -> VoteWeight,
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
ratios
|
||||
.into_iter()
|
||||
@@ -51,7 +47,6 @@ pub fn assignment_ratio_to_staked_normalized<A: IdentifierT, P: PerThing128, FS>
|
||||
) -> Result<Vec<StakedAssignment<A>>, Error>
|
||||
where
|
||||
for<'r> FS: Fn(&'r A) -> VoteWeight,
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
let mut staked = assignment_ratio_to_staked(ratio, &stake_of);
|
||||
staked
|
||||
@@ -68,24 +63,19 @@ where
|
||||
/// Note that this will NOT attempt at normalizing the result.
|
||||
pub fn assignment_staked_to_ratio<A: IdentifierT, P: PerThing>(
|
||||
staked: Vec<StakedAssignment<A>>,
|
||||
) -> Vec<Assignment<A, P>>
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
) -> Vec<Assignment<A, P>> {
|
||||
staked.into_iter().map(|a| a.into_assignment()).collect()
|
||||
}
|
||||
|
||||
/// Same as [`assignment_staked_to_ratio`] and try and do normalization.
|
||||
pub fn assignment_staked_to_ratio_normalized<A: IdentifierT, P: PerThing128>(
|
||||
staked: Vec<StakedAssignment<A>>,
|
||||
) -> Result<Vec<Assignment<A, P>>, Error>
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
) -> Result<Vec<Assignment<A, P>>, Error> {
|
||||
let mut ratio = staked.into_iter().map(|a| a.into_assignment()).collect::<Vec<_>>();
|
||||
ratio.iter_mut().map(|a|
|
||||
a.try_normalize().map_err(|err| Error::ArithmeticError(err))
|
||||
).collect::<Result<_, _>>()?;
|
||||
ratio
|
||||
.iter_mut()
|
||||
.map(|a| a.try_normalize().map_err(|err| Error::ArithmeticError(err)))
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(ratio)
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@
|
||||
|
||||
use sp_arithmetic::{
|
||||
traits::{Bounded, UniqueSaturatedInto, Zero},
|
||||
InnerOf, Normalizable, PerThing, Rational128, ThresholdOrd,
|
||||
Normalizable, PerThing, Rational128, ThresholdOrd,
|
||||
};
|
||||
use sp_std::{
|
||||
cell::RefCell,
|
||||
@@ -209,7 +209,6 @@ pub trait CompactSolution: Sized {
|
||||
where
|
||||
for<'r> FS: Fn(&'r A) -> VoteWeight,
|
||||
A: IdentifierT,
|
||||
ExtendedBalance: From<InnerOf<Self::Accuracy>>,
|
||||
{
|
||||
let ratio = self.into_assignment(voter_at, target_at)?;
|
||||
let staked = helpers::assignment_ratio_to_staked_normalized(ratio, stake_of)?;
|
||||
@@ -332,14 +331,14 @@ impl<AccountId: IdentifierT> Voter<AccountId> {
|
||||
/// Note that this might create _un-normalized_ assignments, due to accuracy loss of `P`. Call
|
||||
/// site might compensate by calling `normalize()` on the returned `Assignment` as a
|
||||
/// post-precessing.
|
||||
pub fn into_assignment<P: PerThing>(self) -> Option<Assignment<AccountId, P>>
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
pub fn into_assignment<P: PerThing>(self) -> Option<Assignment<AccountId, P>> {
|
||||
let who = self.who;
|
||||
let budget = self.budget;
|
||||
let distribution = self.edges.into_iter().filter_map(|e| {
|
||||
let per_thing = P::from_rational_approximation(e.weight, budget);
|
||||
let distribution = self
|
||||
.edges
|
||||
.into_iter()
|
||||
.filter_map(|e| {
|
||||
let per_thing = P::from_rational_approximation(e.weight, budget);
|
||||
// trim zero edges.
|
||||
if per_thing.is_zero() { None } else { Some((e.who, per_thing)) }
|
||||
}).collect::<Vec<_>>();
|
||||
@@ -507,7 +506,6 @@ impl<AccountId> StakedAssignment<AccountId> {
|
||||
/// can never be re-created and does not mean anything useful anymore.
|
||||
pub fn into_assignment<P: PerThing>(self) -> Assignment<AccountId, P>
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
AccountId: IdentifierT,
|
||||
{
|
||||
let stake = self.total();
|
||||
@@ -706,10 +704,7 @@ where
|
||||
/// greater or less than `that`.
|
||||
///
|
||||
/// Note that the third component should be minimized.
|
||||
pub fn is_score_better<P: PerThing>(this: ElectionScore, that: ElectionScore, epsilon: P) -> bool
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
pub fn is_score_better<P: PerThing>(this: ElectionScore, that: ElectionScore, epsilon: P) -> bool {
|
||||
match this
|
||||
.iter()
|
||||
.zip(that.iter())
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
use crate::*;
|
||||
use sp_arithmetic::{
|
||||
traits::{One, SaturatedConversion, Zero},
|
||||
InnerOf, PerThing,
|
||||
PerThing,
|
||||
};
|
||||
use sp_runtime::assert_eq_error_rate;
|
||||
use sp_std::collections::btree_map::BTreeMap;
|
||||
@@ -321,9 +321,7 @@ pub(crate) fn run_and_compare<Output: PerThing128>(
|
||||
voters: Vec<(AccountId, Vec<AccountId>)>,
|
||||
stake_of: &Box<dyn Fn(&AccountId) -> VoteWeight>,
|
||||
to_elect: usize,
|
||||
) where
|
||||
ExtendedBalance: From<InnerOf<Output>>,
|
||||
{
|
||||
) {
|
||||
// run fixed point code.
|
||||
let ElectionResult { winners, assignments } = seq_phragmen::<_, Output>(
|
||||
to_elect,
|
||||
|
||||
@@ -27,7 +27,7 @@ use crate::{
|
||||
use sp_arithmetic::{
|
||||
helpers_128bit::multiply_by_rational,
|
||||
traits::{Bounded, Zero},
|
||||
InnerOf, Rational128,
|
||||
Rational128,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
@@ -68,10 +68,7 @@ pub fn seq_phragmen<AccountId: IdentifierT, P: PerThing128>(
|
||||
initial_candidates: Vec<AccountId>,
|
||||
initial_voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
|
||||
balance: Option<(usize, ExtendedBalance)>,
|
||||
) -> Result<ElectionResult<AccountId, P>, crate::Error>
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
) -> Result<ElectionResult<AccountId, P>, crate::Error> {
|
||||
let (candidates, voters) = setup_inputs(initial_candidates, initial_voters);
|
||||
|
||||
let (candidates, mut voters) = seq_phragmen_core::<AccountId>(
|
||||
|
||||
@@ -25,7 +25,7 @@ use crate::{
|
||||
IdentifierT, ElectionResult, ExtendedBalance, setup_inputs, VoteWeight, Voter, CandidatePtr,
|
||||
balance, PerThing128,
|
||||
};
|
||||
use sp_arithmetic::{PerThing, InnerOf, Rational128, traits::Bounded};
|
||||
use sp_arithmetic::{PerThing, Rational128, traits::Bounded};
|
||||
use sp_std::{prelude::*, rc::Rc};
|
||||
|
||||
/// Execute the phragmms method.
|
||||
@@ -46,10 +46,7 @@ pub fn phragmms<AccountId: IdentifierT, P: PerThing128>(
|
||||
initial_candidates: Vec<AccountId>,
|
||||
initial_voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
|
||||
balancing_config: Option<(usize, ExtendedBalance)>,
|
||||
) -> Result<ElectionResult<AccountId, P>, &'static str>
|
||||
where
|
||||
ExtendedBalance: From<InnerOf<P>>,
|
||||
{
|
||||
) -> Result<ElectionResult<AccountId, P>, &'static str> {
|
||||
let (candidates, mut voters) = setup_inputs(initial_candidates, initial_voters);
|
||||
|
||||
let mut winners = vec![];
|
||||
@@ -89,7 +86,7 @@ where
|
||||
pub(crate) fn calculate_max_score<AccountId: IdentifierT, P: PerThing>(
|
||||
candidates: &[CandidatePtr<AccountId>],
|
||||
voters: &[Voter<AccountId>],
|
||||
) -> Option<CandidatePtr<AccountId>> where ExtendedBalance: From<InnerOf<P>> {
|
||||
) -> Option<CandidatePtr<AccountId>> {
|
||||
for c_ptr in candidates.iter() {
|
||||
let mut candidate = c_ptr.borrow_mut();
|
||||
if !candidate.elected {
|
||||
|
||||
Reference in New Issue
Block a user