Skip to content
tests.rs 142 KiB
Newer Older
		assert_eq!(Balances::total_balance(&10), 1);

		// confirm that 10 is a normal validator and gets paid at the end of the era.
		mock::start_era(1);

		// Initial state of 10
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000,
			active: 1000,
			unlocking: vec![],
			claimed_rewards: vec![],
		assert_eq!(
			Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
			Exposure { total: 1000, own: 1000, others: vec![] }
		);
		// deposit the extra 100 units
		Staking::bond_extra(Origin::signed(11), 100).unwrap();
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000 + 100,
			active: 1000 + 100,
			unlocking: vec![],
			claimed_rewards: vec![],
		// Exposure is a snapshot! only updated after the next era update.
		assert_ne!(
			Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
			Exposure { total: 1000 + 100, own: 1000 + 100, others: vec![] }
		);

		// trigger next era.
		mock::start_era(2);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 2);

		// ledger should be the same.
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000 + 100,
			active: 1000 + 100,
			unlocking: vec![],
			claimed_rewards: vec![],
		// Exposure is now updated.
		assert_eq!(
			Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
			Exposure { total: 1000 + 100, own: 1000 + 100, others: vec![] }
		);

		// Unbond almost all of the funds in stash.
		Staking::unbond(Origin::signed(10), 1000).unwrap();
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger {
				stash: 11,
				total: 1000 + 100,
				active: 100,
				unlocking: vec![UnlockChunk{ value: 1000, era: 2 + 3}],
				claimed_rewards: vec![]
			}),

		// Attempting to free the balances now will fail. 2 eras need to pass.
		assert_ok!(Staking::withdraw_unbonded(Origin::signed(10), 0));
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger {
				stash: 11,
				total: 1000 + 100,
				active: 100,
				unlocking: vec![UnlockChunk{ value: 1000, era: 2 + 3}],
				claimed_rewards: vec![]
			}),
		);

		// trigger next era.
		mock::start_era(3);
		assert_ok!(Staking::withdraw_unbonded(Origin::signed(10), 0));
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger {
				stash: 11,
				total: 1000 + 100,
				active: 100,
				unlocking: vec![UnlockChunk{ value: 1000, era: 2 + 3}],
				claimed_rewards: vec![]
			}),
		);

		// trigger next era.
		mock::start_era(5);
		assert_ok!(Staking::withdraw_unbonded(Origin::signed(10), 0));
		// Now the value is free and the staking ledger is updated.
		assert_eq!(
			Staking::ledger(&10),
			Some(StakingLedger {
				stash: 11,
				total: 100,
				active: 100,
				unlocking: vec![],
				claimed_rewards: vec![]
			}),
		);
#[test]
fn too_many_unbond_calls_should_not_work() {
	ExtBuilder::default().build_and_execute(|| {
		// locked at era 0 until 3
		for _ in 0..MAX_UNLOCKING_CHUNKS-1 {
			assert_ok!(Staking::unbond(Origin::signed(10), 1));
		}

		mock::start_era(1);
		// locked at era 1 until 4
		assert_ok!(Staking::unbond(Origin::signed(10), 1));
		// can't do more.
		assert_noop!(Staking::unbond(Origin::signed(10), 1), Error::<Test>::NoMoreChunks);
		mock::start_era(3);
		assert_noop!(Staking::unbond(Origin::signed(10), 1), Error::<Test>::NoMoreChunks);
		// free up.
		assert_ok!(Staking::withdraw_unbonded(Origin::signed(10), 0));

		// Can add again.
		assert_ok!(Staking::unbond(Origin::signed(10), 1));
		assert_eq!(Staking::ledger(&10).unwrap().unlocking.len(), 2);
	})
}

