#tetcoin #election #algorithm #score #solution #balancing #ensure

no-std tp-npos-elections

Tetcore NPoS election algorithm primitives

Show the crate…

2 stable releases

2.0.2 Apr 3, 2021
2.0.1 Apr 2, 2021

#11 in #tetcoin

Download history 59/week @ 2024-04-18 46/week @ 2024-04-25 35/week @ 2024-05-02 37/week @ 2024-05-09 37/week @ 2024-05-16 33/week @ 2024-05-23 40/week @ 2024-05-30 29/week @ 2024-06-06 31/week @ 2024-06-13 44/week @ 2024-06-20 21/week @ 2024-06-27 10/week @ 2024-07-04 36/week @ 2024-07-11 41/week @ 2024-07-18 32/week @ 2024-07-25 28/week @ 2024-08-01

145 downloads per month
Used in 16 crates (3 directly)

Apache-2.0

550KB
12K SLoC

A set of election algorithms to be used with a tetcore runtime, typically within the staking sub-system. Notable implementation include:

  • 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 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 a solution toward being more balances, which in turn can increase its score.

Terminology

This crate uses context-independent words, not to be confused with staking. This is because the election algorithms of this crate, while designed for staking, can be used in other contexts as well.

Voter: The entity casting some votes to a number of Targets. This is the same as Nominator in the context of staking. Target: The entities eligible to be voted upon. This is the same as Validator in the context of staking. Edge: A mapping from a Voter to a Target.

The goal of an election algorithm is to provide an ElectionResult. A data composed of:

  • winners: A flat list of identifiers belonging to those who have won the election, usually ordered in some meaningful way. They are zipped with their total backing stake.
  • assignment: A mapping from each voter to their winner-only targets, zipped with a ration denoting the amount of support given to that particular target.
// the winners.
let winners = vec![(1, 100), (2, 50)];
let assignments = vec![
    // A voter, giving equal backing to both 1 and 2.
    Assignment {
		who: 10,
		distribution: vec![(1, Perbill::from_percent(50)), (2, Perbill::from_percent(50))],
	},
    // A voter, Only backing 1.
    Assignment { who: 20, distribution: vec![(1, Perbill::from_percent(100))] },
];

// the combination of the two makes the election result.
let election_result = ElectionResult { winners, assignments };

The Assignment field of the election result is voter-major, i.e. it is from the perspective of the voter. The struct that represents the opposite is called a Support. This struct is usually accessed in a map-like manner, i.e. keyed vy voters, therefor it is stored as a mapping called SupportMap.

Moreover, the support is built from absolute backing values, not ratios like the example above. A struct similar to Assignment that has stake value instead of ratios is called an StakedAssignment.

More information can be found at: https://arxiv.org/abs/2004.12990

License: Apache-2.0

Dependencies

~3–12MB
~122K SLoC