Skip to content

[NPoS] Paging reward payouts in order to scale rewardable nominators #1189

Merged
Ank4n merged 23 commits intomasterfrom
ankan/paged-rewards
Nov 1, 2023
Merged

[NPoS] Paging reward payouts in order to scale rewardable nominators #1189
Ank4n merged 23 commits intomasterfrom
ankan/paged-rewards

Conversation

@Ank4n
Copy link
Copy Markdown
Contributor

@Ank4n Ank4n commented Aug 26, 2023

helps #439.
closes #473.

PR link in the older substrate repository: paritytech/substrate#13498.

Context

Rewards payout is processed today in a single block and limited to MaxNominatorRewardedPerValidator. This number is currently 512 on both Kusama and Polkadot.

This PR tries to scale the nominators payout to an unlimited count in a multi-block fashion. Exposures are stored in pages, with each page capped to a certain number (MaxExposurePageSize). Starting out, this number would be the same as MaxNominatorRewardedPerValidator, but eventually, this number can be lowered through new runtime upgrades to limit the rewardeable nominators per dispatched call instruction.

The changes in the PR are backward compatible.

How payouts would work like after this change

Staking exposes two calls, 1) the existing payout_stakers and 2) payout_stakers_by_page.

payout_stakers

This remains backward compatible with no signature change. If for a given era a validator has multiple pages, they can call payout_stakers multiple times. The pages are executed in an ascending sequence and the runtime takes care of preventing double claims.

payout_stakers_by_page

Very similar to payout_stakers but also accepts an extra param page_index. An account can choose to payout rewards only for an explicitly passed page_index.

Lets look at an example scenario
Given an active validator on Kusama had 1100 nominators, MaxExposurePageSize set to 512 for Era e. In order to pay out rewards to all nominators, the caller would need to call payout_stakers 3 times.

  • payout_stakers(origin, stash, e) => will pay the first 512 nominators.
  • payout_stakers(origin, stash, e) => will pay the second set of 512 nominators.
  • payout_stakers(origin, stash, e) => will pay the last set of 76 nominators.
    ...
  • payout_stakers(origin, stash, e) => calling it the 4th time would return an error InvalidPage.

The above calls can also be replaced by payout_stakers_by_page and passing a page_index explicitly.

Commission note

Validator commission is paid out in chunks across all the pages where each commission chunk is proportional to the total stake of the current page. This implies higher the total stake of a page, higher will be the commission. If all the pages of a validator's single era are paid out, the sum of commission paid to the validator across all pages should be equal to what the commission would have been if we had a non-paged exposure.

Migration Note

Strictly speaking, we did not need to bump our storage version since there is no migration of storage in this PR. But it is still useful to mark a storage upgrade for the following reasons:

  • New storage items are introduced in this PR while some older storage items are deprecated.
  • For the next HistoryDepth eras, the exposure would be incrementally migrated to its corresponding paged storage item.
  • Runtimes using staking pallet would strictly need to wait at least HistoryDepth eras with current upgraded version (14) for the migration to complete. At some era E such that E > era_at_which_V14_gets_into_effect + HistoryDepth, we will upgrade to version X which will remove the deprecated storage items.
    In other words, it is a strict requirement that Ex - E14 > HistoryDepth, where
    Ex = Era at which deprecated storages are removed from runtime,
    E14 = Era at which runtime is upgraded to version 14.
  • For Polkadot and Kusama, there is a tracker ticket to clean up the deprecated storage items.

Storage Changes

Added

  • ErasStakersOverview
  • ClaimedRewards
  • ErasStakersPaged

Deprecated

The following can be cleaned up after 84 eras which is tracked here.

  • ErasStakers.
  • ErasStakersClipped.
  • StakingLedger.claimed_rewards, renamed to StakingLedger.legacy_claimed_rewards.

Config Changes

  • Renamed MaxNominatorRewardedPerValidator to MaxExposurePageSize.

TODO

  • Tracker ticket for cleaning up the old code after 84 eras.
  • Add companion.
  • Redo benchmarks before merge.
  • Add Changelog for pallet_staking.
  • Pallet should be configurable to enable/disable paged rewards.
  • Commission payouts are distributed across pages.
  • Review documentation thoroughly.
  • Rename MaxNominatorRewardedPerValidator -> MaxExposurePageSize.
  • NMap for ErasStakersPaged.
  • Deprecate ErasStakers.
  • Integrity tests.

Followup issues

Runtime api for deprecated ErasStakers storage item

