Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2617,8 +2617,8 @@ impl<T: Trait> Module<T> {
// write new results.
<QueuedElected<T>>::put(ElectionResult {
elected_stashes: winners,
compute,
exposures,
compute,
});
QueuedScore::put(submitted_score);

Expand Down
39 changes: 30 additions & 9 deletions primitives/npos-elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
//! - [`seq_phragmen`]: Implements the Phragmén Sequential Method. An un-ranked, relatively fast
//! election method that ensures PJR, but does not provide a constant factor approximation of the
//! maximin problem.
//! - [`phragmms`]: Implements a hybrid approach inspired by Phragmén which is executed faster but
//! - [`phragmms`](phragmms::phragmms): Implements a hybrid approach inspired by Phragmén which is executed faster but
//! it can achieve a constant factor approximation of the maximin problem, similar to that of the
//! MMS algorithm.
//! - [`balance_solution`]: Implements the star balancing algorithm. This iterative process can push
//! - [`balance`](balancing::balance): Implements the star balancing algorithm. This iterative process can push
//! a solution toward being more `balances`, which in turn can increase its score.
//!
//! ### Terminology
Expand Down Expand Up @@ -92,18 +92,20 @@ mod mock;
#[cfg(test)]
mod tests;

mod phragmen;
mod balancing;
mod phragmms;
mod node;
mod reduce;
mod helpers;
pub mod phragmen;
pub mod balancing;
pub mod phragmms;
pub mod node;
pub mod reduce;
pub mod helpers;
pub mod pjr;

pub use reduce::reduce;
pub use helpers::*;
pub use phragmen::*;
pub use phragmms::*;
pub use balancing::*;
pub use pjr::*;

// re-export the compact macro, with the dependencies of the macro.
#[doc(hidden)]
Expand Down Expand Up @@ -189,6 +191,12 @@ pub struct Candidate<AccountId> {
round: usize,
}

impl<AccountId> Candidate<AccountId> {
pub fn to_ptr(self) -> CandidatePtr<AccountId> {
Rc::new(RefCell::new(self))
}
}

/// A vote being casted by a [`Voter`] to a [`Candidate`] is an `Edge`.
#[derive(Clone, Default)]
pub struct Edge<AccountId> {
Expand Down Expand Up @@ -233,6 +241,19 @@ impl<A: IdentifierT> std::fmt::Debug for Voter<A> {
}

impl<AccountId: IdentifierT> Voter<AccountId> {
/// Create a new `Voter`.
pub fn new(who: AccountId) -> Self {
Self { who, ..Default::default() }
}


/// Returns `true` if `self` votes for `target`.
///
/// Note that this does not take into account if `target` is elected (i.e. is *active*) or not.
pub fn votes_for(&self, target: &AccountId) -> bool {
self.edges.iter().any(|e| &e.who == target)
}

/// Returns none if this voter does not have any non-zero distributions.
///
/// Note that this might create _un-normalized_ assignments, due to accuracy loss of `P`. Call
Expand Down Expand Up @@ -625,7 +646,7 @@ pub(crate) fn setup_inputs<AccountId: IdentifierT>(
.enumerate()
.map(|(idx, who)| {
c_idx_cache.insert(who.clone(), idx);
Rc::new(RefCell::new(Candidate { who, ..Default::default() }))
Candidate { who, ..Default::default() }.to_ptr()
})
.collect::<Vec<CandidatePtr<AccountId>>>();

Expand Down
Loading