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

difftreelog

source

pallets/inflation/src/tests.rs7.3 KiBsourcehistory
1#![cfg(test)]2#![allow(clippy::from_over_into)]3use crate as pallet_inflation;45use frame_support::{6	traits::{Currency},7	parameter_types,8};9use frame_support::{traits::OnInitialize};10use sp_core::H256;11use sp_runtime::{12	traits::{BlakeTwo256, IdentityLookup},13	testing::Header,14};1516type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;17type Block = frame_system::mocking::MockBlock<Test>;1819const YEAR: u64 = 5_259_600;2021parameter_types! {22	pub const ExistentialDeposit: u64 = 1;23	pub const MaxLocks: u32 = 50;24}2526impl pallet_balances::Config for Test {27	type AccountStore = System;28	type Balance = u64;29	type DustRemoval = ();30	type Event = ();31	type ExistentialDeposit = ExistentialDeposit;32	type WeightInfo = ();33	type MaxLocks = MaxLocks;34	type MaxReserves = ();35	type ReserveIdentifier = ();36}3738frame_support::construct_runtime!(39	pub enum Test where40		Block = Block,41		NodeBlock = Block,42		UncheckedExtrinsic = UncheckedExtrinsic,43	{44		Balances: pallet_balances::{Pallet, Call, Storage},45		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},46		Inflation: pallet_inflation::{Pallet, Call, Storage},47	}48);4950parameter_types! {51	pub const BlockHashCount: u64 = 250;52	pub BlockWeights: frame_system::limits::BlockWeights =53		frame_system::limits::BlockWeights::simple_max(1024);54	pub const SS58Prefix: u8 = 42;55}5657impl frame_system::Config for Test {58	type BaseCallFilter = ();59	type BlockWeights = ();60	type BlockLength = ();61	type DbWeight = ();62	type Origin = Origin;63	type Call = Call;64	type Index = u64;65	type BlockNumber = u64;66	type Hash = H256;67	type Hashing = BlakeTwo256;68	type AccountId = u64;69	type Lookup = IdentityLookup<Self::AccountId>;70	type Header = Header;71	type Event = ();72	type BlockHashCount = BlockHashCount;73	type Version = ();74	type PalletInfo = PalletInfo;75	type AccountData = pallet_balances::AccountData<u64>;76	type OnNewAccount = ();77	type OnKilledAccount = ();78	type SystemWeightInfo = ();79	type SS58Prefix = SS58Prefix;80	type OnSetCode = ();81}8283parameter_types! {84	pub TreasuryAccountId: u64 = 1234;85	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied86}8788impl pallet_inflation::Config for Test {89	type Currency = Balances;90	type TreasuryAccountId = TreasuryAccountId;91	type InflationBlockInterval = InflationBlockInterval;92}9394// Build genesis storage according to the mock runtime.95pub fn new_test_ext() -> sp_io::TestExternalities {96	frame_system::GenesisConfig::default()97		.build_storage::<Test>()98		.unwrap()99		.into()100}101102#[test]103fn inflation_works() {104	new_test_ext().execute_with(|| {105		// Total issuance = 1_000_000_000106		let initial_issuance: u64 = 1_000_000_000;107		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);108		assert_eq!(Balances::free_balance(1234), initial_issuance);109110		// BlockInflation should be set after 1st block and111		// first inflation deposit should be equal to BlockInflation112		Inflation::on_initialize(1);113		assert!(Inflation::block_inflation() > 0);114		assert_eq!(115			Balances::free_balance(1234) - initial_issuance,116			Inflation::block_inflation()117		);118	});119}120121#[test]122fn inflation_second_deposit() {123	new_test_ext().execute_with(|| {124		// Total issuance = 1_000_000_000125		let initial_issuance: u64 = 1_000_000_000;126		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);127		assert_eq!(Balances::free_balance(1234), initial_issuance);128		Inflation::on_initialize(1);129130		// Next inflation deposit happens when block is multiple of InflationBlockInterval131		let mut block: u32 = 2;132		let balance_before: u64 = Balances::free_balance(1234);133		while block % InflationBlockInterval::get() != 0 {134			Inflation::on_initialize(block as u64);135			block += 1;136		}137		let balance_just_before: u64 = Balances::free_balance(1234);138		assert_eq!(balance_before, balance_just_before);139140		// The block with inflation141		Inflation::on_initialize(block as u64);142		let balance_after: u64 = Balances::free_balance(1234);143		assert_eq!(144			balance_after - balance_just_before,145			Inflation::block_inflation()146		);147	});148}149150#[test]151fn inflation_in_1_year() {152	new_test_ext().execute_with(|| {153		// Total issuance = 1_000_000_000154		let initial_issuance: u64 = 1_000_000_000;155		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);156		assert_eq!(Balances::free_balance(1234), initial_issuance);157		Inflation::on_initialize(1);158		let block_inflation_year_0 = Inflation::block_inflation();159160		Inflation::on_initialize(YEAR);161		let block_inflation_year_1 = Inflation::block_inflation();162163		// Assert that year 1 inflation is less than year 0164		assert!(block_inflation_year_0 > block_inflation_year_1);165	});166}167168#[test]169fn inflation_in_1_to_9_years() {170	new_test_ext().execute_with(|| {171		// Total issuance = 1_000_000_000172		let initial_issuance: u64 = 1_000_000_000;173		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);174		assert_eq!(Balances::free_balance(1234), initial_issuance);175		Inflation::on_initialize(1);176177		for year in 1..=9 {178			let block_inflation_year_before = Inflation::block_inflation();179			Inflation::on_initialize(YEAR * year);180			let block_inflation_year_after = Inflation::block_inflation();181182			// Assert that next year inflation is less than previous year inflation183			assert!(block_inflation_year_before > block_inflation_year_after);184		}185	});186}187188#[test]189fn inflation_after_year_10_is_flat() {190	new_test_ext().execute_with(|| {191		// Total issuance = 1_000_000_000192		let initial_issuance: u64 = 1_000_000_000;193		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);194		assert_eq!(Balances::free_balance(1234), initial_issuance);195		Inflation::on_initialize(YEAR * 9);196197		for year in 10..=20 {198			let block_inflation_year_before = Inflation::block_inflation();199			Inflation::on_initialize(YEAR * year);200			let block_inflation_year_after = Inflation::block_inflation();201202			// Assert that next year inflation is equal to previous year inflation203			assert_eq!(block_inflation_year_before, block_inflation_year_after);204		}205	});206}207208#[test]209fn inflation_rate_by_year() {210	new_test_ext().execute_with(|| {211		let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;212213		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),214		// then it is flat.215		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];216217		// For accuracy total issuance = payout0 * payouts * 10;218		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;219		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);220		assert_eq!(Balances::free_balance(1234), initial_issuance);221222		for year in 0..=10 {223			// Year first block224			Inflation::on_initialize(year * YEAR);225			let mut actual_payout = Inflation::block_inflation();226			assert_eq!(actual_payout, payout_by_year[year as usize]);227228			// Year second block229			Inflation::on_initialize(year * YEAR + 1);230			actual_payout = Inflation::block_inflation();231			assert_eq!(actual_payout, payout_by_year[year as usize]);232233			// Year middle block234			Inflation::on_initialize(year * YEAR + YEAR / 2);235			actual_payout = Inflation::block_inflation();236			assert_eq!(actual_payout, payout_by_year[year as usize]);237238			// Year last block239			Inflation::on_initialize((year + 1) * YEAR - 1);240			actual_payout = Inflation::block_inflation();241			assert_eq!(actual_payout, payout_by_year[year as usize]);242		}243	});244}