Marcio Diaz's avatar
Marcio Diaz committed
#[test]
fn rebond_works() {
	// * Should test
	// * Given an account being bonded [and chosen as a validator](not mandatory)
	// * it can unbond a portion of its funds from the stash account.
	// * it can re-bond a portion of the funds scheduled to unlock.
	ExtBuilder::default()
		.nominate(false)
		.build()
		.execute_with(|| {
			// Set payee to controller. avoids confusion
			assert_ok!(Staking::set_payee(
				Origin::signed(10),
				RewardDestination::Controller
			));

			// Give account 11 some large free balance greater than total
			let _ = Balances::make_free_balance_be(&11, 1000000);

			// confirm that 10 is a normal validator and gets paid at the end of the era.
			mock::start_era(1);
Marcio Diaz's avatar
Marcio Diaz committed

			// Initial state of 10
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 1000,
					unlocking: vec![],
					claimed_rewards: vec![],
			mock::start_era(2);
Gavin Wood's avatar
Gavin Wood committed
			assert_eq!(Staking::active_era().unwrap().index, 2);
Marcio Diaz's avatar
Marcio Diaz committed

			// Try to rebond some funds. We get an error since no fund is unbonded.
			assert_noop!(
				Staking::rebond(Origin::signed(10), 500),
				Error::<Test>::NoUnlockChunk,
			);

			// Unbond almost all of the funds in stash.
			Staking::unbond(Origin::signed(10), 900).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 100,
					unlocking: vec![UnlockChunk {
						value: 900,
Gavin Wood's avatar
Gavin Wood committed
						era: 2 + 3,
					}],
					claimed_rewards: vec![],
Marcio Diaz's avatar
Marcio Diaz committed
				})
			);

			// Re-bond all the funds unbonded.
			Staking::rebond(Origin::signed(10), 900).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 1000,
					unlocking: vec![],
					claimed_rewards: vec![],
Marcio Diaz's avatar
Marcio Diaz committed
				})
			);

			// Unbond almost all of the funds in stash.
			Staking::unbond(Origin::signed(10), 900).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 100,
					unlocking: vec![UnlockChunk { value: 900, era: 5 }],
					claimed_rewards: vec![],
Marcio Diaz's avatar
Marcio Diaz committed
				})
			);

			// Re-bond part of the funds unbonded.
			Staking::rebond(Origin::signed(10), 500).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 600,
					unlocking: vec![UnlockChunk { value: 400, era: 5 }],
					claimed_rewards: vec![],
Marcio Diaz's avatar
Marcio Diaz committed
				})
			);

			// Re-bond the remainder of the funds unbonded.
			Staking::rebond(Origin::signed(10), 500).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 1000,
Gavin Wood's avatar
Gavin Wood committed
					unlocking: vec![],
					claimed_rewards: vec![],
Marcio Diaz's avatar
Marcio Diaz committed
				})
			);

			// Unbond parts of the funds in stash.
			Staking::unbond(Origin::signed(10), 300).unwrap();
			Staking::unbond(Origin::signed(10), 300).unwrap();
			Staking::unbond(Origin::signed(10), 300).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 100,
					unlocking: vec![
						UnlockChunk { value: 300, era: 5 },
						UnlockChunk { value: 300, era: 5 },
						UnlockChunk { value: 300, era: 5 },
Gavin Wood's avatar
Gavin Wood committed
					],
					claimed_rewards: vec![],
Marcio Diaz's avatar
Marcio Diaz committed
				})
			);

			// Re-bond part of the funds unbonded.
			Staking::rebond(Origin::signed(10), 500).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 600,
					unlocking: vec![
						UnlockChunk { value: 300, era: 5 },
						UnlockChunk { value: 100, era: 5 },
Gavin Wood's avatar
Gavin Wood committed
					],
					claimed_rewards: vec![],
