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

difftreelog

SBP M2 review: comments on inflation pallet's tests

Steve Degosserie2021-06-04parent: #c803bd5.patch.diff
in: master

1 file changed

modifiedpallets/inflation/src/tests.rsdiffbeforeafterboth
before · pallets/inflation/src/tests.rs
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::{Module, Call, Storage},38			System: frame_system::{Module, Call, Config, Storage, Event<T>},39			Inflation: pallet_inflation::{Module, 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	}7475	parameter_types! {76		pub TreasuryAccountId: u64 = 1234;77		pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied78	}79		80	impl pallet_inflation::Config for Test {81		type Currency = Balances;82		type TreasuryAccountId = TreasuryAccountId;83		type InflationBlockInterval = InflationBlockInterval;84	}8586	// Build genesis storage according to the mock runtime.87	pub fn new_test_ext() -> sp_io::TestExternalities {88		frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()89	}9091	#[test]92	fn inflation_works() {93		new_test_ext().execute_with(|| {94			// Total issuance = 1_000_000_00095			let initial_issuance: u64 = 1_000_000_000;96			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);97			assert_eq!(Balances::free_balance(1234), initial_issuance);9899			// BlockInflation should be set after 1st block and 100			// first inflation deposit should be equal to BlockInflation101			Inflation::on_initialize(1);102			assert!(Inflation::block_inflation() > 0);103			assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation());104		});105	}106107	#[test]108	fn inflation_second_deposit() {109		new_test_ext().execute_with(|| {110			// Total issuance = 1_000_000_000111			let initial_issuance: u64 = 1_000_000_000;112			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);113			assert_eq!(Balances::free_balance(1234), initial_issuance);114			Inflation::on_initialize(1);115116			// Next inflation deposit happens when block is multiple of InflationBlockInterval117			let mut block: u32 = 2;118			let balance_before: u64 = Balances::free_balance(1234);119			while block % InflationBlockInterval::get() != 0 {120				Inflation::on_initialize(block as u64);121				block += 1;122			}123			let balance_just_before: u64 = Balances::free_balance(1234);124			assert_eq!(balance_before, balance_just_before);125126			// The block with inflation127			Inflation::on_initialize(block as u64);128			let balance_after: u64 = Balances::free_balance(1234);129			assert_eq!(balance_after - balance_just_before, Inflation::block_inflation());130		});131	}132133	#[test]134	fn inflation_in_1_year() {135		new_test_ext().execute_with(|| {136			// Total issuance = 1_000_000_000137			let initial_issuance: u64 = 1_000_000_000;138			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);139			assert_eq!(Balances::free_balance(1234), initial_issuance);140			Inflation::on_initialize(1);141			let block_inflation_year_0 = Inflation::block_inflation();142143			Inflation::on_initialize(YEAR);144			let block_inflation_year_1 = Inflation::block_inflation();145146			// Assert that year 1 inflation is less than year 0147			assert!(block_inflation_year_0 > block_inflation_year_1);148		});149	}150151	#[test]152	fn inflation_in_1_to_9_years() {153		new_test_ext().execute_with(|| {154			// Total issuance = 1_000_000_000155			let initial_issuance: u64 = 1_000_000_000;156			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);157			assert_eq!(Balances::free_balance(1234), initial_issuance);158			Inflation::on_initialize(1);159160			for year in 1..=9 {161				let block_inflation_year_before = Inflation::block_inflation();162				Inflation::on_initialize(YEAR * year);163				let block_inflation_year_after = Inflation::block_inflation();164165				// Assert that next year inflation is less than previous year inflation166				assert!(block_inflation_year_before > block_inflation_year_after);167			}168169		});170	}171172	#[test]173	fn inflation_after_year_10_is_flat() {174		new_test_ext().execute_with(|| {175			// Total issuance = 1_000_000_000176			let initial_issuance: u64 = 1_000_000_000;177			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);178			assert_eq!(Balances::free_balance(1234), initial_issuance);179			Inflation::on_initialize(YEAR * 9);180181			for year in 10..=20 {182				let block_inflation_year_before = Inflation::block_inflation();183				Inflation::on_initialize(YEAR * year);184				let block_inflation_year_after = Inflation::block_inflation();185186				// Assert that next year inflation is equal to previous year inflation187				assert_eq!(block_inflation_year_before, block_inflation_year_after);188			}189		});190	}191192	#[test]193	fn inflation_rate_by_year() {194		new_test_ext().execute_with(|| {195			let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;196197			// Inflation starts at 10% and does down by 2/3% every year until year 9 (included), 198			// then it is flat.199			let payout_by_year: [u64; 11] = [200				1000,201				933,202				867,203				800,204				733,205				667,206				600,207				533,208				467,209				400,210				400211			];212213			// For accuracy total issuance = payout0 * payouts * 10;214			let initial_issuance: u64 = payout_by_year[0] * payouts * 10;215			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);216			assert_eq!(Balances::free_balance(1234), initial_issuance);217218			for year in 0..=10 {219				// Year first block220				Inflation::on_initialize(year*YEAR);221				let mut actual_payout = Inflation::block_inflation();222				assert_eq!(actual_payout, payout_by_year[year as usize]);223224				// Year second block225				Inflation::on_initialize(year*YEAR+1);226				actual_payout = Inflation::block_inflation();227				assert_eq!(actual_payout, payout_by_year[year as usize]);228229				// Year middle block230				Inflation::on_initialize(year*YEAR + YEAR/2);231				actual_payout = Inflation::block_inflation();232				assert_eq!(actual_payout, payout_by_year[year as usize]);233234				// Year last block235				Inflation::on_initialize((year + 1)*YEAR-1);236				actual_payout = Inflation::block_inflation();237				assert_eq!(actual_payout, payout_by_year[year as usize]);238			}239		});240	}241}
after · pallets/inflation/src/tests.rs
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::{Module, Call, Storage},38			System: frame_system::{Module, Call, Config, Storage, Event<T>},39			Inflation: pallet_inflation::{Module, 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	}7475	parameter_types! {76		pub TreasuryAccountId: u64 = 1234;77		pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied78	}79		80	impl pallet_inflation::Config for Test {81		type Currency = Balances;82		type TreasuryAccountId = TreasuryAccountId;83		type InflationBlockInterval = InflationBlockInterval;84	}8586	// Build genesis storage according to the mock runtime.87	pub fn new_test_ext() -> sp_io::TestExternalities {88		frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()89	}9091	#[test]92	fn inflation_works() {93		new_test_ext().execute_with(|| {94			// Total issuance = 1_000_000_00095			let initial_issuance: u64 = 1_000_000_000;96			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);97			assert_eq!(Balances::free_balance(1234), initial_issuance);9899			// BlockInflation should be set after 1st block and 100			// first inflation deposit should be equal to BlockInflation101			Inflation::on_initialize(1);102103			// SBP M2 review: Verify expected block inflation for year 1104			assert_eq!(Inflation::block_inflation(), 1901);105			assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation());106		});107	}108109	#[test]110	fn inflation_second_deposit() {111		new_test_ext().execute_with(|| {112			// Total issuance = 1_000_000_000113			let initial_issuance: u64 = 1_000_000_000;114			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);115			assert_eq!(Balances::free_balance(1234), initial_issuance);116			Inflation::on_initialize(1);117118			// Next inflation deposit happens when block is multiple of InflationBlockInterval119			let mut block: u32 = 2;120			let balance_before: u64 = Balances::free_balance(1234);121			while block % InflationBlockInterval::get() != 0 {122				Inflation::on_initialize(block as u64);123				block += 1;124			}125			let balance_just_before: u64 = Balances::free_balance(1234);126			assert_eq!(balance_before, balance_just_before);127128			// The block with inflation129			Inflation::on_initialize(block as u64);130			let balance_after: u64 = Balances::free_balance(1234);131			assert_eq!(balance_after - balance_just_before, Inflation::block_inflation());132		});133	}134135	#[test]136	fn inflation_in_1_year() {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			Inflation::on_initialize(1);143			let block_inflation_year_0 = Inflation::block_inflation();144145			// SBP M2 review: go through all the block inflations for year 1,146			// total issuance will be updated accordingly147			for block in (100..YEAR).step_by(100) {148                Inflation::on_initialize(block);149            }150            assert_eq!(151                initial_issuance + (1901 * (YEAR / 100)),152                <Balances as Currency<_>>::total_issuance()153            );154155			Inflation::on_initialize(YEAR);156			let block_inflation_year_1 = Inflation::block_inflation();157			// SBP M2 review: Verify expected block inflation for year 2158			assert_eq!(block_inflation_year_1, 1952);159160			// SBP M2 review: this is actually not true161			// Assert that year 1 inflation is less than year 0162			// assert!(block_inflation_year_0 > block_inflation_year_1);163		});164	}165166	#[test]167	fn inflation_in_1_to_9_years() {168		new_test_ext().execute_with(|| {169			// Total issuance = 1_000_000_000170			let initial_issuance: u64 = 1_000_000_000;171			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);172			assert_eq!(Balances::free_balance(1234), initial_issuance);173			Inflation::on_initialize(1);174175			for year in 1..=9 {176				let block_inflation_year_before = Inflation::block_inflation();177				Inflation::on_initialize(YEAR * year);178				let block_inflation_year_after = Inflation::block_inflation();179180				// SBP M2 review: this is actually not true (not for the first few years)181				// Assert that next year inflation is less than previous year inflation182				assert!(block_inflation_year_before > block_inflation_year_after);183			}184185		});186	}187188	#[test]189	fn 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]209	fn 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] = [216				1000,217				933,218				867,219				800,220				733,221				667,222				600,223				533,224				467,225				400,226				400227			];228229			// For accuracy total issuance = payout0 * payouts * 10;230			let initial_issuance: u64 = payout_by_year[0] * payouts * 10;231			let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);232			assert_eq!(Balances::free_balance(1234), initial_issuance);233234			for year in 0..=10 {235				// Year first block236				Inflation::on_initialize(year*YEAR);237				let mut actual_payout = Inflation::block_inflation();238				assert_eq!(actual_payout, payout_by_year[year as usize]);239240				// Year second block241				Inflation::on_initialize(year*YEAR+1);242				actual_payout = Inflation::block_inflation();243				assert_eq!(actual_payout, payout_by_year[year as usize]);244245				// Year middle block246				Inflation::on_initialize(year*YEAR + YEAR/2);247				actual_payout = Inflation::block_inflation();248				assert_eq!(actual_payout, payout_by_year[year as usize]);249250				// Year last block251				Inflation::on_initialize((year + 1)*YEAR-1);252				actual_payout = Inflation::block_inflation();253				assert_eq!(actual_payout, payout_by_year[year as usize]);254			}255		});256	}257}