Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Staking: store minimum active stake on-chain #12889

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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: 17 additions & 1 deletion frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ impl<T: Config> Pallet<T> {
///
/// `maybe_max_len` can imposes a cap on the number of voters returned;
///
/// Sets `LastMinimumActiveBond` to the minimum active bond in the returned set of nominators.
///
/// This function is self-weighing as [`DispatchClass::Mandatory`].
///
/// ### Slashing
Expand All @@ -727,6 +729,7 @@ impl<T: Config> Pallet<T> {
let mut nominators_taken = 0u32;

let mut sorted_voters = T::VoterList::iter();
let mut min_active_bond = None;
gpestana marked this conversation as resolved.
Show resolved Hide resolved
gpestana marked this conversation as resolved.
Show resolved Hide resolved
while all_voters.len() < max_allowed_len &&
voters_seen < (NPOS_MAX_ITERATIONS_COEFFICIENT * max_allowed_len as u32)
{
Expand All @@ -747,10 +750,19 @@ impl<T: Config> Pallet<T> {
.get(stash)
.map_or(true, |spans| submitted_in >= spans.last_nonzero_slash())
});
let voter_weight = weight_of(&voter);
if !targets.len().is_zero() {
all_voters.push((voter.clone(), weight_of(&voter), targets));
all_voters.push((voter.clone(), voter_weight, targets));
nominators_taken.saturating_inc();
}

min_active_bond = min_active_bond.map_or(Some(voter_weight), |current_min| {
if voter_weight < current_min {
Some(voter_weight)
} else {
Some(current_min)
}
});
} else if Validators::<T>::contains_key(&voter) {
// if this voter is a validator:
let self_vote = (
Expand Down Expand Up @@ -785,6 +797,10 @@ impl<T: Config> Pallet<T> {
slashing_spans.len() as u32,
));

LastMinimumActiveBond::<T>::put::<T::CurrencyBalance>(
min_active_bond.map_or(0, |v| v).into(),
);
gpestana marked this conversation as resolved.
Show resolved Hide resolved

log!(
info,
"generated {} npos voters, {} from validators and {} nominators",
Expand Down
4 changes: 4 additions & 0 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ pub mod pallet {
#[pallet::storage]
pub type MinValidatorBond<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

/// The minimum active bond of the last successful election.
#[pallet::storage]
pub type LastMinimumActiveBond<T> = StorageValue<_, BalanceOf<T>, ValueQuery>;

/// The minimum amount of commission that validators can set.
///
/// If set to `0`, no limit exists.
Expand Down
18 changes: 18 additions & 0 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4398,6 +4398,24 @@ mod election_data_provider {
);
}

#[test]
fn set_last_minimum_nominator_bond_is_correct() {
ExtBuilder::default()
.nominate(false)
.add_staker(61, 60, 2_000, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(71, 70, 10, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(81, 80, 50, StakerStatus::<AccountId>::Nominator(vec![21]))
.build_and_execute(|| {
assert_ok!(<Staking as ElectionDataProvider>::electing_voters(None));
gpestana marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(LastMinimumActiveBond::<Test>::get(), 10);

// remove staker with lower bond by limiting the number of voters and check
// `LastMinimumActiveBond` again after electing voters.
assert_ok!(<Staking as ElectionDataProvider>::electing_voters(Some(5)));
assert_eq!(LastMinimumActiveBond::<Test>::get(), 50);
});
}

#[test]
fn voters_include_self_vote() {
ExtBuilder::default().nominate(false).build_and_execute(|| {
Expand Down