Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 4a40eba

Browse files
authored
Fix migrations (#7340)
Okay this was stupid 🤦 Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
1 parent 81309d1 commit 4a40eba

File tree

3 files changed

+104
-66
lines changed

3 files changed

+104
-66
lines changed

runtime/parachains/src/configuration/migration/v5.rs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,35 @@ pub struct V5HostConfiguration<BlockNumber> {
127127
pub minimum_validation_upgrade_delay: BlockNumber,
128128
}
129129

130-
#[frame_support::storage_alias]
131-
pub(crate) type V4ActiveConfig<T: Config> =
132-
StorageValue<Pallet<T>, V4HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
133-
134-
#[frame_support::storage_alias]
135-
pub(crate) type V4PendingConfigs<T: Config> = StorageValue<
136-
Pallet<T>,
137-
Vec<(SessionIndex, V4HostConfiguration<BlockNumberFor<T>>)>,
138-
OptionQuery,
139-
>;
140-
141-
#[frame_support::storage_alias]
142-
pub(crate) type V5ActiveConfig<T: Config> =
143-
StorageValue<Pallet<T>, V5HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
144-
145-
#[frame_support::storage_alias]
146-
pub(crate) type V5PendingConfigs<T: Config> = StorageValue<
147-
Pallet<T>,
148-
Vec<(SessionIndex, V5HostConfiguration<BlockNumberFor<T>>)>,
149-
OptionQuery,
150-
>;
130+
mod v4 {
131+
use super::*;
132+
133+
#[frame_support::storage_alias]
134+
pub(crate) type ActiveConfig<T: Config> =
135+
StorageValue<Pallet<T>, V4HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
136+
137+
#[frame_support::storage_alias]
138+
pub(crate) type PendingConfigs<T: Config> = StorageValue<
139+
Pallet<T>,
140+
Vec<(SessionIndex, V4HostConfiguration<BlockNumberFor<T>>)>,
141+
OptionQuery,
142+
>;
143+
}
144+
145+
mod v5 {
146+
use super::*;
147+
148+
#[frame_support::storage_alias]
149+
pub(crate) type ActiveConfig<T: Config> =
150+
StorageValue<Pallet<T>, V5HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
151+
152+
#[frame_support::storage_alias]
153+
pub(crate) type PendingConfigs<T: Config> = StorageValue<
154+
Pallet<T>,
155+
Vec<(SessionIndex, V5HostConfiguration<BlockNumberFor<T>>)>,
156+
OptionQuery,
157+
>;
158+
}
151159

152160
impl<BlockNumber: Default + From<u32>> Default for V4HostConfiguration<BlockNumber> {
153161
fn default() -> Self {
@@ -266,6 +274,7 @@ impl<T: Config> OnRuntimeUpgrade for MigrateToV5<T> {
266274
}
267275

268276
fn on_runtime_upgrade() -> Weight {
277+
log::info!(target: configuration::LOG_TARGET, "MigrateToV5 started");
269278
if StorageVersion::get::<Pallet<T>>() == 4 {
270279
let weight_consumed = migrate_to_v5::<T>();
271280

@@ -352,13 +361,13 @@ executor_params : Default::default(),
352361
}
353362
};
354363

355-
let v4 = V4ActiveConfig::<T>::get()
364+
let v4 = v4::ActiveConfig::<T>::get()
356365
.defensive_proof("Could not decode old config")
357366
.unwrap_or_default();
358367
let v5 = translate(v4);
359-
V5ActiveConfig::<T>::set(Some(v5));
368+
v5::ActiveConfig::<T>::set(Some(v5));
360369

361-
let pending_v4 = V4PendingConfigs::<T>::get()
370+
let pending_v4 = v4::PendingConfigs::<T>::get()
362371
.defensive_proof("Could not decode old pending")
363372
.unwrap_or_default();
364373
let mut pending_v5 = Vec::new();
@@ -367,7 +376,7 @@ executor_params : Default::default(),
367376
let v5 = translate(v4);
368377
pending_v5.push((session, v5));
369378
}
370-
V5PendingConfigs::<T>::set(Some(pending_v5.clone()));
379+
v5::PendingConfigs::<T>::set(Some(pending_v5.clone()));
371380

372381
let num_configs = (pending_v5.len() + 1) as u64;
373382
T::DbWeight::get().reads_writes(num_configs, num_configs)
@@ -397,8 +406,8 @@ mod tests {
397406
// doesn't need to be read and also leaving it as one line allows to easily copy it.
398407
let raw_config = hex_literal::hex!["0000a000005000000a00000000c8000000c800000a0000000a000000100e0000580200000000500000c800000700e8764817020040011e00000000000000005039278c0400000000000000000000005039278c0400000000000000000000e8030000009001001e00000000000000009001008070000000000000000000000a0000000a0000000a00000001000000010500000001c80000000600000058020000580200000200000059000000000000001e000000280000000700c817a80402004001010200000014000000"];
399408

400-
let v4 = v5::V4HostConfiguration::<primitives::BlockNumber>::decode(&mut &raw_config[..])
401-
.unwrap();
409+
let v4 =
410+
V4HostConfiguration::<primitives::BlockNumber>::decode(&mut &raw_config[..]).unwrap();
402411

403412
// We check only a sample of the values here. If we missed any fields or messed up data types
404413
// that would skew all the fields coming after.
@@ -421,7 +430,7 @@ mod tests {
421430
// We specify only the picked fields and the rest should be provided by the `Default`
422431
// implementation. That implementation is copied over between the two types and should work
423432
// fine.
424-
let v4 = v5::V4HostConfiguration::<primitives::BlockNumber> {
433+
let v4 = V4HostConfiguration::<primitives::BlockNumber> {
425434
ump_max_individual_weight: Weight::from_parts(0x71616e6f6e0au64, 0x71616e6f6e0au64),
426435
needed_approvals: 69,
427436
thread_availability_period: 55,
@@ -438,13 +447,13 @@ mod tests {
438447

439448
new_test_ext(Default::default()).execute_with(|| {
440449
// Implant the v4 version in the state.
441-
V4ActiveConfig::<Test>::set(Some(v4));
442-
V4PendingConfigs::<Test>::set(Some(pending_configs));
450+
v4::ActiveConfig::<Test>::set(Some(v4));
451+
v4::PendingConfigs::<Test>::set(Some(pending_configs));
443452

444453
migrate_to_v5::<Test>();
445454

446-
let v5 = V5ActiveConfig::<Test>::get().unwrap();
447-
let mut configs_to_check = V5PendingConfigs::<Test>::get().unwrap();
455+
let v5 = v5::ActiveConfig::<Test>::get().unwrap();
456+
let mut configs_to_check = v5::PendingConfigs::<Test>::get().unwrap();
448457
configs_to_check.push((0, v5.clone()));
449458

450459
for (_, v4) in configs_to_check {

runtime/parachains/src/configuration/migration/v6.rs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,35 @@ use super::v5::V5HostConfiguration;
3434
// Change this once there is V7.
3535
type V6HostConfiguration<BlockNumber> = configuration::HostConfiguration<BlockNumber>;
3636

37-
#[frame_support::storage_alias]
38-
pub(crate) type V5ActiveConfig<T: Config> =
39-
StorageValue<Pallet<T>, V5HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
40-
41-
#[frame_support::storage_alias]
42-
pub(crate) type V5PendingConfigs<T: Config> = StorageValue<
43-
Pallet<T>,
44-
Vec<(SessionIndex, V5HostConfiguration<BlockNumberFor<T>>)>,
45-
OptionQuery,
46-
>;
47-
48-
#[frame_support::storage_alias]
49-
pub(crate) type V6ActiveConfig<T: Config> =
50-
StorageValue<Pallet<T>, V6HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
51-
52-
#[frame_support::storage_alias]
53-
pub(crate) type V6PendingConfigs<T: Config> = StorageValue<
54-
Pallet<T>,
55-
Vec<(SessionIndex, V6HostConfiguration<BlockNumberFor<T>>)>,
56-
OptionQuery,
57-
>;
37+
mod v5 {
38+
use super::*;
39+
40+
#[frame_support::storage_alias]
41+
pub(crate) type ActiveConfig<T: Config> =
42+
StorageValue<Pallet<T>, V5HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
43+
44+
#[frame_support::storage_alias]
45+
pub(crate) type PendingConfigs<T: Config> = StorageValue<
46+
Pallet<T>,
47+
Vec<(SessionIndex, V5HostConfiguration<BlockNumberFor<T>>)>,
48+
OptionQuery,
49+
>;
50+
}
51+
52+
mod v6 {
53+
use super::*;
54+
55+
#[frame_support::storage_alias]
56+
pub(crate) type ActiveConfig<T: Config> =
57+
StorageValue<Pallet<T>, V6HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
58+
59+
#[frame_support::storage_alias]
60+
pub(crate) type PendingConfigs<T: Config> = StorageValue<
61+
Pallet<T>,
62+
Vec<(SessionIndex, V6HostConfiguration<BlockNumberFor<T>>)>,
63+
OptionQuery,
64+
>;
65+
}
5866

5967
pub struct MigrateToV6<T>(sp_std::marker::PhantomData<T>);
6068
impl<T: Config> OnRuntimeUpgrade for MigrateToV6<T> {
@@ -65,6 +73,7 @@ impl<T: Config> OnRuntimeUpgrade for MigrateToV6<T> {
6573
}
6674

6775
fn on_runtime_upgrade() -> Weight {
76+
log::info!(target: configuration::LOG_TARGET, "MigrateToV6 started");
6877
if StorageVersion::get::<Pallet<T>>() == 5 {
6978
let weight_consumed = migrate_to_v6::<T>();
7079

@@ -145,24 +154,24 @@ executor_params : pre.executor_params,
145154
}
146155
};
147156

148-
let v5 = V5ActiveConfig::<T>::get()
157+
let v5 = v5::ActiveConfig::<T>::get()
149158
.defensive_proof("Could not decode old config")
150159
.unwrap_or_default();
151160
let v6 = translate(v5);
152-
V6ActiveConfig::<T>::set(Some(v6));
161+
v6::ActiveConfig::<T>::set(Some(v6));
153162

154-
let pending_v4 = V5PendingConfigs::<T>::get()
163+
let pending_v5 = v5::PendingConfigs::<T>::get()
155164
.defensive_proof("Could not decode old pending")
156165
.unwrap_or_default();
157-
let mut pending_v5 = Vec::new();
166+
let mut pending_v6 = Vec::new();
158167

159-
for (session, v5) in pending_v4.into_iter() {
168+
for (session, v5) in pending_v5.into_iter() {
160169
let v6 = translate(v5);
161-
pending_v5.push((session, v6));
170+
pending_v6.push((session, v6));
162171
}
163-
V6PendingConfigs::<T>::set(Some(pending_v5.clone()));
172+
v6::PendingConfigs::<T>::set(Some(pending_v6.clone()));
164173

165-
let num_configs = (pending_v5.len() + 1) as u64;
174+
let num_configs = (pending_v6.len() + 1) as u64;
166175
T::DbWeight::get().reads_writes(num_configs, num_configs)
167176
}
168177

@@ -201,6 +210,7 @@ mod tests {
201210
assert_eq!(v5.n_delay_tranches, 40);
202211
assert_eq!(v5.ump_max_individual_weight, Weight::from_parts(20_000_000_000, 5_242_880));
203212
assert_eq!(v5.minimum_validation_upgrade_delay, 15); // This is the last field in the struct.
213+
assert_eq!(v5.group_rotation_frequency, 10);
204214
}
205215

206216
#[test]
@@ -230,13 +240,13 @@ mod tests {
230240

231241
new_test_ext(Default::default()).execute_with(|| {
232242
// Implant the v5 version in the state.
233-
V5ActiveConfig::<Test>::set(Some(v5));
234-
V5PendingConfigs::<Test>::set(Some(pending_configs));
243+
v5::ActiveConfig::<Test>::set(Some(v5));
244+
v5::PendingConfigs::<Test>::set(Some(pending_configs));
235245

236246
migrate_to_v6::<Test>();
237247

238-
let v6 = V6ActiveConfig::<Test>::get().unwrap();
239-
let mut configs_to_check = V6PendingConfigs::<Test>::get().unwrap();
248+
let v6 = v6::ActiveConfig::<Test>::get().unwrap();
249+
let mut configs_to_check = v6::PendingConfigs::<Test>::get().unwrap();
240250
configs_to_check.push((0, v6.clone()));
241251

242252
for (_, v5) in configs_to_check {

runtime/parachains/src/configuration/migration_ump.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,29 @@ pub mod latest {
9999
"Last pending HostConfig upgrade:\n\n{:#?}\n",
100100
pending.last()
101101
);
102+
let Some(last) = pending.last() else {
103+
return Err("There must be a new pending upgrade enqueued".into());
104+
};
102105
ensure!(
103106
pending.len() == old_pending as usize + 1,
104-
"There must be a new pending upgrade enqueued"
107+
"There must be exactly one new pending upgrade enqueued"
105108
);
109+
if let Err(err) = last.1.check_consistency() {
110+
log::error!(
111+
target: LOG_TARGET,
112+
"Last PendingConfig is invalidity {:?}", err,
113+
);
114+
115+
return Err("Pending upgrade must be sane but was not".into())
116+
}
117+
if let Err(err) = ActiveConfig::<T>::get().check_consistency() {
118+
log::error!(
119+
target: LOG_TARGET,
120+
"ActiveConfig is invalid: {:?}", err,
121+
);
122+
123+
return Err("Active upgrade must be sane but was not".into())
124+
}
106125

107126
Ok(())
108127
}

0 commit comments

Comments
 (0)