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

difftreelog

source

pallets/inflation/src/tests.rs7.4 KiBsourcehistory
1#[cfg(test)]2mod tests {3	use crate as pallet_inflation;45	use frame_system;6	use frame_support::{traits::{Currency}, parameter_types};7	use frame_support::{traits::OnInitialize};8	use sp_core::H256;9	use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};1011	type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;12	type Block = frame_system::mocking::MockBlock<Test>;1314	const YEAR: u64 = 5_259_600;1516	parameter_types! {17		pub const ExistentialDeposit: u64 = 1;18		pub const MaxLocks: u32 = 50;19	}20	21	impl pallet_balances::Config for Test {22		type AccountStore = System;23		type Balance = u64;24		type DustRemoval = ();25		type Event = ();26		type ExistentialDeposit = ExistentialDeposit;27		type WeightInfo = ();28		type MaxLocks = MaxLocks;29	}30	31	frame_support::construct_runtime!(32		pub enum Test where33			Block = Block,34			NodeBlock = Block,35			UncheckedExtrinsic = UncheckedExtrinsic,36		{37			Balances: pallet_balances::{Pallet, Call, Storage},38			System: frame_system::{Pallet, Call, Config, Storage, Event<T>},39			Inflation: pallet_inflation::{Pallet, Call, Storage},40		}41	);4243	parameter_types! {44		pub const BlockHashCount: u64 = 250;45		pub BlockWeights: frame_system::limits::BlockWeights =46			frame_system::limits::BlockWeights::simple_max(1024);47		pub const SS58Prefix: u8 = 42;48	}4950	impl frame_system::Config for Test {51		type BaseCallFilter = ();52		type BlockWeights = ();53		type BlockLength = ();54		type DbWeight = ();55		type Origin = Origin;56		type Call = Call;57		type Index = u64;58		type BlockNumber = u64;59		type Hash = H256;60		type Hashing = BlakeTwo256;61		type AccountId = u64;62		type Lookup = IdentityLookup<Self::AccountId>;63		type Header = Header;64		type Event = ();65		type BlockHashCount = BlockHashCount;66		type Version = ();67		type PalletInfo = PalletInfo;68		type AccountData = pallet_balances::AccountData<u64>;69		type OnNewAccount = ();70		type OnKilledAccount = ();71		type SystemWeightInfo = ();72		type SS58Prefix = SS58Prefix;73        type OnSetCode = ();74	}7576	parameter_types! {77		pub TreasuryAccountId: u64 = 1234;78		pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied79	}80		81	impl pallet_inflation::Config for Test {82		type Currency = Balances;83		type TreasuryAccountId = TreasuryAccountId;84		type InflationBlockInterval = InflationBlockInterval;85	}8687	// Build genesis storage according to the mock runtime.88	pub fn new_test_ext() -> sp_io::TestExternalities {89		frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()90	}9192	#[test]93	fn inflation_works() {94		new_test_ext().execute_with(|| {95			// Total issuance = 1_000_000_00096			let initial_issuance: u64 = 1_000_000_000;97			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);98			assert_eq!(Balances::free_balance(1234), initial_issuance);99100			// BlockInflation should be set after 1st block and 101			// first inflation deposit should be equal to BlockInflation102			Inflation::on_initialize(1);103			assert!(Inflation::block_inflation() > 0);104			assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation());105		});106	}107108	#[test]109	fn inflation_second_deposit() {110		new_test_ext().execute_with(|| {111			// Total issuance = 1_000_000_000112			let initial_issuance: u64 = 1_000_000_000;113			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);114			assert_eq!(Balances::free_balance(1234), initial_issuance);115			Inflation::on_initialize(1);116117			// Next inflation deposit happens when block is multiple of InflationBlockInterval118			let mut block: u32 = 2;119			let balance_before: u64 = Balances::free_balance(1234);120			while block % InflationBlockInterval::get() != 0 {121				Inflation::on_initialize(block as u64);122				block += 1;123			}124			let balance_just_before: u64 = Balances::free_balance(1234);125			assert_eq!(balance_before, balance_just_before);126127			// The block with inflation128			Inflation::on_initialize(block as u64);129			let balance_after: u64 = Balances::free_balance(1234);130			assert_eq!(balance_after - balance_just_before, Inflation::block_inflation());131		});132	}133134	#[test]135	fn inflation_in_1_year() {136		new_test_ext().execute_with(|| {137			// Total issuance = 1_000_000_000138			let initial_issuance: u64 = 1_000_000_000;139			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);140			assert_eq!(Balances::free_balance(1234), initial_issuance);141			Inflation::on_initialize(1);142			let block_inflation_year_0 = Inflation::block_inflation();143144			Inflation::on_initialize(YEAR);145			let block_inflation_year_1 = Inflation::block_inflation();146147			// Assert that year 1 inflation is less than year 0148			assert!(block_inflation_year_0 > block_inflation_year_1);149		});150	}151152	#[test]153	fn inflation_in_1_to_9_years() {154		new_test_ext().execute_with(|| {155			// Total issuance = 1_000_000_000156			let initial_issuance: u64 = 1_000_000_000;157			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);158			assert_eq!(Balances::free_balance(1234), initial_issuance);159			Inflation::on_initialize(1);160161			for year in 1..=9 {162				let block_inflation_year_before = Inflation::block_inflation();163				Inflation::on_initialize(YEAR * year);164				let block_inflation_year_after = Inflation::block_inflation();165166				// Assert that next year inflation is less than previous year inflation167				assert!(block_inflation_year_before > block_inflation_year_after);168			}169170		});171	}172173	#[test]174	fn inflation_after_year_10_is_flat() {175		new_test_ext().execute_with(|| {176			// Total issuance = 1_000_000_000177			let initial_issuance: u64 = 1_000_000_000;178			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);179			assert_eq!(Balances::free_balance(1234), initial_issuance);180			Inflation::on_initialize(YEAR * 9);181182			for year in 10..=20 {183				let block_inflation_year_before = Inflation::block_inflation();184				Inflation::on_initialize(YEAR * year);185				let block_inflation_year_after = Inflation::block_inflation();186187				// Assert that next year inflation is equal to previous year inflation188				assert_eq!(block_inflation_year_before, block_inflation_year_after);189			}190		});191	}192193	#[test]194	fn inflation_rate_by_year() {195		new_test_ext().execute_with(|| {196			let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;197198			// Inflation starts at 10% and does down by 2/3% every year until year 9 (included), 199			// then it is flat.200			let payout_by_year: [u64; 11] = [201				1000,202				933,203				867,204				800,205				733,206				667,207				600,208				533,209				467,210				400,211				400212			];213214			// For accuracy total issuance = payout0 * payouts * 10;215			let initial_issuance: u64 = payout_by_year[0] * payouts * 10;216			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);217			assert_eq!(Balances::free_balance(1234), initial_issuance);218219			for year in 0..=10 {220				// Year first block221				Inflation::on_initialize(year*YEAR);222				let mut actual_payout = Inflation::block_inflation();223				assert_eq!(actual_payout, payout_by_year[year as usize]);224225				// Year second block226				Inflation::on_initialize(year*YEAR+1);227				actual_payout = Inflation::block_inflation();228				assert_eq!(actual_payout, payout_by_year[year as usize]);229230				// Year middle block231				Inflation::on_initialize(year*YEAR + YEAR/2);232				actual_payout = Inflation::block_inflation();233				assert_eq!(actual_payout, payout_by_year[year as usize]);234235				// Year last block236				Inflation::on_initialize((year + 1)*YEAR-1);237				actual_payout = Inflation::block_inflation();238				assert_eq!(actual_payout, payout_by_year[year as usize]);239			}240		});241	}242}