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

		// Reward current era
		Staking::reward_by_ids(vec![(11, 1)]);

		// New active era is triggered here.
		<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);

		// 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]
fn set_history_depth_works() {
	ExtBuilder::default().build().execute_with(|| {
		mock::start_era(10);
Gavin Wood's avatar
Gavin Wood committed
		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));
	});
}
#[test]
fn test_payout_stakers() {
	// Here we will test validator can set `max_nominators_payout` and it works.
	// We also test that `payout_extra_nominators` works.
	ExtBuilder::default().has_stakers(false).build().execute_with(|| {
		let balance = 1000;
		// Create three validators:
		bond_validator(11, 10, balance); // Default(64)

		// Create nominators, targeting stash of validators
		for i in 0..100 {
			bond_nominator(1000 + i, 100 + i, balance + i, vec![11]);
		}

		mock::start_era(1);
		Staking::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);
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 1));

		// Top 64 nominators of validator 11 automatically paid out, including the validator
		// Validator payout goes to controller.
		assert!(Balances::free_balance(&10) > balance);
		for i in 36..100 {
			assert!(Balances::free_balance(&(100 + i)) > balance + i);
		}
		// The bottom 36 do not
		for i in 0..36 {
			assert_eq!(Balances::free_balance(&(100 + i)), balance + i);
		}

		// We track rewards in `claimed_rewards` vec
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![], claimed_rewards: vec![1] })
		);

		for i in 3..16 {
			Staking::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(i);
			assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, i - 1));
		}

		// We track rewards in `claimed_rewards` vec
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![], claimed_rewards: (1..=14).collect() })
		);

		for i in 16..100 {
			Staking::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(i);
		}

		// We clean it up as history passes
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 15));
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 98));
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![], claimed_rewards: vec![15, 98] })
		);

		// Out of order claims works.
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 69));
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 23));
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 42));
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![], claimed_rewards: vec![15, 23, 42, 69, 98] })
		);
	});
}

#[test]
fn payout_stakers_handles_basic_errors() {
	// Here we will test payouts handle all errors.
	ExtBuilder::default().has_stakers(false).build().execute_with(|| {
		// Same setup as the test above
		let balance = 1000;
		bond_validator(11, 10, balance); // Default(64)

		// Create nominators, targeting stash
		for i in 0..100 {
			bond_nominator(1000 + i, 100 + i, balance + i, vec![11]);
		}

		mock::start_era(1);
		Staking::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);

		// Wrong Era, too big
		assert_noop!(Staking::payout_stakers(Origin::signed(1337), 11, 2), Error::<Test>::InvalidEraToReward);
		// Wrong Staker
		assert_noop!(Staking::payout_stakers(Origin::signed(1337), 10, 1), Error::<Test>::NotStash);

		for i in 3..100 {
			Staking::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(i);
		}
		// We are at era 99, with history depth of 84
		// We should be able to payout era 15 through 98 (84 total eras), but not 14 or 99.
		assert_noop!(Staking::payout_stakers(Origin::signed(1337), 11, 14), Error::<Test>::InvalidEraToReward);
		assert_noop!(Staking::payout_stakers(Origin::signed(1337), 11, 99), Error::<Test>::InvalidEraToReward);
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 15));
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 98));

		// Can't claim again
		assert_noop!(Staking::payout_stakers(Origin::signed(1337), 11, 15), Error::<Test>::AlreadyClaimed);
		assert_noop!(Staking::payout_stakers(Origin::signed(1337), 11, 98), Error::<Test>::AlreadyClaimed);
	});
}

#[test]
fn bond_during_era_correctly_populates_claimed_rewards() {
	ExtBuilder::default().has_stakers(false).build().execute_with(|| {
		// Era = None
		bond_validator(9, 8, 1000);
		assert_eq!(
			Staking::ledger(&8),
			Some(StakingLedger {
				stash: 9,
				total: 1000,
				active: 1000,
				unlocking: vec![],
				claimed_rewards: vec![],
			})
		);
		mock::start_era(5);
		bond_validator(11, 10, 1000);
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger {
				stash: 11,
				total: 1000,
				active: 1000,
				unlocking: vec![],
				claimed_rewards: (0..5).collect(),
			})
		);
		mock::start_era(99);
		bond_validator(13, 12, 1000);
		assert_eq!(
			Staking::ledger(&12),
			Some(StakingLedger {
				stash: 13,
				total: 1000,
				active: 1000,
				unlocking: vec![],
				claimed_rewards: (15..99).collect(),
			})
		);
	});
}

