Skip to content

Commit

Permalink
Speed up nominator state checks in staking pallet (paritytech#2153)
Browse files Browse the repository at this point in the history
Should help paritytech#234.
Related to paritytech#2020 and
paritytech#2108.

Refactors and improves running time for try runtime checks for staking
pallet.

Tested on westend on my M2 pro: running time drops from 90 seconds to 7
seconds.
  • Loading branch information
Ank4n authored Nov 4, 2023
1 parent 99daa09 commit 7a1dd0d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions substrate/frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,13 @@ impl<T: Config> Pallet<T> {
// a check per nominator to ensure their entire stake is correctly distributed. Will only
// kick-in if the nomination was submitted before the current era.
let era = Self::active_era().unwrap().index;

// cache era exposures to avoid too many db reads.
let era_exposures = T::SessionInterface::validators()
.iter()
.map(|v| Self::eras_stakers(era, v))
.collect::<Vec<_>>();

<Nominators<T>>::iter()
.filter_map(
|(nominator, nomination)| {
Expand All @@ -1878,9 +1885,8 @@ impl<T: Config> Pallet<T> {
// must be bonded.
Self::ensure_is_stash(&nominator)?;
let mut sum = BalanceOf::<T>::zero();
T::SessionInterface::validators()
era_exposures
.iter()
.map(|v| Self::eras_stakers(era, v))
.map(|e| -> Result<(), TryRuntimeError> {
let individual =
e.others.iter().filter(|e| e.who == nominator).collect::<Vec<_>>();
Expand All @@ -1896,6 +1902,14 @@ impl<T: Config> Pallet<T> {
Ok(())
})
.collect::<Result<Vec<_>, _>>()?;

// We take total instead of active as the nominator might have requested to unbond
// some of their stake that is still exposed in the current era.
if sum <= Self::ledger(Stash(nominator.clone()))?.total {
// This can happen when there is a slash in the current era so we only warn.
log!(warn, "nominator stake exceeds what is bonded.");
}

Ok(())
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down

0 comments on commit 7a1dd0d

Please sign in to comment.