pallet_core_fellowship/
migration.rs1use super::*;
20use frame_support::{
21 pallet_prelude::*,
22 storage_alias,
23 traits::{DefensiveTruncateFrom, UncheckedOnRuntimeUpgrade},
24 BoundedVec,
25};
26
27#[cfg(feature = "try-runtime")]
28use alloc::vec::Vec;
29#[cfg(feature = "try-runtime")]
30use sp_runtime::TryRuntimeError;
31
32mod v0 {
33 use frame_system::pallet_prelude::BlockNumberFor;
34
35 use super::*;
36
37 #[derive(Encode, Decode, Eq, PartialEq, Clone, TypeInfo, MaxEncodedLen, RuntimeDebug)]
38 pub struct ParamsType<Balance, BlockNumber, const RANKS: usize> {
39 pub active_salary: [Balance; RANKS],
40 pub passive_salary: [Balance; RANKS],
41 pub demotion_period: [BlockNumber; RANKS],
42 pub min_promotion_period: [BlockNumber; RANKS],
43 pub offboard_timeout: BlockNumber,
44 }
45
46 impl<Balance: Default + Copy, BlockNumber: Default + Copy, const RANKS: usize> Default
47 for ParamsType<Balance, BlockNumber, RANKS>
48 {
49 fn default() -> Self {
50 Self {
51 active_salary: [Balance::default(); RANKS],
52 passive_salary: [Balance::default(); RANKS],
53 demotion_period: [BlockNumber::default(); RANKS],
54 min_promotion_period: [BlockNumber::default(); RANKS],
55 offboard_timeout: BlockNumber::default(),
56 }
57 }
58 }
59
60 pub(crate) const RANK_COUNT: usize = 9;
62
63 pub type ParamsOf<T, I> = ParamsType<<T as Config<I>>::Balance, BlockNumberFor<T>, RANK_COUNT>;
64
65 #[storage_alias]
67 pub type Params<T: Config<I>, I: 'static> =
68 StorageValue<Pallet<T, I>, ParamsOf<T, I>, ValueQuery>;
69}
70
71pub struct MigrateToV1<T, I = ()>(PhantomData<(T, I)>);
72impl<T: Config<I>, I: 'static> UncheckedOnRuntimeUpgrade for MigrateToV1<T, I> {
73 #[cfg(feature = "try-runtime")]
74 fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
75 ensure!(
76 T::MaxRank::get() as usize >= v0::RANK_COUNT,
77 "pallet-core-fellowship: new bound should not truncate"
78 );
79 Ok(Default::default())
80 }
81
82 fn on_runtime_upgrade() -> frame_support::weights::Weight {
83 let old_value = v0::Params::<T, I>::take();
85 let new = crate::ParamsType {
87 active_salary: BoundedVec::defensive_truncate_from(old_value.active_salary.to_vec()),
88 passive_salary: BoundedVec::defensive_truncate_from(old_value.passive_salary.to_vec()),
89 demotion_period: BoundedVec::defensive_truncate_from(
90 old_value.demotion_period.to_vec(),
91 ),
92 min_promotion_period: BoundedVec::defensive_truncate_from(
93 old_value.min_promotion_period.to_vec(),
94 ),
95 offboard_timeout: old_value.offboard_timeout,
96 };
97 crate::Params::<T, I>::put(new);
98 T::DbWeight::get().reads_writes(1, 1)
99 }
100}
101
102pub type MigrateV0ToV1<T, I> = frame_support::migrations::VersionedMigration<
108 0, 1, MigrateToV1<T, I>,
111 crate::pallet::Pallet<T, I>,
112 <T as frame_system::Config>::DbWeight,
113>;