Skip to content
tests.rs 102 KiB
Newer Older
Gavin Wood's avatar
Gavin Wood committed
		<Staking as SessionManager<_>>::end_session(init_session + 2);
		<Staking as SessionManager<_>>::start_session(init_session + 3);
		assert_eq!(Staking::active_era().unwrap().index, init_active_era + 1);
		<Staking as SessionManager<_>>::end_session(init_session + 3);
		<Staking as SessionManager<_>>::start_session(init_session + 4);
		assert_eq!(Staking::active_era().unwrap().index, init_active_era + 1);
		<Staking as SessionManager<_>>::end_session(init_session + 4);
		<Staking as SessionManager<_>>::start_session(init_session + 5);
		assert_eq!(Staking::active_era().unwrap().index, init_active_era + 1);

		// Reward current era
		Staking::reward_by_ids(vec![(21, 2)]);

		// New active era is triggered here.
		<Staking as SessionManager<_>>::end_session(init_session + 5);
		<Staking as SessionManager<_>>::start_session(init_session + 6);
		assert_eq!(Staking::active_era().unwrap().index, init_active_era + 2);

		// That reward are correct
		assert_eq!(Staking::eras_reward_points(init_active_era).total, 1);
		assert_eq!(Staking::eras_reward_points(init_active_era + 1).total, 2);
	});
}

#[test]
fn test_max_nominator_rewarded_per_validator_and_cant_steal_someone_else_reward() {
	// Test:
	// * If nominator nomination is below the $MaxNominatorRewardedPerValidator other nominator
	//   then the nominator can't claim its reward
	// * A nominator can't claim another nominator reward
	ExtBuilder::default().build().execute_with(|| {
		for i in 0..=<Test as Trait>::MaxNominatorRewardedPerValidator::get() {
			let stash = 10_000 + i as u64;
			let controller = 20_000 + i as u64;
			let balance = 10_000 + i as u64;
			Balances::make_free_balance_be(&stash, balance);
			assert_ok!(
				Staking::bond(
					Origin::signed(stash),
					controller,
					balance,
					RewardDestination::Stash
				)
			);
			assert_ok!(Staking::nominate(Origin::signed(controller), vec![11]));
		}
		mock::start_era(1);

		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_0 = current_total_payout_for_duration(3 * 1000);
		assert!(total_payout_0 > 100); // Test is meaningful if reward something

		mock::start_era(2);
		mock::make_all_reward_payment(1);

		// nominator 10_000 can't get its reward because exposure is clipped. However it will try
		// to query other people reward.
		assert_ok!(Staking::payout_nominator(Origin::signed(20_000), 1, vec![(11, 0)]));

		// Assert only nominators from 1 to Max are rewarded
		for i in 0..=<Test as Trait>::MaxNominatorRewardedPerValidator::get() {
			let stash = 10_000 + i as u64;
			let balance = 10_000 + i as u64;
			if stash == 10_000 {
				assert!(Balances::free_balance(&stash) == balance);
			} else {
				assert!(Balances::free_balance(&stash) > balance);
			}
		}
	});
}