@Ank4n Ank4n self-assigned this Aug 26, 2023
@paritytech-ci paritytech-ci requested review from a team August 26, 2023 18:28
@Ank4n Ank4n added A3-backport Pull request is already reviewed well in another branch. D2-substantial Can be fixed by an experienced coder with a working knowledge of the codebase. labels Aug 26, 2023
@Ank4n Ank4n requested review from a team, gpestana, kianenigma and rossbulat August 26, 2023 18:29
/// Returns the number of pages of exposure a validator has for the given era.
///
/// For eras where paged exposure does not exist, this returns 1 to keep backward compatibility.
pub(crate) fn get_page_count(era: EraIndex, validator: &T::AccountId) -> PageIndex {
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.

This shouldn't return PageIndex, just usize

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Its a good point and I have thought about it too. But isn't page count always going to have the same type as PageIndex?

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.

Yeah it is, it works perfectly fine and will probably never change, it's just a minor naming thing. By reading PageIndex, you would expect you get back an actual page index, not the number of pages. Even though they are both numbers, it's better to be expressive with type names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah agree about the readability but this is stored in storage and we can't use usize for that, so am keeping it as same type as PageIndex. It also is used to compare against PageIndex so it makes sense to have them as same type.

What do you think about renaming the type to just call it Page?

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.

You can still use u32, which is the type aliased to PageIndex, that'd be better in my opinion

/// Wrapper struct for Era related information. It is not a pure encapsulation as these storage
/// items can be accessed directly but nevertheless, its recommended to use `EraInfo` where we
/// can and add more functions to it as needed.
pub(crate) struct EraInfo<T>(sp_std::marker::PhantomData<T>);
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.

This could go on another file so as to not keep on bloating the lib.rs

@Ank4n Ank4n added T1-FRAME This PR/Issue is related to core FRAME, the framework. T8-polkadot This PR/Issue is related to/affects the Polkadot network. labels Sep 6, 2023

type MaxAuthorities = MaxAuthorities;
type MaxNominators = MaxNominatorRewardedPerValidator;
type MaxNominators = MaxExposurePageSize;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Isn't this a pretty bad name? I belive what pallet_babe wants to convey here is MaxNominatorsPerValidator?

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.

Also, indeed, the correct value for this should be something like max_pages * page_size, but I trust that this will come in a follow-up.

Copy link
Copy Markdown
Contributor Author

@Ank4n Ank4n Sep 6, 2023

Choose a reason for hiding this comment

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

Its terrible but its just more apparent now :(. MaxNominatorRewardedPerValidator was also wrong and exactly same thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will add a comment about this in the code. Not sure what else I can do in the scope of current PR.

type SessionInterface = Self;
type UnixTime = pallet_timestamp::Pallet<Test>;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
type MaxNominatorRewardedPerValidator = ConstU32<64>;
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.

can't wait to use derive_impl for staking as well, it is one of those pallet that is used in a couple of mocks.

bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 9, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 9, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 9, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 9, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 10, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 10, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 10, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 10, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 10, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 11, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 12, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 12, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 22, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 23, 2024
claravanstaden pushed a commit to Snowfork/runtimes that referenced this pull request Feb 5, 2024
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
…aritytech#1189)

helps paritytech#439.
closes paritytech#473.

PR link in the older substrate repository:
paritytech/substrate#13498.

# Context
Rewards payout is processed today in a single block and limited to
`MaxNominatorRewardedPerValidator`. This number is currently 512 on both
Kusama and Polkadot.

This PR tries to scale the nominators payout to an unlimited count in a
multi-block fashion. Exposures are stored in pages, with each page
capped to a certain number (`MaxExposurePageSize`). Starting out, this
number would be the same as `MaxNominatorRewardedPerValidator`, but
eventually, this number can be lowered through new runtime upgrades to
limit the rewardeable nominators per dispatched call instruction.

The changes in the PR are backward compatible.

## How payouts would work like after this change
Staking exposes two calls, 1) the existing `payout_stakers` and 2)
`payout_stakers_by_page`.

### payout_stakers
This remains backward compatible with no signature change. If for a
given era a validator has multiple pages, they can call `payout_stakers`
multiple times. The pages are executed in an ascending sequence and the
runtime takes care of preventing double claims.

### payout_stakers_by_page
Very similar to `payout_stakers` but also accepts an extra param
`page_index`. An account can choose to payout rewards only for an
explicitly passed `page_index`.

**Lets look at an example scenario**
Given an active validator on Kusama had 1100 nominators,
`MaxExposurePageSize` set to 512 for Era e. In order to pay out rewards
to all nominators, the caller would need to call `payout_stakers` 3
times.