#[test]
fn rebond_is_fifo() {
	// Rebond should proceed by reversing the most recent bond operations.
	ExtBuilder::default()
		.nominate(false)
		.build()
		.execute_with(|| {
			// Set payee to controller. avoids confusion
			assert_ok!(Staking::set_payee(
				Origin::signed(10),
				RewardDestination::Controller
			));

			// Give account 11 some large free balance greater than total
			let _ = Balances::make_free_balance_be(&11, 1000000);

			// confirm that 10 is a normal validator and gets paid at the end of the era.
			mock::start_era(1);

			// Initial state of 10
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 1000,
					unlocking: vec![],
					claimed_rewards: vec![],
			mock::start_era(2);

			// Unbond some of the funds in stash.
			Staking::unbond(Origin::signed(10), 400).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 600,
					unlocking: vec![
						UnlockChunk { value: 400, era: 2 + 3 },
Gavin Wood's avatar
Gavin Wood committed
					],
					claimed_rewards: vec![],
			mock::start_era(3);

			// Unbond more of the funds in stash.
			Staking::unbond(Origin::signed(10), 300).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 300,
					unlocking: vec![
						UnlockChunk { value: 400, era: 2 + 3 },
						UnlockChunk { value: 300, era: 3 + 3 },
Gavin Wood's avatar
Gavin Wood committed
					],
					claimed_rewards: vec![],
			mock::start_era(4);

			// Unbond yet more of the funds in stash.
			Staking::unbond(Origin::signed(10), 200).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 100,
					unlocking: vec![
						UnlockChunk { value: 400, era: 2 + 3 },
						UnlockChunk { value: 300, era: 3 + 3 },
						UnlockChunk { value: 200, era: 4 + 3 },
Gavin Wood's avatar
Gavin Wood committed
					],
					claimed_rewards: vec![],
				})
			);

			// Re-bond half of the unbonding funds.
			Staking::rebond(Origin::signed(10), 400).unwrap();
			assert_eq!(
				Staking::ledger(&10),
				Some(StakingLedger {
					stash: 11,
					total: 1000,
					active: 500,
					unlocking: vec![
						UnlockChunk { value: 400, era: 2 + 3 },
						UnlockChunk { value: 100, era: 3 + 3 },
Gavin Wood's avatar
Gavin Wood committed
					],
					claimed_rewards: vec![],
Gavin Wood's avatar
Gavin Wood committed
fn reward_to_stake_works() {
	ExtBuilder::default().nominate(false).fair(false).build_and_execute(|| {
		// Confirm validator count is 2
		assert_eq!(Staking::validator_count(), 2);
		// Confirm account 10 and 20 are validators
		assert!(<Validators<Test>>::contains_key(&11) && <Validators<Test>>::contains_key(&21));
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000);
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 2000);
		// Give the man some money.
		let _ = Balances::make_free_balance_be(&10, 1000);
		let _ = Balances::make_free_balance_be(&20, 1000);
Gavin Wood's avatar
Gavin Wood committed
		// Bypass logic and change current exposure
		ErasStakers::<Test>::insert(0, 21, Exposure { total: 69, own: 69, others: vec![] });
		// Now lets lower account 20 stake
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69);
		<Ledger<Test>>::insert(&20, StakingLedger { stash: 21, total: 69, active: 69, unlocking: vec![], claimed_rewards: vec![] });
		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_0 = current_total_payout_for_duration(3000);
		assert!(total_payout_0 > 100); // Test is meaningful if reward something
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
		<Module<Test>>::reward_by_ids(vec![(21, 1)]);
		// New era --> rewards are paid --> stakes are changed
		mock::start_era(1);
Gavin Wood's avatar
Gavin Wood committed
		mock::make_all_reward_payment(0);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000);
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69);

		let _11_balance = Balances::free_balance(&11);
		assert_eq!(_11_balance, 1000 + total_payout_0 / 2);
Gavin Wood's avatar
Gavin Wood committed
		// Trigger another new era as the info are frozen before the era start.
		mock::start_era(2);
Gavin Wood's avatar
Gavin Wood committed

		// -- new infos
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000 + total_payout_0 / 2);
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69 + total_payout_0 / 2);

