-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mainnet storage version migrations (#913)
* staking migration (#904) * Scheduler migration (#905) * History migration (#906) * Transaction payment (#908) * lint * Bumped node and runtime version --------- Co-authored-by: Marcin <[email protected]>
- Loading branch information
1 parent
7d21e8e
commit f875597
Showing
9 changed files
with
288 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,35 @@ | ||
use frame_support::{pallet_prelude::StorageVersion, traits::OnRuntimeUpgrade}; | ||
use pallet_session::historical::{Config, Pallet}; | ||
use sp_runtime::traits::Get; | ||
|
||
use crate::{log, Weight}; | ||
|
||
pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>); | ||
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> { | ||
fn on_runtime_upgrade() -> Weight { | ||
StorageVersion::new(1).put::<Pallet<T>>(); | ||
log::info!(target: "runtime::historical", "migrated to V1"); | ||
T::DbWeight::get().reads_writes(0, 1) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
frame_support::ensure!( | ||
StorageVersion::get::<Pallet<T>>() == 0 || StorageVersion::get::<Pallet<T>>() == 1, | ||
"💸 Migration being executed on the wrong storage \ | ||
version, expected 0 or 1" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
frame_support::ensure!( | ||
StorageVersion::get::<Pallet<T>>() == 1, | ||
"💸 must upgrade to v1" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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,9 @@ | ||
mod history; | ||
mod scheduler; | ||
mod staking; | ||
mod transaction_payment; | ||
|
||
pub use history::MigrateToV1 as HistoryMigrateToV1; | ||
pub use scheduler::MigrateToV3 as SchedulerMigrateToV3; | ||
pub use staking::MigrateToV10 as StakingMigrateToV10; | ||
pub use transaction_payment::MigrateToV2 as TransactionPaymentMigrateToV2; |
This file contains 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,70 @@ | ||
use frame_support::{ | ||
log, | ||
pallet_prelude::{Get, StorageVersion}, | ||
traits::OnRuntimeUpgrade, | ||
}; | ||
use pallet_scheduler::{Agenda, Config, Pallet}; | ||
|
||
use crate::Weight; | ||
|
||
const TARGET: &str = "runtime::scheduler::migration"; | ||
|
||
/// Custom migrations the scheduler pallet from V0 to V3 that only bumps StorageVersion to 3 | ||
pub struct MigrateToV3<T>(sp_std::marker::PhantomData<T>); | ||
|
||
impl<T: Config> OnRuntimeUpgrade for MigrateToV3<T> { | ||
fn on_runtime_upgrade() -> Weight { | ||
let version = StorageVersion::get::<Pallet<T>>(); | ||
if version != 0 { | ||
log::warn!( | ||
target: TARGET, | ||
"skipping v0 to v3 migration: executed on wrong storage version.\ | ||
Expected version 0, found {:?}", | ||
version, | ||
); | ||
return T::DbWeight::get().reads(1); | ||
} | ||
|
||
let agendas = Agenda::<T>::iter_keys().count() as u32; | ||
if agendas != 0 { | ||
log::warn!( | ||
target: TARGET, | ||
"skipping v0 to v3 migration: Agendas are not empty. Found {:?} agendas.", | ||
agendas, | ||
); | ||
return T::DbWeight::get().reads(1 + agendas as u64); | ||
} | ||
|
||
StorageVersion::new(3).put::<Pallet<T>>(); | ||
T::DbWeight::get().reads_writes(1 + agendas as u64, 1) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
assert_eq!( | ||
StorageVersion::get::<Pallet<T>>(), | ||
0, | ||
"Can only upgrade from version 0" | ||
); | ||
|
||
let agendas = Agenda::<T>::iter_keys().count() as u32; | ||
assert_eq!(agendas, 0, "Agendas should be empty pre-upgrade!"); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
assert_eq!(StorageVersion::get::<Pallet<T>>(), 3, "Must upgrade"); | ||
|
||
let new_agendas = Agenda::<T>::iter_keys().count() as u32; | ||
assert_eq!(new_agendas, 0, "Agendas should be empty post-upgrade!"); | ||
|
||
log::info!( | ||
target: TARGET, | ||
"Migrated 0 agendas, bumped StorageVersion to V3" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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,82 @@ | ||
use codec::{Decode, Encode, MaxEncodedLen}; | ||
use frame_support::{ | ||
log, | ||
pallet_prelude::{Get, TypeInfo}, | ||
storage_alias, | ||
traits::OnRuntimeUpgrade, | ||
RuntimeDebug, | ||
}; | ||
use pallet_staking::Config; | ||
|
||
use crate::Weight; | ||
|
||
#[storage_alias] | ||
type StorageVersion = StorageValue<Staking, Releases>; | ||
|
||
// copied from pallet staking, hack for that fact that original struct is not exported | ||
#[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`. | ||
} | ||
|
||
pub struct MigrateToV10<T>(sp_std::marker::PhantomData<T>); | ||
impl<T: Config> OnRuntimeUpgrade for MigrateToV10<T> { | ||
fn on_runtime_upgrade() -> Weight { | ||
match StorageVersion::get() { | ||
None => { | ||
log::info!( | ||
target: "runtime::staking", | ||
"💸 Migrating storage to Releases::V10_0_0 from unknown version" | ||
); | ||
StorageVersion::put(Releases::V10_0_0); | ||
T::DbWeight::get().reads_writes(1, 1) | ||
} | ||
Some(Releases::V10_0_0) => { | ||
log::info!( | ||
target: "runtime::staking", | ||
"💸 Migrating storage to Releases::V10_0_0 from Releases::V10_0_0" | ||
); | ||
StorageVersion::put(Releases::V10_0_0); | ||
T::DbWeight::get().reads_writes(1, 1) | ||
} | ||
_ => { | ||
log::warn!( | ||
target: "runtime::staking", | ||
"💸 Migration being executed on the wrong storage \ | ||
version, expected Releases::V10_0_0 or None" | ||
); | ||
T::DbWeight::get().reads(1) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
frame_support::ensure!( | ||
StorageVersion::get() == Some(Releases::V10_0_0) || StorageVersion::get() == None, | ||
"💸 Migration being executed on the wrong storage \ | ||
version, expected Releases::V10_0_0 or None" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
frame_support::ensure!( | ||
StorageVersion::get() == Some(Releases::V10_0_0), | ||
"💸 must upgrade to Releases::V10_0_0" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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,75 @@ | ||
use codec::{Decode, Encode}; | ||
use frame_support::{ | ||
dispatch::Weight, | ||
log, | ||
pallet_prelude::{Get, TypeInfo}, | ||
storage_alias, | ||
traits::OnRuntimeUpgrade, | ||
RuntimeDebug, | ||
}; | ||
use pallet_transaction_payment::Config; | ||
|
||
/// Storage releases of the pallet. | ||
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)] | ||
enum Releases { | ||
/// Original version of the pallet. | ||
V1Ancient, | ||
/// One that bumps the usage to FixedU128 from FixedI128. | ||
V2, | ||
} | ||
|
||
#[storage_alias] | ||
type StorageVersion = StorageValue<TransactionPayment, Releases>; | ||
|
||
pub struct MigrateToV2<T>(sp_std::marker::PhantomData<T>); | ||
impl<T: Config> OnRuntimeUpgrade for MigrateToV2<T> { | ||
fn on_runtime_upgrade() -> Weight { | ||
match StorageVersion::get() { | ||
None => { | ||
log::info!( | ||
target: "runtime::transaction-payment", | ||
"💸 Migrating storage to Releases::V2 from unknown version" | ||
); | ||
StorageVersion::put(Releases::V2); | ||
T::DbWeight::get().reads_writes(1, 1) | ||
} | ||
Some(Releases::V1Ancient) => { | ||
log::info!( | ||
target: "runtime::transaction-payment", | ||
"💸 Migrating storage to Releases::V2 from Releases::V1Ancient" | ||
); | ||
StorageVersion::put(Releases::V2); | ||
T::DbWeight::get().reads_writes(1, 1) | ||
} | ||
_ => { | ||
log::warn!( | ||
target: "runtime::transaction-payment", | ||
"💸 Migration being executed on the wrong storage \ | ||
version, expected Releases::V1Ancient or None" | ||
); | ||
T::DbWeight::get().reads(1) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
frame_support::ensure!( | ||
StorageVersion::get() == Some(Releases::V1Ancient) || StorageVersion::get() == None, | ||
"💸 Migration being executed on the wrong storage \ | ||
version, expected Releases::V1Ancient or None" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
frame_support::ensure!( | ||
StorageVersion::get() == Some(Releases::V2), | ||
"💸 must upgrade to Releases::V2" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |