Skip to content
tests.rs 73.5 KiB
Newer Older
		assert_eq!(Staking::stakers(5).total, u64::max_value());
fn phragmen_large_scale_test() {
	with_externalities(&mut ExtBuilder::default()
		.nominate(false)
		.minimum_validator_count(1)
		.validator_count(20)
		.build(),
	|| {
		let _ = Staking::chill(Origin::signed(10));
		let _ = Staking::chill(Origin::signed(20));
		let _ = Staking::chill(Origin::signed(30));
		let prefix = 200;

		bond_validator(prefix + 2,  1);
		bond_validator(prefix + 4,  100);
		bond_validator(prefix + 6,  1000000);
		bond_validator(prefix + 8,  100000000001000);
		bond_validator(prefix + 10, 100000000002000);
		bond_validator(prefix + 12, 100000000003000);
		bond_validator(prefix + 14, 400000000000000);
		bond_validator(prefix + 16, 400000000001000);
		bond_validator(prefix + 18, 18000000000000000);
		bond_validator(prefix + 20, 20000000000000000);
		bond_validator(prefix + 22, 500000000000100000);
		bond_validator(prefix + 24, 500000000000200000);

		bond_nominator(50, 990000000000000000, vec![
			prefix + 3,
			prefix + 5,
			prefix + 7,
			prefix + 9,
			prefix + 11,
			prefix + 13,
			prefix + 15,
			prefix + 17,
			prefix + 19,
			prefix + 21,
			prefix + 23,
			prefix + 25]
		check_exposure_all();
		check_nominator_all();
#[test]
fn phragmen_large_scale_test_2() {
	with_externalities(&mut ExtBuilder::default()
		.nominate(false)
		.minimum_validator_count(1)
		.validator_count(2)
		.build(),
	|| {
		let _ = Staking::chill(Origin::signed(10));
		let _ = Staking::chill(Origin::signed(20));
		let nom_budget: u64 = 1_000_000_000_000_000_000;
		let c_budget: u64 = 4_000_000;

		bond_validator(2, c_budget as u64);
		bond_validator(4, c_budget as u64);

		bond_nominator(50, nom_budget, vec![3, 5]);


		// Each exposure => total == own + sum(others)
		check_exposure_all();
		check_nominator_all();

		assert_total_expo(3, nom_budget / 2 + c_budget);
		assert_total_expo(5, nom_budget / 2 + c_budget);

#[test]
fn reward_validator_slashing_validator_doesnt_overflow() {
	with_externalities(&mut ExtBuilder::default()
		.build(),
	|| {
		let stake = u32::max_value() as u64 * 2;
		let reward_slash = u32::max_value() as u64 * 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);
		<Stakers<Test>>::insert(&11, Exposure { total: stake, own: stake, others: vec![] });

		// Check reward
		let _ = Staking::reward_validator(&11, reward_slash);
		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);
		<Stakers<Test>>::insert(&11, Exposure { total: stake, own: 1, others: vec![
			IndividualExposure { who: 2, value: stake - 1 }
		]});

		// Check slashing
		Staking::slash_validator(&11, reward_slash);
		assert_eq!(Balances::total_balance(&11), stake - 1);
		assert_eq!(Balances::total_balance(&2), 1);
	})
}

#[test]
fn reward_from_authorship_event_handler_works() {
	with_externalities(&mut ExtBuilder::default()
		.build(),
	|| {
		use authorship::EventHandler;

		assert_eq!(<authorship::Module<Test>>::author(), 11);

		<Module<Test>>::note_author(11);
		<Module<Test>>::note_uncle(21, 1);
		// An uncle author that is not currently elected doesn't get rewards,
		// but the block producer does get reward for referencing it.
		<Module<Test>>::note_uncle(31, 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
		assert_eq!(<CurrentElected<Test>>::get(), vec![21, 11]);

thiolliere's avatar
thiolliere committed
		// 21 is rewarded as an uncle producer
		// 11 is rewarded as a block procuder and uncle referencer and uncle producer
		assert_eq!(CurrentEraRewards::get().rewards, vec![1, 20+2*3 + 1]);
		assert_eq!(CurrentEraRewards::get().total, 28);
	})
}

#[test]
fn add_reward_points_fns_works() {
	with_externalities(&mut ExtBuilder::default()
		.build(),
	|| {
		let validators = <Module<Test>>::current_elected();
		// Not mandatory but must be coherent with rewards
		assert_eq!(validators, vec![21, 11]);

		<Module<Test>>::reward_by_indices(vec![
			(0, 1),
			(1, 1),
			(2, 1),
			(1, 1),
		]);

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

		assert_eq!(CurrentEraRewards::get().rewards, vec![2, 4]);
		assert_eq!(CurrentEraRewards::get().total, 6);
Gavin Wood's avatar
Gavin Wood committed

#[test]
fn unbonded_balance_is_not_slashable() {
	with_externalities(&mut ExtBuilder::default().build(), || {
		// 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);
	})
}