#[test]
fn on_free_balance_zero_stash_removes_validator() {
	// Tests that validator storage items are cleaned up when stash is empty
	// Tests that storage items are untouched when controller is empty
	ExtBuilder::default().existential_deposit(10).build_and_execute(|| {
		// Check the balance of the validator account
		assert_eq!(Balances::free_balance(10), 256);
		// Check the balance of the stash account
		assert_eq!(Balances::free_balance(11), 256000);
		// Check these two accounts are bonded
		assert_eq!(Staking::bonded(&11), Some(10));

		// Set some storage items which we expect to be cleaned up
		// Set payee information
		assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash));

		// Check storage items that should be cleaned up
		assert!(<Ledger<Test>>::contains_key(&10));
		assert!(<Bonded<Test>>::contains_key(&11));
		assert!(<Validators<Test>>::contains_key(&11));
		assert!(<Payee<Test>>::contains_key(&11));

		// Reduce free_balance of controller to 0
		let _ = Balances::slash(&10, Balance::max_value());

		// Check the balance of the stash account has not been touched
		assert_eq!(Balances::free_balance(11), 256000);
		// Check these two accounts are still bonded
		assert_eq!(Staking::bonded(&11), Some(10));

		// Check storage items have not changed
		assert!(<Ledger<Test>>::contains_key(&10));
		assert!(<Bonded<Test>>::contains_key(&11));
		assert!(<Validators<Test>>::contains_key(&11));
		assert!(<Payee<Test>>::contains_key(&11));

		// Reduce free_balance of stash to 0
		let _ = Balances::slash(&11, Balance::max_value());
		// Check total balance of stash
		assert_eq!(Balances::total_balance(&11), 0);
Gavin Wood's avatar
Gavin Wood committed
		// Reap the stash
		assert_ok!(Staking::reap_stash(Origin::none(), 11, 0));
Gavin Wood's avatar
Gavin Wood committed

		// Check storage items do not exist
		assert!(!<Ledger<Test>>::contains_key(&10));
		assert!(!<Bonded<Test>>::contains_key(&11));
		assert!(!<Validators<Test>>::contains_key(&11));
		assert!(!<Nominators<Test>>::contains_key(&11));
		assert!(!<Payee<Test>>::contains_key(&11));
	});
}

#[test]
fn on_free_balance_zero_stash_removes_nominator() {
	// Tests that nominator storage items are cleaned up when stash is empty
	// Tests that storage items are untouched when controller is empty
	ExtBuilder::default().existential_deposit(10).build_and_execute(|| {
		// Make 10 a nominator
		assert_ok!(Staking::nominate(Origin::signed(10), vec![20]));
		// Check that account 10 is a nominator
		assert!(<Nominators<Test>>::contains_key(11));
		// Check the balance of the nominator account
		assert_eq!(Balances::free_balance(10), 256);
		// Check the balance of the stash account
		assert_eq!(Balances::free_balance(11), 256000);

		// Set payee information
		assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash));

		// Check storage items that should be cleaned up
		assert!(<Ledger<Test>>::contains_key(&10));
		assert!(<Bonded<Test>>::contains_key(&11));
		assert!(<Nominators<Test>>::contains_key(&11));
		assert!(<Payee<Test>>::contains_key(&11));

		// Reduce free_balance of controller to 0
		let _ = Balances::slash(&10, Balance::max_value());
		// Check total balance of account 10
		assert_eq!(Balances::total_balance(&10), 0);

		// Check the balance of the stash account has not been touched
		assert_eq!(Balances::free_balance(11), 256000);
		// Check these two accounts are still bonded
		assert_eq!(Staking::bonded(&11), Some(10));

		// Check storage items have not changed
		assert!(<Ledger<Test>>::contains_key(&10));
		assert!(<Bonded<Test>>::contains_key(&11));
		assert!(<Nominators<Test>>::contains_key(&11));
		assert!(<Payee<Test>>::contains_key(&11));

		// Reduce free_balance of stash to 0
		let _ = Balances::slash(&11, Balance::max_value());
		// Check total balance of stash
		assert_eq!(Balances::total_balance(&11), 0);
Gavin Wood's avatar
Gavin Wood committed
		// Reap the stash
		assert_ok!(Staking::reap_stash(Origin::none(), 11, 0));
Gavin Wood's avatar
Gavin Wood committed

		// Check storage items do not exist
		assert!(!<Ledger<Test>>::contains_key(&10));
		assert!(!<Bonded<Test>>::contains_key(&11));
		assert!(!<Validators<Test>>::contains_key(&11));
		assert!(!<Nominators<Test>>::contains_key(&11));
		assert!(!<Payee<Test>>::contains_key(&11));
}


