Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

srml-module: Phragmen election #3364

Merged
merged 55 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
8717ccb
phragmen election module.
kianenigma Aug 12, 2019
8e5d5ee
Add new files.
kianenigma Aug 12, 2019
f7d8c13
Some doc update
kianenigma Aug 12, 2019
15506ab
Merge branch 'master' of github.com:paritytech/substrate into kiz-phr…
kianenigma Aug 12, 2019
ac3078c
Update weights.
kianenigma Aug 12, 2019
edcdee1
bump and a few nits.
kianenigma Aug 12, 2019
33b9409
Performance improvement.
kianenigma Aug 13, 2019
141f6be
Merge branch 'master' of github.com:paritytech/substrate into kiz-phr…
kianenigma Aug 13, 2019
9cc03ce
Master.into()
kianenigma Aug 13, 2019
bd7c642
Remote.into()
kianenigma Aug 13, 2019
bb81b9f
Update srml/elections-phragmen/src/lib.rs
kianenigma Aug 14, 2019
b456c9b
Fix build
kianenigma Aug 15, 2019
6433a81
Some fixes.
kianenigma Aug 15, 2019
14da1e2
Master.into()
kianenigma Aug 15, 2019
3d90cd3
Fix build.
kianenigma Aug 15, 2019
88e8108
Proper outgoing and runner-up managment.
kianenigma Aug 17, 2019
cf15877
Bit more sensical weight values.
kianenigma Aug 17, 2019
0e8af21
Update srml/elections-phragmen/src/lib.rs
gavofyork Aug 22, 2019
c4cf8bc
Update srml/elections-phragmen/src/lib.rs
kianenigma Aug 22, 2019
20415ca
Update srml/elections-phragmen/src/lib.rs
kianenigma Aug 22, 2019
6e5c780
Update srml/elections-phragmen/src/lib.rs
kianenigma Aug 22, 2019
ba41fb8
Master.into()
kianenigma Aug 22, 2019
dbfaec3
fix lock file
kianenigma Aug 22, 2019
c163efe
Merge branch 'master' of github.com:paritytech/substrate into kiz-phr…
kianenigma Aug 22, 2019
12bd3c3
Fix build.
kianenigma Aug 22, 2019
aefea27
Remove runner-ups
kianenigma Aug 22, 2019
19af37b
Some refactors.
kianenigma Aug 23, 2019
325c43f
Master.into()
kianenigma Aug 29, 2019
840737c
Add support for reporting voters.
kianenigma Aug 30, 2019
e9c8e34
Fix member check.
kianenigma Aug 30, 2019
308feaf
Remove equlize.rs
kianenigma Aug 30, 2019
75d8f19
Update srml/elections-phragmen/src/lib.rs
gavofyork Sep 2, 2019
44cb54d
Update srml/elections-phragmen/src/lib.rs
gavofyork Sep 2, 2019
8ef41eb
Update srml/elections-phragmen/src/lib.rs
kianenigma Sep 5, 2019
b7a1dc8
Update srml/elections-phragmen/src/lib.rs
kianenigma Sep 5, 2019
fd1c772
Bring back runner ups.
kianenigma Sep 5, 2019
d01edec
Master.into()
kianenigma Sep 5, 2019
a8590fa
use decode_len
kianenigma Sep 5, 2019
6debacb
Better weight values.
kianenigma Sep 5, 2019
28e9372
Update bogus doc
kianenigma Sep 9, 2019
e6f001a
Merge branch 'master' of github.com:paritytech/substrate into kiz-phr…
kianenigma Sep 9, 2019
016e5ff
Bump.
kianenigma Sep 9, 2019
7e4a4ed
Update srml/elections-phragmen/src/lib.rs
kianenigma Sep 9, 2019
dec0b55
Master.into()
kianenigma Sep 12, 2019
f3f2be3
Merge branch 'kiz-phragmen-election' of github.com:paritytech/substra…
kianenigma Sep 12, 2019
b76d95f
Review comments.
kianenigma Sep 12, 2019
8f7f589
One more test
kianenigma Sep 13, 2019
ad94542
Master.into()
kianenigma Sep 14, 2019
509387e
Fix tests
kianenigma Sep 15, 2019
a6e1a3e
Fix build
kianenigma Sep 15, 2019
1adc36b
.. and fix benchmarks.
kianenigma Sep 15, 2019
0375443
Master.into()
kianenigma Sep 18, 2019
3ae3121
Update srml/elections-phragmen/src/lib.rs
gavofyork Sep 19, 2019
70ccf32
Merge remote-tracking branch 'origin/master' into kiz-phragmen-election
gavofyork Sep 19, 2019
91827db
Version bump
gavofyork Sep 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ members = [
"srml/collective",
"srml/democracy",
"srml/elections",
"srml/elections-phragmen",
"srml/example",
"srml/executive",
"srml/finality-tracker",
Expand Down
2 changes: 1 addition & 1 deletion core/phragmen/benches/phragmen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn do_phragmen(
let mut supports = <SupportMap<u64>>::new();
elected_stashes
.iter()
.map(|e| (e, to_votes(slashable_balance(e))))
.map(|(e, _)| (e, to_votes(slashable_balance(e))))
.for_each(|(e, s)| {
let item = Support { own: s, total: s, ..Default::default() };
supports.insert(e.clone(), item);
Expand Down
14 changes: 8 additions & 6 deletions core/phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>)>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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 }
Expand Down
10 changes: 5 additions & 5 deletions core/phragmen/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>)>
}

Expand All @@ -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();
Expand Down Expand Up @@ -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
}
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions core/phragmen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down Expand Up @@ -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![
Expand Down
4 changes: 2 additions & 2 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to equal spec_version. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 158,
impl_version: 160,
spec_version: 159,
impl_version: 159,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
34 changes: 34 additions & 0 deletions srml/elections-phragmen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "srml-elections-phragmen"
version = "2.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
serde = { version = "1.0", optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
runtime_io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
phragmen = { package = "substrate-phragmen", path = "../../core/phragmen", default-features = false }
srml-support = { path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }

[dev-dependencies]
hex-literal = "0.2.0"
balances = { package = "srml-balances", path = "../balances" }

[features]
default = ["std"]
std = [
"codec/std",
"primitives/std",
"serde",
"runtime_io/std",
"srml-support/std",
"sr-primitives/std",
"phragmen/std",
"system/std",
"rstd/std",
]
Loading