Skip to content
tests.rs 150 KiB
Newer Older
			assert_ok!(Staking::unbond(Origin::signed(20), 1000 - ed));
			start_active_era(3);
			assert_ok!(Staking::withdraw_unbonded(Origin::signed(20), 100));

			// initial stuff.
			assert_eq!(
				Staking::ledger(&20).unwrap(),
				StakingLedger {
					stash: 21,
					total: ed,
					active: ed,
					unlocking: vec![],
					claimed_rewards: vec![]
				}
			);
		})
}

mod election_data_provider {
	use super::*;
	use sp_election_providers::ElectionDataProvider;

	#[test]
	fn voters_include_self_vote() {
		ExtBuilder::default().nominate(false).build().execute_with(|| {
			assert!(<Validators<Test>>::iter().map(|(x, _)| x).all(|v| Staking::voters()
				.into_iter()
				.find(|(w, _, t)| { v == *w && t[0] == *w })
				.is_some()))
		})
	}

	#[test]
	fn voters_exclude_slashed() {
		ExtBuilder::default().build().execute_with(|| {
			assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]);
			assert_eq!(
				<Staking as ElectionDataProvider<AccountId, BlockNumber>>::voters()
					.iter()
					.find(|x| x.0 == 101)
					.unwrap()
					.2,
				vec![11, 21]
			);

			start_active_era(1);
			add_slash(&11);

			// 11 is gone.
			start_active_era(2);
			assert_eq!(
				<Staking as ElectionDataProvider<AccountId, BlockNumber>>::voters()
					.iter()
					.find(|x| x.0 == 101)
					.unwrap()
					.2,
				vec![21]
			);

			// resubmit and it is back
			assert_ok!(Staking::nominate(Origin::signed(100), vec![11, 21]));
			assert_eq!(
				<Staking as ElectionDataProvider<AccountId, BlockNumber>>::voters()
					.iter()
					.find(|x| x.0 == 101)
					.unwrap()
					.2,
				vec![11, 21]
			);
		})
	}

	#[test]
	fn estimate_next_election_works() {
		ExtBuilder::default().session_per_era(5).period(5).build().execute_with(|| {
			// 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);
			assert_eq!(
				*staking_events().last().unwrap(),
				RawEvent::StakingElection(ElectionCompute::OnChain)
			);

			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);
			assert_eq!(
				*staking_events().last().unwrap(),
				RawEvent::StakingElection(ElectionCompute::OnChain)
			);
		})
	}
}