#[test]
fn switching_roles() {
	// Test that it should be possible to switch between roles (nominator, validator, idle) with minimal overhead.
	ExtBuilder::default().nominate(false).build_and_execute(|| {
		// Reset reward destination
		for i in &[10, 20] { assert_ok!(Staking::set_payee(Origin::signed(*i), RewardDestination::Controller)); }

		assert_eq_uvec!(validator_controllers(), vec![20, 10]);

		// put some money in account that we'll use.
		for i in 1..7 { let _ = Balances::deposit_creating(&i, 5000); }

		// add 2 nominators
		assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::Controller));
		assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 5]));

		assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Controller));
		assert_ok!(Staking::nominate(Origin::signed(4), vec![21, 1]));

		// add a new validator candidate
		assert_ok!(Staking::bond(Origin::signed(5), 6, 1000, RewardDestination::Controller));
		assert_ok!(Staking::validate(Origin::signed(6), ValidatorPrefs::default()));

		mock::start_era(1);
		// with current nominators 10 and 5 have the most stake
		assert_eq_uvec!(validator_controllers(), vec![6, 10]);
		// 2 decides to be a validator. Consequences:
		assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default()));
		// new stakes:
		// 10: 1000 self vote
		// 20: 1000 self vote + 250 vote
		// 6 : 1000 self vote
		// 2 : 2000 self vote + 250 vote.
		mock::start_era(2);
		assert_eq_uvec!(validator_controllers(), vec![2, 20]);
	});
}

#[test]
fn wrong_vote_is_null() {
	ExtBuilder::default().nominate(false).validator_pool(true).build_and_execute(|| {
		assert_eq_uvec!(validator_controllers(), vec![40, 30]);

		// put some money in account that we'll use.
		for i in 1..3 { let _ = Balances::deposit_creating(&i, 5000); }

		// add 1 nominators
		assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default()));
		assert_ok!(Staking::nominate(Origin::signed(2), vec![
			11, 21, 			// good votes
			1, 2, 15, 1000, 25  // crap votes. No effect.
		]));

		mock::start_era(1);
		assert_eq_uvec!(validator_controllers(), vec![20, 10]);
	});
}

#[test]
fn bond_with_no_staked_value() {
	// Behavior when someone bonds with no staked value.
	// Particularly when she votes and the candidate is elected.
	ExtBuilder::default()
		.validator_count(3)
		.existential_deposit(5)
		.nominate(false)
		.minimum_validator_count(1)
		.build()
		.execute_with(|| {
			// Can't bond with 1
			assert_noop!(
				Staking::bond(Origin::signed(1), 2, 1, RewardDestination::Controller),
				Error::<Test>::InsufficientValue,
			);
			// bonded with absolute minimum value possible.
			assert_ok!(Staking::bond(Origin::signed(1), 2, 5, RewardDestination::Controller));
			assert_eq!(Balances::locks(&1)[0].amount, 5);
			// unbonding even 1 will cause all to be unbonded.
			assert_ok!(Staking::unbond(Origin::signed(2), 1));
			assert_eq!(
				Staking::ledger(2),
				Some(StakingLedger {
					stash: 1,
					active: 0,
					total: 5,
Gavin Wood's avatar
Gavin Wood committed
					unlocking: vec![UnlockChunk {value: 5, era: 3}],
					claimed_rewards: vec![],
			mock::start_era(1);
			mock::start_era(2);
			assert_ok!(Staking::withdraw_unbonded(Origin::signed(2), 0));
			assert!(Staking::ledger(2).is_some());
			assert_eq!(Balances::locks(&1)[0].amount, 5);
			mock::start_era(3);
			// poof. Account 1 is removed from the staking system.
			assert_ok!(Staking::withdraw_unbonded(Origin::signed(2), 0));
			assert!(Staking::ledger(2).is_none());
			assert_eq!(Balances::locks(&1).len(), 0);
		});
Gavin Wood's avatar
Gavin Wood committed
fn bond_with_little_staked_value_bounded() {
	// Behavior when someone bonds with little staked value.
	// Particularly when she votes and the candidate is elected.
	ExtBuilder::default()
		.validator_count(3)
		.nominate(false)
		.minimum_validator_count(1)
		.build()
		.execute_with(|| {
			// setup
			assert_ok!(Staking::chill(Origin::signed(30)));
			assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller));
			let init_balance_2 = Balances::free_balance(&2);
			let init_balance_10 = Balances::free_balance(&10);

			// Stingy validator.
			assert_ok!(Staking::bond(Origin::signed(1), 2, 1, RewardDestination::Controller));
			assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default()));

Gavin Wood's avatar
Gavin Wood committed
			// reward era 0
			let total_payout_0 = current_total_payout_for_duration(3000);
			assert!(total_payout_0 > 100); // Test is meaningful if reward something
			reward_all_elected();
			mock::start_era(1);
Gavin Wood's avatar
Gavin Wood committed
			mock::make_all_reward_payment(0);

			// 2 is elected.
			assert_eq_uvec!(validator_controllers(), vec![20, 10, 2]);
Gavin Wood's avatar
Gavin Wood committed
			// And has minimal stake
			assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 2).total, 0);
			assert_eq!(Balances::free_balance(10), init_balance_10 + total_payout_0 / 3);
			// no rewards paid to 2. This was initial election.
			assert_eq!(Balances::free_balance(2), init_balance_2);
Gavin Wood's avatar
Gavin Wood committed
			// reward era 1
			let total_payout_1 = current_total_payout_for_duration(3000);
			assert!(total_payout_1 > 100); // Test is meaningful if reward something
			reward_all_elected();
			mock::start_era(2);
Gavin Wood's avatar
Gavin Wood committed
			mock::make_all_reward_payment(1);

			assert_eq_uvec!(validator_controllers(), vec![20, 10, 2]);
Gavin Wood's avatar
Gavin Wood committed
			assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 2).total, 0);
			assert_eq!(Balances::free_balance(2), init_balance_2 + total_payout_1 / 3);
			assert_eq!(
				Balances::free_balance(&10),
				init_balance_10 + total_payout_0 / 3 + total_payout_1 / 3,
			);
		});
