Skip to content

Commit

Permalink
update: format
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav committed Feb 26, 2020
1 parent bac2c2e commit deb27ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ construct_runtime!(
TransactionPayment: pallet_transaction_payment::{Module, Storage},
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
Council: pallet_collective::<Instance1>::{Module, Call, Storage, Origin<T>, Event<T>, Config<T>},
Elections: pallet_elections_phragmen::{Module, Call, Storage, Event<T>},
TechnicalCommittee: pallet_collective::<Instance2>::{Module, Call, Storage, Origin<T>, Event<T>, Config<T>},
TechnicalMembership: pallet_membership ::<Instance1>,
FinalityTracker: pallet_finality_tracker::{Module, Call, Inherent},
Expand All @@ -536,6 +535,7 @@ construct_runtime!(
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage},
Nicks: pallet_nicks::{Module, Call, Storage, Event<T>},

Elections: pallet_elections_phragmen::{Module, Call, Storage, Event<T>},
EthBacking: pallet_eth_backing,
EthRelay: pallet_eth_relay,
Kton: pallet_kton,
Expand Down
24 changes: 12 additions & 12 deletions primitives/phragmen/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pub(crate) fn create_stake_of(stakes: &[(AccountId, Power)]) -> Box<dyn Fn(&Acco
stakes.iter().for_each(|s| {
storage.insert(s.0, s.1);
});
let stake_of = move |who: &AccountId| -> Power { storage.get(who).unwrap().to_owned() };
Box::new(stake_of)
let power_of = move |who: &AccountId| -> Power { storage.get(who).unwrap().to_owned() };
Box::new(power_of)
}

pub(crate) fn auto_generate_self_voters<A: Clone>(candidates: &[A]) -> Vec<(A, Vec<A>)> {
Expand All @@ -71,16 +71,16 @@ pub(crate) fn check_assignments(assignments: Vec<(AccountId, Vec<PhragmenAssignm
pub(crate) fn run_and_compare(
candidates: Vec<AccountId>,
voters: Vec<(AccountId, Vec<AccountId>)>,
stake_of: Box<dyn Fn(&AccountId) -> Power>,
power_of: Box<dyn Fn(&AccountId) -> Power>,
to_elect: usize,
min_to_elect: usize,
) {
// run fixed point code.
let PhragmenResult { winners, assignments } =
elect::<_, _>(to_elect, min_to_elect, candidates.clone(), voters.clone(), &stake_of).unwrap();
elect::<_, _>(to_elect, min_to_elect, candidates.clone(), voters.clone(), &power_of).unwrap();

// run float poc code.
let truth_value = elect_float(to_elect, min_to_elect, candidates, voters, &stake_of).unwrap();
let truth_value = elect_float(to_elect, min_to_elect, candidates, voters, &power_of).unwrap();

assert_eq!(winners, truth_value.winners);

Expand Down Expand Up @@ -110,7 +110,7 @@ pub(crate) fn elect_float<A, FS>(
minimum_candidate_count: usize,
initial_candidates: Vec<A>,
initial_voters: Vec<(A, Vec<A>)>,
stake_of: FS,
power_of: FS,
) -> Option<_PhragmenResult<A>>
where
A: Default + Ord + Member + Copy,
Expand Down Expand Up @@ -139,7 +139,7 @@ where
}

voters.extend(initial_voters.into_iter().map(|(who, votes)| {
let voter_stake = stake_of(&who) as f64;
let voter_stake = power_of(&who) as f64;
let mut edges: Vec<_Edge<A>> = Vec::with_capacity(votes.len());
for v in votes {
if let Some(idx) = c_idx_cache.get(&v) {
Expand Down Expand Up @@ -220,15 +220,15 @@ where
})
}

pub(crate) fn build_support_map<FS>(result: &mut _PhragmenResult<AccountId>, stake_of: FS) -> _SupportMap<AccountId>
pub(crate) fn build_support_map<FS>(result: &mut _PhragmenResult<AccountId>, power_of: FS) -> _SupportMap<AccountId>
where
for<'r> FS: Fn(&'r AccountId) -> Power,
{
let mut supports = <_SupportMap<AccountId>>::new();
result
.winners
.iter()
.map(|(e, _)| (e, stake_of(e) as f64))
.map(|(e, _)| (e, power_of(e) as f64))
.for_each(|(e, s)| {
let item = _Support {
own: s,
Expand All @@ -240,7 +240,7 @@ where

for (n, assignment) in result.assignments.iter_mut() {
for (c, r) in assignment.iter_mut() {
let nominator_stake = stake_of(n) as f64;
let nominator_stake = power_of(n) as f64;
let other_stake = nominator_stake * *r;
if let Some(support) = supports.get_mut(c) {
support.total = support.total + other_stake;
Expand All @@ -257,15 +257,15 @@ pub(crate) fn equalize_float<A, FS>(
supports: &mut _SupportMap<A>,
tolerance: f64,
iterations: usize,
stake_of: FS,
power_of: FS,
) where
for<'r> FS: Fn(&'r A) -> Power,
A: Ord + Clone + std::fmt::Debug,
{
for _i in 0..iterations {
let mut max_diff = 0.0;
for (voter, assignment) in assignments.iter_mut() {
let voter_budget = stake_of(&voter);
let voter_budget = power_of(&voter);
let diff = do_equalize_float(voter, voter_budget, assignment, supports, tolerance);
if diff > max_diff {
max_diff = diff;
Expand Down
52 changes: 26 additions & 26 deletions primitives/phragmen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{elect, mock::*, PhragmenResult};
fn float_phragmen_poc_works() {
let candidates = vec![1, 2, 3];
let voters = vec![(10, vec![1, 2]), (20, vec![1, 3]), (30, vec![2, 3])];
let stake_of = create_stake_of(&[(10, 10), (20, 20), (30, 30), (1, 0), (2, 0), (3, 0)]);
let mut phragmen_result = elect_float(2, 2, candidates, voters, &stake_of).unwrap();
let power_of = create_stake_of(&[(10, 10), (20, 20), (30, 30), (1, 0), (2, 0), (3, 0)]);
let mut phragmen_result = elect_float(2, 2, candidates, voters, &power_of).unwrap();
let winners = phragmen_result.clone().winners;
let assignments = phragmen_result.clone().assignments;

Expand All @@ -26,7 +26,7 @@ fn float_phragmen_poc_works() {
]
);

let mut support_map = build_support_map(&mut phragmen_result, &stake_of);
let mut support_map = build_support_map(&mut phragmen_result, &power_of);

assert_eq!(
support_map.get(&2).unwrap(),
Expand All @@ -45,7 +45,7 @@ fn float_phragmen_poc_works() {
}
);

equalize_float(phragmen_result.assignments, &mut support_map, 0.0, 2, stake_of);
equalize_float(phragmen_result.assignments, &mut support_map, 0.0, 2, power_of);

assert_eq!(
support_map.get(&2).unwrap(),
Expand Down Expand Up @@ -97,26 +97,26 @@ fn phragmen_poc_works() {
fn phragmen_poc_2_works() {
let candidates = vec![10, 20, 30];
let voters = vec![(2, vec![10, 20, 30]), (4, vec![10, 20, 40])];
let stake_of = create_stake_of(&[(10, 1_000), (20, 1_000), (30, 1_000), (40, 1_000), (2, 500), (4, 500)]);
let power_of = create_stake_of(&[(10, 1_000), (20, 1_000), (30, 1_000), (40, 1_000), (2, 500), (4, 500)]);

run_and_compare(candidates, voters, stake_of, 2, 2);
run_and_compare(candidates, voters, power_of, 2, 2);
}

#[test]
fn phragmen_poc_3_works() {
let candidates = vec![10, 20, 30];
let voters = vec![(2, vec![10, 20, 30]), (4, vec![10, 20, 40])];
let stake_of = create_stake_of(&[(10, 1_000), (20, 1_000), (30, 1_000), (2, 50), (4, 1_000)]);
let power_of = create_stake_of(&[(10, 1_000), (20, 1_000), (30, 1_000), (2, 50), (4, 1_000)]);

run_and_compare(candidates, voters, stake_of, 2, 2);
run_and_compare(candidates, voters, power_of, 2, 2);
}

#[test]
fn phragmen_accuracy_on_large_scale_only_validators() {
// because of this particular situation we had per_u128 and now rational128. In practice, a
// candidate can have the maximum amount of tokens, and also supported by the maximum.
let candidates = vec![1, 2, 3, 4, 5];
let stake_of = create_stake_of(&[
let power_of = create_stake_of(&[
(1, (u32::max_value() - 1).into()),
(2, (u32::max_value() - 4).into()),
(3, (u32::max_value() - 5).into()),
Expand All @@ -129,7 +129,7 @@ fn phragmen_accuracy_on_large_scale_only_validators() {
2,
candidates.clone(),
auto_generate_self_voters(&candidates),
stake_of,
power_of,
)
.unwrap();

Expand All @@ -143,7 +143,7 @@ fn phragmen_accuracy_on_large_scale_validators_and_nominators() {
let candidates = vec![1, 2, 3, 4, 5];
let mut voters = vec![(13, vec![1, 3, 5]), (14, vec![2, 4])];
voters.extend(auto_generate_self_voters(&candidates));
let stake_of = create_stake_of(&[
let power_of = create_stake_of(&[
(1, (u32::max_value() - 1).into()),
(2, (u32::max_value() - 4).into()),
(3, (u32::max_value() - 5).into()),
Expand All @@ -153,7 +153,7 @@ fn phragmen_accuracy_on_large_scale_validators_and_nominators() {
(14, u32::max_value().into()),
]);

let PhragmenResult { winners, assignments } = elect::<_, _>(2, 2, candidates, voters, stake_of).unwrap();
let PhragmenResult { winners, assignments } = elect::<_, _>(2, 2, candidates, voters, power_of).unwrap();

assert_eq_uvec!(winners, vec![(2, 8_589_934_586_u64), (1, 8_589_934_579_u64)]);
assert_eq!(
Expand All @@ -172,12 +172,12 @@ fn phragmen_accuracy_on_large_scale_validators_and_nominators() {
fn phragmen_accuracy_on_small_scale_self_vote() {
let candidates = vec![40, 10, 20, 30];
let voters = auto_generate_self_voters(&candidates);
let stake_of = create_stake_of(&[(40, 0), (10, 1), (20, 2), (30, 1)]);
let power_of = create_stake_of(&[(40, 0), (10, 1), (20, 2), (30, 1)]);

let PhragmenResult {
winners,
assignments: _,
} = elect::<_, _>(3, 3, candidates, voters, stake_of).unwrap();
} = elect::<_, _>(3, 3, candidates, voters, power_of).unwrap();

assert_eq_uvec!(winners, vec![(20, 2), (10, 1), (30, 1)]);
}
Expand All @@ -186,7 +186,7 @@ fn phragmen_accuracy_on_small_scale_self_vote() {
fn phragmen_accuracy_on_small_scale_no_self_vote() {
let candidates = vec![40, 10, 20, 30];
let voters = vec![(1, vec![10]), (2, vec![20]), (3, vec![30]), (4, vec![40])];
let stake_of = create_stake_of(&[
let power_of = create_stake_of(&[
(40, 1000), // don't care
(10, 1000), // don't care
(20, 1000), // don't care
Expand All @@ -200,7 +200,7 @@ fn phragmen_accuracy_on_small_scale_no_self_vote() {
let PhragmenResult {
winners,
assignments: _,
} = elect::<_, _>(3, 3, candidates, voters, stake_of).unwrap();
} = elect::<_, _>(3, 3, candidates, voters, power_of).unwrap();

assert_eq_uvec!(winners, vec![(20, 2), (10, 1), (30, 1)]);
}
Expand All @@ -210,7 +210,7 @@ fn phragmen_large_scale_test() {
let candidates = vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24];
let mut voters = vec![(50, vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24])];
voters.extend(auto_generate_self_voters(&candidates));
let stake_of = create_stake_of(&[
let power_of = create_stake_of(&[
(2, 1),
(4, 10),
(6, 100),
Expand All @@ -226,7 +226,7 @@ fn phragmen_large_scale_test() {
(50, 99_000_000),
]);

let PhragmenResult { winners, assignments } = elect::<_, _>(2, 2, candidates, voters, stake_of).unwrap();
let PhragmenResult { winners, assignments } = elect::<_, _>(2, 2, candidates, voters, power_of).unwrap();

assert_eq_uvec!(winners, vec![(24, 149_200_000_u64), (22, 149_100_000_u64)]);
check_assignments(assignments);
Expand All @@ -241,9 +241,9 @@ fn phragmen_large_scale_test_2() {
let mut voters = vec![(50, vec![2, 4])];
voters.extend(auto_generate_self_voters(&candidates));

let stake_of = create_stake_of(&[(2, c_budget as _), (4, c_budget as _), (50, nom_budget as _)]);
let power_of = create_stake_of(&[(2, c_budget as _), (4, c_budget as _), (50, nom_budget as _)]);

let PhragmenResult { winners, assignments } = elect::<_, _>(2, 2, candidates, voters, stake_of).unwrap();
let PhragmenResult { winners, assignments } = elect::<_, _>(2, 2, candidates, voters, power_of).unwrap();

assert_eq_uvec!(winners, vec![(2, 999_999_991_u64), (4, 999_999_991_u64)]);
assert_eq!(
Expand Down Expand Up @@ -275,7 +275,7 @@ fn phragmen_linear_equalize() {
(120, vec![51, 61]),
(130, vec![61, 71]),
];
let stake_of = create_stake_of(&[
let power_of = create_stake_of(&[
(11, 1000),
(21, 1000),
(31, 1000),
Expand All @@ -292,19 +292,19 @@ fn phragmen_linear_equalize() {
(130, 1000),
]);

run_and_compare(candidates, voters, stake_of, 2, 2);
run_and_compare(candidates, voters, power_of, 2, 2);
}

#[test]
fn elect_has_no_entry_barrier() {
let candidates = vec![10, 20, 30];
let voters = vec![(1, vec![10]), (2, vec![20])];
let stake_of = create_stake_of(&[(1, 10), (2, 10)]);
let power_of = create_stake_of(&[(1, 10), (2, 10)]);

let PhragmenResult {
winners,
assignments: _,
} = elect::<_, _>(3, 3, candidates, voters, stake_of).unwrap();
} = elect::<_, _>(3, 3, candidates, voters, power_of).unwrap();

// 30 is elected with stake 0. The caller is responsible for stripping this.
assert_eq_uvec!(winners, vec![(10, 10), (20, 10), (30, 0),]);
Expand All @@ -314,9 +314,9 @@ fn elect_has_no_entry_barrier() {
fn minimum_to_elect_is_respected() {
let candidates = vec![10, 20, 30];
let voters = vec![(1, vec![10]), (2, vec![20])];
let stake_of = create_stake_of(&[(1, 10), (2, 10)]);
let power_of = create_stake_of(&[(1, 10), (2, 10)]);

let maybe_result = elect::<_, _>(10, 10, candidates, voters, stake_of);
let maybe_result = elect::<_, _>(10, 10, candidates, voters, power_of);

assert!(maybe_result.is_none());
}

0 comments on commit deb27ed

Please sign in to comment.