git.delta.rocks / unique-network / refs/commits / ad0de30fbb71

difftreelog

feature(app-promo) added `force_unstake` extrinsic

PraetorP2023-05-30parent: #da0f861.patch.diff
in: master
`on_initialize` now stops unstakes when the Maintainance mode is enabled.

1 file changed

modifiedpallets/app-promotion/src/lib.rsdiffbeforeafterboth
153 #[pallet::constant]153 #[pallet::constant]
154 type Nominal: Get<BalanceOf<Self>>;154 type Nominal: Get<BalanceOf<Self>>;
155
156 /// Maintenance mode status.
157 type IsMaintenanceModeEnabled: Get<bool>;
155158
156 /// Weight information for extrinsics in this pallet.159 /// Weight information for extrinsics in this pallet.
157 type WeightInfo: WeightInfo;160 type WeightInfo: WeightInfo;
288 where291 where
289 <T as frame_system::Config>::BlockNumber: From<u32>,292 <T as frame_system::Config>::BlockNumber: From<u32>,
290 {293 {
294 if T::IsMaintenanceModeEnabled::get() {
295 return T::DbWeight::get().reads_writes(1, 0);
296 }
297
291 let block_pending = PendingUnstake::<T>::take(current_block_number);298 let block_pending = PendingUnstake::<T>::take(current_block_number);
292 let counter = block_pending.len() as u32;299 let counter = block_pending.len() as u32;
846 .into_iter()853 .into_iter()
847 .try_for_each(|s| -> Result<_, DispatchError> {854 .try_for_each(|s| -> Result<_, DispatchError> {
848 if let Some(BalanceLock { amount, .. }) = Self::get_locked_balance(&s) {855 if let Some(BalanceLock { amount, .. }) = Self::get_locked_balance(&s) {
849 if let Some(_) = Self::get_frozen_balance(&s) {856 if Self::get_frozen_balance(&s).is_some() {
850 return Err(Error::<T>::InconsistencyState.into());857 return Err(Error::<T>::InconsistencyState.into());
851 }858 }
852859
865 Ok(())872 Ok(())
866 }873 }
874
875 /// Called for blocks that, for some reason, have not been unstacked
876 ///
877 /// # Permissions
878 ///
879 /// * Sudo
880 ///
881 /// # Arguments
882 ///
883 /// * `origin`: Must be `Signed`.
884 /// * `pending_blocks`: Block numbers that will be processed.
885 #[pallet::call_index(10)]
886 #[pallet::weight(<T as Config>::WeightInfo::on_initialize(PENDING_LIMIT_PER_BLOCK*pending_blocks.len() as u32))]
887 pub fn force_unstake(
888 origin: OriginFor<T>,
889 pending_blocks: Vec<T::BlockNumber>,
890 ) -> DispatchResult {
891 ensure_root(origin)?;
892
893 ensure!(
894 pending_blocks
895 .iter()
896 .all(|b| *b < <frame_system::Pallet<T>>::block_number()),
897 <Error<T>>::NoPermission
898 );
899
900 let mut pendings =
901 Vec::with_capacity(PENDING_LIMIT_PER_BLOCK as usize * pending_blocks.len());
902 pending_blocks
903 .into_iter()
904 .for_each(|b| pendings.append(&mut PendingUnstake::<T>::take(b).into_inner()));
905
906 pendings.into_iter().for_each(|(staker, amount)| {
907 Self::get_frozen_balance(&staker).map(|b| {
908 let new_state = b.checked_sub(&amount).unwrap_or_default();
909 Self::set_freeze_unchecked(&staker, new_state);
910 });
911 });
912
913 Ok(())
914 }
867 }915 }
868}916}
869917
1158 }1206 }
11591207
1160 fn get_next_calculated_key() -> Option<Vec<u8>> {1208 fn get_next_calculated_key() -> Option<Vec<u8>> {
1161 Self::get_next_calculated_record().map(|key| Staked::<T>::hashed_key_for(key))1209 Self::get_next_calculated_record().map(Staked::<T>::hashed_key_for)
1162 }1210 }
1163}1211}
11641212