difftreelog
fix rust tests
in: master
8 files changed
pallets/collator-selection/Cargo.tomldiffbeforeafterboth--- a/pallets/collator-selection/Cargo.toml
+++ b/pallets/collator-selection/Cargo.toml
@@ -55,9 +55,12 @@
"frame-system/std",
"log/std",
"pallet-authorship/std",
+ 'pallet-aura/std',
+ 'pallet-balances/std',
"pallet-session/std",
"rand/std",
"scale-info/std",
+ "sp-consensus-aura/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
pallets/collator-selection/src/mock.rsdiffbeforeafterboth--- a/pallets/collator-selection/src/mock.rs
+++ b/pallets/collator-selection/src/mock.rs
@@ -102,6 +102,8 @@
parameter_types! {
pub const ExistentialDeposit: u64 = 5;
pub const MaxReserves: u32 = 50;
+ pub const MaxHolds: u32 = 2;
+ pub const MaxFreezes: u32 = 2;
}
impl pallet_balances::Config for Test {
@@ -114,6 +116,10 @@
type MaxLocks = ();
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
+ type HoldIdentifier = [u8; 16];
+ type FreezeIdentifier = [u8; 16];
+ type MaxHolds = MaxHolds;
+ type MaxFreezes = MaxFreezes;
}
pub struct Author4;
@@ -209,6 +215,7 @@
pub const MaxXcmAllowedLocations: u32 = 16;
pub AppPromotionDailyRate: Perbill = Perbill::from_rational(5u32, 10_000);
pub const DayRelayBlocks: u32 = 1;
+ pub const LicenceBondIdentifier: [u8; 16] = *b"licenceidentifie";
}
impl pallet_configuration::Config for Test {
@@ -257,6 +264,7 @@
type ValidatorId = <Self as frame_system::Config>::AccountId;
type ValidatorIdOf = IdentityCollator;
type ValidatorRegistration = IsRegistered;
+ type LicenceBondIdentifier = LicenceBondIdentifier;
type WeightInfo = ();
}
@@ -267,7 +275,9 @@
.unwrap();
let invulnerables = vec![1, 2];
- let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100)];
+ let ed = <Test as pallet_balances::Config>::ExistentialDeposit::get();
+
+ let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100), (33, ed)];
let keys = balances
.iter()
.map(|&(i, _)| {
pallets/collator-selection/src/tests.rsdiffbeforeafterboth--- a/pallets/collator-selection/src/tests.rs
+++ b/pallets/collator-selection/src/tests.rs
@@ -34,16 +34,16 @@
use crate::{mock::*, Error};
use frame_support::{
assert_noop, assert_ok,
- traits::{Currency, GenesisBuild, OnInitialize},
+ traits::{fungible, GenesisBuild, OnInitialize},
};
use frame_system::RawOrigin;
-use pallet_balances::Error as BalancesError;
-use sp_runtime::traits::BadOrigin;
+use sp_runtime::{traits::BadOrigin, TokenError};
use pallet_configuration::{
CollatorSelectionDesiredCollatorsOverride as DesiredCollators,
CollatorSelectionKickThresholdOverride as KickThreshold,
CollatorSelectionLicenseBondOverride as LicenseBond,
};
+use scale_info::prelude::*;
fn get_license_and_onboard(account_id: <Test as frame_system::Config>::AccountId) {
assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(
@@ -226,8 +226,9 @@
#[test]
fn cannot_obtain_license_if_poor() {
new_test_ext().execute_with(|| {
+ let ed = <Test as pallet_balances::Config>::ExistentialDeposit::get();
assert_eq!(Balances::free_balance(&3), 100);
- assert_eq!(Balances::free_balance(&33), 0);
+ assert_eq!(Balances::free_balance(&33), ed);
// works
assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));
@@ -235,7 +236,7 @@
// poor
assert_noop!(
CollatorSelection::get_license(RuntimeOrigin::signed(33)),
- BalancesError::<Test>::InsufficientBalance,
+ TokenError::FundsUnavailable,
);
});
}
@@ -417,10 +418,7 @@
fn authorship_event_handler() {
new_test_ext().execute_with(|| {
// put 100 in the pot + 5 for ED
- <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::set_balance(
- &CollatorSelection::account_id(),
- 105,
- );
+ <Balances as fungible::Mutate<_>>::set_balance(&CollatorSelection::account_id(), 105);
// 4 is the default author.
assert_eq!(Balances::free_balance(4), 100);
@@ -444,10 +442,7 @@
// Nothing panics, no reward when no ED in balance
Authorship::on_initialize(1);
// put some money into the pot at ED
- <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::set_balance(
- &CollatorSelection::account_id(),
- 5,
- );
+ <Balances as fungible::Mutate<_>>::set_balance(&CollatorSelection::account_id(), 5);
// 4 is the default author.
assert_eq!(Balances::free_balance(4), 100);
get_license_and_onboard(4);
pallets/foreign-assets/Cargo.tomldiffbeforeafterboth--- a/pallets/foreign-assets/Cargo.toml
+++ b/pallets/foreign-assets/Cargo.toml
@@ -42,5 +42,6 @@
"sp-runtime/std",
"sp-std/std",
"up-data-structs/std",
+ "xcm-executor/std"
]
try-runtime = ["frame-support/try-runtime"]
pallets/identity/Cargo.tomldiffbeforeafterboth--- a/pallets/identity/Cargo.toml
+++ b/pallets/identity/Cargo.toml
@@ -46,5 +46,6 @@
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
+ "pallet-balances/std",
]
try-runtime = ["frame-support/try-runtime"]
pallets/identity/src/tests.rsdiffbeforeafterboth--- a/pallets/identity/src/tests.rs
+++ b/pallets/identity/src/tests.rs
@@ -106,6 +106,10 @@
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
+ type HoldIdentifier = ();
+ type FreezeIdentifier = ();
+ type MaxHolds = ();
+ type MaxFreezes = ();
}
parameter_types! {
pallets/inflation/src/lib.rsdiffbeforeafterboth--- a/pallets/inflation/src/lib.rs
+++ b/pallets/inflation/src/lib.rs
@@ -187,12 +187,11 @@
<NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());
// First time deposit - create Treasury account so that we can call deposit_into_existing everywhere else
- let imbalance = T::Currency::deposit(
+ let _ = T::Currency::deposit(
&T::TreasuryAccountId::get(),
<BlockInflation<T>>::get(),
Precision::Exact,
)?;
- debug_assert!(imbalance.peek().is_zero());
}
Ok(())
pallets/inflation/src/tests.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg(test)]18#![allow(clippy::from_over_into)]19use crate as pallet_inflation;2021use frame_support::{22 assert_ok, parameter_types,23 traits::{Currency, OnInitialize, Everything, ConstU32},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; // 6-second blocks37 // const YEAR: u64 = 2_629_800; // 12-second blocks38 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION39const 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}5758frame_support::construct_runtime!(59 pub enum Test where60 Block = Block,61 NodeBlock = Block,62 UncheckedExtrinsic = UncheckedExtrinsic,63 {64 Balances: pallet_balances::{Pallet, Call, Storage},65 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},66 Inflation: pallet_inflation::{Pallet, Call, Storage},67 }68);6970parameter_types! {71 pub const BlockHashCount: u64 = 250;72 pub BlockWeights: frame_system::limits::BlockWeights =73 frame_system::limits::BlockWeights::simple_max(Weight::from_ref_time(1024));74 pub const SS58Prefix: u8 = 42;75}7677impl frame_system::Config for Test {78 type BaseCallFilter = Everything;79 type BlockWeights = ();80 type BlockLength = ();81 type DbWeight = ();82 type RuntimeOrigin = RuntimeOrigin;83 type RuntimeCall = RuntimeCall;84 type Index = u64;85 type BlockNumber = u64;86 type Hash = H256;87 type Hashing = BlakeTwo256;88 type AccountId = u64;89 type Lookup = IdentityLookup<Self::AccountId>;90 type Header = Header;91 type RuntimeEvent = ();92 type BlockHashCount = BlockHashCount;93 type Version = ();94 type PalletInfo = PalletInfo;95 type AccountData = pallet_balances::AccountData<u64>;96 type OnNewAccount = ();97 type OnKilledAccount = ();98 type SystemWeightInfo = ();99 type SS58Prefix = SS58Prefix;100 type OnSetCode = ();101 type MaxConsumers = ConstU32<16>;102}103104parameter_types! {105 pub TreasuryAccountId: u64 = 1234;106 pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied107 pub static MockBlockNumberProvider: u64 = 0;108}109110impl BlockNumberProvider for MockBlockNumberProvider {111 type BlockNumber = u64;112113 fn current_block_number() -> Self::BlockNumber {114 Self::get()115 }116}117118impl pallet_inflation::Config for Test {119 type Currency = Balances;120 type TreasuryAccountId = TreasuryAccountId;121 type InflationBlockInterval = InflationBlockInterval;122 type BlockNumberProvider = MockBlockNumberProvider;123}124125pub fn new_test_ext() -> sp_io::TestExternalities {126 frame_system::GenesisConfig::default()127 .build_storage::<Test>()128 .unwrap()129 .into()130}131132macro_rules! block_inflation {133 // Block inflation doesn't have any argumets134 () => {135 // Return BlockInflation state variable current value136 <pallet_inflation::BlockInflation<Test>>::get()137 };138}139140#[test]141fn uninitialized_inflation() {142 new_test_ext().execute_with(|| {143 let initial_issuance: u64 = 1_000_000_000;144 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);145 assert_eq!(Balances::free_balance(1234), initial_issuance);146147 // BlockInflation should be set after inflation is started148 // first inflation deposit should be equal to BlockInflation149 MockBlockNumberProvider::set(1);150151 assert_eq!(block_inflation!(), 0);152 });153}154155#[test]156fn inflation_works() {157 new_test_ext().execute_with(|| {158 // Total issuance = 1_000_000_000159 let initial_issuance: u64 = 1_000_000_000;160 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);161 assert_eq!(Balances::free_balance(1234), initial_issuance);162163 // BlockInflation should be set after inflation is started164 // first inflation deposit should be equal to BlockInflation165 MockBlockNumberProvider::set(1);166167 // Start inflation as sudo168 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));169 assert_eq!(block_inflation!(), FIRST_YEAR_BLOCK_INFLATION);170 assert_eq!(171 Balances::free_balance(1234) - initial_issuance,172 block_inflation!()173 );174175 // Trigger inflation176 MockBlockNumberProvider::set(102);177 Inflation::on_initialize(0);178 assert_eq!(179 Balances::free_balance(1234) - initial_issuance,180 2 * block_inflation!()181 );182 });183}184185#[test]186fn inflation_second_deposit() {187 new_test_ext().execute_with(|| {188 // Total issuance = 1_000_000_000189 let initial_issuance: u64 = 1_000_000_000;190 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);191 assert_eq!(Balances::free_balance(1234), initial_issuance);192 MockBlockNumberProvider::set(1);193194 // Start inflation as sudo195 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));196197 // Next inflation deposit happens when block is greater then or equal to NextInflationBlock198 let mut block: u64 = 2;199 let balance_before: u64 = Balances::free_balance(1234);200 while block < <pallet_inflation::NextInflationBlock<Test>>::get() {201 MockBlockNumberProvider::set(block as u64);202 Inflation::on_initialize(0);203 block += 1;204 }205 let balance_just_before: u64 = Balances::free_balance(1234);206 assert_eq!(balance_before, balance_just_before);207208 // The block with inflation209 MockBlockNumberProvider::set(block as u64);210 Inflation::on_initialize(0);211 let balance_after: u64 = Balances::free_balance(1234);212 assert_eq!(balance_after - balance_just_before, block_inflation!());213 });214}215216#[test]217fn inflation_in_1_year() {218 new_test_ext().execute_with(|| {219 // Total issuance = 1_000_000_000220 let initial_issuance: u64 = 1_000_000_000;221 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);222 assert_eq!(Balances::free_balance(1234), initial_issuance);223 MockBlockNumberProvider::set(1);224225 // Start inflation as sudo226 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));227228 // Go through all the block inflations for year 1,229 // total issuance will be updated accordingly230 // Inflation is set to start in block 1, so first iteration is block 101231 for block in (101..YEAR).step_by(100) {232 MockBlockNumberProvider::set(block);233 Inflation::on_initialize(0);234 }235 assert_eq!(236 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),237 <Balances as Currency<_>>::total_issuance()238 );239240 MockBlockNumberProvider::set(YEAR + 1);241 Inflation::on_initialize(0);242 let block_inflation_year_2 = block_inflation!();243 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951244 let expecter_year_2_inflation: u64 = (initial_issuance245 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)246 * 933 * 100 / (10000 * YEAR);247 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality248 });249}250251#[test]252fn inflation_start_large_kusama_block() {253 new_test_ext().execute_with(|| {254 // Total issuance = 1_000_000_000255 let initial_issuance: u64 = 1_000_000_000;256 let start_block: u64 = 10457457;257 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);258 assert_eq!(Balances::free_balance(1234), initial_issuance);259 MockBlockNumberProvider::set(start_block);260261 // Start inflation as sudo262 assert_ok!(Inflation::start_inflation(263 RawOrigin::Root.into(),264 start_block265 ));266267 // Go through all the block inflations for year 1,268 // total issuance will be updated accordingly269 // Inflation is set to start in block 1, so first iteration is block 101270 for block in (101..YEAR).step_by(100) {271 MockBlockNumberProvider::set(start_block + block);272 Inflation::on_initialize(0);273 }274 assert_eq!(275 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),276 <Balances as Currency<_>>::total_issuance()277 );278279 MockBlockNumberProvider::set(start_block + YEAR + 1);280 Inflation::on_initialize(0);281 let block_inflation_year_2 = block_inflation!();282 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951283 let expecter_year_2_inflation: u64 = (initial_issuance284 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)285 * 933 * 100 / (10000 * YEAR);286 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality287 });288}289290#[test]291fn inflation_after_year_10_is_flat() {292 new_test_ext().execute_with(|| {293 // Total issuance = 1_000_000_000294 let initial_issuance: u64 = 1_000_000_000;295 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);296 assert_eq!(Balances::free_balance(1234), initial_issuance);297 MockBlockNumberProvider::set(YEAR * 9 + 1);298299 // Start inflation as sudo300 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));301302 // Let inflation catch up303 for _year in 1..=9 {304 Inflation::on_initialize(0);305 }306307 for year in 10..=20 {308 let block_inflation_year_before = block_inflation!();309 MockBlockNumberProvider::set(YEAR * year + 1);310 Inflation::on_initialize(0);311 let block_inflation_year_after = block_inflation!();312313 // Assert that next year inflation is equal to previous year inflation314 assert_eq!(block_inflation_year_before, block_inflation_year_after);315 }316 });317}318319#[test]320fn inflation_rate_by_year() {321 new_test_ext().execute_with(|| {322 let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;323324 // Inflation starts at 10% and does down by 2/3% every year until year 9 (included),325 // then it is flat.326 let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];327328 // For accuracy total issuance = payout0 * payouts * 10;329 let initial_issuance: u64 = payout_by_year[0] * payouts * 10;330 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);331 assert_eq!(Balances::free_balance(1234), initial_issuance);332333 // Start inflation as sudo334 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));335336 for year in 0..=10 {337 // Year first block338 MockBlockNumberProvider::set(YEAR * year + 1);339 Inflation::on_initialize(0);340 let mut actual_payout = block_inflation!();341 assert_eq!(actual_payout, payout_by_year[year as usize]);342343 // Year second block344 MockBlockNumberProvider::set(YEAR * year + 2);345 Inflation::on_initialize(0);346 actual_payout = block_inflation!();347 assert_eq!(actual_payout, payout_by_year[year as usize]);348349 // Year middle block350 MockBlockNumberProvider::set(year * YEAR + YEAR / 2);351 Inflation::on_initialize(0);352 actual_payout = block_inflation!();353 assert_eq!(actual_payout, payout_by_year[year as usize]);354355 // Year last block356 MockBlockNumberProvider::set((year + 1) * YEAR);357 Inflation::on_initialize(0);358 actual_payout = block_inflation!();359 assert_eq!(actual_payout, payout_by_year[year as usize]);360 }361 });362}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![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; // 6-second blocks37 // const YEAR: u64 = 2_629_800; // 12-second blocks38 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION39const 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; // every time per how many blocks inflation is applied111 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 // Block inflation doesn't have any argumets138 () => {139 // Return BlockInflation state variable current value140 <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 // BlockInflation should be set after inflation is started152 // first inflation deposit should be equal to BlockInflation153 MockBlockNumberProvider::set(1);154155 assert_eq!(block_inflation!(), 0);156 });157}158159#[test]160fn inflation_works() {161 new_test_ext().execute_with(|| {162 // Total issuance = 1_000_000_000163 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 // BlockInflation should be set after inflation is started168 // first inflation deposit should be equal to BlockInflation169 MockBlockNumberProvider::set(1);170171 // Start inflation as sudo172 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 // Trigger inflation180 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 // Total issuance = 1_000_000_000193 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 // Start inflation as sudo199 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));200201 // Next inflation deposit happens when block is greater then or equal to NextInflationBlock202 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 // The block with inflation213 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 // Total issuance = 1_000_000_000224 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 // Start inflation as sudo230 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));231232 // Go through all the block inflations for year 1,233 // total issuance will be updated accordingly234 // Inflation is set to start in block 1, so first iteration is block 101235 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 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951248 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); // divide by 10 for approx. equality252 });253}254255#[test]256fn inflation_start_large_kusama_block() {257 new_test_ext().execute_with(|| {258 // Total issuance = 1_000_000_000259 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 // Start inflation as sudo266 assert_ok!(Inflation::start_inflation(267 RawOrigin::Root.into(),268 start_block269 ));270271 // Go through all the block inflations for year 1,272 // total issuance will be updated accordingly273 // Inflation is set to start in block 1, so first iteration is block 101274 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 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951287 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); // divide by 10 for approx. equality291 });292}293294#[test]295fn inflation_after_year_10_is_flat() {296 new_test_ext().execute_with(|| {297 // Total issuance = 1_000_000_000298 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 // Start inflation as sudo304 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));305306 // Let inflation catch up307 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 // Assert that next year inflation is equal to previous year inflation318 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 // Inflation starts at 10% and does down by 2/3% every year until year 9 (included),329 // then it is flat.330 let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];331332 // For accuracy total issuance = payout0 * payouts * 10;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 // Start inflation as sudo338 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));339340 for year in 0..=10 {341 // Year first block342 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 // Year second block348 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 // Year middle block354 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 // Year last block360 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}