Skip to content

Commit b659b7d

Browse files
dastansamliamaharon
authored andcommitted
migrations: prevent accidentally using unversioned migrations instead of VersionedMigration (#3835)
closes #1324 #### Problem Currently, it is possible to accidentally use inner unversioned migration instead of `VersionedMigration` since both implement `OnRuntimeUpgrade`. #### Solution With this change, we make it clear that value of `Inner` is not intended to be used directly. It is achieved by bounding `Inner` to new trait `UncheckedOnRuntimeUpgrade`, which has the same interface (except `unchecked_` prefix) as `OnRuntimeUpgrade`. #### `try-runtime` functions Since developers can implement `try-runtime` for `Inner` value in `VersionedMigration` and have custom logic for it, I added the same `try-runtime` functions to `UncheckedOnRuntimeUpgrade`. I looked for a ways to not duplicate functions, but couldn't find anything that doesn't significantly change the codebase. So I would appreciate If you have any suggestions to improve this cc @liamaharon @xlc polkadot address: 16FqwPZ8GRC5U5D4Fu7W33nA55ZXzXGWHwmbnE1eT6pxuqcT --------- Co-authored-by: Liam Aharon <[email protected]>
1 parent 8164280 commit b659b7d

File tree

22 files changed

+257
-189
lines changed

22 files changed

+257
-189
lines changed

cumulus/pallets/xcmp-queue/src/migration.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{Config, OverweightIndex, Pallet, QueueConfig, QueueConfigData, DEFAU
2020
use cumulus_primitives_core::XcmpMessageFormat;
2121
use frame_support::{
2222
pallet_prelude::*,
23-
traits::{EnqueueMessage, OnRuntimeUpgrade, StorageVersion},
23+
traits::{EnqueueMessage, StorageVersion, UncheckedOnRuntimeUpgrade},
2424
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
2525
};
2626

@@ -96,7 +96,7 @@ pub mod v2 {
9696
/// 2D weights).
9797
pub struct UncheckedMigrationToV2<T: Config>(PhantomData<T>);
9898

99-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrationToV2<T> {
99+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV2<T> {
100100
#[allow(deprecated)]
101101
fn on_runtime_upgrade() -> Weight {
102102
let translate = |pre: v1::QueueConfigData| -> v2::QueueConfigData {
@@ -187,7 +187,7 @@ pub mod v3 {
187187
/// Migrates the pallet storage to v3.
188188
pub struct UncheckedMigrationToV3<T: Config>(PhantomData<T>);
189189

190-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrationToV3<T> {
190+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV3<T> {
191191
fn on_runtime_upgrade() -> Weight {
192192
#[frame_support::storage_alias]
193193
type Overweight<T: Config> =
@@ -266,7 +266,7 @@ pub mod v4 {
266266
/// thresholds to at least the default values.
267267
pub struct UncheckedMigrationToV4<T: Config>(PhantomData<T>);
268268

269-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrationToV4<T> {
269+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV4<T> {
270270
fn on_runtime_upgrade() -> Weight {
271271
let translate = |pre: v2::QueueConfigData| -> QueueConfigData {
272272
let pre_default = v2::QueueConfigData::default();
@@ -315,6 +315,7 @@ pub mod v4 {
315315
mod tests {
316316
use super::*;
317317
use crate::mock::{new_test_ext, Test};
318+
use frame_support::traits::OnRuntimeUpgrade;
318319

319320
#[test]
320321
#[allow(deprecated)]

polkadot/runtime/common/src/assigned_slots/migration.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use super::{Config, MaxPermanentSlots, MaxTemporarySlots, Pallet, LOG_TARGET};
18-
use frame_support::traits::{Get, GetStorageVersion, OnRuntimeUpgrade};
18+
use frame_support::traits::{Get, GetStorageVersion, UncheckedOnRuntimeUpgrade};
1919

2020
#[cfg(feature = "try-runtime")]
2121
use frame_support::ensure;
2222
#[cfg(feature = "try-runtime")]
2323
use sp_std::vec::Vec;
2424

2525
pub mod v1 {
26-
2726
use super::*;
2827
pub struct VersionUncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
29-
impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
28+
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
3029
#[cfg(feature = "try-runtime")]
3130
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
3231
let on_chain_version = Pallet::<T>::on_chain_storage_version();

polkadot/runtime/common/src/paras_registrar/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use super::*;
18-
use frame_support::traits::{Contains, OnRuntimeUpgrade};
18+
use frame_support::traits::{Contains, UncheckedOnRuntimeUpgrade};
1919

2020
#[derive(Encode, Decode)]
2121
pub struct ParaInfoV1<Account, Balance> {
@@ -27,7 +27,7 @@ pub struct ParaInfoV1<Account, Balance> {
2727
pub struct VersionUncheckedMigrateToV1<T, UnlockParaIds>(
2828
sp_std::marker::PhantomData<(T, UnlockParaIds)>,
2929
);
30-
impl<T: Config, UnlockParaIds: Contains<ParaId>> OnRuntimeUpgrade
30+
impl<T: Config, UnlockParaIds: Contains<ParaId>> UncheckedOnRuntimeUpgrade
3131
for VersionUncheckedMigrateToV1<T, UnlockParaIds>
3232
{
3333
fn on_runtime_upgrade() -> Weight {

polkadot/runtime/parachains/src/assigner_on_demand/migration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use super::*;
1919
use frame_support::{
2020
migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias,
21-
traits::OnRuntimeUpgrade, weights::Weight,
21+
traits::UncheckedOnRuntimeUpgrade, weights::Weight,
2222
};
2323

2424
mod v0 {
@@ -51,7 +51,7 @@ mod v1 {
5151

5252
/// Migration to V1
5353
pub struct UncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
54-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV1<T> {
54+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrateToV1<T> {
5555
fn on_runtime_upgrade() -> Weight {
5656
let mut weight: Weight = Weight::zero();
5757

@@ -141,7 +141,7 @@ pub type MigrateV0ToV1<T> = VersionedMigration<
141141

142142
#[cfg(test)]
143143
mod tests {
144-
use super::{v0, v1, OnRuntimeUpgrade, Weight};
144+
use super::{v0, v1, UncheckedOnRuntimeUpgrade, Weight};
145145
use crate::mock::{new_test_ext, MockGenesisConfig, OnDemandAssigner, Test};
146146
use primitives::Id as ParaId;
147147

@@ -163,7 +163,7 @@ mod tests {
163163

164164
// For tests, db weight is zero.
165165
assert_eq!(
166-
<v1::UncheckedMigrateToV1<Test> as OnRuntimeUpgrade>::on_runtime_upgrade(),
166+
<v1::UncheckedMigrateToV1<Test> as UncheckedOnRuntimeUpgrade>::on_runtime_upgrade(),
167167
Weight::zero()
168168
);
169169

polkadot/runtime/parachains/src/configuration/migration/v10.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
//! A module that is responsible for migration of storage.
1818
1919
use crate::configuration::{Config, Pallet};
20-
use frame_support::{pallet_prelude::*, traits::Defensive, weights::Weight};
20+
use frame_support::{
21+
pallet_prelude::*,
22+
traits::{Defensive, UncheckedOnRuntimeUpgrade},
23+
weights::Weight,
24+
};
2125
use frame_system::pallet_prelude::BlockNumberFor;
2226
use primitives::{
2327
AsyncBackingParams, Balance, ExecutorParams, NodeFeatures, SessionIndex,
@@ -26,8 +30,6 @@ use primitives::{
2630
use sp_runtime::Perbill;
2731
use sp_std::vec::Vec;
2832

29-
use frame_support::traits::OnRuntimeUpgrade;
30-
3133
use super::v9::V9HostConfiguration;
3234
// All configuration of the runtime with respect to paras.
3335
#[derive(Clone, Encode, PartialEq, Decode, Debug)]
@@ -163,7 +165,7 @@ mod v10 {
163165
}
164166

165167
pub struct VersionUncheckedMigrateToV10<T>(sp_std::marker::PhantomData<T>);
166-
impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateToV10<T> {
168+
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV10<T> {
167169
#[cfg(feature = "try-runtime")]
168170
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
169171
log::trace!(target: crate::configuration::LOG_TARGET, "Running pre_upgrade() for HostConfiguration MigrateToV10");

polkadot/runtime/parachains/src/configuration/migration/v11.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
1919
use crate::configuration::{self, Config, Pallet};
2020
use frame_support::{
21-
migrations::VersionedMigration, pallet_prelude::*, traits::Defensive, weights::Weight,
21+
migrations::VersionedMigration,
22+
pallet_prelude::*,
23+
traits::{Defensive, UncheckedOnRuntimeUpgrade},
24+
weights::Weight,
2225
};
2326
use frame_system::pallet_prelude::BlockNumberFor;
2427
use primitives::{
@@ -27,7 +30,6 @@ use primitives::{
2730
};
2831
use sp_std::vec::Vec;
2932

30-
use frame_support::traits::OnRuntimeUpgrade;
3133
use polkadot_core_primitives::Balance;
3234
use sp_arithmetic::Perbill;
3335

@@ -176,7 +178,7 @@ pub type MigrateToV11<T> = VersionedMigration<
176178
>;
177179

178180
pub struct UncheckedMigrateToV11<T>(sp_std::marker::PhantomData<T>);
179-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV11<T> {
181+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrateToV11<T> {
180182
#[cfg(feature = "try-runtime")]
181183
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
182184
log::trace!(target: crate::configuration::LOG_TARGET, "Running pre_upgrade() for HostConfiguration MigrateToV11");

polkadot/runtime/parachains/src/configuration/migration/v12.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::configuration::{self, migration::v11::V11HostConfiguration, Config, P
2020
use frame_support::{
2121
migrations::VersionedMigration,
2222
pallet_prelude::*,
23-
traits::{Defensive, OnRuntimeUpgrade},
23+
traits::{Defensive, UncheckedOnRuntimeUpgrade},
2424
};
2525
use frame_system::pallet_prelude::BlockNumberFor;
2626
use primitives::vstaging::SchedulerParams;
@@ -70,7 +70,7 @@ pub type MigrateToV12<T> = VersionedMigration<
7070

7171
pub struct UncheckedMigrateToV12<T>(sp_std::marker::PhantomData<T>);
7272

73-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV12<T> {
73+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrateToV12<T> {
7474
#[cfg(feature = "try-runtime")]
7575
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
7676
log::trace!(target: crate::configuration::LOG_TARGET, "Running pre_upgrade() for HostConfiguration MigrateToV12");

polkadot/runtime/parachains/src/inclusion/migration.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod v1 {
7373
CandidatePendingAvailability as V1CandidatePendingAvailability, Config, Pallet,
7474
PendingAvailability as V1PendingAvailability,
7575
};
76-
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
76+
use frame_support::{traits::UncheckedOnRuntimeUpgrade, weights::Weight};
7777
use sp_core::Get;
7878
use sp_std::{collections::vec_deque::VecDeque, vec::Vec};
7979

@@ -87,7 +87,7 @@ mod v1 {
8787

8888
pub struct VersionUncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
8989

90-
impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
90+
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
9191
#[cfg(feature = "try-runtime")]
9292
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
9393
log::trace!(target: crate::inclusion::LOG_TARGET, "Running pre_upgrade() for inclusion MigrateToV1");
@@ -216,7 +216,7 @@ mod tests {
216216
},
217217
mock::{new_test_ext, MockGenesisConfig, Test},
218218
};
219-
use frame_support::traits::OnRuntimeUpgrade;
219+
use frame_support::traits::UncheckedOnRuntimeUpgrade;
220220
use primitives::{AvailabilityBitfield, Id as ParaId};
221221
use test_helpers::{dummy_candidate_commitments, dummy_candidate_descriptor, dummy_hash};
222222

@@ -225,7 +225,7 @@ mod tests {
225225
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
226226
// No data to migrate.
227227
assert_eq!(
228-
<VersionUncheckedMigrateToV1<Test> as OnRuntimeUpgrade>::on_runtime_upgrade(),
228+
<VersionUncheckedMigrateToV1<Test> as UncheckedOnRuntimeUpgrade>::on_runtime_upgrade(),
229229
Weight::zero()
230230
);
231231
assert!(V1PendingAvailability::<Test>::iter().next().is_none());
@@ -299,7 +299,7 @@ mod tests {
299299

300300
// For tests, db weight is zero.
301301
assert_eq!(
302-
<VersionUncheckedMigrateToV1<Test> as OnRuntimeUpgrade>::on_runtime_upgrade(),
302+
<VersionUncheckedMigrateToV1<Test> as UncheckedOnRuntimeUpgrade>::on_runtime_upgrade(),
303303
Weight::zero()
304304
);
305305

polkadot/runtime/parachains/src/scheduler/migration.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use super::*;
2020
use frame_support::{
2121
migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias,
22-
traits::OnRuntimeUpgrade, weights::Weight,
22+
traits::UncheckedOnRuntimeUpgrade, weights::Weight,
2323
};
2424

2525
/// Old/legacy assignment representation (v0).
@@ -105,7 +105,8 @@ mod v0 {
105105
// - Assignments only consist of `ParaId`, `Assignment` is a concrete type (Same as V0Assignment).
106106
mod v1 {
107107
use frame_support::{
108-
pallet_prelude::ValueQuery, storage_alias, traits::OnRuntimeUpgrade, weights::Weight,
108+
pallet_prelude::ValueQuery, storage_alias, traits::UncheckedOnRuntimeUpgrade,
109+
weights::Weight,
109110
};
110111
use frame_system::pallet_prelude::BlockNumberFor;
111112

@@ -164,7 +165,7 @@ mod v1 {
164165

165166
/// Migration to V1
166167
pub struct UncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
167-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV1<T> {
168+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrateToV1<T> {
168169
fn on_runtime_upgrade() -> Weight {
169170
let mut weight: Weight = Weight::zero();
170171

@@ -302,7 +303,7 @@ mod v2 {
302303
/// Migration to V2
303304
pub struct UncheckedMigrateToV2<T>(sp_std::marker::PhantomData<T>);
304305

305-
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV2<T> {
306+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrateToV2<T> {
306307
fn on_runtime_upgrade() -> Weight {
307308
let mut weight: Weight = Weight::zero();
308309

polkadot/xcm/pallet-xcm/src/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
};
2020
use frame_support::{
2121
pallet_prelude::*,
22-
traits::{OnRuntimeUpgrade, StorageVersion},
22+
traits::{OnRuntimeUpgrade, StorageVersion, UncheckedOnRuntimeUpgrade},
2323
weights::Weight,
2424
};
2525

@@ -35,7 +35,7 @@ pub mod v1 {
3535
///
3636
/// Use experimental [`MigrateToV1`] instead.
3737
pub struct VersionUncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
38-
impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
38+
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
3939
fn on_runtime_upgrade() -> Weight {
4040
let mut weight = T::DbWeight::get().reads(1);
4141

0 commit comments

Comments
 (0)