git.delta.rocks / unique-network / refs/commits / 70abe578b643

difftreelog

source

pallets/collator-selection/src/tests.rs15.5 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// Original license:18// Copyright (C) 2021 Parity Technologies (UK) Ltd.19// SPDX-License-Identifier: Apache-2.02021// Licensed under the Apache License, Version 2.0 (the "License");22// you may not use this file except in compliance with the License.23// You may obtain a copy of the License at24//25// 	http://www.apache.org/licenses/LICENSE-2.026//27// Unless required by applicable law or agreed to in writing, software28// distributed under the License is distributed on an "AS IS" BASIS,29// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.30// See the License for the specific language governing permissions and31// limitations under the License.3233use crate as collator_selection;34use crate::{mock::*, Error};35use frame_support::{36	assert_noop, assert_ok,37	traits::{Currency, GenesisBuild, OnInitialize},38};39use pallet_balances::Error as BalancesError;40use sp_runtime::traits::BadOrigin;4142fn get_license_and_onboard(account_id: <Test as frame_system::Config>::AccountId) {43	assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(44		account_id45	)));46	assert_ok!(CollatorSelection::onboard(RuntimeOrigin::signed(47		account_id48	)));49}5051#[test]52fn basic_setup_works() {53	new_test_ext().execute_with(|| {54		assert_eq!(CollatorSelection::desired_collators(), 5);55		assert_eq!(CollatorSelection::license_bond(), 10);5657		assert!(CollatorSelection::candidates().is_empty());58		assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);59	});60}6162#[test]63fn it_should_add_invulnerables() {64	new_test_ext().execute_with(|| {65		assert_ok!(CollatorSelection::add_invulnerable(66			RuntimeOrigin::signed(RootAccount::get()),67			168		));69		assert_ok!(CollatorSelection::add_invulnerable(70			RuntimeOrigin::signed(RootAccount::get()),71			272		));73		assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);7475		// cannot set with non-root.76		assert_noop!(77			CollatorSelection::add_invulnerable(RuntimeOrigin::signed(1), 3),78			BadOrigin79		);8081		// cannot set invulnerables without associated validator keys82		assert_noop!(83			CollatorSelection::add_invulnerable(RuntimeOrigin::signed(RootAccount::get()), 7),84			Error::<Test>::ValidatorNotRegistered85		);86	});87}8889#[test]90fn it_should_remove_invulnerables() {91	new_test_ext().execute_with(|| {92		assert_ok!(CollatorSelection::add_invulnerable(93			RuntimeOrigin::signed(RootAccount::get()),94			195		));96		assert_ok!(CollatorSelection::add_invulnerable(97			RuntimeOrigin::signed(RootAccount::get()),98			299		));100101		// cannot remove with non-root.102		assert_noop!(103			CollatorSelection::remove_invulnerable(RuntimeOrigin::signed(1), 3),104			BadOrigin105		);106107		assert_ok!(CollatorSelection::remove_invulnerable(108			RuntimeOrigin::signed(RootAccount::get()),109			2110		));111		assert_eq!(CollatorSelection::invulnerables(), vec![1]);112113		// cannot remove an invulnerable if there would be 0 invulnerables.114		assert_noop!(115			CollatorSelection::remove_invulnerable(RuntimeOrigin::signed(RootAccount::get()), 1),116			Error::<Test>::TooFewInvulnerables117		);118	});119}120121#[test]122fn set_desired_collators_works() {123	new_test_ext().execute_with(|| {124		// given125		assert_eq!(CollatorSelection::desired_collators(), 5);126127		// can set128		assert_ok!(CollatorSelection::set_desired_collators(129			RuntimeOrigin::signed(RootAccount::get()),130			7131		));132		assert_eq!(CollatorSelection::desired_collators(), 7);133134		// rejects bad origin135		assert_noop!(136			CollatorSelection::set_desired_collators(RuntimeOrigin::signed(1), 8),137			BadOrigin138		);139	});140}141142#[test]143fn set_license_bond() {144	new_test_ext().execute_with(|| {145		// given146		assert_eq!(CollatorSelection::license_bond(), 10);147148		// can set149		assert_ok!(CollatorSelection::set_license_bond(150			RuntimeOrigin::signed(RootAccount::get()),151			7152		));153		assert_eq!(CollatorSelection::license_bond(), 7);154155		// rejects bad origin.156		assert_noop!(157			CollatorSelection::set_license_bond(RuntimeOrigin::signed(1), 8),158			BadOrigin159		);160	});161}162163#[test]164fn cannot_onboard_candidate_with_no_license() {165	new_test_ext().execute_with(|| {166		// can't onboard a candidate who did not get a license.167		assert_noop!(168			CollatorSelection::onboard(RuntimeOrigin::signed(3)),169			Error::<Test>::NoLicense,170		);171172		// but give it a license and welcome aboard.173		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));174		assert_ok!(CollatorSelection::onboard(RuntimeOrigin::signed(3)));175	})176}177178#[test]179fn cannot_onboard_candidate_if_too_many() {180	new_test_ext().execute_with(|| {181		// reset desired candidates182		<crate::DesiredCollators<Test>>::put(0);183184		// can still get a license.185		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(4)));186187		// can't accept anyone anymore.188		assert_noop!(189			CollatorSelection::onboard(RuntimeOrigin::signed(4)),190			Error::<Test>::TooManyCandidates,191		);192193		// reset desired candidates to invulnerables + 1194		<crate::DesiredCollators<Test>>::put(3);195		assert_ok!(CollatorSelection::onboard(RuntimeOrigin::signed(4)));196197		// but no more.198		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(5)));199		assert_noop!(200			CollatorSelection::onboard(RuntimeOrigin::signed(5)),201			Error::<Test>::TooManyCandidates,202		);203	})204}205206#[test]207fn cannot_obtain_license_if_keys_not_registered() {208	new_test_ext().execute_with(|| {209		// can't 7 because keys not registered.210		assert_noop!(211			CollatorSelection::get_license(RuntimeOrigin::signed(7)),212			Error::<Test>::ValidatorNotRegistered213		);214	})215}216217#[test]218fn cannot_obtain_license_if_poor() {219	new_test_ext().execute_with(|| {220		assert_eq!(Balances::free_balance(&3), 100);221		assert_eq!(Balances::free_balance(&33), 0);222223		// works224		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));225226		// poor227		assert_noop!(228			CollatorSelection::get_license(RuntimeOrigin::signed(33)),229			BalancesError::<Test>::InsufficientBalance,230		);231	});232}233234#[test]235fn cannot_onboard_dupe_candidate() {236	new_test_ext().execute_with(|| {237		// can add 3 as candidate238		get_license_and_onboard(3);239		assert_eq!(CollatorSelection::licenses(3), 10);240		assert_eq!(CollatorSelection::candidates(), vec![3]);241		assert_eq!(CollatorSelection::last_authored_block(3), 10);242		assert_eq!(Balances::free_balance(3), 90);243244		// but no more245		assert_noop!(246			CollatorSelection::get_license(RuntimeOrigin::signed(3)),247			Error::<Test>::AlreadyHoldingLicense,248		);249		assert_noop!(250			CollatorSelection::onboard(RuntimeOrigin::signed(3)),251			Error::<Test>::AlreadyCandidate,252		);253	})254}255256#[test]257fn becoming_candidate_works() {258	new_test_ext().execute_with(|| {259		// given260		assert_eq!(CollatorSelection::desired_collators(), 5);261		assert_eq!(CollatorSelection::license_bond(), 10);262		assert_eq!(CollatorSelection::candidates(), Vec::new());263		assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);264265		// take two endowed, non-invulnerables accounts.266		assert_eq!(Balances::free_balance(&3), 100);267		assert_eq!(Balances::free_balance(&4), 100);268269		get_license_and_onboard(3);270		get_license_and_onboard(4);271272		assert_eq!(Balances::free_balance(&3), 90);273		assert_eq!(Balances::free_balance(&4), 90);274275		assert_eq!(CollatorSelection::candidates().len(), 2);276	});277}278279#[test]280fn cannot_become_candidate_if_invulnerable() {281	new_test_ext().execute_with(|| {282		assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);283284		// can obtain a license even if is invulnerable.285		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(1)));286		// but cannot onboard287		assert_noop!(288			CollatorSelection::onboard(RuntimeOrigin::signed(1)),289			Error::<Test>::AlreadyInvulnerable,290		);291292		// get a license and then become invulnerable.293		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));294		assert_ok!(CollatorSelection::add_invulnerable(295			RuntimeOrigin::signed(RootAccount::get()),296			3297		));298		assert_noop!(299			CollatorSelection::onboard(RuntimeOrigin::signed(3)),300			Error::<Test>::AlreadyInvulnerable,301		);302	})303}304305#[test]306fn can_become_invulnerable_if_candidate() {307	new_test_ext().execute_with(|| {308		// become a candidate and then become invulnerable.309		get_license_and_onboard(3);310		assert_eq!(CollatorSelection::candidates(), vec![3]);311312		assert_ok!(CollatorSelection::add_invulnerable(313			RuntimeOrigin::signed(RootAccount::get()),314			3315		));316		// should exclude from candidates, but not revoke the license317		assert_eq!(CollatorSelection::candidates(), vec![]);318		assert_eq!(CollatorSelection::licenses(3), 10);319		assert_eq!(Balances::free_balance(3), 90);320	});321}322323#[test]324fn offboard() {325	new_test_ext().execute_with(|| {326		// register a candidate.327		get_license_and_onboard(3);328		assert_eq!(Balances::free_balance(3), 90);329330		// cannot leave if holds license but not yet candidate.331		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(4)));332		assert_noop!(333			CollatorSelection::offboard(RuntimeOrigin::signed(4)),334			Error::<Test>::NotCandidate335		);336		// cannot leave if does not hold license.337		assert_noop!(338			CollatorSelection::offboard(RuntimeOrigin::signed(5)),339			Error::<Test>::NotCandidate340		);341342		// bond is returned - only after releasing the license343		assert_ok!(CollatorSelection::offboard(RuntimeOrigin::signed(3)));344		assert_eq!(Balances::free_balance(3), 90);345		assert_eq!(CollatorSelection::last_authored_block(3), 0);346		assert_ok!(CollatorSelection::release_license(RuntimeOrigin::signed(3)));347		assert_eq!(Balances::free_balance(3), 100);348	});349}350351#[test]352fn release_license() {353	new_test_ext().execute_with(|| {354		// obtain a license to collate and reserve the bond.355		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));356		assert_eq!(Balances::free_balance(3), 90);357358		// release the license and get the bond back.359		assert_ok!(CollatorSelection::release_license(RuntimeOrigin::signed(3)));360		assert_eq!(Balances::free_balance(3), 100);361362		// register a candidate.363		get_license_and_onboard(3);364		assert_eq!(Balances::free_balance(3), 90);365366		// can release license even if onboarded.367		assert_ok!(CollatorSelection::release_license(RuntimeOrigin::signed(3)));368		assert_eq!(Balances::free_balance(3), 100);369		assert_eq!(CollatorSelection::candidates(), vec![]);370	});371}372373#[test]374fn force_revoke_license() {375	new_test_ext().execute_with(|| {376		// obtain a license to collate and reserve the bond.377		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));378		assert_eq!(Balances::free_balance(3), 90);379380		// cannot execute the operation as non-root381		assert_noop!(382			CollatorSelection::force_revoke_license(RuntimeOrigin::signed(3), 3),383			BadOrigin384		);385386		// release the license and get the bond back.387		assert_ok!(CollatorSelection::force_revoke_license(388			RuntimeOrigin::signed(RootAccount::get()),389			3390		));391		assert_eq!(Balances::free_balance(3), 100);392393		// register a candidate.394		get_license_and_onboard(3);395		assert_eq!(Balances::free_balance(3), 90);396397		// can release license even if onboarded.398		assert_ok!(CollatorSelection::force_revoke_license(399			RuntimeOrigin::signed(RootAccount::get()),400			3401		));402		assert_eq!(Balances::free_balance(3), 100);403		assert_eq!(CollatorSelection::candidates(), vec![]);404	});405}406407#[test]408fn authorship_event_handler() {409	new_test_ext().execute_with(|| {410		// put 100 in the pot + 5 for ED411		Balances::make_free_balance_be(&CollatorSelection::account_id(), 105);412413		// 4 is the default author.414		assert_eq!(Balances::free_balance(4), 100);415		get_license_and_onboard(4);416		// triggers `note_author`417		Authorship::on_initialize(1);418419		assert_eq!(CollatorSelection::candidates(), vec![4]);420		assert_eq!(CollatorSelection::last_authored_block(4), 0);421422		// half of the pot goes to the collator who's the author (4 in tests).423		assert_eq!(Balances::free_balance(4), 140);424		// half + ED stays.425		assert_eq!(Balances::free_balance(CollatorSelection::account_id()), 55);426	});427}428429#[test]430fn fees_edgecases() {431	new_test_ext().execute_with(|| {432		// Nothing panics, no reward when no ED in balance433		Authorship::on_initialize(1);434		// put some money into the pot at ED435		Balances::make_free_balance_be(&CollatorSelection::account_id(), 5);436		// 4 is the default author.437		assert_eq!(Balances::free_balance(4), 100);438		get_license_and_onboard(4);439		// triggers `note_author`440		Authorship::on_initialize(1);441442		assert_eq!(CollatorSelection::candidates(), vec![4]);443		assert_eq!(CollatorSelection::last_authored_block(4), 0);444		// Nothing received445		assert_eq!(Balances::free_balance(4), 90);446		// all fee stays447		assert_eq!(Balances::free_balance(CollatorSelection::account_id()), 5);448	});449}450451#[test]452fn session_management_works() {453	new_test_ext().execute_with(|| {454		initialize_to_block(1);455456		assert_eq!(SessionChangeBlock::get(), 0);457		assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);458459		initialize_to_block(4);460461		assert_eq!(SessionChangeBlock::get(), 0);462		assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);463464		// add a new collator465		get_license_and_onboard(5);466467		// session won't see this.468		assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);469		// but we have a new candidate.470		assert_eq!(CollatorSelection::candidates().len(), 1);471472		initialize_to_block(10);473		assert_eq!(SessionChangeBlock::get(), 10);474		// pallet-session has 1 session delay; current validators are the same.475		assert_eq!(Session::validators(), vec![1, 2]);476		// queued ones are changed, and now we have 3.477		assert_eq!(Session::queued_keys().len(), 3);478		// session handlers (aura, et. al.) cannot see this yet.479		assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);480481		initialize_to_block(20);482		assert_eq!(SessionChangeBlock::get(), 20);483		// changed are now reflected to session handlers.484		assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 5]);485	});486}487488#[test]489fn kick_mechanism() {490	new_test_ext().execute_with(|| {491		// add a new collator492		get_license_and_onboard(3);493		get_license_and_onboard(4);494495		initialize_to_block(10);496		assert_eq!(CollatorSelection::candidates().len(), 2);497498		initialize_to_block(20);499		assert_eq!(SessionChangeBlock::get(), 20);500		// 4 authored this block, gets to stay 3 was kicked501		assert_eq!(CollatorSelection::candidates().len(), 1);502		// 3 will be kicked after 1 session delay503		assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 3, 4]);504505		assert_eq!(CollatorSelection::candidates(), vec![4]);506		assert_eq!(CollatorSelection::kick_threshold(), 10);507		assert_eq!(CollatorSelection::last_authored_block(4), 20);508509		initialize_to_block(30);510		// 3 gets kicked after 1 session delay511		assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 4]);512		// kicked collator gets their funds slashed, the deposit going to treasury513		assert_eq!(Balances::free_balance(3), 90);514	});515}516517#[test]518#[should_panic = "duplicate invulnerables in genesis."]519fn cannot_set_genesis_value_twice() {520	sp_tracing::try_init_simple();521	let mut t = frame_system::GenesisConfig::default()522		.build_storage::<Test>()523		.unwrap();524	let invulnerables = vec![1, 1];525526	let collator_selection = collator_selection::GenesisConfig::<Test> {527		desired_collators: 5,528		license_bond: 10,529		kick_threshold: 10,530		invulnerables,531	};532	// collator selection must be initialized before session.533	collator_selection.assimilate_storage(&mut t).unwrap();534}