Skip to content

Commit

Permalink
mock & chain_spec: remove ctrl from stakers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed May 26, 2024
1 parent ddb8256 commit 714f2f9
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 104 deletions.
4 changes: 2 additions & 2 deletions polkadot/runtime/common/src/try_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
let mut unstaked_err = 0;
let mut unstaked_slashed = 0;

let all_stakers = Ledger::<T>::iter().map(|(ctrl, l)| (ctrl, l.stash)).collect::<BTreeSet<_>>();
let all_stakers = Ledger::<T>::iter().map(|(stash, l)| l.stash).collect::<BTreeSet<_>>();
let mut all_exposed = BTreeSet::new();
ErasStakers::<T>::iter().for_each(|(_, val, expo)| {
all_exposed.insert(val);
Expand All @@ -43,7 +43,7 @@ where

let eligible = all_stakers
.iter()
.filter_map(|(ctrl, stash)| all_exposed.contains(stash).then_some(ctrl))
.filter_map(|stash| all_exposed.contains(stash).then_some(stash))
.collect::<Vec<_>>();

log::info!(
Expand Down
18 changes: 3 additions & 15 deletions substrate/bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ pub fn authority_keys_from_seed(

fn configure_accounts(
initial_authorities: Vec<(
AccountId,
AccountId,
GrandpaId,
BabeId,
Expand All @@ -293,19 +292,10 @@ fn configure_accounts(
endowed_accounts: Option<Vec<AccountId>>,
stash: Balance,
) -> (
Vec<(
AccountId,
AccountId,
GrandpaId,
BabeId,
ImOnlineId,
AuthorityDiscoveryId,
MixnetId,
BeefyId,
)>,
Vec<(AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId, BeefyId)>,
Vec<AccountId>,
usize,
Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
Vec<(AccountId, Balance, StakerStatus<AccountId>)>,
) {
let mut endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(|| {
vec![
Expand Down Expand Up @@ -338,7 +328,7 @@ fn configure_accounts(
let mut rng = rand::thread_rng();
let stakers = initial_authorities
.iter()
.map(|x| (x.0.clone(), x.0.clone(), stash, StakerStatus::Validator))
.map(|x| (x.0.clone(), stash, StakerStatus::Validator))
.chain(initial_nominators.iter().map(|x| {
use rand::{seq::SliceRandom, Rng};
let limit = (MaxNominations::get() as usize).min(initial_authorities.len());
Expand All @@ -361,7 +351,6 @@ fn configure_accounts(
/// Helper function to create RuntimeGenesisConfig json patch for testing.
pub fn testnet_genesis(
initial_authorities: Vec<(
AccountId,
AccountId,
GrandpaId,
BabeId,
Expand All @@ -386,7 +375,6 @@ pub fn testnet_genesis(
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
session_keys(
x.2.clone(),
Expand Down
21 changes: 3 additions & 18 deletions substrate/frame/delegated-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,9 @@ impl ExtBuilder {
.assimilate_storage(&mut storage);

let stakers = vec![
(
GENESIS_VALIDATOR,
GENESIS_VALIDATOR,
1000,
sp_staking::StakerStatus::<AccountId>::Validator,
),
(
GENESIS_NOMINATOR_ONE,
GENESIS_NOMINATOR_ONE,
100,
sp_staking::StakerStatus::<AccountId>::Nominator(vec![1]),
),
(
GENESIS_NOMINATOR_TWO,
GENESIS_NOMINATOR_TWO,
200,
sp_staking::StakerStatus::<AccountId>::Nominator(vec![1]),
),
(GENESIS_VALIDATOR, 1000, sp_staking::StakerStatus::<AccountId>::Validator),
(GENESIS_NOMINATOR_ONE, 100, sp_staking::StakerStatus::<AccountId>::Nominator(vec![1])),
(GENESIS_NOMINATOR_TWO, 200, sp_staking::StakerStatus::<AccountId>::Nominator(vec![1])),
];

let _ = pallet_staking::GenesisConfig::<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,27 +394,27 @@ pub struct StakingExtBuilder {
min_validator_bond: Balance,
status: BTreeMap<AccountId, StakerStatus<AccountId>>,
stakes: BTreeMap<AccountId, Balance>,
stakers: Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
stakers: Vec<(AccountId, Balance, StakerStatus<AccountId>)>,
}

impl Default for StakingExtBuilder {
fn default() -> Self {
let stakers = vec![
// (stash, ctrl, stake, status)
// (stash, stake, status)
// these two will be elected in the default test where we elect 2.
(11, 11, 1000, StakerStatus::<AccountId>::Validator),
(21, 21, 1000, StakerStatus::<AccountId>::Validator),
(11, 1000, StakerStatus::<AccountId>::Validator),
(21, 1000, StakerStatus::<AccountId>::Validator),
// loser validators if validator_count() is default.
(31, 31, 500, StakerStatus::<AccountId>::Validator),
(41, 41, 1500, StakerStatus::<AccountId>::Validator),
(51, 51, 1500, StakerStatus::<AccountId>::Validator),
(61, 61, 1500, StakerStatus::<AccountId>::Validator),
(71, 71, 1500, StakerStatus::<AccountId>::Validator),
(81, 81, 1500, StakerStatus::<AccountId>::Validator),
(91, 91, 1500, StakerStatus::<AccountId>::Validator),
(101, 101, 500, StakerStatus::<AccountId>::Validator),
(31, 500, StakerStatus::<AccountId>::Validator),
(41, 1500, StakerStatus::<AccountId>::Validator),
(51, 1500, StakerStatus::<AccountId>::Validator),
(61, 1500, StakerStatus::<AccountId>::Validator),
(71, 1500, StakerStatus::<AccountId>::Validator),
(81, 1500, StakerStatus::<AccountId>::Validator),
(91, 1500, StakerStatus::<AccountId>::Validator),
(101, 500, StakerStatus::<AccountId>::Validator),
// an idle validator
(201, 201, 1000, StakerStatus::<AccountId>::Idle),
(201, 1000, StakerStatus::<AccountId>::Idle),
];

Self {
Expand Down Expand Up @@ -493,6 +493,7 @@ impl Default for BalancesExtBuilder {
(3, 300),
(4, 400),
// controllers (still used in some tests. Soon to be deprecated).
// TODO: Ensure these are not being used in tests & remove.
(10, 100),
(20, 100),
(30, 100),
Expand Down Expand Up @@ -554,15 +555,15 @@ impl ExtBuilder {

let mut stakers = self.staking_builder.stakers.clone();
self.staking_builder.status.clone().into_iter().for_each(|(stash, status)| {
let (_, _, _, ref mut prev_status) = stakers
let (_, _, ref mut prev_status) = stakers
.iter_mut()
.find(|s| s.0 == stash)
.expect("set_status staker should exist; qed");
*prev_status = status;
});
// replaced any of the stakes if needed.
self.staking_builder.stakes.clone().into_iter().for_each(|(stash, stake)| {
let (_, _, ref mut prev_stake, _) = stakers
let (_, ref mut prev_stake, _) = stakers
.iter_mut()
.find(|s| s.0 == stash)
.expect("set_stake staker should exits; qed.");
Expand Down
10 changes: 5 additions & 5 deletions substrate/frame/root-offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ impl ExtBuilder {
.unwrap();

let stakers = vec![
// (stash, ctrl, stake, status)
// (stash, stake, status)
// these two will be elected in the default test where we elect 2.
(11, 11, 1000, StakerStatus::<AccountId>::Validator),
(21, 21, 1000, StakerStatus::<AccountId>::Validator),
(11, 1000, StakerStatus::<AccountId>::Validator),
(21, 1000, StakerStatus::<AccountId>::Validator),
// a loser validator
(31, 31, 500, StakerStatus::<AccountId>::Validator),
(31, 500, StakerStatus::<AccountId>::Validator),
// an idle validator
(41, 41, 1000, StakerStatus::<AccountId>::Idle),
(41, 1000, StakerStatus::<AccountId>::Idle),
];

let _ = pallet_staking::GenesisConfig::<Test> {
Expand Down
27 changes: 12 additions & 15 deletions substrate/frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub struct ExtBuilder {
balance_factor: Balance,
status: BTreeMap<AccountId, StakerStatus<AccountId>>,
stakes: BTreeMap<AccountId, Balance>,
stakers: Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
stakers: Vec<(AccountId, Balance, StakerStatus<AccountId>)>,
}

impl Default for ExtBuilder {
Expand Down Expand Up @@ -424,11 +424,10 @@ impl ExtBuilder {
pub fn add_staker(
mut self,
stash: AccountId,
ctrl: AccountId,
stake: Balance,
status: StakerStatus<AccountId>,
) -> Self {
self.stakers.push((stash, ctrl, stake, status));
self.stakers.push((stash, stake, status));
self
}
pub fn balance_factor(mut self, factor: Balance) -> Self {
Expand Down Expand Up @@ -482,38 +481,36 @@ impl ExtBuilder {
let mut stakers = vec![];
if self.has_stakers {
stakers = vec![
// (stash, ctrl, stake, status)
// TODO: Remove `ctrl` from stakers.
// (stash, stake, status)
// these two will be elected in the default test where we elect 2.
(11, 11, self.balance_factor * 1000, StakerStatus::<AccountId>::Validator),
(21, 21, self.balance_factor * 1000, StakerStatus::<AccountId>::Validator),
(11, self.balance_factor * 1000, StakerStatus::<AccountId>::Validator),
(21, self.balance_factor * 1000, StakerStatus::<AccountId>::Validator),
// a loser validator
(31, 31, self.balance_factor * 500, StakerStatus::<AccountId>::Validator),
(31, self.balance_factor * 500, StakerStatus::<AccountId>::Validator),
// an idle validator
(41, 41, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(51, 51, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(201, 201, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(202, 202, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(41, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(51, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(201, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
(202, self.balance_factor * 1000, StakerStatus::<AccountId>::Idle),
]; // optionally add a nominator
if self.nominate {
stakers.push((
101,
101,
self.balance_factor * 500,
StakerStatus::<AccountId>::Nominator(vec![11, 21]),
))
}
// replace any of the status if needed.
self.status.into_iter().for_each(|(stash, status)| {
let (_, _, _, ref mut prev_status) = stakers
let (_, _, ref mut prev_status) = stakers
.iter_mut()
.find(|s| s.0 == stash)
.expect("set_status staker should exist; qed");
*prev_status = status;
});
// replaced any of the stakes if needed.
self.stakes.into_iter().for_each(|(stash, stake)| {
let (_, _, ref mut prev_stake, _) = stakers
let (_, ref mut prev_stake, _) = stakers
.iter_mut()
.find(|s| s.0 == stash)
.expect("set_stake staker should exits; qed.");
Expand Down
5 changes: 2 additions & 3 deletions substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,7 @@ pub mod pallet {
pub force_era: Forcing,
pub slash_reward_fraction: Perbill,
pub canceled_payout: BalanceOf<T>,
pub stakers:
Vec<(T::AccountId, T::AccountId, BalanceOf<T>, crate::StakerStatus<T::AccountId>)>,
pub stakers: Vec<(T::AccountId, BalanceOf<T>, crate::StakerStatus<T::AccountId>)>,
pub min_nominator_bond: BalanceOf<T>,
pub min_validator_bond: BalanceOf<T>,
pub max_validator_count: Option<u32>,
Expand All @@ -705,7 +704,7 @@ pub mod pallet {
MaxNominatorsCount::<T>::put(x);
}

for &(ref stash, _, balance, ref status) in &self.stakers {
for &(ref stash, balance, ref status) in &self.stakers {
crate::log!(
trace,
"inserting genesis staker: {:?} => {:?} => {:?}",
Expand Down
44 changes: 13 additions & 31 deletions substrate/frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,6 @@ fn switching_roles() {
fn wrong_vote_is_moot() {
ExtBuilder::default()
.add_staker(
61,
61,
500,
StakerStatus::Nominator(vec![
Expand Down Expand Up @@ -5044,9 +5043,9 @@ mod election_data_provider {
fn set_minimum_active_stake_is_correct() {
ExtBuilder::default()
.nominate(false)
.add_staker(61, 61, 2_000, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(71, 71, 10, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(81, 81, 50, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(61, 2_000, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(71, 10, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(81, 50, StakerStatus::<AccountId>::Nominator(vec![21]))
.build_and_execute(|| {
// default bounds are unbounded.
assert_ok!(<Staking as ElectionDataProvider>::electing_voters(
Expand Down Expand Up @@ -5111,7 +5110,7 @@ mod election_data_provider {
ExtBuilder::default()
.has_stakers(true)
.nominate(true)
.add_staker(61, 61, 2_000, StakerStatus::<AccountId>::Nominator(vec![21]))
.add_staker(61, 2_000, StakerStatus::<AccountId>::Nominator(vec![21]))
.build_and_execute(|| {
assert_eq!(Staking::weight_of(&101), 500);
let voters = <Staking as ElectionDataProvider>::electing_voters(
Expand Down Expand Up @@ -5170,24 +5169,9 @@ mod election_data_provider {
.nominate(false)
// the best way to invalidate a bunch of nominators is to have them nominate a lot of
// ppl, but then lower the MaxNomination limit.
.add_staker(
61,
61,
2_000,
StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]),
)
.add_staker(
71,
71,
2_000,
StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]),
)
.add_staker(
81,
81,
2_000,
StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]),
)
.add_staker(61, 2_000, StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]))
.add_staker(71, 2_000, StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]))
.add_staker(81, 2_000, StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]))
.build_and_execute(|| {
let bounds_builder = ElectionBoundsBuilder::default();
// all voters ordered by stake,
Expand Down Expand Up @@ -5335,7 +5319,6 @@ mod election_data_provider {
.nominate(false)
.add_staker(
61,
60,
300, // 300 bond has 16 nomination quota.
StakerStatus::<AccountId>::Nominator(vec![21, 22, 23, 24, 25]),
)
Expand Down Expand Up @@ -5364,7 +5347,6 @@ mod election_data_provider {
.nominate(false)
.add_staker(
71,
70,
333,
StakerStatus::<AccountId>::Nominator(vec![16, 15, 14, 13, 12, 11, 10]),
)
Expand Down Expand Up @@ -5842,8 +5824,8 @@ fn min_commission_works() {
fn change_of_absolute_max_nominations() {
use frame_election_provider_support::ElectionDataProvider;
ExtBuilder::default()
.add_staker(61, 61, 10, StakerStatus::Nominator(vec![1]))
.add_staker(71, 71, 10, StakerStatus::Nominator(vec![1, 2, 3]))
.add_staker(61, 10, StakerStatus::Nominator(vec![1]))
.add_staker(71, 10, StakerStatus::Nominator(vec![1, 2, 3]))
.balance_factor(10)
.build_and_execute(|| {
// pre-condition
Expand Down Expand Up @@ -5940,10 +5922,10 @@ fn change_of_absolute_max_nominations() {
fn nomination_quota_max_changes_decoding() {
use frame_election_provider_support::ElectionDataProvider;
ExtBuilder::default()
.add_staker(60, 61, 10, StakerStatus::Nominator(vec![1]))
.add_staker(70, 71, 10, StakerStatus::Nominator(vec![1, 2, 3]))
.add_staker(30, 330, 10, StakerStatus::Nominator(vec![1, 2, 3, 4]))
.add_staker(50, 550, 10, StakerStatus::Nominator(vec![1, 2, 3, 4]))
.add_staker(60, 10, StakerStatus::Nominator(vec![1]))
.add_staker(70, 10, StakerStatus::Nominator(vec![1, 2, 3]))
.add_staker(30, 10, StakerStatus::Nominator(vec![1, 2, 3, 4]))
.add_staker(50, 10, StakerStatus::Nominator(vec![1, 2, 3, 4]))
.balance_factor(10)
.build_and_execute(|| {
// pre-condition.
Expand Down

0 comments on commit 714f2f9

Please sign in to comment.