Skip to main content

sp_staking/
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#![cfg_attr(not(feature = "std"), no_std)]
19
20//! A crate which contains primitives that are useful for implementation that uses staking
21//! approaches in general. Definitions related to sessions, slashing, etc go here.
22
23extern crate alloc;
24
25use crate::currency_to_vote::CurrencyToVote;
26use alloc::{collections::btree_map::BTreeMap, vec, vec::Vec};
27use codec::{Decode, DecodeWithMemTracking, Encode, FullCodec, HasCompact, MaxEncodedLen};
28use core::ops::{Add, AddAssign, Sub, SubAssign};
29use scale_info::TypeInfo;
30use sp_runtime::{
31	traits::{AtLeast32BitUnsigned, Zero},
32	Debug, DispatchError, DispatchResult, Perbill, Saturating,
33};
34
35pub mod budget;
36pub mod offence;
37
38pub mod currency_to_vote;
39
40/// Simple index type with which we can count sessions.
41pub type SessionIndex = u32;
42
43/// Counter for the number of eras that have passed.
44pub type EraIndex = u32;
45
46/// Type for identifying a page.
47pub type Page = u32;
48/// Representation of a staking account, which may be a stash or controller account.
49///
50/// Note: once the controller is completely deprecated, this enum can also be deprecated in favor of
51/// the stash account. Tracking issue: <https://github.com/paritytech/substrate/issues/6927>.
52#[derive(Clone, Debug)]
53pub enum StakingAccount<AccountId> {
54	Stash(AccountId),
55	Controller(AccountId),
56}
57
58#[cfg(feature = "std")]
59impl<AccountId> From<AccountId> for StakingAccount<AccountId> {
60	fn from(account: AccountId) -> Self {
61		StakingAccount::Stash(account)
62	}
63}
64
65/// Representation of the status of a staker.
66#[derive(Debug, TypeInfo)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize, PartialEq, Eq, Clone))]
68pub enum StakerStatus<AccountId> {
69	/// Chilling.
70	Idle,
71	/// Declaring desire in validate, i.e author blocks.
72	Validator,
73	/// Declaring desire to nominate, delegate, or generally approve of the given set of others.
74	Nominator(Vec<AccountId>),
75}
76
77/// A struct that reflects stake that an account has in the staking system. Provides a set of
78/// methods to operate on it's properties. Aimed at making `StakingInterface` more concise.
79#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
80pub struct Stake<Balance> {
81	/// The total stake that `stash` has in the staking system. This includes the
82	/// `active` stake, and any funds currently in the process of unbonding via
83	/// [`StakingInterface::unbond`].
84	///
85	/// # Note
86	///
87	/// This is only guaranteed to reflect the amount locked by the staking system. If there are
88	/// non-staking locks on the bonded pair's balance this amount is going to be larger in
89	/// reality.
90	pub total: Balance,
91	/// The total amount of the stash's balance that will be at stake in any forthcoming
92	/// rounds.
93	pub active: Balance,
94}
95
96/// A generic staking event listener.
97///
98/// Note that the interface is designed in a way that the events are fired post-action, so any
99/// pre-action data that is needed needs to be passed to interface methods. The rest of the data can
100/// be retrieved by using `StakingInterface`.
101#[impl_trait_for_tuples::impl_for_tuples(10)]
102pub trait OnStakingUpdate<AccountId, Balance> {
103	/// Fired when the stake amount of someone updates.
104	///
105	/// This is effectively any changes to the bond amount, such as bonding more funds, and
106	/// unbonding.
107	fn on_stake_update(_who: &AccountId, _prev_stake: Option<Stake<Balance>>) {}
108
109	/// Fired when someone sets their intention to nominate.
110	///
111	/// This should never be fired for existing nominators.
112	fn on_nominator_add(_who: &AccountId) {}
113
114	/// Fired when an existing nominator updates their nominations.
115	///
116	/// Note that this is not fired when a nominator changes their stake. For that,
117	/// `on_stake_update` should be used, followed by querying whether `who` was a validator or a
118	/// nominator.
119	fn on_nominator_update(_who: &AccountId, _prev_nominations: Vec<AccountId>) {}
120
121	/// Fired when someone removes their intention to nominate, either due to chill or validating.
122	///
123	/// The set of nominations at the time of removal is provided as it can no longer be fetched in
124	/// any way.
125	fn on_nominator_remove(_who: &AccountId, _nominations: Vec<AccountId>) {}
126
127	/// Fired when someone sets their intention to validate.
128	///
129	/// Note validator preference changes are not communicated, but could be added if needed.
130	fn on_validator_add(_who: &AccountId) {}
131
132	/// Fired when an existing validator updates their preferences.
133	///
134	/// Note validator preference changes are not communicated, but could be added if needed.
135	fn on_validator_update(_who: &AccountId) {}
136
137	/// Fired when someone removes their intention to validate, either due to chill or nominating.
138	fn on_validator_remove(_who: &AccountId) {}
139
140	/// Fired when someone is fully unstaked.
141	fn on_unstake(_who: &AccountId) {}
142
143	/// Fired when a staker is slashed.
144	///
145	/// * `stash` - The stash of the staker whom the slash was applied to.
146	/// * `slashed_active` - The new bonded balance of the staker after the slash was applied.
147	/// * `slashed_unlocking` - A map of slashed eras, and the balance of that unlocking chunk after
148	///   the slash is applied. Any era not present in the map is not affected at all.
149	/// * `slashed_total` - The aggregated balance that was lost due to the slash.
150	fn on_slash(
151		_stash: &AccountId,
152		_slashed_active: Balance,
153		_slashed_unlocking: &BTreeMap<EraIndex, Balance>,
154		_slashed_total: Balance,
155	) {
156	}
157
158	/// Fired when a portion of a staker's balance has been withdrawn.
159	fn on_withdraw(_stash: &AccountId, _amount: Balance) {}
160}
161
162/// A generic representation of a staking implementation.
163///
164/// This interface uses the terminology of NPoS, but it is aims to be generic enough to cover other
165/// implementations as well.
166pub trait StakingInterface {
167	/// Balance type used by the staking system.
168	type Balance: Sub<Output = Self::Balance>
169		+ Ord
170		+ PartialEq
171		+ Default
172		+ Copy
173		+ MaxEncodedLen
174		+ FullCodec
175		+ TypeInfo
176		+ Saturating;
177
178	/// AccountId type used by the staking system.
179	type AccountId: Clone + core::fmt::Debug;
180
181	/// Means of converting Currency to VoteWeight.
182	type CurrencyToVote: CurrencyToVote<Self::Balance>;
183
184	/// The minimum amount required to bond in order to set nomination intentions. This does not
185	/// necessarily mean the nomination will be counted in an election, but instead just enough to
186	/// be stored as a nominator. In other words, this is the minimum amount to register the
187	/// intention to nominate.
188	fn minimum_nominator_bond() -> Self::Balance;
189
190	/// The minimum amount required to bond in order to set validation intentions.
191	fn minimum_validator_bond() -> Self::Balance;
192
193	/// Return a stash account that is controlled by a `controller`.
194	///
195	/// ## Note
196	///
197	/// The controller abstraction is not permanent and might go away. Avoid using this as much as
198	/// possible.
199	fn stash_by_ctrl(controller: &Self::AccountId) -> Result<Self::AccountId, DispatchError>;
200
201	/// Number of eras that staked funds must remain bonded for.
202	///
203	/// This is the full bonding duration used by validators and recent ex-validators.
204	fn bonding_duration() -> EraIndex;
205
206	/// Number of eras that staked funds of a pure nominator must remain bonded for.
207	///
208	/// Same as [`Self::bonding_duration`] by default, but can be lower for pure nominators
209	/// (who have not been validators in recent eras) when nominators are not slashable.
210	///
211	/// Note: The actual unbonding duration for a specific account may vary:
212	/// - Validators always use [`Self::bonding_duration`]
213	/// - Nominators who were recently validators use [`Self::bonding_duration`]
214	/// - Pure nominators (never validators, or not validators in recent eras) may use a shorter
215	///   duration when not slashable
216	fn nominator_bonding_duration() -> EraIndex {
217		Self::bonding_duration()
218	}
219
220	/// The current era index.
221	///
222	/// This should be the latest planned era that the staking system knows about.
223	fn current_era() -> EraIndex;
224
225	/// Returns the [`Stake`] of `who`.
226	fn stake(who: &Self::AccountId) -> Result<Stake<Self::Balance>, DispatchError>;
227
228	/// Total stake of a staker, `Err` if not a staker.
229	fn total_stake(who: &Self::AccountId) -> Result<Self::Balance, DispatchError> {
230		Self::stake(who).map(|s| s.total)
231	}
232
233	/// Total active portion of a staker's [`Stake`], `Err` if not a staker.
234	fn active_stake(who: &Self::AccountId) -> Result<Self::Balance, DispatchError> {
235		Self::stake(who).map(|s| s.active)
236	}
237
238	/// Returns whether a staker is unbonding, `Err` if not a staker at all.
239	fn is_unbonding(who: &Self::AccountId) -> Result<bool, DispatchError> {
240		Self::stake(who).map(|s| s.active != s.total)
241	}
242
243	/// Returns whether a staker is FULLY unbonding, `Err` if not a staker at all.
244	fn fully_unbond(who: &Self::AccountId) -> DispatchResult {
245		Self::unbond(who, Self::stake(who)?.active)
246	}
247
248	/// Bond (lock) `value` of `who`'s balance, while forwarding any rewards to `payee`.
249	fn bond(who: &Self::AccountId, value: Self::Balance, payee: &Self::AccountId)
250		-> DispatchResult;
251
252	/// Have `who` nominate `validators`.
253	fn nominate(who: &Self::AccountId, validators: Vec<Self::AccountId>) -> DispatchResult;
254
255	/// Chill `who`.
256	fn chill(who: &Self::AccountId) -> DispatchResult;
257
258	/// Bond some extra amount in `who`'s free balance against the active bonded balance of
259	/// the account. The amount extra actually bonded will never be more than `who`'s free
260	/// balance.
261	fn bond_extra(who: &Self::AccountId, extra: Self::Balance) -> DispatchResult;
262
263	/// Schedule a portion of the active bonded balance to be unlocked at era
264	/// [Self::current_era] + [`Self::bonding_duration`].
265	///
266	/// Once the unlock era has been reached, [`Self::withdraw_unbonded`] can be called to unlock
267	/// the funds.
268	///
269	/// The amount of times this can be successfully called is limited based on how many distinct
270	/// eras funds are schedule to unlock in. Calling [`Self::withdraw_unbonded`] after some unlock
271	/// schedules have reached their unlocking era should allow more calls to this function.
272	fn unbond(stash: &Self::AccountId, value: Self::Balance) -> DispatchResult;
273
274	/// Set the reward destination for the ledger associated with the stash.
275	fn set_payee(stash: &Self::AccountId, reward_acc: &Self::AccountId) -> DispatchResult;
276
277	/// Unlock any funds schedule to unlock before or at the current era.
278	///
279	/// Returns whether the stash was killed because of this withdraw or not.
280	fn withdraw_unbonded(
281		stash: Self::AccountId,
282		num_slashing_spans: u32,
283	) -> Result<bool, DispatchError>;
284
285	/// The ideal number of active validators.
286	fn desired_validator_count() -> u32;
287
288	/// Whether or not there is an ongoing election.
289	fn election_ongoing() -> bool;
290
291	/// Force a current staker to become completely unstaked, immediately.
292	fn force_unstake(who: Self::AccountId) -> DispatchResult;
293
294	/// Checks whether an account `staker` has been exposed in an era.
295	fn is_exposed_in_era(who: &Self::AccountId, era: &EraIndex) -> bool;
296
297	/// Return the status of the given staker, `Err` if not staked at all.
298	fn status(who: &Self::AccountId) -> Result<StakerStatus<Self::AccountId>, DispatchError>;
299
300	/// Checks whether or not this is a validator account.
301	fn is_validator(who: &Self::AccountId) -> bool {
302		Self::status(who).map(|s| matches!(s, StakerStatus::Validator)).unwrap_or(false)
303	}
304
305	/// Checks whether the staker is a virtual account.
306	///
307	/// A virtual staker is an account whose locks are not managed by the [`StakingInterface`]
308	/// implementation but by an external pallet. See [`StakingUnchecked::virtual_bond`] for more
309	/// details.
310	fn is_virtual_staker(who: &Self::AccountId) -> bool;
311
312	/// Get the nominations of a stash, if they are a nominator, `None` otherwise.
313	fn nominations(who: &Self::AccountId) -> Option<Vec<Self::AccountId>> {
314		match Self::status(who) {
315			Ok(StakerStatus::Nominator(t)) => Some(t),
316			_ => None,
317		}
318	}
319
320	/// Returns the fraction of the slash to be rewarded to reporter.
321	fn slash_reward_fraction() -> Perbill;
322
323	#[cfg(feature = "runtime-benchmarks")]
324	fn max_exposure_page_size() -> Page;
325
326	#[cfg(feature = "runtime-benchmarks")]
327	fn add_era_stakers(
328		current_era: &EraIndex,
329		stash: &Self::AccountId,
330		exposures: Vec<(Self::AccountId, Self::Balance)>,
331	);
332
333	/// Benchmark and test helper to set both active and current era.
334	#[cfg(any(feature = "std", feature = "runtime-benchmarks"))]
335	fn set_era(era: EraIndex);
336}
337
338/// Set of low level apis to manipulate staking ledger.
339///
340/// These apis bypass some or all safety checks and should only be used if you know what you are
341/// doing.
342pub trait StakingUnchecked: StakingInterface {
343	/// Migrate an existing staker to a virtual staker.
344	///
345	/// It would release all funds held by the implementation pallet.
346	fn migrate_to_virtual_staker(who: &Self::AccountId) -> DispatchResult;
347
348	/// Book-keep a new bond for `keyless_who` without applying any locks (hence virtual).
349	///
350	/// It is important that `keyless_who` is a keyless account and therefore cannot interact with
351	/// staking pallet directly. Caller is responsible for ensuring the passed amount is locked and
352	/// valid.
353	fn virtual_bond(
354		keyless_who: &Self::AccountId,
355		value: Self::Balance,
356		payee: &Self::AccountId,
357	) -> DispatchResult;
358
359	/// Migrate a virtual staker to a direct staker.
360	///
361	/// Only used for testing.
362	#[cfg(feature = "runtime-benchmarks")]
363	fn migrate_to_direct_staker(who: &Self::AccountId);
364}
365
366/// The amount of exposure for an era that an individual nominator has (susceptible to slashing).
367#[derive(
368	PartialEq,
369	Eq,
370	PartialOrd,
371	Ord,
372	Clone,
373	Encode,
374	Decode,
375	DecodeWithMemTracking,
376	Debug,
377	TypeInfo,
378	Copy,
379)]
380pub struct IndividualExposure<AccountId, Balance: HasCompact> {
381	/// The stash account of the nominator in question.
382	pub who: AccountId,
383	/// Amount of funds exposed.
384	#[codec(compact)]
385	pub value: Balance,
386}
387
388/// A snapshot of the stake backing a single validator in the system.
389#[derive(
390	PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, DecodeWithMemTracking, Debug, TypeInfo,
391)]
392pub struct Exposure<AccountId, Balance: HasCompact> {
393	/// The total balance backing this validator.
394	#[codec(compact)]
395	pub total: Balance,
396	/// The validator's own stash that is exposed.
397	#[codec(compact)]
398	pub own: Balance,
399	/// The portions of nominators stashes that are exposed.
400	pub others: Vec<IndividualExposure<AccountId, Balance>>,
401}
402
403impl<AccountId, Balance: Default + HasCompact> Default for Exposure<AccountId, Balance> {
404	fn default() -> Self {
405		Self { total: Default::default(), own: Default::default(), others: vec![] }
406	}
407}
408
409impl<
410		AccountId: Clone,
411		Balance: HasCompact + AtLeast32BitUnsigned + Copy + codec::MaxEncodedLen,
412	> Exposure<AccountId, Balance>
413{
414	/// Splits self into two instances of exposures.
415	///
416	/// `n_others` individual exposures are consumed from self and returned as part of the new
417	/// exposure.
418	///
419	/// Since this method splits `others` of a single exposure, `total.own` will be the same for
420	/// both `self` and the returned exposure.
421	pub fn split_others(&mut self, n_others: u32) -> Self {
422		let head_others: Vec<_> =
423			self.others.drain(..(n_others as usize).min(self.others.len())).collect();
424
425		let total_others_head: Balance = head_others
426			.iter()
427			.fold(Zero::zero(), |acc: Balance, o| acc.saturating_add(o.value));
428
429		self.total = self.total.saturating_sub(total_others_head);
430
431		Self {
432			total: total_others_head.saturating_add(self.own),
433			own: self.own,
434			others: head_others,
435		}
436	}
437
438	/// Converts an `Exposure` into `PagedExposureMetadata` and multiple chunks of
439	/// `IndividualExposure` with each chunk having maximum of `page_size` elements.
440	pub fn into_pages(
441		self,
442		page_size: Page,
443	) -> (PagedExposureMetadata<Balance>, Vec<ExposurePage<AccountId, Balance>>) {
444		let individual_chunks = self.others.chunks(page_size as usize);
445		let mut exposure_pages: Vec<ExposurePage<AccountId, Balance>> =
446			Vec::with_capacity(individual_chunks.len());
447
448		for chunk in individual_chunks {
449			let mut page_total: Balance = Zero::zero();
450			let mut others: Vec<IndividualExposure<AccountId, Balance>> =
451				Vec::with_capacity(chunk.len());
452			for individual in chunk.iter() {
453				page_total.saturating_accrue(individual.value);
454				others.push(IndividualExposure {
455					who: individual.who.clone(),
456					value: individual.value,
457				})
458			}
459			exposure_pages.push(ExposurePage { page_total, others });
460		}
461
462		(
463			PagedExposureMetadata {
464				total: self.total,
465				own: self.own,
466				nominator_count: self.others.len() as u32,
467				page_count: exposure_pages.len() as Page,
468			},
469			exposure_pages,
470		)
471	}
472}
473
474/// A snapshot of the stake backing a single validator in the system.
475#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Debug, TypeInfo)]
476pub struct ExposurePage<AccountId, Balance: HasCompact> {
477	/// The total balance of this chunk/page.
478	#[codec(compact)]
479	pub page_total: Balance,
480	/// The portions of nominators stashes that are exposed.
481	pub others: Vec<IndividualExposure<AccountId, Balance>>,
482}
483
484impl<A, B: Default + HasCompact> Default for ExposurePage<A, B> {
485	fn default() -> Self {
486		ExposurePage { page_total: Default::default(), others: vec![] }
487	}
488}
489
490/// Returns an exposure page from a set of individual exposures.
491impl<A, B: HasCompact + Default + AddAssign + SubAssign + Clone> From<Vec<IndividualExposure<A, B>>>
492	for ExposurePage<A, B>
493{
494	fn from(exposures: Vec<IndividualExposure<A, B>>) -> Self {
495		exposures.into_iter().fold(ExposurePage::default(), |mut page, e| {
496			page.page_total += e.value.clone();
497			page.others.push(e);
498			page
499		})
500	}
501}
502
503/// Metadata for Paged Exposure of a validator such as total stake across pages and page count.
504///
505/// In combination with the associated `ExposurePage`s, it can be used to reconstruct a full
506/// `Exposure` set of a validator. This is useful for cases where we want to query full set of
507/// `Exposure` as one page (for backward compatibility).
508#[derive(
509	PartialEq,
510	Eq,
511	PartialOrd,
512	Ord,
513	Clone,
514	Encode,
515	Decode,
516	Debug,
517	TypeInfo,
518	Default,
519	MaxEncodedLen,
520	Copy,
521)]
522pub struct PagedExposureMetadata<Balance: HasCompact + codec::MaxEncodedLen> {
523	/// The total balance backing this validator.
524	#[codec(compact)]
525	pub total: Balance,
526	/// The validator's own stash that is exposed.
527	#[codec(compact)]
528	pub own: Balance,
529	/// Number of nominators backing this validator.
530	pub nominator_count: u32,
531	/// Number of pages of nominators.
532	pub page_count: Page,
533}
534
535impl<Balance> PagedExposureMetadata<Balance>
536where
537	Balance: HasCompact
538		+ codec::MaxEncodedLen
539		+ Add<Output = Balance>
540		+ Sub<Output = Balance>
541		+ sp_runtime::Saturating
542		+ PartialEq
543		+ Copy
544		+ sp_runtime::traits::Debug,
545{
546	/// Consumes self and returns the result of the metadata updated with `other_balances` and
547	/// of adding `other_num` nominators to the metadata.
548	///
549	/// `Max` is a getter of the maximum number of nominators per page.
550	pub fn update_with<Max: sp_core::Get<u32>>(
551		self,
552		others_balance: Balance,
553		others_num: u32,
554	) -> Self {
555		let page_limit = Max::get().max(1);
556		let new_nominator_count = self.nominator_count.saturating_add(others_num);
557		let new_page_count = new_nominator_count
558			.saturating_add(page_limit)
559			.saturating_sub(1)
560			.saturating_div(page_limit);
561
562		Self {
563			total: self.total.saturating_add(others_balance),
564			own: self.own,
565			nominator_count: new_nominator_count,
566			page_count: new_page_count,
567		}
568	}
569}
570
571/// A type that belongs only in the context of an `Agent`.
572///
573/// `Agent` is someone that manages delegated funds from [`Delegator`] accounts. It can
574/// then use these funds to participate in the staking system. It can never use its own funds to
575/// stake. They instead (virtually bond)[`StakingUnchecked::virtual_bond`] into the staking system
576/// and are also called `Virtual Stakers`.
577///
578/// The `Agent` is also responsible for managing rewards and slashing for all the `Delegators` that
579/// have delegated funds to it.
580#[derive(Clone, Debug)]
581pub struct Agent<T>(T);
582impl<T> From<T> for Agent<T> {
583	fn from(acc: T) -> Self {
584		Agent(acc)
585	}
586}
587
588impl<T> Agent<T> {
589	pub fn get(self) -> T {
590		self.0
591	}
592}
593
594/// A type that belongs only in the context of a `Delegator`.
595///
596/// `Delegator` is someone that delegates funds to an `Agent`, allowing them to pool funds
597/// along with other delegators and participate in the staking system.
598#[derive(Clone, Debug)]
599pub struct Delegator<T>(T);
600impl<T> From<T> for Delegator<T> {
601	fn from(acc: T) -> Self {
602		Delegator(acc)
603	}
604}
605
606impl<T> Delegator<T> {
607	pub fn get(self) -> T {
608		self.0
609	}
610}
611
612/// Trait to provide delegation functionality for stakers.
613pub trait DelegationInterface {
614	/// Balance type used by the staking system.
615	type Balance: Sub<Output = Self::Balance>
616		+ Ord
617		+ PartialEq
618		+ Default
619		+ Copy
620		+ MaxEncodedLen
621		+ FullCodec
622		+ TypeInfo
623		+ Saturating;
624
625	/// AccountId type used by the staking system.
626	type AccountId: Clone + core::fmt::Debug;
627
628	/// Returns effective balance of the `Agent` account. `None` if not an `Agent`.
629	///
630	/// This takes into account any pending slashes to `Agent` against the delegated balance.
631	fn agent_balance(agent: Agent<Self::AccountId>) -> Option<Self::Balance>;
632
633	/// Returns the total amount of funds that is unbonded and can be withdrawn from the `Agent`
634	/// account. `None` if not an `Agent`.
635	fn agent_transferable_balance(agent: Agent<Self::AccountId>) -> Option<Self::Balance>;
636
637	/// Returns the total amount of funds delegated. `None` if not a `Delegator`.
638	fn delegator_balance(delegator: Delegator<Self::AccountId>) -> Option<Self::Balance>;
639
640	/// Register `Agent` such that it can accept delegation.
641	fn register_agent(
642		agent: Agent<Self::AccountId>,
643		reward_account: &Self::AccountId,
644	) -> DispatchResult;
645
646	/// Removes `Agent` registration.
647	///
648	/// This should only be allowed if the agent has no staked funds.
649	fn remove_agent(agent: Agent<Self::AccountId>) -> DispatchResult;
650
651	/// Add delegation to the `Agent`.
652	fn delegate(
653		delegator: Delegator<Self::AccountId>,
654		agent: Agent<Self::AccountId>,
655		amount: Self::Balance,
656	) -> DispatchResult;
657
658	/// Withdraw or revoke delegation to `Agent`.
659	///
660	/// If there are `Agent` funds upto `amount` available to withdraw, then those funds would
661	/// be released to the `delegator`
662	fn withdraw_delegation(
663		delegator: Delegator<Self::AccountId>,
664		agent: Agent<Self::AccountId>,
665		amount: Self::Balance,
666		num_slashing_spans: u32,
667	) -> DispatchResult;
668
669	/// Returns pending slashes posted to the `Agent` account. None if not an `Agent`.
670	///
671	/// Slashes to `Agent` account are not immediate and are applied lazily. Since `Agent`
672	/// has an unbounded number of delegators, immediate slashing is not possible.
673	fn pending_slash(agent: Agent<Self::AccountId>) -> Option<Self::Balance>;
674
675	/// Apply a pending slash to an `Agent` by slashing `value` from `delegator`.
676	///
677	/// A reporter may be provided (if one exists) in order for the implementor to reward them,
678	/// if applicable.
679	fn delegator_slash(
680		agent: Agent<Self::AccountId>,
681		delegator: Delegator<Self::AccountId>,
682		value: Self::Balance,
683		maybe_reporter: Option<Self::AccountId>,
684	) -> DispatchResult;
685}
686
687/// Trait to provide functionality for direct stakers to migrate to delegation agents.
688/// See [`DelegationInterface`] for more details on delegation.
689pub trait DelegationMigrator {
690	/// Balance type used by the staking system.
691	type Balance: Sub<Output = Self::Balance>
692		+ Ord
693		+ PartialEq
694		+ Default
695		+ Copy
696		+ MaxEncodedLen
697		+ FullCodec
698		+ TypeInfo
699		+ Saturating;
700
701	/// AccountId type used by the staking system.
702	type AccountId: Clone + core::fmt::Debug;
703
704	/// Migrate an existing `Nominator` to `Agent` account.
705	///
706	/// The implementation should ensure the `Nominator` account funds are moved to an escrow
707	/// from which `Agents` can later release funds to its `Delegators`.
708	fn migrate_nominator_to_agent(
709		agent: Agent<Self::AccountId>,
710		reward_account: &Self::AccountId,
711	) -> DispatchResult;
712
713	/// Migrate `value` of delegation to `delegator` from a migrating agent.
714	///
715	/// When a direct `Nominator` migrates to `Agent`, the funds are kept in escrow. This function
716	/// allows the `Agent` to release the funds to the `delegator`.
717	fn migrate_delegation(
718		agent: Agent<Self::AccountId>,
719		delegator: Delegator<Self::AccountId>,
720		value: Self::Balance,
721	) -> DispatchResult;
722
723	/// Drop the `Agent` account and its associated delegators.
724	///
725	/// Also removed from [`StakingUnchecked`] as a Virtual Staker. Useful for testing.
726	#[cfg(feature = "runtime-benchmarks")]
727	fn force_kill_agent(agent: Agent<Self::AccountId>);
728}
729
730/// Handler for determining how much of a balance should be paid out on the current era.
731///
732/// [`budget::IssuanceCurve`] is the successor to this trait, decoupling issuance computation
733/// from staking state.
734pub trait EraPayout<Balance> {
735	/// Determine the payout for this era.
736	///
737	/// Returns the amount to be paid to stakers in this era, as well as whatever else should be
738	/// paid out ("the rest").
739	fn era_payout(
740		total_staked: Balance,
741		total_issuance: Balance,
742		era_duration_millis: u64,
743	) -> (Balance, Balance);
744}
745
746impl<Balance: Default> EraPayout<Balance> for () {
747	fn era_payout(
748		_total_staked: Balance,
749		_total_issuance: Balance,
750		_era_duration_millis: u64,
751	) -> (Balance, Balance) {
752		(Default::default(), Default::default())
753	}
754}
755
756/// Result of splitting a validator's staking reward between the validator and their nominators.
757///
758/// Produced by [`StakerRewardCalculator::calculate_staker_reward`].
759#[derive(Debug, Clone, PartialEq, Eq)]
760pub struct StakerRewardResult<Balance> {
761	/// Total payout for the validator (commission + proportional stake reward).
762	pub validator_payout: Balance,
763	/// Total payout for all nominators, to be split proportionally by the caller.
764	pub nominator_payout: Balance,
765}
766
767/// Handles two independent reward calculations:
768///
769/// 1. **Staker reward split** ([`Self::calculate_staker_reward`]) — determines how a validator's
770///    staking reward is divided between the validator and their nominators.
771///
772/// 2. **Validator incentive weight** ([`Self::calculate_validator_incentive_weight`]) — determines
773///    a validator's relative share of a separate validator incentive pot, based on self-stake. This
774///    incentive pot is validator-only; nominators do not receive from it.
775pub trait StakerRewardCalculator<Balance> {
776	/// Compute a weight for this validator's share of the validator incentive pot.
777	///
778	/// Called once per validator during era planning. All validators' weights are summed, and
779	/// each validator's incentive payout is proportional to `their_weight / total_weight`.
780	fn calculate_validator_incentive_weight(self_stake: Balance) -> Balance;
781
782	/// Split a validator's staking reward into validator and nominator portions.
783	fn calculate_staker_reward(
784		validator_total_reward: Balance,
785		validator_commission: Perbill,
786		validator_own_stake: Balance,
787		total_exposure: Balance,
788	) -> StakerRewardResult<Balance>;
789}
790
791impl<Balance: Default> StakerRewardCalculator<Balance> for () {
792	fn calculate_validator_incentive_weight(_self_stake: Balance) -> Balance {
793		Default::default()
794	}
795
796	fn calculate_staker_reward(
797		_validator_total_reward: Balance,
798		_validator_commission: Perbill,
799		_validator_own_stake: Balance,
800		_total_exposure: Balance,
801	) -> StakerRewardResult<Balance> {
802		StakerRewardResult {
803			validator_payout: Default::default(),
804			nominator_payout: Default::default(),
805		}
806	}
807}
808
809sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);
810sp_core::generate_feature_enabled_macro!(std_or_benchmarks_enabled, any(feature = "std", feature = "runtime-benchmarks"), $);
811
812#[cfg(test)]
813mod tests {
814	use sp_core::ConstU32;
815
816	use super::*;
817
818	#[test]
819	fn update_with_works() {
820		let metadata = PagedExposureMetadata::<u32> {
821			total: 1000,
822			own: 0, // don't care
823			nominator_count: 10,
824			page_count: 1,
825		};
826
827		assert_eq!(
828			metadata.update_with::<ConstU32<10>>(1, 1),
829			PagedExposureMetadata { total: 1001, own: 0, nominator_count: 11, page_count: 2 },
830		);
831
832		assert_eq!(
833			metadata.update_with::<ConstU32<5>>(1, 1),
834			PagedExposureMetadata { total: 1001, own: 0, nominator_count: 11, page_count: 3 },
835		);
836
837		assert_eq!(
838			metadata.update_with::<ConstU32<4>>(1, 1),
839			PagedExposureMetadata { total: 1001, own: 0, nominator_count: 11, page_count: 3 },
840		);
841
842		assert_eq!(
843			metadata.update_with::<ConstU32<1>>(1, 1),
844			PagedExposureMetadata { total: 1001, own: 0, nominator_count: 11, page_count: 11 },
845		);
846	}
847
848	#[test]
849	fn individual_exposures_to_exposure_works() {
850		let exposure_1 = IndividualExposure { who: 1, value: 10u32 };
851		let exposure_2 = IndividualExposure { who: 2, value: 20 };
852		let exposure_3 = IndividualExposure { who: 3, value: 30 };
853
854		let exposure_page: ExposurePage<u32, u32> = vec![exposure_1, exposure_2, exposure_3].into();
855
856		assert_eq!(
857			exposure_page,
858			ExposurePage { page_total: 60, others: vec![exposure_1, exposure_2, exposure_3] },
859		);
860	}
861
862	#[test]
863	fn empty_individual_exposures_to_exposure_works() {
864		let empty_exposures: Vec<IndividualExposure<u32, u32>> = vec![];
865
866		let exposure_page: ExposurePage<u32, u32> = empty_exposures.into();
867		assert_eq!(exposure_page, ExposurePage { page_total: 0, others: vec![] });
868	}
869
870	#[test]
871	fn exposure_split_others_works() {
872		let exposure = Exposure {
873			total: 100,
874			own: 20,
875			others: vec![
876				IndividualExposure { who: 1, value: 20u32 },
877				IndividualExposure { who: 2, value: 20 },
878				IndividualExposure { who: 3, value: 20 },
879				IndividualExposure { who: 4, value: 20 },
880			],
881		};
882
883		let mut exposure_0 = exposure.clone();
884		// split others with with 0 `n_others` is a noop and returns an empty exposure (with `own`
885		// only).
886		let split_exposure = exposure_0.split_others(0);
887		assert_eq!(exposure_0, exposure);
888		assert_eq!(split_exposure, Exposure { total: 20, own: 20, others: vec![] });
889
890		let mut exposure_1 = exposure.clone();
891		// split individual exposures so that the returned exposure has 1 individual exposure.
892		let split_exposure = exposure_1.split_others(1);
893		assert_eq!(exposure_1.own, 20);
894		assert_eq!(exposure_1.total, 20 + 3 * 20);
895		assert_eq!(exposure_1.others.len(), 3);
896
897		assert_eq!(split_exposure.own, 20);
898		assert_eq!(split_exposure.total, 20 + 1 * 20);
899		assert_eq!(split_exposure.others.len(), 1);
900
901		let mut exposure_3 = exposure.clone();
902		// split individual exposures so that the returned exposure has 3 individual exposures,
903		// which are consumed from the original exposure.
904		let split_exposure = exposure_3.split_others(3);
905		assert_eq!(exposure_3.own, 20);
906		assert_eq!(exposure_3.total, 20 + 1 * 20);
907		assert_eq!(exposure_3.others.len(), 1);
908
909		assert_eq!(split_exposure.own, 20);
910		assert_eq!(split_exposure.total, 20 + 3 * 20);
911		assert_eq!(split_exposure.others.len(), 3);
912
913		let mut exposure_max = exposure.clone();
914		// split others with with more `n_others` than the number of others in the exposure
915		// consumes all the individual exposures of the original Exposure and returns them in the
916		// new exposure.
917		let split_exposure = exposure_max.split_others(u32::MAX);
918		assert_eq!(split_exposure, exposure);
919		assert_eq!(exposure_max, Exposure { total: 20, own: 20, others: vec![] });
920	}
921}