git.delta.rocks / unique-network / refs/heads / master

difftreelog

source

pallets/inflation/src/tests.rs11.7 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#![cfg(test)]18#![allow(clippy::from_over_into)]19use frame_support::{20	assert_ok, parameter_types,21	traits::{22		fungible::{Balanced, Inspect},23		tokens::Precision,24		ConstU32, Everything, OnInitialize,25	},26	weights::Weight,27};28use frame_system::RawOrigin;29use sp_core::H256;30use sp_runtime::{31	traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},32	BuildStorage,33};3435use crate as pallet_inflation;3637type Block = frame_system::mocking::MockBlockU32<Test>;3839// 6-second blocks40// const YEAR: u32 = 2_629_800; // 12-second blocks41// Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION42const YEAR: u32 = 5_259_600;43const FIRST_YEAR_BLOCK_INFLATION: u64 = 1901;4445parameter_types! {46	pub const ExistentialDeposit: u64 = 1;47	pub const MaxLocks: u32 = 50;48}4950impl pallet_balances::Config for Test {51	type AccountStore = System;52	type Balance = u64;53	type DustRemoval = ();54	type RuntimeEvent = ();55	type ExistentialDeposit = ExistentialDeposit;56	type WeightInfo = ();57	type MaxLocks = MaxLocks;58	type MaxReserves = ();59	type ReserveIdentifier = ();60	type FreezeIdentifier = ();61	type MaxFreezes = ();62	type RuntimeHoldReason = RuntimeHoldReason;63	type RuntimeFreezeReason = RuntimeFreezeReason;64}6566frame_support::construct_runtime!(67	pub enum Test {68		Balances: pallet_balances,69		System: frame_system,70		Inflation: pallet_inflation,71	}72);7374parameter_types! {75	pub const BlockHashCount: u32 = 250;76	pub BlockWeights: frame_system::limits::BlockWeights =77		frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, 0));78	pub const SS58Prefix: u8 = 42;79}8081impl frame_system::Config for Test {82	type BaseCallFilter = Everything;83	type Block = Block;84	type BlockWeights = ();85	type BlockLength = ();86	type DbWeight = ();87	type RuntimeOrigin = RuntimeOrigin;88	type RuntimeCall = RuntimeCall;89	type Nonce = u64;90	type Hash = H256;91	type Hashing = BlakeTwo256;92	type AccountId = u64;93	type Lookup = IdentityLookup<Self::AccountId>;94	type RuntimeEvent = ();95	type BlockHashCount = BlockHashCount;96	type Version = ();97	type PalletInfo = PalletInfo;98	type AccountData = pallet_balances::AccountData<u64>;99	type OnNewAccount = ();100	type OnKilledAccount = ();101	type SystemWeightInfo = ();102	type SS58Prefix = SS58Prefix;103	type OnSetCode = ();104	type MaxConsumers = ConstU32<16>;105	type RuntimeTask = ();106	type PreInherents = ();107	type PostInherents = ();108	type PostTransactions = ();109	type SingleBlockMigrations = ();110	type MultiBlockMigrator = ();111}112113parameter_types! {114	pub TreasuryAccountId: u64 = 1234;115	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied116	pub static MockBlockNumberProvider: u32 = 0;117}118119impl BlockNumberProvider for MockBlockNumberProvider {120	type BlockNumber = u32;121122	fn current_block_number() -> Self::BlockNumber {123		Self::get()124	}125}126127impl pallet_inflation::Config for Test {128	type Currency = Balances;129	type TreasuryAccountId = TreasuryAccountId;130	type InflationBlockInterval = InflationBlockInterval;131	type OnInitializeBlockNumberProvider = MockBlockNumberProvider;132}133134pub fn new_test_ext() -> sp_io::TestExternalities {135	<frame_system::GenesisConfig<Test>>::default()136		.build_storage()137		.unwrap()138		.into()139}140141macro_rules! block_inflation {142	// Block inflation doesn't have any argumets143	() => {144		// Return BlockInflation state variable current value145		<pallet_inflation::BlockInflation<Test>>::get()146	};147}148149#[test]150fn uninitialized_inflation() {151	new_test_ext().execute_with(|| {152		let initial_issuance: u64 = 1_000_000_000;153		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);154		assert_eq!(Balances::free_balance(1234), initial_issuance);155156		// BlockInflation should be set after inflation is started157		// first inflation deposit should be equal to BlockInflation158		MockBlockNumberProvider::set(1);159160		assert_eq!(block_inflation!(), 0);161	});162}163164#[test]165fn inflation_works() {166	new_test_ext().execute_with(|| {167		// Total issuance = 1_000_000_000168		let initial_issuance: u64 = 1_000_000_000;169		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);170		assert_eq!(Balances::free_balance(1234), initial_issuance);171172		// BlockInflation should be set after inflation is started173		// first inflation deposit should be equal to BlockInflation174		MockBlockNumberProvider::set(1);175176		// Start inflation as sudo177		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));178		assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);179		assert_eq!(180			Balances::free_balance(1234) - initial_issuance,181			block_inflation!()182		);183184		// Trigger inflation185		MockBlockNumberProvider::set(102);186		Inflation::on_initialize(0);187		assert_eq!(188			Balances::free_balance(1234) - initial_issuance,189			2 * block_inflation!()190		);191	});192}193194#[test]195fn inflation_second_deposit() {196	new_test_ext().execute_with(|| {197		// Total issuance = 1_000_000_000198		let initial_issuance = 1_000_000_000;199		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);200		assert_eq!(Balances::free_balance(1234), initial_issuance);201		MockBlockNumberProvider::set(1);202203		// Start inflation as sudo204		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));205206		// Next inflation deposit happens when block is greater then or equal to NextInflationBlock207		let mut block = 2;208		let balance_before = Balances::free_balance(1234);209		while block < <pallet_inflation::NextInflationBlock<Test>>::get() {210			MockBlockNumberProvider::set(block);211			Inflation::on_initialize(0);212			block += 1;213		}214		let balance_just_before = Balances::free_balance(1234);215		assert_eq!(balance_before, balance_just_before);216217		// The block with inflation218		MockBlockNumberProvider::set(block);219		Inflation::on_initialize(0);220		let balance_after = Balances::free_balance(1234);221		assert_eq!(balance_after - balance_just_before, block_inflation!());222	});223}224225#[test]226fn inflation_in_1_year() {227	new_test_ext().execute_with(|| {228		// Total issuance = 1_000_000_000229		let initial_issuance: u64 = 1_000_000_000;230		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);231		assert_eq!(Balances::free_balance(1234), initial_issuance);232		MockBlockNumberProvider::set(1);233234		// Start inflation as sudo235		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));236237		// Go through all the block inflations for year 1,238		// total issuance will be updated accordingly239		// Inflation is set to start in block 1, so first iteration is block 101240		for block in (101..YEAR).step_by(100) {241			MockBlockNumberProvider::set(block);242			Inflation::on_initialize(0);243		}244		assert_eq!(245			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * ((YEAR as u64) / 100)),246			<Balances as Inspect<_>>::total_issuance()247		);248249		MockBlockNumberProvider::set(YEAR + 1);250		Inflation::on_initialize(0);251		let block_inflation_year_2 = block_inflation!();252		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951253		let expecter_year_2_inflation: u64 = (initial_issuance254			+ FIRST_YEAR_BLOCK_INFLATION * (YEAR as u64) / 100)255			* 933 * 100 / (10000 * (YEAR as u64));256		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality257	});258}259260#[test]261fn inflation_start_large_kusama_block() {262	new_test_ext().execute_with(|| {263		// Total issuance = 1_000_000_000264		let initial_issuance = 1_000_000_000;265		let start_block = 10457457;266		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);267		assert_eq!(Balances::free_balance(1234), initial_issuance);268		MockBlockNumberProvider::set(start_block);269270		// Start inflation as sudo271		assert_ok!(Inflation::start_inflation(272			RawOrigin::Root.into(),273			start_block274		));275276		// Go through all the block inflations for year 1,277		// total issuance will be updated accordingly278		// Inflation is set to start in block 1, so first iteration is block 101279		for block in (101..YEAR).step_by(100) {280			MockBlockNumberProvider::set(start_block + block);281			Inflation::on_initialize(0);282		}283		assert_eq!(284			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * ((YEAR as u64) / 100)),285			<Balances as Inspect<_>>::total_issuance()286		);287288		MockBlockNumberProvider::set(start_block + YEAR + 1);289		Inflation::on_initialize(0);290		let block_inflation_year_2 = block_inflation!();291		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951292		let expecter_year_2_inflation: u64 = (initial_issuance293			+ FIRST_YEAR_BLOCK_INFLATION * (YEAR as u64) / 100)294			* 933 * 100 / (10000 * (YEAR as u64));295		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality296	});297}298299#[test]300fn inflation_after_year_10_is_flat() {301	new_test_ext().execute_with(|| {302		// Total issuance = 1_000_000_000303		let initial_issuance: u64 = 1_000_000_000;304		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);305		assert_eq!(Balances::free_balance(1234), initial_issuance);306		MockBlockNumberProvider::set(YEAR * 9 + 1);307308		// Start inflation as sudo309		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));310311		// Let inflation catch up312		for _year in 1..=9 {313			Inflation::on_initialize(0);314		}315316		for year in 10..=20 {317			let block_inflation_year_before = block_inflation!();318			MockBlockNumberProvider::set(YEAR * year + 1);319			Inflation::on_initialize(0);320			let block_inflation_year_after = block_inflation!();321322			// Assert that next year inflation is equal to previous year inflation323			assert_eq!(block_inflation_year_before, block_inflation_year_after);324		}325	});326}327328#[test]329fn inflation_rate_by_year() {330	new_test_ext().execute_with(|| {331		let payouts = (YEAR / InflationBlockInterval::get()) as u64;332333		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),334		// then it is flat.335		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];336337		// For accuracy total issuance = payout0 * payouts * 10;338		let initial_issuance = payout_by_year[0] * payouts * 10;339		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);340		assert_eq!(Balances::free_balance(1234), initial_issuance);341342		// Start inflation as sudo343		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));344345		for year in 0..=10 {346			// Year first block347			MockBlockNumberProvider::set(YEAR * year + 1);348			Inflation::on_initialize(0);349			let mut actual_payout = block_inflation!();350			assert_eq!(actual_payout, payout_by_year[year as usize]);351352			// Year second block353			MockBlockNumberProvider::set(YEAR * year + 2);354			Inflation::on_initialize(0);355			actual_payout = block_inflation!();356			assert_eq!(actual_payout, payout_by_year[year as usize]);357358			// Year middle block359			MockBlockNumberProvider::set(year * YEAR + YEAR / 2);360			Inflation::on_initialize(0);361			actual_payout = block_inflation!();362			assert_eq!(actual_payout, payout_by_year[year as usize]);363364			// Year last block365			MockBlockNumberProvider::set((year + 1) * YEAR);366			Inflation::on_initialize(0);367			actual_payout = block_inflation!();368			assert_eq!(actual_payout, payout_by_year[year as usize]);369		}370	});371}