/* These migration tests below can be removed once migration code is removed */

#[test]
fn assert_migration_is_noop() {
	let kusama_active_era = "4a0200000190e2721171010000";
	let era = ActiveEraInfo::decode(&mut &hex::decode(kusama_active_era).unwrap()[..]).unwrap();
	assert_eq!(era.index, 586);
	assert_eq!(era.start, Some(1585135674000));
}
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 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 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 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721

#[test]
fn test_last_reward_migration() {
	use sp_storage::Storage;

	let mut s = Storage::default();

	#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
	struct OldStakingLedger<AccountId, Balance: HasCompact> {
		pub stash: AccountId,
		#[codec(compact)]
		pub total: Balance,
		#[codec(compact)]
		pub active: Balance,
		pub unlocking: Vec<UnlockChunk<Balance>>,
		pub last_reward: Option<EraIndex>,
	}

	let old_staking10 = OldStakingLedger::<u64, u64> {
		stash: 0,
		total: 10,
		active: 10,
		unlocking: vec![UnlockChunk{ value: 1234, era: 56}],
		last_reward: Some(8),
	};

	let old_staking11 = OldStakingLedger::<u64, u64> {
		stash: 1,
		total: 0,
		active: 0,
		unlocking: vec![],
		last_reward: None,
	};

	let old_staking12 = OldStakingLedger::<u64, u64> {
		stash: 2,
		total: 100,
		active: 100,
		unlocking: vec![UnlockChunk{ value: 9876, era: 54}, UnlockChunk{ value: 98, era: 76}],
		last_reward: Some(23),
	};

	let old_staking13 = OldStakingLedger::<u64, u64> {
		stash: 3,
		total: 100,
		active: 100,
		unlocking: vec![],
		last_reward: Some(23),
	};

	let data = vec![
		(
			Ledger::<Test>::hashed_key_for(10),
			old_staking10.encode().to_vec()
		),
		(
			Ledger::<Test>::hashed_key_for(11),
			old_staking11.encode().to_vec()
		),
		(
			Ledger::<Test>::hashed_key_for(12),
			old_staking12.encode().to_vec()
		),
		(
			Ledger::<Test>::hashed_key_for(13),
			old_staking13.encode().to_vec()
		),
	];

	s.top = data.into_iter().collect();
	sp_io::TestExternalities::new(s).execute_with(|| {
		HistoryDepth::put(84);
		CurrentEra::put(99);
		let nominations = Nominations::<u64> {
			targets: vec![],
			submitted_in: 0,
			suppressed: false
		};
		Nominators::<Test>::insert(3, nominations);
		Bonded::<Test>::insert(3, 13);
		Staking::migrate_last_reward_to_claimed_rewards();
		// Test staker out of range
		assert_eq!(
			Ledger::<Test>::get(10),
			Some(StakingLedger {
				stash: 0,
				total: 10,
				active: 10,
				unlocking: vec![UnlockChunk{ value: 1234, era: 56}],
				claimed_rewards: vec![],
			})
		);
		// Test staker none
		assert_eq!(
			Ledger::<Test>::get(11),
			Some(StakingLedger {
				stash: 1,
				total: 0,
				active: 0,
				unlocking: vec![],
				claimed_rewards: vec![],
			})
		);
		// Test staker migration
		assert_eq!(
			Ledger::<Test>::get(12),
			Some(StakingLedger {
				stash: 2,
				total: 100,
				active: 100,
				unlocking: vec![UnlockChunk{ value: 9876, era: 54}, UnlockChunk{ value: 98, era: 76}],
				claimed_rewards: vec![15,16,17,18,19,20,21,22,23],
			})
		);
		// Test nominator migration
		assert_eq!(
			Ledger::<Test>::get(13),
			Some(StakingLedger {
				stash: 3,
				total: 100,
				active: 100,
				unlocking: vec![],
				claimed_rewards: vec![15,16,17,18,19,20,21,22,23],
			})
		);
	});
}

