|
1 | 1 | #![allow(clippy::nonminimal_bool)] |
2 | 2 |
|
3 | | -use frame_support::{ |
4 | | - dispatch::DispatchResultWithPostInfo, pallet_prelude::Get, traits::LockIdentifier, |
5 | | - WeakBoundedVec, |
6 | | -}; |
| 3 | +use frame_support::{dispatch::DispatchResult, traits::LockIdentifier, WeakBoundedVec}; |
7 | 4 | use pallet_balances::BalanceLock; |
8 | 5 | use parity_scale_codec::Encode; |
9 | 6 | use sp_core::hexdisplay::HexDisplay; |
10 | 7 | use sp_runtime::DispatchError; |
11 | 8 |
|
12 | 9 | use crate::{ |
13 | 10 | pallet::{Config, Event, Pallet}, |
14 | | - traits::{AccountInfoProvider, BalancesProvider, NextKeysSessionProvider}, |
| 11 | + traits::{AccountInfoProvider, BalancesProvider, BondedStashProvider, NextKeysSessionProvider}, |
15 | 12 | LOG_TARGET, STAKING_ID, VESTING_ID, |
16 | 13 | }; |
17 | 14 |
|
18 | 15 | impl<T: Config> Pallet<T> { |
19 | 16 | /// Checks if account has an underflow of `consumers` counter. In such case, it increments |
20 | 17 | /// it by one. |
21 | | - pub fn fix_underflow_consumer_counter(who: T::AccountId) -> DispatchResultWithPostInfo { |
22 | | - let mut weight = T::DbWeight::get().reads(1); |
23 | | - let consumers = T::AccountInfoProvider::get_consumers(&who); |
| 18 | + pub fn fix_underflow_consumer_counter(who: T::AccountId) -> DispatchResult { |
| 19 | + let current_consumers = T::AccountInfoProvider::get_consumers(&who); |
| 20 | + let mut expected_consumers: u32 = 0; |
24 | 21 |
|
25 | | - weight += T::DbWeight::get().reads(1); |
26 | | - if Self::no_consumers_some_reserved(&who, consumers) { |
27 | | - Self::increment_consumers(who)?; |
28 | | - weight += T::DbWeight::get().writes(1); |
29 | | - return Ok(Some(weight).into()); |
| 22 | + if Self::reserved_or_frozen_non_zero(&who) { |
| 23 | + expected_consumers += 1; |
| 24 | + } |
| 25 | + let has_vesting_lock = Self::has_vesting_lock(&who); |
| 26 | + let has_staking_lock = Self::has_staking_lock(&who); |
| 27 | + if has_staking_lock || has_vesting_lock { |
| 28 | + expected_consumers += 1; |
| 29 | + if has_staking_lock { |
| 30 | + expected_consumers += 1; |
| 31 | + } |
| 32 | + } |
| 33 | + if Self::has_next_session_keys_and_account_is_controller(&who) { |
| 34 | + expected_consumers += 1; |
30 | 35 | } |
31 | 36 |
|
32 | | - weight += T::DbWeight::get().reads(2); |
33 | | - if Self::staker_has_consumers_underflow(&who, consumers) { |
| 37 | + if current_consumers < expected_consumers { |
| 38 | + log::debug!( |
| 39 | + target: LOG_TARGET, |
| 40 | + "Account {:?} has current consumers {} less than expected consumers {:?}, incrementing ", |
| 41 | + HexDisplay::from(&who.encode()), current_consumers, expected_consumers); |
34 | 42 | Self::increment_consumers(who)?; |
35 | | - weight += T::DbWeight::get().writes(1); |
36 | | - return Ok(Some(weight).into()); |
| 43 | + } else { |
| 44 | + log::debug!( |
| 45 | + target: LOG_TARGET, |
| 46 | + "Account {:?} does not have consumers underflow, not incrementing", |
| 47 | + HexDisplay::from(&who.encode()) |
| 48 | + ); |
37 | 49 | } |
38 | 50 |
|
39 | | - log::debug!( |
40 | | - target: LOG_TARGET, |
41 | | - "Account {:?} has correct consumer counter, not incrementing", |
42 | | - HexDisplay::from(&who.encode()) |
43 | | - ); |
44 | | - Ok(Some(weight).into()) |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + fn reserved_or_frozen_non_zero(who: &T::AccountId) -> bool { |
| 55 | + !T::BalancesProvider::is_reserved_zero(who) || !T::BalancesProvider::is_frozen_zero(who) |
45 | 56 | } |
46 | 57 |
|
47 | | - fn staker_has_consumers_underflow(who: &T::AccountId, consumers: u32) -> bool { |
| 58 | + fn has_vesting_lock(who: &T::AccountId) -> bool { |
48 | 59 | let locks = T::BalancesProvider::locks(who); |
49 | | - let has_vesting_lock = Self::has_lock(&locks, VESTING_ID); |
50 | | - let vester_has_consumers_underflow = consumers == 1 && has_vesting_lock; |
51 | | - let has_staking_lock = Self::has_lock(&locks, STAKING_ID); |
52 | | - let nominator_has_consumers_underflow = consumers == 2 && has_staking_lock; |
53 | | - let has_next_session_keys = T::NextKeysSessionProvider::has_next_session_keys(who); |
54 | | - let validator_has_consumers_underflow = |
55 | | - consumers == 3 && has_staking_lock && has_next_session_keys; |
56 | | - vester_has_consumers_underflow |
57 | | - || nominator_has_consumers_underflow |
58 | | - || validator_has_consumers_underflow |
| 60 | + Self::has_lock(&locks, VESTING_ID) |
59 | 61 | } |
60 | 62 |
|
61 | | - fn no_consumers_some_reserved(who: &T::AccountId, consumers: u32) -> bool { |
62 | | - let is_reserved_not_zero = T::BalancesProvider::is_reserved_not_zero(who); |
| 63 | + fn has_staking_lock(who: &T::AccountId) -> bool { |
| 64 | + let locks = T::BalancesProvider::locks(who); |
| 65 | + Self::has_lock(&locks, STAKING_ID) |
| 66 | + } |
63 | 67 |
|
64 | | - consumers == 0 && is_reserved_not_zero |
| 68 | + fn has_next_session_keys_and_account_is_controller(who: &T::AccountId) -> bool { |
| 69 | + let has_next_session_keys = T::NextKeysSessionProvider::has_next_session_keys(who); |
| 70 | + let stash_equal_to_controller = match T::BondedStashProvider::get_controller(who) { |
| 71 | + Some(controller) => *who == controller, |
| 72 | + None => false, |
| 73 | + }; |
| 74 | + if has_next_session_keys && stash_equal_to_controller { |
| 75 | + return true; |
| 76 | + } |
| 77 | + match T::BondedStashProvider::get_stash(who) { |
| 78 | + Some(stash) => { |
| 79 | + *who != stash && T::NextKeysSessionProvider::has_next_session_keys(&stash) |
| 80 | + } |
| 81 | + None => false, |
| 82 | + } |
65 | 83 | } |
66 | 84 |
|
67 | 85 | fn has_lock<U, V>(locks: &WeakBoundedVec<BalanceLock<U>, V>, id: LockIdentifier) -> bool { |
|
0 commit comments