referrerpolicy=no-referrer-when-downgrade

pallet_core_fellowship/
migration.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Storage migrations for the core-fellowship pallet.
19use 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	/// Number of available ranks from old version.
61	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	/// V0 type for [`crate::Params`].
66	#[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		// Read the old value from storage
84		let old_value = v0::Params::<T, I>::take();
85		// Write the new value to storage
86		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
102/// [`UncheckedOnRuntimeUpgrade`] implementation [`MigrateToV1`] wrapped in a
103/// [`VersionedMigration`](frame_support::migrations::VersionedMigration), which ensures that:
104/// - The migration only runs once when the on-chain storage version is 0
105/// - The on-chain storage version is updated to `1` after the migration executes
106/// - Reads/Writes from checking/settings the on-chain storage version are accounted for
107pub type MigrateV0ToV1<T, I> = frame_support::migrations::VersionedMigration<
108	0, // The migration will only execute when the on-chain storage version is 0
109	1, // The on-chain storage version will be set to 1 after the migration is complete
110	MigrateToV1<T, I>,
111	crate::pallet::Pallet<T, I>,
112	<T as frame_system::Config>::DbWeight,
113>;