#[test]
fn rewards_should_work_before_migration() {
	// should check that before migration:
	// * rewards get recorded per session
	// * rewards get paid per Era
	// * Check that nominators are also rewarded
	ExtBuilder::default().nominate(true).build().execute_with(|| {
		MigrateEra::put(10);
		let init_balance_10 = Balances::total_balance(&10);
		let init_balance_11 = Balances::total_balance(&11);
		let init_balance_20 = Balances::total_balance(&20);
		let init_balance_21 = Balances::total_balance(&21);
		let init_balance_100 = Balances::total_balance(&100);
		let init_balance_101 = Balances::total_balance(&101);

		// Check state
		Payee::<Test>::insert(11, RewardDestination::Controller);
		Payee::<Test>::insert(21, RewardDestination::Controller);
		Payee::<Test>::insert(101, RewardDestination::Controller);

		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		// This is the second validator of the current elected set.
		<Module<Test>>::reward_by_ids(vec![(21, 50)]);

		// 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 > 10); // Test is meaningful if reward something

		start_session(1);

		assert_eq!(Balances::total_balance(&10), init_balance_10);
		assert_eq!(Balances::total_balance(&11), init_balance_11);
		assert_eq!(Balances::total_balance(&20), init_balance_20);
		assert_eq!(Balances::total_balance(&21), init_balance_21);
		assert_eq!(Balances::total_balance(&100), init_balance_100);
		assert_eq!(Balances::total_balance(&101), init_balance_101);
		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		assert_eq!(Staking::eras_reward_points(Staking::active_era().unwrap().index), EraRewardPoints {
			total: 50*3,
			individual: vec![(11, 100), (21, 50)].into_iter().collect(),
		});
		let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
		let part_for_20 = Perbill::from_rational_approximation::<u32>(1000, 1375);
		let part_for_100_from_10 = Perbill::from_rational_approximation::<u32>(125, 1125);
		let part_for_100_from_20 = Perbill::from_rational_approximation::<u32>(375, 1375);

		start_session(2);
		start_session(3);

		assert_eq!(Staking::active_era().unwrap().index, 1);
		mock::make_all_reward_payment_before_migration(0);

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * total_payout_0*2/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0*1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * total_payout_0 * 2/3
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);

		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);

		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_1 = current_total_payout_for_duration(3 * 1000);
		assert!(total_payout_1 > 10); // Test is meaningful if reward something

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

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * (total_payout_0 * 2/3 + total_payout_1), 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0 * 1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * (total_payout_0 * 2/3 + total_payout_1)
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);
	});
}

#[test]
fn migrate_era_should_work() {
	// should check that before and after migration:
	// * rewards get recorded per session
	// * rewards get paid per Era
	// * Check that nominators are also rewarded
	ExtBuilder::default().nominate(true).build().execute_with(|| {
		MigrateEra::put(1);
		let init_balance_10 = Balances::total_balance(&10);
		let init_balance_11 = Balances::total_balance(&11);
		let init_balance_20 = Balances::total_balance(&20);
		let init_balance_21 = Balances::total_balance(&21);
		let init_balance_100 = Balances::total_balance(&100);
		let init_balance_101 = Balances::total_balance(&101);

		// Check state
		Payee::<Test>::insert(11, RewardDestination::Controller);
		Payee::<Test>::insert(21, RewardDestination::Controller);
		Payee::<Test>::insert(101, RewardDestination::Controller);

		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		// This is the second validator of the current elected set.
		<Module<Test>>::reward_by_ids(vec![(21, 50)]);

		// 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 > 10); // Test is meaningful if reward something

		start_session(1);

		assert_eq!(Balances::total_balance(&10), init_balance_10);
		assert_eq!(Balances::total_balance(&11), init_balance_11);
		assert_eq!(Balances::total_balance(&20), init_balance_20);
		assert_eq!(Balances::total_balance(&21), init_balance_21);
		assert_eq!(Balances::total_balance(&100), init_balance_100);
		assert_eq!(Balances::total_balance(&101), init_balance_101);
		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		assert_eq!(Staking::eras_reward_points(Staking::active_era().unwrap().index), EraRewardPoints {
			total: 50*3,
			individual: vec![(11, 100), (21, 50)].into_iter().collect(),
		});
		let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
		let part_for_20 = Perbill::from_rational_approximation::<u32>(1000, 1375);
		let part_for_100_from_10 = Perbill::from_rational_approximation::<u32>(125, 1125);
		let part_for_100_from_20 = Perbill::from_rational_approximation::<u32>(375, 1375);

		start_session(2);
		start_session(3);

		assert_eq!(Staking::active_era().unwrap().index, 1);
		mock::make_all_reward_payment_before_migration(0);

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * total_payout_0*2/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0*1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * total_payout_0 * 2/3
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);

		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);

		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_1 = current_total_payout_for_duration(3 * 1000);
		assert!(total_payout_1 > 10); // Test is meaningful if reward something

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

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * (total_payout_0 * 2/3 + total_payout_1), 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0 * 1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * (total_payout_0 * 2/3 + total_payout_1)
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);
	});
}

