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
Add batching to fast-unstake pallet #12394
Merged
Merged
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3b88c13
implement a brand new batch with all tests passing.
kianenigma 53aa232
fix benchmarks as well
kianenigma ac2be3d
make benchmarks more or less work
kianenigma 0ba02aa
fix migration
kianenigma e34f7a0
add some testing
kianenigma d875edc
Update frame/fast-unstake/src/benchmarking.rs
kianenigma c7cff31
Merge branch 'master' of github.com:paritytech/substrate into kiz-fas…
kianenigma 4af299b
Merge branch 'kiz-fast-unstake-batch-try-3' of github.com:paritytech/…
kianenigma 8c40452
review comments
kianenigma f8af8a7
Merge branch 'master' of github.com:paritytech/substrate into kiz-fas…
kianenigma 752d22f
some fixes
kianenigma 87df442
Merge branch 'kiz-fast-unstake-batch-try-3' of github.com:paritytech/…
kianenigma 70461dd
fix review comments
kianenigma 268208e
fix build
kianenigma db2174b
fmt
kianenigma 1a91286
fix benchmarks
kianenigma 50542d6
Merge branch 'master' of github.com:paritytech/substrate into kiz-fas…
kianenigma 78ce1ea
merge master and everything works
kianenigma d992c2f
Merge branch 'kiz-fast-unstake-batch-try-3' of github.com:paritytech/…
kianenigma 7334232
fmt
kianenigma d2db1b6
update
kianenigma 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ mod tests; | |
| // NOTE: enable benchmarking in tests as well. | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| mod benchmarking; | ||
| pub mod migrations; | ||
| pub mod types; | ||
| pub mod weights; | ||
|
|
||
|
|
@@ -83,7 +84,7 @@ pub mod pallet { | |
| use frame_election_provider_support::ElectionProviderBase; | ||
| use frame_support::{ | ||
| pallet_prelude::*, | ||
| traits::{Defensive, ReservableCurrency}, | ||
| traits::{Defensive, ReservableCurrency, StorageVersion}, | ||
| }; | ||
| use frame_system::{pallet_prelude::*, RawOrigin}; | ||
| use pallet_staking::Pallet as Staking; | ||
|
|
@@ -105,7 +106,10 @@ pub mod pallet { | |
| } | ||
| } | ||
|
|
||
| const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); | ||
|
|
||
| #[pallet::pallet] | ||
| #[pallet::storage_version(STORAGE_VERSION)] | ||
| pub struct Pallet<T>(_); | ||
|
|
||
| #[pallet::config] | ||
|
|
@@ -167,12 +171,12 @@ pub mod pallet { | |
| /// An internal error happened. Operations will be paused now. | ||
| InternalError, | ||
| /// A batch was partially checked for the given eras, but the process did not finish. | ||
| Checking { eras: Vec<EraIndex> }, | ||
| BatchChecked { eras: Vec<EraIndex> }, | ||
kianenigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// A batch was terminated. | ||
| /// | ||
| /// This is always follows by a number of `Unstaked` or `Slashed` events, marking the end | ||
| /// of the batch. A new batch will be created upon next block. | ||
| Terminated, | ||
| BatchFinished, | ||
| } | ||
|
|
||
| #[pallet::error] | ||
|
|
@@ -218,6 +222,7 @@ pub mod pallet { | |
| return T::DbWeight::get().reads_writes(1, 1) | ||
| }, | ||
| }; | ||
|
|
||
| // insert them back into the queue. | ||
| Queue::<T>::insert(stash, deposit); | ||
kianenigma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Head::<T>::kill(); | ||
|
|
@@ -401,8 +406,8 @@ pub mod pallet { | |
|
|
||
| log!( | ||
| debug, | ||
| "checking {:?}, eras_to_check_per_block = {:?}, remaining_weight = {:?}", | ||
| stashes, | ||
| "checking {:?} stashes, eras_to_check_per_block = {:?}, remaining_weight = {:?}", | ||
| stashes.len(), | ||
| eras_to_check_per_block, | ||
| remaining_weight | ||
| ); | ||
|
|
@@ -480,7 +485,7 @@ 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. | ||
| stashes.into_iter().for_each(|(stash, deposit)| unstake_stash(stash, deposit)); | ||
| Self::deposit_event(Event::<T>::Terminated); | ||
| Self::deposit_event(Event::<T>::BatchFinished); | ||
| <T as Config>::WeightInfo::on_idle_unstake() | ||
|
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. nit: Curious if we could do a fast return here instead. Really not a fan of long
|
||
| } else { | ||
| // eras checked so far. | ||
|
|
@@ -508,10 +513,10 @@ pub mod pallet { | |
| match checked.try_extend(unchecked_eras_to_check.clone().into_iter()) { | ||
|
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. I'm assuming this is just defensive, should never happen?
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. indeed, ergo we call |
||
| Ok(_) => | ||
| if stashes.is_empty() { | ||
| Self::deposit_event(Event::<T>::Terminated); | ||
| Self::deposit_event(Event::<T>::BatchFinished); | ||
| } else { | ||
| Head::<T>::put(UnstakeRequest { stashes, checked }); | ||
| Self::deposit_event(Event::<T>::Checking { | ||
| Self::deposit_event(Event::<T>::BatchChecked { | ||
| eras: unchecked_eras_to_check, | ||
| }); | ||
| }, | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2022 Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| pub mod v1 { | ||
| use crate::{types::BalanceOf, *}; | ||
| use frame_support::{ | ||
| storage::unhashed, | ||
| traits::{Defensive, Get, GetStorageVersion, OnRuntimeUpgrade}, | ||
| weights::Weight, | ||
| }; | ||
| use sp_staking::EraIndex; | ||
|
|
||
| pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>); | ||
kianenigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> { | ||
| fn on_runtime_upgrade() -> Weight { | ||
| let current = Pallet::<T>::current_storage_version(); | ||
| let onchain = Pallet::<T>::on_chain_storage_version(); | ||
|
|
||
| log!( | ||
| info, | ||
| "Running migration with current storage version {:?} / onchain {:?}", | ||
| current, | ||
| onchain | ||
| ); | ||
|
|
||
| if current == 1 && onchain == 0 { | ||
| // if a head exists, then we put them back into the queue. | ||
| if Head::<T>::exists() { | ||
| if let Some((stash, _, deposit)) = | ||
| unhashed::take::<(T::AccountId, Vec<EraIndex>, BalanceOf<T>)>( | ||
| &Head::<T>::hashed_key(), | ||
| ) | ||
| .defensive() | ||
| { | ||
| Queue::<T>::insert(stash, deposit); | ||
| current.put::<Pallet<T>>(); | ||
| } else { | ||
| // not much we can do here -- head is already deleted. | ||
| } | ||
| T::DbWeight::get().reads_writes(2, 3) | ||
| } else { | ||
| T::DbWeight::get().reads(2) | ||
| } | ||
| } else { | ||
| log!(info, "Migration did not executed. This probably should be removed"); | ||
kianenigma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| T::DbWeight::get().reads(1) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade(_: Vec<u8>) -> Result<(), &'static str> { | ||
| assert_eq!(Pallet::<T>::on_chain_storage_version(), 0); | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> { | ||
| assert_eq!(Pallet::<T>::on_chain_storage_version(), 1); | ||
| } | ||
| } | ||
| } | ||
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.