Skip to content

Commit

Permalink
Mainnet storage version migrations (#913)
Browse files Browse the repository at this point in the history
* 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
kostekIV and Marcin-Radecki authored Feb 21, 2023
1 parent 7d21e8e commit f875597
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph-node"
version = "0.8.3+mainnet"
version = "0.8.4+mainnet"
authors = ["Cardinal Cryptography"]
description = "Aleph node binary"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion bin/runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph-runtime"
version = "0.8.1+mainnet"
version = "0.8.4+mainnet"
authors = ["Cardinal Cryptography"]
edition = "2021"
homepage = "https://alephzero.org"
Expand Down
14 changes: 13 additions & 1 deletion bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

mod migrations;

pub use frame_support::{
construct_runtime, log, parameter_types,
traits::{
Expand Down Expand Up @@ -56,6 +58,10 @@ use sp_std::prelude::*;
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

use crate::migrations::{
HistoryMigrateToV1, SchedulerMigrateToV3, StakingMigrateToV10, TransactionPaymentMigrateToV2,
};

/// An index to a block.
pub type BlockNumber = u32;

Expand Down Expand Up @@ -104,7 +110,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("aleph-node"),
impl_name: create_runtime_str!("aleph-node"),
authoring_version: 1,
spec_version: 46,
spec_version: 47,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 14,
Expand Down Expand Up @@ -718,6 +724,12 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(
StakingMigrateToV10<Runtime>,
SchedulerMigrateToV3<Runtime>,
HistoryMigrateToV1<Runtime>,
TransactionPaymentMigrateToV2<Runtime>,
),
>;

impl_runtime_apis! {
Expand Down
35 changes: 35 additions & 0 deletions bin/runtime/src/migrations/history.rs
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(())
}
}
9 changes: 9 additions & 0 deletions bin/runtime/src/migrations/mod.rs
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;
70 changes: 70 additions & 0 deletions bin/runtime/src/migrations/scheduler.rs
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(())
}
}
82 changes: 82 additions & 0 deletions bin/runtime/src/migrations/staking.rs
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(())
}
}
75 changes: 75 additions & 0 deletions bin/runtime/src/migrations/transaction_payment.rs
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(())
}
}

0 comments on commit f875597

Please sign in to comment.