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
75 pub type NextRecalculationBlock<T: Config> =75 pub type NextRecalculationBlock<T: Config> =
76 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;76 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;
77
78 /// Relay block when inflation has started
79 #[pallet::storage]
80 pub type StartBlock<T: Config> =
81 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;
7782
78 #[pallet::hooks]83 #[pallet::hooks]
79 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {84 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
145 ensure_root(origin)?;150 ensure_root(origin)?;
146151
147 // Start inflation if it has not been yet initialized152 // Start inflation if it has not been yet initialized
148 let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();
149 if next_inflation == 0u32.into() {153 if <StartBlock<T>>::get() == 0u32.into() {
154 // Set inflation global start block
155 <StartBlock<T>>::set(inflation_start_relay_block);
156
150 // Recalculate inflation. This can be backdated and will catch up.157 // Recalculate inflation. This can be backdated and will catch up.
151 Self::recalculate_inflation(inflation_start_relay_block);158 Self::recalculate_inflation(inflation_start_relay_block);
166173
167impl<T: Config> Pallet<T> {174impl<T: Config> Pallet<T> {
168 pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {175 pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {
169 let current_year: u32 = (recalculation_block / T::BlockNumber::from(YEAR))176 let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get()) / T::BlockNumber::from(YEAR))
170 .try_into()177 .try_into()
171 .unwrap_or(0);178 .unwrap_or(0);
172 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);179 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);
modifiedpallets/inflation/src/tests.rsdiffbeforeafterboth
--- a/pallets/inflation/src/tests.rs
+++ b/pallets/inflation/src/tests.rs
@@ -231,6 +231,42 @@
 }
 
 #[test]
+fn inflation_start_large_kusama_block() {
+	new_test_ext().execute_with(|| {
+		// Total issuance = 1_000_000_000
+		let initial_issuance: u64 = 1_000_000_000;
+		let start_block: u64 = 10457457;
+		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		assert_eq!(Balances::free_balance(1234), initial_issuance);
+		MockBlockNumberProvider::set(start_block);
+
+		// Start inflation as sudo
+		assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), start_block));
+
+		// Go through all the block inflations for year 1,
+		// total issuance will be updated accordingly
+		// Inflation is set to start in block 1, so first iteration is block 101
+		for block in (101..YEAR).step_by(100) {
+			MockBlockNumberProvider::set(start_block + block);
+			Inflation::on_initialize(0);
+		}
+		assert_eq!(
+			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),
+			<Balances as Currency<_>>::total_issuance()
+		);
+
+		MockBlockNumberProvider::set(start_block + YEAR + 1);
+		Inflation::on_initialize(0);
+		let block_inflation_year_2 = block_inflation!();
+		// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951
+		let expecter_year_2_inflation: u64 = (initial_issuance
+			+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)
+			* 933 * 100 / (10000 * YEAR);
+		assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality
+	});
+}
+
+#[test]
 fn inflation_after_year_10_is_flat() {
 	new_test_ext().execute_with(|| {
 		// Total issuance = 1_000_000_000