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

Conversation

@kianenigma
Copy link
Contributor

This code was dead for a while and needed a shake.

  • The API between phragmen and callers is now standardised as: any crate calling into phragmen must pass in vote weights (aka stakes) as u64 (potentially via a T::CurrencyToVote). phragmen then converts this value to u128 and performs the calculations safely. This type is named VoteWeight. Consequently, we don't pass around some T::CurrencyToVote everywhere.
  • More closures are removed from the API. Yet, I am not yet sure if in the long run it is desirable. I did some benchmarking and the overhead of the function call in the closure is pretty much negligible. For now we avoid them since Sensible way of selecting Prime member #5346 removed one. In the case of Sensible way of selecting Prime member #5346 it is obviously easier to pass the stakes zipped with the voters. But for example the staking code gets slightly more complicated if you want to do that since stakes are stored elsewhere in storage and passing just one closure down the line is easier. Moreover, the closure can mimic the function of am in-memory cache and not even go to overlay in every call. Some operations related to compact still use closures.
  • Added fuzzing to ensure that equalise() always gives a better solution.

@kianenigma kianenigma added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes labels Mar 30, 2020
@kianenigma kianenigma requested a review from gavofyork March 30, 2020 07:29
@gavofyork
Copy link
Member

polkadot apparently doesn't build.

@kianenigma
Copy link
Contributor Author

all ✅

@gavofyork gavofyork added A6-mustntgrumble and removed A0-please_review Pull request needs code review. A6-mustntgrumble labels Apr 13, 2020
0,
candidates,
voters_and_votes.clone(),
voters_and_votes.iter().cloned().map(|(v, s, t)| (v, to_votes(s), t)).collect::<Vec<_>>(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gavofyork if it is okay to move your prime election code to use u64 (VoteWeight) instead of BalanceOf<T> then this extra clone of all voters can be removed.

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've changed it for now, correct me if it is wrong.

@kianenigma kianenigma added the A0-please_review Pull request needs code review. label Apr 14, 2020
Comment on lines 84 to 91
let to_range = |x: usize, a: usize, b: usize| {
let collapsed = x % b;
if collapsed >= a {
collapsed
} else {
collapsed + a
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

the result can be more than b if b < a*2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah true, this is slightly wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for now since all ranges are bigger than the mentioned range, I'll add an assertion to prevent this. I'd be interested to devise a minimum, correct and fair algorithm for this though.

target_count = to_range(target_count, 50, 2000);
voter_count = to_range(voter_count, 50, 1000);
iterations = to_range(iterations, 1, 20);
to_elect = to_range(to_elect, 50, target_count);
Copy link
Contributor

@gui1117 gui1117 Apr 15, 2020

Choose a reason for hiding this comment

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

this to_range can result in to_elect > target_count, is that wanted?

(due to #5452 (comment))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, this should not happen and can cause false negatives, temporarily fixed.

Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

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

Looks good to me, I didn't read deeply bench nor fuzzer, but everything else

@kianenigma
Copy link
Contributor Author

I've no idea what this polkadot build error is.

@gavofyork gavofyork merged commit bfd4479 into master Apr 17, 2020
@gavofyork gavofyork deleted the kiz-clean-equalise-2 branch April 17, 2020 07:53
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. B0-silent Changes should not be mentioned in any release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants