Skip to content

Commit

Permalink
fix: 🐛 add StorageVersion (#1670)
Browse files Browse the repository at this point in the history
* fix: 🐛 add StorageVersion

* fix: 🐛 post_upgrade
  • Loading branch information
yooml authored Feb 27, 2025
1 parent 90ee143 commit 82fd10b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ try-kusama-runtime-upgrade:build-try-runtime
--runtime \
target/release/wbuild/bifrost-kusama-runtime/bifrost_kusama_runtime.compact.compressed.wasm \
on-runtime-upgrade \
--disable-idempotency-checks \
live \
--uri wss://hk.bifrost-rpc.liebi.com:443/ws

Expand All @@ -149,7 +148,6 @@ try-polkadot-runtime-upgrade:build-try-runtime
--runtime \
target/release/wbuild/bifrost-polkadot-runtime/bifrost_polkadot_runtime.compact.compressed.wasm \
on-runtime-upgrade \
--disable-idempotency-checks \
live \
--uri wss://hk.p.bifrost-rpc.liebi.com:443/ws

Expand All @@ -163,7 +161,6 @@ try-polkadot-runtime-upgrade-snap:build-try-runtime
--runtime \
target/release/wbuild/bifrost-polkadot-runtime/bifrost_polkadot_runtime.compact.compressed.wasm \
on-runtime-upgrade \
--disable-idempotency-checks \
--checks=all \
snap -p [email protected]

Expand All @@ -177,7 +174,6 @@ try-kusama-runtime-upgrade-snap:build-try-runtime
--runtime \
target/release/wbuild/bifrost-kusama-runtime/bifrost_kusama_runtime.compact.compressed.wasm \
on-runtime-upgrade \
--disable-idempotency-checks \
--checks=all \
snap -p [email protected]

Expand Down
2 changes: 2 additions & 0 deletions pallets/fee-share/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ pub struct DollarStandardInfo<BlockNumberFor, AccountIdOf> {
pub mod pallet {
use super::*;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::config]
Expand Down
35 changes: 20 additions & 15 deletions pallets/fee-share/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

use super::{AutoEra, Config, DollarStandardInfos, Weight};
use super::{AutoEra, Config, DollarStandardInfos, Pallet, StorageVersion, Weight};
use frame_support::traits::{Get, OnRuntimeUpgrade};
use sp_std::marker::PhantomData;

Expand All @@ -41,21 +41,27 @@ impl<T: Config> OnRuntimeUpgrade for FeeShareOnRuntimeUpgrade<T> {
use frame_support::{migration, Identity};
log::info!("Bifrost `pre_upgrade`...");

let (era_length, _next_era) = AutoEra::<T>::get();
log::info!("Old era_length is {:?}", era_length);
assert_eq!(era_length, 7200u32.into());
if StorageVersion::get::<Pallet<T>>() == 0 {
let (era_length, _next_era) = AutoEra::<T>::get();
log::info!("Old era_length is {:?}", era_length);
assert_eq!(era_length, 7200u32.into());
}

Ok(sp_std::prelude::Vec::new())
}

fn on_runtime_upgrade() -> Weight {
log::info!("Bifrost `on_runtime_upgrade`...");

let weight = update_for_async::<T>();

log::info!("Bifrost `on_runtime_upgrade finished`");

weight
if StorageVersion::get::<Pallet<T>>() == 0 {
let weight = update_for_async::<T>();
log::info!("Migrating fee-share storage to v1");
StorageVersion::new(1).put::<Pallet<T>>();
weight
} else {
log::warn!("fee-share migration should be removed.");
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
Expand All @@ -64,12 +70,11 @@ impl<T: Config> OnRuntimeUpgrade for FeeShareOnRuntimeUpgrade<T> {
use frame_support::{migration, Identity};
log::info!("Bifrost `post_upgrade`...");

let (era_length, _next_era) = AutoEra::<T>::get();
log::info!("New era_length is {:?}", era_length);
assert_eq!(era_length, 14400u32.into());
DollarStandardInfos::<T>::iter().for_each(|(_distribution_id, info)| {
assert_eq!(info.interval, 432000u32.into());
});
if StorageVersion::get::<Pallet<T>>() == 1 {
let (era_length, _next_era) = AutoEra::<T>::get();
log::info!("New era_length is {:?}", era_length);
assert_eq!(era_length, 14400u32.into());
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/system-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub mod pallet {
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::without_storage_info]
Expand Down
37 changes: 24 additions & 13 deletions pallets/system-staking/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

#![cfg_attr(not(feature = "std"), no_std)]

use super::{Config, Round, Weight};
use frame_support::traits::{Get, OnRuntimeUpgrade};
use super::{Config, Pallet, Round, Weight};
use frame_support::{
pallet_prelude::StorageVersion,
traits::{Get, OnRuntimeUpgrade},
};
use sp_std::marker::PhantomData;

pub fn update_for_async<T: Config>() -> Weight {
Expand All @@ -39,9 +42,11 @@ impl<T: Config> OnRuntimeUpgrade for SystemStakingOnRuntimeUpgrade<T> {
use frame_support::{migration, Identity};
log::info!("Bifrost `pre_upgrade`...");

if let Some(round) = <Round<T>>::get() {
log::info!("Old round is {:?}", round);
assert_eq!(round.length, 1500u32);
if StorageVersion::get::<Pallet<T>>() == 1 {
if let Some(round) = <Round<T>>::get() {
log::info!("Old round is {:?}", round);
assert_eq!(round.length, 1500u32);
}
}

Ok(sp_std::prelude::Vec::new())
Expand All @@ -50,11 +55,15 @@ impl<T: Config> OnRuntimeUpgrade for SystemStakingOnRuntimeUpgrade<T> {
fn on_runtime_upgrade() -> Weight {
log::info!("Bifrost `on_runtime_upgrade`...");

let weight = update_for_async::<T>();

log::info!("Bifrost `on_runtime_upgrade finished`");

weight
if StorageVersion::get::<Pallet<T>>() == 1 {
let weight = update_for_async::<T>();
log::info!("Migrating system-staking storage to v2");
StorageVersion::new(2).put::<Pallet<T>>();
weight
} else {
log::warn!("system-staking migration should be removed.");
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
Expand All @@ -63,9 +72,11 @@ impl<T: Config> OnRuntimeUpgrade for SystemStakingOnRuntimeUpgrade<T> {
use frame_support::{migration, Identity};
log::info!("Bifrost `post_upgrade`...");

if let Some(round) = <Round<T>>::get() {
log::info!("New round is {:?}", round);
assert_eq!(round.length, 3000u32);
if StorageVersion::get::<Pallet<T>>() == 2 {
if let Some(round) = <Round<T>>::get() {
log::info!("New round is {:?}", round);
assert_eq!(round.length, 3000u32);
}
}

Ok(())
Expand Down

0 comments on commit 82fd10b

Please sign in to comment.