Update contract multi-block migration (#14313)

* move migrate sequence to config

* remove commented out code

* Update frame/contracts/src/lib.rs

Co-authored-by: PG Herveou <pgherveou@gmail.com>

* remove Migrations generic

* make runtime use noop migrations

* restrict is_upgrade_supported

* Update contract multi-block migration

Ensure that we do as many steps as possible given the weight limit passed to on_idle

* undo is_upgrade_supported change

* Update bin/node/runtime/src/lib.rs

Co-authored-by: PG Herveou <pgherveou@gmail.com>

* wip

* fix comment (#14316)

* fix test

* fix

* Update frame/contracts/src/migration.rs

Co-authored-by: Juan <juangirini@gmail.com>

* fix test doc

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Fix compilation with feature runtime-benchmarks

* fix example

* fix  cargo doc --document-private-items

* private links

* Remove dup comment

* add doc for MigrationInProgress

* PR review remove duplicate asserts

* simplify upper bound

* fix link

* typo

* typo

* no unwrap()

* correct log message

* missing

* fix typo

* PR comment

* Add example with single element tuple

* Improve migration message

* Update frame/contracts/src/benchmarking/mod.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Update frame/contracts/src/migration.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Update frame/contracts/src/migration.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* use saturating_accrue instead of +=

* add more doc

* Contracts: Better migration types (#14418)

* Add explicit error, if try-runtime runs a noop migration

* use mut remaining_weight

---------

Co-authored-by: Juan Girini <juangirini@gmail.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
This commit is contained in:
PG Herveou
2023-06-20 15:16:28 +02:00
committed by GitHub
parent 6ea3736004
commit 6ac8537436
8 changed files with 151 additions and 103 deletions
+10 -15
View File
@@ -18,17 +18,15 @@
//! Update `CodeStorage` with the new `determinism` field.
use crate::{
migration::{IsFinished, Migrate},
migration::{IsFinished, MigrationStep},
weights::WeightInfo,
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::*;
@@ -79,11 +77,10 @@ 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> Migrate for Migration<T> {
impl<T: Config> MigrationStep for Migration<T> {
const VERSION: u16 = 9;
fn max_step_weight() -> Weight {
@@ -91,8 +88,8 @@ impl<T: Config> Migrate 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()
};
@@ -108,7 +105,7 @@ impl<T: Config> Migrate 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");
@@ -126,8 +123,8 @@ impl<T: Config> Migrate for Migration<T> {
#[cfg(feature = "try-runtime")]
fn post_upgrade_step(state: Vec<u8>) -> Result<(), TryRuntimeError> {
let sample =
<Vec<(CodeHash<T>, old::PrefabWasmModule)> as Decode>::decode(&mut &state[..]).unwrap();
let sample = <Vec<(CodeHash<T>, old::PrefabWasmModule)> as Decode>::decode(&mut &state[..])
.expect("pre_upgrade_step provides a valid state; qed");
log::debug!(target: LOG_TARGET, "Validating sample of {} contract codes", sample.len());
for (code_hash, old) in sample {
@@ -140,8 +137,6 @@ impl<T: Config> Migrate for Migration<T> {
ensure!(module.initial == old.initial, "invalid initial");
ensure!(module.maximum == old.maximum, "invalid maximum");
ensure!(module.code == old.code, "invalid code");
ensure!(module.maximum == old.maximum, "invalid maximum");
ensure!(module.code == old.code, "invalid code");
}
Ok(())