srml-module: Phragmen election (#3364)

* phragmen election module.

* Add new files.

* Some doc update

* Update weights.

* bump and a few nits.

* Performance improvement.

* Master.into()

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* Fix build

* Some fixes.

* Fix build.

* Proper outgoing and runner-up managment.

* Bit more sensical weight values.

* Update srml/elections-phragmen/src/lib.rs

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* fix lock file

* Fix build.

* Remove runner-ups

* Some refactors.

* Add support for reporting voters.

* Fix member check.

* Remove equlize.rs

* Update srml/elections-phragmen/src/lib.rs

* Update srml/elections-phragmen/src/lib.rs

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* Bring back runner ups.

* use decode_len

* Better weight values.

* Update bogus doc

* Bump.

* Update srml/elections-phragmen/src/lib.rs

Co-Authored-By: Gavin Wood <gavin@parity.io>

* Review comments.

* One more test

* Fix tests

* Fix build

* .. and fix benchmarks.

* Update srml/elections-phragmen/src/lib.rs

* Version bump
This commit is contained in:
Kian Paimani
2019-09-19 12:04:02 +02:00
committed by Gavin Wood
parent 5585770a12
commit bfe240d1b0
12 changed files with 1715 additions and 45 deletions
+8 -6
View File
@@ -106,9 +106,11 @@ pub struct Edge<AccountId> {
pub type PhragmenAssignment<AccountId> = (AccountId, ExtendedBalance);
/// Final result of the phragmen election.
#[cfg_attr(feature = "std", derive(Debug))]
pub struct PhragmenResult<AccountId> {
/// Just winners.
pub winners: Vec<AccountId>,
/// Just winners zipped with their approval stake. Note that the approval stake is merely the
/// sub of their received stake and could be used for very basic sorting and approval voting.
pub winners: Vec<(AccountId, ExtendedBalance)>,
/// Individual assignments. for each tuple, the first elements is a voter and the second
/// is the list of candidates that it supports.
pub assignments: Vec<(AccountId, Vec<PhragmenAssignment<AccountId>>)>
@@ -166,7 +168,7 @@ pub fn elect<AccountId, Balance, FS, C>(
<C as Convert<Balance, u64>>::convert(b) as ExtendedBalance;
// return structures
let mut elected_candidates: Vec<AccountId>;
let mut elected_candidates: Vec<(AccountId, ExtendedBalance)>;
let mut assigned: Vec<(AccountId, Vec<PhragmenAssignment<AccountId>>)>;
// used to cache and access candidates index.
@@ -282,7 +284,7 @@ pub fn elect<AccountId, Balance, FS, C>(
}
}
elected_candidates.push(winner.who.clone());
elected_candidates.push((winner.who.clone(), winner.approval_stake));
} else {
break
}
@@ -292,8 +294,8 @@ pub fn elect<AccountId, Balance, FS, C>(
for n in &mut voters {
let mut assignment = (n.who.clone(), vec![]);
for e in &mut n.edges {
if let Some(c) = elected_candidates.iter().cloned().find(|c| *c == e.who) {
if c != n.who {
if let Some(c) = elected_candidates.iter().cloned().find(|(c, _)| *c == e.who) {
if c.0 != n.who {
let ratio = {
// Full support. No need to calculate.
if *n.load == *e.load { ACCURACY }
+5 -5
View File
@@ -69,7 +69,7 @@ pub(crate) type AccountId = u64;
#[derive(Debug, Clone)]
pub(crate) struct _PhragmenResult<A: Clone> {
pub winners: Vec<A>,
pub winners: Vec<(A, Balance)>,
pub assignments: Vec<(A, Vec<_PhragmenAssignment<A>>)>
}
@@ -84,7 +84,7 @@ pub(crate) fn elect_float<A, FS>(
A: Default + Ord + Member + Copy,
for<'r> FS: Fn(&'r A) -> Balance,
{
let mut elected_candidates: Vec<A>;
let mut elected_candidates: Vec<(A, Balance)>;
let mut assigned: Vec<(A, Vec<_PhragmenAssignment<A>>)>;
let mut c_idx_cache = BTreeMap::<A, usize>::new();
let num_voters = initial_candidates.len() + initial_voters.len();
@@ -178,7 +178,7 @@ pub(crate) fn elect_float<A, FS>(
}
}
elected_candidates.push(winner.who.clone());
elected_candidates.push((winner.who.clone(), winner.approval_stake as Balance));
} else {
break
}
@@ -187,7 +187,7 @@ pub(crate) fn elect_float<A, FS>(
for n in &mut voters {
let mut assignment = (n.who.clone(), vec![]);
for e in &mut n.edges {
if let Some(c) = elected_candidates.iter().cloned().find(|c| *c == e.who) {
if let Some(c) = elected_candidates.iter().cloned().map(|(c, _)| c).find(|c| *c == e.who) {
if c != n.who {
let ratio = e.load / n.load;
assignment.1.push((e.who.clone(), ratio));
@@ -397,7 +397,7 @@ pub(crate) fn build_support_map<FS>(
let mut supports = <_SupportMap<AccountId>>::new();
result.winners
.iter()
.map(|e| (e, stake_of(e) as f64))
.map(|(e, _)| (e, stake_of(e) as f64))
.for_each(|(e, s)| {
let item = _Support { own: s, total: s, ..Default::default() };
supports.insert(e.clone(), item);
+2 -2
View File
@@ -35,7 +35,7 @@ fn float_phragmen_poc_works() {
let winners = phragmen_result.clone().winners;
let assignments = phragmen_result.clone().assignments;
assert_eq_uvec!(winners, vec![2, 3]);
assert_eq_uvec!(winners, vec![(2, 40), (3, 50)]);
assert_eq_uvec!(
assignments,
vec![
@@ -86,7 +86,7 @@ fn phragmen_poc_works() {
false,
).unwrap();
assert_eq_uvec!(winners, vec![2, 3]);
assert_eq_uvec!(winners, vec![(2, 40), (3, 50)]);
assert_eq_uvec!(
assignments,
vec![