1234567891011121314151617#![cfg(test)]18#![allow(clippy::from_over_into)]19use crate as pallet_inflation;2021use frame_support::{22 assert_ok, parameter_types,23 traits::{24 fungible::{Balanced, Inspect},25 OnInitialize, Everything, ConstU32,26 tokens::Precision,27 },28 weights::Weight,29};30use frame_system::RawOrigin;31use sp_core::H256;32use sp_runtime::{33 traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},34 testing::Header,35};3637type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;38type Block = frame_system::mocking::MockBlock<Test>;3940const YEAR: u64 = 5_259_600; 41 42 43const FIRST_YEAR_BLOCK_INFLATION: u64 = 1901;4445parameter_types! {46 pub const ExistentialDeposit: u64 = 1;47 pub const MaxLocks: u32 = 50;48}4950impl pallet_balances::Config for Test {51 type AccountStore = System;52 type Balance = u64;53 type DustRemoval = ();54 type RuntimeEvent = ();55 type ExistentialDeposit = ExistentialDeposit;56 type WeightInfo = ();57 type MaxLocks = MaxLocks;58 type MaxReserves = ();59 type ReserveIdentifier = ();60 type HoldIdentifier = ();61 type FreezeIdentifier = ();62 type MaxHolds = ();63 type MaxFreezes = ();64}6566frame_support::construct_runtime!(67 pub enum Test where68 Block = Block,69 NodeBlock = Block,70 UncheckedExtrinsic = UncheckedExtrinsic,71 {72 Balances: pallet_balances::{Pallet, Call, Storage},73 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},74 Inflation: pallet_inflation::{Pallet, Call, Storage},75 }76);7778parameter_types! {79 pub const BlockHashCount: u64 = 250;80 pub BlockWeights: frame_system::limits::BlockWeights =81 frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, 0));82 pub const SS58Prefix: u8 = 42;83}8485impl frame_system::Config for Test {86 type BaseCallFilter = Everything;87 type BlockWeights = ();88 type BlockLength = ();89 type DbWeight = ();90 type RuntimeOrigin = RuntimeOrigin;91 type RuntimeCall = RuntimeCall;92 type Index = u64;93 type BlockNumber = u64;94 type Hash = H256;95 type Hashing = BlakeTwo256;96 type AccountId = u64;97 type Lookup = IdentityLookup<Self::AccountId>;98 type Header = Header;99 type RuntimeEvent = ();100 type BlockHashCount = BlockHashCount;101 type Version = ();102 type PalletInfo = PalletInfo;103 type AccountData = pallet_balances::AccountData<u64>;104 type OnNewAccount = ();105 type OnKilledAccount = ();106 type SystemWeightInfo = ();107 type SS58Prefix = SS58Prefix;108 type OnSetCode = ();109 type MaxConsumers = ConstU32<16>;110}111112parameter_types! {113 pub TreasuryAccountId: u64 = 1234;114 pub const InflationBlockInterval: u32 = 100; 115 pub static MockBlockNumberProvider: u64 = 0;116}117118impl BlockNumberProvider for MockBlockNumberProvider {119 type BlockNumber = u64;120121 fn current_block_number() -> Self::BlockNumber {122 Self::get()123 }124}125126impl pallet_inflation::Config for Test {127 type Currency = Balances;128 type TreasuryAccountId = TreasuryAccountId;129 type InflationBlockInterval = InflationBlockInterval;130 type BlockNumberProvider = MockBlockNumberProvider;131}132133pub fn new_test_ext() -> sp_io::TestExternalities {134 frame_system::GenesisConfig::default()135 .build_storage::<Test>()136 .unwrap()137 .into()138}139140macro_rules! block_inflation {141 142 () => {143 144 <pallet_inflation::BlockInflation<Test>>::get()145 };146}147148#[test]149fn uninitialized_inflation() {150 new_test_ext().execute_with(|| {151 let initial_issuance: u64 = 1_000_000_000;152 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);153 assert_eq!(Balances::free_balance(1234), initial_issuance);154155 156 157 MockBlockNumberProvider::set(1);158159 assert_eq!(block_inflation!(), 0);160 });161}162163#[test]164fn inflation_works() {165 new_test_ext().execute_with(|| {166 167 let initial_issuance: u64 = 1_000_000_000;168 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);169 assert_eq!(Balances::free_balance(1234), initial_issuance);170171 172 173 MockBlockNumberProvider::set(1);174175 176 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));177 assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);178 assert_eq!(179 Balances::free_balance(1234) - initial_issuance,180 block_inflation!()181 );182183 184 MockBlockNumberProvider::set(102);185 Inflation::on_initialize(0);186 assert_eq!(187 Balances::free_balance(1234) - initial_issuance,188 2 * block_inflation!()189 );190 });191}192193#[test]194fn inflation_second_deposit() {195 new_test_ext().execute_with(|| {196 197 let initial_issuance: u64 = 1_000_000_000;198 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);199 assert_eq!(Balances::free_balance(1234), initial_issuance);200 MockBlockNumberProvider::set(1);201202 203 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));204205 206 let mut block: u64 = 2;207 let balance_before: u64 = Balances::free_balance(1234);208 while block < <pallet_inflation::NextInflationBlock<Test>>::get() {209 MockBlockNumberProvider::set(block as u64);210 Inflation::on_initialize(0);211 block += 1;212 }213 let balance_just_before: u64 = Balances::free_balance(1234);214 assert_eq!(balance_before, balance_just_before);215216 217 MockBlockNumberProvider::set(block as u64);218 Inflation::on_initialize(0);219 let balance_after: u64 = Balances::free_balance(1234);220 assert_eq!(balance_after - balance_just_before, block_inflation!());221 });222}223224#[test]225fn inflation_in_1_year() {226 new_test_ext().execute_with(|| {227 228 let initial_issuance: u64 = 1_000_000_000;229 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);230 assert_eq!(Balances::free_balance(1234), initial_issuance);231 MockBlockNumberProvider::set(1);232233 234 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));235236 237 238 239 for block in (101..YEAR).step_by(100) {240 MockBlockNumberProvider::set(block);241 Inflation::on_initialize(0);242 }243 assert_eq!(244 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),245 <Balances as Inspect<_>>::total_issuance()246 );247248 MockBlockNumberProvider::set(YEAR + 1);249 Inflation::on_initialize(0);250 let block_inflation_year_2 = block_inflation!();251 252 let expecter_year_2_inflation: u64 = (initial_issuance253 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)254 * 933 * 100 / (10000 * YEAR);255 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); 256 });257}258259#[test]260fn inflation_start_large_kusama_block() {261 new_test_ext().execute_with(|| {262 263 let initial_issuance: u64 = 1_000_000_000;264 let start_block: u64 = 10457457;265 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);266 assert_eq!(Balances::free_balance(1234), initial_issuance);267 MockBlockNumberProvider::set(start_block);268269 270 assert_ok!(Inflation::start_inflation(271 RawOrigin::Root.into(),272 start_block273 ));274275 276 277 278 for block in (101..YEAR).step_by(100) {279 MockBlockNumberProvider::set(start_block + block);280 Inflation::on_initialize(0);281 }282 assert_eq!(283 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),284 <Balances as Inspect<_>>::total_issuance()285 );286287 MockBlockNumberProvider::set(start_block + YEAR + 1);288 Inflation::on_initialize(0);289 let block_inflation_year_2 = block_inflation!();290 291 let expecter_year_2_inflation: u64 = (initial_issuance292 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)293 * 933 * 100 / (10000 * YEAR);294 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); 295 });296}297298#[test]299fn inflation_after_year_10_is_flat() {300 new_test_ext().execute_with(|| {301 302 let initial_issuance: u64 = 1_000_000_000;303 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);304 assert_eq!(Balances::free_balance(1234), initial_issuance);305 MockBlockNumberProvider::set(YEAR * 9 + 1);306307 308 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));309310 311 for _year in 1..=9 {312 Inflation::on_initialize(0);313 }314315 for year in 10..=20 {316 let block_inflation_year_before = block_inflation!();317 MockBlockNumberProvider::set(YEAR * year + 1);318 Inflation::on_initialize(0);319 let block_inflation_year_after = block_inflation!();320321 322 assert_eq!(block_inflation_year_before, block_inflation_year_after);323 }324 });325}326327#[test]328fn inflation_rate_by_year() {329 new_test_ext().execute_with(|| {330 let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;331332 333 334 let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];335336 337 let initial_issuance: u64 = payout_by_year[0] * payouts * 10;338 let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);339 assert_eq!(Balances::free_balance(1234), initial_issuance);340341 342 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));343344 for year in 0..=10 {345 346 MockBlockNumberProvider::set(YEAR * year + 1);347 Inflation::on_initialize(0);348 let mut actual_payout = block_inflation!();349 assert_eq!(actual_payout, payout_by_year[year as usize]);350351 352 MockBlockNumberProvider::set(YEAR * year + 2);353 Inflation::on_initialize(0);354 actual_payout = block_inflation!();355 assert_eq!(actual_payout, payout_by_year[year as usize]);356357 358 MockBlockNumberProvider::set(year * YEAR + YEAR / 2);359 Inflation::on_initialize(0);360 actual_payout = block_inflation!();361 assert_eq!(actual_payout, payout_by_year[year as usize]);362363 364 MockBlockNumberProvider::set((year + 1) * YEAR);365 Inflation::on_initialize(0);366 actual_payout = block_inflation!();367 assert_eq!(actual_payout, payout_by_year[year as usize]);368 }369 });370}