difftreelog
Inflation pallet block provider added. Setted up to relay chain.
in: master
3 files changed
pallets/inflation/src/lib.rsdiffbeforeafterboth--- a/pallets/inflation/src/lib.rs
+++ b/pallets/inflation/src/lib.rs
@@ -36,7 +36,7 @@
use sp_runtime::{
Perbill,
- traits::{Zero},
+ traits::{BlockNumberProvider, Zero},
};
use sp_std::convert::TryInto;
@@ -56,6 +56,9 @@
type Currency: Currency<Self::AccountId>;
type TreasuryAccountId: Get<Self::AccountId>;
type InflationBlockInterval: Get<Self::BlockNumber>;
+
+ // The block number provider
+ type BlockNumberProvider: BlockNumberProvider<BlockNumber = Self::BlockNumber>;
}
decl_storage! {
@@ -75,7 +78,7 @@
{
const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();
- fn on_initialize(now: T::BlockNumber) -> Weight
+ fn on_initialize() -> Weight
{
let mut consumed_weight = 0;
let mut add_weight = |reads, writes, weight| {
@@ -84,13 +87,11 @@
};
let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);
-
- // TODO: Rewrite inflation to use block timestamp instead of block number
- // let _now = <timestamp::Module<T>>::get();
+ let _now = T::BlockNumberProvider::current_block_number();
// Recalculate inflation on the first block of the year (or if it is not initialized yet)
- if (now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {
- let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);
+ if (_now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {
+ let current_year: u32 = (_now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);
let one_percent = Perbill::from_percent(1);
@@ -117,7 +118,7 @@
}
// Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account
- else if (now % T::BlockNumber::from(block_interval)).is_zero() {
+ else if (_now % T::BlockNumber::from(block_interval)).is_zero() {
T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();
add_weight(3, 2, 12_900_000);
pallets/inflation/src/tests.rsdiffbeforeafterboth11};11};12use sp_core::H256;12use sp_core::H256;13use sp_runtime::{13use sp_runtime::{14 traits::{BlakeTwo256, IdentityLookup},14 traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},15 testing::Header,15 testing::Header,16};16};171785parameter_types! {85parameter_types! {86 pub TreasuryAccountId: u64 = 1234;86 pub TreasuryAccountId: u64 = 1234;87 pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied87 pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied88 pub static MockBlockNumberProvider: u64 = 0;88}89}9091impl BlockNumberProvider for MockBlockNumberProvider {92 type BlockNumber = u64;9394 fn current_block_number() -> Self::BlockNumber {95 Self::get()96 }97}899890impl pallet_inflation::Config for Test {99impl pallet_inflation::Config for Test {91 type Currency = Balances;100 type Currency = Balances;92 type TreasuryAccountId = TreasuryAccountId;101 type TreasuryAccountId = TreasuryAccountId;93 type InflationBlockInterval = InflationBlockInterval;102 type InflationBlockInterval = InflationBlockInterval;103 type BlockNumberProvider = MockBlockNumberProvider;94}104}9510596pub fn new_test_ext() -> sp_io::TestExternalities {106pub fn new_test_ext() -> sp_io::TestExternalities {110120111 // BlockInflation should be set after 1st block and121 // BlockInflation should be set after 1st block and112 // first inflation deposit should be equal to BlockInflation122 // first inflation deposit should be equal to BlockInflation123 MockBlockNumberProvider::set(1);113 Inflation::on_initialize(1);124 Inflation::on_initialize(0);114125115 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = 3803126 // Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = 3803116 assert_eq!(Inflation::block_inflation(), 3803);127 assert_eq!(Inflation::block_inflation(), 3803);128 let initial_issuance: u64 = 1_000_000_000;139 let initial_issuance: u64 = 1_000_000_000;129 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);140 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);130 assert_eq!(Balances::free_balance(1234), initial_issuance);141 assert_eq!(Balances::free_balance(1234), initial_issuance);142 MockBlockNumberProvider::set(1);131 Inflation::on_initialize(1);143 Inflation::on_initialize(0);132144133 // Next inflation deposit happens when block is multiple of InflationBlockInterval145 // Next inflation deposit happens when block is multiple of InflationBlockInterval134 let mut block: u32 = 2;146 let mut block: u32 = 2;135 let balance_before: u64 = Balances::free_balance(1234);147 let balance_before: u64 = Balances::free_balance(1234);136 while block % InflationBlockInterval::get() != 0 {148 while block % InflationBlockInterval::get() != 0 {137 Inflation::on_initialize(block as u64);149 MockBlockNumberProvider::set(block as u64);150 Inflation::on_initialize(0);138 block += 1;151 block += 1;139 }152 }140 let balance_just_before: u64 = Balances::free_balance(1234);153 let balance_just_before: u64 = Balances::free_balance(1234);141 assert_eq!(balance_before, balance_just_before);154 assert_eq!(balance_before, balance_just_before);142155143 // The block with inflation156 // The block with inflation144 Inflation::on_initialize(block as u64);157 MockBlockNumberProvider::set(block as u64);158 Inflation::on_initialize(0);145 let balance_after: u64 = Balances::free_balance(1234);159 let balance_after: u64 = Balances::free_balance(1234);146 assert_eq!(160 assert_eq!(147 balance_after - balance_just_before,161 balance_after - balance_just_before,157 let initial_issuance: u64 = 1_000_000_000;171 let initial_issuance: u64 = 1_000_000_000;158 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);172 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);159 assert_eq!(Balances::free_balance(1234), initial_issuance);173 assert_eq!(Balances::free_balance(1234), initial_issuance);174 MockBlockNumberProvider::set(1);160 Inflation::on_initialize(1);175 Inflation::on_initialize(0);161176162 // Go through all the block inflations for year 1,177 // Go through all the block inflations for year 1,163 // total issuance will be updated accordingly178 // total issuance will be updated accordingly164 for block in (100..YEAR).step_by(100) {179 for block in (100..YEAR).step_by(100) {180 MockBlockNumberProvider::set(block);165 Inflation::on_initialize(block);181 Inflation::on_initialize(0);166 }182 }167 assert_eq!(183 assert_eq!(168 initial_issuance + (3803 * (YEAR / 100)),184 initial_issuance + (3803 * (YEAR / 100)),169 <Balances as Currency<_>>::total_issuance()185 <Balances as Currency<_>>::total_issuance()170 );186 );171187188 MockBlockNumberProvider::set(YEAR);172 Inflation::on_initialize(YEAR);189 Inflation::on_initialize(0);173 let block_inflation_year_1 = Inflation::block_inflation();190 let block_inflation_year_1 = Inflation::block_inflation();174 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR = 3904191 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR = 3904175 assert_eq!(block_inflation_year_1, 3904);192 assert_eq!(block_inflation_year_1, 3904);201184 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);202 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);185 assert_eq!(Balances::free_balance(1234), initial_issuance);203 assert_eq!(Balances::free_balance(1234), initial_issuance);204 MockBlockNumberProvider::set(1);186 Inflation::on_initialize(1);205 Inflation::on_initialize(0);187206188 for year in 1..=9 {207 for year in 1..=9 {189 let block_inflation_year_before = Inflation::block_inflation();208 let block_inflation_year_before = Inflation::block_inflation();190 Inflation::on_initialize(YEAR * year);209 MockBlockNumberProvider::set(YEAR * year);210 Inflation::on_initialize(0);191 let block_inflation_year_after = Inflation::block_inflation();211 let block_inflation_year_after = Inflation::block_inflation();192212193 // SBP M2 review: this is actually not true (not for the first few years)213 // SBP M2 review: this is actually not true (not for the first few years)204 let initial_issuance: u64 = 1_000_000_000;224 let initial_issuance: u64 = 1_000_000_000;205 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);225 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);206 assert_eq!(Balances::free_balance(1234), initial_issuance);226 assert_eq!(Balances::free_balance(1234), initial_issuance);207 Inflation::on_initialize(YEAR * 9);227 MockBlockNumberProvider::set(YEAR * 9);228 Inflation::on_initialize(0);208229209 for year in 10..=20 {230 for year in 10..=20 {210 let block_inflation_year_before = Inflation::block_inflation();231 let block_inflation_year_before = Inflation::block_inflation();211 Inflation::on_initialize(YEAR * year);232 MockBlockNumberProvider::set(YEAR * year);233 Inflation::on_initialize(0);212 let block_inflation_year_after = Inflation::block_inflation();234 let block_inflation_year_after = Inflation::block_inflation();213235214 // Assert that next year inflation is equal to previous year inflation236 // Assert that next year inflation is equal to previous year inflation233255234 for year in 0..=10 {256 for year in 0..=10 {235 // Year first block257 // Year first block258 MockBlockNumberProvider::set(YEAR * year);236 Inflation::on_initialize(year * YEAR);259 Inflation::on_initialize(0);237 let mut actual_payout = Inflation::block_inflation();260 let mut actual_payout = Inflation::block_inflation();238 assert_eq!(actual_payout, payout_by_year[year as usize]);261 assert_eq!(actual_payout, payout_by_year[year as usize]);239262240 // Year second block263 // Year second block241 Inflation::on_initialize(year * YEAR + 1);264 MockBlockNumberProvider::set(YEAR * year + 1);265 Inflation::on_initialize(0);242 actual_payout = Inflation::block_inflation();266 actual_payout = Inflation::block_inflation();243 assert_eq!(actual_payout, payout_by_year[year as usize]);267 assert_eq!(actual_payout, payout_by_year[year as usize]);244268245 // Year middle block269 // Year middle block246 Inflation::on_initialize(year * YEAR + YEAR / 2);270 MockBlockNumberProvider::set(year * YEAR + YEAR / 2);271 Inflation::on_initialize(0);247 actual_payout = Inflation::block_inflation();272 actual_payout = Inflation::block_inflation();248 assert_eq!(actual_payout, payout_by_year[year as usize]);273 assert_eq!(actual_payout, payout_by_year[year as usize]);249274250 // Year last block275 // Year last block251 Inflation::on_initialize((year + 1) * YEAR - 1);276 MockBlockNumberProvider::set((year + 1) * YEAR - 1);277 Inflation::on_initialize(0);252 actual_payout = Inflation::block_inflation();278 actual_payout = Inflation::block_inflation();253 assert_eq!(actual_payout, payout_by_year[year as usize]);279 assert_eq!(actual_payout, payout_by_year[year as usize]);254 }280 }runtime/src/lib.rsdiffbeforeafterboth--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -768,6 +768,7 @@
type Currency = Balances;
type TreasuryAccountId = TreasuryAccountId;
type InflationBlockInterval = InflationBlockInterval;
+ type BlockNumberProvider = RelayChainBlockNumberProvider<Runtime>;
}
// parameter_types! {