Skip to content
tests.rs 127 KiB
Newer Older
			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);
			assert_eq!(
				*staking_events().last().unwrap(),
				Event::StakingElection
			);
			// The new era has been planned, forcing is changed from `ForceNew` to `NotForcing`.
			assert_eq!(ForceEra::<Test>::get(), Forcing::NotForcing);

	#[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)
			.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));
			})
	}

	#[test]
	fn chill_other_works() {
		ExtBuilder::default()
			.existential_deposit(100)
			.min_nominator_bond(1_000)
			.min_validator_bond(1_500)
			.build_and_execute(|| {
				// Nominator
				assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller));
				assert_ok!(Staking::nominate(Origin::signed(2), vec![1]));

				// Validator
				assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller));
				assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default()));

				// Can't chill these users
				assert_noop!(Staking::chill_other(Origin::signed(1), 2), Error::<Test>::CannotChillOther);
				assert_noop!(Staking::chill_other(Origin::signed(1), 4), Error::<Test>::CannotChillOther);

				// Change the minimum bond
				assert_ok!(Staking::update_staking_limits(Origin::root(), 1_500, 2_000, None, None));

				// Users can now be chilled
				assert_ok!(Staking::chill_other(Origin::signed(1), 2));
				assert_ok!(Staking::chill_other(Origin::signed(1), 4));
			})
	}

	#[test]
	fn capped_stakers_works() {
		ExtBuilder::default().build_and_execute(|| {
			let validator_count = CurrentValidatorsCount::<Test>::get();
			assert_eq!(validator_count, 3);
			let nominator_count = CurrentNominatorsCount::<Test>::get();
			assert_eq!(nominator_count, 1);

			// Change the maximums
			let max = 10;
			assert_ok!(Staking::update_staking_limits(Origin::root(), 10, 10, Some(max), Some(max)));

			// can create `max - validator_count` validators
			assert_ok!(testing_utils::create_validators::<Test>(max - validator_count, 100));

			// 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
			for i in 0 .. max - nominator_count {
				let (_, controller) = testing_utils::create_stash_controller::<Test>(
					i + 10_000_000, 100, RewardDestination::Controller,
				).unwrap();
				assert_ok!(Staking::nominate(Origin::signed(controller), vec![1]));
			}

			// one more is too many
			let (_, last_nominator) = testing_utils::create_stash_controller::<Test>(
				20_000_000, 100, RewardDestination::Controller,
			).unwrap();
			assert_noop!(Staking::nominate(Origin::signed(last_nominator), vec![1]), Error::<Test>::TooManyNominators);

			// No problem when we set to `None` again
			assert_ok!(Staking::update_staking_limits(Origin::root(), 10, 10, None, None));
			assert_ok!(Staking::nominate(Origin::signed(last_nominator), vec![1]));
			assert_ok!(Staking::validate(Origin::signed(last_validator), ValidatorPrefs::default()));
		})
	}