git.delta.rocks / unique-network / refs/commits / 4dd148686c6b

difftreelog

source

pallets/inflation/src/tests.rs11.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#![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 UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;38type Block = frame_system::mocking::MockBlock<Test>;3940const YEAR: u64 = 5_259_600; // 6-second blocks41							 // const YEAR: u64 = 2_629_800; // 12-second blocks42							 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION43const 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 MaxHolds = ();62	type MaxFreezes = ();63}6465frame_support::construct_runtime!(66	pub enum Test {67		Balances: pallet_balances,68		System: frame_system,69		Inflation: pallet_inflation,70	}71);7273parameter_types! {74	pub const BlockHashCount: u64 = 250;75	pub BlockWeights: frame_system::limits::BlockWeights =76		frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, 0));77	pub const SS58Prefix: u8 = 42;78}7980impl frame_system::Config for Test {81	type BaseCallFilter = Everything;82	type BlockWeights = ();83	type BlockLength = ();84	type DbWeight = ();85	type RuntimeOrigin = RuntimeOrigin;86	type RuntimeCall = RuntimeCall;87	type Nonce = u64;88	type Hash = H256;89	type Hashing = BlakeTwo256;90	type AccountId = u64;91	type Lookup = IdentityLookup<Self::AccountId>;92	type RuntimeEvent = ();93	type BlockHashCount = BlockHashCount;94	type Version = ();95	type PalletInfo = PalletInfo;96	type AccountData = pallet_balances::AccountData<u64>;97	type OnNewAccount = ();98	type OnKilledAccount = ();99	type SystemWeightInfo = ();100	type SS58Prefix = SS58Prefix;101	type OnSetCode = ();102	type MaxConsumers = ConstU32<16>;103}104105parameter_types! {106	pub TreasuryAccountId: u64 = 1234;107	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied108	pub static MockBlockNumberProvider: u32 = 0;109}110111impl BlockNumberProvider for MockBlockNumberProvider {112	type BlockNumber = u32;113114	fn current_block_number() -> Self::BlockNumber {115		Self::get()116	}117}118119impl pallet_inflation::Config for Test {120	type Currency = Balances;121	type TreasuryAccountId = TreasuryAccountId;122	type InflationBlockInterval = InflationBlockInterval;123	type BlockNumberProvider = MockBlockNumberProvider;124}125126pub fn new_test_ext() -> sp_io::TestExternalities {127	<frame_system::GenesisConfig<Test>>::default()128		.build_storage()129		.unwrap()130		.into()131}132133macro_rules! block_inflation {134	// Block inflation doesn't have any argumets135	() => {136		// Return BlockInflation state variable current value137		<pallet_inflation::BlockInflation<Test>>::get()138	};139}140141#[test]142fn uninitialized_inflation() {143	new_test_ext().execute_with(|| {144		let initial_issuance: u64 = 1_000_000_000;145		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);146		assert_eq!(Balances::free_balance(1234), initial_issuance);147148		// BlockInflation should be set after inflation is started149		// first inflation deposit should be equal to BlockInflation150		MockBlockNumberProvider::set(1);151152		assert_eq!(block_inflation!(), 0);153	});154}155156#[test]157fn inflation_works() {158	new_test_ext().execute_with(|| {159		// Total issuance = 1_000_000_000160		let initial_issuance: u64 = 1_000_000_000;161		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);162		assert_eq!(Balances::free_balance(1234), initial_issuance);163164		// BlockInflation should be set after inflation is started165		// first inflation deposit should be equal to BlockInflation166		MockBlockNumberProvider::set(1);167168		// Start inflation as sudo169		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));170		assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);171		assert_eq!(172			Balances::free_balance(1234) - initial_issuance,173			block_inflation!()174		);175176		// Trigger inflation177		MockBlockNumberProvider::set(102);178		Inflation::on_initialize(0);179		assert_eq!(180			Balances::free_balance(1234) - initial_issuance,181			2 * block_inflation!()182		);183	});184}185186#[test]187fn inflation_second_deposit() {188	new_test_ext().execute_with(|| {189		// Total issuance = 1_000_000_000190		let initial_issuance: u64 = 1_000_000_000;191		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);192		assert_eq!(Balances::free_balance(1234), initial_issuance);193		MockBlockNumberProvider::set(1);194195		// Start inflation as sudo196		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));197198		// Next inflation deposit happens when block is greater then or equal to NextInflationBlock199		let mut block: u64 = 2;200		let balance_before: u64 = Balances::free_balance(1234);201		while block < <pallet_inflation::NextInflationBlock<Test>>::get() {202			MockBlockNumberProvider::set(block as u64);203			Inflation::on_initialize(0);204			block += 1;205		}206		let balance_just_before: u64 = Balances::free_balance(1234);207		assert_eq!(balance_before, balance_just_before);208209		// The block with inflation210		MockBlockNumberProvider::set(block as u64);211		Inflation::on_initialize(0);212		let balance_after: u64 = Balances::free_balance(1234);213		assert_eq!(balance_after - balance_just_before, block_inflation!());214	});215}216217#[test]218fn inflation_in_1_year() {219	new_test_ext().execute_with(|| {220		// Total issuance = 1_000_000_000221		let initial_issuance: u64 = 1_000_000_000;222		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);223		assert_eq!(Balances::free_balance(1234), initial_issuance);224		MockBlockNumberProvider::set(1);225226		// Start inflation as sudo227		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));228229		// Go through all the block inflations for year 1,230		// total issuance will be updated accordingly231		// Inflation is set to start in block 1, so first iteration is block 101232		for block in (101..YEAR).step_by(100) {233			MockBlockNumberProvider::set(block);234			Inflation::on_initialize(0);235		}236		assert_eq!(237			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),238			<Balances as Inspect<_>>::total_issuance()239		);240241		MockBlockNumberProvider::set(YEAR + 1);242		Inflation::on_initialize(0);243		let block_inflation_year_2 = block_inflation!();244		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951245		let expecter_year_2_inflation: u64 = (initial_issuance246			+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)247			* 933 * 100 / (10000 * YEAR);248		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality249	});250}251252#[test]253fn inflation_start_large_kusama_block() {254	new_test_ext().execute_with(|| {255		// Total issuance = 1_000_000_000256		let initial_issuance: u64 = 1_000_000_000;257		let start_block: u64 = 10457457;258		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);259		assert_eq!(Balances::free_balance(1234), initial_issuance);260		MockBlockNumberProvider::set(start_block);261262		// Start inflation as sudo263		assert_ok!(Inflation::start_inflation(264			RawOrigin::Root.into(),265			start_block266		));267268		// Go through all the block inflations for year 1,269		// total issuance will be updated accordingly270		// Inflation is set to start in block 1, so first iteration is block 101271		for block in (101..YEAR).step_by(100) {272			MockBlockNumberProvider::set(start_block + block);273			Inflation::on_initialize(0);274		}275		assert_eq!(276			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),277			<Balances as Inspect<_>>::total_issuance()278		);279280		MockBlockNumberProvider::set(start_block + YEAR + 1);281		Inflation::on_initialize(0);282		let block_inflation_year_2 = block_inflation!();283		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951284		let expecter_year_2_inflation: u64 = (initial_issuance285			+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)286			* 933 * 100 / (10000 * YEAR);287		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality288	});289}290291#[test]292fn inflation_after_year_10_is_flat() {293	new_test_ext().execute_with(|| {294		// Total issuance = 1_000_000_000295		let initial_issuance: u64 = 1_000_000_000;296		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);297		assert_eq!(Balances::free_balance(1234), initial_issuance);298		MockBlockNumberProvider::set(YEAR * 9 + 1);299300		// Start inflation as sudo301		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));302303		// Let inflation catch up304		for _year in 1..=9 {305			Inflation::on_initialize(0);306		}307308		for year in 10..=20 {309			let block_inflation_year_before = block_inflation!();310			MockBlockNumberProvider::set(YEAR * year + 1);311			Inflation::on_initialize(0);312			let block_inflation_year_after = block_inflation!();313314			// Assert that next year inflation is equal to previous year inflation315			assert_eq!(block_inflation_year_before, block_inflation_year_after);316		}317	});318}319320#[test]321fn inflation_rate_by_year() {322	new_test_ext().execute_with(|| {323		let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;324325		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),326		// then it is flat.327		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];328329		// For accuracy total issuance = payout0 * payouts * 10;330		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;331		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);332		assert_eq!(Balances::free_balance(1234), initial_issuance);333334		// Start inflation as sudo335		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));336337		for year in 0..=10 {338			// Year first block339			MockBlockNumberProvider::set(YEAR * year + 1);340			Inflation::on_initialize(0);341			let mut actual_payout = block_inflation!();342			assert_eq!(actual_payout, payout_by_year[year as usize]);343344			// Year second block345			MockBlockNumberProvider::set(YEAR * year + 2);346			Inflation::on_initialize(0);347			actual_payout = block_inflation!();348			assert_eq!(actual_payout, payout_by_year[year as usize]);349350			// Year middle block351			MockBlockNumberProvider::set(year * YEAR + YEAR / 2);352			Inflation::on_initialize(0);353			actual_payout = block_inflation!();354			assert_eq!(actual_payout, payout_by_year[year as usize]);355356			// Year last block357			MockBlockNumberProvider::set((year + 1) * YEAR);358			Inflation::on_initialize(0);359			actual_payout = block_inflation!();360			assert_eq!(actual_payout, payout_by_year[year as usize]);361		}362	});363}