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

[NPoS] Implements dynamic number of nominators - #12970

Merged
paritytech-processbot[bot] merged 96 commits into
masterfrom
gpestana/staking-dynamic-nominators
Aug 10, 2023
Merged

[NPoS] Implements dynamic number of nominators#12970
paritytech-processbot[bot] merged 96 commits into
masterfrom
gpestana/staking-dynamic-nominators

Conversation

@gpestana

@gpestanagpestana commented Dec 19, 2022

Copy link
Copy Markdown
Contributor

This PR refactors the implementation of the ElectionDataProvider bounds and implements a dynamic nomination quota per voter, based on the account's balance and an arbitrary curve.

Instead of a fixed MAX_NOMINATIONS per nominator, the ElectionDataProvider uses an implementor of trait NominationsQuota<Balance> to calculate the max votes per nominator. Since the number of votes per nominator is dynamic, the election data provider (pallet staking) keeps track of the size of the snapshot to ensure that the final snapshot size is bounded, even when the number of votes per voter is calculated on the fly. This PR implements an ElectionSizeTracker to achieve this goal.

The FixedNominationsQuota<const MAX: u32> struct implements the nomination quota trait and can be used to set the maximum number of nominations per nominator to 16, which is the current behaviour.

Lazy nomination quota checks: Note that if a nominator's quota decreases after the nomination has been done, all the nominations will be used. The nominations quota limits is only enacted when an account performs a new nomination, in which case, if the number of nomination exceeds the current nominator quota, the fn nominate extrinsic fails with TooManyTargets.

Notable changes:

Trait NominationsQuota

An implementor of NominationsQuota returns the maximum number of votes per nominator. FixedNominationsQuota implements an instance of a fixed number of nominations, similar to the current implementation when set to 16.

ElectionsBounds and DataProviderBounds

The MaxElectableTargets and TargetsBound are replaced by ElectionBounds. ElectionsBounds defines the count and size limits of both voters and targets for an election.

The struct ElectionBounds sets the upper bounds in size (MB) and number of voters/targets to request from the election data provider. The bounds are defined over two axis: number of elements and size (in MB) of the snapshot that is built from the election result.

Struct ElectionSizeTracker

Keeps track of the amount of data (in size and number of elements) that an election data provider prepares in get_npos_voters and get_npos_targets. The election data provider uses an instance of ElectionSizeTracker to know when the number of voters exceeded the bounds in terms of size and return the election data to EPM.

Changes to the ElectionDataProvider interface

Both fn ElectionDataProvider::election_voters and fn ElectionDataProvider::electable_targets take as an input an instance of DataProviderBounds, instead of an optional max in the of number of returned elements.

New events

  • SnapshotVotersSizeExceeded: emitted when the election snapshot was exhausted due to the voters size limit.
  • SnapshotTargetsSizeExceeded: emitted when the election snapshot was exhausted due to the targets size limit.

Runtime API

This PR also adds a new runtime API to allow clients to query the nominations quota for a given balance.


polkadot companion: paritytech/polkadot#6807

Related to and reviving this PR and discussion #10340

Helps #13069
Closes#12968

@gpestanagpestana added A3-in_progress Pull request is in progress. No review needed at this stage. B3-apinoteworthy C1-low PR touches the given topic and has a low impact on builders. D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Dec 19, 2022
@gpestanagpestana self-assigned this Dec 19, 2022
@gpestana
gpestana marked this pull request as draft December 19, 2022 11:01
@github-actionsgithub-actionsBot added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Dec 19, 2022
@gpestanagpestana changed the title Allow for a dynamic number of nominatorsAllow for a dynamic number of nominators (v2)Dec 19, 2022
Comment threadframe/election-provider-multi-phase/src/lib.rs Outdated
Comment threadframe/election-provider-support/src/lib.rs Outdated
Comment threadframe/election-provider-support/src/lib.rs Outdated
Comment threadframe/election-provider-multi-phase/src/lib.rs Outdated

@kianenigmakianenigma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks in the right direction so far.

@gpestana
gpestana marked this pull request as ready for review December 27, 2022 21:29
@jakoblelljakoblell added D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Aug 3, 2023
@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot fmt

@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot rebase

1 similar comment
@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot rebase

@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot fmt

@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot clean

@command-botcommand-botBot deleted a comment from paritytech-processbotBotAug 8, 2023
@command-botcommand-botBot deleted a comment from paritytech-processbotBotAug 8, 2023
@command-botcommand-botBot deleted a comment from paritytech-processbotBotAug 8, 2023
@command-botcommand-botBot deleted a comment from paritytech-processbotBotAug 8, 2023
@command-botcommand-botBot deleted a comment from paritytech-processbotBotAug 8, 2023
@command-botcommand-botBot deleted a comment from paritytech-processbotBotAug 8, 2023
@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot rebase

@paritytech-processbot

Copy link
Copy Markdown

Rebased

@gpestana

Copy link
Copy Markdown
ContributorAuthor

bot merge

@paritytech-processbot
paritytech-processbotBot merged commit 57d2e53 into masterAug 10, 2023
@paritytech-processbot
paritytech-processbotBot deleted the gpestana/staking-dynamic-nominators branch August 10, 2023 07:45

#[test]
fn change_of_max_nominations() {
#[should_panic]

@arkpararkparAug 12, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this intended? If so, Is this test supposed to panic at the last assertion?

@arkpar

Copy link
Copy Markdown
Member

cargo test --release -p pallet-staking fails for me after this PR. Apparently some tests are supposed to panic with debug_assert which only works in debug config.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A0-please_reviewPull request needs code review.B1-note_worthyChanges should be noted in the release notesC1-lowPR touches the given topic and has a low impact on builders.D1-audited 👍PR contains changes to fund-managing logic that has been properly reviewed and externally auditedT1-runtimeThis PR/Issue is related to the topic “runtime”.

Projects

Status: ✅ Done
Status: Done
Archived in project

Development

Successfully merging this pull request may close these issues.

Allow for a dynamic number of nominators

20 participants

@gpestana@rossbulat@Polkadot-Forum@FatemeShirazi@arkpar@ruseinov@sam0x17@kianenigma@Ank4n@jakoblell@the-right-joyce@ggwpez@davxy@yjhmelody@koute@dmitry-markin@lazam@andresilva@pmikolajczyk41@acatangiu