// Test that an upgrade from previous test environment works.
#[test]
fn test_upgrade_from_master_works() {
	let data_sets = &[
		test_upgrade_from_master_dataset::_0,
		test_upgrade_from_master_dataset::_1,
		test_upgrade_from_master_dataset::_2,
		test_upgrade_from_master_dataset::_3,
		test_upgrade_from_master_dataset::_4,
		test_upgrade_from_master_dataset::_5,
		test_upgrade_from_master_dataset::_6,
		test_upgrade_from_master_dataset::_7,
		test_upgrade_from_master_dataset::_8,
	];
	for data_set in data_sets.iter() {
		let mut storage = sp_runtime::Storage::default();
		for (key, value) in data_set.iter() {
			storage.top.insert(key.to_vec(), value.to_vec());
		}
		let mut ext = sp_io::TestExternalities::from(storage);
		ext.execute_with(|| {
			let old_stakers =
				get_storage_value::<Vec<AccountId>>(b"Staking", b"CurrentElected", b"").unwrap();
			let old_staker_0 = old_stakers[0];
			let old_staker_1 = old_stakers[1];
			let old_current_era =
				get_storage_value::<EraIndex>(b"Staking", b"CurrentEra", b"").unwrap();
			let old_staker_0_exposure = get_storage_value::<Exposure<AccountId, Balance>>(
				b"Staking", b"Stakers", &blake2_256(&old_staker_0.encode())
			).unwrap();
			let old_staker_1_exposure = get_storage_value::<Exposure<AccountId, Balance>>(
				b"Staking", b"Stakers", &blake2_256(&old_staker_1.encode())
			).unwrap();
			let (
				old_era_points_earned_total,
				old_era_points_earned_individual
			) = get_storage_value::<(u32, Vec<u32>)>(b"Staking", b"CurrentEraPointsEarned", b"")
				.unwrap_or((0, vec![]));

			Staking::ensure_storage_upgraded();
			assert!(<Staking as Store>::IsUpgraded::get());

			// Check ActiveEra and CurrentEra
			let active_era = Staking::active_era().unwrap().index;
			let current_era = Staking::current_era().unwrap();
			assert!(current_era == active_era);
			assert!(current_era == old_current_era);

			// Check ErasStartSessionIndex
			let active_era_start = Staking::eras_start_session_index(active_era).unwrap();
			let current_era_start = Staking::eras_start_session_index(current_era).unwrap();
			let current_session_index = Session::current_index();
			assert!(current_era_start == active_era_start);
			assert!(active_era_start <= current_session_index);
			assert_eq!(<Staking as Store>::ErasStartSessionIndex::iter().count(), 1);

			// Check ErasStakers
			assert_eq!(<Staking as Store>::ErasStakers::iter().count(), 2);
			assert_eq!(
				<Staking as Store>::ErasStakers::get(current_era, old_staker_0),
				old_staker_0_exposure
			);
			assert_eq!(
				<Staking as Store>::ErasStakers::get(current_era, old_staker_1),
				old_staker_1_exposure
			);

			// Check ErasStakersClipped
			assert_eq!(<Staking as Store>::ErasStakersClipped::iter().count(), 2);
			assert!(<Staking as Store>::ErasStakersClipped::iter().all(|exposure_clipped| {
				let max = <Test as Trait>::MaxNominatorRewardedPerValidator::get() as usize;
				exposure_clipped.others.len() <= max
			}));
			assert_eq!(
				<Staking as Store>::ErasStakersClipped::get(current_era, old_staker_0),
				old_staker_0_exposure
			);
			assert_eq!(
				<Staking as Store>::ErasStakersClipped::get(current_era, old_staker_1),
				old_staker_1_exposure
			);

			// Check ErasValidatorPrefs
			assert_eq!(<Staking as Store>::ErasValidatorPrefs::iter().count(), 2);
			assert_eq!(
				<Staking as Store>::ErasValidatorPrefs::get(current_era, old_staker_0),
				Staking::validators(old_staker_0)
			);
			assert_eq!(
				<Staking as Store>::ErasValidatorPrefs::get(current_era, old_staker_1),
				Staking::validators(old_staker_1)
			);

			// Check ErasTotalStake
			assert_eq!(<Staking as Store>::ErasTotalStake::iter().count(), 1);
			assert_eq!(
				<Staking as Store>::ErasTotalStake::get(current_era),
				old_staker_0_exposure.total + old_staker_1_exposure.total
			);

			// Check ErasRewardPoints
			assert_eq!(<Staking as Store>::ErasRewardPoints::iter().count(), 1);
			let mut individual = BTreeMap::new();
			if let Some(p) = old_era_points_earned_individual.get(0) {
				individual.insert(old_staker_0, p.clone());
			}
			if let Some(p) = old_era_points_earned_individual.get(1) {
				individual.insert(old_staker_1, p.clone());
			}
			assert_eq!(
				<Staking as Store>::ErasRewardPoints::get(current_era),
				EraRewardPoints {
					total: old_era_points_earned_total,
					individual,
				}
			);

			// Check ErasValidatorReward
			assert_eq!(<Staking as Store>::ErasValidatorReward::iter().count(), 0);
		});
	}
}

#[test]
fn set_history_depth_works() {
	ExtBuilder::default().build().execute_with(|| {
		start_era(10);
		Staking::set_history_depth(Origin::ROOT, 20).unwrap();
		assert!(<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
		assert!(<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
		Staking::set_history_depth(Origin::ROOT, 4).unwrap();
		assert!(<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
		assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
		Staking::set_history_depth(Origin::ROOT, 3).unwrap();
		assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
		assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
		Staking::set_history_depth(Origin::ROOT, 8).unwrap();
		assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
		assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
	});
}