Newer
Older
// we take 4 voters
assert_eq!(
Staking::voters(Some(4))
.unwrap()
.iter()
.map(|(stash, _, _)| stash)
.copied()
.collect::<Vec<_>>(),
vec![
31, 11, // 2 validators (21 was slashed)
101, 71 // 2 nominators, excluding 61
],
);
});
#[test]
fn estimate_next_election_works() {
ExtBuilder::default().session_per_era(5).period(5).build_and_execute(|| {
// first session is always length 0.
for b in 1..20 {
run_to_block(b);
assert_eq!(Staking::next_election_prediction(System::block_number()), 20);
}
// election
run_to_block(20);
assert_eq!(Staking::next_election_prediction(System::block_number()), 45);
assert_eq!(staking_events().len(), 1);
Gavin Wood
committed
assert_eq!(*staking_events().last().unwrap(), Event::StakersElected);
for b in 21..45 {
run_to_block(b);
assert_eq!(Staking::next_election_prediction(System::block_number()), 45);
}
// election
run_to_block(45);
assert_eq!(Staking::next_election_prediction(System::block_number()), 70);
assert_eq!(staking_events().len(), 3);
Gavin Wood
committed
assert_eq!(*staking_events().last().unwrap(), Event::StakersElected);
thiolliere
committed
Staking::force_no_eras(Origin::root()).unwrap();
assert_eq!(Staking::next_election_prediction(System::block_number()), u64::MAX);
thiolliere
committed
Staking::force_new_era_always(Origin::root()).unwrap();
assert_eq!(Staking::next_election_prediction(System::block_number()), 45 + 5);
Staking::force_new_era(Origin::root()).unwrap();
assert_eq!(Staking::next_election_prediction(System::block_number()), 45 + 5);
// Do a fail election
MinimumValidatorCount::<Test>::put(1000);
run_to_block(50);
// Election: failed, next session is a new election
assert_eq!(Staking::next_election_prediction(System::block_number()), 50 + 5);
// The new era is still forced until a new era is planned.
assert_eq!(ForceEra::<Test>::get(), Forcing::ForceNew);
MinimumValidatorCount::<Test>::put(2);
run_to_block(55);
assert_eq!(Staking::next_election_prediction(System::block_number()), 55 + 25);
assert_eq!(staking_events().len(), 6);
Gavin Wood
committed
assert_eq!(*staking_events().last().unwrap(), Event::StakersElected);
thiolliere
committed
// The new era has been planned, forcing is changed from `ForceNew` to `NotForcing`.
assert_eq!(ForceEra::<Test>::get(), Forcing::NotForcing);
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
#[test]
#[should_panic]
fn count_check_works() {
ExtBuilder::default().build_and_execute(|| {
// We should never insert into the validators or nominators map directly as this will
// not keep track of the count. This test should panic as we verify the count is accurate
// after every test using the `post_checks` in `mock`.
Validators::<Test>::insert(987654321, ValidatorPrefs::default());
Nominators::<Test>::insert(
987654321,
Nominations { targets: vec![], submitted_in: Default::default(), suppressed: false },
);
})
}
#[test]
fn min_bond_checks_work() {
ExtBuilder::default()
.existential_deposit(100)
.balance_factor(100)
.min_nominator_bond(1_000)
.min_validator_bond(1_500)
.build_and_execute(|| {
// 500 is not enough for any role
assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Controller));
assert_noop!(
Staking::nominate(Origin::signed(4), vec![1]),
Error::<Test>::InsufficientBond
);
assert_noop!(
Staking::validate(Origin::signed(4), ValidatorPrefs::default()),
Error::<Test>::InsufficientBond,
// 1000 is enough for nominator
assert_ok!(Staking::bond_extra(Origin::signed(3), 500));
assert_ok!(Staking::nominate(Origin::signed(4), vec![1]));
assert_noop!(
Staking::validate(Origin::signed(4), ValidatorPrefs::default()),
Error::<Test>::InsufficientBond,
);
// 1500 is enough for validator
assert_ok!(Staking::bond_extra(Origin::signed(3), 500));
assert_ok!(Staking::nominate(Origin::signed(4), vec![1]));
assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default()));
// Can't unbond anything as validator
assert_noop!(Staking::unbond(Origin::signed(4), 500), Error::<Test>::InsufficientBond);
// Once they are a nominator, they can unbond 500
assert_ok!(Staking::nominate(Origin::signed(4), vec![1]));
assert_ok!(Staking::unbond(Origin::signed(4), 500));
assert_noop!(Staking::unbond(Origin::signed(4), 500), Error::<Test>::InsufficientBond);
// Once they are chilled they can unbond everything
assert_ok!(Staking::chill(Origin::signed(4)));
assert_ok!(Staking::unbond(Origin::signed(4), 1000));
})
}
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
#[test]
fn chill_other_works() {
ExtBuilder::default()
.existential_deposit(100)
.balance_factor(100)
.min_nominator_bond(1_000)
.min_validator_bond(1_500)
.build_and_execute(|| {
let initial_validators = CounterForValidators::<Test>::get();
let initial_nominators = CounterForNominators::<Test>::get();
for i in 0..15 {
let a = 4 * i;
let b = 4 * i + 1;
let c = 4 * i + 2;
let d = 4 * i + 3;
Balances::make_free_balance_be(&a, 100_000);
Balances::make_free_balance_be(&b, 100_000);
Balances::make_free_balance_be(&c, 100_000);
Balances::make_free_balance_be(&d, 100_000);
// Nominator
assert_ok!(Staking::bond(
Origin::signed(a),
b,
1000,
RewardDestination::Controller
));
assert_ok!(Staking::nominate(Origin::signed(b), vec![1]));
// Validator
assert_ok!(Staking::bond(
Origin::signed(c),
d,
1500,
RewardDestination::Controller
assert_ok!(Staking::validate(Origin::signed(d), ValidatorPrefs::default()));
}
// To chill other users, we need to:
// * Set a minimum bond amount
// * Set a limit
// * Set a threshold
//
// If any of these are missing, we do not have enough information to allow the
// `chill_other` to succeed from one user to another.
// Can't chill these users
assert_noop!(
Staking::chill_other(Origin::signed(1337), 1),
Error::<Test>::CannotChillOther
);
assert_noop!(
Staking::chill_other(Origin::signed(1337), 3),
Error::<Test>::CannotChillOther
);
// Change the minimum bond... but no limits.
assert_ok!(Staking::set_staking_limits(Origin::root(), 1_500, 2_000, None, None, None));
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
// Still can't chill these users
assert_noop!(
Staking::chill_other(Origin::signed(1337), 1),
Error::<Test>::CannotChillOther
);
assert_noop!(
Staking::chill_other(Origin::signed(1337), 3),
Error::<Test>::CannotChillOther
);
// Add limits, but no threshold
assert_ok!(Staking::set_staking_limits(
Origin::root(),
1_500,
2_000,
Some(10),
Some(10),
None
));
// Still can't chill these users
assert_noop!(
Staking::chill_other(Origin::signed(1337), 1),
Error::<Test>::CannotChillOther
);
assert_noop!(
Staking::chill_other(Origin::signed(1337), 3),
Error::<Test>::CannotChillOther
);
1_500,
2_000,
None,
None,
Some(Percent::from_percent(0))
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
// Still can't chill these users
assert_noop!(
Staking::chill_other(Origin::signed(1337), 1),
Error::<Test>::CannotChillOther
);
assert_noop!(
Staking::chill_other(Origin::signed(1337), 3),
Error::<Test>::CannotChillOther
);
// Add threshold and limits
assert_ok!(Staking::set_staking_limits(
Origin::root(),
1_500,
2_000,
Some(10),
Some(10),
Some(Percent::from_percent(75))
));
// 16 people total because tests start with 2 active one
assert_eq!(CounterForNominators::<Test>::get(), 15 + initial_nominators);
assert_eq!(CounterForValidators::<Test>::get(), 15 + initial_validators);
// Users can now be chilled down to 7 people, so we try to remove 9 of them (starting
// with 16)
for i in 6..15 {
let b = 4 * i + 1;
let d = 4 * i + 3;
assert_ok!(Staking::chill_other(Origin::signed(1337), b));
assert_ok!(Staking::chill_other(Origin::signed(1337), d));
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
// chill a nominator. Limit is not reached, not chill-able
assert_eq!(CounterForNominators::<Test>::get(), 7);
assert_noop!(
Staking::chill_other(Origin::signed(1337), 1),
Error::<Test>::CannotChillOther
);
// chill a validator. Limit is reached, chill-able.
assert_eq!(CounterForValidators::<Test>::get(), 9);
assert_ok!(Staking::chill_other(Origin::signed(1337), 3));
})
}
#[test]
fn capped_stakers_works() {
ExtBuilder::default().build_and_execute(|| {
let validator_count = CounterForValidators::<Test>::get();
assert_eq!(validator_count, 3);
let nominator_count = CounterForNominators::<Test>::get();
assert_eq!(nominator_count, 1);
// Change the maximums
let max = 10;
assert_ok!(Staking::set_staking_limits(
Origin::root(),
10,
10,
Some(max),
Some(max),
Some(Percent::from_percent(0))
));
// can create `max - validator_count` validators
let mut some_existing_validator = AccountId::default();
for i in 0..max - validator_count {
let (_, controller) = testing_utils::create_stash_controller::<Test>(
i + 10_000_000,
100,
RewardDestination::Controller,
)
.unwrap();
assert_ok!(Staking::validate(Origin::signed(controller), ValidatorPrefs::default()));
some_existing_validator = controller;
}
// but no more
let (_, last_validator) = testing_utils::create_stash_controller::<Test>(
1337,
100,
RewardDestination::Controller,
)
.unwrap();
assert_noop!(
Staking::validate(Origin::signed(last_validator), ValidatorPrefs::default()),
Error::<Test>::TooManyValidators,
);
// same with nominators
let mut some_existing_nominator = AccountId::default();
for i in 0..max - nominator_count {
let (_, controller) = testing_utils::create_stash_controller::<Test>(
i + 20_000_000,
100,
RewardDestination::Controller,
)
.unwrap();
assert_ok!(Staking::nominate(Origin::signed(controller), vec![1]));
some_existing_nominator = controller;
}
// one more is too many
let (_, last_nominator) = testing_utils::create_stash_controller::<Test>(
30_000_000,
100,
RewardDestination::Controller,
)
.unwrap();
assert_noop!(
Staking::nominate(Origin::signed(last_nominator), vec![1]),
Error::<Test>::TooManyNominators
);
// Re-nominate works fine
assert_ok!(Staking::nominate(Origin::signed(some_existing_nominator), vec![1]));
// Re-validate works fine
assert_ok!(Staking::validate(
Origin::signed(some_existing_validator),
ValidatorPrefs::default()
));
// No problem when we set to `None` again
assert_ok!(Staking::set_staking_limits(Origin::root(), 10, 10, None, None, None));
assert_ok!(Staking::nominate(Origin::signed(last_nominator), vec![1]));
assert_ok!(Staking::validate(Origin::signed(last_validator), ValidatorPrefs::default()));
})
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
mod sorted_list_provider {
use super::*;
use frame_election_provider_support::SortedListProvider;
#[test]
fn re_nominate_does_not_change_counters_or_list() {
ExtBuilder::default().nominate(true).build_and_execute(|| {
// given
let pre_insert_nominator_count = Nominators::<Test>::iter().count() as u32;
assert_eq!(<Test as Config>::SortedListProvider::count(), pre_insert_nominator_count);
assert!(Nominators::<Test>::contains_key(101));
assert_eq!(<Test as Config>::SortedListProvider::iter().collect::<Vec<_>>(), vec![101]);
// when account 101 renominates
assert_ok!(Staking::nominate(Origin::signed(100), vec![41]));
// then counts don't change
assert_eq!(<Test as Config>::SortedListProvider::count(), pre_insert_nominator_count);
assert_eq!(Nominators::<Test>::iter().count() as u32, pre_insert_nominator_count);
// and the list is the same
assert_eq!(<Test as Config>::SortedListProvider::iter().collect::<Vec<_>>(), vec![101]);
});
}
}