git.delta.rocks / unique-network / refs/commits / 94b124f78550

difftreelog

Inflation pallet block provider added. Setted up to relay chain.

str-mv2021-12-02parent: #30c3834.patch.diff
in: master

3 files changed

modifiedpallets/inflation/src/lib.rsdiffbeforeafterboth
3636
37use sp_runtime::{37use sp_runtime::{
38 Perbill,38 Perbill,
39 traits::{Zero},39 traits::{BlockNumberProvider, Zero},
40};40};
41use sp_std::convert::TryInto;41use sp_std::convert::TryInto;
4242
57 type TreasuryAccountId: Get<Self::AccountId>;57 type TreasuryAccountId: Get<Self::AccountId>;
58 type InflationBlockInterval: Get<Self::BlockNumber>;58 type InflationBlockInterval: Get<Self::BlockNumber>;
59
60 // The block number provider
61 type BlockNumberProvider: BlockNumberProvider<BlockNumber = Self::BlockNumber>;
59}62}
6063
61decl_storage! {64decl_storage! {
75 {78 {
76 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();79 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();
7780
78 fn on_initialize(now: T::BlockNumber) -> Weight81 fn on_initialize() -> Weight
79 {82 {
80 let mut consumed_weight = 0;83 let mut consumed_weight = 0;
81 let mut add_weight = |reads, writes, weight| {84 let mut add_weight = |reads, writes, weight| {
84 };87 };
8588
86 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);89 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);
8790 let _now = T::BlockNumberProvider::current_block_number();
88 // TODO: Rewrite inflation to use block timestamp instead of block number
89 // let _now = <timestamp::Module<T>>::get();
9091
91 // Recalculate inflation on the first block of the year (or if it is not initialized yet)92 // 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 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);94 let current_year: u32 = (_now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);
9495
95 let one_percent = Perbill::from_percent(1);96 let one_percent = Perbill::from_percent(1);
9697
117 }118 }
118119
119 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account120 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account
120 else if (now % T::BlockNumber::from(block_interval)).is_zero() {121 else if (_now % T::BlockNumber::from(block_interval)).is_zero() {
121 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();122 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();
122123
123 add_weight(3, 2, 12_900_000);124 add_weight(3, 2, 12_900_000);
modifiedpallets/inflation/src/tests.rsdiffbeforeafterboth
11};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};
1717
85parameter_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 applied
88 pub static MockBlockNumberProvider: u64 = 0;
88}89}
90
91impl BlockNumberProvider for MockBlockNumberProvider {
92 type BlockNumber = u64;
93
94 fn current_block_number() -> Self::BlockNumber {
95 Self::get()
96 }
97}
8998
90impl 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}
95105
96pub fn new_test_ext() -> sp_io::TestExternalities {106pub fn new_test_ext() -> sp_io::TestExternalities {
110120
111 // BlockInflation should be set after 1st block and121 // BlockInflation should be set after 1st block and
112 // first inflation deposit should be equal to BlockInflation122 // first inflation deposit should be equal to BlockInflation
123 MockBlockNumberProvider::set(1);
113 Inflation::on_initialize(1);124 Inflation::on_initialize(0);
114125
115 // 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 = 3803
116 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);
132144
133 // Next inflation deposit happens when block is multiple of InflationBlockInterval145 // Next inflation deposit happens when block is multiple of InflationBlockInterval
134 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);
142155
143 // The block with inflation156 // The block with inflation
144 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);
161176
162 // 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 accordingly
164 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 );
171187
188 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 = 3904
175 assert_eq!(block_inflation_year_1, 3904);192 assert_eq!(block_inflation_year_1, 3904);
201
184 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);
187206
188 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();
192212
193 // 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);
208229
209 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();
213235
214 // Assert that next year inflation is equal to previous year inflation236 // Assert that next year inflation is equal to previous year inflation
233255
234 for year in 0..=10 {256 for year in 0..=10 {
235 // Year first block257 // Year first block
258 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]);
239262
240 // Year second block263 // Year second block
241 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]);
244268
245 // Year middle block269 // Year middle block
246 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]);
249274
250 // Year last block275 // Year last block
251 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 }
modifiedruntime/src/lib.rsdiffbeforeafterboth
768 type Currency = Balances;768 type Currency = Balances;
769 type TreasuryAccountId = TreasuryAccountId;769 type TreasuryAccountId = TreasuryAccountId;
770 type InflationBlockInterval = InflationBlockInterval;770 type InflationBlockInterval = InflationBlockInterval;
771 type BlockNumberProvider = RelayChainBlockNumberProvider<Runtime>;
771}772}
772773
773// parameter_types! {774// parameter_types! {