Skip to content

Commit 5d6fc17

Browse files
author
José Molina
committed
Improve migration for try-state
1 parent ffae449 commit 5d6fc17

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/migrations/v2.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ use frame_support::weights::WeightMeter;
2626
use sp_runtime::{FixedU128, Percent};
2727

2828
#[cfg(feature = "try-runtime")]
29-
use sp_std::vec::Vec;
29+
use sp_runtime::TryRuntimeError;
3030
#[cfg(feature = "try-runtime")]
3131
use sp_std::collections::btree_map::BTreeMap;
3232
#[cfg(feature = "try-runtime")]
33-
use sp_runtime::TryRuntimeError;
34-
33+
use sp_std::vec::Vec;
3534

3635
pub(crate) mod v1 {
3736
use super::*;
@@ -87,6 +86,8 @@ pub enum MigrationSteps<T: Config> {
8786
MigrateAutocompounding { cursor: Option<T::AccountId> },
8887
/// [`crate::ClaimableRewards`] are to be set to zero, resetting all rewards.
8988
ResetClaimableRewards,
89+
/// Changes the storage version to 2.
90+
ChangeStorageVersion,
9091
/// No more operations to be performed.
9192
Noop,
9293
}
@@ -100,12 +101,22 @@ pub enum MigrationSteps<T: Config> {
100101
pub struct LazyMigrationV1ToV2<T: Config>(PhantomData<T>);
101102

102103
impl<T: Config> LazyMigrationV1ToV2<T> {
104+
pub(crate) fn set_storage_version(meter: &mut WeightMeter) -> MigrationSteps<T> {
105+
let required = T::DbWeight::get().reads_writes(0, 1);
106+
if meter.try_consume(required).is_ok() {
107+
StorageVersion::new(Self::id().version_to as u16).put::<Pallet<T>>();
108+
MigrationSteps::Noop
109+
} else {
110+
MigrationSteps::ChangeStorageVersion
111+
}
112+
}
113+
103114
pub(crate) fn reset_rewards(meter: &mut WeightMeter) -> MigrationSteps<T> {
104115
// This step can be manually calculated.
105116
let required = T::DbWeight::get().reads_writes(0, 1);
106117
if meter.try_consume(required).is_ok() {
107118
ClaimableRewards::<T>::set(Zero::zero());
108-
MigrationSteps::Noop
119+
Self::set_storage_version(meter)
109120
} else {
110121
MigrationSteps::ResetClaimableRewards
111122
}
@@ -235,6 +246,7 @@ impl<T: Config> SteppedMigration for LazyMigrationV1ToV2<T> {
235246
Some(Self::migrate_autocompounding(meter, checkpoint))
236247
},
237248
MigrationSteps::ResetClaimableRewards => Some(Self::reset_rewards(meter)),
249+
MigrationSteps::ChangeStorageVersion => Some(Self::set_storage_version(meter)),
238250
MigrationSteps::Noop => None,
239251
};
240252

@@ -246,11 +258,6 @@ impl<T: Config> SteppedMigration for LazyMigrationV1ToV2<T> {
246258
use codec::Encode;
247259

248260
// Return the state of the storage before the migration.
249-
assert_ne!(
250-
Pallet::<T>::on_chain_storage_version(),
251-
Self::id().version_from as u16,
252-
"Migration pre-upgrade failed: the storage version is not the expected one"
253-
);
254261
let map: BTreeMap<(T::AccountId, T::AccountId), v1::CandidateStakeInfo<BalanceOf<T>>> =
255262
v1::CandidateStake::<T>::iter().map(|(k1, k2, v)| ((k1, k2), v)).collect();
256263
let autocompound = v1::AutoCompound::<T>::iter().collect::<Vec<_>>();
@@ -260,11 +267,10 @@ impl<T: Config> SteppedMigration for LazyMigrationV1ToV2<T> {
260267
#[cfg(feature = "try-runtime")]
261268
fn post_upgrade(prev: Vec<u8>) -> Result<(), TryRuntimeError> {
262269
use codec::Decode;
263-
264270
// Check the state of the storage after the migration.
265-
assert_ne!(
271+
assert_eq!(
266272
Pallet::<T>::on_chain_storage_version(),
267-
Self::id().version_to as u16,
273+
StorageVersion::new(Self::id().version_to as u16),
268274
"Migration post-upgrade failed: the storage version is not the expected one"
269275
);
270276
let (prev_map, prev_autocompound) = <(
@@ -314,6 +320,7 @@ mod tests {
314320
fn migration_of_single_element_should_work() {
315321
new_test_ext().execute_with(|| {
316322
StorageVersion::new(1).put::<Pallet<Test>>();
323+
assert_eq!(Pallet::<Test>::on_chain_storage_version(), 1);
317324
initialize_to_block(1);
318325
ClaimableRewards::<Test>::set(100);
319326

@@ -335,13 +342,15 @@ mod tests {
335342
);
336343
assert_eq!(AutoCompound::<Test>::get(Layer::Commit, &1), true);
337344
assert_eq!(ClaimableRewards::<Test>::get(), 0);
345+
assert_eq!(Pallet::<Test>::on_chain_storage_version(), 2);
338346
});
339347
}
340348

341349
#[test]
342350
fn migration_of_many_elements_should_work() {
343351
new_test_ext().execute_with(|| {
344352
StorageVersion::new(1).put::<Pallet<Test>>();
353+
assert_eq!(Pallet::<Test>::on_chain_storage_version(), 1);
345354
initialize_to_block(1);
346355
ClaimableRewards::<Test>::set(100);
347356

@@ -367,6 +376,7 @@ mod tests {
367376
assert_eq!(AutoCompound::<Test>::get(Layer::Commit, &i), true);
368377
}
369378
assert_eq!(ClaimableRewards::<Test>::get(), 0);
379+
assert_eq!(Pallet::<Test>::on_chain_storage_version(), StorageVersion::new(2));
370380
});
371381
}
372382
}

0 commit comments

Comments
 (0)