pallet_staking_async/lib.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Staking Async Pallet
19//!
20//! This pallet is a fork of the original `pallet-staking`, with a number of key differences:
21//!
22//! * It no longer has access to a secure timestamp, previously used to calculate the duration of an
23//! era.
24//! * It no longer has access to a pallet-session.
25//! * It no longer has access to a pallet-authorship.
26//! * It is capable of working with a multi-page `ElectionProvider``, aka.
27//! `pallet-election-provider-multi-block`.
28//!
29//! While `pallet-staking` was somewhat general-purpose, this pallet is absolutely NOT right from
30//! the get-go: It is designed to be used ONLY in Polkadot/Kusama AssetHub system parachains.
31//!
32//! ## Reward and Inflation
33//!
34//! This pallet supports two reward modes, controlled by [`Config::DisableMinting`]:
35//!
36//! ### Non-minting mode (`DisableMinting = true`)
37//!
38//! Staking does **not** mint tokens. It expects an external source (e.g. `pallet-dap`) to
39//! fund the general staker reward pot ([`PotAccountProvider`]). At each era boundary,
40//! staking snapshots the accumulated balance into an era-specific pot via
41//! [`EraRewardManager`](reward::EraRewardManager). Payouts transfer from the era pot.
42//!
43//! Unclaimed rewards from expired eras (past `HistoryDepth`) are withdrawn and passed to
44//! [`Config::UnclaimedRewardHandler`].
45//!
46//! [`DisableMintingGuard`] is set on the first successful snapshot as a safety net — it
47//! prevents the payout side from falling back to legacy minting for eras that should have
48//! reward pots.
49//!
50//! ### Legacy minting mode (`DisableMinting = false`)
51//!
52//! At era boundary, [`Config::EraPayout`] computes inflation based on `total_staked`,
53//! `total_issuance`, and era duration. Tokens are minted on-the-fly during `payout_stakers`.
54//! The treasury remainder is sent to [`Config::RewardRemainder`]. [`MaxStakedRewards`] can
55//! cap the staker portion. [`Config::MaxEraDuration`] caps the effective era duration.
56//!
57//! This mode is kept for Kusama/non-polkadot runtime's compatibility where inflation depends on the
58//! staking ratio.
59//!
60//! ### Switching modes
61//!
62//! Switching from legacy to non-minting is a one-way migration. Once `DisableMinting` is
63//! set to `true`, it must **never** be switched back — eras created in non-minting mode
64//! have funded reward pots, and switching to legacy would orphan those pots and cause
65//! double-minting.
66//!
67//! ## Slashing Pipeline and Withdrawal Restrictions
68//!
69//! This pallet implements a robust slashing mechanism that ensures the integrity of the staking
70//! system while preventing stakers from withdrawing funds that might still be subject to slashing.
71//!
72//! ### Overview of the Slashing Pipeline
73//!
74//! The slashing process consists of multiple phases:
75//!
76//! 1. **Offence Reporting**: Offences are reported from the relay chain through `on_new_offences`
77//! 2. **Queuing**: Valid offences are added to the `OffenceQueue` for processing
78//! 3. **Processing**: Offences are processed incrementally over multiple blocks
79//! 4. **Application**: Slashes are either applied immediately or deferred based on configuration
80//!
81//! ### Phase 1: Offence Reporting
82//!
83//! Offences are reported from the relay chain (e.g., from BABE, GRANDPA, BEEFY, or parachain
84//! modules) through the `on_new_offences` function:
85//!
86//! ```text
87//! struct Offence {
88//! offender: AccountId, // The validator being slashed
89//! reporters: Vec<AccountId>, // Who reported the offence (may be empty)
90//! slash_fraction: Perbill, // Percentage of stake to slash
91//! }
92//! ```
93//!
94//! **Reporting Deadlines**:
95//! - With deferred slashing: Offences must be reported within `SlashDeferDuration - 1` eras
96//! - With immediate slashing: Offences can be reported up to `BondingDuration` eras old
97//!
98//! Example: If `SlashDeferDuration = 27` and current era is 100:
99//! - Oldest reportable offence: Era 74 (100 - 26)
100//! - Offences from era 73 or earlier are rejected
101//!
102//! ### Phase 2: Queuing
103//!
104//! When an offence passes validation, it's added to the queue:
105//!
106//! 1. **Storage**: Added to `OffenceQueue`: `(EraIndex, AccountId) -> OffenceRecord`
107//! 2. **Era Tracking**: Era added to `OffenceQueueEras` (sorted vector of eras with offences)
108//! 3. **Duplicate Handling**: If an offence already exists for the same validator in the same era,
109//! only the higher slash fraction is kept
110//!
111//! ### Phase 3: Processing
112//!
113//! Offences are processed incrementally in `on_initialize` each block:
114//!
115//! ```text
116//! 1. Load oldest offence from queue
117//! 2. Move to `ProcessingOffence` storage
118//! 3. For each exposure page (from last to first):
119//! - Calculate slash for validator's own stake
120//! - Calculate slash for each nominator (pro-rata based on exposure)
121//! - Track total slash and reward amounts
122//! 4. Once all pages processed, create `UnappliedSlash`
123//! ```
124//!
125//! **Key Features**:
126//! - **Page-by-page processing**: Large validator sets don't overwhelm a single block
127//! - **Pro-rata slashing**: Nominators slashed proportionally to their stake
128//! - **Reward calculation**: A portion goes to reporters (if any)
129//!
130//! ### Phase 4: Application
131//!
132//! Based on `SlashDeferDuration`, slashes are either:
133//!
134//! **Immediate (SlashDeferDuration = 0)**:
135//! - Applied right away in the same block
136//! - Funds deducted from staking ledger immediately
137//!
138//! **Deferred (SlashDeferDuration > 0)**:
139//! - Stored in `UnappliedSlashes` for future application
140//! - Applied at era: `offence_era + SlashDeferDuration`
141//! - Can be cancelled by governance before application
142//!
143//! ### Storage Items Involved
144//!
145//! - `OffenceQueue`: Pending offences to process
146//! - `OffenceQueueEras`: Sorted list of eras with offences
147//! - `ProcessingOffence`: Currently processing offence
148//! - `ValidatorSlashInEra`: Tracks highest slash per validator per era
149//! - `UnappliedSlashes`: Deferred slashes waiting for application
150//!
151//! ### Withdrawal Restrictions
152//!
153//! To maintain slashing guarantees, withdrawals are restricted:
154//!
155//! **Withdrawal Era Calculation**:
156//! ```text
157//! earliest_era_to_withdraw = min(
158//! active_era,
159//! last_fully_processed_offence_era + BondingDuration
160//! )
161//! ```
162//!
163//! **Example**:
164//! - Active era: 100
165//! - Oldest unprocessed offence: Era 70
166//! - BondingDuration: 28
167//! - Withdrawal allowed only for chunks with era ≤ 97 (70 - 1 + 28)
168//!
169//! **Withdrawal Timeline Example with an Offence**:
170//! ```text
171//! Era: 90 91 92 93 94 95 96 97 98 99 100 ... 117 118
172//! | | | | | | | | | | | | |
173//! Unbond: U
174//! Offence: X
175//! Reported: R
176//! Processed: P (within next few blocks)
177//! Slash Applied: S
178//! Withdraw: ❌ ✓
179//!
180//! With BondingDuration = 28 and SlashDeferDuration = 27:
181//! - User unbonds in era 90
182//! - Offence occurs in era 90
183//! - Reported in era 92 (typically within 2 days, but reportable until Era 116)
184//! - Processed in era 92 (within next few blocks after reporting)
185//! - Slash deferred for 27 eras, applied at era 117 (90 + 27)
186//! - Cannot withdraw unbonded chunks until era 118 (90 + 28)
187//!
188//! The 28-era bonding duration ensures that any offences committed before or during
189//! unbonding have time to be reported, processed, and applied before funds can be
190//! withdrawn. This provides a window for governance to cancel slashes that may have
191//! resulted from software bugs.
192//! ```
193//!
194//! **Key Restrictions**:
195//! 1. Cannot withdraw if previous era has unapplied slashes
196//! 2. Cannot withdraw funds from eras with unprocessed offences
197
198#![cfg_attr(not(feature = "std"), no_std)]
199#![recursion_limit = "256"]
200
201#[cfg(feature = "runtime-benchmarks")]
202pub mod benchmarking;
203#[cfg(any(feature = "runtime-benchmarks", test))]
204pub mod testing_utils;
205
206#[cfg(test)]
207pub(crate) mod mock;
208#[cfg(test)]
209mod tests;
210
211pub mod asset;
212pub mod election_size_tracker;
213pub mod ledger;
214pub mod migrations;
215mod pallet;
216pub mod reward;
217pub mod session_rotation;
218pub mod slashing;
219pub mod weights;
220
221extern crate alloc;
222use alloc::{vec, vec::Vec};
223use codec::{Decode, DecodeWithMemTracking, Encode, HasCompact, MaxEncodedLen};
224use frame_election_provider_support::ElectionProvider;
225use frame_support::{
226 traits::{
227 tokens::fungible::{Credit, Debt},
228 ConstU32, Contains, Get, LockIdentifier,
229 },
230 BoundedVec, DebugNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound, WeakBoundedVec,
231};
232use frame_system::pallet_prelude::BlockNumberFor;
233use ledger::LedgerIntegrityState;
234use scale_info::TypeInfo;
235use sp_runtime::{
236 traits::{AtLeast32BitUnsigned, One, StaticLookup, UniqueSaturatedInto},
237 BoundedBTreeMap, Debug, Perbill, Saturating,
238};
239use sp_staking::{EraIndex, ExposurePage, PagedExposureMetadata, SessionIndex};
240pub use sp_staking::{Exposure, IndividualExposure, StakerStatus};
241pub use weights::WeightInfo;
242
243// public exports
244pub use ledger::{StakingLedger, UnlockChunk};
245pub use pallet::{pallet::*, UseNominatorsAndValidatorsMap, UseValidatorsMap};
246
247pub(crate) const STAKING_ID: LockIdentifier = *b"staking ";
248pub(crate) const LOG_TARGET: &str = "runtime::staking-async";
249
250// syntactic sugar for logging.
251#[macro_export]
252macro_rules! log {
253 ($level:tt, $patter:expr $(, $values:expr)* $(,)?) => {
254 log::$level!(
255 target: crate::LOG_TARGET,
256 concat!("[{:?}] 💸 ", $patter), <frame_system::Pallet<T>>::block_number() $(, $values)*
257 )
258 };
259}
260
261/// Alias for a bounded set of exposures behind a validator, parameterized by this pallet's
262/// election provider.
263pub type BoundedExposuresOf<T> = BoundedVec<
264 (
265 <T as frame_system::Config>::AccountId,
266 Exposure<<T as frame_system::Config>::AccountId, BalanceOf<T>>,
267 ),
268 MaxWinnersPerPageOf<<T as Config>::ElectionProvider>,
269>;
270
271/// Alias for the maximum number of winners (aka. active validators), as defined in by this pallet's
272/// config.
273pub type MaxWinnersOf<T> = <T as Config>::MaxValidatorSet;
274
275/// Alias for the maximum number of winners per page, as expected by the election provider.
276pub type MaxWinnersPerPageOf<P> = <P as ElectionProvider>::MaxWinnersPerPage;
277
278/// Maximum number of nominations per nominator.
279pub type MaxNominationsOf<T> =
280 <<T as Config>::NominationsQuota as NominationsQuota<BalanceOf<T>>>::MaxNominations;
281
282/// Counter for the number of "reward" points earned by a given validator.
283pub type RewardPoint = u32;
284
285/// The balance type of this pallet.
286pub type BalanceOf<T> = <T as Config>::CurrencyBalance;
287
288type PositiveImbalanceOf<T> = Debt<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;
289pub type NegativeImbalanceOf<T> =
290 Credit<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;
291
292type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
293
294/// Information regarding the active era (era in used in session).
295#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen, PartialEq, Eq, Clone)]
296pub struct ActiveEraInfo {
297 /// Index of era.
298 pub index: EraIndex,
299 /// Moment of start expressed as millisecond from `$UNIX_EPOCH`.
300 ///
301 /// Start can be none if start hasn't been set for the era yet,
302 /// Start is set on the first on_finalize of the era to guarantee usage of `Time`.
303 pub start: Option<u64>,
304}
305
306/// Reward points of an era. Used to split era total payout between validators.
307///
308/// This points will be used to reward validators and their respective nominators.
309#[derive(
310 PartialEqNoBound, Encode, Decode, DebugNoBound, TypeInfo, MaxEncodedLen, DefaultNoBound,
311)]
312#[codec(mel_bound())]
313#[scale_info(skip_type_params(T))]
314pub struct EraRewardPoints<T: Config> {
315 /// Total number of points. Equals the sum of reward points for each validator.
316 pub total: RewardPoint,
317 /// The reward points earned by a given validator.
318 pub individual: BoundedBTreeMap<T::AccountId, RewardPoint, T::MaxValidatorSet>,
319}
320
321/// A destination account for payment.
322#[derive(
323 PartialEq,
324 Eq,
325 Copy,
326 Clone,
327 Encode,
328 Decode,
329 DecodeWithMemTracking,
330 Debug,
331 TypeInfo,
332 MaxEncodedLen,
333)]
334pub enum RewardDestination<AccountId> {
335 /// Pay into the stash account, increasing the amount at stake accordingly.
336 Staked,
337 /// Pay into the stash account, not increasing the amount at stake.
338 Stash,
339 #[deprecated(
340 note = "`Controller` will be removed after January 2024. Use `Account(controller)` instead."
341 )]
342 Controller,
343 /// Pay into a specified account.
344 Account(AccountId),
345 /// Receive no reward.
346 None,
347}
348
349/// Preference of what happens regarding validation.
350#[derive(
351 PartialEq,
352 Eq,
353 Clone,
354 Encode,
355 Decode,
356 DecodeWithMemTracking,
357 Debug,
358 TypeInfo,
359 Default,
360 MaxEncodedLen,
361)]
362pub struct ValidatorPrefs {
363 /// Reward that validator takes up-front; only the rest is split between themselves and
364 /// nominators.
365 #[codec(compact)]
366 pub commission: Perbill,
367 /// Whether or not this validator is accepting more nominations. If `true`, then no nominator
368 /// who is not already nominating this validator may nominate them. By default, validators
369 /// are accepting nominations.
370 pub blocked: bool,
371}
372
373/// Status of a paged snapshot progress.
374#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, MaxEncodedLen, Default)]
375pub enum SnapshotStatus<AccountId> {
376 /// Paged snapshot is in progress, the `AccountId` was the last staker iterated in the list.
377 Ongoing(AccountId),
378 /// All the stakers in the system have been consumed since the snapshot started.
379 Consumed,
380 /// Waiting for a new snapshot to be requested.
381 #[default]
382 Waiting,
383}
384
385/// A record of the nominations made by a specific account.
386#[derive(
387 PartialEqNoBound, EqNoBound, Clone, Encode, Decode, DebugNoBound, TypeInfo, MaxEncodedLen,
388)]
389#[codec(mel_bound())]
390#[scale_info(skip_type_params(T))]
391pub struct Nominations<T: Config> {
392 /// The targets of nomination.
393 pub targets: BoundedVec<T::AccountId, MaxNominationsOf<T>>,
394 /// The era the nominations were submitted.
395 ///
396 /// Except for initial nominations which are considered submitted at era 0.
397 pub submitted_in: EraIndex,
398 /// Whether the nominations have been suppressed. This can happen due to slashing of the
399 /// validators, or other events that might invalidate the nomination.
400 ///
401 /// NOTE: this for future proofing and is thus far not used.
402 pub suppressed: bool,
403}
404
405/// Facade struct to encapsulate `PagedExposureMetadata` and a single page of `ExposurePage`.
406///
407/// This is useful where we need to take into account the validator's own stake and total exposure
408/// in consideration, in addition to the individual nominators backing them.
409#[derive(Encode, Decode, Debug, TypeInfo, PartialEq, Eq)]
410pub struct PagedExposure<AccountId, Balance: HasCompact + codec::MaxEncodedLen> {
411 exposure_metadata: PagedExposureMetadata<Balance>,
412 exposure_page: ExposurePage<AccountId, Balance>,
413}
414
415impl<AccountId, Balance: HasCompact + Copy + AtLeast32BitUnsigned + codec::MaxEncodedLen>
416 PagedExposure<AccountId, Balance>
417{
418 /// Create a new instance of `PagedExposure` from legacy clipped exposures.
419 pub fn from_clipped(exposure: Exposure<AccountId, Balance>) -> Self {
420 Self {
421 exposure_metadata: PagedExposureMetadata {
422 total: exposure.total,
423 own: exposure.own,
424 nominator_count: exposure.others.len() as u32,
425 page_count: 1,
426 },
427 exposure_page: ExposurePage { page_total: exposure.total, others: exposure.others },
428 }
429 }
430
431 /// Create a new instance of `PagedExposure` from just the exposure metadata (overview).
432 ///
433 /// This creates a `PagedExposure` with an empty `others` list, useful when only the
434 /// validator's own stake needs to be considered (e.g., when nominators are not slashable).
435 pub fn from_overview(overview: PagedExposureMetadata<Balance>) -> Self {
436 Self {
437 exposure_metadata: PagedExposureMetadata {
438 total: overview.total,
439 own: overview.own,
440 nominator_count: overview.nominator_count,
441 page_count: 1,
442 },
443 exposure_page: ExposurePage { page_total: overview.total, others: vec![] },
444 }
445 }
446
447 /// Returns total exposure of this validator across pages
448 pub fn total(&self) -> Balance {
449 self.exposure_metadata.total
450 }
451
452 /// Returns total exposure of this validator for the current page
453 pub fn page_total(&self) -> Balance {
454 self.exposure_page.page_total + self.exposure_metadata.own
455 }
456
457 /// Returns validator's own stake that is exposed
458 pub fn own(&self) -> Balance {
459 self.exposure_metadata.own
460 }
461
462 /// Returns the portions of nominators stashes that are exposed in this page.
463 pub fn others(&self) -> &Vec<IndividualExposure<AccountId, Balance>> {
464 &self.exposure_page.others
465 }
466}
467
468/// A pending slash record. The value of the slash has been computed but not applied yet,
469/// rather deferred for several eras.
470#[derive(Encode, Decode, DebugNoBound, TypeInfo, MaxEncodedLen, PartialEqNoBound, EqNoBound)]
471#[scale_info(skip_type_params(T))]
472pub struct UnappliedSlash<T: Config> {
473 /// The stash ID of the offending validator.
474 pub validator: T::AccountId,
475 /// The validator's own slash.
476 pub own: BalanceOf<T>,
477 /// All other slashed stakers and amounts.
478 pub others: WeakBoundedVec<(T::AccountId, BalanceOf<T>), T::MaxExposurePageSize>,
479 /// Reporters of the offence; bounty payout recipients.
480 pub reporter: Option<T::AccountId>,
481 /// The amount of payout.
482 pub payout: BalanceOf<T>,
483}
484
485/// Something that defines the maximum number of nominations per nominator based on a curve.
486///
487/// The method `curve` implements the nomination quota curve and should not be used directly.
488/// However, `get_quota` returns the bounded maximum number of nominations based on `fn curve` and
489/// the nominator's balance.
490pub trait NominationsQuota<Balance> {
491 /// Strict maximum number of nominations that caps the nominations curve. This value can be
492 /// used as the upper bound of the number of votes per nominator.
493 type MaxNominations: Get<u32>;
494
495 /// Returns the voter's nomination quota within reasonable bounds [`min`, `max`], where `min`
496 /// is 1 and `max` is `Self::MaxNominations`.
497 fn get_quota(balance: Balance) -> u32 {
498 Self::curve(balance).clamp(1, Self::MaxNominations::get())
499 }
500
501 /// Returns the voter's nomination quota based on its balance and a curve.
502 fn curve(balance: Balance) -> u32;
503}
504
505/// Check if validator was inactive at some era.
506///
507/// The check is based on the [`RewardPoint`] amount received during the era by the given validator.
508pub trait IsValidatorInactive<AccountId> {
509 /// Tell if the validator considered inactive.
510 fn is_inactive(era: EraIndex, stash: &AccountId, era_points: RewardPoint) -> bool;
511}
512
513impl<AccountId> IsValidatorInactive<AccountId> for () {
514 fn is_inactive(_era: EraIndex, _stash: &AccountId, era_points: RewardPoint) -> bool {
515 era_points == 0
516 }
517}
518
519/// A nomination quota that allows up to MAX nominations for all validators.
520pub struct FixedNominationsQuota<const MAX: u32>;
521impl<Balance, const MAX: u32> NominationsQuota<Balance> for FixedNominationsQuota<MAX> {
522 type MaxNominations = ConstU32<MAX>;
523
524 fn curve(_: Balance) -> u32 {
525 MAX
526 }
527}
528
529pub use sp_staking::EraPayout;
530
531/// Mode of era-forcing.
532#[derive(
533 Copy,
534 Clone,
535 PartialEq,
536 Eq,
537 Encode,
538 Decode,
539 DecodeWithMemTracking,
540 Debug,
541 TypeInfo,
542 MaxEncodedLen,
543 serde::Serialize,
544 serde::Deserialize,
545)]
546pub enum Forcing {
547 /// Not forcing anything - just let whatever happen.
548 NotForcing,
549 /// Force a new era, then reset to `NotForcing` as soon as it is done.
550 /// Note that this will force to trigger an election until a new era is triggered, if the
551 /// election failed, the next session end will trigger a new election again, until success.
552 ForceNew,
553 /// Avoid a new era indefinitely.
554 ForceNone,
555 /// Force a new era at the end of all sessions indefinitely.
556 ForceAlways,
557}
558
559impl Default for Forcing {
560 fn default() -> Self {
561 Forcing::NotForcing
562 }
563}
564
565/// A utility struct that provides a way to check if a given account is a staker.
566///
567/// This struct implements the `Contains` trait, allowing it to determine whether
568/// a particular account is currently staking by checking if the account exists in
569/// the staking ledger.
570///
571/// Intended to be used in [`crate::Config::Filter`].
572pub struct AllStakers<T: Config>(core::marker::PhantomData<T>);
573
574impl<T: Config> Contains<T::AccountId> for AllStakers<T> {
575 /// Checks if the given account ID corresponds to a staker.
576 ///
577 /// # Returns
578 /// - `true` if the account has an entry in the staking ledger (indicating it is staking).
579 /// - `false` otherwise.
580 fn contains(account: &T::AccountId) -> bool {
581 Ledger::<T>::contains_key(account)
582 }
583}
584
585/// Size of the rotating pool of era-specific pot accounts.
586///
587/// Era pots are addressed by `era % POT_POOL_SIZE`, so a pot account is reused
588/// every `POT_POOL_SIZE` eras instead of a fresh account being created per era.
589/// This bounds the total storage footprint contributed by era pot accounts to a
590/// constant rather than growing with chain age.
591///
592/// Must be strictly greater than [`Config::HistoryDepth`] so that a slot is only
593/// reused after its previous era has been pruned and drained. The
594/// [`integrity_test`] enforces this invariant at runtime startup.
595pub(crate) const POT_POOL_SIZE: u32 = 200;
596
597/// Maps an era index to its slot in the rotating pot pool.
598pub(crate) fn pot_slot(era: EraIndex) -> u32 {
599 era % POT_POOL_SIZE
600}
601
602/// Kind of reward managed by staking pots.
603#[derive(
604 Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, codec::DecodeWithMemTracking, TypeInfo,
605)]
606pub enum RewardKind {
607 /// Staker rewards (nominators + validators).
608 #[codec(index = 0)]
609 StakerRewards,
610 /// Pot for validator self-stake incentive.
611 #[codec(index = 1)]
612 ValidatorSelfStake,
613}
614
615/// Identifies a reward pot account.
616#[derive(
617 Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, codec::DecodeWithMemTracking, TypeInfo,
618)]
619pub enum RewardPot {
620 /// General pot: funded by an external source (e.g. pallet-dap).
621 /// At era boundaries, staking snapshots the balance into an era-specific pot.
622 #[codec(index = 0)]
623 General(RewardKind),
624 /// Era-specific pot: snapshotted from the general pot at era boundaries.
625 /// See `POT_POOL_SIZE` for the slot rotation scheme.
626 #[codec(index = 1)]
627 Era(EraIndex, RewardKind),
628}
629
630/// Trait for generating reward pot account IDs.
631pub trait PotAccountProvider<AccountId> {
632 fn pot_account(pot: RewardPot) -> AccountId;
633}
634
635/// Seed-based pot account provider for production use.
636///
637/// Era pots are derived from `(slot, kind)` where `slot = era % POT_POOL_SIZE`,
638/// so a fixed pool of accounts rotates instead of creating one per era.
639pub struct Seed<S>(core::marker::PhantomData<S>);
640
641impl<AccountId, S> PotAccountProvider<AccountId> for Seed<S>
642where
643 AccountId: codec::FullCodec,
644 S: Get<frame_support::PalletId>,
645{
646 fn pot_account(pot: RewardPot) -> AccountId {
647 use sp_runtime::traits::AccountIdConversion;
648 // Era pots are addressed by slot (`era % POT_POOL_SIZE`), not by the
649 // raw era index, so a fixed pool of accounts rotates instead of
650 // growing per era.
651 let normalized = match pot {
652 RewardPot::Era(era, kind) => RewardPot::Era(pot_slot(era), kind),
653 other => other,
654 };
655 S::get().into_sub_account_truncating(normalized)
656 }
657}
658
659/// Sequential pot account provider for testing.
660///
661/// Mirrors the production rotation: era pots collide every `POT_POOL_SIZE`
662/// eras so tests exercise the same pool reuse path.
663#[cfg(feature = "std")]
664pub struct SequentialTest;
665
666#[cfg(feature = "std")]
667impl<AccountId> PotAccountProvider<AccountId> for SequentialTest
668where
669 AccountId: From<u64>,
670{
671 fn pot_account(pot: RewardPot) -> AccountId {
672 match pot {
673 RewardPot::General(RewardKind::StakerRewards) => AccountId::from(200_000u64),
674 RewardPot::General(RewardKind::ValidatorSelfStake) => AccountId::from(200_001u64),
675 RewardPot::Era(era, RewardKind::StakerRewards) => {
676 AccountId::from(100_000 + (pot_slot(era) as u64 * 10))
677 },
678 RewardPot::Era(era, RewardKind::ValidatorSelfStake) => {
679 AccountId::from(100_000 + (pot_slot(era) as u64 * 10) + 1)
680 },
681 }
682 }
683}
684
685/// Budget recipient for staker rewards.
686///
687/// Exposes the general staker reward pot so DAP can drip inflation into it.
688pub struct StakerRewardRecipient<P>(core::marker::PhantomData<P>);
689
690impl<AccountId, P> sp_staking::budget::BudgetRecipient<AccountId> for StakerRewardRecipient<P>
691where
692 P: PotAccountProvider<AccountId>,
693{
694 fn budget_key() -> sp_staking::budget::BudgetKey {
695 sp_staking::budget::BudgetKey::truncate_from(b"staker_rewards".to_vec())
696 }
697
698 fn pot_account() -> AccountId {
699 P::pot_account(RewardPot::General(RewardKind::StakerRewards))
700 }
701}
702
703/// Budget recipient for validator self-stake incentive.
704///
705/// Exposes the general validator incentive pot so DAP can drip inflation into it.
706pub struct ValidatorIncentiveRecipient<P>(core::marker::PhantomData<P>);
707
708impl<AccountId, P> sp_staking::budget::BudgetRecipient<AccountId> for ValidatorIncentiveRecipient<P>
709where
710 P: PotAccountProvider<AccountId>,
711{
712 fn budget_key() -> sp_staking::budget::BudgetKey {
713 sp_staking::budget::BudgetKey::truncate_from(b"validator_incentive".to_vec())
714 }
715
716 fn pot_account() -> AccountId {
717 P::pot_account(RewardPot::General(RewardKind::ValidatorSelfStake))
718 }
719}
720
721/// A smart type to determine the [`Config::PlanningEraOffset`], given:
722///
723/// * Expected relay session duration, `RS`
724/// * Time taking into consideration for XCM sending, `S`
725///
726/// It will use the estimated election duration, the relay session duration, and add one as it knows
727/// the relay chain will want to buffer validators for one session. This is needed because we use
728/// this in our calculation based on the "active era".
729pub struct PlanningEraOffsetOf<T, RS, S>(core::marker::PhantomData<(T, RS, S)>);
730impl<T: Config, RS: Get<BlockNumberFor<T>>, S: Get<BlockNumberFor<T>>> Get<SessionIndex>
731 for PlanningEraOffsetOf<T, RS, S>
732{
733 fn get() -> SessionIndex {
734 let election_duration = <T::ElectionProvider as ElectionProvider>::duration_with_export();
735 let sessions_needed = (election_duration + S::get()) / RS::get();
736 // add one, because we know the RC session pallet wants to buffer for one session, and
737 // another one cause we will receive activation report one session after that.
738 sessions_needed
739 .saturating_add(One::one())
740 .saturating_add(One::one())
741 .unique_saturated_into()
742 }
743}