#[test]
fn bond_with_duplicate_vote_should_be_ignored_by_npos_election() {
	ExtBuilder::default()
		.validator_count(2)
		.nominate(false)
		.minimum_validator_count(1)
		.build()
		.execute_with(|| {
			// disable the nominator
			assert_ok!(Staking::chill(Origin::signed(100)));
			// make stakes equal.
			assert_ok!(Staking::bond_extra(Origin::signed(31), 999));

			assert_eq!(
				<Validators<Test>>::iter()
					.map(|(v, _)| (v, Staking::ledger(v - 1).unwrap().total))
					.collect::<Vec<_>>(),
				vec![(31, 1000), (21, 1000), (11, 1000)],
			);
			assert!(<Nominators<Test>>::iter().map(|(n, _)| n).collect::<Vec<_>>().is_empty());

			// give the man some money
			let initial_balance = 1000;
			for i in [1, 2, 3, 4,].iter() {
				let _ = Balances::make_free_balance_be(i, initial_balance);
			}

			assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller));
			assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 11, 11, 21, 31,]));

			assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::Controller));
			assert_ok!(Staking::nominate(Origin::signed(4), vec![21, 31]));

			// winners should be 21 and 31. Otherwise this election is taking duplicates into account.
			let sp_npos_elections::ElectionResult {
				winners,
				assignments,
Kian Paimani's avatar
Kian Paimani committed
			} = Staking::do_phragmen::<Perbill>(0).unwrap();
			let winners = sp_npos_elections::to_without_backing(winners);

			assert_eq!(winners, vec![31, 21]);
			// only distribution to 21 and 31.
			assert_eq!(assignments.iter().find(|a| a.who == 1).unwrap().distribution.len(), 2);
		});
}

