From c9084ee638f4552dfc9678b776021dd791503960 Mon Sep 17 00:00:00 2001 From: Yi Date: Thu, 28 Jul 2022 23:07:55 +0800 Subject: [PATCH 01/10] STORAGE_VERSION on manta's pallet Signed-off-by: Yi --- pallets/asset-manager/src/lib.rs | 6 ++++++ primitives/manta/src/lib.rs | 1 + primitives/manta/src/migration.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 primitives/manta/src/migration.rs diff --git a/pallets/asset-manager/src/lib.rs b/pallets/asset-manager/src/lib.rs index 8c58a65ba..868c76f81 100644 --- a/pallets/asset-manager/src/lib.rs +++ b/pallets/asset-manager/src/lib.rs @@ -30,6 +30,8 @@ mod benchmarking; pub mod migrations; pub mod weights; + +use frame_support::traits::StorageVersion; pub use crate::weights::WeightInfo; pub use pallet::*; @@ -40,6 +42,8 @@ mod mock; #[cfg(test)] mod tests; +const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + #[frame_support::pallet] pub mod pallet { @@ -59,6 +63,7 @@ pub mod pallet { ArithmeticError, }; use xcm::latest::prelude::*; + use super::STORAGE_VERSION; /// Alias for the junction Parachain(#[codec(compact)] u32), pub(crate) type ParaId = u32; @@ -67,6 +72,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] #[pallet::without_storage_info] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); /// Convert AssetId and AssetLocation diff --git a/primitives/manta/src/lib.rs b/primitives/manta/src/lib.rs index 7ac86c79c..c85872e6a 100644 --- a/primitives/manta/src/lib.rs +++ b/primitives/manta/src/lib.rs @@ -23,3 +23,4 @@ pub mod assets; pub mod constants; pub mod types; pub mod xcm; +pub mod migration; diff --git a/primitives/manta/src/migration.rs b/primitives/manta/src/migration.rs new file mode 100644 index 000000000..70c21b94d --- /dev/null +++ b/primitives/manta/src/migration.rs @@ -0,0 +1,26 @@ +// Copyright 2020-2022 Manta Network. +// This file is part of Manta. +// +// Manta is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Manta is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Manta. If not, see . + + +//! Migration creates some helper function to make storage migration more convenient. + +/// MigratePalletPv2Sv means a wrapped handler to automatically upgrade our pallet +/// from PalletVersion(Pv) to StorageVersion(Sv). +/// +/// It's actually a simple rewriting about storage flag: delete [pallet_name] + '__STORAGE_VERSION__' key +/// and reset [pallet_name] + '__PALLET_VERSION__' key. +/// So It's a one-time job, and should be removed soon to minimize runtime size. +pub struct MigratePalletPv2Sv; \ No newline at end of file From df8a1fdacec98278b899cd2cf9535210a1cb15e1 Mon Sep 17 00:00:00 2001 From: Yi Date: Fri, 29 Jul 2022 22:13:51 +0800 Subject: [PATCH 02/10] [no ci] implement manta pallets migration Signed-off-by: Yi --- pallets/asset-manager/src/lib.rs | 10 ++++------ pallets/manta-pay/src/lib.rs | 4 ++++ pallets/tx-pause/src/lib.rs | 4 ++++ pallets/vesting/src/lib.rs | 4 ++++ primitives/manta/src/migration.rs | 21 +++++++++++++++++++-- runtime/calamari/src/lib.rs | 14 +++++++++++++- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/pallets/asset-manager/src/lib.rs b/pallets/asset-manager/src/lib.rs index 868c76f81..c4f73f475 100644 --- a/pallets/asset-manager/src/lib.rs +++ b/pallets/asset-manager/src/lib.rs @@ -30,10 +30,7 @@ mod benchmarking; pub mod migrations; pub mod weights; - -use frame_support::traits::StorageVersion; pub use crate::weights::WeightInfo; - pub use pallet::*; #[cfg(test)] @@ -42,13 +39,12 @@ mod mock; #[cfg(test)] mod tests; -const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); - #[frame_support::pallet] pub mod pallet { use crate::weights::WeightInfo; use frame_support::{pallet_prelude::*, traits::Contains, transactional, PalletId}; + use frame_support::traits::StorageVersion; use frame_system::pallet_prelude::*; use manta_primitives::{ assets::{ @@ -63,7 +59,9 @@ pub mod pallet { ArithmeticError, }; use xcm::latest::prelude::*; - use super::STORAGE_VERSION; + + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + /// Alias for the junction Parachain(#[codec(compact)] u32), pub(crate) type ParaId = u32; diff --git a/pallets/manta-pay/src/lib.rs b/pallets/manta-pay/src/lib.rs index ebecaec6c..ba488eee6 100644 --- a/pallets/manta-pay/src/lib.rs +++ b/pallets/manta-pay/src/lib.rs @@ -110,12 +110,16 @@ pub mod runtime; pub mod pallet { use super::*; use frame_support::pallet_prelude::*; + use frame_support::traits::StorageVersion; use frame_system::pallet_prelude::*; use sp_runtime::traits::AccountIdConversion; + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + /// Pallet #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); /// The module configuration trait. diff --git a/pallets/tx-pause/src/lib.rs b/pallets/tx-pause/src/lib.rs index 3c2d311c5..dcd99ca6f 100644 --- a/pallets/tx-pause/src/lib.rs +++ b/pallets/tx-pause/src/lib.rs @@ -42,8 +42,11 @@ pub use weights::WeightInfo; #[frame_support::pallet] pub mod pallet { + use frame_support::traits::StorageVersion; use super::*; + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + #[pallet::config] pub trait Config: frame_system::Config { type Event: From> + IsType<::Event>; @@ -82,6 +85,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::without_storage_info] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); #[pallet::hooks] diff --git a/pallets/vesting/src/lib.rs b/pallets/vesting/src/lib.rs index ff8d5f0de..f5288fee5 100644 --- a/pallets/vesting/src/lib.rs +++ b/pallets/vesting/src/lib.rs @@ -48,8 +48,11 @@ const VESTING_ID: LockIdentifier = *b"calamvst"; #[frame_support::pallet] pub mod pallet { + use frame_support::traits::StorageVersion; use super::*; + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + #[pallet::config] pub trait Config: frame_system::Config { /// The overarching event type. @@ -111,6 +114,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] #[pallet::without_storage_info] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); #[pallet::event] diff --git a/primitives/manta/src/migration.rs b/primitives/manta/src/migration.rs index 70c21b94d..61bd4e046 100644 --- a/primitives/manta/src/migration.rs +++ b/primitives/manta/src/migration.rs @@ -14,13 +14,30 @@ // You should have received a copy of the GNU General Public License // along with Manta. If not, see . - //! Migration creates some helper function to make storage migration more convenient. +use frame_support::dispatch::Weight; +use frame_support::migrations::migrate_from_pallet_version_to_storage_version; +use frame_support::traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}; +use frame_support::weights::constants::RocksDbWeight; +use frame_system::Config; +use sp_std::marker::PhantomData; + /// MigratePalletPv2Sv means a wrapped handler to automatically upgrade our pallet /// from PalletVersion(Pv) to StorageVersion(Sv). /// /// It's actually a simple rewriting about storage flag: delete [pallet_name] + '__STORAGE_VERSION__' key /// and reset [pallet_name] + '__PALLET_VERSION__' key. /// So It's a one-time job, and should be removed soon to minimize runtime size. -pub struct MigratePalletPv2Sv; \ No newline at end of file +pub struct MigratePalletPv2Sv(PhantomData); + +impl OnRuntimeUpgrade for MigratePalletPv2Sv +where + T: GetStorageVersion + PalletInfoAccess, +{ + fn on_runtime_upgrade() -> Weight { + // let db_weight = ::DbWeight::get(); + let db_weight = RocksDbWeight::get(); + migrate_from_pallet_version_to_storage_version::(&db_weight) + } +} diff --git a/runtime/calamari/src/lib.rs b/runtime/calamari/src/lib.rs index 0b93e1ed4..d2c4d7158 100644 --- a/runtime/calamari/src/lib.rs +++ b/runtime/calamari/src/lib.rs @@ -74,6 +74,7 @@ pub mod xcm_config; use currency::*; use fee::WeightToFee; use impls::DealWithFees; +use manta_primitives::migration::MigratePalletPv2Sv; pub type NegativeImbalance = >::NegativeImbalance; @@ -768,6 +769,17 @@ pub type SignedExtra = ( pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; + +/// Types for runtime upgrading. +/// Each type should implement trait `OnRuntimeUpgrade`. +pub type OnRuntimeUpgradeHooks = ( + UpgradeSessionKeys, + MigratePalletPv2Sv>, + MigratePalletPv2Sv>, + MigratePalletPv2Sv>, + MigratePalletPv2Sv>, +); + /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< Runtime, @@ -775,7 +787,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsReversedWithSystemFirst, - UpgradeSessionKeys, + OnRuntimeUpgradeHooks >; // When this is removed, should also remove `OldSessionKeys`. pub struct UpgradeSessionKeys; From d4dbf47066a0c05c442e5783f43eba9c5e21d924 Mon Sep 17 00:00:00 2001 From: Yi Date: Fri, 29 Jul 2022 22:21:08 +0800 Subject: [PATCH 03/10] [no ci] implement manta pallets migration Signed-off-by: Yi --- pallets/asset-manager/src/lib.rs | 8 +++++--- pallets/manta-pay/src/lib.rs | 3 +-- pallets/tx-pause/src/lib.rs | 2 +- pallets/vesting/src/lib.rs | 2 +- runtime/calamari/src/lib.rs | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pallets/asset-manager/src/lib.rs b/pallets/asset-manager/src/lib.rs index c4f73f475..1a5b5f8ea 100644 --- a/pallets/asset-manager/src/lib.rs +++ b/pallets/asset-manager/src/lib.rs @@ -43,8 +43,11 @@ mod tests; pub mod pallet { use crate::weights::WeightInfo; - use frame_support::{pallet_prelude::*, traits::Contains, transactional, PalletId}; - use frame_support::traits::StorageVersion; + use frame_support::{ + pallet_prelude::*, + traits::{Contains, StorageVersion}, + transactional, PalletId, + }; use frame_system::pallet_prelude::*; use manta_primitives::{ assets::{ @@ -62,7 +65,6 @@ pub mod pallet { const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); - /// Alias for the junction Parachain(#[codec(compact)] u32), pub(crate) type ParaId = u32; pub(crate) type AssetCount = u32; diff --git a/pallets/manta-pay/src/lib.rs b/pallets/manta-pay/src/lib.rs index ba488eee6..f078113a4 100644 --- a/pallets/manta-pay/src/lib.rs +++ b/pallets/manta-pay/src/lib.rs @@ -109,8 +109,7 @@ pub mod runtime; #[frame_support::pallet] pub mod pallet { use super::*; - use frame_support::pallet_prelude::*; - use frame_support::traits::StorageVersion; + use frame_support::{pallet_prelude::*, traits::StorageVersion}; use frame_system::pallet_prelude::*; use sp_runtime::traits::AccountIdConversion; diff --git a/pallets/tx-pause/src/lib.rs b/pallets/tx-pause/src/lib.rs index dcd99ca6f..9c79a3ca8 100644 --- a/pallets/tx-pause/src/lib.rs +++ b/pallets/tx-pause/src/lib.rs @@ -42,8 +42,8 @@ pub use weights::WeightInfo; #[frame_support::pallet] pub mod pallet { - use frame_support::traits::StorageVersion; use super::*; + use frame_support::traits::StorageVersion; const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); diff --git a/pallets/vesting/src/lib.rs b/pallets/vesting/src/lib.rs index f5288fee5..42dd5ddcd 100644 --- a/pallets/vesting/src/lib.rs +++ b/pallets/vesting/src/lib.rs @@ -48,8 +48,8 @@ const VESTING_ID: LockIdentifier = *b"calamvst"; #[frame_support::pallet] pub mod pallet { - use frame_support::traits::StorageVersion; use super::*; + use frame_support::traits::StorageVersion; const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); diff --git a/runtime/calamari/src/lib.rs b/runtime/calamari/src/lib.rs index d2c4d7158..69b8e6a17 100644 --- a/runtime/calamari/src/lib.rs +++ b/runtime/calamari/src/lib.rs @@ -787,7 +787,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsReversedWithSystemFirst, - OnRuntimeUpgradeHooks + OnRuntimeUpgradeHooks, >; // When this is removed, should also remove `OldSessionKeys`. pub struct UpgradeSessionKeys; From 00d4dd057e6c7f28a6e6b888656f37f079f5d628 Mon Sep 17 00:00:00 2001 From: Yi Date: Fri, 29 Jul 2022 22:21:41 +0800 Subject: [PATCH 04/10] [no ci] implement manta pallets migration Signed-off-by: Yi --- primitives/manta/src/lib.rs | 2 +- primitives/manta/src/migration.rs | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/primitives/manta/src/lib.rs b/primitives/manta/src/lib.rs index c85872e6a..213c8376f 100644 --- a/primitives/manta/src/lib.rs +++ b/primitives/manta/src/lib.rs @@ -21,6 +21,6 @@ pub mod assets; pub mod constants; +pub mod migration; pub mod types; pub mod xcm; -pub mod migration; diff --git a/primitives/manta/src/migration.rs b/primitives/manta/src/migration.rs index 61bd4e046..009b76553 100644 --- a/primitives/manta/src/migration.rs +++ b/primitives/manta/src/migration.rs @@ -16,10 +16,12 @@ //! Migration creates some helper function to make storage migration more convenient. -use frame_support::dispatch::Weight; -use frame_support::migrations::migrate_from_pallet_version_to_storage_version; -use frame_support::traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}; -use frame_support::weights::constants::RocksDbWeight; +use frame_support::{ + dispatch::Weight, + migrations::migrate_from_pallet_version_to_storage_version, + traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, + weights::constants::RocksDbWeight, +}; use frame_system::Config; use sp_std::marker::PhantomData; From d5cfc54a7263e2e8a204525715898f7007d949e3 Mon Sep 17 00:00:00 2001 From: Yi Date: Fri, 29 Jul 2022 22:23:12 +0800 Subject: [PATCH 05/10] [no ci] implement manta pallets migration Signed-off-by: Yi --- primitives/manta/src/migration.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/primitives/manta/src/migration.rs b/primitives/manta/src/migration.rs index 009b76553..304b12f13 100644 --- a/primitives/manta/src/migration.rs +++ b/primitives/manta/src/migration.rs @@ -19,10 +19,9 @@ use frame_support::{ dispatch::Weight, migrations::migrate_from_pallet_version_to_storage_version, - traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, + traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, weights::constants::RocksDbWeight, }; -use frame_system::Config; use sp_std::marker::PhantomData; /// MigratePalletPv2Sv means a wrapped handler to automatically upgrade our pallet From 75c2ed5c0e22fc0a60c249cc532d411eb6e421c6 Mon Sep 17 00:00:00 2001 From: Yi Date: Mon, 1 Aug 2022 16:41:49 +0800 Subject: [PATCH 06/10] [no ci] update Signed-off-by: Yi --- primitives/manta/src/lib.rs | 1 - runtime/calamari/src/lib.rs | 2 +- runtime/common/src/lib.rs | 2 ++ {primitives/manta => runtime/common}/src/migration.rs | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) rename {primitives/manta => runtime/common}/src/migration.rs (98%) diff --git a/primitives/manta/src/lib.rs b/primitives/manta/src/lib.rs index 213c8376f..7ac86c79c 100644 --- a/primitives/manta/src/lib.rs +++ b/primitives/manta/src/lib.rs @@ -21,6 +21,5 @@ pub mod assets; pub mod constants; -pub mod migration; pub mod types; pub mod xcm; diff --git a/runtime/calamari/src/lib.rs b/runtime/calamari/src/lib.rs index 69b8e6a17..c990b433d 100644 --- a/runtime/calamari/src/lib.rs +++ b/runtime/calamari/src/lib.rs @@ -74,7 +74,7 @@ pub mod xcm_config; use currency::*; use fee::WeightToFee; use impls::DealWithFees; -use manta_primitives::migration::MigratePalletPv2Sv; +use runtime_common::migration::MigratePalletPv2Sv; pub type NegativeImbalance = >::NegativeImbalance; diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 3479e15ed..48f499e66 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -19,6 +19,8 @@ #![allow(clippy::upper_case_acronyms)] #![cfg_attr(not(feature = "std"), no_std)] +pub mod migration; + use frame_support::parameter_types; use manta_primitives::types::BlockNumber; use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; diff --git a/primitives/manta/src/migration.rs b/runtime/common/src/migration.rs similarity index 98% rename from primitives/manta/src/migration.rs rename to runtime/common/src/migration.rs index 304b12f13..899000747 100644 --- a/primitives/manta/src/migration.rs +++ b/runtime/common/src/migration.rs @@ -22,7 +22,7 @@ use frame_support::{ traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, weights::constants::RocksDbWeight, }; -use sp_std::marker::PhantomData; +use std::marker::PhantomData; /// MigratePalletPv2Sv means a wrapped handler to automatically upgrade our pallet /// from PalletVersion(Pv) to StorageVersion(Sv). From f7fd4cd98220a44804deabc26b99d3a5ab3f8d12 Mon Sep 17 00:00:00 2001 From: Yi Date: Mon, 1 Aug 2022 18:19:35 +0800 Subject: [PATCH 07/10] add an unit test. Signed-off-by: Yi --- runtime/common/Cargo.toml | 2 + runtime/common/src/migration.rs | 94 ++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index edf48ae36..d579e687d 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -12,6 +12,8 @@ version = '3.2.1' frame-support = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.22", default-features = false } pallet-transaction-payment = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.22", default-features = false } sp-runtime = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.22", default-features = false } +sp-std = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.22", default-features = false } + # Self dependencies manta-primitives = { path = '../../primitives/manta', default-features = false } diff --git a/runtime/common/src/migration.rs b/runtime/common/src/migration.rs index 899000747..6ae94e398 100644 --- a/runtime/common/src/migration.rs +++ b/runtime/common/src/migration.rs @@ -16,13 +16,16 @@ //! Migration creates some helper function to make storage migration more convenient. +#![cfg_attr(not(feature = "std"), no_std)] + use frame_support::{ dispatch::Weight, migrations::migrate_from_pallet_version_to_storage_version, traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, weights::constants::RocksDbWeight, }; -use std::marker::PhantomData; + +use sp_std::marker::PhantomData; /// MigratePalletPv2Sv means a wrapped handler to automatically upgrade our pallet /// from PalletVersion(Pv) to StorageVersion(Sv). @@ -37,8 +40,95 @@ where T: GetStorageVersion + PalletInfoAccess, { fn on_runtime_upgrade() -> Weight { - // let db_weight = ::DbWeight::get(); let db_weight = RocksDbWeight::get(); migrate_from_pallet_version_to_storage_version::(&db_weight) } } + +#[cfg(test)] +mod test { + use super::*; + use frame_support::traits::{CrateVersion, StorageInstance, StorageVersion}; + use sp_io::TestExternalities; + + pub struct DummyPrefix; + + impl StorageInstance for DummyPrefix { + fn pallet_prefix() -> &'static str { + "test_pv2sv" + } + + const STORAGE_PREFIX: &'static str = "foo"; + } + + // just used for below migration test. + // avoiding declare a huge Runtime part. + struct MockForMigrationTesting {} + + impl GetStorageVersion for MockForMigrationTesting { + fn current_storage_version() -> StorageVersion { + StorageVersion::new(10) + } + + fn on_chain_storage_version() -> StorageVersion { + StorageVersion::get::() + } + } + + impl PalletInfoAccess for MockForMigrationTesting { + fn index() -> usize { + 0 + } + + fn name() -> &'static str { + "test_pv_2_sv" + } + + fn module_name() -> &'static str { + "test_module_name" + } + + fn crate_version() -> CrateVersion { + CrateVersion { + major: 4, + minor: 0, + patch: 0, + } + } + } + + #[test] + fn test_pv_2_sv_works() { + // 1. write old pallet version into storage. + // 2. call utility + // 3. test whether it works. + const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:"; + fn pallet_version_key(name: &str) -> [u8; 32] { + frame_support::storage::storage_prefix( + name.as_bytes(), + PALLET_VERSION_STORAGE_KEY_POSTFIX, + ) + } + + let mut db = TestExternalities::default(); + db.execute_with(|| { + sp_io::storage::set( + &pallet_version_key(MockForMigrationTesting::name()), + &[1, 0, 0], + ); + assert_eq!( + MockForMigrationTesting::on_chain_storage_version(), + StorageVersion::new(0) + ); + let weight = MigratePalletPv2Sv::::on_runtime_upgrade(); + assert_eq!(100_000 * 1000 * 2, weight); + assert!( + sp_io::storage::get(&pallet_version_key(MockForMigrationTesting::name())).is_none() + ); + assert_eq!( + MockForMigrationTesting::on_chain_storage_version(), + StorageVersion::new(10) + ); + }) + } +} From f4af278f5d19804bcc5d394a4dd9d2f043cf03ff Mon Sep 17 00:00:00 2001 From: Yi Date: Thu, 4 Aug 2022 19:40:53 +0800 Subject: [PATCH 08/10] add dolphin migration Signed-off-by: Yi --- runtime/dolphin/src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/runtime/dolphin/src/lib.rs b/runtime/dolphin/src/lib.rs index 6c1246985..c51ebe601 100644 --- a/runtime/dolphin/src/lib.rs +++ b/runtime/dolphin/src/lib.rs @@ -75,6 +75,7 @@ pub mod xcm_config; use currency::*; use fee::WeightToFee; use impls::DealWithFees; +use runtime_common::migration::MigratePalletPv2Sv; pub type NegativeImbalance = >::NegativeImbalance; @@ -762,6 +763,17 @@ pub type SignedExtra = ( pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; + +/// Types for runtime upgrading. +/// Each type should implement trait `OnRuntimeUpgrade`. +pub type OnRuntimeUpgradeHooks = ( + UpgradeSessionKeys, + MigratePalletPv2Sv>, + MigratePalletPv2Sv>, + MigratePalletPv2Sv>, + MigratePalletPv2Sv>, +); + /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< Runtime, @@ -769,7 +781,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsReversedWithSystemFirst, - UpgradeSessionKeys, + OnRuntimeUpgradeHooks, >; // When this is removed, should also remove `OldSessionKeys`. pub struct UpgradeSessionKeys; From 394a0ca1c1cec7ad7d6320178266c9eae31de994 Mon Sep 17 00:00:00 2001 From: Yi Date: Fri, 5 Aug 2022 17:10:44 +0800 Subject: [PATCH 09/10] add try-runtime testing Signed-off-by: Yi --- runtime/calamari/Cargo.toml | 1 + runtime/common/Cargo.toml | 4 +++- runtime/common/src/migration.rs | 24 +++++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/runtime/calamari/Cargo.toml b/runtime/calamari/Cargo.toml index 167df8571..2392a574e 100644 --- a/runtime/calamari/Cargo.toml +++ b/runtime/calamari/Cargo.toml @@ -155,6 +155,7 @@ try-runtime = [ 'pallet-assets/try-runtime', 'pallet-tx-pause/try-runtime', 'pallet-asset-manager/try-runtime', + 'runtime-common/try-runtime', ] # Set timing constants (e.g. session period) to faster versions to speed up testing. fast-runtime = [] diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index c9a08de22..0b6baa033 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -14,7 +14,6 @@ pallet-transaction-payment = { git = 'https://github.com/paritytech/substrate.gi sp-runtime = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26", default-features = false } sp-std = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26", default-features = false } - # Self dependencies manta-primitives = { path = '../../primitives/manta', default-features = false } @@ -66,3 +65,6 @@ std = [ "pallet-transaction-payment/std", "sp-runtime/std", ] +try-runtime = [ + "frame-support/try-runtime", +] diff --git a/runtime/common/src/migration.rs b/runtime/common/src/migration.rs index 6ae94e398..ec19c0d6a 100644 --- a/runtime/common/src/migration.rs +++ b/runtime/common/src/migration.rs @@ -20,8 +20,9 @@ use frame_support::{ dispatch::Weight, + ensure, migrations::migrate_from_pallet_version_to_storage_version, - traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, + traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion}, weights::constants::RocksDbWeight, }; @@ -43,6 +44,27 @@ where let db_weight = RocksDbWeight::get(); migrate_from_pallet_version_to_storage_version::(&db_weight) } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + let storage_version = StorageVersion::get::(); + frame_support::debug(&"----PreUpgrade----"); + frame_support::debug(&T::module_name()); + frame_support::debug(&T::name()); + frame_support::debug(&storage_version); + Ok(()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + let storage_version = StorageVersion::get::(); + frame_support::debug(&"----PostUpgrade----"); + frame_support::debug(&T::module_name()); + frame_support::debug(&T::name()); + frame_support::debug(&storage_version); + ensure!(storage_version == StorageVersion::new(1), T::module_name()); + Ok(()) + } } #[cfg(test)] From 966740acb99f67fd397acdc55e981e8a8d1f5f93 Mon Sep 17 00:00:00 2001 From: Yi Date: Fri, 5 Aug 2022 17:48:35 +0800 Subject: [PATCH 10/10] fix Signed-off-by: Yi --- runtime/common/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 0b6baa033..c55e30aff 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -64,6 +64,7 @@ std = [ "manta-primitives/std", "pallet-transaction-payment/std", "sp-runtime/std", + "sp-std/std", ] try-runtime = [ "frame-support/try-runtime",