Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Staking: store last min-active-bond on-chain
Storing the `min-active-bond` onchain helps the UIs with minimal on-chain costs.

Closes #12746
  • Loading branch information
gpestana committed Dec 9, 2022
commit bff8df5fb854a73756eebae6c991fb2f8fd16475
8 changes: 8 additions & 0 deletions 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 Down Expand Up @@ -793,6 +795,12 @@ impl<T: Config> Pallet<T> {
nominators_taken
);

// `all_voters` is sorted by vote weight, so we use the last element to set the current
// minimum active bond. Note that depending on the `T::VoterList` list, the sorting may be
// inconsistent as the sorting may be lazy.
let min_active_bond: T::CurrencyBalance = all_voters.last().map_or(0, |v| v.1).into();
LastMinimumActiveBond::<T>::put(min_active_bond);

all_voters
}

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));
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