Skip to content

Commit

Permalink
Migrations
Browse files Browse the repository at this point in the history
Bump spec version to 7
  • Loading branch information
kacperzuk-neti committed Feb 14, 2023
1 parent 7f2f75d commit cb3b214
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bin/node/cli/tests/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async fn telemetry_works() {

// Stop the process
kill(Pid::from_raw(substrate.id().try_into().unwrap()), SIGINT).unwrap();
assert!(common::wait_for(&mut substrate, 60).map(|x| x.success()).unwrap_or_default());
assert!(common::wait_for(&mut substrate, 120).map(|x| x.success()).unwrap_or_default());

let output = substrate.wait_with_output().unwrap();

Expand Down
67 changes: 66 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 6,
spec_version: 7,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -1677,10 +1677,75 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;


// staking is only expected to be used by polkadot/kusama/et al., so they didn't
// bother to bump the default storage version. as such, we have V7_0_0 version
// set, but it's actually the layout of V12. Fix it before running V13 migration.
mod staking_v12 {
use super::*;
use frame_support::{storage_alias, traits::OnRuntimeUpgrade, pallet_prelude::*};

#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
enum ObsoleteReleases {
V1_0_0Ancient,
V2_0_0,
V3_0_0,
V4_0_0,
V5_0_0,
V6_0_0,
V7_0_0,
V8_0_0,
V9_0_0,
V10_0_0,
V11_0_0,
V12_0_0,
}

impl Default for ObsoleteReleases {
fn default() -> Self {
Self::V12_0_0
}
}

#[storage_alias]
type StorageVersion<T: pallet_staking::Config> = StorageValue<pallet_staking::Pallet<T>, ObsoleteReleases, ValueQuery>;

pub struct Migration<T>(sp_std::marker::PhantomData<T>);
impl<T: pallet_staking::Config> OnRuntimeUpgrade for Migration<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
frame_support::ensure!(
StorageVersion::<T>::get() == ObsoleteReleases::V7_0_0,
"Expected v7 before upgrading to v12"
);

Ok(Default::default())
}

fn on_runtime_upgrade() -> Weight {
StorageVersion::<T>::put(ObsoleteReleases::V12_0_0);
log::info!("Migrated pallet-staking StorageVersion to V12");
T::DbWeight::get().reads_writes(1, 1)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
frame_support::ensure!(
StorageVersion::<T>::get() == ObsoleteReleases::V12_0_0,
"Failed to upgrade to v12"
);
Ok(())
}
}
}

// All migrations executed on runtime upgrade as a nested tuple of types implementing
// `OnRuntimeUpgrade`.
type Migrations = (
pallet_contracts::Migration<Runtime>,
pallet_referenda::migration::v1::MigrateV0ToV1<Runtime>,
staking_v12::Migration<Runtime>,
pallet_staking::migrations::v13::MigrateToV13<Runtime>,
);

/// MMR helper types.
Expand Down
16 changes: 1 addition & 15 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ use frame_support::{
ensure,
traits::{
defensive_prelude::*,
EnsureOrigin, OnRuntimeUpgrade,
EnsureOrigin,
schedule::{v3::Named as ScheduleNamed, DispatchTime},
Bounded, Currency, Get, LockIdentifier, LockableCurrency, QueryPreimage,
ReservableCurrency, StorePreimage,
Expand Down Expand Up @@ -559,20 +559,6 @@ pub mod pallet {
fn on_initialize(n: T::BlockNumber) -> Weight {
Self::begin_block(n)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
migrations::v3::Migration::<T>::pre_upgrade()
}

fn on_runtime_upgrade() -> Weight {
migrations::v3::Migration::<T>::on_runtime_upgrade()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
migrations::v3::Migration::<T>::post_upgrade(state)
}
}

#[pallet::call]
Expand Down
16 changes: 1 addition & 15 deletions frame/llm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub mod pallet {
use super::*;
use frame_support::{
pallet_prelude::{DispatchResult, *},
traits::{OnRuntimeUpgrade, fungibles::Mutate},
traits::fungibles::Mutate,
PalletId,
};
use frame_system::{ensure_signed, pallet_prelude::*};
Expand Down Expand Up @@ -314,20 +314,6 @@ pub mod pallet {
Self::maybe_release(blocknumber);
Weight::zero()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
migrations::v1::Migration::<T>::pre_upgrade()
}

fn on_runtime_upgrade() -> Weight {
migrations::v1::Migration::<T>::on_runtime_upgrade()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
migrations::v1::Migration::<T>::post_upgrade(state)
}
}

#[pallet::call]
Expand Down

0 comments on commit cb3b214

Please sign in to comment.