Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up nominator state checks in staking pallet #2153

Merged
merged 4 commits into from
Nov 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading