mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 03:58:04 +00:00
e54279699b
closes #1324 #### Problem Currently, it is possible to accidentally use inner unversioned migration instead of `VersionedMigration` since both implement `OnRuntimeUpgrade`. #### Solution With this change, we make it clear that value of `Inner` is not intended to be used directly. It is achieved by bounding `Inner` to new trait `UncheckedOnRuntimeUpgrade`, which has the same interface (except `unchecked_` prefix) as `OnRuntimeUpgrade`. #### `try-runtime` functions Since developers can implement `try-runtime` for `Inner` value in `VersionedMigration` and have custom logic for it, I added the same `try-runtime` functions to `UncheckedOnRuntimeUpgrade`. I looked for a ways to not duplicate functions, but couldn't find anything that doesn't significantly change the codebase. So I would appreciate If you have any suggestions to improve this cc @liamaharon @xlc polkadot address: 16FqwPZ8GRC5U5D4Fu7W33nA55ZXzXGWHwmbnE1eT6pxuqcT --------- Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
127 lines
3.9 KiB
Rust
127 lines
3.9 KiB
Rust
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Storage migrations for the Identity pallet.
|
|
|
|
use super::*;
|
|
use frame_support::{
|
|
migrations::VersionedMigration, pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade,
|
|
};
|
|
|
|
#[cfg(feature = "try-runtime")]
|
|
use codec::{Decode, Encode};
|
|
#[cfg(feature = "try-runtime")]
|
|
use sp_runtime::TryRuntimeError;
|
|
|
|
pub mod versioned {
|
|
use super::*;
|
|
|
|
pub type V0ToV1<T, const KL: u64> = VersionedMigration<
|
|
0,
|
|
1,
|
|
v1::VersionUncheckedMigrateV0ToV1<T, KL>,
|
|
crate::pallet::Pallet<T>,
|
|
<T as frame_system::Config>::DbWeight,
|
|
>;
|
|
}
|
|
|
|
pub mod v1 {
|
|
use super::*;
|
|
|
|
/// The log target.
|
|
const TARGET: &'static str = "runtime::identity::migration::v1";
|
|
|
|
/// The old identity type, useful in pre-upgrade.
|
|
mod v0 {
|
|
use super::*;
|
|
use frame_support::storage_alias;
|
|
|
|
#[storage_alias]
|
|
pub type IdentityOf<T: Config> = StorageMap<
|
|
Pallet<T>,
|
|
Twox64Concat,
|
|
<T as frame_system::Config>::AccountId,
|
|
Registration<
|
|
BalanceOf<T>,
|
|
<T as pallet::Config>::MaxRegistrars,
|
|
<T as pallet::Config>::IdentityInformation,
|
|
>,
|
|
OptionQuery,
|
|
>;
|
|
}
|
|
|
|
/// Migration to add usernames to Identity info.
|
|
///
|
|
/// `T` is the runtime and `KL` is the key limit to migrate. This is just a safety guard to
|
|
/// prevent stalling a parachain by accumulating too much weight in the migration. To have an
|
|
/// unlimited migration (e.g. in a chain without PoV limits), set this to `u64::MAX`.
|
|
pub struct VersionUncheckedMigrateV0ToV1<T, const KL: u64>(PhantomData<T>);
|
|
impl<T: Config, const KL: u64> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateV0ToV1<T, KL> {
|
|
#[cfg(feature = "try-runtime")]
|
|
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
|
|
let identities = v0::IdentityOf::<T>::iter().count();
|
|
log::info!(
|
|
target: TARGET,
|
|
"pre-upgrade state contains '{}' identities.",
|
|
identities
|
|
);
|
|
ensure!((identities as u64) < KL, "too many identities to migrate");
|
|
Ok((identities as u64).encode())
|
|
}
|
|
|
|
fn on_runtime_upgrade() -> Weight {
|
|
log::info!(
|
|
target: TARGET,
|
|
"running storage migration from version 0 to version 1."
|
|
);
|
|
|
|
let mut weight = T::DbWeight::get().reads(1);
|
|
let mut translated: u64 = 0;
|
|
let mut interrupted = false;
|
|
|
|
for (account, registration) in v0::IdentityOf::<T>::iter() {
|
|
IdentityOf::<T>::insert(account, (registration, None::<Username<T>>));
|
|
translated.saturating_inc();
|
|
if translated >= KL {
|
|
log::warn!(
|
|
"Incomplete! Migration limit reached. Only {} identities migrated.",
|
|
translated
|
|
);
|
|
interrupted = true;
|
|
break
|
|
}
|
|
}
|
|
if !interrupted {
|
|
log::info!("all {} identities migrated", translated);
|
|
}
|
|
|
|
weight.saturating_accrue(T::DbWeight::get().reads_writes(translated, translated));
|
|
weight.saturating_accrue(T::DbWeight::get().writes(1));
|
|
weight
|
|
}
|
|
|
|
#[cfg(feature = "try-runtime")]
|
|
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
|
|
let identities_to_migrate: u64 = Decode::decode(&mut &state[..])
|
|
.expect("failed to decode the state from pre-upgrade.");
|
|
let identities = IdentityOf::<T>::iter().count() as u64;
|
|
log::info!("post-upgrade expects '{}' identities to have been migrated.", identities);
|
|
ensure!(identities_to_migrate == identities, "must migrate all identities.");
|
|
log::info!(target: TARGET, "migrated all identities.");
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|