git.delta.rocks / unique-network / refs/commits / a993a5bf6636

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 crate as pallet_inflation;2021use frame_support::{22	assert_ok, parameter_types,23	traits::{Currency, OnInitialize, Everything, ConstU32},24};25use frame_system::RawOrigin;26use sp_core::H256;27use sp_runtime::{28	traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},29	testing::Header,30};3132type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;33type Block = frame_system::mocking::MockBlock<Test>;3435const YEAR: u64 = 5_259_600; // 6-second blocks36							 // const YEAR: u64 = 2_629_800; // 12-second blocks37							 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION38const FIRST_YEAR_BLOCK_INFLATION: u64 = 1901;3940parameter_types! {41	pub const ExistentialDeposit: u64 = 1;42	pub const MaxLocks: u32 = 50;43}4445impl pallet_balances::Config for Test {46	type AccountStore = System;47	type Balance = u64;48	type DustRemoval = ();49	type Event = ();50	type ExistentialDeposit = ExistentialDeposit;51	type WeightInfo = ();52	type MaxLocks = MaxLocks;53	type MaxReserves = ();54	type ReserveIdentifier = ();55}5657frame_support::construct_runtime!(58	pub enum Test where59		Block = Block,60		NodeBlock = Block,61		UncheckedExtrinsic = UncheckedExtrinsic,62	{63		Balances: pallet_balances::{Pallet, Call, Storage},64		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},65		Inflation: pallet_inflation::{Pallet, Call, Storage},66	}67);6869parameter_types! {70	pub const BlockHashCount: u64 = 250;71	pub BlockWeights: frame_system::limits::BlockWeights =72		frame_system::limits::BlockWeights::simple_max(1024);73	pub const SS58Prefix: u8 = 42;74}7576impl frame_system::Config for Test {77	type BaseCallFilter = Everything;78	type BlockWeights = ();79	type BlockLength = ();80	type DbWeight = ();81	type Origin = Origin;82	type Call = Call;83	type Index = u64;84	type BlockNumber = u64;85	type Hash = H256;86	type Hashing = BlakeTwo256;87	type AccountId = u64;88	type Lookup = IdentityLookup<Self::AccountId>;89	type Header = Header;90	type Event = ();91	type BlockHashCount = BlockHashCount;92	type Version = ();93	type PalletInfo = PalletInfo;94	type AccountData = pallet_balances::AccountData<u64>;95	type OnNewAccount = ();96	type OnKilledAccount = ();97	type SystemWeightInfo = ();98	type SS58Prefix = SS58Prefix;99	type OnSetCode = ();100	type MaxConsumers = ConstU32<16>;101}102103parameter_types! {104	pub TreasuryAccountId: u64 = 1234;105	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied106	pub static MockBlockNumberProvider: u64 = 0;107}108109impl BlockNumberProvider for MockBlockNumberProvider {110	type BlockNumber = u64;111112	fn current_block_number() -> Self::BlockNumber {113		Self::get()114	}115}116117impl pallet_inflation::Config for Test {118	type Currency = Balances;119	type TreasuryAccountId = TreasuryAccountId;120	type InflationBlockInterval = InflationBlockInterval;121	type BlockNumberProvider = MockBlockNumberProvider;122}123124pub fn new_test_ext() -> sp_io::TestExternalities {125	frame_system::GenesisConfig::default()126		.build_storage::<Test>()127		.unwrap()128		.into()129}130131macro_rules! block_inflation {132	// Block inflation doesn't have any argumets133	() => {134		// Return BlockInflation state variable current value135		<pallet_inflation::BlockInflation<Test>>::get()136	};137}138139#[test]140fn uninitialized_inflation() {141	new_test_ext().execute_with(|| {142		let initial_issuance: u64 = 1_000_000_000;143		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);144		assert_eq!(Balances::free_balance(1234), initial_issuance);145146		// BlockInflation should be set after inflation is started147		// first inflation deposit should be equal to BlockInflation148		MockBlockNumberProvider::set(1);149150		assert_eq!(block_inflation!(), 0);151	});152}153154#[test]155fn inflation_works() {156	new_test_ext().execute_with(|| {157		// Total issuance = 1_000_000_000158		let initial_issuance: u64 = 1_000_000_000;159		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);160		assert_eq!(Balances::free_balance(1234), initial_issuance);161162		// BlockInflation should be set after inflation is started163		// first inflation deposit should be equal to BlockInflation164		MockBlockNumberProvider::set(1);165166		// Start inflation as sudo167		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));168		assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);169		assert_eq!(170			Balances::free_balance(1234) - initial_issuance,171			block_inflation!()172		);173174		// Trigger inflation175		MockBlockNumberProvider::set(102);176		Inflation::on_initialize(0);177		assert_eq!(178			Balances::free_balance(1234) - initial_issuance,179			2 * block_inflation!()180		);181	});182}183184#[test]185fn inflation_second_deposit() {186	new_test_ext().execute_with(|| {187		// Total issuance = 1_000_000_000188		let initial_issuance: u64 = 1_000_000_000;189		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);190		assert_eq!(Balances::free_balance(1234), initial_issuance);191		MockBlockNumberProvider::set(1);192193		// Start inflation as sudo194		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));195196		// Next inflation deposit happens when block is greater then or equal to NextInflationBlock197		let mut block: u64 = 2;198		let balance_before: u64 = Balances::free_balance(1234);199		while block < <pallet_inflation::NextInflationBlock<Test>>::get() {200			MockBlockNumberProvider::set(block as u64);201			Inflation::on_initialize(0);202			block += 1;203		}204		let balance_just_before: u64 = Balances::free_balance(1234);205		assert_eq!(balance_before, balance_just_before);206207		// The block with inflation208		MockBlockNumberProvider::set(block as u64);209		Inflation::on_initialize(0);210		let balance_after: u64 = Balances::free_balance(1234);211		assert_eq!(balance_after - balance_just_before, block_inflation!());212	});213}214215#[test]216fn inflation_in_1_year() {217	new_test_ext().execute_with(|| {218		// Total issuance = 1_000_000_000219		let initial_issuance: u64 = 1_000_000_000;220		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);221		assert_eq!(Balances::free_balance(1234), initial_issuance);222		MockBlockNumberProvider::set(1);223224		// Start inflation as sudo225		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));226227		// Go through all the block inflations for year 1,228		// total issuance will be updated accordingly229		// Inflation is set to start in block 1, so first iteration is block 101230		for block in (101..YEAR).step_by(100) {231			MockBlockNumberProvider::set(block);232			Inflation::on_initialize(0);233		}234		assert_eq!(235			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),236			<Balances as Currency<_>>::total_issuance()237		);238239		MockBlockNumberProvider::set(YEAR + 1);240		Inflation::on_initialize(0);241		let block_inflation_year_2 = block_inflation!();242		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951243		let expecter_year_2_inflation: u64 = (initial_issuance244			+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)245			* 933 * 100 / (10000 * YEAR);246		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality247	});248}249250#[test]251fn inflation_start_large_kusama_block() {252	new_test_ext().execute_with(|| {253		// Total issuance = 1_000_000_000254		let initial_issuance: u64 = 1_000_000_000;255		let start_block: u64 = 10457457;256		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);257		assert_eq!(Balances::free_balance(1234), initial_issuance);258		MockBlockNumberProvider::set(start_block);259260		// Start inflation as sudo261		assert_ok!(Inflation::start_inflation(262			RawOrigin::Root.into(),263			start_block264		));265266		// Go through all the block inflations for year 1,267		// total issuance will be updated accordingly268		// Inflation is set to start in block 1, so first iteration is block 101269		for block in (101..YEAR).step_by(100) {270			MockBlockNumberProvider::set(start_block + block);271			Inflation::on_initialize(0);272		}273		assert_eq!(274			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),275			<Balances as Currency<_>>::total_issuance()276		);277278		MockBlockNumberProvider::set(start_block + YEAR + 1);279		Inflation::on_initialize(0);280		let block_inflation_year_2 = block_inflation!();281		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951282		let expecter_year_2_inflation: u64 = (initial_issuance283			+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)284			* 933 * 100 / (10000 * YEAR);285		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality286	});287}288289#[test]290fn inflation_after_year_10_is_flat() {291	new_test_ext().execute_with(|| {292		// Total issuance = 1_000_000_000293		let initial_issuance: u64 = 1_000_000_000;294		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);295		assert_eq!(Balances::free_balance(1234), initial_issuance);296		MockBlockNumberProvider::set(YEAR * 9 + 1);297298		// Start inflation as sudo299		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));300301		// Let inflation catch up302		for _year in 1..=9 {303			Inflation::on_initialize(0);304		}305306		for year in 10..=20 {307			let block_inflation_year_before = block_inflation!();308			MockBlockNumberProvider::set(YEAR * year + 1);309			Inflation::on_initialize(0);310			let block_inflation_year_after = block_inflation!();311312			// Assert that next year inflation is equal to previous year inflation313			assert_eq!(block_inflation_year_before, block_inflation_year_after);314		}315	});316}317318#[test]319fn inflation_rate_by_year() {320	new_test_ext().execute_with(|| {321		let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;322323		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),324		// then it is flat.325		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];326327		// For accuracy total issuance = payout0 * payouts * 10;328		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;329		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);330		assert_eq!(Balances::free_balance(1234), initial_issuance);331332		// Start inflation as sudo333		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));334335		for year in 0..=10 {336			// Year first block337			MockBlockNumberProvider::set(YEAR * year + 1);338			Inflation::on_initialize(0);339			let mut actual_payout = block_inflation!();340			assert_eq!(actual_payout, payout_by_year[year as usize]);341342			// Year second block343			MockBlockNumberProvider::set(YEAR * year + 2);344			Inflation::on_initialize(0);345			actual_payout = block_inflation!();346			assert_eq!(actual_payout, payout_by_year[year as usize]);347348			// Year middle block349			MockBlockNumberProvider::set(year * YEAR + YEAR / 2);350			Inflation::on_initialize(0);351			actual_payout = block_inflation!();352			assert_eq!(actual_payout, payout_by_year[year as usize]);353354			// Year last block355			MockBlockNumberProvider::set((year + 1) * YEAR);356			Inflation::on_initialize(0);357			actual_payout = block_inflation!();358			assert_eq!(actual_payout, payout_by_year[year as usize]);359		}360	});361}