Skip to content

Commit b7af797

Browse files
liamaharonbkchr
authored andcommitted
Migration hook fixes (paritytech#14174)
* fix offences pre_upgrade hook * identify source of ensure! failures * stop migration hooks breaking post migration * add childbounties storage version * init child bounties version to zero * Update frame/child-bounties/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * remove redundant preupgrade version checks * update test * fix nom pools v3 migration * kick ci * kick ci --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent 0e90dec commit b7af797

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

frame/nomination-pools/src/migration.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,14 @@ pub mod v3 {
416416
let current = Pallet::<T>::current_storage_version();
417417
let onchain = Pallet::<T>::on_chain_storage_version();
418418

419-
log!(
420-
info,
421-
"Running migration with current storage version {:?} / onchain {:?}",
422-
current,
423-
onchain
424-
);
419+
if onchain == 2 {
420+
log!(
421+
info,
422+
"Running migration with current storage version {:?} / onchain {:?}",
423+
current,
424+
onchain
425+
);
425426

426-
if current > onchain {
427427
let mut metadata_iterated = 0u64;
428428
let mut metadata_removed = 0u64;
429429
Metadata::<T>::iter_keys()
@@ -437,7 +437,7 @@ pub mod v3 {
437437
metadata_removed += 1;
438438
Metadata::<T>::remove(&id);
439439
});
440-
current.put::<Pallet<T>>();
440+
StorageVersion::new(3).put::<Pallet<T>>();
441441
// metadata iterated + bonded pools read + a storage version read
442442
let total_reads = metadata_iterated * 2 + 1;
443443
// metadata removed + a storage version write
@@ -451,10 +451,6 @@ pub mod v3 {
451451

452452
#[cfg(feature = "try-runtime")]
453453
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
454-
ensure!(
455-
Pallet::<T>::current_storage_version() > Pallet::<T>::on_chain_storage_version(),
456-
"the on_chain version is equal or more than the current one"
457-
);
458454
Ok(Vec::new())
459455
}
460456

@@ -464,7 +460,10 @@ pub mod v3 {
464460
Metadata::<T>::iter_keys().all(|id| BondedPools::<T>::contains_key(&id)),
465461
"not all of the stale metadata has been removed"
466462
);
467-
ensure!(Pallet::<T>::on_chain_storage_version() == 3, "wrong storage version");
463+
ensure!(
464+
Pallet::<T>::on_chain_storage_version() >= 3,
465+
"nomination-pools::migration::v3: wrong storage version"
466+
);
468467
Ok(())
469468
}
470469
}
@@ -551,28 +550,35 @@ pub mod v4 {
551550

552551
#[cfg(feature = "try-runtime")]
553552
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
554-
ensure!(
555-
Pallet::<T>::current_storage_version() > Pallet::<T>::on_chain_storage_version(),
556-
"the on_chain version is equal or more than the current one"
557-
);
558553
Ok(Vec::new())
559554
}
560555

561556
#[cfg(feature = "try-runtime")]
562557
fn post_upgrade(_: Vec<u8>) -> Result<(), TryRuntimeError> {
563558
// ensure all BondedPools items now contain an `inner.commission: Commission` field.
564559
ensure!(
565-
BondedPools::<T>::iter().all(|(_, inner)| inner.commission.current.is_none() &&
566-
inner.commission.max.is_none() &&
567-
inner.commission.change_rate.is_none() &&
568-
inner.commission.throttle_from.is_none()),
569-
"a commission value has been incorrectly set"
560+
BondedPools::<T>::iter().all(|(_, inner)|
561+
// Check current
562+
(inner.commission.current.is_none() ||
563+
inner.commission.current.is_some()) &&
564+
// Check max
565+
(inner.commission.max.is_none() || inner.commission.max.is_some()) &&
566+
// Check change_rate
567+
(inner.commission.change_rate.is_none() ||
568+
inner.commission.change_rate.is_some()) &&
569+
// Check throttle_from
570+
(inner.commission.throttle_from.is_none() ||
571+
inner.commission.throttle_from.is_some())),
572+
"a commission value has not been set correctly"
570573
);
571574
ensure!(
572575
GlobalMaxCommission::<T>::get() == Some(U::get()),
573576
"global maximum commission error"
574577
);
575-
ensure!(Pallet::<T>::on_chain_storage_version() == 4, "wrong storage version");
578+
ensure!(
579+
Pallet::<T>::on_chain_storage_version() >= 4,
580+
"nomination-pools::migration::v4: wrong storage version"
581+
);
576582
Ok(())
577583
}
578584
}
@@ -636,11 +642,6 @@ pub mod v5 {
636642

637643
#[cfg(feature = "try-runtime")]
638644
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
639-
ensure!(
640-
Pallet::<T>::current_storage_version() > Pallet::<T>::on_chain_storage_version(),
641-
"the on_chain version is equal or more than the current one"
642-
);
643-
644645
let rpool_keys = RewardPools::<T>::iter_keys().count();
645646
let rpool_values = RewardPools::<T>::iter_values().count();
646647
if rpool_keys != rpool_values {
@@ -690,13 +691,16 @@ pub mod v5 {
690691
// `total_commission_claimed` field.
691692
ensure!(
692693
RewardPools::<T>::iter().all(|(_, reward_pool)| reward_pool
693-
.total_commission_pending
694-
.is_zero() && reward_pool
695-
.total_commission_claimed
696-
.is_zero()),
694+
.total_commission_pending >=
695+
Zero::zero() && reward_pool
696+
.total_commission_claimed >=
697+
Zero::zero()),
697698
"a commission value has been incorrectly set"
698699
);
699-
ensure!(Pallet::<T>::on_chain_storage_version() == 5, "wrong storage version");
700+
ensure!(
701+
Pallet::<T>::on_chain_storage_version() >= 5,
702+
"nomination-pools::migration::v5: wrong storage version"
703+
);
700704

701705
// These should not have been touched - just in case.
702706
ensure!(

frame/offences/src/migration.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ pub mod v1 {
5454
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
5555
#[cfg(feature = "try-runtime")]
5656
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
57-
let onchain = Pallet::<T>::on_chain_storage_version();
58-
ensure!(onchain < 1, "pallet_offences::MigrateToV1 migration can be deleted");
59-
6057
log::info!(
6158
target: LOG_TARGET,
6259
"Number of reports to refund and delete: {}",
@@ -67,19 +64,16 @@ pub mod v1 {
6764
}
6865

6966
fn on_runtime_upgrade() -> Weight {
70-
let onchain = Pallet::<T>::on_chain_storage_version();
71-
72-
if onchain > 0 {
67+
if Pallet::<T>::on_chain_storage_version() > 0 {
7368
log::info!(target: LOG_TARGET, "pallet_offences::MigrateToV1 should be removed");
7469
return T::DbWeight::get().reads(1)
7570
}
7671

7772
let keys_removed = v0::ReportsByKindIndex::<T>::clear(u32::MAX, None).unique as u64;
78-
let weight = T::DbWeight::get().reads_writes(keys_removed, keys_removed);
79-
8073
StorageVersion::new(1).put::<Pallet<T>>();
8174

82-
weight
75+
// + 1 for reading/writing the new storage version
76+
T::DbWeight::get().reads_writes(keys_removed + 1, keys_removed + 1)
8377
}
8478

8579
#[cfg(feature = "try-runtime")]
@@ -147,7 +141,7 @@ mod test {
147141
ext.execute_with(|| {
148142
assert_eq!(
149143
v1::MigrateToV1::<T>::on_runtime_upgrade(),
150-
<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1),
144+
<T as frame_system::Config>::DbWeight::get().reads_writes(2, 2),
151145
);
152146

153147
assert!(<v0::ReportsByKindIndex<T>>::iter_values().count() == 0);

0 commit comments

Comments
 (0)