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

Contracts: Better migration types #14418

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions frame/contracts/src/migration/v10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use sp_core::hexdisplay::HexDisplay;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use sp_runtime::{traits::Zero, Perbill, Saturating};
use sp_std::{marker::PhantomData, ops::Deref, prelude::*};
use sp_std::{ops::Deref, prelude::*};

mod old {
use super::*;
Expand Down Expand Up @@ -109,8 +109,7 @@ pub struct ContractInfo<T: Config> {

#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)]
pub struct Migration<T: Config> {
last_key: Option<BoundedVec<u8, ConstU32<256>>>,
_phantom: PhantomData<T>,
last_account: Option<T::AccountId>,
}

#[storage_alias]
Expand All @@ -125,8 +124,10 @@ impl<T: Config> MigrationStep for Migration<T> {
}

fn step(&mut self) -> (IsFinished, Weight) {
let mut iter = if let Some(last_key) = self.last_key.take() {
old::ContractInfoOf::<T>::iter_from(last_key.to_vec())
let mut iter = if let Some(last_account) = self.last_account.take() {
old::ContractInfoOf::<T>::iter_from(old::ContractInfoOf::<T>::hashed_key_for(
last_account,
))
} else {
old::ContractInfoOf::<T>::iter()
};
Expand All @@ -135,9 +136,6 @@ impl<T: Config> MigrationStep for Migration<T> {
let min_balance = Pallet::<T>::min_balance();
log::debug!(target: LOG_TARGET, "Account: 0x{} ", HexDisplay::from(&account.encode()));

// Store last key for next migration step
self.last_key = Some(iter.last_raw_key().to_vec().try_into().unwrap());

// Get the new deposit account address
let deposit_account: DepositAccount<T> =
DepositAccount(T::AddressGenerator::deposit_address(&account));
Expand Down Expand Up @@ -223,6 +221,10 @@ impl<T: Config> MigrationStep for Migration<T> {
};

ContractInfoOf::<T>::insert(&account, new_contract_info);

// Store last key for next migration step
self.last_account = Some(account);

(IsFinished::No, T::WeightInfo::v10_migration_step())
} else {
log::debug!(target: LOG_TARGET, "Done Migrating contract info");
Expand Down
15 changes: 6 additions & 9 deletions frame/contracts/src/migration/v9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ use crate::{
CodeHash, Config, Determinism, Pallet, Weight, LOG_TARGET,
};
use codec::{Decode, Encode};
use frame_support::{
codec, pallet_prelude::*, storage_alias, BoundedVec, DefaultNoBound, Identity,
};
use frame_support::{codec, pallet_prelude::*, storage_alias, DefaultNoBound, Identity};
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use sp_std::{marker::PhantomData, prelude::*};
use sp_std::prelude::*;

mod old {
use super::*;
Expand Down Expand Up @@ -79,8 +77,7 @@ type CodeStorage<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, Prefa

#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)]
pub struct Migration<T: Config> {
last_key: Option<BoundedVec<u8, ConstU32<256>>>,
_phantom: PhantomData<T>,
last_code_hash: Option<CodeHash<T>>,
}

impl<T: Config> MigrationStep for Migration<T> {
Expand All @@ -91,8 +88,8 @@ impl<T: Config> MigrationStep for Migration<T> {
}

fn step(&mut self) -> (IsFinished, Weight) {
let mut iter = if let Some(last_key) = self.last_key.take() {
old::CodeStorage::<T>::iter_from(last_key.to_vec())
let mut iter = if let Some(last_key) = self.last_code_hash.take() {
old::CodeStorage::<T>::iter_from(old::CodeStorage::<T>::hashed_key_for(last_key))
} else {
old::CodeStorage::<T>::iter()
};
Expand All @@ -108,7 +105,7 @@ impl<T: Config> MigrationStep for Migration<T> {
determinism: Determinism::Enforced,
};
CodeStorage::<T>::insert(key, module);
self.last_key = Some(iter.last_raw_key().to_vec().try_into().unwrap());
self.last_code_hash = Some(key);
(IsFinished::No, T::WeightInfo::v9_migration_step(len))
} else {
log::debug!(target: LOG_TARGET, "No more contracts code to migrate");
Expand Down
Loading