- `payout_stakers(origin, stash, e)` => will pay the first 512
nominators.
- `payout_stakers(origin, stash, e)` => will pay the second set of 512
nominators.
- `payout_stakers(origin, stash, e)` => will pay the last set of 76
nominators.
...
- `payout_stakers(origin, stash, e)` => calling it the 4th time would
return an error `InvalidPage`.

The above calls can also be replaced by `payout_stakers_by_page` and
passing a `page_index` explicitly.

## Commission note
Validator commission is paid out in chunks across all the pages where
each commission chunk is proportional to the total stake of the current
page. This implies higher the total stake of a page, higher will be the
commission. If all the pages of a validator's single era are paid out,
the sum of commission paid to the validator across all pages should be
equal to what the commission would have been if we had a non-paged
exposure.

### Migration Note
Strictly speaking, we did not need to bump our storage version since
there is no migration of storage in this PR. But it is still useful to
mark a storage upgrade for the following reasons:

- New storage items are introduced in this PR while some older storage
items are deprecated.
- For the next `HistoryDepth` eras, the exposure would be incrementally
migrated to its corresponding paged storage item.
- Runtimes using staking pallet would strictly need to wait at least
`HistoryDepth` eras with current upgraded version (14) for the migration
to complete. At some era `E` such that `E >
era_at_which_V14_gets_into_effect + HistoryDepth`, we will upgrade to
version X which will remove the deprecated storage items.
In other words, it is a strict requirement that E<sub>x</sub> -
E<sub>14</sub> > `HistoryDepth`, where
E<sub>x</sub> = Era at which deprecated storages are removed from
runtime,
E<sub>14</sub> = Era at which runtime is upgraded to version 14.
- For Polkadot and Kusama, there is a [tracker
ticket](paritytech#433) to clean
up the deprecated storage items.

### Storage Changes

#### Added
- ErasStakersOverview
- ClaimedRewards
- ErasStakersPaged

#### Deprecated
The following can be cleaned up after 84 eras which is tracked
[here](paritytech#433).

- ErasStakers.
- ErasStakersClipped.
- StakingLedger.claimed_rewards, renamed to
StakingLedger.legacy_claimed_rewards.

### Config Changes
- Renamed MaxNominatorRewardedPerValidator to MaxExposurePageSize.

### TODO
- [x] Tracker ticket for cleaning up the old code after 84 eras.
- [x] Add companion.
- [x] Redo benchmarks before merge.
- [x] Add Changelog for pallet_staking.
- [x] Pallet should be configurable to enable/disable paged rewards.
- [x] Commission payouts are distributed across pages.
- [x] Review documentation thoroughly.
- [x] Rename `MaxNominatorRewardedPerValidator` ->
`MaxExposurePageSize`.
- [x] NMap for `ErasStakersPaged`.
- [x] Deprecate ErasStakers.
- [x] Integrity tests.

### Followup issues
[Runtime api for deprecated ErasStakers storage
item](paritytech#426)

---------

Co-authored-by: Javier Viola <[email protected]>
Co-authored-by: Ross Bulat <[email protected]>
Co-authored-by: command-bot <>
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
…aritytech#2369)

Addresses a bug caused by
paritytech#1189. The changes are
still not released yet, so would like to push the fix soon so it can go
together with the release of the above PR.

`fast_unstake` checks if a staker is exposed in an era. However, this fn
is still returning whether the staker is exposed based on the old
storage item. This PR fixes that by looking in both old and new exposure
storages.

Also adds some integrity tests for paged exposures.
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
…tytech#2459)

The FastUnstake pallet tests were previously directly modifying storage
items in the pallet-staking to alter exposure. However, due to the
introduction of the [new paged exposure
feature](paritytech#1189) these
tests were not testing against correct storage items. This issue
resulted in a bug that I didn't catch, which has been addressed in [this
fix](paritytech#2369).

This PR introduces a modification to how the pallet-fast-unstake handles
exposure. It now utilizes `pallet-staking::EraInfo` to set or mutate
Exposures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A3-backport Pull request is already reviewed well in another branch. D2-substantial Can be fixed by an experienced coder with a working knowledge of the codebase. T1-FRAME This PR/Issue is related to core FRAME, the framework. T8-polkadot This PR/Issue is related to/affects the Polkadot network.

Projects

Status: ✅ Done
Status: Audited

Development

Successfully merging this pull request may close these issues.

Consolidate Nominator Support in Slashing and Rewards

8 participants