lib.rs 81.3 KiB
Newer Older
			if <Ledger<T>>::contains_key(&controller) {
				Err(Error::<T>::AlreadyPaired)?
			// reject a bond which is considered to be _dust_.
			if value < T::Currency::minimum_balance() {
				Err(Error::<T>::InsufficientValue)?
			// You're auto-bonded forever, here. We might improve this by only bonding when
			// you actually validate/nominate and remove once you unbond __everything__.
			<Bonded<T>>::insert(&stash, &controller);
			<Payee<T>>::insert(&stash, payee);
Gavin Wood's avatar
Gavin Wood committed
			system::Module::<T>::inc_ref(&stash);

			let stash_balance = T::Currency::free_balance(&stash);
			let value = value.min(stash_balance);
			Self::deposit_event(RawEvent::Bonded(stash.clone(), value));
Gavin Wood's avatar
Gavin Wood committed
			let item = StakingLedger {
				stash,
				total: value,
				active: value,
				unlocking: vec![],
				last_reward: Self::current_era(),
			};
			Self::update_ledger(&controller, &item);
		/// Add some extra amount that have appeared in the stash `free_balance` into the balance up
Gavin Wood's avatar
Gavin Wood committed
		/// for staking.
		/// Use this if there are additional funds in your stash account that you wish to bond.
		/// Unlike [`bond`] or [`unbond`] this function does not impose any limitation on the amount
		/// that can be added.
		/// The dispatch origin for this call must be _Signed_ by the stash, not the controller.
		/// # <weight>
		/// - Independent of the arguments. Insignificant complexity.
		/// - O(1).
		/// - One DB entry.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
		fn bond_extra(origin, #[compact] max_additional: BalanceOf<T>) {
			let stash = ensure_signed(origin)?;
			let controller = Self::bonded(&stash).ok_or(Error::<T>::NotStash)?;
			let mut ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			let stash_balance = T::Currency::free_balance(&stash);

			if let Some(extra) = stash_balance.checked_sub(&ledger.total) {
				let extra = extra.min(max_additional);
				ledger.total += extra;
				ledger.active += extra;
				Self::deposit_event(RawEvent::Bonded(stash, extra));
				Self::update_ledger(&controller, &ledger);
		/// Schedule a portion of the stash to be unlocked ready for transfer out after the bond
Bastian Köcher's avatar
Bastian Köcher committed
		/// period ends. If this leaves an amount actively bonded less than
		/// T::Currency::minimum_balance(), then it is increased to the full amount.
		/// Once the unlock period is done, you can call `withdraw_unbonded` to actually move
Bastian Köcher's avatar
Bastian Köcher committed
		/// the funds out of management ready for transfer.
		///
		/// No more than a limited number of unlocking chunks (see `MAX_UNLOCKING_CHUNKS`)
		/// can co-exists at the same time. In that case, [`Call::withdraw_unbonded`] need
		/// to be called first to remove some of the chunks (if possible).
		///
		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
Bastian Köcher's avatar
Bastian Köcher committed
		/// See also [`Call::withdraw_unbonded`].
		///
		/// # <weight>
		/// - Independent of the arguments. Limited but potentially exploitable complexity.
		/// - Contains a limited number of reads.
		/// - Each call (requires the remainder of the bonded balance to be above `minimum_balance`)
		///   will cause a new entry to be inserted into a vector (`Ledger.unlocking`) kept in storage.
Gavin Wood's avatar
Gavin Wood committed
		///   The only way to clean the aforementioned storage item is also user-controlled via
		///   `withdraw_unbonded`.
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(400_000)]
		fn unbond(origin, #[compact] value: BalanceOf<T>) {
			let controller = ensure_signed(origin)?;
			let mut ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			ensure!(
				ledger.unlocking.len() < MAX_UNLOCKING_CHUNKS,
				Error::<T>::NoMoreChunks,

			let mut value = value.min(ledger.active);

			if !value.is_zero() {
				ledger.active -= value;

				// Avoid there being a dust balance left in the staking system.
				if ledger.active < T::Currency::minimum_balance() {
					value += ledger.active;
					ledger.active = Zero::zero();
				}
Gavin Wood's avatar
Gavin Wood committed
				// Note: in case there is no current era it is fine to bond one era more.
				let era = Self::current_era().unwrap_or(0) + T::BondingDuration::get();
				ledger.unlocking.push(UnlockChunk { value, era });
				Self::update_ledger(&controller, &ledger);
				Self::deposit_event(RawEvent::Unbonded(ledger.stash.clone(), value));
		/// Remove any unlocked chunks from the `unlocking` queue from our management.
		/// This essentially frees up that balance to be used by the stash account to do
		/// whatever it wants.
		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
Bastian Köcher's avatar
Bastian Köcher committed
		/// See also [`Call::unbond`].
		/// - Could be dependent on the `origin` argument and how much `unlocking` chunks exist.
		///  It implies `consolidate_unlocked` which loops over `Ledger.unlocking`, which is
		///  indirectly user-controlled. See [`unbond`] for more detail.
		/// - Contains a limited number of reads, yet the size of which could be large based on `ledger`.
		/// - Writes are limited to the `origin` account key.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(400_000)]
		fn withdraw_unbonded(origin) {
			let controller = ensure_signed(origin)?;
Gavin Wood's avatar
Gavin Wood committed
			let mut ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			let (stash, old_total) = (ledger.stash.clone(), ledger.total);
Gavin Wood's avatar
Gavin Wood committed
			if let Some(current_era) = Self::current_era() {
				ledger = ledger.consolidate_unlocked(current_era)
			}

			if ledger.unlocking.is_empty() && ledger.active.is_zero() {
				// This account must have called `unbond()` with some value that caused the active
				// portion to fall below existential deposit + will have no more unlocking chunks
				// left. We can now safely remove all staking-related information.
Gavin Wood's avatar
Gavin Wood committed
				Self::kill_stash(&stash)?;
				// remove the lock.
				T::Currency::remove_lock(STAKING_ID, &stash);
			} else {
				// This was the consequence of a partial unbond. just update the ledger and move on.
				Self::update_ledger(&controller, &ledger);
			}

			// `old_total` should never be less than the new total because
			// `consolidate_unlocked` strictly subtracts balance.
			if ledger.total < old_total {
				// Already checked that this won't overflow by entry condition.
				let value = old_total - ledger.total;
				Self::deposit_event(RawEvent::Withdrawn(stash, value));
			}
		/// Declare the desire to validate for the origin controller.
		///
		/// Effects will be felt at the beginning of the next era.
		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
		///
		/// # <weight>
		/// - Independent of the arguments. Insignificant complexity.
		/// - Contains a limited number of reads.
		/// - Writes are limited to the `origin` account key.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(750_000)]
		fn validate(origin, prefs: ValidatorPrefs) {
			let controller = ensure_signed(origin)?;
			let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			let stash = &ledger.stash;
			<Nominators<T>>::remove(stash);
			<Validators<T>>::insert(stash, prefs);
		/// Declare the desire to nominate `targets` for the origin controller.
		///
		/// Effects will be felt at the beginning of the next era.
		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
		///
		/// # <weight>
		/// - The transaction's complexity is proportional to the size of `targets`,
		/// which is capped at `MAX_NOMINATIONS`.
		/// - Both the reads and writes follow a similar pattern.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(750_000)]
		fn nominate(origin, targets: Vec<<T::Lookup as StaticLookup>::Source>) {
			let controller = ensure_signed(origin)?;
			let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			let stash = &ledger.stash;
			ensure!(!targets.is_empty(), Error::<T>::EmptyTargets);
			let targets = targets.into_iter()
				.take(MAX_NOMINATIONS)
				.map(|t| T::Lookup::lookup(t))
				.collect::<result::Result<Vec<T::AccountId>, _>>()?;
			let nominations = Nominations {
				targets,
Gavin Wood's avatar
Gavin Wood committed
				// initial nominations are considered submitted at era 0. See `Nominations` doc
				submitted_in: Self::current_era().unwrap_or(0),
			<Validators<T>>::remove(stash);
			<Nominators<T>>::insert(stash, &nominations);
		/// Declare no desire to either validate or nominate.
		/// Effects will be felt at the beginning of the next era.
		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
		///
		/// # <weight>
		/// - Independent of the arguments. Insignificant complexity.
		/// - Contains one read.
		/// - Writes are limited to the `origin` account key.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
		fn chill(origin) {
			let controller = ensure_signed(origin)?;
			let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			Self::chill_stash(&ledger.stash);
		/// (Re-)set the payment target for a controller.
		///
		/// Effects will be felt at the beginning of the next era.
		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
		///
		/// # <weight>
		/// - Independent of the arguments. Insignificant complexity.
		/// - Contains a limited number of reads.
		/// - Writes are limited to the `origin` account key.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
		fn set_payee(origin, payee: RewardDestination) {
			let controller = ensure_signed(origin)?;
			let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			let stash = &ledger.stash;
			<Payee<T>>::insert(stash, payee);
		}

		/// (Re-)set the controller of a stash.
		///
		/// Effects will be felt at the beginning of the next era.
		///
		/// The dispatch origin for this call must be _Signed_ by the stash, not the controller.
		///
		/// # <weight>
		/// - Independent of the arguments. Insignificant complexity.
		/// - Contains a limited number of reads.
		/// - Writes are limited to the `origin` account key.
		/// # </weight>
Kian Peymani's avatar
Kian Peymani committed
		#[weight = SimpleDispatchInfo::FixedNormal(750_000)]
		fn set_controller(origin, controller: <T::Lookup as StaticLookup>::Source) {
			let stash = ensure_signed(origin)?;
			let old_controller = Self::bonded(&stash).ok_or(Error::<T>::NotStash)?;
			let controller = T::Lookup::lookup(controller)?;
			if <Ledger<T>>::contains_key(&controller) {
				Err(Error::<T>::AlreadyPaired)?
			}
			if controller != old_controller {
				<Bonded<T>>::insert(&stash, &controller);
				if let Some(l) = <Ledger<T>>::take(&old_controller) {
					<Ledger<T>>::insert(&controller, l);
				}
		/// The ideal number of validators.
		#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
		fn set_validator_count(origin, #[compact] new: u32) {
			ensure_root(origin)?;
		// ----- Root calls.

		/// Force there to be no new eras indefinitely.
		///
		/// # <weight>
		/// - No arguments.
		/// # </weight>
		#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
		fn force_no_eras(origin) {
			ensure_root(origin)?;
			ForceEra::put(Forcing::ForceNone);
		}

		/// Force there to be a new era at the end of the next session. After this, it will be
		/// reset to normal (non-forced) behaviour.
		/// - No arguments.
		#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
		fn force_new_era(origin) {
			ensure_root(origin)?;
			ForceEra::put(Forcing::ForceNew);
		/// Set the validators who cannot be slashed (if any).
		#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
		fn set_invulnerables(origin, validators: Vec<T::AccountId>) {
			ensure_root(origin)?;
			<Invulnerables<T>>::put(validators);

		/// Force a current staker to become completely unstaked, immediately.
		#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
		fn force_unstake(origin, stash: T::AccountId) {
			ensure_root(origin)?;
Gavin Wood's avatar
Gavin Wood committed
			// remove all staking-related information.
			Self::kill_stash(&stash)?;

			// remove the lock.
			T::Currency::remove_lock(STAKING_ID, &stash);
		}

		/// Force there to be a new era at the end of sessions indefinitely.
		///
		/// # <weight>
		/// - One storage write
		/// # </weight>
		#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
		fn force_new_era_always(origin) {
			ensure_root(origin)?;
			ForceEra::put(Forcing::ForceAlways);
		}

		/// Cancel enactment of a deferred slash. Can be called by either the root origin or
		/// the `T::SlashCancelOrigin`.
		/// passing the era and indices of the slashes for that era to kill.
		///
		/// # <weight>
		/// - One storage write.
		/// # </weight>
		#[weight = SimpleDispatchInfo::FixedNormal(1_000_000)]
		fn cancel_deferred_slash(origin, era: EraIndex, slash_indices: Vec<u32>) {
			T::SlashCancelOrigin::try_origin(origin)
				.map(|_| ())
			ensure!(!slash_indices.is_empty(), Error::<T>::EmptyTargets);
			ensure!(Self::is_sorted_and_unique(&slash_indices), Error::<T>::NotSortedAndUnique);

			let mut unapplied = <Self as Store>::UnappliedSlashes::get(&era);
			let last_item = slash_indices[slash_indices.len() - 1];
			ensure!((last_item as usize) < unapplied.len(), Error::<T>::InvalidSlashIndex);

			for (removed, index) in slash_indices.into_iter().enumerate() {
				let index = (index as usize) - removed;
				unapplied.remove(index);
			}

			<Self as Store>::UnappliedSlashes::insert(&era, &unapplied);
		}
Gavin Wood's avatar
Gavin Wood committed
		/// Make one nominator's payout for one era.
		///
		/// - `who` is the controller account of the nominator to pay out.
		/// - `era` may not be lower than one following the most recently paid era. If it is higher,
		///   then it indicates an instruction to skip the payout of all previous eras.
		/// - `validators` is the list of all validators that `who` had exposure to during `era`,
		///   alongside the index of `who` in the clipped exposure of the validator.
		///   I.e. each element is a tuple of
		///   `(validator, index of `who` in clipped exposure of validator)`.
Gavin Wood's avatar
Gavin Wood committed
		///   If it is incomplete, then less than the full reward will be paid out.
		///   It must not exceed `MAX_NOMINATIONS`.
		///
		/// WARNING: once an era is payed for a validator such validator can't claim the payout of
		/// previous era.
		///
		/// WARNING: Incorrect arguments here can result in loss of payout. Be very careful.
		///
		/// # <weight>
		/// - Number of storage read of `O(validators)`; `validators` is the argument of the call,
		///   and is bounded by `MAX_NOMINATIONS`.
		/// - Each storage read is `O(N)` size and decode complexity; `N` is the  maximum
		///   nominations that can be given to a single validator.
		/// - Computation complexity: `O(MAX_NOMINATIONS * logN)`; `MAX_NOMINATIONS` is the
		///   maximum number of validators that may be nominated by a single nominator, it is
		///   bounded only economically (all nominators are required to place a minimum stake).
		/// # </weight>
		#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
		fn payout_nominator(origin, era: EraIndex, validators: Vec<(T::AccountId, u32)>)
			-> DispatchResult
		{
			let who = ensure_signed(origin)?;
			Self::do_payout_nominator(who, era, validators)
		}

		/// Make one validator's payout for one era.
		///
		/// - `who` is the controller account of the validator to pay out.
		/// - `era` may not be lower than one following the most recently paid era. If it is higher,
		///   then it indicates an instruction to skip the payout of all previous eras.
		///
		/// WARNING: once an era is payed for a validator such validator can't claim the payout of
		/// previous era.
		///
		/// WARNING: Incorrect arguments here can result in loss of payout. Be very careful.
		///
		/// # <weight>
		/// - Time complexity: O(1).
		/// - Contains a limited number of reads and writes.
		/// # </weight>
		#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
		fn payout_validator(origin, era: EraIndex) -> DispatchResult {
			let who = ensure_signed(origin)?;
			Self::do_payout_validator(who, era)
		}

Gavin Wood's avatar
Gavin Wood committed
		/// Rebond a portion of the stash scheduled to be unlocked.
		///
		/// # <weight>
		/// - Time complexity: O(1). Bounded by `MAX_UNLOCKING_CHUNKS`.
		/// - Storage changes: Can't increase storage, only decrease it.
		/// # </weight>
		#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
		fn rebond(origin, #[compact] value: BalanceOf<T>) {
			let controller = ensure_signed(origin)?;
			let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
			ensure!(
				!ledger.unlocking.is_empty(),
Gavin Wood's avatar
Gavin Wood committed
				Error::<T>::NoUnlockChunk,
			);

			let ledger = ledger.rebond(value);
			Self::update_ledger(&controller, &ledger);
		}
Gavin Wood's avatar
Gavin Wood committed

Gavin Wood's avatar
Gavin Wood committed
		/// Set history_depth value.
		///
		/// Origin must be root.
		#[weight = SimpleDispatchInfo::FixedOperational(500_000)]
		fn set_history_depth(origin, #[compact] new_history_depth: EraIndex) {
			ensure_root(origin)?;
			if let Some(current_era) = Self::current_era() {
				HistoryDepth::mutate(|history_depth| {
					let last_kept = current_era.checked_sub(*history_depth).unwrap_or(0);
					let new_last_kept = current_era.checked_sub(new_history_depth).unwrap_or(0);
					for era_index in last_kept..new_last_kept {
						Self::clear_era_information(era_index);
					}
					*history_depth = new_history_depth
				})
			}
		}

Gavin Wood's avatar
Gavin Wood committed
		/// Remove all data structure concerning a staker/stash once its balance is zero.
		/// This is essentially equivalent to `withdraw_unbonded` except it can be called by anyone
		/// and the target `stash` must have no funds left.
		///
		/// This can be called from any origin.
		///
		/// - `stash`: The stash account to reap. Its balance must be zero.
		fn reap_stash(_origin, stash: T::AccountId) {
			ensure!(T::Currency::total_balance(&stash).is_zero(), Error::<T>::FundedTarget);
			Self::kill_stash(&stash)?;
			T::Currency::remove_lock(STAKING_ID, &stash);
		}
impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
	fn migrate_account(a: &T::AccountId) {
		if let Some(controller) = Bonded::<T>::migrate_key_from_blake(a) {
			Ledger::<T>::migrate_key_from_blake(controller);
			Payee::<T>::migrate_key_from_blake(a);
			Validators::<T>::migrate_key_from_blake(a);
			Nominators::<T>::migrate_key_from_blake(a);
			SlashingSpans::<T>::migrate_key_from_blake(a);
		}
	}
}

fn migrate<T: Trait>() {
	if let Some(current_era) = CurrentEra::get() {
		let history_depth = HistoryDepth::get();
		for era in current_era.saturating_sub(history_depth)..=current_era {
			ErasStartSessionIndex::migrate_key_from_blake(era);
			ErasValidatorReward::<T>::migrate_key_from_blake(era);
			ErasRewardPoints::<T>::migrate_key_from_blake(era);
			ErasTotalStake::<T>::migrate_key_from_blake(era);
		}
	}
}

Gav Wood's avatar
Gav Wood committed
impl<T: Trait> Module<T> {
	// PUBLIC IMMUTABLES

	/// The total balance that can be slashed from a stash account as of right now.
	pub fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {
		Self::bonded(stash).and_then(Self::ledger).map(|l| l.active).unwrap_or_default()
	/// Check that list is sorted and has no duplicates.
	fn is_sorted_and_unique(list: &Vec<u32>) -> bool {
		list.windows(2).all(|w| w[0] < w[1])
	}

Gavin Wood's avatar
Gavin Wood committed
	fn do_payout_nominator(who: T::AccountId, era: EraIndex, validators: Vec<(T::AccountId, u32)>)
		-> DispatchResult
	{
		// validators len must not exceed `MAX_NOMINATIONS` to avoid querying more validator
		// exposure than necessary.
		if validators.len() > MAX_NOMINATIONS {
			return Err(Error::<T>::InvalidNumberOfNominations.into());
		}

		// Note: if era has no reward to be claimed, era may be future. better not to update
		// `nominator_ledger.last_reward` in this case.
		let era_payout = <ErasValidatorReward<T>>::get(&era)
			.ok_or_else(|| Error::<T>::InvalidEraToReward)?;

		let mut nominator_ledger = <Ledger<T>>::get(&who).ok_or_else(|| Error::<T>::NotController)?;

		if nominator_ledger.last_reward.map(|last_reward| last_reward >= era).unwrap_or(false) {
			return Err(Error::<T>::InvalidEraToReward.into());
		}

		nominator_ledger.last_reward = Some(era);
		<Ledger<T>>::insert(&who, &nominator_ledger);

		let mut reward = Perbill::zero();
		let era_reward_points = <ErasRewardPoints<T>>::get(&era);

		for (validator, nominator_index) in validators.into_iter() {
			let commission = Self::eras_validator_prefs(&era, &validator).commission;
			let validator_exposure = <ErasStakersClipped<T>>::get(&era, &validator);

			if let Some(nominator_exposure) = validator_exposure.others
				.get(nominator_index as usize)
			{
				if nominator_exposure.who != nominator_ledger.stash {
					continue;
				}

				let nominator_exposure_part = Perbill::from_rational_approximation(
					nominator_exposure.value,
					validator_exposure.total,
				);
				let validator_point = era_reward_points.individual.get(&validator)
					.map(|points| *points)
					.unwrap_or_else(|| Zero::zero());
				let validator_point_part = Perbill::from_rational_approximation(
					validator_point,
					era_reward_points.total,
				);
				reward = reward.saturating_add(
					validator_point_part
						.saturating_mul(Perbill::one().saturating_sub(commission))
						.saturating_mul(nominator_exposure_part)
				);
			}
		}

		if let Some(imbalance) = Self::make_payout(&nominator_ledger.stash, reward * era_payout) {
			Self::deposit_event(RawEvent::Reward(who, imbalance.peek()));
		}

		Ok(())
	}

	fn do_payout_validator(who: T::AccountId, era: EraIndex) -> DispatchResult {
		// Note: if era has no reward to be claimed, era may be future. better not to update
		// `ledger.last_reward` in this case.
		let era_payout = <ErasValidatorReward<T>>::get(&era)
			.ok_or_else(|| Error::<T>::InvalidEraToReward)?;

		let mut ledger = <Ledger<T>>::get(&who).ok_or_else(|| Error::<T>::NotController)?;
		if ledger.last_reward.map(|last_reward| last_reward >= era).unwrap_or(false) {
			return Err(Error::<T>::InvalidEraToReward.into());
		}

		ledger.last_reward = Some(era);
		<Ledger<T>>::insert(&who, &ledger);

		let era_reward_points = <ErasRewardPoints<T>>::get(&era);
		let commission = Self::eras_validator_prefs(&era, &ledger.stash).commission;
		let exposure = <ErasStakersClipped<T>>::get(&era, &ledger.stash);
Gavin Wood's avatar
Gavin Wood committed

		let exposure_part = Perbill::from_rational_approximation(
			exposure.own,
			exposure.total,
		);
		let validator_point = era_reward_points.individual.get(&ledger.stash)
			.map(|points| *points)
			.unwrap_or_else(|| Zero::zero());
		let validator_point_part = Perbill::from_rational_approximation(
			validator_point,
			era_reward_points.total,
		);
		let reward = validator_point_part.saturating_mul(
			commission.saturating_add(
				Perbill::one().saturating_sub(commission).saturating_mul(exposure_part)
			)
		);

		if let Some(imbalance) = Self::make_payout(&ledger.stash, reward * era_payout) {
			Self::deposit_event(RawEvent::Reward(who, imbalance.peek()));
		}

		Ok(())
	}

	/// Update the ledger for a controller. This will also update the stash lock. The lock will
	/// will lock the entire funds except paying for further transactions.
	fn update_ledger(
		controller: &T::AccountId,
		ledger: &StakingLedger<T::AccountId, BalanceOf<T>>
	) {
		T::Currency::set_lock(
			STAKING_ID,
			&ledger.stash,
			ledger.total,
			WithdrawReasons::all(),
		<Ledger<T>>::insert(controller, ledger);
	}

	/// Chill a stash account.
	fn chill_stash(stash: &T::AccountId) {
		<Validators<T>>::remove(stash);
		<Nominators<T>>::remove(stash);
	}
	/// Actually make a payment to a staker. This uses the currency's reward function
	/// to pay the right payee for the given staker account.
	fn make_payout(stash: &T::AccountId, amount: BalanceOf<T>) -> Option<PositiveImbalanceOf<T>> {
		let dest = Self::payee(stash);
		match dest {
			RewardDestination::Controller => Self::bonded(stash)
				.and_then(|controller|
					T::Currency::deposit_into_existing(&controller, amount).ok()
				),
			RewardDestination::Stash =>
				T::Currency::deposit_into_existing(stash, amount).ok(),
			RewardDestination::Staked => Self::bonded(stash)
				.and_then(|c| Self::ledger(&c).map(|l| (c, l)))
				.and_then(|(controller, mut l)| {
					l.active += amount;
					l.total += amount;
					let r = T::Currency::deposit_into_existing(stash, amount).ok();
					Self::update_ledger(&controller, &l);
Gavin Wood's avatar
Gavin Wood committed
	/// Plan a new session potentially trigger a new era.
	fn new_session(session_index: SessionIndex) -> Option<Vec<T::AccountId>> {
		if let Some(current_era) = Self::current_era() {
			// Initial era has been set.

			let current_era_start_session_index = Self::eras_start_session_index(current_era)
				.unwrap_or_else(|| {
					frame_support::print("Error: start_session_index must be set for current_era");
					0
				});

			let era_length = session_index.checked_sub(current_era_start_session_index)
				.unwrap_or(0); // Must never happen.

			match ForceEra::get() {
				Forcing::ForceNew => ForceEra::kill(),
				Forcing::ForceAlways => (),
				Forcing::NotForcing if era_length >= T::SessionsPerEra::get() => (),
				_ => return None,
Gav Wood's avatar
Gav Wood committed
			}
Gavin Wood's avatar
Gavin Wood committed
			Self::new_era(session_index)
		} else {
			// Set initial era
			Self::new_era(session_index)
Gavin Wood's avatar
Gavin Wood committed
	/// Start a session potentially starting an era.
	fn start_session(start_session: SessionIndex) {
		let next_active_era = Self::active_era().map(|e| e.index + 1).unwrap_or(0);
		if let Some(next_active_era_start_session_index) =
			Self::eras_start_session_index(next_active_era)
		{
			if next_active_era_start_session_index == start_session {
				Self::start_era(start_session);
			} else if next_active_era_start_session_index < start_session {
				// This arm should never happen, but better handle it than to stall the
				// staking pallet.
				frame_support::print("Warning: A session appears to have been skipped.");
				Self::start_era(start_session);
			}
		}
Gavin Wood's avatar
Gavin Wood committed
	/// End a session potentially ending an era.
	fn end_session(session_index: SessionIndex) {
		if let Some(active_era) = Self::active_era() {
thiolliere's avatar
thiolliere committed
			if let Some(next_active_era_start_session_index) =
Gavin Wood's avatar
Gavin Wood committed
				Self::eras_start_session_index(active_era.index + 1)
thiolliere's avatar
thiolliere committed
			{
				if next_active_era_start_session_index == session_index + 1 {
					Self::end_era(active_era, session_index);
				}
Gavin Wood's avatar
Gavin Wood committed
	}
Gavin Wood's avatar
Gavin Wood committed
	/// * Increment `active_era.index`,
	/// * reset `active_era.start`,
	/// * update `BondedEras` and apply slashes.
	fn start_era(start_session: SessionIndex) {
		let active_era = <ActiveEra<T>>::mutate(|active_era| {
			let new_index = active_era.as_ref().map(|info| info.index + 1).unwrap_or(0);
			*active_era = Some(ActiveEraInfo {
				index: new_index,
				// Set new active era start in next `on_finalize`. To guarantee usage of `Time`
				start: None,
			});
			new_index
Gavin Wood's avatar
Gavin Wood committed

		let bonding_duration = T::BondingDuration::get();

		BondedEras::mutate(|bonded| {
Gavin Wood's avatar
Gavin Wood committed
			bonded.push((active_era, start_session));
Gavin Wood's avatar
Gavin Wood committed
			if active_era > bonding_duration {
				let first_kept = active_era - bonding_duration;

				// prune out everything that's from before the first-kept index.
				let n_to_prune = bonded.iter()
					.take_while(|&&(era_idx, _)| era_idx < first_kept)
					.count();

				// kill slashing metadata.
				for (pruned_era, _) in bonded.drain(..n_to_prune) {
					slashing::clear_era_metadata::<T>(pruned_era);
				}

				if let Some(&(_, first_session)) = bonded.first() {
					T::SessionInterface::prune_historical_up_to(first_session);
				}
Gav Wood's avatar
Gav Wood committed

Gavin Wood's avatar
Gavin Wood committed
		Self::apply_unapplied_slashes(active_era);
	}

	/// Compute payout for era.
	fn end_era(active_era: ActiveEraInfo<MomentOf<T>>, _session_index: SessionIndex) {
		// Note: active_era_start can be None if end era is called during genesis config.
		if let Some(active_era_start) = active_era.start {
			let now = T::Time::now();

			let era_duration = now - active_era_start;
			let (total_payout, _max_payout) = inflation::compute_total_payout(
				&T::RewardCurve::get(),
				Self::eras_total_stake(&active_era.index),
				T::Currency::total_issuance(),
				// Duration of era; more than u64::MAX is rewarded as u64::MAX.
				era_duration.saturated_into::<u64>(),
			);

			// Set ending era reward.
			<ErasValidatorReward<T>>::insert(&active_era.index, total_payout);
		}
	}

	/// Plan a new era. Return the potential new staking set.
	fn new_era(start_session_index: SessionIndex) -> Option<Vec<T::AccountId>> {
		// Increment or set current era.
		let current_era = CurrentEra::mutate(|s| {
			*s = Some(s.map(|s| s + 1).unwrap_or(0));
			s.unwrap()
		});
		ErasStartSessionIndex::insert(&current_era, &start_session_index);

		// Clean old era information.
		if let Some(old_era) = current_era.checked_sub(Self::history_depth() + 1) {
			Self::clear_era_information(old_era);
		}

		// Set staking information for new era.
		let maybe_new_validators = Self::select_validators(current_era);

		maybe_new_validators
Gavin Wood's avatar
Gavin Wood committed
	/// Clear all era information for given era.
	fn clear_era_information(era_index: EraIndex) {
		<ErasStakers<T>>::remove_prefix(era_index);
		<ErasStakersClipped<T>>::remove_prefix(era_index);
		<ErasValidatorPrefs<T>>::remove_prefix(era_index);
		<ErasValidatorReward<T>>::remove(era_index);
		<ErasRewardPoints<T>>::remove(era_index);
		<ErasTotalStake<T>>::remove(era_index);
		ErasStartSessionIndex::remove(era_index);
	}

	/// Apply previously-unapplied slashes on the beginning of a new era, after a delay.
Gavin Wood's avatar
Gavin Wood committed
	fn apply_unapplied_slashes(active_era: EraIndex) {
		let slash_defer_duration = T::SlashDeferDuration::get();
		<Self as Store>::EarliestUnappliedSlash::mutate(|earliest| if let Some(ref mut earliest) = earliest {
Gavin Wood's avatar
Gavin Wood committed
			let keep_from = active_era.saturating_sub(slash_defer_duration);
			for era in (*earliest)..keep_from {
				let era_slashes = <Self as Store>::UnappliedSlashes::take(&era);
				for slash in era_slashes {
					slashing::apply_slash::<T>(slash);
				}
			}

			*earliest = (*earliest).max(keep_from)
		})
	}

Gavin Wood's avatar
Gavin Wood committed
	/// Select a new validator set from the assembled stakers and their role preferences, and store
	/// staking information for the new current era.
	///
	/// Fill the storages `ErasStakers`, `ErasStakersClipped`, `ErasValidatorPrefs` and
	/// `ErasTotalStake` for current era.
Gavin Wood's avatar
Gavin Wood committed
	/// Returns a set of newly selected _stash_ IDs.
	///
	/// Assumes storage is coherent with the declaration.
Gavin Wood's avatar
Gavin Wood committed
	fn select_validators(current_era: EraIndex) -> Option<Vec<T::AccountId>> {
		let mut all_nominators: Vec<(T::AccountId, Vec<T::AccountId>)> = Vec::new();
Gavin Wood's avatar
Gavin Wood committed
		let mut all_validators_and_prefs = BTreeMap::new();
		let mut all_validators = Vec::new();
		for (validator, preference) in <Validators<T>>::iter() {
Gavin Wood's avatar
Gavin Wood committed
			let self_vote = (validator.clone(), vec![validator.clone()]);
			all_nominators.push(self_vote);
Gavin Wood's avatar
Gavin Wood committed
			all_validators_and_prefs.insert(validator.clone(), preference);
			all_validators.push(validator);
		}
		let nominator_votes = <Nominators<T>>::iter().map(|(nominator, nominations)| {
			let Nominations { submitted_in, mut targets, suppressed: _ } = nominations;

			// Filter out nomination targets which were nominated before the most recent
			targets.retain(|stash| {
				<Self as Store>::SlashingSpans::get(&stash).map_or(
					true,
					|spans| submitted_in >= spans.last_nonzero_slash(),
				)
			});

			(nominator, targets)
		});
		all_nominators.extend(nominator_votes);

Kian Paimani's avatar
Kian Paimani committed
		let maybe_phragmen_result = sp_phragmen::elect::<_, _, _, T::CurrencyToVote, Perbill>(
			Self::validator_count() as usize,
			Self::minimum_validator_count().max(1) as usize,
			all_validators,
			all_nominators,
			Self::slashable_balance_of,
		if let Some(phragmen_result) = maybe_phragmen_result {
Gavin Wood's avatar
Gavin Wood committed
			let elected_stashes = phragmen_result.winners.into_iter()
				.map(|(s, _)| s)
				.collect::<Vec<T::AccountId>>();
			let assignments = phragmen_result.assignments;
			let to_balance = |e: ExtendedBalance|
				<T::CurrencyToVote as Convert<ExtendedBalance, BalanceOf<T>>>::convert(e);
Kian Paimani's avatar
Kian Paimani committed
			let supports = sp_phragmen::build_support_map::<_, _, _, T::CurrencyToVote, Perbill>(
				&elected_stashes,
				&assignments,
				Self::slashable_balance_of,
			);
Gavin Wood's avatar
Gavin Wood committed
			// Populate stakers information and figure out the total stake.
			let mut total_staked = BalanceOf::<T>::zero();
			for (c, s) in supports.into_iter() {
				// build `struct exposure` from `support`
				let mut others = Vec::new();
				let mut own: BalanceOf<T> = Zero::zero();
				let mut total: BalanceOf<T> = Zero::zero();
				s.voters
					.into_iter()
					.map(|(who, value)| (who, to_balance(value)))
					.for_each(|(who, value)| {
						if who == c {
							own = own.saturating_add(value);
						} else {
							others.push(IndividualExposure { who, value });
						}
						total = total.saturating_add(value);
					});
Gavin Wood's avatar
Gavin Wood committed

				total_staked = total_staked.saturating_add(total);

				let exposure = Exposure {
					// This might reasonably saturate and we cannot do much about it. The sum of
					// someone's stake might exceed the balance type if they have the maximum amount
					// of balance and receive some support. This is super unlikely to happen, yet
					// we simulate it in some tests.
Gavin Wood's avatar
Gavin Wood committed
				<ErasStakers<T>>::insert(&current_era, &c, &exposure);
Gavin Wood's avatar
Gavin Wood committed
				let mut exposure_clipped = exposure;
				let clipped_max_len = T::MaxNominatorRewardedPerValidator::get() as usize;
				if exposure_clipped.others.len() > clipped_max_len {
					exposure_clipped.others.sort_unstable_by(|a, b| a.value.cmp(&b.value).reverse());
					exposure_clipped.others.truncate(clipped_max_len);
Gavin Wood's avatar
Gavin Wood committed
				<ErasStakersClipped<T>>::insert(&current_era, &c, exposure_clipped);
Gavin Wood's avatar
Gavin Wood committed
			// Insert current era staking informations
			<ErasTotalStake<T>>::insert(&current_era, total_staked);
			let default_pref = ValidatorPrefs::default();
			for stash in &elected_stashes {
				let pref = all_validators_and_prefs.get(stash)
					.unwrap_or(&default_pref); // Must never happen, but better to be safe.
				<ErasValidatorPrefs<T>>::insert(&current_era, stash, pref);
			}
			// In order to keep the property required by `n_session_ending`
			// that we must return the new validator set even if it's the same as the old,
			// as long as any underlying economic conditions have changed, we don't attempt
			// to do any optimization where we compare against the prior set.
Gavin Wood's avatar
Gavin Wood committed
			Some(elected_stashes)
		} else {
			// There were not enough candidates for even our minimal level of functionality.
			// This is bad.
			// We should probably disable all functionality except for block production
			// and let the chain keep producing blocks until we can decide on a sufficiently
			// substantial set.
			// TODO: #2494
Gavin Wood's avatar
Gavin Wood committed
			None
Gav Wood's avatar
Gav Wood committed
	}
	/// Remove all associated data of a stash account from the staking system.
	///
	/// Assumes storage is upgraded before calling.
	///
Gavin Wood's avatar
Gavin Wood committed
	/// This is called:
	/// - after a `withdraw_unbond()` call that frees all of a stash's bonded balance.
Gavin Wood's avatar
Gavin Wood committed
	/// - through `reap_stash()` if the balance has fallen to zero (through slashing).
	fn kill_stash(stash: &T::AccountId) -> DispatchResult {
		let controller = Bonded::<T>::take(stash).ok_or(Error::<T>::NotStash)?;
		<Ledger<T>>::remove(&controller);

		<Payee<T>>::remove(stash);
		<Validators<T>>::remove(stash);
		<Nominators<T>>::remove(stash);

		slashing::clear_stash_metadata::<T>(stash);
Gavin Wood's avatar
Gavin Wood committed

		system::Module::<T>::dec_ref(stash);

		Ok(())
thiolliere's avatar
thiolliere committed
	/// Add reward points to validators using their stash account ID.
	///
	/// Validators are keyed by stash account ID and must be in the current elected set.
	///
	/// For each element in the iterator the given number of points in u32 is added to the
	/// validator, thus duplicates are handled.
	///
	/// At the end of the era each the total payout will be distributed among validator
	/// relatively to their points.
thiolliere's avatar
thiolliere committed
	///
	/// COMPLEXITY: Complexity is `number_of_validator_to_reward x current_elected_len`.
	/// If you need to reward lots of validator consider using `reward_by_indices`.
Gavin Wood's avatar
Gavin Wood committed
	pub fn reward_by_ids(
		validators_points: impl IntoIterator<Item = (T::AccountId, u32)>
	) {
		if let Some(active_era) = Self::active_era() {
			<ErasRewardPoints<T>>::mutate(active_era.index, |era_rewards| {
				for (validator, points) in validators_points.into_iter() {
					*era_rewards.individual.entry(validator).or_default() += points;