1234567891011121314151617#![cfg(test)]18#![allow(clippy::from_over_into)]19use crate as pallet_inflation;2021use frame_support::{22 assert_ok, parameter_types,23 traits::{fungible::{Balanced, Inspect}, OnInitialize, Everything, ConstU32, tokens::Precision},24 weights::Weight,25};26use frame_system::RawOrigin;27use sp_core::H256;28use sp_runtime::{29 traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},30 testing::Header,31};3233type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;34type Block = frame_system::mocking::MockBlock<Test>;3536const YEAR: u64 = 5_259_600; 37 38 39const FIRST_YEAR_BLOCK_INFLATION: u64 = 1901;4041parameter_types! {42 pub const ExistentialDeposit: u64 = 1;43 pub const MaxLocks: u32 = 50;44}4546impl pallet_balances::Config for Test {47 type AccountStore = System;48 type Balance = u64;49 type DustRemoval = ();50 type RuntimeEvent = ();51 type ExistentialDeposit = ExistentialDeposit;52 type WeightInfo = ();53 type MaxLocks = MaxLocks;54 type MaxReserves = ();55 type ReserveIdentifier = ();56 type HoldIdentifier = ();57 type FreezeIdentifier = ();58 type MaxHolds = ();59 type MaxFreezes = ();60}6162frame_support::construct_runtime!(63 pub enum Test where64 Block = Block,65 NodeBlock = Block,66 UncheckedExtrinsic = UncheckedExtrinsic,67 {68 Balances: pallet_balances::{Pallet, Call, Storage},69 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},70 Inflation: pallet_inflation::{Pallet, Call, Storage},71 }72);7374parameter_types! {75 pub const BlockHashCount: u64 = 250;76 pub BlockWeights: frame_system::limits::BlockWeights =77 frame_system::limits::BlockWeights::simple_max(Weight::from_ref_time(1024));78 pub const SS58Prefix: u8 = 42;79}8081impl frame_system::Config for Test {82 type BaseCallFilter = Everything;83 type BlockWeights = ();84 type BlockLength = ();85 type DbWeight = ();86 type RuntimeOrigin = RuntimeOrigin;87 type RuntimeCall = RuntimeCall;88 type Index = u64;89 type BlockNumber = u64;90 type Hash = H256;91 type Hashing = BlakeTwo256;92 type AccountId = u64;93 type Lookup = IdentityLookup<Self::AccountId>;94 type Header = Header;95 type RuntimeEvent = ();96 type BlockHashCount = BlockHashCount;97 type Version = ();98 type PalletInfo = PalletInfo;99 type AccountData = pallet_balances::AccountData<u64>;100 type OnNewAccount = ();101 type OnKilledAccount = ();102 type SystemWeightInfo = ();103 type SS58Prefix = SS58Prefix;104 type OnSetCode = ();105 type MaxConsumers = ConstU32<16>;106}107108parameter_types! {109 pub TreasuryAccountId: u64 = 1234;110 pub const InflationBlockInterval: u32 = 100; 111 pub static MockBlockNumberProvider: u64 = 0;112}113114impl BlockNumberProvider for MockBlockNumberProvider {115 type BlockNumber = u64;116117 fn current_block_number() -> Self::BlockNumber {118 Self::get()119 }120}121122impl pallet_inflation::Config for Test {123 type Currency = Balances;124 type TreasuryAccountId = TreasuryAccountId;125 type InflationBlockInterval = InflationBlockInterval;126 type BlockNumberProvider = MockBlockNumberProvider;127}128129pub fn new_test_ext() -> sp_io::TestExternalities {130 frame_system::GenesisConfig::default()131 .build_storage::<Test>()132 .unwrap()133 .into()134}135136macro_rules! block_inflation {137 138 () => {139 140 <pallet_inflation::BlockInflation<Test>>::get()141 };142}143144#[test]145fn uninitialized_inflation() {146 new_test_ext().execute_with(|| {147 let initial_issuance: u64 = 1_000_000_000;148 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);149 assert_eq!(Balances::free_balance(1234), initial_issuance);150151 152 153 MockBlockNumberProvider::set(1);154155 assert_eq!(block_inflation!(), 0);156 });157}158159#[test]160fn inflation_works() {161 new_test_ext().execute_with(|| {162 163 let initial_issuance: u64 = 1_000_000_000;164 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);165 assert_eq!(Balances::free_balance(1234), initial_issuance);166167 168 169 MockBlockNumberProvider::set(1);170171 172 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));173 assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);174 assert_eq!(175 Balances::free_balance(1234) - initial_issuance,176 block_inflation!()177 );178179 180 MockBlockNumberProvider::set(102);181 Inflation::on_initialize(0);182 assert_eq!(183 Balances::free_balance(1234) - initial_issuance,184 2 * block_inflation!()185 );186 });187}188189#[test]190fn inflation_second_deposit() {191 new_test_ext().execute_with(|| {192 193 let initial_issuance: u64 = 1_000_000_000;194 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);195 assert_eq!(Balances::free_balance(1234), initial_issuance);196 MockBlockNumberProvider::set(1);197198 199 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));200201 202 let mut block: u64 = 2;203 let balance_before: u64 = Balances::free_balance(1234);204 while block < <pallet_inflation::NextInflationBlock<Test>>::get() {205 MockBlockNumberProvider::set(block as u64);206 Inflation::on_initialize(0);207 block += 1;208 }209 let balance_just_before: u64 = Balances::free_balance(1234);210 assert_eq!(balance_before, balance_just_before);211212 213 MockBlockNumberProvider::set(block as u64);214 Inflation::on_initialize(0);215 let balance_after: u64 = Balances::free_balance(1234);216 assert_eq!(balance_after - balance_just_before, block_inflation!());217 });218}219220#[test]221fn inflation_in_1_year() {222 new_test_ext().execute_with(|| {223 224 let initial_issuance: u64 = 1_000_000_000;225 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);226 assert_eq!(Balances::free_balance(1234), initial_issuance);227 MockBlockNumberProvider::set(1);228229 230 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));231232 233 234 235 for block in (101..YEAR).step_by(100) {236 MockBlockNumberProvider::set(block);237 Inflation::on_initialize(0);238 }239 assert_eq!(240 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),241 <Balances as Inspect<_>>::total_issuance()242 );243244 MockBlockNumberProvider::set(YEAR + 1);245 Inflation::on_initialize(0);246 let block_inflation_year_2 = block_inflation!();247 248 let expecter_year_2_inflation: u64 = (initial_issuance249 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)250 * 933 * 100 / (10000 * YEAR);251 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); 252 });253}254255#[test]256fn inflation_start_large_kusama_block() {257 new_test_ext().execute_with(|| {258 259 let initial_issuance: u64 = 1_000_000_000;260 let start_block: u64 = 10457457;261 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);262 assert_eq!(Balances::free_balance(1234), initial_issuance);263 MockBlockNumberProvider::set(start_block);264265 266 assert_ok!(Inflation::start_inflation(267 RawOrigin::Root.into(),268 start_block269 ));270271 272 273 274 for block in (101..YEAR).step_by(100) {275 MockBlockNumberProvider::set(start_block + block);276 Inflation::on_initialize(0);277 }278 assert_eq!(279 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),280 <Balances as Inspect<_>>::total_issuance()281 );282283 MockBlockNumberProvider::set(start_block + YEAR + 1);284 Inflation::on_initialize(0);285 let block_inflation_year_2 = block_inflation!();286 287 let expecter_year_2_inflation: u64 = (initial_issuance288 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)289 * 933 * 100 / (10000 * YEAR);290 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); 291 });292}293294#[test]295fn inflation_after_year_10_is_flat() {296 new_test_ext().execute_with(|| {297 298 let initial_issuance: u64 = 1_000_000_000;299 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);300 assert_eq!(Balances::free_balance(1234), initial_issuance);301 MockBlockNumberProvider::set(YEAR * 9 + 1);302303 304 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));305306 307 for _year in 1..=9 {308 Inflation::on_initialize(0);309 }310311 for year in 10..=20 {312 let block_inflation_year_before = block_inflation!();313 MockBlockNumberProvider::set(YEAR * year + 1);314 Inflation::on_initialize(0);315 let block_inflation_year_after = block_inflation!();316317 318 assert_eq!(block_inflation_year_before, block_inflation_year_after);319 }320 });321}322323#[test]324fn inflation_rate_by_year() {325 new_test_ext().execute_with(|| {326 let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;327328 329 330 let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];331332 333 let initial_issuance: u64 = payout_by_year[0] * payouts * 10;334 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);335 assert_eq!(Balances::free_balance(1234), initial_issuance);336337 338 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));339340 for year in 0..=10 {341 342 MockBlockNumberProvider::set(YEAR * year + 1);343 Inflation::on_initialize(0);344 let mut actual_payout = block_inflation!();345 assert_eq!(actual_payout, payout_by_year[year as usize]);346347 348 MockBlockNumberProvider::set(YEAR * year + 2);349 Inflation::on_initialize(0);350 actual_payout = block_inflation!();351 assert_eq!(actual_payout, payout_by_year[year as usize]);352353 354 MockBlockNumberProvider::set(year * YEAR + YEAR / 2);355 Inflation::on_initialize(0);356 actual_payout = block_inflation!();357 assert_eq!(actual_payout, payout_by_year[year as usize]);358359 360 MockBlockNumberProvider::set((year + 1) * YEAR);361 Inflation::on_initialize(0);362 actual_payout = block_inflation!();363 assert_eq!(actual_payout, payout_by_year[year as usize]);364 }365 });366}