difftreelog
Replace spaces with tabs
in: master
2 files changed
pallets/inflation/src/lib.rsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56#![recursion_limit = "1024"]78#![cfg_attr(not(feature = "std"), no_std)]910#[cfg(feature = "std")]11pub use std::*;1213#[cfg(feature = "std")]14pub use serde::*;1516#[cfg(feature = "runtime-benchmarks")]17mod benchmarking;1819#[cfg(test)]20mod tests;2122pub use frame_support::{23 construct_runtime, decl_module, decl_storage,24 ensure,25 traits::{26 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,27 Randomness, IsSubType, WithdrawReasons,28 },29 weights::{30 constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},31 DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,32 WeightToFeePolynomial, DispatchClass,33 },34 StorageValue,35 transactional,36};3738// #[cfg(feature = "runtime-benchmarks")]39pub use frame_support::dispatch::DispatchResult;4041use sp_runtime::{42 Perbill,43 traits::{Zero}44};45use sp_std::convert::TryInto;4647use frame_system::{self as system};4849/// The balance type of this module.50pub type BalanceOf<T> =51 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;5253pub const YEAR: u32 = 5_259_600;54pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;55pub const START_INFLATION_PERCENT: u32 = 10;56pub const END_INFLATION_PERCENT: u32 = 4;5758pub trait Config: system::Config {59 type Currency: Currency<Self::AccountId>;60 type TreasuryAccountId: Get<Self::AccountId>;61 type InflationBlockInterval: Get<Self::BlockNumber>;62}6364decl_storage! {65 trait Store for Module<T: Config> as Inflation {66 /// starting year total issuance67 pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf<T>;6869 /// Current block inflation70 pub BlockInflation get(fn block_inflation): BalanceOf<T>;71 }72}7374decl_module! {75 pub struct Module<T: Config> for enum Call 76 where 77 origin: T::Origin,78 {79 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();8081 fn on_initialize(now: T::BlockNumber) -> Weight 82 {83 let mut consumed_weight = 0;84 let mut add_weight = |reads, writes, weight| {85 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);86 consumed_weight += weight;87 };8889 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);9091 // Recalculate inflation on the first block of the year (or if it is not initialized yet)92 if (now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {93 let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);9495 let one_percent = Perbill::from_percent(1);9697 if current_year <= TOTAL_YEARS_UNTIL_FLAT {98 let amount: BalanceOf<T> = Perbill::from_rational_approximation(99 block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)), 100 YEAR * TOTAL_YEARS_UNTIL_FLAT101 ) * ( one_percent * T::Currency::total_issuance() );102 <BlockInflation<T>>::put(amount);103 }104 else {105 let amount: BalanceOf<T> = Perbill::from_rational_approximation(106 block_interval * END_INFLATION_PERCENT, 107 YEAR108 ) * (one_percent * T::Currency::total_issuance());109 <BlockInflation<T>>::put(amount);110 }111 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());112113 // First time deposit114 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());115116 add_weight(7, 6, 28_300_000);117 }118119 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account120 else if (now % T::BlockNumber::from(block_interval)).is_zero() {121 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();122123 add_weight(3, 2, 12_900_000);124 }125126 consumed_weight127 }128129 }130}pallets/inflation/src/tests.rsdiffbeforeafterboth--- a/pallets/inflation/src/tests.rs
+++ b/pallets/inflation/src/tests.rs
@@ -2,39 +2,39 @@
mod tests {
use crate as pallet_inflation;
- use frame_system;
+ use frame_system;
use frame_support::{traits::{Currency}, parameter_types};
- use frame_support::{traits::OnInitialize};
+ use frame_support::{traits::OnInitialize};
use sp_core::H256;
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
- const YEAR: u64 = 5_259_600;
+ const YEAR: u64 = 5_259_600;
- parameter_types! {
- pub const ExistentialDeposit: u64 = 1;
- pub const MaxLocks: u32 = 50;
- }
-
- impl pallet_balances::Config for Test {
- type AccountStore = System;
- type Balance = u64;
- type DustRemoval = ();
- type Event = ();
- type ExistentialDeposit = ExistentialDeposit;
- type WeightInfo = ();
- type MaxLocks = MaxLocks;
- }
-
+ parameter_types! {
+ pub const ExistentialDeposit: u64 = 1;
+ pub const MaxLocks: u32 = 50;
+ }
+
+ impl pallet_balances::Config for Test {
+ type AccountStore = System;
+ type Balance = u64;
+ type DustRemoval = ();
+ type Event = ();
+ type ExistentialDeposit = ExistentialDeposit;
+ type WeightInfo = ();
+ type MaxLocks = MaxLocks;
+ }
+
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
- Balances: pallet_balances::{Module, Call, Storage},
+ Balances: pallet_balances::{Module, Call, Storage},
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Inflation: pallet_inflation::{Module, Call, Storage},
}
@@ -44,198 +44,198 @@
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(1024);
- pub const SS58Prefix: u8 = 42;
- }
+ pub const SS58Prefix: u8 = 42;
+ }
- impl frame_system::Config for Test {
- type BaseCallFilter = ();
- type BlockWeights = ();
- type BlockLength = ();
- type DbWeight = ();
- type Origin = Origin;
- type Call = Call;
- type Index = u64;
- type BlockNumber = u64;
- type Hash = H256;
- type Hashing = BlakeTwo256;
- type AccountId = u64;
- type Lookup = IdentityLookup<Self::AccountId>;
- type Header = Header;
- type Event = ();
- type BlockHashCount = BlockHashCount;
- type Version = ();
- type PalletInfo = PalletInfo;
- type AccountData = pallet_balances::AccountData<u64>;
- type OnNewAccount = ();
- type OnKilledAccount = ();
- type SystemWeightInfo = ();
- type SS58Prefix = SS58Prefix;
- }
+ impl frame_system::Config for Test {
+ type BaseCallFilter = ();
+ type BlockWeights = ();
+ type BlockLength = ();
+ type DbWeight = ();
+ type Origin = Origin;
+ type Call = Call;
+ type Index = u64;
+ type BlockNumber = u64;
+ type Hash = H256;
+ type Hashing = BlakeTwo256;
+ type AccountId = u64;
+ type Lookup = IdentityLookup<Self::AccountId>;
+ type Header = Header;
+ type Event = ();
+ type BlockHashCount = BlockHashCount;
+ type Version = ();
+ type PalletInfo = PalletInfo;
+ type AccountData = pallet_balances::AccountData<u64>;
+ type OnNewAccount = ();
+ type OnKilledAccount = ();
+ type SystemWeightInfo = ();
+ type SS58Prefix = SS58Prefix;
+ }
- parameter_types! {
- pub TreasuryAccountId: u64 = 1234;
- pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied
- }
-
- impl pallet_inflation::Config for Test {
- type Currency = Balances;
- type TreasuryAccountId = TreasuryAccountId;
- type InflationBlockInterval = InflationBlockInterval;
- }
+ parameter_types! {
+ pub TreasuryAccountId: u64 = 1234;
+ pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied
+ }
+
+ impl pallet_inflation::Config for Test {
+ type Currency = Balances;
+ type TreasuryAccountId = TreasuryAccountId;
+ type InflationBlockInterval = InflationBlockInterval;
+ }
- // Build genesis storage according to the mock runtime.
- pub fn new_test_ext() -> sp_io::TestExternalities {
- frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
- }
+ // Build genesis storage according to the mock runtime.
+ pub fn new_test_ext() -> sp_io::TestExternalities {
+ frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
+ }
- #[test]
+ #[test]
fn inflation_works() {
new_test_ext().execute_with(|| {
- // Total issuance = 1_000_000_000
- let initial_issuance: u64 = 1_000_000_000;
- let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
- assert_eq!(Balances::free_balance(1234), initial_issuance);
+ // Total issuance = 1_000_000_000
+ let initial_issuance: u64 = 1_000_000_000;
+ let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+ assert_eq!(Balances::free_balance(1234), initial_issuance);
- // BlockInflation should be set after 1st block and
- // first inflation deposit should be equal to BlockInflation
- Inflation::on_initialize(1);
- assert!(Inflation::block_inflation() > 0);
- assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation());
+ // BlockInflation should be set after 1st block and
+ // first inflation deposit should be equal to BlockInflation
+ Inflation::on_initialize(1);
+ assert!(Inflation::block_inflation() > 0);
+ assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation());
});
}
- #[test]
+ #[test]
fn inflation_second_deposit() {
new_test_ext().execute_with(|| {
- // Total issuance = 1_000_000_000
- let initial_issuance: u64 = 1_000_000_000;
- let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
- assert_eq!(Balances::free_balance(1234), initial_issuance);
+ // Total issuance = 1_000_000_000
+ let initial_issuance: u64 = 1_000_000_000;
+ let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+ assert_eq!(Balances::free_balance(1234), initial_issuance);
Inflation::on_initialize(1);
- // Next inflation deposit happens when block is multiple of InflationBlockInterval
- let mut block: u32 = 2;
- let balance_before: u64 = Balances::free_balance(1234);
- while block % InflationBlockInterval::get() != 0 {
- Inflation::on_initialize(block as u64);
- block += 1;
- }
- let balance_just_before: u64 = Balances::free_balance(1234);
- assert_eq!(balance_before, balance_just_before);
+ // Next inflation deposit happens when block is multiple of InflationBlockInterval
+ let mut block: u32 = 2;
+ let balance_before: u64 = Balances::free_balance(1234);
+ while block % InflationBlockInterval::get() != 0 {
+ Inflation::on_initialize(block as u64);
+ block += 1;
+ }
+ let balance_just_before: u64 = Balances::free_balance(1234);
+ assert_eq!(balance_before, balance_just_before);
- // The block with inflation
- Inflation::on_initialize(block as u64);
- let balance_after: u64 = Balances::free_balance(1234);
- assert_eq!(balance_after - balance_just_before, Inflation::block_inflation());
+ // The block with inflation
+ Inflation::on_initialize(block as u64);
+ let balance_after: u64 = Balances::free_balance(1234);
+ assert_eq!(balance_after - balance_just_before, Inflation::block_inflation());
});
}
- #[test]
+ #[test]
fn inflation_in_1_year() {
new_test_ext().execute_with(|| {
- // Total issuance = 1_000_000_000
- let initial_issuance: u64 = 1_000_000_000;
- let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
- assert_eq!(Balances::free_balance(1234), initial_issuance);
+ // Total issuance = 1_000_000_000
+ let initial_issuance: u64 = 1_000_000_000;
+ let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+ assert_eq!(Balances::free_balance(1234), initial_issuance);
Inflation::on_initialize(1);
- let block_inflation_year_0 = Inflation::block_inflation();
+ let block_inflation_year_0 = Inflation::block_inflation();
- Inflation::on_initialize(YEAR);
- let block_inflation_year_1 = Inflation::block_inflation();
+ Inflation::on_initialize(YEAR);
+ let block_inflation_year_1 = Inflation::block_inflation();
- // Assert that year 1 inflation is less than year 0
- assert!(block_inflation_year_0 > block_inflation_year_1);
+ // Assert that year 1 inflation is less than year 0
+ assert!(block_inflation_year_0 > block_inflation_year_1);
});
}
- #[test]
+ #[test]
fn inflation_in_1_to_9_years() {
new_test_ext().execute_with(|| {
- // Total issuance = 1_000_000_000
- let initial_issuance: u64 = 1_000_000_000;
- let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
- assert_eq!(Balances::free_balance(1234), initial_issuance);
- Inflation::on_initialize(1);
+ // Total issuance = 1_000_000_000
+ let initial_issuance: u64 = 1_000_000_000;
+ let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+ assert_eq!(Balances::free_balance(1234), initial_issuance);
+ Inflation::on_initialize(1);
- for year in 1..=9 {
- let block_inflation_year_before = Inflation::block_inflation();
- Inflation::on_initialize(YEAR * year);
- let block_inflation_year_after = Inflation::block_inflation();
+ for year in 1..=9 {
+ let block_inflation_year_before = Inflation::block_inflation();
+ Inflation::on_initialize(YEAR * year);
+ let block_inflation_year_after = Inflation::block_inflation();
- // Assert that next year inflation is less than previous year inflation
- assert!(block_inflation_year_before > block_inflation_year_after);
- }
+ // Assert that next year inflation is less than previous year inflation
+ assert!(block_inflation_year_before > block_inflation_year_after);
+ }
});
}
- #[test]
+ #[test]
fn inflation_after_year_10_is_flat() {
new_test_ext().execute_with(|| {
- // Total issuance = 1_000_000_000
- let initial_issuance: u64 = 1_000_000_000;
- let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
- assert_eq!(Balances::free_balance(1234), initial_issuance);
- Inflation::on_initialize(YEAR * 9);
+ // Total issuance = 1_000_000_000
+ let initial_issuance: u64 = 1_000_000_000;
+ let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+ assert_eq!(Balances::free_balance(1234), initial_issuance);
+ Inflation::on_initialize(YEAR * 9);
- for year in 10..=20 {
- let block_inflation_year_before = Inflation::block_inflation();
- Inflation::on_initialize(YEAR * year);
- let block_inflation_year_after = Inflation::block_inflation();
+ for year in 10..=20 {
+ let block_inflation_year_before = Inflation::block_inflation();
+ Inflation::on_initialize(YEAR * year);
+ let block_inflation_year_after = Inflation::block_inflation();
- // Assert that next year inflation is equal to previous year inflation
- assert_eq!(block_inflation_year_before, block_inflation_year_after);
- }
+ // Assert that next year inflation is equal to previous year inflation
+ assert_eq!(block_inflation_year_before, block_inflation_year_after);
+ }
});
}
- #[test]
+ #[test]
fn inflation_rate_by_year() {
new_test_ext().execute_with(|| {
- let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;
+ let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;
- // Inflation starts at 10% and does down by 2/3% every year until year 9 (included),
- // then it is flat.
- let payout_by_year: [u64; 11] = [
- 1000,
- 933,
- 867,
- 800,
- 733,
- 667,
- 600,
- 533,
- 467,
- 400,
- 400
- ];
+ // Inflation starts at 10% and does down by 2/3% every year until year 9 (included),
+ // then it is flat.
+ let payout_by_year: [u64; 11] = [
+ 1000,
+ 933,
+ 867,
+ 800,
+ 733,
+ 667,
+ 600,
+ 533,
+ 467,
+ 400,
+ 400
+ ];
- // For accuracy total issuance = payout0 * payouts * 10;
- let initial_issuance: u64 = payout_by_year[0] * payouts * 10;
- let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
- assert_eq!(Balances::free_balance(1234), initial_issuance);
+ // For accuracy total issuance = payout0 * payouts * 10;
+ let initial_issuance: u64 = payout_by_year[0] * payouts * 10;
+ let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+ assert_eq!(Balances::free_balance(1234), initial_issuance);
- for year in 0..=10 {
- // Year first block
- Inflation::on_initialize(year*YEAR);
- let mut actual_payout = Inflation::block_inflation();
- assert_eq!(actual_payout, payout_by_year[year as usize]);
+ for year in 0..=10 {
+ // Year first block
+ Inflation::on_initialize(year*YEAR);
+ let mut actual_payout = Inflation::block_inflation();
+ assert_eq!(actual_payout, payout_by_year[year as usize]);
- // Year second block
- Inflation::on_initialize(year*YEAR+1);
- actual_payout = Inflation::block_inflation();
- assert_eq!(actual_payout, payout_by_year[year as usize]);
+ // Year second block
+ Inflation::on_initialize(year*YEAR+1);
+ actual_payout = Inflation::block_inflation();
+ assert_eq!(actual_payout, payout_by_year[year as usize]);
- // Year middle block
- Inflation::on_initialize(year*YEAR + YEAR/2);
- actual_payout = Inflation::block_inflation();
- assert_eq!(actual_payout, payout_by_year[year as usize]);
+ // Year middle block
+ Inflation::on_initialize(year*YEAR + YEAR/2);
+ actual_payout = Inflation::block_inflation();
+ assert_eq!(actual_payout, payout_by_year[year as usize]);
- // Year last block
- Inflation::on_initialize((year + 1)*YEAR-1);
- actual_payout = Inflation::block_inflation();
- assert_eq!(actual_payout, payout_by_year[year as usize]);
- }
+ // Year last block
+ Inflation::on_initialize((year + 1)*YEAR-1);
+ actual_payout = Inflation::block_inflation();
+ assert_eq!(actual_payout, payout_by_year[year as usize]);
+ }
});
}
}