git.delta.rocks / unique-network / refs/commits / 3495f61f0acd

difftreelog

Fix imflation unit tests

Greg Zaitsev2021-12-07parent: #4a7e624.patch.diff
in: master

1 file changed

modifiedpallets/inflation/src/tests.rsdiffbeforeafterboth
before · pallets/inflation/src/tests.rs
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::{10	traits::{OnInitialize, Everything},11};12use sp_core::H256;13use sp_runtime::{14	traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},15	testing::Header,16};1718type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;19type Block = frame_system::mocking::MockBlock<Test>;2021const YEAR: u64 = 2_629_800;2223parameter_types! {24	pub const ExistentialDeposit: u64 = 1;25	pub const MaxLocks: u32 = 50;26}2728impl pallet_balances::Config for Test {29	type AccountStore = System;30	type Balance = u64;31	type DustRemoval = ();32	type Event = ();33	type ExistentialDeposit = ExistentialDeposit;34	type WeightInfo = ();35	type MaxLocks = MaxLocks;36	type MaxReserves = ();37	type ReserveIdentifier = ();38}3940frame_support::construct_runtime!(41	pub enum Test where42		Block = Block,43		NodeBlock = Block,44		UncheckedExtrinsic = UncheckedExtrinsic,45	{46		Balances: pallet_balances::{Pallet, Call, Storage},47		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},48		Inflation: pallet_inflation::{Pallet, Call, Storage},49	}50);5152parameter_types! {53	pub const BlockHashCount: u64 = 250;54	pub BlockWeights: frame_system::limits::BlockWeights =55		frame_system::limits::BlockWeights::simple_max(1024);56	pub const SS58Prefix: u8 = 42;57}5859impl frame_system::Config for Test {60	type BaseCallFilter = Everything;61	type BlockWeights = ();62	type BlockLength = ();63	type DbWeight = ();64	type Origin = Origin;65	type Call = Call;66	type Index = u64;67	type BlockNumber = u64;68	type Hash = H256;69	type Hashing = BlakeTwo256;70	type AccountId = u64;71	type Lookup = IdentityLookup<Self::AccountId>;72	type Header = Header;73	type Event = ();74	type BlockHashCount = BlockHashCount;75	type Version = ();76	type PalletInfo = PalletInfo;77	type AccountData = pallet_balances::AccountData<u64>;78	type OnNewAccount = ();79	type OnKilledAccount = ();80	type SystemWeightInfo = ();81	type SS58Prefix = SS58Prefix;82	type OnSetCode = ();83}8485parameter_types! {86	pub TreasuryAccountId: u64 = 1234;87	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied88	pub static MockBlockNumberProvider: u64 = 0;89}9091impl BlockNumberProvider for MockBlockNumberProvider {92	type BlockNumber = u64;9394	fn current_block_number() -> Self::BlockNumber {95		Self::get()96	}97}9899impl pallet_inflation::Config for Test {100	type Currency = Balances;101	type TreasuryAccountId = TreasuryAccountId;102	type InflationBlockInterval = InflationBlockInterval;103	type BlockNumberProvider = MockBlockNumberProvider;104}105106pub fn new_test_ext() -> sp_io::TestExternalities {107	frame_system::GenesisConfig::default()108		.build_storage::<Test>()109		.unwrap()110		.into()111}112113#[test]114fn inflation_works() {115	new_test_ext().execute_with(|| {116		// Total issuance = 1_000_000_000117		let initial_issuance: u64 = 1_000_000_000;118		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);119		assert_eq!(Balances::free_balance(1234), initial_issuance);120121		// BlockInflation should be set after 1st block and122		// first inflation deposit should be equal to BlockInflation123		MockBlockNumberProvider::set(1);124		Inflation::on_initialize(0);125126		// Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = 3803127		assert_eq!(Inflation::block_inflation(), 3803);128		assert_eq!(129			Balances::free_balance(1234) - initial_issuance,130			Inflation::block_inflation()131		);132	});133}134135#[test]136fn inflation_second_deposit() {137	new_test_ext().execute_with(|| {138		// Total issuance = 1_000_000_000139		let initial_issuance: u64 = 1_000_000_000;140		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);141		assert_eq!(Balances::free_balance(1234), initial_issuance);142		MockBlockNumberProvider::set(1);143		Inflation::on_initialize(0);144145		// Next inflation deposit happens when block is multiple of InflationBlockInterval146		let mut block: u32 = 2;147		let balance_before: u64 = Balances::free_balance(1234);148		while block % InflationBlockInterval::get() != 0 {149			MockBlockNumberProvider::set(block as u64);150			Inflation::on_initialize(0);151			block += 1;152		}153		let balance_just_before: u64 = Balances::free_balance(1234);154		assert_eq!(balance_before, balance_just_before);155156		// The block with inflation157		MockBlockNumberProvider::set(block as u64);158		Inflation::on_initialize(0);159		let balance_after: u64 = Balances::free_balance(1234);160		assert_eq!(161			balance_after - balance_just_before,162			Inflation::block_inflation()163		);164	});165}166167#[test]168fn inflation_in_1_year() {169	new_test_ext().execute_with(|| {170		// Total issuance = 1_000_000_000171		let initial_issuance: u64 = 1_000_000_000;172		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);173		assert_eq!(Balances::free_balance(1234), initial_issuance);174		MockBlockNumberProvider::set(1);175		Inflation::on_initialize(0);176177		// Go through all the block inflations for year 1,178		// total issuance will be updated accordingly179		for block in (100..YEAR).step_by(100) {180			MockBlockNumberProvider::set(block);181			Inflation::on_initialize(0);182		}183		assert_eq!(184			initial_issuance + (3803 * (YEAR / 100)),185			<Balances as Currency<_>>::total_issuance()186		);187188		MockBlockNumberProvider::set(YEAR);189		Inflation::on_initialize(0);190		let block_inflation_year_1 = Inflation::block_inflation();191		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR = 3904192		assert_eq!(block_inflation_year_1, 3904);193	});194}195196#[test]197fn inflation_in_1_to_9_years() {198	new_test_ext().execute_with(|| {199		// Total issuance = 1_000_000_000200		let initial_issuance: u64 = 1_000_000_000;201202		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);203		assert_eq!(Balances::free_balance(1234), initial_issuance);204		MockBlockNumberProvider::set(1);205		Inflation::on_initialize(0);206207		for year in 1..=9 {208			let block_inflation_year_before = Inflation::block_inflation();209			MockBlockNumberProvider::set(YEAR * year);210			Inflation::on_initialize(0);211			let block_inflation_year_after = Inflation::block_inflation();212213			// SBP M2 review: this is actually not true (not for the first few years)214			// Assert that next year inflation is less than previous year inflation215			assert!(block_inflation_year_before > block_inflation_year_after);216		}217	});218}219220#[test]221fn inflation_after_year_10_is_flat() {222	new_test_ext().execute_with(|| {223		// Total issuance = 1_000_000_000224		let initial_issuance: u64 = 1_000_000_000;225		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);226		assert_eq!(Balances::free_balance(1234), initial_issuance);227		MockBlockNumberProvider::set(YEAR * 9);228		Inflation::on_initialize(0);229230		for year in 10..=20 {231			let block_inflation_year_before = Inflation::block_inflation();232			MockBlockNumberProvider::set(YEAR * year);233			Inflation::on_initialize(0);234			let block_inflation_year_after = Inflation::block_inflation();235236			// Assert that next year inflation is equal to previous year inflation237			assert_eq!(block_inflation_year_before, block_inflation_year_after);238		}239	});240}241242#[test]243fn inflation_rate_by_year() {244	new_test_ext().execute_with(|| {245		let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;246247		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),248		// then it is flat.249		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];250251		// For accuracy total issuance = payout0 * payouts * 10;252		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;253		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);254		assert_eq!(Balances::free_balance(1234), initial_issuance);255256		for year in 0..=10 {257			// Year first block258			MockBlockNumberProvider::set(YEAR * year);259			Inflation::on_initialize(0);260			let mut actual_payout = Inflation::block_inflation();261			assert_eq!(actual_payout, payout_by_year[year as usize]);262263			// Year second block264			MockBlockNumberProvider::set(YEAR * year + 1);265			Inflation::on_initialize(0);266			actual_payout = Inflation::block_inflation();267			assert_eq!(actual_payout, payout_by_year[year as usize]);268269			// Year middle block270			MockBlockNumberProvider::set(year * YEAR + YEAR / 2);271			Inflation::on_initialize(0);272			actual_payout = Inflation::block_inflation();273			assert_eq!(actual_payout, payout_by_year[year as usize]);274275			// Year last block276			MockBlockNumberProvider::set((year + 1) * YEAR - 1);277			Inflation::on_initialize(0);278			actual_payout = Inflation::block_inflation();279			assert_eq!(actual_payout, payout_by_year[year as usize]);280		}281	});282}
after · pallets/inflation/src/tests.rs
1#![cfg(test)]2#![allow(clippy::from_over_into)]3use crate as pallet_inflation;45use frame_support::{6	assert_ok, parameter_types,7	traits::{Currency, OnInitialize, Everything},8};9use frame_system::RawOrigin;10use sp_core::H256;11use sp_runtime::{12	traits::{BlakeTwo256, BlockNumberProvider, 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; // 6-second blocks20							 // const YEAR: u64 = 2_629_800; // 12-second blocks21							 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION22const FIRST_YEAR_BLOCK_INFLATION: u64 = 1901;2324parameter_types! {25	pub const ExistentialDeposit: u64 = 1;26	pub const MaxLocks: u32 = 50;27}2829impl pallet_balances::Config for Test {30	type AccountStore = System;31	type Balance = u64;32	type DustRemoval = ();33	type Event = ();34	type ExistentialDeposit = ExistentialDeposit;35	type WeightInfo = ();36	type MaxLocks = MaxLocks;37	type MaxReserves = ();38	type ReserveIdentifier = ();39}4041frame_support::construct_runtime!(42	pub enum Test where43		Block = Block,44		NodeBlock = Block,45		UncheckedExtrinsic = UncheckedExtrinsic,46	{47		Balances: pallet_balances::{Pallet, Call, Storage},48		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},49		Inflation: pallet_inflation::{Pallet, Call, Storage},50	}51);5253parameter_types! {54	pub const BlockHashCount: u64 = 250;55	pub BlockWeights: frame_system::limits::BlockWeights =56		frame_system::limits::BlockWeights::simple_max(1024);57	pub const SS58Prefix: u8 = 42;58}5960impl frame_system::Config for Test {61	type BaseCallFilter = Everything;62	type BlockWeights = ();63	type BlockLength = ();64	type DbWeight = ();65	type Origin = Origin;66	type Call = Call;67	type Index = u64;68	type BlockNumber = u64;69	type Hash = H256;70	type Hashing = BlakeTwo256;71	type AccountId = u64;72	type Lookup = IdentityLookup<Self::AccountId>;73	type Header = Header;74	type Event = ();75	type BlockHashCount = BlockHashCount;76	type Version = ();77	type PalletInfo = PalletInfo;78	type AccountData = pallet_balances::AccountData<u64>;79	type OnNewAccount = ();80	type OnKilledAccount = ();81	type SystemWeightInfo = ();82	type SS58Prefix = SS58Prefix;83	type OnSetCode = ();84}8586parameter_types! {87	pub TreasuryAccountId: u64 = 1234;88	pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied89	pub static MockBlockNumberProvider: u64 = 0;90}9192impl BlockNumberProvider for MockBlockNumberProvider {93	type BlockNumber = u64;9495	fn current_block_number() -> Self::BlockNumber {96		Self::get()97	}98}99100impl pallet_inflation::Config for Test {101	type Currency = Balances;102	type TreasuryAccountId = TreasuryAccountId;103	type InflationBlockInterval = InflationBlockInterval;104	type BlockNumberProvider = MockBlockNumberProvider;105}106107pub fn new_test_ext() -> sp_io::TestExternalities {108	frame_system::GenesisConfig::default()109		.build_storage::<Test>()110		.unwrap()111		.into()112}113114macro_rules! block_inflation {115	// Block inflation doesn't have any argumets116	() => {117		// Return BlockInflation state variable current value118		<pallet_inflation::BlockInflation<Test>>::get()119	};120}121122#[test]123fn uninitialized_inflation() {124	new_test_ext().execute_with(|| {125		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);128129		// BlockInflation should be set after inflation is started130		// first inflation deposit should be equal to BlockInflation131		MockBlockNumberProvider::set(1);132133		assert_eq!(block_inflation!(), 0);134	});135}136137#[test]138fn inflation_works() {139	new_test_ext().execute_with(|| {140		// Total issuance = 1_000_000_000141		let initial_issuance: u64 = 1_000_000_000;142		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);143		assert_eq!(Balances::free_balance(1234), initial_issuance);144145		// BlockInflation should be set after inflation is started146		// first inflation deposit should be equal to BlockInflation147		MockBlockNumberProvider::set(1);148149		// Start inflation as sudo150		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));151		assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);152		assert_eq!(153			Balances::free_balance(1234) - initial_issuance,154			block_inflation!()155		);156157		// Trigger inflation158		MockBlockNumberProvider::set(102);159		Inflation::on_initialize(0);160		assert_eq!(161			Balances::free_balance(1234) - initial_issuance,162			2 * block_inflation!()163		);164	});165}166167#[test]168fn inflation_second_deposit() {169	new_test_ext().execute_with(|| {170		// Total issuance = 1_000_000_000171		let initial_issuance: u64 = 1_000_000_000;172		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);173		assert_eq!(Balances::free_balance(1234), initial_issuance);174		MockBlockNumberProvider::set(1);175176		// Start inflation as sudo177		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));178179		// Next inflation deposit happens when block is greater then or equal to NextInflationBlock180		let mut block: u64 = 2;181		let balance_before: u64 = Balances::free_balance(1234);182		while block < <pallet_inflation::NextInflationBlock<Test>>::get() {183			MockBlockNumberProvider::set(block as u64);184			Inflation::on_initialize(0);185			block += 1;186		}187		let balance_just_before: u64 = Balances::free_balance(1234);188		assert_eq!(balance_before, balance_just_before);189190		// The block with inflation191		MockBlockNumberProvider::set(block as u64);192		Inflation::on_initialize(0);193		let balance_after: u64 = Balances::free_balance(1234);194		assert_eq!(balance_after - balance_just_before, block_inflation!());195	});196}197198#[test]199fn inflation_in_1_year() {200	new_test_ext().execute_with(|| {201		// Total issuance = 1_000_000_000202		let initial_issuance: u64 = 1_000_000_000;203		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);204		assert_eq!(Balances::free_balance(1234), initial_issuance);205		MockBlockNumberProvider::set(1);206207		// Start inflation as sudo208		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));209210		// Go through all the block inflations for year 1,211		// total issuance will be updated accordingly212		// Inflation is set to start in block 1, so first iteration is block 101213		for block in (101..YEAR).step_by(100) {214			MockBlockNumberProvider::set(block);215			Inflation::on_initialize(0);216		}217		assert_eq!(218			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),219			<Balances as Currency<_>>::total_issuance()220		);221222		MockBlockNumberProvider::set(YEAR + 1);223		Inflation::on_initialize(0);224		let block_inflation_year_2 = block_inflation!();225		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951226		let expecter_year_2_inflation: u64 = (initial_issuance227			+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)228			* 933 * 100 / (10000 * YEAR);229		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality230	});231}232233#[test]234fn inflation_after_year_10_is_flat() {235	new_test_ext().execute_with(|| {236		// Total issuance = 1_000_000_000237		let initial_issuance: u64 = 1_000_000_000;238		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);239		assert_eq!(Balances::free_balance(1234), initial_issuance);240		MockBlockNumberProvider::set(YEAR * 9 + 1);241242		// Start inflation as sudo243		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));244245		// Let inflation catch up246		for _year in 1..=9 {247			Inflation::on_initialize(0);248		}249250		for year in 10..=20 {251			let block_inflation_year_before = block_inflation!();252			MockBlockNumberProvider::set(YEAR * year + 1);253			Inflation::on_initialize(0);254			let block_inflation_year_after = block_inflation!();255256			// Assert that next year inflation is equal to previous year inflation257			assert_eq!(block_inflation_year_before, block_inflation_year_after);258		}259	});260}261262#[test]263fn inflation_rate_by_year() {264	new_test_ext().execute_with(|| {265		let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;266267		// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),268		// then it is flat.269		let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];270271		// For accuracy total issuance = payout0 * payouts * 10;272		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;273		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);274		assert_eq!(Balances::free_balance(1234), initial_issuance);275276		// Start inflation as sudo277		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));278279		for year in 0..=10 {280			// Year first block281			MockBlockNumberProvider::set(YEAR * year + 1);282			Inflation::on_initialize(0);283			let mut actual_payout = block_inflation!();284			assert_eq!(actual_payout, payout_by_year[year as usize]);285286			// Year second block287			MockBlockNumberProvider::set(YEAR * year + 2);288			Inflation::on_initialize(0);289			actual_payout = block_inflation!();290			assert_eq!(actual_payout, payout_by_year[year as usize]);291292			// Year middle block293			MockBlockNumberProvider::set(year * YEAR + YEAR / 2);294			Inflation::on_initialize(0);295			actual_payout = block_inflation!();296			assert_eq!(actual_payout, payout_by_year[year as usize]);297298			// Year last block299			MockBlockNumberProvider::set((year + 1) * YEAR);300			Inflation::on_initialize(0);301			actual_payout = block_inflation!();302			assert_eq!(actual_payout, payout_by_year[year as usize]);303		}304	});305}