This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[Enhancement] Convert fast-unstake to use StakingInterface, decouplin… #12424
Merged
paritytech-processbot
merged 48 commits into
master
from
ru/feature/fast-unstake-istaking
Oct 29, 2022
Merged
Changes from 42 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
3a11e89
[Enhancement] Convert fast-unstake to use StakingInterface, decouplin…
ruseinov 4f39589
Update primitives/staking/src/lib.rs
ruseinov 9b8e123
Update primitives/staking/src/lib.rs
ruseinov 25520d6
fix validator comment
ruseinov ad90903
remove todo
ruseinov 89b32b5
ideas from Kian (#12474)
ruseinov f51c073
Merge remote-tracking branch 'origin/master' into ru/feature/fast-uns…
41d04ad
Rename StakingInterface -> Staking for nomination-pools
ruseinov 7fc52f8
Staking fixes
ruseinov d3958ba
StakingInterface changes
ruseinov 2ff689c
fix fast-unstake
ruseinov 1fc6510
fix nomination-pools
ruseinov 28d15d7
Merge remote-tracking branch 'origin/master' into ru/feature/fast-uns…
0705029
Fix fast-unstake tests
ruseinov 39cf580
Fix benches for fast-unstake
ruseinov 8093567
fix is_unbonding
ruseinov 30a5882
fix nomination pools
ruseinov 37107ef
fix node code
ruseinov 097a9d1
add mock comments
ruseinov 613a701
fix imports
ruseinov e785dd5
remove todo
ruseinov 03586d7
more fixes
ruseinov 20a4f29
more fixes
ruseinov 9807cf2
bench fixes
ruseinov 3639af8
more fixes
ruseinov b7827d5
more fixes
ruseinov 5232b99
import fix
ruseinov f881456
more fixes
ruseinov c3cee71
more bench fix
ruseinov 068c565
refix
ruseinov ff36f90
refix
ruseinov 6f862e4
Update primitives/staking/src/lib.rs
ruseinov bff58a4
is_unbonding returns a result
ruseinov bdb525e
fix
ruseinov 4bd57b1
review fixes
ruseinov f6040ec
more review fixes
ruseinov 8c5604d
more fixes
ruseinov 731fab2
more fixes
ruseinov b90082a
Update frame/fast-unstake/src/benchmarking.rs
ruseinov 1eee8a5
remove redundant CurrencyBalance from nom-pools
ruseinov 1f4fad2
remove CB
ruseinov 27530dc
Merge remote-tracking branch 'origin/master' into ru/feature/fast-uns…
90cec9f
rephrase
ruseinov b1560aa
Apply suggestions from code review
kianenigma 75a36bb
Update frame/nomination-pools/src/tests.rs
kianenigma b3e843e
finish damn renamed
kianenigma 963837b
clippy fix
ruseinov cbed2f9
fix
ruseinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,18 +80,16 @@ macro_rules! log { | |
| pub mod pallet { | ||
| use super::*; | ||
| use crate::types::*; | ||
| use frame_election_provider_support::ElectionProviderBase; | ||
| use frame_support::{ | ||
| pallet_prelude::*, | ||
| traits::{Defensive, ReservableCurrency}, | ||
| }; | ||
| use frame_system::{pallet_prelude::*, RawOrigin}; | ||
| use pallet_staking::Pallet as Staking; | ||
| use frame_system::pallet_prelude::*; | ||
| use sp_runtime::{ | ||
| traits::{Saturating, Zero}, | ||
| DispatchResult, | ||
| }; | ||
| use sp_staking::EraIndex; | ||
| use sp_staking::{EraIndex, StakingInterface}; | ||
| use sp_std::{prelude::*, vec::Vec}; | ||
| pub use weights::WeightInfo; | ||
|
|
||
|
|
@@ -101,22 +99,22 @@ pub mod pallet { | |
| pub struct MaxChecking<T: Config>(sp_std::marker::PhantomData<T>); | ||
| impl<T: Config> frame_support::traits::Get<u32> for MaxChecking<T> { | ||
| fn get() -> u32 { | ||
| <T as pallet_staking::Config>::BondingDuration::get() + 1 | ||
| T::Staking::bonding_duration() + 1 | ||
| } | ||
| } | ||
|
|
||
| #[pallet::pallet] | ||
| pub struct Pallet<T>(_); | ||
|
|
||
| #[pallet::config] | ||
| pub trait Config: frame_system::Config + pallet_staking::Config { | ||
| pub trait Config: frame_system::Config { | ||
| /// The overarching event type. | ||
| type RuntimeEvent: From<Event<Self>> | ||
| + IsType<<Self as frame_system::Config>::RuntimeEvent> | ||
| + TryInto<Event<Self>>; | ||
|
|
||
| /// The currency used for deposits. | ||
| type DepositCurrency: ReservableCurrency<Self::AccountId, Balance = BalanceOf<Self>>; | ||
| type Currency: ReservableCurrency<Self::AccountId>; | ||
|
|
||
| /// Deposit to take for unstaking, to make sure we're able to slash the it in order to cover | ||
| /// the costs of resources on unsuccessful unstake. | ||
|
|
@@ -125,6 +123,9 @@ pub mod pallet { | |
| /// The origin that can control this pallet. | ||
| type ControlOrigin: frame_support::traits::EnsureOrigin<Self::RuntimeOrigin>; | ||
|
|
||
| /// The access to staking functionality. | ||
| type Staking: StakingInterface<Balance = BalanceOf<Self>, AccountId = Self::AccountId>; | ||
kianenigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// The weight information of this pallet. | ||
| type WeightInfo: WeightInfo; | ||
| } | ||
|
|
@@ -221,28 +222,25 @@ pub mod pallet { | |
| let ctrl = ensure_signed(origin)?; | ||
|
|
||
| ensure!(ErasToCheckPerBlock::<T>::get() != 0, <Error<T>>::CallNotAllowed); | ||
|
|
||
| let ledger = | ||
| pallet_staking::Ledger::<T>::get(&ctrl).ok_or(Error::<T>::NotController)?; | ||
| ensure!(!Queue::<T>::contains_key(&ledger.stash), Error::<T>::AlreadyQueued); | ||
| let stash_account = | ||
| T::Staking::stash_by_ctrl(&ctrl).map_err(|_| Error::<T>::NotController)?; | ||
| ensure!(!Queue::<T>::contains_key(&stash_account), Error::<T>::AlreadyQueued); | ||
| ensure!( | ||
| Head::<T>::get().map_or(true, |UnstakeRequest { stash, .. }| stash != ledger.stash), | ||
| Head::<T>::get() | ||
| .map_or(true, |UnstakeRequest { stash, .. }| stash_account != stash), | ||
| Error::<T>::AlreadyHead | ||
| ); | ||
| // second part of the && is defensive. | ||
| ensure!( | ||
| ledger.active == ledger.total && ledger.unlocking.is_empty(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need to check for ledger.unlocking.is_empty() ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, we are not exposing the ledger anymore, so the defensive condition has to go. |
||
| Error::<T>::NotFullyBonded | ||
| ); | ||
|
|
||
| ensure!(!T::Staking::is_unbonding(&stash_account)?, Error::<T>::NotFullyBonded); | ||
|
|
||
| // chill and fully unstake. | ||
| Staking::<T>::chill(RawOrigin::Signed(ctrl.clone()).into())?; | ||
| Staking::<T>::unbond(RawOrigin::Signed(ctrl).into(), ledger.total)?; | ||
| T::Staking::chill(&stash_account)?; | ||
| T::Staking::fully_unbond(&stash_account)?; | ||
|
|
||
| T::DepositCurrency::reserve(&ledger.stash, T::Deposit::get())?; | ||
| T::Currency::reserve(&stash_account, T::Deposit::get())?; | ||
|
|
||
| // enqueue them. | ||
| Queue::<T>::insert(ledger.stash, T::Deposit::get()); | ||
| Queue::<T>::insert(stash_account, T::Deposit::get()); | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -259,18 +257,18 @@ pub mod pallet { | |
|
|
||
| ensure!(ErasToCheckPerBlock::<T>::get() != 0, <Error<T>>::CallNotAllowed); | ||
|
|
||
| let stash = pallet_staking::Ledger::<T>::get(&ctrl) | ||
| .map(|l| l.stash) | ||
| .ok_or(Error::<T>::NotController)?; | ||
| ensure!(Queue::<T>::contains_key(&stash), Error::<T>::NotQueued); | ||
| let stash_account = | ||
| T::Staking::stash_by_ctrl(&ctrl).map_err(|_| Error::<T>::NotController)?; | ||
| ensure!(Queue::<T>::contains_key(&stash_account), Error::<T>::NotQueued); | ||
| ensure!( | ||
| Head::<T>::get().map_or(true, |UnstakeRequest { stash, .. }| stash != stash), | ||
| Head::<T>::get() | ||
| .map_or(true, |UnstakeRequest { stash, .. }| stash_account != stash), | ||
| Error::<T>::AlreadyHead | ||
| ); | ||
| let deposit = Queue::<T>::take(stash.clone()); | ||
| let deposit = Queue::<T>::take(stash_account.clone()); | ||
|
|
||
| if let Some(deposit) = deposit.defensive() { | ||
| let remaining = T::DepositCurrency::unreserve(&stash, deposit); | ||
| let remaining = T::Currency::unreserve(&stash_account, deposit); | ||
| if !remaining.is_zero() { | ||
| frame_support::defensive!("`not enough balance to unreserve`"); | ||
| ErasToCheckPerBlock::<T>::put(0); | ||
|
|
@@ -314,7 +312,7 @@ pub mod pallet { | |
|
|
||
| // NOTE: here we're assuming that the number of validators has only ever increased, | ||
| // meaning that the number of exposures to check is either this per era, or less. | ||
| let validator_count = pallet_staking::ValidatorCount::<T>::get(); | ||
| let validator_count = T::Staking::desired_validator_count(); | ||
|
|
||
| // determine the number of eras to check. This is based on both `ErasToCheckPerBlock` | ||
| // and `remaining_weight` passed on to us from the runtime executive. | ||
|
|
@@ -330,8 +328,7 @@ pub mod pallet { | |
| } | ||
| } | ||
|
|
||
| if <<T as pallet_staking::Config>::ElectionProvider as ElectionProviderBase>::ongoing() | ||
| { | ||
| if T::Staking::election_ongoing() { | ||
| // NOTE: we assume `ongoing` does not consume any weight. | ||
| // there is an ongoing election -- we better not do anything. Imagine someone is not | ||
| // exposed anywhere in the last era, and the snapshot for the election is already | ||
|
|
@@ -366,8 +363,8 @@ pub mod pallet { | |
| ); | ||
|
|
||
| // the range that we're allowed to check in this round. | ||
| let current_era = pallet_staking::CurrentEra::<T>::get().unwrap_or_default(); | ||
| let bonding_duration = <T as pallet_staking::Config>::BondingDuration::get(); | ||
| let current_era = T::Staking::current_era(); | ||
| let bonding_duration = T::Staking::bonding_duration(); | ||
| // prune all the old eras that we don't care about. This will help us keep the bound | ||
| // of `checked`. | ||
| checked.retain(|e| *e >= current_era.saturating_sub(bonding_duration)); | ||
|
|
@@ -401,16 +398,9 @@ pub mod pallet { | |
| ); | ||
|
|
||
| if unchecked_eras_to_check.is_empty() { | ||
| // `stash` is not exposed in any era now -- we can let go of them now. | ||
| let num_slashing_spans = Staking::<T>::slashing_spans(&stash).iter().count() as u32; | ||
| let result = T::Staking::force_unstake(stash.clone()); | ||
|
|
||
| let result = pallet_staking::Pallet::<T>::force_unstake( | ||
| RawOrigin::Root.into(), | ||
| stash.clone(), | ||
| num_slashing_spans, | ||
| ); | ||
|
|
||
| let remaining = T::DepositCurrency::unreserve(&stash, deposit); | ||
| let remaining = T::Currency::unreserve(&stash, deposit); | ||
| if !remaining.is_zero() { | ||
| frame_support::defensive!("`not enough balance to unreserve`"); | ||
| ErasToCheckPerBlock::<T>::put(0); | ||
kianenigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -426,7 +416,7 @@ pub mod pallet { | |
| let mut eras_checked = 0u32; | ||
| let is_exposed = unchecked_eras_to_check.iter().any(|e| { | ||
| eras_checked.saturating_inc(); | ||
| Self::is_exposed_in_era(&stash, e) | ||
| T::Staking::is_exposed_in_era(&stash, e) | ||
| }); | ||
|
|
||
| log!( | ||
|
|
@@ -442,7 +432,7 @@ pub mod pallet { | |
| // the last 28 eras, have registered yourself to be unstaked, midway being checked, | ||
| // you are exposed. | ||
| if is_exposed { | ||
| T::DepositCurrency::slash_reserved(&stash, deposit); | ||
| T::Currency::slash_reserved(&stash, deposit); | ||
| log!(info, "slashed {:?} by {:?}", stash, deposit); | ||
| Self::deposit_event(Event::<T>::Slashed { stash, amount: deposit }); | ||
| } else { | ||
|
|
@@ -472,12 +462,5 @@ pub mod pallet { | |
| <T as Config>::WeightInfo::on_idle_check(validator_count * eras_checked) | ||
| } | ||
| } | ||
|
|
||
| /// Checks whether an account `staker` has been exposed in an era. | ||
| fn is_exposed_in_era(staker: &T::AccountId, era: &EraIndex) -> bool { | ||
| pallet_staking::ErasStakers::<T>::iter_prefix(era).any(|(validator, exposures)| { | ||
| validator == *staker || exposures.others.iter().any(|i| i.who == *staker) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.