Contracts: Ensure latest migration match pallet_version (#14676)

* Add version check

* fix format

* simplify

* restructure imports

* add version check

* Fix benchmarking

* Rename migrations -> BenchMigrations

* doc

* add more docs

* fix format string

* Update frame/contracts/build.rs

* fix

* add cargo:rerun-if-changed

---------

Co-authored-by: parity-processbot <>
This commit is contained in:
PG Herveou
2023-07-31 16:48:53 +02:00
committed by GitHub
parent fb39893bb3
commit 49816ff4d9
7 changed files with 139 additions and 46 deletions
+33 -17
View File
@@ -61,6 +61,7 @@ pub mod v10;
pub mod v11;
pub mod v12;
pub mod v13;
include!(concat!(env!("OUT_DIR"), "/migration_codegen.rs"));
use crate::{weights::WeightInfo, Config, Error, MigrationInProgress, Pallet, Weight, LOG_TARGET};
use codec::{Codec, Decode};
@@ -522,7 +523,10 @@ impl MigrateSequence for Tuple {
#[cfg(test)]
mod test {
use super::*;
use crate::tests::{ExtBuilder, Test};
use crate::{
migration::codegen::LATEST_MIGRATION_VERSION,
tests::{ExtBuilder, Test},
};
#[derive(Default, Encode, Decode, MaxEncodedLen)]
struct MockMigration<const N: u16> {
@@ -546,6 +550,11 @@ mod test {
}
}
#[test]
fn test_storage_version_matches_last_migration_file() {
assert_eq!(StorageVersion::new(LATEST_MIGRATION_VERSION), crate::pallet::STORAGE_VERSION);
}
#[test]
fn version_range_works() {
let range = <(MockMigration<1>, MockMigration<2>)>::VERSION_RANGE;
@@ -584,7 +593,7 @@ mod test {
type TestMigration = Migration<Test>;
ExtBuilder::default().build().execute_with(|| {
assert_eq!(StorageVersion::get::<Pallet<Test>>(), 2);
assert_eq!(StorageVersion::get::<Pallet<Test>>(), LATEST_MIGRATION_VERSION);
assert_eq!(TestMigration::migrate(Weight::MAX).0, MigrateResult::NoMigrationInProgress)
});
}
@@ -593,21 +602,28 @@ mod test {
fn migration_works() {
type TestMigration = Migration<Test, false>;
ExtBuilder::default().set_storage_version(0).build().execute_with(|| {
assert_eq!(StorageVersion::get::<Pallet<Test>>(), 0);
TestMigration::on_runtime_upgrade();
for (version, status) in
[(1, MigrateResult::InProgress { steps_done: 1 }), (2, MigrateResult::Completed)]
{
assert_eq!(TestMigration::migrate(Weight::MAX).0, status);
assert_eq!(
<Pallet<Test>>::on_chain_storage_version(),
StorageVersion::new(version)
);
}
ExtBuilder::default()
.set_storage_version(LATEST_MIGRATION_VERSION - 2)
.build()
.execute_with(|| {
assert_eq!(StorageVersion::get::<Pallet<Test>>(), LATEST_MIGRATION_VERSION - 2);
TestMigration::on_runtime_upgrade();
for (version, status) in [
(LATEST_MIGRATION_VERSION - 1, MigrateResult::InProgress { steps_done: 1 }),
(LATEST_MIGRATION_VERSION, MigrateResult::Completed),
] {
assert_eq!(TestMigration::migrate(Weight::MAX).0, status);
assert_eq!(
<Pallet<Test>>::on_chain_storage_version(),
StorageVersion::new(version)
);
}
assert_eq!(TestMigration::migrate(Weight::MAX).0, MigrateResult::NoMigrationInProgress);
assert_eq!(StorageVersion::get::<Pallet<Test>>(), 2);
});
assert_eq!(
TestMigration::migrate(Weight::MAX).0,
MigrateResult::NoMigrationInProgress
);
assert_eq!(StorageVersion::get::<Pallet<Test>>(), LATEST_MIGRATION_VERSION);
});
}
}