git.delta.rocks / unique-network / refs/commits / 40e660fc7f24

difftreelog

source

pallets/inflation/src/tests.rs7.2 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}3536frame_support::construct_runtime!(37	pub enum Test where38		Block = Block,39		NodeBlock = Block,40		UncheckedExtrinsic = UncheckedExtrinsic,41	{42		Balances: pallet_balances::{Pallet, Call, Storage},43		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},44		Inflation: pallet_inflation::{Pallet, Call, Storage},45	}46);4748parameter_types! {49	pub const BlockHashCount: u64 = 250;50	pub BlockWeights: frame_system::limits::BlockWeights =51		frame_system::limits::BlockWeights::simple_max(1024);52	pub const SS58Prefix: u8 = 42;53}5455impl frame_system::Config for Test {56	type BaseCallFilter = ();57	type BlockWeights = ();58	type BlockLength = ();59	type DbWeight = ();60	type Origin = Origin;61	type Call = Call;62	type Index = u64;63	type BlockNumber = u64;64	type Hash = H256;65	type Hashing = BlakeTwo256;66	type AccountId = u64;67	type Lookup = IdentityLookup<Self::AccountId>;68	type Header = Header;69	type Event = ();70	type BlockHashCount = BlockHashCount;71	type Version = ();72	type PalletInfo = PalletInfo;73	type AccountData = pallet_balances::AccountData<u64>;74	type OnNewAccount = ();75	type OnKilledAccount = ();76	type SystemWeightInfo = ();77	type SS58Prefix = SS58Prefix;78	type OnSetCode = ();79}8081parameter_types! {82	pub TreasuryAccountId: u64 = 1234;83	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied84}8586impl pallet_inflation::Config for Test {87	type Currency = Balances;88	type TreasuryAccountId = TreasuryAccountId;89	type InflationBlockInterval = InflationBlockInterval;90}9192// Build genesis storage according to the mock runtime.93pub fn new_test_ext() -> sp_io::TestExternalities {94	frame_system::GenesisConfig::default()95		.build_storage::<Test>()96		.unwrap()97		.into()98}99100#[test]101fn inflation_works() {102	new_test_ext().execute_with(|| {103		// Total issuance = 1_000_000_000104		let initial_issuance: u64 = 1_000_000_000;105		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);106		assert_eq!(Balances::free_balance(1234), initial_issuance);107108		// BlockInflation should be set after 1st block and 109		// first inflation deposit should be equal to BlockInflation110		Inflation::on_initialize(1);111		assert!(Inflation::block_inflation() > 0);112		assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation());113	});114}115116#[test]117fn inflation_second_deposit() {118	new_test_ext().execute_with(|| {119		// Total issuance = 1_000_000_000120		let initial_issuance: u64 = 1_000_000_000;121		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);122		assert_eq!(Balances::free_balance(1234), initial_issuance);123		Inflation::on_initialize(1);124125		// Next inflation deposit happens when block is multiple of InflationBlockInterval126		let mut block: u32 = 2;127		let balance_before: u64 = Balances::free_balance(1234);128		while block % InflationBlockInterval::get() != 0 {129			Inflation::on_initialize(block as u64);130			block += 1;131		}132		let balance_just_before: u64 = Balances::free_balance(1234);133		assert_eq!(balance_before, balance_just_before);134135		// The block with inflation136		Inflation::on_initialize(block as u64);137		let balance_after: u64 = Balances::free_balance(1234);138		assert_eq!(139			balance_after - balance_just_before,140			Inflation::block_inflation()141		);142	});143}144145#[test]146fn inflation_in_1_year() {147	new_test_ext().execute_with(|| {148		// Total issuance = 1_000_000_000149		let initial_issuance: u64 = 1_000_000_000;150		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);151		assert_eq!(Balances::free_balance(1234), initial_issuance);152		Inflation::on_initialize(1);153		let block_inflation_year_0 = Inflation::block_inflation();154155		Inflation::on_initialize(YEAR);156		let block_inflation_year_1 = Inflation::block_inflation();157158		// Assert that year 1 inflation is less than year 0159		assert!(block_inflation_year_0 > block_inflation_year_1);160	});161}162163#[test]164fn inflation_in_1_to_9_years() {165	new_test_ext().execute_with(|| {166		// Total issuance = 1_000_000_000167		let initial_issuance: u64 = 1_000_000_000;168		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);169		assert_eq!(Balances::free_balance(1234), initial_issuance);170		Inflation::on_initialize(1);171172		for year in 1..=9 {173			let block_inflation_year_before = Inflation::block_inflation();174			Inflation::on_initialize(YEAR * year);175			let block_inflation_year_after = Inflation::block_inflation();176177			// Assert that next year inflation is less than previous year inflation178			assert!(block_inflation_year_before > block_inflation_year_after);179		}180	});181}182183#[test]184fn inflation_after_year_10_is_flat() {185	new_test_ext().execute_with(|| {186		// Total issuance = 1_000_000_000187		let initial_issuance: u64 = 1_000_000_000;188		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);189		assert_eq!(Balances::free_balance(1234), initial_issuance);190		Inflation::on_initialize(YEAR * 9);191192		for year in 10..=20 {193			let block_inflation_year_before = Inflation::block_inflation();194			Inflation::on_initialize(YEAR * year);195			let block_inflation_year_after = Inflation::block_inflation();196197			// Assert that next year inflation is equal to previous year inflation198			assert_eq!(block_inflation_year_before, block_inflation_year_after);199		}200	});201}202203#[test]204fn inflation_rate_by_year() {205	new_test_ext().execute_with(|| {206		let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;207208		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),209		// then it is flat.210		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];211212		// For accuracy total issuance = payout0 * payouts * 10;213		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;214		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);215		assert_eq!(Balances::free_balance(1234), initial_issuance);216217		for year in 0..=10 {218			// Year first block219			Inflation::on_initialize(year * YEAR);220			let mut actual_payout = Inflation::block_inflation();221			assert_eq!(actual_payout, payout_by_year[year as usize]);222223			// Year second block224			Inflation::on_initialize(year * YEAR + 1);225			actual_payout = Inflation::block_inflation();226			assert_eq!(actual_payout, payout_by_year[year as usize]);227228			// Year middle block229			Inflation::on_initialize(year * YEAR + YEAR / 2);230			actual_payout = Inflation::block_inflation();231			assert_eq!(actual_payout, payout_by_year[year as usize]);232233			// Year last block234			Inflation::on_initialize((year + 1) * YEAR - 1);235			actual_payout = Inflation::block_inflation();236			assert_eq!(actual_payout, payout_by_year[year as usize]);237		}238	});239}