|
| 1 | +use frame_support::{ |
| 2 | + pallet_prelude::*, storage_alias, traits::fungible::Inspect, DefaultNoBound, Identity, |
| 3 | +}; |
| 4 | +use pallet_contracts::{ |
| 5 | + migration::{IsFinished, MigrationStep}, |
| 6 | + weights::WeightInfo, |
| 7 | + Config, Determinism, Pallet, |
| 8 | +}; |
| 9 | +use parity_scale_codec::{Decode, Encode}; |
| 10 | +use scale_info::prelude::format; |
| 11 | +#[cfg(feature = "try-runtime")] |
| 12 | +use sp_runtime::TryRuntimeError; |
| 13 | +use sp_std::marker::PhantomData; |
| 14 | + |
| 15 | +const LOG_TARGET: &str = "runtime::contracts"; |
| 16 | + |
| 17 | +type BalanceOf<T> = |
| 18 | + <<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance; |
| 19 | +type AccountIdOf<T> = <T as frame_system::Config>::AccountId; |
| 20 | +type CodeHash<T> = <T as frame_system::Config>::Hash; |
| 21 | +type CodeVec<T> = BoundedVec<u8, <T as Config>::MaxCodeLen>; |
| 22 | + |
| 23 | +mod old { |
| 24 | + use super::*; |
| 25 | + |
| 26 | + #[storage_alias] |
| 27 | + pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Twox64Concat, CodeHash<T>, CodeInfo<T>>; |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)] |
| 31 | +#[codec(mel_bound())] |
| 32 | +#[scale_info(skip_type_params(T))] |
| 33 | +pub struct CodeInfo<T: Config> { |
| 34 | + owner: AccountIdOf<T>, |
| 35 | + #[codec(compact)] |
| 36 | + deposit: BalanceOf<T>, |
| 37 | + #[codec(compact)] |
| 38 | + refcount: u64, |
| 39 | + determinism: Determinism, |
| 40 | + code_len: u32, |
| 41 | +} |
| 42 | + |
| 43 | +#[storage_alias] |
| 44 | +pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, CodeInfo<T>>; |
| 45 | + |
| 46 | +#[storage_alias] |
| 47 | +pub type PristineCode<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, CodeVec<T>>; |
| 48 | + |
| 49 | +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] |
| 50 | +pub struct Migration<T: Config> { |
| 51 | + last_code_hash: Option<CodeHash<T>>, |
| 52 | + _phantom: PhantomData<T>, |
| 53 | +} |
| 54 | + |
| 55 | +/// Logic as follows, |
| 56 | +/// Since we need to modifiy `CodeInfoOf` mapping we cannot use `iter()` or `drain()` on it as |
| 57 | +/// that will be undefined behaviour, so we are iterating over keys of `PristineCode` mappings |
| 58 | +/// which are code hashes. |
| 59 | +/// |
| 60 | +/// Migration Weights: Reusing v12 migration weights as most heavy operation which is moving |
| 61 | +/// code info is same. |
| 62 | +impl<T: Config> MigrationStep for Migration<T> { |
| 63 | + const VERSION: u16 = 15; |
| 64 | + |
| 65 | + fn max_step_weight() -> Weight { |
| 66 | + T::WeightInfo::v12_migration_step(T::MaxCodeLen::get()) |
| 67 | + } |
| 68 | + |
| 69 | + fn step(&mut self) -> (IsFinished, Weight) { |
| 70 | + let mut iter = if let Some(last_key) = self.last_code_hash.take() { |
| 71 | + PristineCode::<T>::iter_keys_from(PristineCode::<T>::hashed_key_for(last_key)) |
| 72 | + } else { |
| 73 | + PristineCode::<T>::iter_keys() |
| 74 | + }; |
| 75 | + |
| 76 | + if let Some(code_hash) = iter.next() { |
| 77 | + log::debug!( |
| 78 | + target: LOG_TARGET, |
| 79 | + "Migrating CodeInfoOf for code_hash {:?}", |
| 80 | + code_hash |
| 81 | + ); |
| 82 | + |
| 83 | + let code_info = old::CodeInfoOf::<T>::take(code_hash) |
| 84 | + .expect(format!("No CodeInfo found for code_hash: {:?}", code_hash).as_str()); |
| 85 | + let code_len = code_info.code_len; |
| 86 | + |
| 87 | + CodeInfoOf::<T>::insert(code_hash, code_info); |
| 88 | + |
| 89 | + self.last_code_hash = Some(code_hash); |
| 90 | + (IsFinished::No, T::WeightInfo::v12_migration_step(code_len)) |
| 91 | + } else { |
| 92 | + log::debug!(target: LOG_TARGET, "No more CodeInfo to migrate"); |
| 93 | + (IsFinished::Yes, T::WeightInfo::v12_migration_step(0)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + #[cfg(feature = "try-runtime")] |
| 98 | + fn pre_upgrade_step() -> Result<sp_std::vec::Vec<u8>, TryRuntimeError> { |
| 99 | + let len = 100; |
| 100 | + let sample: Vec<_> = old::CodeInfoOf::<T>::iter_keys().take(len).collect(); |
| 101 | + log::debug!( |
| 102 | + target: LOG_TARGET, |
| 103 | + "Taking sample of {} CodeInfoOf(s)", |
| 104 | + sample.len() |
| 105 | + ); |
| 106 | + |
| 107 | + Ok(sample.encode()) |
| 108 | + } |
| 109 | + |
| 110 | + #[cfg(feature = "try-runtime")] |
| 111 | + fn post_upgrade_step(state: Vec<u8>) -> Result<(), TryRuntimeError> { |
| 112 | + let state = <Vec<CodeHash<T>> as Decode>::decode(&mut &state[..]).unwrap(); |
| 113 | + |
| 114 | + log::debug!( |
| 115 | + target: LOG_TARGET, |
| 116 | + "Validating state of {} Codeinfo(s)", |
| 117 | + state.len() |
| 118 | + ); |
| 119 | + for hash in state { |
| 120 | + ensure!( |
| 121 | + old::CodeInfoOf::<T>::get(&hash).is_none(), |
| 122 | + "Old CodeInfoFor is not none!" |
| 123 | + ); |
| 124 | + let _ = CodeInfoOf::<T>::get(&hash) |
| 125 | + .expect(format!("CodeInfo for code_hash {:?} not found!", hash).as_str()); |
| 126 | + } |
| 127 | + Ok(()) |
| 128 | + } |
| 129 | +} |
0 commit comments