Skip to content

Commit

Permalink
remove nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
Doordashcon committed Apr 18, 2024
1 parent 23a0d06 commit e4fc2a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion substrate/frame/core-fellowship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub mod pallet {
};
use frame_system::{ensure_root, pallet_prelude::*};
/// The in-code storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down
64 changes: 35 additions & 29 deletions substrate/frame/core-fellowship/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::*;
use frame_support::{
pallet_prelude::*, storage_alias, traits::{UncheckedOnRuntimeUpgrade, DefensiveTruncateFrom}, BoundedVec,
pallet_prelude::*,
storage_alias,
traits::{DefensiveTruncateFrom, UncheckedOnRuntimeUpgrade},
BoundedVec,
};

#[cfg(feature = "try-runtime")]
Expand Down Expand Up @@ -45,43 +48,46 @@ mod v0 {
StorageValue<Pallet<T, I>, ParamsOf<T, I>, ValueQuery>;
}

mod v1 {
use super::*;

pub struct Migration<T, I = ()>(PhantomData<(T, I)>);
impl<T: Config<I>, I: 'static> UncheckedOnRuntimeUpgrade for Migration<T, I> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
ensure!(T::MaxRank::get() >= v0::RANK_COUNT as u32, "pallet-core-fellowship: new bound should not truncate");
Ok(Default::default())
}
pub struct Migration<T, I = ()>(PhantomData<(T, I)>);
impl<T: Config<I>, I: 'static> UncheckedOnRuntimeUpgrade for Migration<T, I> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
ensure!(
T::MaxRank::get() >= v0::RANK_COUNT as u32,
"pallet-core-fellowship: new bound should not truncate"
);
Ok(Default::default())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
// Read the old value from storage
let old_value = v0::Params::<T, I>::take();
// Write the new value to storage
let new = crate::ParamsType {
active_salary: BoundedVec::defensive_truncate_from(old_value.active_salary.to_vec()),
passive_salary: BoundedVec::defensive_truncate_from(old_value.passive_salary.to_vec()),
demotion_period: BoundedVec::defensive_truncate_from(old_value.demotion_period.to_vec()),
min_promotion_period: BoundedVec::defensive_truncate_from(old_value.min_promotion_period.to_vec()),
offboard_timeout: old_value.offboard_timeout,
};
crate::Params::<T, I>::put(new);
T::DbWeight::get().reads_writes(1, 1)
}
fn on_runtime_upgrade() -> frame_support::weights::Weight {
// Read the old value from storage
let old_value = v0::Params::<T, I>::take();
// Write the new value to storage
let new = crate::ParamsType {
active_salary: BoundedVec::defensive_truncate_from(old_value.active_salary.to_vec()),
passive_salary: BoundedVec::defensive_truncate_from(old_value.passive_salary.to_vec()),
demotion_period: BoundedVec::defensive_truncate_from(
old_value.demotion_period.to_vec(),
),
min_promotion_period: BoundedVec::defensive_truncate_from(
old_value.min_promotion_period.to_vec(),
),
offboard_timeout: old_value.offboard_timeout,
};
crate::Params::<T, I>::put(new);
T::DbWeight::get().reads_writes(1, 1)
}
}

/// [`UncheckedOnRuntimeUpgrade`] implementation [`v1::Migration`] wrapped in a
/// [`UncheckedOnRuntimeUpgrade`] implementation [`Migration`] wrapped in a
/// [`VersionedMigration`](frame_support::migrations::VersionedMigration), which ensures that:
/// - The migration only runs once when the on-chain storage version is 0
/// - The on-chain storage version is updated to `1` after the migration executes
/// - The on-chain storage version is updated to `2` after the migration executes
/// - Reads/Writes from checking/settings the on-chain storage version are accounted for
pub type Migrate<T, I> = frame_support::migrations::VersionedMigration<
0, // The migration will only execute when the on-chain storage version is 0
1, // The on-chain storage version will be set to 1 after the migration is complete
v1::Migration<T, I>,
2, // The on-chain storage version will be set to 2 after the migration is complete
Migration<T, I>,
crate::pallet::Pallet<T, I>,
<T as frame_system::Config>::DbWeight,
>;

0 comments on commit e4fc2a1

Please sign in to comment.