#[test]
#[should_panic]
fn migrate_era_should_handle_error() {
	ExtBuilder::default().nominate(true).build().execute_with(|| {
		MigrateEra::put(1);
		let init_balance_10 = Balances::total_balance(&10);
		let init_balance_11 = Balances::total_balance(&11);
		let init_balance_20 = Balances::total_balance(&20);
		let init_balance_21 = Balances::total_balance(&21);
		let init_balance_100 = Balances::total_balance(&100);
		let init_balance_101 = Balances::total_balance(&101);

		// Check state
		Payee::<Test>::insert(11, RewardDestination::Controller);
		Payee::<Test>::insert(21, RewardDestination::Controller);
		Payee::<Test>::insert(101, RewardDestination::Controller);

		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		// This is the second validator of the current elected set.
		<Module<Test>>::reward_by_ids(vec![(21, 50)]);

		// 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 > 10); // Test is meaningful if reward something

		start_session(1);

		assert_eq!(Balances::total_balance(&10), init_balance_10);
		assert_eq!(Balances::total_balance(&11), init_balance_11);
		assert_eq!(Balances::total_balance(&20), init_balance_20);
		assert_eq!(Balances::total_balance(&21), init_balance_21);
		assert_eq!(Balances::total_balance(&100), init_balance_100);
		assert_eq!(Balances::total_balance(&101), init_balance_101);
		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		assert_eq!(Staking::eras_reward_points(Staking::active_era().unwrap().index), EraRewardPoints {
			total: 50*3,
			individual: vec![(11, 100), (21, 50)].into_iter().collect(),
		});

		start_session(2);
		start_session(3);

		assert_eq!(Staking::active_era().unwrap().index, 1);
		mock::make_all_reward_payment(0);
	});
}

#[test]
#[should_panic]
fn migrate_era_should_handle_errors_2() {
	// should check that before and after migration:
	// * rewards get recorded per session
	// * rewards get paid per Era
	// * Check that nominators are also rewarded
	ExtBuilder::default().nominate(true).build().execute_with(|| {
		MigrateEra::put(1);
		let init_balance_10 = Balances::total_balance(&10);
		let init_balance_11 = Balances::total_balance(&11);
		let init_balance_20 = Balances::total_balance(&20);
		let init_balance_21 = Balances::total_balance(&21);
		let init_balance_100 = Balances::total_balance(&100);
		let init_balance_101 = Balances::total_balance(&101);

		// Check state
		Payee::<Test>::insert(11, RewardDestination::Controller);
		Payee::<Test>::insert(21, RewardDestination::Controller);
		Payee::<Test>::insert(101, RewardDestination::Controller);

		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		// This is the second validator of the current elected set.
		<Module<Test>>::reward_by_ids(vec![(21, 50)]);

		// 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 > 10); // Test is meaningful if reward something

		start_session(1);

		assert_eq!(Balances::total_balance(&10), init_balance_10);
		assert_eq!(Balances::total_balance(&11), init_balance_11);
		assert_eq!(Balances::total_balance(&20), init_balance_20);
		assert_eq!(Balances::total_balance(&21), init_balance_21);
		assert_eq!(Balances::total_balance(&100), init_balance_100);
		assert_eq!(Balances::total_balance(&101), init_balance_101);
		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		assert_eq!(Staking::eras_reward_points(Staking::active_era().unwrap().index), EraRewardPoints {
			total: 50*3,
			individual: vec![(11, 100), (21, 50)].into_iter().collect(),
		});
		let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
		let part_for_20 = Perbill::from_rational_approximation::<u32>(1000, 1375);
		let part_for_100_from_10 = Perbill::from_rational_approximation::<u32>(125, 1125);
		let part_for_100_from_20 = Perbill::from_rational_approximation::<u32>(375, 1375);

		start_session(2);
		start_session(3);

		assert_eq!(Staking::active_era().unwrap().index, 1);
		mock::make_all_reward_payment_before_migration(0);

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * total_payout_0*2/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0*1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * total_payout_0 * 2/3
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);

		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);

		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_1 = current_total_payout_for_duration(3 * 1000);
		assert!(total_payout_1 > 10); // Test is meaningful if reward something

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

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * (total_payout_0 * 2/3 + total_payout_1), 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0 * 1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * (total_payout_0 * 2/3 + total_payout_1)
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);
	});
}