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
+17 -12
View File
@@ -1,5 +1,4 @@
// This file is part of Substrate.
mod pallet_dummy;
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
@@ -16,6 +15,8 @@ mod pallet_dummy;
// See the License for the specific language governing permissions and
// limitations under the License.
mod pallet_dummy;
use self::test_utils::{ensure_stored, expected_deposit, hash};
use crate::{
self as pallet_contracts,
@@ -24,13 +25,14 @@ use crate::{
Result as ExtensionResult, RetVal, ReturnFlags, SysConfig,
},
exec::{Frame, Key},
migration::codegen::LATEST_MIGRATION_VERSION,
storage::DeletionQueueManager,
tests::test_utils::{get_contract, get_contract_checked},
wasm::{Determinism, ReturnCode as RuntimeReturnCode},
weights::WeightInfo,
BalanceOf, Code, CodeHash, CodeInfoOf, CollectEvents, Config, ContractInfo, ContractInfoOf,
DebugInfo, DefaultAddressGenerator, DeletionQueueCounter, Error, MigrationInProgress,
NoopMigration, Origin, Pallet, PristineCode, Schedule,
DebugInfo, DefaultAddressGenerator, DeletionQueueCounter, Error, MigrationInProgress, Origin,
Pallet, PristineCode, Schedule,
};
use assert_matches::assert_matches;
use codec::Encode;
@@ -457,7 +459,7 @@ impl Config for Test {
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = UnstableInterface;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
type Migrations = (NoopMigration<1>, NoopMigration<2>);
type Migrations = crate::migration::codegen::BenchMigrations;
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type MaxDelegateDependencies = MaxDelegateDependencies;
}
@@ -610,17 +612,20 @@ fn calling_plain_account_fails() {
fn migration_on_idle_hooks_works() {
// Defines expectations of how many migration steps can be done given the weight limit.
let tests = [
(Weight::zero(), 0),
(<Test as Config>::WeightInfo::migrate() + 1.into(), 1),
(Weight::MAX, 2),
(Weight::zero(), LATEST_MIGRATION_VERSION - 2),
(<Test as Config>::WeightInfo::migrate() + 1.into(), LATEST_MIGRATION_VERSION - 1),
(Weight::MAX, LATEST_MIGRATION_VERSION),
];
for (weight, expected_version) in tests {
ExtBuilder::default().set_storage_version(0).build().execute_with(|| {
MigrationInProgress::<Test>::set(Some(Default::default()));
Contracts::on_idle(System::block_number(), weight);
assert_eq!(StorageVersion::get::<Pallet<Test>>(), expected_version);
});
ExtBuilder::default()
.set_storage_version(LATEST_MIGRATION_VERSION - 2)
.build()
.execute_with(|| {
MigrationInProgress::<Test>::set(Some(Default::default()));
Contracts::on_idle(System::block_number(), weight);
assert_eq!(StorageVersion::get::<Pallet<Test>>(), expected_version);
});
}
}