git.delta.rocks / unique-network / refs/commits / 2c4dad5597c4

difftreelog

Fix inflation for large kusama start block

Greg Zaitsev2021-12-10parent: #737a762.patch.diff
in: master

2 files changed

modifiedpallets/inflation/src/lib.rsdiffbeforeafterboth
--- a/pallets/inflation/src/lib.rs
+++ b/pallets/inflation/src/lib.rs
@@ -75,6 +75,11 @@
 	pub type NextRecalculationBlock<T: Config> =
 		StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;
 
+	/// Relay block when inflation has started
+	#[pallet::storage]
+	pub type StartBlock<T: Config> =
+		StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;
+
 	#[pallet::hooks]
 	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
 		fn on_initialize(_: T::BlockNumber) -> Weight
@@ -145,8 +150,10 @@
 			ensure_root(origin)?;
 
 			// Start inflation if it has not been yet initialized
-			let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();
-			if next_inflation == 0u32.into() {
+			if <StartBlock<T>>::get() == 0u32.into() {
+				// Set inflation global start block
+				<StartBlock<T>>::set(inflation_start_relay_block);
+
 				// Recalculate inflation. This can be backdated and will catch up.
 				Self::recalculate_inflation(inflation_start_relay_block);
 				let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);
@@ -166,7 +173,7 @@
 
 impl<T: Config> Pallet<T> {
 	pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {
-		let current_year: u32 = (recalculation_block / T::BlockNumber::from(YEAR))
+		let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get()) / T::BlockNumber::from(YEAR))
 			.try_into()
 			.unwrap_or(0);
 		let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);
modifiedpallets/inflation/src/tests.rsdiffbeforeafterboth
230 });230 });
231}231}
232
233#[test]
234fn inflation_start_large_kusama_block() {
235 new_test_ext().execute_with(|| {
236 // Total issuance = 1_000_000_000
237 let initial_issuance: u64 = 1_000_000_000;
238 let start_block: u64 = 10457457;
239 let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
240 assert_eq!(Balances::free_balance(1234), initial_issuance);
241 MockBlockNumberProvider::set(start_block);
242
243 // Start inflation as sudo
244 assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), start_block));
245
246 // Go through all the block inflations for year 1,
247 // total issuance will be updated accordingly
248 // Inflation is set to start in block 1, so first iteration is block 101
249 for block in (101..YEAR).step_by(100) {
250 MockBlockNumberProvider::set(start_block + block);
251 Inflation::on_initialize(0);
252 }
253 assert_eq!(
254 initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),
255 <Balances as Currency<_>>::total_issuance()
256 );
257
258 MockBlockNumberProvider::set(start_block + YEAR + 1);
259 Inflation::on_initialize(0);
260 let block_inflation_year_2 = block_inflation!();
261 // Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951
262 let expecter_year_2_inflation: u64 = (initial_issuance
263 + FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)
264 * 933 * 100 / (10000 * YEAR);
265 assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality
266 });
267}
232268
233#[test]269#[test]
234fn inflation_after_year_10_is_flat() {270fn inflation_after_year_10_is_flat() {