#[test]
fn bond_with_duplicate_vote_should_be_ignored_by_npos_election_elected() {
	// same as above but ensures that even when the duple is being elected, everything is sane.
	ExtBuilder::default()
		.validator_count(2)
		.nominate(false)
		.minimum_validator_count(1)
		.build()
		.execute_with(|| {
			// disable the nominator
			assert_ok!(Staking::chill(Origin::signed(100)));
			// make stakes equal.
			assert_ok!(Staking::bond_extra(Origin::signed(31), 99));

			assert_eq!(
				<Validators<Test>>::iter()
					.map(|(v, _)| (v, Staking::ledger(v - 1).unwrap().total))
					.collect::<Vec<_>>(),
				vec![(31, 100), (21, 1000), (11, 1000)],
			);
			assert!(<Nominators<Test>>::iter().map(|(n, _)| n).collect::<Vec<_>>().is_empty());

			// give the man some money
			let initial_balance = 1000;
			for i in [1, 2, 3, 4,].iter() {
				let _ = Balances::make_free_balance_be(i, initial_balance);
			}

			assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller));
			assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 11, 11, 21, 31,]));

			assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::Controller));
			assert_ok!(Staking::nominate(Origin::signed(4), vec![21, 31]));

			// winners should be 21 and 31. Otherwise this election is taking duplicates into account.

			let sp_npos_elections::ElectionResult {
				winners,
				assignments,
Kian Paimani's avatar
Kian Paimani committed
			} = Staking::do_phragmen::<Perbill>(0).unwrap();

			let winners = sp_npos_elections::to_without_backing(winners);
			assert_eq!(winners, vec![21, 11]);
			// only distribution to 21 and 31.
			assert_eq!(assignments.iter().find(|a| a.who == 1).unwrap().distribution.len(), 2);
		});
}

