referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_aura_ext/
migration.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16extern crate alloc;
17
18use crate::{Config, Pallet};
19#[cfg(feature = "try-runtime")]
20use alloc::vec::Vec;
21use frame_support::{migrations::VersionedMigration, pallet_prelude::StorageVersion};
22
23/// The in-code storage version.
24pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
25
26mod v0 {
27	use super::*;
28	use frame_support::{pallet_prelude::OptionQuery, storage_alias};
29	use sp_consensus_aura::Slot;
30
31	/// Current slot paired with a number of authored blocks.
32	///
33	/// Updated on each block initialization.
34	#[storage_alias]
35	pub(super) type SlotInfo<T: Config> = StorageValue<Pallet<T>, (Slot, u32), OptionQuery>;
36}
37mod v1 {
38	use super::*;
39	use frame_support::{pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade};
40
41	pub struct UncheckedMigrationToV1<T: Config>(PhantomData<T>);
42
43	impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV1<T> {
44		fn on_runtime_upgrade() -> Weight {
45			let mut weight: Weight = Weight::zero();
46			weight += migrate::<T>();
47			weight
48		}
49
50		#[cfg(feature = "try-runtime")]
51		fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
52			Ok(Vec::new())
53		}
54		#[cfg(feature = "try-runtime")]
55		fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
56			ensure!(!v0::SlotInfo::<T>::exists(), "SlotInfo should not exist");
57			Ok(())
58		}
59	}
60
61	pub fn migrate<T: Config>() -> Weight {
62		v0::SlotInfo::<T>::kill();
63		T::DbWeight::get().writes(1)
64	}
65}
66
67/// Migrate `V0` to `V1`.
68pub type MigrateV0ToV1<T> = VersionedMigration<
69	0,
70	1,
71	v1::UncheckedMigrationToV1<T>,
72	Pallet<T>,
73	<T as frame_system::Config>::DbWeight,
74>;