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

difftreelog

chore(app-promo) changes based on review

PraetorP2023-05-31parent: #313d931.patch.diff
in: master
Added a comment for code. Change `force_unstake` ext behaviour.

1 file changed

modifiedpallets/app-promotion/src/lib.rsdiffbeforeafterboth
278 pub type PreviousCalculatedRecord<T: Config> =278 pub type PreviousCalculatedRecord<T: Config> =
279 StorageValue<Value = (T::AccountId, T::BlockNumber), QueryKind = OptionQuery>;279 StorageValue<Value = (T::AccountId, T::BlockNumber), QueryKind = OptionQuery>;
280
281 // #[pallet::storage]
282 // pub(crate) type UpgradedToFreezes<T: Config> =
283 // StorageValue<Value = bool, QueryKind = ValueQuery>;
284280
285 #[pallet::hooks]281 #[pallet::hooks]
286 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {282 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
303 Self::get_frozen_balance(&staker).map(|b| {299 Self::get_frozen_balance(&staker).map(|b| {
304 let new_state = b.checked_sub(&amount).unwrap_or_default();300 let new_state = b.checked_sub(&amount).unwrap_or_default();
301
302 // In this case, setting a new state for the frozen funds cannot fail
303 // because the state change goes in the direction of decreasing the frozen funds
304 // and the validity of this transition is ensured by the fact
305 // that we cannot (in the current implementation) unfreeze more funds
306 // than were originally frozen by the pallet. Either way, `on_initialize()` cannot fail.
305 Self::set_freeze_unchecked(&staker, new_state);307 Self::set_freeze_unchecked(&staker, new_state);
306 });308 });
307 });309 });
310 <T as Config>::WeightInfo::on_initialize(counter)312 <T as Config>::WeightInfo::on_initialize(counter)
311 }313 }
312
313 // fn on_runtime_upgrade() -> Weight {
314 // use scale_info::prelude::collections::HashSet;
315 // let mut consumed_weight = Weight::zero();
316 // let mut add_weight = |reads, writes, weight| {
317 // consumed_weight += T::DbWeight::get().reads_writes(reads, writes);
318 // consumed_weight += weight;
319 // };
320
321 // let mut stakes_unstakes = vec![];
322
323 // if <UpgradedToFreezes<T>>::get() {
324 // add_weight(1, 0, Weight::zero());
325 // return consumed_weight;
326 // } else {
327 // add_weight(1, 1, Weight::zero());
328 // <UpgradedToFreezes<T>>::set(true);
329 // }
330 // <Staked<T>>::iter_keys().for_each(|(staker_id, _)| {
331 // add_weight(1, 0, Weight::zero());
332 // stakes_unstakes.push(staker_id);
333 // });
334
335 // <PendingUnstake<T>>::iter().for_each(|(_, v)| {
336 // add_weight(1, 0, Weight::zero());
337 // v.into_iter().for_each(|(staker, _)| {
338 // stakes_unstakes.push(staker);
339 // });
340 // });
341
342 // // filter duplicated id.
343 // stakes_unstakes = stakes_unstakes
344 // .into_iter()
345 // .map(|key| key)
346 // .collect::<HashSet<_>>()
347 // .into_iter()
348 // .collect();
349
350 // stakes_unstakes
351 // .map(|a| (a, <Pallet<T>>::get_locked_balance(&a).amount))
352 // .for_each(|(staker, amount)| {
353 // <<T as Config>::Currency as LockableCurrency<T::AccountId>>::remove_lock(
354 // LOCK_IDENTIFIER,
355 // &staker,
356 // );
357 // <<T as Config>::Currency as MutateFreeze<T::AccountId>>::set_freeze(
358 // &<T as Config>::FreezeIdentifier::get(),
359 // &staker,
360 // amount,
361 // );
362 // add_weight(1, 2, Weight::zero())
363 // });
364
365 // consumed_weight
366 // }
367
368 // #[cfg(feature = "try-runtime")]
369 // fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
370 // use sp_std::collections::btree_map::BTreeMap;
371 // if <UpgradedToFreezes<T>>::get() {
372 // return Ok(Default::default());
373 // }
374 // // Staker -> (total (stakes and unstakes) locked by promotion);
375 // let mut pre_state: BTreeMap<T::AccountId, BalanceOf<T>> = BTreeMap::new();
376
377 // <Staked<T>>::iter().for_each(|((staker, _), (amount, _))| {
378 // if let Some(locked_balance) = pre_state.get_mut(&staker) {
379 // *locked_balance += amount;
380 // } else {
381 // pre_state.insert(staker, amount);
382 // }
383 // });
384
385 // <PendingUnstake<T>>::iter().for_each(|(_, v)| {
386 // v.into_iter().for_each(|(staker, amount)| {
387 // if let Some(locked_balance) = pre_state.get_mut(&staker) {
388 // *locked_balance += amount;
389 // } else {
390 // pre_state.insert(staker, amount);
391 // }
392 // })
393 // });
394
395 // Ok(pre_state.encode())
396 // }
397
398 // #[cfg(feature = "try-runtime")]
399 // fn post_upgrade(pre_state: Vec<u8>) -> Result<(), &'static str> {
400 // use sp_std::collections::btree_map::BTreeMap;
401
402 // if <UpgradedToFreezes<T>>::get() {
403 // return Ok(());
404 // }
405
406 // let mut is_ok = true;
407
408 // let pre_state: BTreeMap<T::AccountId, BalanceOf<T>> =
409 // Decode::decode(&mut &pre_state[..]).map_err(|_| "failed to decode pre_state")?;
410 // for (staker, frozen_by_promo) in pre_state.into_iter() {
411 // let storage_freeze_state = <<T as Config>::Currency as InspectFreeze<
412 // T::AccountId,
413 // >>::balance_frozen(
414 // &<T as Config>::FreezeIdentifier::get(), staker
415 // );
416 // if storage_freeze_state != frozen_by_promo {
417 // is_ok = false;
418 // log::error!(
419 // "Incorrect frozen balance for {:?}. New balance: {:?}. Before runtime upgrade: locked by promo - {:?}",
420 // staker, storage_freeze_state, frozen_by_promo
421 // );
422 // }
423
424 // if !<Pallet<T>>::get_locked_balance(&staker).amount.is_zero() {
425 // is_ok = false;
426 // log::error!(
427 // "Incorrect(non-zero) locked by app promo balance for {:?}",
428 // staker
429 // );
430 // }
431 // }
432
433 // if is_ok {
434 // Ok(())
435 // } else {
436 // Err("Incorrect balance for some of stakers... See logs")
437 // }
438 // }
439 }314 }
440315
441 #[pallet::call]316 #[pallet::call]
903 .into_iter()778 .into_iter()
904 .for_each(|b| pendings.append(&mut PendingUnstake::<T>::take(b).into_inner()));779 .for_each(|b| pendings.append(&mut PendingUnstake::<T>::take(b).into_inner()));
905780
906 pendings.into_iter().for_each(|(staker, amount)| {781 pendings
782 .into_iter()
783 .try_for_each(|(staker, amount)| -> Result<(), DispatchError> {
907 Self::get_frozen_balance(&staker).map(|b| {784 if let Some(b) = Self::get_frozen_balance(&staker) {
908 let new_state = b.checked_sub(&amount).unwrap_or_default();785 let new_state = b.checked_sub(&amount).unwrap_or_default();
909 Self::set_freeze_unchecked(&staker, new_state);786 Self::set_freeze_with_result(&staker, new_state)?;
910 });787 }
788
789 Ok(())
911 });790 })?;
912791
913 Ok(())792 Ok(())
914 }793 }
1018 Ok(())897 Ok(())
1019 }898 }
1020
1021 /// Adds the balance to locked by the pallet.
1022 ///
1023 /// - `staker`: staker account.
1024 /// - `amount`: amount of added locked funds.
1025 // fn add_lock_balance(staker: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
1026 // Self::get_locked_balance(staker)
1027 // .map_or(<BalanceOf<T>>::default(), |l| l.amount)
1028 // .checked_add(&amount)
1029 // .map(|new_lock| Self::set_lock_unchecked(staker, new_lock))
1030 // .ok_or(ArithmeticError::Overflow.into())
1031 // }
1032899
1033 /// Adds the balance to frozen by the pallet.900 /// Adds the balance to frozen by the pallet.
1034 ///901 ///
1042 .ok_or::<DispatchError>(ArithmeticError::Overflow.into())?909 .ok_or::<DispatchError>(ArithmeticError::Overflow.into())?
1043 }910 }
1044
1045 /// Sets the new state of a balance locked by the pallet.
1046 ///
1047 /// - `staker`: staker account.
1048 /// - `amount`: amount of locked funds.
1049 // fn set_lock_unchecked(staker: &T::AccountId, amount: BalanceOf<T>) {
1050 // if amount.is_zero() {
1051 // <<T as Config>::Currency as LockableCurrency<T::AccountId>>::remove_lock(
1052 // LOCK_IDENTIFIER,
1053 // &staker,
1054 // );
1055 // } else {
1056 // <<T as Config>::Currency as LockableCurrency<T::AccountId>>::set_lock(
1057 // LOCK_IDENTIFIER,
1058 // staker,
1059 // amount,
1060 // WithdrawReasons::all(),
1061 // )
1062 // }
1063 // }
1064911
1065 /// Sets the new state of a balance frozen by the pallet.912 /// Sets the new state of a balance frozen by the pallet.
1066 ///913 ///
1067 /// - `staker`: staker account.914 /// - `staker`: staker account.
1068 /// - `amount`: amount of frozen funds.915 /// - `amount`: amount of frozen funds.
1069 fn set_freeze_unchecked(staker: &T::AccountId, amount: BalanceOf<T>) {916 fn set_freeze_unchecked(staker: &T::AccountId, amount: BalanceOf<T>) {
1070 Self::set_freeze_with_result(staker, amount);917 let _ = Self::set_freeze_with_result(staker, amount);
1071 }918 }
1072919
1073 /// Sets the new state of a balance frozen by the pallet.920 /// Sets the new state of a balance frozen by the pallet.