fn new_era_elects_correct_number_of_validators() {
	ExtBuilder::default()
		.nominate(true)
		.validator_pool(true)
		.fair(true)
		.validator_count(1)
		.build()
		.execute_with(|| {
			assert_eq!(Staking::validator_count(), 1);
			assert_eq!(validator_controllers().len(), 1);

			Session::on_initialize(System::block_number());

			assert_eq!(validator_controllers().len(), 1);
		})
fn phragmen_should_not_overflow() {
	ExtBuilder::default().nominate(false).build_and_execute(|| {
		// This is the maximum value that we can have as the outcome of CurrencyToVote.
		type Votes = u64;
		let _ = Staking::chill(Origin::signed(10));
		let _ = Staking::chill(Origin::signed(20));

		bond_validator(3, 2, Votes::max_value() as Balance);
		bond_validator(5, 4, Votes::max_value() as Balance);
		bond_nominator(7, 6, Votes::max_value() as Balance, vec![3, 5]);
		bond_nominator(9, 8, Votes::max_value() as Balance, vec![3, 5]);
		mock::start_era(1);
		assert_eq_uvec!(validator_controllers(), vec![4, 2]);
		// We can safely convert back to values within [u64, u128].
		assert!(Staking::eras_stakers(active_era(), 3).total > Votes::max_value() as Balance);
		assert!(Staking::eras_stakers(active_era(), 5).total > Votes::max_value() as Balance);
fn reward_validator_slashing_validator_does_not_overflow() {
	ExtBuilder::default().build_and_execute(|| {
		let stake = u64::max_value() as Balance * 2;
		let reward_slash = u64::max_value() as Balance * 2;

		// Assert multiplication overflows in balance arithmetic.
		assert!(stake.checked_mul(reward_slash).is_none());

		// Set staker
		let _ = Balances::make_free_balance_be(&11, stake);
Gavin Wood's avatar
Gavin Wood committed

		let exposure = Exposure::<AccountId, Balance> { total: stake, own: stake, others: vec![] };
		let reward = EraRewardPoints::<AccountId> {
			total: 1,
			individual: vec![(11, 1)].into_iter().collect(),
		};
Gavin Wood's avatar
Gavin Wood committed
		ErasRewardPoints::<Test>::insert(0, reward);
		ErasStakers::<Test>::insert(0, 11, &exposure);
		ErasStakersClipped::<Test>::insert(0, 11, exposure);
		ErasValidatorReward::<Test>::insert(0, stake);
		assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 0));
		assert_eq!(Balances::total_balance(&11), stake * 2);

		// Set staker
		let _ = Balances::make_free_balance_be(&11, stake);
		let _ = Balances::make_free_balance_be(&2, stake);

		// only slashes out of bonded stake are applied. without this line,
		// it is 0.
		Staking::bond(Origin::signed(2), 20000, stake - 1, RewardDestination::default()).unwrap();
Gavin Wood's avatar
Gavin Wood committed
		// Override exposure of 11
		ErasStakers::<Test>::insert(0, 11, Exposure {
			total: stake,
			own: 1,
			others: vec![ IndividualExposure { who: 2, value: stake - 1 }]
		});
		on_offence_now(
			&[
				OffenceDetails {
Gavin Wood's avatar
Gavin Wood committed
					offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
					reporters: vec![],
				},
			],
			&[Perbill::from_percent(100)],
		);

		assert_eq!(Balances::total_balance(&11), stake - 1);
		assert_eq!(Balances::total_balance(&2), 1);
	})
}

#[test]
fn reward_from_authorship_event_handler_works() {
	ExtBuilder::default().build_and_execute(|| {
		use pallet_authorship::EventHandler;
		assert_eq!(<pallet_authorship::Module<Test>>::author(), 11);

		<Module<Test>>::note_author(11);
		<Module<Test>>::note_uncle(21, 1);
thiolliere's avatar
thiolliere committed
		// Rewarding the same two times works.
		<Module<Test>>::note_uncle(11, 1);

		// Not mandatory but must be coherent with rewards
Gavin Wood's avatar
Gavin Wood committed
		assert_eq_uvec!(Session::validators(), vec![11, 21]);
thiolliere's avatar
thiolliere committed
		// 21 is rewarded as an uncle producer
		// 11 is rewarded as a block producer and uncle referencer and uncle producer
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(
			ErasRewardPoints::<Test>::get(Staking::active_era().unwrap().index),
			EraRewardPoints {
				individual: vec![(11, 20 + 2 * 2 + 1), (21, 1)].into_iter().collect(),
				total: 26,
			},
		);
thiolliere's avatar
thiolliere committed
	})
}

#[test]
fn add_reward_points_fns_works() {
	ExtBuilder::default().build_and_execute(|| {
thiolliere's avatar
thiolliere committed
		// Not mandatory but must be coherent with rewards
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::validators(), vec![21, 11]);
Gavin Wood's avatar
Gavin Wood committed
		<Module<Test>>::reward_by_ids(vec![
			(21, 1),
			(11, 1),
			(11, 1),
thiolliere's avatar
thiolliere committed
		]);

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

Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(
			ErasRewardPoints::<Test>::get(Staking::active_era().unwrap().index),
			EraRewardPoints {
				individual: vec![(11, 4), (21, 2)].into_iter().collect(),
				total: 6,
			},
		);
Gavin Wood's avatar
Gavin Wood committed

#[test]
fn unbonded_balance_is_not_slashable() {
	ExtBuilder::default().build_and_execute(|| {
Gavin Wood's avatar
Gavin Wood committed
		// total amount staked is slashable.
		assert_eq!(Staking::slashable_balance_of(&11), 1000);

		assert_ok!(Staking::unbond(Origin::signed(10),  800));

		// only the active portion.
		assert_eq!(Staking::slashable_balance_of(&11), 200);
	})
}

#[test]
fn era_is_always_same_length() {
	// This ensures that the sessions is always of the same length if there is no forcing no
	// session changes.
	ExtBuilder::default().build_and_execute(|| {
		let session_per_era = <SessionsPerEra as Get<SessionIndex>>::get();

		mock::start_era(1);
		assert_eq!(Staking::eras_start_session_index(current_era()).unwrap(), session_per_era);
		mock::start_era(2);
		assert_eq!(Staking::eras_start_session_index(current_era()).unwrap(), session_per_era * 2u32);

		let session = Session::current_index();
		ForceEra::put(Forcing::ForceNew);
		advance_session();
Gavin Wood's avatar
Gavin Wood committed
		advance_session();
		assert_eq!(current_era(), 3);
		assert_eq!(Staking::eras_start_session_index(current_era()).unwrap(), session + 2);
		mock::start_era(4);
		assert_eq!(Staking::eras_start_session_index(current_era()).unwrap(), session + 2u32 + session_per_era);