This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Generic Normalize impl for arithmetic and npos-elections #6374
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…l-max-score-1-normalize-stuff
drahnr
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
kianenigma
commented
Jun 22, 2020
kianenigma
commented
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
gui1117
reviewed
Jun 22, 2020
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
burdges
reviewed
Jun 22, 2020
gui1117
reviewed
Jun 22, 2020
drahnr
reviewed
Jun 22, 2020
@thiolliere @drahnr Thanks for the reviews. All reviews either resolved or written down why otherwise. Please resolve the comments if you agree, else elaborate why. |
gui1117
approved these changes
Jun 23, 2020
There was a problem hiding this 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
drahnr
reviewed
Jun 24, 2020
drahnr
reviewed
Jun 24, 2020
drahnr
approved these changes
Jun 24, 2020
Co-authored-by: Bernhard Schuster <bernhard@ahoi.io>
bot merge |
Waiting for commit status... |
Merge failed - |
bot merge |
Trying merge. |
This pull request was closed.
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
C1-low
PR touches the given topic and has a low impact on builders.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context:
When converting certain types to one another, we typically lose some accuracy based on what type of
PerThing
we are using. This is normal, as these types are limited.When it comes to staking, we want to make sure that this always compensated. For example, we have an Assignment saying that someone's stake should be distributed between
[a, b, c]
by[33%, 33%, 33%]
. This is actually a plausible example, and we have to make sure we always inject a 1% into this to have it add up to 100%.Same story applies to when we are dealing with stake values. If I own 100 tokens, and my weight distribution is
[66, 33]
, then we need to inject to 1 which has been lost due to rounding errors.This is critically important for offchain phragmen as well, since validators need to compute the score offchain, which is a function of stake values, and it must be exactly the same one as computed on-chain on the validation phase, hence these rounding errors could cause a mismatch. The
submit_solution
fuzzer in staking particularly checks and catches bugs related to this.Before
We had a pitiful, minimal and potentially buggy implementation for this operation here:
substrate/primitives/npos-elections/src/lib.rs
Lines 201 to 213 in db8916a
in which we simply and blindly added all the leftovers of any accuracy loss to the last element.
Now
We do this much better, and in a generic way. See
trait Normalizable
andfn normalize()
.The implementation is not idea, but fast, and based on our need in which the errors are almost always small, it is the most sensible.
Next
This was part of a much bigger refactor that I am doing toward the second goal of #6242, but I don't want to again submit a PR with 10K additions and have it impossible to review. So this PR is generally a step toward breaking things down. Almost no logic in staking is changed here, so I see it is nicely isolated.
A follow up will further revamp
npos-elections
and add implementations for our new election algorithms.Discussion
Normalizable
for allVec<T>
whereT
is some unsigned number, and andVec<P>
whereP
is somePerThing
. But we can't do this. So I've moved theimpl for PerThing
to the macro and implement it individually for each one, i.e.Vec<Perbill>
. But this is pretty useless, sincenpos-elections
always works withP: PerThing
, not directly aPerbill
.