-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Groundwork to store validators also in a bags-list instance (2) #10944
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -767,11 +767,12 @@ enum Releases { | |||||||||
| V2_0_0, | ||||||||||
| V3_0_0, | ||||||||||
| V4_0_0, | ||||||||||
| V5_0_0, // blockable validators. | ||||||||||
| V6_0_0, // removal of all storage associated with offchain phragmen. | ||||||||||
| V7_0_0, // keep track of number of nominators / validators in map | ||||||||||
| V8_0_0, // populate `SortedListProvider`. | ||||||||||
| V9_0_0, // inject validators into `SortedListProvider` as well. | ||||||||||
| V5_0_0, // blockable validators. | ||||||||||
| V6_0_0, // removal of all storage associated with offchain phragmen. | ||||||||||
| V7_0_0, // keep track of number of nominators / validators in map | ||||||||||
| V8_0_0, // populate `VoterList`. | ||||||||||
| V9_0_0, // inject validators into `NPoSVoteProvider` as well. | ||||||||||
| V10_0_0, // inject validator into `NPoSTargetList`. | ||||||||||
|
Comment on lines
+774
to
+775
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.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| impl Default for Releases { | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,45 +22,79 @@ use frame_support::traits::OnRuntimeUpgrade; | |||||
|
|
||||||
| /// Migration implementation that injects all validators into sorted list. | ||||||
| /// | ||||||
| /// This is only useful for chains that started their `SortedListProvider` just based on nominators. | ||||||
| pub struct InjectValidatorsIntoSortedListProvider<T>(sp_std::marker::PhantomData<T>); | ||||||
| impl<T: Config> OnRuntimeUpgrade for InjectValidatorsIntoSortedListProvider<T> { | ||||||
| /// This is only useful for chains that started their `VoterList` just based on nominators. | ||||||
| pub struct InjectValidatorsIntoVoterList<T>(sp_std::marker::PhantomData<T>); | ||||||
| impl<T: Config> OnRuntimeUpgrade for InjectValidatorsIntoVoterList<T> { | ||||||
| fn on_runtime_upgrade() -> Weight { | ||||||
| if StorageVersion::<T>::get() == Releases::V8_0_0 { | ||||||
| for (v, _) in Validators::<T>::iter() { | ||||||
| let weight = Pallet::<T>::vote_weight(&v); | ||||||
| let _ = T::SortedListProvider::on_insert(v.clone(), weight).map_err(|err| { | ||||||
| log!(warn, "failed to insert {:?} into SortedListProvider: {:?}", v, err) | ||||||
| let _ = T::VoterList::on_insert(v.clone(), weight).map_err(|err| { | ||||||
| log!(warn, "failed to insert {:?} into VoterList: {:?}", v, err) | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| StorageVersion::<T>::put(Releases::V9_0_0); | ||||||
| T::BlockWeights::get().max_block | ||||||
| } else { | ||||||
| log!(warn, "InjectValidatorsIntoSortedListProvider being executed on the wrong storage version, expected Releases::V8_0_0"); | ||||||
| log!(warn, "InjectValidatorsIntoVoterList being executed on the wrong storage version, expected Releases::V8_0_0"); | ||||||
|
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.
Suggested change
maybe? Executed makes it sound like we did the migration anyways |
||||||
| T::DbWeight::get().reads(1) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(feature = "try-runtime")] | ||||||
| fn pre_upgrade() -> Result<(), &'static str> { | ||||||
| use frame_support::traits::OnRuntimeUpgradeHelpersExt; | ||||||
| frame_support::ensure!( | ||||||
| StorageVersion::<T>::get() == crate::Releases::V8_0_0, | ||||||
| "must upgrade linearly" | ||||||
| ); | ||||||
| ensure!(StorageVersion::<T>::get() == crate::Releases::V8_0_0, "must upgrade linearly"); | ||||||
|
|
||||||
| let prev_count = T::SortedListProvider::count(); | ||||||
| let prev_count = T::VoterList::count(); | ||||||
| Self::set_temp_storage(prev_count, "prev"); | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| #[cfg(feature = "try-runtime")] | ||||||
| fn post_upgrade() -> Result<(), &'static str> { | ||||||
| use frame_support::traits::OnRuntimeUpgradeHelpersExt; | ||||||
| let post_count = T::SortedListProvider::count(); | ||||||
| let post_count = T::VoterList::count(); | ||||||
| let prev_count = Self::get_temp_storage::<u32>("prev").unwrap(); | ||||||
| let validators = Validators::<T>::count(); | ||||||
| assert!(post_count == prev_count + validators); | ||||||
| ensure!(post_count == prev_count + validators, "incorrect count"); | ||||||
| ensure!(StorageVersion::<T>::get(), Releases::V9_0_0, "version not set"); | ||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Migration implementation that injects all validators into sorted list. | ||||||
| /// | ||||||
| /// This is only useful for chains that started their `VoterList` just based on nominators. | ||||||
| pub struct InjectValidatorsIntoTargetList<T>(sp_std::marker::PhantomData<T>); | ||||||
| impl<T: Config> OnRuntimeUpgrade for InjectValidatorsIntoTargetList<T> { | ||||||
| fn on_runtime_upgrade() -> Weight { | ||||||
| if StorageVersion::<T>::get() == Releases::V9_0_0 { | ||||||
| for (v, _) in Validators::<T>::iter() { | ||||||
| let weight = Pallet::<T>::vote_weight(&v); | ||||||
| let _ = T::TargetList::on_insert(v.clone(), weight).map_err(|err| { | ||||||
| log!(warn, "failed to insert {:?} into TargetList: {:?}", v, err) | ||||||
| }); | ||||||
| } | ||||||
|
Comment on lines
+74
to
+79
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. To avoid duplicate code we can instead use: TargetList::unsafe_regenerate(
Validators::<T>::iter().map(|(id, _)| id),
Pallet::<T>::weight_of_fn(),
)There isn't any performance benefit though (originally there was, but that never made it into master because we refactored |
||||||
|
|
||||||
| StorageVersion::<T>::put(Releases::V10_0_0); | ||||||
| T::BlockWeights::get().max_block | ||||||
| } else { | ||||||
| log!(warn, "InjectValidatorsIntoTargetList being executed on the wrong storage version, expected Releases::V9_0_0"); | ||||||
|
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.
Suggested change
Same rationale as above - although not really a big deal |
||||||
| T::DbWeight::get().reads(1) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(feature = "try-runtime")] | ||||||
| fn pre_upgrade() -> Result<(), &'static str> { | ||||||
| ensure!(StorageVersion::<T>::get() == Releases::V9_0_0, "must upgrade linearly"); | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| #[cfg(feature = "try-runtime")] | ||||||
| fn post_upgrade() -> Result<(), &'static str> { | ||||||
| ensure!(StorageVersion::<T>::get(), Releases::V10_0_0, "must upgrade linearly"); | ||||||
|
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. We should
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. Also |
||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -87,11 +121,11 @@ pub mod v8 { | |||||
| if StorageVersion::<T>::get() == crate::Releases::V7_0_0 { | ||||||
| crate::log!(info, "migrating staking to Releases::V8_0_0"); | ||||||
|
|
||||||
| let migrated = T::SortedListProvider::unsafe_regenerate( | ||||||
| let migrated = T::VoterList::unsafe_regenerate( | ||||||
| Nominators::<T>::iter().map(|(id, _)| id), | ||||||
| Pallet::<T>::weight_of_fn(), | ||||||
| ); | ||||||
| debug_assert_eq!(T::SortedListProvider::sanity_check(), Ok(())); | ||||||
| debug_assert_eq!(T::VoterList::sanity_check(), Ok(())); | ||||||
|
|
||||||
| StorageVersion::<T>::put(crate::Releases::V8_0_0); | ||||||
| crate::log!( | ||||||
|
|
@@ -108,8 +142,7 @@ pub mod v8 { | |||||
|
|
||||||
| #[cfg(feature = "try-runtime")] | ||||||
| pub fn post_migrate<T: Config>() -> Result<(), &'static str> { | ||||||
| T::SortedListProvider::sanity_check() | ||||||
| .map_err(|_| "SortedListProvider is not in a sane state.")?; | ||||||
| T::VoterList::sanity_check().map_err(|_| "VoterList is not in a sane state.")?; | ||||||
| crate::log!(info, "👜 staking bags-list migration passes POST migrate checks ✅",); | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like just a line re-wrap? IMO it would be preferable to keep this out since it seems totally unrelated