referrerpolicy=no-referrer-when-downgrade

pallet_fast_unstake/
migrations.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
18pub mod v1 {
19	use crate::{types::BalanceOf, *};
20	use alloc::vec::Vec;
21	use frame_support::{
22		storage::unhashed,
23		traits::{Defensive, Get, GetStorageVersion, OnRuntimeUpgrade},
24		weights::Weight,
25	};
26	use sp_staking::EraIndex;
27
28	#[cfg(feature = "try-runtime")]
29	use frame_support::ensure;
30	#[cfg(feature = "try-runtime")]
31	use sp_runtime::TryRuntimeError;
32
33	pub struct MigrateToV1<T>(core::marker::PhantomData<T>);
34	impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
35		fn on_runtime_upgrade() -> Weight {
36			let current = Pallet::<T>::in_code_storage_version();
37			let onchain = Pallet::<T>::on_chain_storage_version();
38
39			log!(
40				info,
41				"Running migration with in-code storage version {:?} / onchain {:?}",
42				current,
43				onchain
44			);
45
46			if current == 1 && onchain == 0 {
47				// update the version nonetheless.
48				current.put::<Pallet<T>>();
49
50				// if a head exists, then we put them back into the queue.
51				if Head::<T>::exists() {
52					if let Some((stash, _, deposit)) =
53						unhashed::take::<(T::AccountId, Vec<EraIndex>, BalanceOf<T>)>(
54							&Head::<T>::hashed_key(),
55						)
56						.defensive()
57					{
58						Queue::<T>::insert(stash, deposit);
59					} else {
60						// not much we can do here -- head is already deleted.
61					}
62					T::DbWeight::get().reads_writes(2, 3)
63				} else {
64					T::DbWeight::get().reads(2)
65				}
66			} else {
67				log!(info, "Migration did not execute. This probably should be removed");
68				T::DbWeight::get().reads(1)
69			}
70		}
71
72		#[cfg(feature = "try-runtime")]
73		fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
74			ensure!(
75				Pallet::<T>::on_chain_storage_version() == 0,
76				"The onchain storage version must be zero for the migration to execute."
77			);
78			Ok(Default::default())
79		}
80
81		#[cfg(feature = "try-runtime")]
82		fn post_upgrade(_: Vec<u8>) -> Result<(), TryRuntimeError> {
83			ensure!(
84				Pallet::<T>::on_chain_storage_version() == 1,
85				"The onchain version must be updated after the migration."
86			);
87			Ok(())
88		}
89	}
90}