Skip to content

Commit 870a457

Browse files
authored
fix: add required migration (#856)
I added the required migration for the new deployments, and made the `BumpStorageVersion` type a bit more generic to work with any pallets that might need the same treatment in the future. I tried the new migrations with both Peregrine (new runtime) and Spiritnet (previous runtime), and I got no errors.
1 parent 5494f07 commit 870a457

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

runtimes/common/src/migrations.rs

+33-19
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,69 @@
1717
// If you feel like getting in touch with us, you can do so at info@botlabs.org
1818

1919
use frame_support::{
20-
pallet_prelude::StorageVersion,
21-
traits::{GetStorageVersion, OnRuntimeUpgrade},
20+
traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion},
2221
weights::Weight,
2322
};
2423
use sp_core::Get;
25-
use sp_std::marker::PhantomData;
24+
use sp_std::{fmt::Debug, marker::PhantomData};
25+
use sp_weights::RuntimeDbWeight;
2626

2727
const LOG_TARGET: &str = "migration::BumpStorageVersion";
2828

2929
/// There are some pallets without a storage version.
3030
/// Based on the changes in the PR <https://github.com/paritytech/substrate/pull/13417>,
3131
/// pallets without a storage version or with a wrong version throw an error
3232
/// in the try state tests.
33-
pub struct BumpStorageVersion<T>(PhantomData<T>);
34-
35-
const TARGET_PALLET_ASSETS_STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
33+
pub struct BumpStorageVersion<T, W>(PhantomData<(T, W)>)
34+
where
35+
T: GetStorageVersion + PalletInfoAccess,
36+
T::CurrentStorageVersion: Debug + Into<StorageVersion>,
37+
StorageVersion: PartialOrd<T::CurrentStorageVersion>,
38+
W: Get<RuntimeDbWeight>;
3639

