From 805ea9a436548691ed68929ab8fa9ec7df332544 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Thu, 17 Nov 2022 15:22:19 +0100 Subject: [PATCH 01/10] delete releases --- frame/staking/src/lib.rs | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index a0144463540be..cce5d5fe169c9 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -877,31 +877,6 @@ impl Default for Forcing { } } -// A value placed in storage that represents the current version of the Staking storage. This value -// is used by the `on_runtime_upgrade` logic to determine whether we run storage migration logic. -// This should match directly with the semantic versions of the Rust crate. -#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] -enum Releases { - V1_0_0Ancient, - 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 `VoterList`. - V9_0_0, // inject validators into `VoterList` as well. - V10_0_0, // remove `EarliestUnappliedSlash`. - V11_0_0, // Move pallet storage prefix, e.g. BagsList -> VoterBagsList - V12_0_0, // remove `HistoryDepth`. -} - -impl Default for Releases { - fn default() -> Self { - Releases::V11_0_0 - } -} - /// A `Convert` implementation that finds the stash of the given controller account, /// if any. pub struct StashOf(sp_std::marker::PhantomData); From 4c71f5a52d727746c27f8afe5529f9c2fa961163 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Thu, 17 Nov 2022 15:24:22 +0100 Subject: [PATCH 02/10] use standard pallet storage version --- frame/staking/src/migrations.rs | 74 ++++++++++++++++++++------------- frame/staking/src/pallet/mod.rs | 14 +++---- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index f2ccb4f8b096f..3e7bbdd8c656a 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -36,7 +36,7 @@ pub mod v12 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == Releases::V11_0_0, + Pallet::::on_chain_storage_version() == 11, "Expected v11 before upgrading to v12" ); @@ -53,9 +53,12 @@ pub mod v12 { } fn on_runtime_upgrade() -> frame_support::weights::Weight { - if StorageVersion::::get() == Releases::V11_0_0 { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if current == 12 && onchain == 11 { HistoryDepth::::kill(); - StorageVersion::::put(Releases::V12_0_0); + current.put::>(); log!(info, "v12 applied successfully"); T::DbWeight::get().reads_writes(1, 2) @@ -68,7 +71,7 @@ pub mod v12 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V12_0_0, + Pallet::::on_chain_storage_version() == 12, "v12 not applied" ); Ok(()) @@ -92,7 +95,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V10_0_0, + Pallet::::on_chain_storage_version() == 10, "must upgrade linearly" ); let old_pallet_prefix = twox_128(N::get().as_bytes()); @@ -114,12 +117,15 @@ pub mod v11 { /// The migration will look into the storage version in order to avoid triggering a /// migration on an up to date storage. fn on_runtime_upgrade() -> Weight { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + let old_pallet_name = N::get(); let new_pallet_name =

