feat: staking payout refactor
fixes KILTProtocol/ticket#1930
Follow-up PRs
- #413
- #414
TLDR Description
- Refactors staking payouts to not happen automatically
- Instead, a counter (similar to the Substrate staking pallet participation points) is incremented for each authored block
- The counter can be consumed and converted into rewards
- Adds extrinsic
claim_rewardswhich enables any signed origin to trigger a payout for themselves
Lengthy Description
The proposed solution goes more into the direction of Substrate staking. Every time a collator authors a block, their rewards counter is incremented. The counter can be consumed to increment accumulated rewards. The rewards need to be actively claimed by the new claim_rewards_for extrinsic.
There are a lot of events which automatically consume the RewardCounter and increment the accumulated rewards since the last actively requested payout:
- Collator changes stake (more, less)
- A delegator changes stake (more, less)
- A delegator removes delegation
- A delegator is replaced
- A collator leaves
- The inflation configuration changes
Background
The main reason for this refactor is that the current solution is not highly scalable as it inherently emits a Rewarded event for each address that gets rewarded. For the current configuration of delegators, this is rather unproblematic.
Moreover, we currently enforce the taxable Rewarded event onto all stakers and it occurs very frequently. Therefore, it is quite a challenge to summarize all such events.
TODO
- [x] More thorough description
- [x] Add claiming extrinsic
- [x] Refactor yearly inflation adjustment
- [x] Unit tests
- [x] Benchmarks
Extrinsics
- [x] set_inflation
- [x] force_remove_candidate
- [x] init_leave_candidates
- [x] candidate_stake_more
- [x] candidate_stake_less
- [x] join_delegators
- [x] delegate_another_candidate
- [x] leave_delegators
- [x] revoke_delegation
- [x] delegator_stake_more
- [x] delegator_stake_less
Checklist:
- [x] I have verified that the code works
- [x] No panics! (checked arithmetic ops, no indexing
array[3]useget(3), ...)
- [x] No panics! (checked arithmetic ops, no indexing
- [x] I have verified that the code is easy to understand
- [ ] If not, I have left a well-balanced amount of inline comments
- [x] I have left the code in a better state
- [x] I have documented the changes (where applicable)
@weichweich @ntn-x2 Could you review my additions of runtime API calls (6f73296, 20fd523 and 0f8160b) when you have the time. Thank you!
I also made claiming stricter. ~IMO, this PR is done now.~ Just realized that I haven't written any migrations yet.
@weichweich Please review when you have the time, thanks!