37-
impl<T> OnRuntimeUpgrade for BumpStorageVersion<T>
40+
impl<T, W> OnRuntimeUpgrade for BumpStorageVersion<T, W>
3841
where
39-
T: pallet_assets::Config,
42+
T: GetStorageVersion + PalletInfoAccess,
43+
T::CurrentStorageVersion: Debug + Into<StorageVersion>,
44+
StorageVersion: PartialOrd<T::CurrentStorageVersion>,
45+
W: Get<RuntimeDbWeight>,
4046
{
4147
#[cfg(feature = "try-runtime")]
4248
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, sp_runtime::TryRuntimeError> {
43-
if pallet_assets::Pallet::<T>::on_chain_storage_version() < TARGET_PALLET_ASSETS_STORAGE_VERSION {
44-
log::trace!(target: LOG_TARGET, "pallet_assets to be migrated to v1.");
49+
let (on_chain_version, current_version) = (T::on_chain_storage_version(), T::current_storage_version());
50+
let pallet_name = T::name();
51+
if on_chain_version < current_version {
52+
log::trace!(target: LOG_TARGET, "Pallet {:?} to be migrated from version {:?} to version {:?}.", pallet_name, on_chain_version, current_version);
4553
} else {
46-
log::trace!(target: LOG_TARGET, "pallet_assets already on v1. No migration will run.");
54+
log::trace!(target: LOG_TARGET, "Pallet {:?} already on latest version {:?}. No migration will run.", pallet_name, current_version);
4755
}
4856
Ok([].into())
4957
}
5058

5159
fn on_runtime_upgrade() -> Weight {
5260
log::info!(target: LOG_TARGET, "Initiating migration.");
5361

54-
if pallet_assets::Pallet::<T>::on_chain_storage_version() < TARGET_PALLET_ASSETS_STORAGE_VERSION {
55-
log::info!(target: LOG_TARGET, "pallet_assets to be migrated to v1.");
56-
TARGET_PALLET_ASSETS_STORAGE_VERSION.put::<pallet_assets::Pallet<T>>();
57-
<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
62+
let (on_chain_version, current_version) = (T::on_chain_storage_version(), T::current_storage_version());
63+
let pallet_name = T::name();
64+
65+
if on_chain_version < current_version {
66+
log::trace!(target: LOG_TARGET, "Pallet {:?} to be migrated from version {:?} to version {:?}.", pallet_name, on_chain_version, current_version);
67+
current_version.into().put::<T>();
68+
W::get().reads_writes(1, 1)
5869
} else {
59-
log::info!(target: LOG_TARGET, "pallet_assets already on v1. No migration will run.");
60-
<T as frame_system::Config>::DbWeight::get().reads(1)
70+
log::trace!(target: LOG_TARGET, "Pallet {:?} already on latest version {:?}. No migration will run.", pallet_name, current_version);
71+
W::get().reads(1)
6172
}
6273
}
6374

6475
#[cfg(feature = "try-runtime")]
6576
fn post_upgrade(_state: sp_std::vec::Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
66-
if pallet_assets::Pallet::<T>::on_chain_storage_version() < TARGET_PALLET_ASSETS_STORAGE_VERSION {
77+
let (on_chain_version, current_version) = (T::on_chain_storage_version(), T::current_storage_version());
78+
79+
if on_chain_version < current_version {
80+
log::error!(target: LOG_TARGET, "Storage version for pallet {:?} was not updated to the latest version {:?}.", T::name(), current_version);
6781
Err(sp_runtime::TryRuntimeError::Other(
68-
"pallet_assets storage version was not updated to v1.",
82+
"Pallet storage version was not updated to the latest version.",
6983
))
7084
} else {
7185
Ok(())

runtimes/peregrine/src/migrations/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
// If you feel like getting in touch with us, you can do so at info@botlabs.org
1818

1919
use frame_support::parameter_types;
20-
use runtime_common::constants;
20+
use runtime_common::{constants, migrations::BumpStorageVersion};
2121

22-
use crate::{weights, Balances, Runtime, RuntimeEvent};
22+
use crate::{
23+
weights::{self, rocksdb_weights::constants::RocksDbWeight},
24+
Balances, DotNames, Runtime, RuntimeEvent, UniqueLinking,
25+
};
2326

2427
parameter_types! {
2528
pub const DmpPalletName: &'static str = "DmpQueue";
@@ -28,6 +31,8 @@ parameter_types! {
2831
pub type RuntimeMigrations = (
2932
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
3033
frame_support::migrations::RemovePallet<DmpPalletName, <Runtime as frame_system::Config>::DbWeight>,
34+
BumpStorageVersion<DotNames, RocksDbWeight>,
35+
BumpStorageVersion<UniqueLinking, RocksDbWeight>,
3136
);
3237

3338
impl pallet_migration::Config for Runtime {

runtimes/spiritnet/src/migrations/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717
// If you feel like getting in touch with us, you can do so at info@botlabs.org
1818

1919
use frame_support::parameter_types;
20-
use runtime_common::constants;
20+
use runtime_common::{constants, migrations::BumpStorageVersion};
2121

22-
use crate::{weights, Balances, Runtime, RuntimeEvent};
22+
use crate::{
23+
weights::{self, rocksdb_weights::constants::RocksDbWeight},
24+
Balances, DotNames, Runtime, RuntimeEvent, UniqueLinking,
25+
};
2326

2427
parameter_types! {
2528
pub const DmpPalletName: &'static str = "DmpQueue";
2629
}
2730

2831
pub type RuntimeMigrations = (
29-
frame_support::migrations::RemovePallet<DmpPalletName, <Runtime as frame_system::Config>::DbWeight>,
3032
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
33+
frame_support::migrations::RemovePallet<DmpPalletName, <Runtime as frame_system::Config>::DbWeight>,
34+
BumpStorageVersion<DotNames, RocksDbWeight>,
35+
BumpStorageVersion<UniqueLinking, RocksDbWeight>,
3136
);
3237

3338
impl pallet_migration::Config for Runtime {

0 commit comments

Comments
 (0)