::name(); - if StorageVersion::::get() == Releases::V10_0_0 { + if current == 11 && onchain == 10 { // bump version anyway, even if we don't need to move the prefix - StorageVersion::::put(Releases::V11_0_0); + current.put::>(); if new_pallet_name == old_pallet_name { log!( warn, @@ -139,7 +145,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V11_0_0, + Pallet::::on_chain_storage_version() == 11, "wrong version after the upgrade" ); @@ -184,7 +190,10 @@ pub mod v10 { pub struct MigrateToV10(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV10 { fn on_runtime_upgrade() -> frame_support::weights::Weight { - if StorageVersion::::get() == Releases::V9_0_0 { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if current == 10 && onchain == 9 { let pending_slashes = as Store>::UnappliedSlashes::iter().take(512); for (era, slashes) in pending_slashes { for slash in slashes { @@ -196,7 +205,7 @@ pub mod v10 { } EarliestUnappliedSlash::::kill(); - StorageVersion::::put(Releases::V10_0_0); + current.put::>(); log!(info, "MigrateToV10 executed successfully"); T::DbWeight::get().reads_writes(1, 1) @@ -221,7 +230,10 @@ pub mod v9 { pub struct InjectValidatorsIntoVoterList(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for InjectValidatorsIntoVoterList { fn on_runtime_upgrade() -> Weight { - if StorageVersion::::get() == Releases::V8_0_0 { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if current == 9 && onchain == 8 { let prev_count = T::VoterList::count(); let weight_of_cached = Pallet::::weight_of_fn(); for (v, _) in Validators::::iter() { @@ -239,13 +251,13 @@ pub mod v9 { T::VoterList::count(), ); - StorageVersion::::put(crate::Releases::V9_0_0); + current.put::>(); T::BlockWeights::get().max_block } else { log!( warn, "InjectValidatorsIntoVoterList being executed on the wrong storage \ - version, expected Releases::V8_0_0" + version, expected 8" ); T::DbWeight::get().reads(1) } @@ -254,7 +266,7 @@ pub mod v9 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V8_0_0, + Pallet::::on_chain_storage_version() == 8, "must upgrade linearly" ); @@ -272,7 +284,7 @@ pub mod v9 { assert!(post_count == prev_count + validators); frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V9_0_0, + Pallet::::on_chain_storage_version() == 9, "must upgrade " ); Ok(()) @@ -281,14 +293,15 @@ pub mod v9 { } pub mod v8 { - use crate::{Config, Nominators, Pallet, StorageVersion, Weight}; + use super::*; + use crate::{Config, Nominators, Pallet, Weight}; use frame_election_provider_support::SortedListProvider; - use frame_support::traits::Get; + use frame_support::traits::{Get}; #[cfg(feature = "try-runtime")] pub fn pre_migrate() -> Result<(), &'static str> { frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V7_0_0, + Pallet::::on_chain_storage_version() == 7 "must upgrade linearly" ); @@ -298,8 +311,11 @@ pub mod v8 { /// Migration to sorted `VoterList`. pub fn migrate() -> Weight { - if StorageVersion::::get() == crate::Releases::V7_0_0 { - crate::log!(info, "migrating staking to Releases::V8_0_0"); + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if current == 8 && onchain == 7 { + crate::log!(info, "migrating staking to 8"); let migrated = T::VoterList::unsafe_regenerate( Nominators::::iter().map(|(id, _)| id), @@ -307,10 +323,10 @@ pub mod v8 { ); debug_assert_eq!(T::VoterList::try_state(), Ok(())); - StorageVersion::::put(crate::Releases::V8_0_0); + current.put::>(); crate::log!( info, - "👜 completed staking migration to Releases::V8_0_0 with {} voters migrated", + "👜 completed staking migration to 8_with {} voters migrated", migrated, ); @@ -348,20 +364,21 @@ pub mod v7 { ); assert!(Validators::::count().is_zero(), "Validators already set."); assert!(Nominators::::count().is_zero(), "Nominators already set."); - assert!(StorageVersion::::get() == Releases::V6_0_0); + assert!(Pallet::::on_chain_storage_version() == 6); Ok(()) } pub fn migrate() -> Weight { - log!(info, "Migrating staking to Releases::V7_0_0"); + log!(info, "Migrating staking to 7"); let validator_count = Validators::::iter().count() as u32; let nominator_count = Nominators::::iter().count() as u32; CounterForValidators::::put(validator_count); CounterForNominators::::put(nominator_count); - StorageVersion::::put(Releases::V7_0_0); - log!(info, "Completed staking migration to Releases::V7_0_0"); + Pallet::::current_storage_version().put::>(); + + log!(info, "Completed staking migration to 7"); T::DbWeight::get().reads_writes(validator_count.saturating_add(nominator_count).into(), 2) } @@ -403,7 +420,7 @@ pub mod v6 { /// Migrate storage to v6. pub fn migrate() -> Weight { - log!(info, "Migrating staking to Releases::V6_0_0"); + log!(info, "Migrating staking to 6"); SnapshotValidators::::kill(); SnapshotNominators::::kill(); @@ -412,7 +429,8 @@ pub mod v6 { EraElectionStatus::::kill(); IsCurrentSessionFinal::::kill(); - StorageVersion::::put(Releases::V6_0_0); + Pallet::::current_storage_version().put::>(); + log!(info, "Done."); T::DbWeight::get().writes(6 + 1) } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index df90873ab2e59..fec1ae65738d0 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -44,7 +44,7 @@ pub use impls::*; use crate::{ slashing, weights::WeightInfo, AccountIdLookupOf, ActiveEraInfo, BalanceOf, EraPayout, EraRewardPoints, Exposure, Forcing, NegativeImbalanceOf, Nominations, PositiveImbalanceOf, - Releases, RewardDestination, SessionInterface, StakingLedger, UnappliedSlash, UnlockChunk, + RewardDestination, SessionInterface, StakingLedger, UnappliedSlash, UnlockChunk, ValidatorPrefs, }; @@ -58,8 +58,12 @@ pub mod pallet { use super::*; + /// The current storage version. + const STORAGE_VERSION: StorageVersion = StorageVersion::new(12); + #[pallet::pallet] #[pallet::generate_store(pub(crate) trait Store)] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); /// Possible operations on the configuration values of this pallet. @@ -550,13 +554,6 @@ pub mod pallet { #[pallet::getter(fn offending_validators)] pub type OffendingValidators = StorageValue<_, Vec<(u32, bool)>, ValueQuery>; - /// True if network has been upgraded to this version. - /// Storage version of the pallet. - /// - /// This is set to v7.0.0 for new networks. - #[pallet::storage] - pub(crate) type StorageVersion = StorageValue<_, Releases, ValueQuery>; - /// The threshold for when users can start calling `chill_other` for other validators / /// nominators. The threshold is compared to the actual number of validators / nominators /// (`CountFor*`) in the system compared to the configured max (`Max*Count`). @@ -607,7 +604,6 @@ pub mod pallet { ForceEra::::put(self.force_era); CanceledSlashPayout::::put(self.canceled_payout); SlashRewardFraction::::put(self.slash_reward_fraction); - StorageVersion::::put(Releases::V7_0_0); MinNominatorBond::::put(self.min_nominator_bond); MinValidatorBond::::put(self.min_validator_bond); if let Some(x) = self.max_validator_count { From 59b0689a23bb78f5bdc107d97b5a4594e8d68acf Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Thu, 17 Nov 2022 15:40:19 +0100 Subject: [PATCH 03/10] migrate to standard storage version for staking --- frame/staking/src/migrations.rs | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 3e7bbdd8c656a..aec715098842f 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -19,6 +19,55 @@ use super::*; use frame_election_provider_support::SortedListProvider; use frame_support::traits::OnRuntimeUpgrade; +use frame_support::dispatch::GetStorageVersion; + +pub mod v13 { + use super::*; + use frame_support::{pallet_prelude::ValueQuery, storage_alias}; + + #[storage_alias] + type StorageVersion = StorageValue, u32, ValueQuery>; + + pub struct MigrateToV13(sp_std::marker::PhantomData); + impl OnRuntimeUpgrade for MigrateToV13 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + frame_support::ensure!( + Pallet::::on_chain_storage_version() == 12, + "Required v12 before upgrading to v13" + ); + + Ok(Default::default()) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if current == 13 && onchain == 12 { + StorageVersion::::kill(); + current.put::>(); + + log!(info, "v13 applied successfully"); + T::DbWeight::get().reads_writes(1, 2) + } else { + log!(warn, "Skipping v13, should be removed"); + T::DbWeight::get().reads(1) + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + frame_support::ensure!( + Pallet::::on_chain_storage_version() == 13, + "v13 not applied" + ); + Ok(()) + } + + } + +} pub mod v12 { use super::*; From 2a8dcc1a841f9d80648b36c271a9e86fef6a2dac Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Thu, 17 Nov 2022 16:03:07 +0100 Subject: [PATCH 04/10] not compiling --- frame/staking/src/migrations.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index aec715098842f..7d6fea123aa5c 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -26,14 +26,18 @@ pub mod v13 { use frame_support::{pallet_prelude::ValueQuery, storage_alias}; #[storage_alias] - type StorageVersion = StorageValue, u32, ValueQuery>; + type StorageVersion = StorageValue, Releases, ValueQuery>; + #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] + enum Releases { + V12_0_0, + } pub struct MigrateToV13(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV13 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 12, + StorageVersion::::get() == Releases::V12_0_0, "Required v12 before upgrading to v13" ); @@ -42,9 +46,9 @@ pub mod v13 { fn on_runtime_upgrade() -> frame_support::weights::Weight { let current = Pallet::::current_storage_version(); - let onchain = Pallet::::on_chain_storage_version(); + let onchain = StorageVersion::::get(); - if current == 13 && onchain == 12 { + if current == 13 && onchain == Releases::V12_0_0 { StorageVersion::::kill(); current.put::>(); @@ -62,6 +66,12 @@ pub mod v13 { Pallet::::on_chain_storage_version() == 13, "v13 not applied" ); + + frame_support::ensure!( + !StorageVersion::::exists(), + "Storage version not migrated correctly" + ); + Ok(()) } From b3b1090c3373dd6878d5800ec08661377049f4f8 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Sat, 19 Nov 2022 09:10:26 +0000 Subject: [PATCH 05/10] keep old releases enum around for decoding --- frame/staking/src/migrations.rs | 107 ++++++++++++++++---------------- frame/staking/src/pallet/mod.rs | 2 +- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 7d6fea123aa5c..efb0468fce5f7 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -20,35 +20,53 @@ use super::*; use frame_election_provider_support::SortedListProvider; use frame_support::traits::OnRuntimeUpgrade; use frame_support::dispatch::GetStorageVersion; +use frame_support::{pallet_prelude::ValueQuery, storage_alias}; + +#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +enum OldReleases { + V1_0_0Ancient, + 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 `VoterList`. + V9_0_0, // inject validators into `VoterList` as well. + V10_0_0, // remove `EarliestUnappliedSlash`. + V11_0_0, // Move pallet storage prefix, e.g. BagsList -> VoterBagsList + V12_0_0, // remove `HistoryDepth`. +} + +impl Default for OldReleases { + fn default() -> Self { + OldReleases::V12_0_0 + } +} + +#[storage_alias] +type StorageVersion = StorageValue, OldReleases, ValueQuery>; pub mod v13 { use super::*; - use frame_support::{pallet_prelude::ValueQuery, storage_alias}; - #[storage_alias] - type StorageVersion = StorageValue, Releases, ValueQuery>; - - #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] - enum Releases { - V12_0_0, - } pub struct MigrateToV13(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV13 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == Releases::V12_0_0, + StorageVersion::::get() == OldReleases::V12_0_0, "Required v12 before upgrading to v13" ); Ok(Default::default()) } - fn on_runtime_upgrade() -> frame_support::weights::Weight { + fn on_runtime_upgrade() -> Weight { let current = Pallet::::current_storage_version(); let onchain = StorageVersion::::get(); - if current == 13 && onchain == Releases::V12_0_0 { + if current == 13 && onchain == OldReleases::V12_0_0 { StorageVersion::::kill(); current.put::>(); @@ -95,7 +113,7 @@ pub mod v12 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 11, + StorageVersion::::get() == OldReleases::V11_0_0, "Expected v11 before upgrading to v12" ); @@ -112,12 +130,9 @@ pub mod v12 { } fn on_runtime_upgrade() -> frame_support::weights::Weight { - let current = Pallet::::current_storage_version(); - let onchain = Pallet::::on_chain_storage_version(); - - if current == 12 && onchain == 11 { + if StorageVersion::::get() == OldReleases::V11_0_0 { HistoryDepth::::kill(); - current.put::>(); + StorageVersion::::put(OldReleases::V12_0_0); log!(info, "v12 applied successfully"); T::DbWeight::get().reads_writes(1, 2) @@ -130,7 +145,7 @@ pub mod v12 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 12, + StorageVersion::::get() == OldReleases::V12_0_0, "v12 not applied" ); Ok(()) @@ -154,7 +169,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 10, + StorageVersion::::get() == OldReleases::V10_0_0, "must upgrade linearly" ); let old_pallet_prefix = twox_128(N::get().as_bytes()); @@ -176,15 +191,13 @@ pub mod v11 { /// The migration will look into the storage version in order to avoid triggering a /// migration on an up to date storage. fn on_runtime_upgrade() -> Weight { - let current = Pallet::::current_storage_version(); - let onchain = Pallet::::on_chain_storage_version(); let old_pallet_name = N::get(); let new_pallet_name =

::name(); - if current == 11 && onchain == 10 { + if StorageVersion::::get() == OldReleases::V10_0_0 { // bump version anyway, even if we don't need to move the prefix - current.put::>(); + StorageVersion::::put(OldReleases::V11_0_0); if new_pallet_name == old_pallet_name { log!( warn, @@ -204,7 +217,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 11, + StorageVersion::::get() == OldReleases::V11_0_0, "wrong version after the upgrade" ); @@ -249,10 +262,7 @@ pub mod v10 { pub struct MigrateToV10(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV10 { fn on_runtime_upgrade() -> frame_support::weights::Weight { - let current = Pallet::::current_storage_version(); - let onchain = Pallet::::on_chain_storage_version(); - - if current == 10 && onchain == 9 { + if StorageVersion::::get() == OldReleases::V9_0_0 { let pending_slashes = as Store>::UnappliedSlashes::iter().take(512); for (era, slashes) in pending_slashes { for slash in slashes { @@ -264,7 +274,7 @@ pub mod v10 { } EarliestUnappliedSlash::::kill(); - current.put::>(); + StorageVersion::::put(OldReleases::V10_0_0); log!(info, "MigrateToV10 executed successfully"); T::DbWeight::get().reads_writes(1, 1) @@ -289,10 +299,7 @@ pub mod v9 { pub struct InjectValidatorsIntoVoterList(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for InjectValidatorsIntoVoterList { fn on_runtime_upgrade() -> Weight { - let current = Pallet::::current_storage_version(); - let onchain = Pallet::::on_chain_storage_version(); - - if current == 9 && onchain == 8 { + if StorageVersion::::get() == OldReleases::V8_0_0 { let prev_count = T::VoterList::count(); let weight_of_cached = Pallet::::weight_of_fn(); for (v, _) in Validators::::iter() { @@ -310,13 +317,13 @@ pub mod v9 { T::VoterList::count(), ); - current.put::>(); + StorageVersion::::put(OldReleases::V9_0_0); T::BlockWeights::get().max_block } else { log!( warn, "InjectValidatorsIntoVoterList being executed on the wrong storage \ - version, expected 8" + version, expected OldReleases::V8_0_0" ); T::DbWeight::get().reads(1) } @@ -325,7 +332,7 @@ pub mod v9 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 8, + StorageVersion::::get() == OldReleases::V8_0_0, "must upgrade linearly" ); @@ -343,7 +350,7 @@ pub mod v9 { assert!(post_count == prev_count + validators); frame_support::ensure!( - Pallet::::on_chain_storage_version() == 9, + StorageVersion::::get() == OldReleases::V9_0_0, "must upgrade " ); Ok(()) @@ -360,7 +367,7 @@ pub mod v8 { #[cfg(feature = "try-runtime")] pub fn pre_migrate() -> Result<(), &'static str> { frame_support::ensure!( - Pallet::::on_chain_storage_version() == 7 + StorageVersion::::get() == OldReleases::V7_0_0, "must upgrade linearly" ); @@ -370,11 +377,8 @@ pub mod v8 { /// Migration to sorted `VoterList`. pub fn migrate() -> Weight { - let current = Pallet::::current_storage_version(); - let onchain = Pallet::::on_chain_storage_version(); - - if current == 8 && onchain == 7 { - crate::log!(info, "migrating staking to 8"); + if StorageVersion::::get() == OldReleases::V7_0_0 { + crate::log!(info, "migrating staking to OldReleases::V8_0_0"); let migrated = T::VoterList::unsafe_regenerate( Nominators::::iter().map(|(id, _)| id), @@ -382,10 +386,10 @@ pub mod v8 { ); debug_assert_eq!(T::VoterList::try_state(), Ok(())); - current.put::>(); + StorageVersion::::put(OldReleases::V8_0_0); crate::log!( info, - "👜 completed staking migration to 8_with {} voters migrated", + "👜 completed staking migration to OldReleases::V8_0_0 with {} voters migrated", migrated, ); @@ -423,21 +427,20 @@ pub mod v7 { ); assert!(Validators::::count().is_zero(), "Validators already set."); assert!(Nominators::::count().is_zero(), "Nominators already set."); - assert!(Pallet::::on_chain_storage_version() == 6); + assert!(StorageVersion::::get() == Releases::V6_0_0); Ok(()) } pub fn migrate() -> Weight { - log!(info, "Migrating staking to 7"); + log!(info, "Migrating staking to Releases::V7_0_0"); let validator_count = Validators::::iter().count() as u32; let nominator_count = Nominators::::iter().count() as u32; CounterForValidators::::put(validator_count); CounterForNominators::::put(nominator_count); - Pallet::::current_storage_version().put::>(); - - log!(info, "Completed staking migration to 7"); + StorageVersion::::put(Releases::V7_0_0); + log!(info, "Completed staking migration to Releases::V7_0_0"); T::DbWeight::get().reads_writes(validator_count.saturating_add(nominator_count).into(), 2) } @@ -479,7 +482,7 @@ pub mod v6 { /// Migrate storage to v6. pub fn migrate() -> Weight { - log!(info, "Migrating staking to 6"); + log!(info, "Migrating staking to OldReleases::V6_0_0"); SnapshotValidators::::kill(); SnapshotNominators::::kill(); @@ -488,7 +491,7 @@ pub mod v6 { EraElectionStatus::::kill(); IsCurrentSessionFinal::::kill(); - Pallet::::current_storage_version().put::>(); + StorageVersion::::put(Releases::V6_0_0); log!(info, "Done."); T::DbWeight::get().writes(6 + 1) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index fec1ae65738d0..027a5c5b6ba0a 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -59,7 +59,7 @@ pub mod pallet { use super::*; /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(12); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(13); #[pallet::pallet] #[pallet::generate_store(pub(crate) trait Store)] From 90f17a9750d25daec67c0ea2468aa0617a47de9b Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Sun, 20 Nov 2022 09:21:50 +0000 Subject: [PATCH 06/10] fix releases --- frame/staking/src/migrations.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index efb0468fce5f7..818153f087bf6 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -427,20 +427,20 @@ pub mod v7 { ); assert!(Validators::::count().is_zero(), "Validators already set."); assert!(Nominators::::count().is_zero(), "Nominators already set."); - assert!(StorageVersion::::get() == Releases::V6_0_0); + assert!(StorageVersion::::get() == OldReleases::V6_0_0); Ok(()) } pub fn migrate() -> Weight { - log!(info, "Migrating staking to Releases::V7_0_0"); + log!(info, "Migrating staking to OldReleases::V7_0_0"); let validator_count = Validators::::iter().count() as u32; let nominator_count = Nominators::::iter().count() as u32; CounterForValidators::::put(validator_count); CounterForNominators::::put(nominator_count); - StorageVersion::::put(Releases::V7_0_0); - log!(info, "Completed staking migration to Releases::V7_0_0"); + StorageVersion::::put(OldReleases::V7_0_0); + log!(info, "Completed staking migration to OldReleases::V7_0_0"); T::DbWeight::get().reads_writes(validator_count.saturating_add(nominator_count).into(), 2) } @@ -491,7 +491,7 @@ pub mod v6 { EraElectionStatus::::kill(); IsCurrentSessionFinal::::kill(); - StorageVersion::::put(Releases::V6_0_0); + StorageVersion::::put(OldReleases::V6_0_0); log!(info, "Done."); T::DbWeight::get().writes(6 + 1) From e5d1f70f7ae4a1b40d313d6d29f927019389f975 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Thu, 24 Nov 2022 09:56:16 +0000 Subject: [PATCH 07/10] rename old releases --- frame/staking/src/migrations.rs | 68 ++++++++++++++++----------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 818153f087bf6..a697f800c06a1 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -18,12 +18,14 @@ use super::*; use frame_election_provider_support::SortedListProvider; -use frame_support::traits::OnRuntimeUpgrade; -use frame_support::dispatch::GetStorageVersion; -use frame_support::{pallet_prelude::ValueQuery, storage_alias}; +use frame_support::{ + dispatch::GetStorageVersion, pallet_prelude::ValueQuery, storage_alias, + traits::OnRuntimeUpgrade, +}; +/// Outdated since v12. Useful to make encoding/decoding of old migration code easier. #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] -enum OldReleases { +enum ObsoleteReleases { V1_0_0Ancient, V2_0_0, V3_0_0, @@ -38,14 +40,15 @@ enum OldReleases { V12_0_0, // remove `HistoryDepth`. } -impl Default for OldReleases { +impl Default for ObsoleteReleases { fn default() -> Self { - OldReleases::V12_0_0 + ObsoleteReleases::V12_0_0 } } +/// Alias to the old storage item for keeping versions. Outdated since v12. #[storage_alias] -type StorageVersion = StorageValue, OldReleases, ValueQuery>; +type StorageVersion = StorageValue, ObsoleteReleases, ValueQuery>; pub mod v13 { use super::*; @@ -55,7 +58,7 @@ pub mod v13 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V12_0_0, + StorageVersion::::get() == ObsoleteReleases::V12_0_0, "Required v12 before upgrading to v13" ); @@ -66,7 +69,7 @@ pub mod v13 { let current = Pallet::::current_storage_version(); let onchain = StorageVersion::::get(); - if current == 13 && onchain == OldReleases::V12_0_0 { + if current == 13 && onchain == ObsoleteReleases::V12_0_0 { StorageVersion::::kill(); current.put::>(); @@ -88,13 +91,11 @@ pub mod v13 { frame_support::ensure!( !StorageVersion::::exists(), "Storage version not migrated correctly" - ); + ); Ok(()) } - } - } pub mod v12 { @@ -113,7 +114,7 @@ pub mod v12 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V11_0_0, + StorageVersion::::get() == ObsoleteReleases::V11_0_0, "Expected v11 before upgrading to v12" ); @@ -130,9 +131,9 @@ pub mod v12 { } fn on_runtime_upgrade() -> frame_support::weights::Weight { - if StorageVersion::::get() == OldReleases::V11_0_0 { + if StorageVersion::::get() == ObsoleteReleases::V11_0_0 { HistoryDepth::::kill(); - StorageVersion::::put(OldReleases::V12_0_0); + StorageVersion::::put(ObsoleteReleases::V12_0_0); log!(info, "v12 applied successfully"); T::DbWeight::get().reads_writes(1, 2) @@ -145,7 +146,7 @@ pub mod v12 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V12_0_0, + StorageVersion::::get() == ObsoleteReleases::V12_0_0, "v12 not applied" ); Ok(()) @@ -169,7 +170,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V10_0_0, + StorageVersion::::get() == ObsoleteReleases::V10_0_0, "must upgrade linearly" ); let old_pallet_prefix = twox_128(N::get().as_bytes()); @@ -191,13 +192,12 @@ pub mod v11 { /// The migration will look into the storage version in order to avoid triggering a /// migration on an up to date storage. fn on_runtime_upgrade() -> Weight { - let old_pallet_name = N::get(); let new_pallet_name =

::name(); - if StorageVersion::::get() == OldReleases::V10_0_0 { + if StorageVersion::::get() == ObsoleteReleases::V10_0_0 { // bump version anyway, even if we don't need to move the prefix - StorageVersion::::put(OldReleases::V11_0_0); + StorageVersion::::put(ObsoleteReleases::V11_0_0); if new_pallet_name == old_pallet_name { log!( warn, @@ -217,7 +217,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V11_0_0, + StorageVersion::::get() == ObsoleteReleases::V11_0_0, "wrong version after the upgrade" ); @@ -262,7 +262,7 @@ pub mod v10 { pub struct MigrateToV10(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV10 { fn on_runtime_upgrade() -> frame_support::weights::Weight { - if StorageVersion::::get() == OldReleases::V9_0_0 { + if StorageVersion::::get() == ObsoleteReleases::V9_0_0 { let pending_slashes = as Store>::UnappliedSlashes::iter().take(512); for (era, slashes) in pending_slashes { for slash in slashes { @@ -274,7 +274,7 @@ pub mod v10 { } EarliestUnappliedSlash::::kill(); - StorageVersion::::put(OldReleases::V10_0_0); + StorageVersion::::put(ObsoleteReleases::V10_0_0); log!(info, "MigrateToV10 executed successfully"); T::DbWeight::get().reads_writes(1, 1) @@ -299,7 +299,7 @@ pub mod v9 { pub struct InjectValidatorsIntoVoterList(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for InjectValidatorsIntoVoterList { fn on_runtime_upgrade() -> Weight { - if StorageVersion::::get() == OldReleases::V8_0_0 { + if StorageVersion::::get() == ObsoleteReleases::V8_0_0 { let prev_count = T::VoterList::count(); let weight_of_cached = Pallet::::weight_of_fn(); for (v, _) in Validators::::iter() { @@ -317,7 +317,7 @@ pub mod v9 { T::VoterList::count(), ); - StorageVersion::::put(OldReleases::V9_0_0); + StorageVersion::::put(ObsoleteReleases::V9_0_0); T::BlockWeights::get().max_block } else { log!( @@ -332,7 +332,7 @@ pub mod v9 { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V8_0_0, + StorageVersion::::get() == ObsoleteReleases::V8_0_0, "must upgrade linearly" ); @@ -350,7 +350,7 @@ pub mod v9 { assert!(post_count == prev_count + validators); frame_support::ensure!( - StorageVersion::::get() == OldReleases::V9_0_0, + StorageVersion::::get() == ObsoleteReleases::V9_0_0, "must upgrade " ); Ok(()) @@ -362,12 +362,12 @@ pub mod v8 { use super::*; use crate::{Config, Nominators, Pallet, Weight}; use frame_election_provider_support::SortedListProvider; - use frame_support::traits::{Get}; + use frame_support::traits::Get; #[cfg(feature = "try-runtime")] pub fn pre_migrate() -> Result<(), &'static str> { frame_support::ensure!( - StorageVersion::::get() == OldReleases::V7_0_0, + StorageVersion::::get() == ObsoleteReleases::V7_0_0, "must upgrade linearly" ); @@ -377,7 +377,7 @@ pub mod v8 { /// Migration to sorted `VoterList`. pub fn migrate() -> Weight { - if StorageVersion::::get() == OldReleases::V7_0_0 { + if StorageVersion::::get() == ObsoleteReleases::V7_0_0 { crate::log!(info, "migrating staking to OldReleases::V8_0_0"); let migrated = T::VoterList::unsafe_regenerate( @@ -386,7 +386,7 @@ pub mod v8 { ); debug_assert_eq!(T::VoterList::try_state(), Ok(())); - StorageVersion::::put(OldReleases::V8_0_0); + StorageVersion::::put(ObsoleteReleases::V8_0_0); crate::log!( info, "👜 completed staking migration to OldReleases::V8_0_0 with {} voters migrated", @@ -427,7 +427,7 @@ pub mod v7 { ); assert!(Validators::::count().is_zero(), "Validators already set."); assert!(Nominators::::count().is_zero(), "Nominators already set."); - assert!(StorageVersion::::get() == OldReleases::V6_0_0); + assert!(StorageVersion::::get() == ObsoleteReleases::V6_0_0); Ok(()) } @@ -439,7 +439,7 @@ pub mod v7 { CounterForValidators::::put(validator_count); CounterForNominators::::put(nominator_count); - StorageVersion::::put(OldReleases::V7_0_0); + StorageVersion::::put(ObsoleteReleases::V7_0_0); log!(info, "Completed staking migration to OldReleases::V7_0_0"); T::DbWeight::get().reads_writes(validator_count.saturating_add(nominator_count).into(), 2) @@ -491,7 +491,7 @@ pub mod v6 { EraElectionStatus::::kill(); IsCurrentSessionFinal::::kill(); - StorageVersion::::put(OldReleases::V6_0_0); + StorageVersion::::put(ObsoleteReleases::V6_0_0); log!(info, "Done."); T::DbWeight::get().writes(6 + 1) From c53a84884d2acf3495d64f02fe13b8d4da10eac8 Mon Sep 17 00:00:00 2001 From: Ankan Anurag Date: Tue, 29 Nov 2022 13:13:01 +0100 Subject: [PATCH 08/10] retriggering ci --- frame/election-provider-support/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/election-provider-support/src/lib.rs b/frame/election-provider-support/src/lib.rs index 38924a18e2f54..5da0f48ab6965 100644 --- a/frame/election-provider-support/src/lib.rs +++ b/frame/election-provider-support/src/lib.rs @@ -391,7 +391,7 @@ pub trait ElectionProviderBase { if desired_targets <= Self::MaxWinners::get() { Ok(desired_targets) } else { - Err("desired_targets should not be greater than MaxWinners") + Err("desired_targets should never be greater than MaxWinners") }, Err(e) => Err(e), } From 93ba64a7b6db82de276d29fdc90bce0c6fc59498 Mon Sep 17 00:00:00 2001 From: Ankan Date: Mon, 19 Dec 2022 13:17:40 +0100 Subject: [PATCH 09/10] fix migration comments --- frame/staking/src/migrations.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index a697f800c06a1..b647b0426213e 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -323,7 +323,7 @@ pub mod v9 { log!( warn, "InjectValidatorsIntoVoterList being executed on the wrong storage \ - version, expected OldReleases::V8_0_0" + version, expected ObsoleteReleases::V8_0_0" ); T::DbWeight::get().reads(1) } @@ -378,7 +378,7 @@ pub mod v8 { /// Migration to sorted `VoterList`. pub fn migrate() -> Weight { if StorageVersion::::get() == ObsoleteReleases::V7_0_0 { - crate::log!(info, "migrating staking to OldReleases::V8_0_0"); + crate::log!(info, "migrating staking to ObsoleteReleases::V8_0_0"); let migrated = T::VoterList::unsafe_regenerate( Nominators::::iter().map(|(id, _)| id), @@ -389,7 +389,7 @@ pub mod v8 { StorageVersion::::put(ObsoleteReleases::V8_0_0); crate::log!( info, - "👜 completed staking migration to OldReleases::V8_0_0 with {} voters migrated", + "👜 completed staking migration to ObsoleteReleases::V8_0_0 with {} voters migrated", migrated, ); @@ -432,7 +432,7 @@ pub mod v7 { } pub fn migrate() -> Weight { - log!(info, "Migrating staking to OldReleases::V7_0_0"); + log!(info, "Migrating staking to ObsoleteReleases::V7_0_0"); let validator_count = Validators::::iter().count() as u32; let nominator_count = Nominators::::iter().count() as u32; @@ -440,7 +440,7 @@ pub mod v7 { CounterForNominators::::put(nominator_count); StorageVersion::::put(ObsoleteReleases::V7_0_0); - log!(info, "Completed staking migration to OldReleases::V7_0_0"); + log!(info, "Completed staking migration to ObsoleteReleases::V7_0_0"); T::DbWeight::get().reads_writes(validator_count.saturating_add(nominator_count).into(), 2) } @@ -482,7 +482,7 @@ pub mod v6 { /// Migrate storage to v6. pub fn migrate() -> Weight { - log!(info, "Migrating staking to OldReleases::V6_0_0"); + log!(info, "Migrating staking to ObsoleteReleases::V6_0_0"); SnapshotValidators::::kill(); SnapshotNominators::::kill(); From c2f3cbc8eaa3afb1fab4c4dcde87dc287e6a6ca3 Mon Sep 17 00:00:00 2001 From: Ankan Date: Mon, 19 Dec 2022 14:16:17 +0100 Subject: [PATCH 10/10] doc update --- frame/staking/src/migrations.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index b647b0426213e..6253c3feed17d 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -23,7 +23,9 @@ use frame_support::{ traits::OnRuntimeUpgrade, }; -/// Outdated since v12. Useful to make encoding/decoding of old migration code easier. +/// Used for release versioning upto v12. +/// +/// Obsolete from v13. Keeping around to make encoding/decoding of old migration code easier. #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] enum ObsoleteReleases { V1_0_0Ancient, @@ -46,7 +48,7 @@ impl Default for ObsoleteReleases { } } -/// Alias to the old storage item for keeping versions. Outdated since v12. +/// Alias to the old storage item used for release versioning. Obsolete since v13. #[storage_alias] type StorageVersion = StorageValue, ObsoleteReleases, ValueQuery>;