Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Move type Migrations to Config #14309

Merged
merged 12 commits into from
Jun 9, 2023
4 changes: 3 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use frame_system::{
pub use node_primitives::{AccountId, Signature};
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment};
use pallet_asset_conversion::{NativeOrAssetId, NativeOrAssetIdConverter};
use pallet_contracts::migration::{v10, v11, v9};
use pallet_election_provider_multi_phase::SolutionAccuracyOf;
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_nfts::PalletFeatures;
Expand Down Expand Up @@ -1240,6 +1241,7 @@ impl pallet_contracts::Config for Runtime {
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = ConstBool<false>;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
type Migrations = (v9::Migration<Runtime>, v10::Migration<Runtime>, v11::Migration<Runtime>);
}

impl pallet_sudo::Config for Runtime {
Expand Down Expand Up @@ -1981,7 +1983,7 @@ pub type Executive = frame_executive::Executive<
type Migrations = (
pallet_nomination_pools::migration::v2::MigrateToV2<Runtime>,
pallet_alliance::migration::Migration<Runtime>,
pallet_contracts::Migration<Runtime>,
pallet_contracts::Migration<Runtime, <Runtime as pallet_contracts::Config>::Migrations>,
);

type EventRecord = frame_system::EventRecord<
Expand Down
10 changes: 5 additions & 5 deletions frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ benchmarks! {
migration_noop {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 2);
}: {
Migration::<T>::migrate(Weight::MAX)
Migration::<T, T::Migrations>::migrate(Weight::MAX)
} verify {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 2);
}
Expand All @@ -281,7 +281,7 @@ benchmarks! {
#[pov_mode = Measured]
migrate {
StorageVersion::new(0).put::<Pallet<T>>();
<Migration::<T> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade();
<Migration::<T, T::Migrations> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade();
let origin: RawOrigin<<T as frame_system::Config>::AccountId> = RawOrigin::Signed(whitelisted_caller());
}: {
<Contracts<T>>::migrate(origin.into(), Weight::MAX).unwrap()
Expand All @@ -294,7 +294,7 @@ benchmarks! {
on_runtime_upgrade_noop {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 2);
}: {
<Migration::<T> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade()
<Migration::<T, T::Migrations> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade()
} verify {
assert!(MigrationInProgress::<T>::get().is_none());
}
Expand All @@ -306,7 +306,7 @@ benchmarks! {
let v = vec![42u8].try_into().ok();
MigrationInProgress::<T>::set(v.clone());
}: {
<Migration::<T> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade()
<Migration::<T, T::Migrations> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade()
} verify {
assert!(MigrationInProgress::<T>::get().is_some());
assert_eq!(MigrationInProgress::<T>::get(), v);
Expand All @@ -317,7 +317,7 @@ benchmarks! {
on_runtime_upgrade {
StorageVersion::new(0).put::<Pallet<T>>();
}: {
<Migration::<T> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade()
<Migration::<T, T::Migrations> as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade()
} verify {
assert!(MigrationInProgress::<T>::get().is_some());
}
Expand Down
29 changes: 16 additions & 13 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ mod address;
mod benchmarking;
mod exec;
mod gas;
mod migration;
mod schedule;
mod storage;
mod wasm;

pub mod chain_extension;
pub mod migration;
pub mod weights;

#[cfg(test)]
Expand Down Expand Up @@ -319,14 +319,17 @@ pub mod pallet {
/// The maximum length of the debug buffer in bytes.
#[pallet::constant]
type MaxDebugBufferLen: Get<u32>;

/// The sequence of migrations applied.
juangirini marked this conversation as resolved.
Show resolved Hide resolved
type Migrations: MigrateSequence;
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(_block: T::BlockNumber, remaining_weight: Weight) -> Weight {
use migration::MigrateResult::*;

let (result, weight) = Migration::<T>::migrate(remaining_weight);
let (result, weight) = Migration::<T, T::Migrations>::migrate(remaining_weight);
juangirini marked this conversation as resolved.
Show resolved Hide resolved
let remaining_weight = remaining_weight.saturating_sub(weight);

if !matches!(result, Completed | NoMigrationInProgress) {
Expand All @@ -338,7 +341,7 @@ pub mod pallet {
}

fn integrity_test() {
Migration::<T>::integrity_test();
Migration::<T, T::Migrations>::integrity_test();

// Total runtime memory limit
let max_runtime_mem: u32 = T::Schedule::get().limits.runtime_memory;
Expand Down Expand Up @@ -518,7 +521,7 @@ pub mod pallet {
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
determinism: Determinism,
) -> DispatchResult {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
let origin = ensure_signed(origin)?;
Self::bare_upload_code(origin, code, storage_deposit_limit.map(Into::into), determinism)
.map(|_| ())
Expand All @@ -534,7 +537,7 @@ pub mod pallet {
origin: OriginFor<T>,
code_hash: CodeHash<T>,
) -> DispatchResultWithPostInfo {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
let origin = ensure_signed(origin)?;
<PrefabWasmModule<T>>::remove(&origin, code_hash)?;
// we waive the fee because removing unused code is beneficial
Expand All @@ -558,7 +561,7 @@ pub mod pallet {
dest: AccountIdLookupOf<T>,
code_hash: CodeHash<T>,
) -> DispatchResult {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
ensure_root(origin)?;
let dest = T::Lookup::lookup(dest)?;
<ContractInfoOf<T>>::try_mutate(&dest, |contract| {
Expand Down Expand Up @@ -608,7 +611,7 @@ pub mod pallet {
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
data: Vec<u8>,
) -> DispatchResultWithPostInfo {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
let common = CommonInput {
origin: Origin::from_runtime_origin(origin)?,
value,
Expand Down Expand Up @@ -668,7 +671,7 @@ pub mod pallet {
data: Vec<u8>,
salt: Vec<u8>,
) -> DispatchResultWithPostInfo {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
let code_len = code.len() as u32;
let data_len = data.len() as u32;
let salt_len = salt.len() as u32;
Expand Down Expand Up @@ -711,7 +714,7 @@ pub mod pallet {
data: Vec<u8>,
salt: Vec<u8>,
) -> DispatchResultWithPostInfo {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
let data_len = data.len() as u32;
let salt_len = salt.len() as u32;
let common = CommonInput {
Expand Down Expand Up @@ -746,7 +749,7 @@ pub mod pallet {
ensure_signed(origin)?;

let weight_limit = weight_limit.saturating_add(T::WeightInfo::migrate());
let (result, weight) = Migration::<T>::migrate(weight_limit);
let (result, weight) = Migration::<T, T::Migrations>::migrate(weight_limit);

match result {
Completed =>
Expand Down Expand Up @@ -1272,7 +1275,7 @@ impl<T: Config> Invokable<T> for InstantiateInput<T> {

macro_rules! ensure_no_migration_in_progress {
() => {
if Migration::<T>::in_progress() {
if Migration::<T, T::Migrations>::in_progress() {
return ContractResult {
gas_consumed: Zero::zero(),
gas_required: Zero::zero(),
Expand Down Expand Up @@ -1412,7 +1415,7 @@ impl<T: Config> Pallet<T> {
storage_deposit_limit: Option<BalanceOf<T>>,
determinism: Determinism,
) -> CodeUploadResult<CodeHash<T>, BalanceOf<T>> {
Migration::<T>::ensure_migrated()?;
Migration::<T, T::Migrations>::ensure_migrated()?;
let schedule = T::Schedule::get();
let module = PrefabWasmModule::from_code(
code,
Expand All @@ -1433,7 +1436,7 @@ impl<T: Config> Pallet<T> {

/// Query storage of a specified contract under a specified key.
pub fn get_storage(address: T::AccountId, key: Vec<u8>) -> GetStorageResult {
if Migration::<T>::in_progress() {
if Migration::<T, T::Migrations>::in_progress() {
return Err(ContractAccessError::MigrationInProgress)
}
let contract_info =
Expand Down
18 changes: 1 addition & 17 deletions frame/contracts/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
//! Migration framework for pallets.

/// Macro to include all migration modules.
/// We only want to make these modules public when `runtime-benchmarks` is
/// enabled, so we can access migration code in benchmarks.
macro_rules! use_modules {
($($module:ident),*) => {
$(
#[cfg(feature = "runtime-benchmarks")]
pub mod $module;
#[cfg(not(feature = "runtime-benchmarks"))]
mod $module;
)*
};
}
juangirini marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -58,10 +53,6 @@ fn invalid_version(version: StorageVersion) -> ! {
/// The cursor used to store the state of the current migration step.
pub type Cursor = BoundedVec<u8, ConstU32<1024>>;

// In benchmark and tests we use noop migrations, to test and bench the migration framework itself.
#[cfg(not(any(feature = "runtime-benchmarks", test)))]
type Migrations<T> = (v9::Migration<T>, v10::Migration<T>, v11::Migration<T>);

/// IsFinished describes whether a migration is finished or not.
pub enum IsFinished {
Yes,
Expand Down Expand Up @@ -206,14 +197,7 @@ pub trait MigrateSequence: private::Sealed {
}

/// Performs all necessary migrations based on `StorageVersion`.
#[cfg(not(any(feature = "runtime-benchmarks", test)))]
pub struct Migration<T: Config, M: MigrateSequence = Migrations<T>>(PhantomData<(T, M)>);

/// Custom migration for running runtime-benchmarks and tests.
#[cfg(any(feature = "runtime-benchmarks", test))]
pub struct Migration<T: Config, M: MigrateSequence = (NoopMigration<1>, NoopMigration<2>)>(
PhantomData<(T, M)>,
);
juangirini marked this conversation as resolved.
Show resolved Hide resolved
juangirini marked this conversation as resolved.
Show resolved Hide resolved
pub struct Migration<T: Config, M: MigrateSequence>(PhantomData<(T, M)>);

#[cfg(feature = "try-runtime")]
impl<T: Config, M: MigrateSequence> Migration<T, M> {
Expand Down
5 changes: 3 additions & 2 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::{
wasm::{Determinism, PrefabWasmModule, ReturnCode as RuntimeReturnCode},
weights::WeightInfo,
BalanceOf, Code, CodeStorage, CollectEvents, Config, ContractInfo, ContractInfoOf, DebugInfo,
DefaultAddressGenerator, DeletionQueueCounter, Error, MigrationInProgress, Origin, Pallet,
Schedule,
DefaultAddressGenerator, DeletionQueueCounter, Error, MigrationInProgress, NoopMigration,
Origin, Pallet, Schedule,
};
use assert_matches::assert_matches;
use codec::Encode;
Expand Down Expand Up @@ -428,6 +428,7 @@ impl Config for Test {
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = UnstableInterface;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
type Migrations = (NoopMigration<1>, NoopMigration<2>);
}

pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);
Expand Down