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

Generalizes the elections-phragmen pallet to use an ElectionProvider #12563

Closed
wants to merge 7 commits into from

Conversation

gpestana
Copy link
Contributor

@gpestana gpestana commented Oct 25, 2022

This PR refactors the current elections-phragmen pallet to use an election provider instead of hardcoded phragmen election logic. The pallet itself implements the DataProvider used by the election provider.

WIP, todo:

  • rename the pallet to pallet-elections
  • review/fix weights

Closes #8250

@gpestana gpestana self-assigned this Oct 25, 2022
@gpestana gpestana marked this pull request as draft October 25, 2022 12:56
@github-actions github-actions bot added the A0-please_review Pull request needs code review. label Oct 25, 2022
Self::register_weight(T::WeightInfo::electable_candidates(targets.len() as u32));

if let Some(max_candidates) = maybe_max_len {
// TODO(gpestana):: or should return Err instead?
Copy link
Contributor Author

@gpestana gpestana Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return an error here or silently truncate the list of eligible candidates?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should maintain whatever was the current behavior for now, unless if we can improve it without breaking our backs.

},
}

Self::register_weight(T::WeightInfo::electing_voters(voters_and_stakes.len() as u32));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add the self-weighing logic in DataProvider::elect() only and reuse most of the weighting logic of the former do_phragmen(). I think that should be OK since I would expect the DataProvider to be used only by the election provider. OTOH, if the developers call the DataProvider explicitly for some reason, if the weights are implicit under elect() we may get into inconsistent weights.


let _ = T::ElectionProvider::elect().map(|mut winners| {
// sort winners by stake
winners.sort_by(|i, j| j.1.total.cmp(&i.1.total));
Copy link
Contributor Author

@gpestana gpestana Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of explicitly sorting the winners by stake for every election, should we have a SortedListProvider? If not, should the sorting be done in the ElectionDataProvider?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately nothing in the new interface requires this to be sorted. the old interface was sorted though: https://paritytech.github.io/substrate/master/sp_npos_elections/phragmen/fn.seq_phragmen.html

I think using Solver might help with this as well, other than simplifying a few other things.

) -> frame_election_provider_support::data_provider::Result<Vec<Self::AccountId>> {
let mut candidates_and_deposit = Self::candidates();
// add all the previous members and runners-up as candidates as well.
candidates_and_deposit.append(&mut Self::implicit_candidates_with_deposit());
Copy link
Contributor

@kianenigma kianenigma Oct 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is implicit_candidates_with_deposit here? I don't see this function being defined anywhere on Self.

I recall now that all candidates are considered to be voters, and now since they are two distinct functions, we would need to sync this somehow, or re-read them etc.

I am more and more leaning toward type Solver: NPoSSolver was a better approach :(

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having seen a full draft now, I think we should say goodbye to ElectionProvider and use the more low level NPoSSolver here.

Reduces the diff, weighing will be simpler etc.

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
@gpestana
Copy link
Contributor Author

Having seen a full draft now, I think we should say goodbye to ElectionProvider and use the more low level NPoSSolver here.

Reduces the diff, weighing will be simpler etc.

Yes, I agree. I will close this PR and open a new one using the NposSolver abstraction, it should be substantially simpler.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use ElectionProvider for pallet-elections-phragmen
2 participants