1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

            
4
// Polkadot is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Polkadot is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
use crate::{
18
	pallet::CurrentMigration, Config, Pallet, VersionMigrationStage, VersionNotifyTargets,
19
};
20
use frame_support::{
21
	pallet_prelude::*,
22
	traits::{OnRuntimeUpgrade, StorageVersion, UncheckedOnRuntimeUpgrade},
23
	weights::Weight,
24
};
25

            
26
const DEFAULT_PROOF_SIZE: u64 = 64 * 1024;
27

            
28
pub mod v1 {
29
	use super::*;
30
	use crate::{CurrentMigration, VersionMigrationStage};
31

            
32
	/// Named with the 'VersionUnchecked'-prefix because although this implements some version
33
	/// checking, the version checking is not complete as it will begin failing after the upgrade is
34
	/// enacted on-chain.
35
	///
36
	/// Use experimental [`MigrateToV1`] instead.
37
	pub struct VersionUncheckedMigrateToV1<T>(core::marker::PhantomData<T>);
38
	impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
39
		fn on_runtime_upgrade() -> Weight {
40
			let mut weight = T::DbWeight::get().reads(1);
41

            
42
			if StorageVersion::get::<Pallet<T>>() != 0 {
43
				log::warn!("skipping v1, should be removed");
44
				return weight
45
			}
46

            
47
			weight.saturating_accrue(T::DbWeight::get().writes(1));
48
			CurrentMigration::<T>::put(VersionMigrationStage::default());
49

            
50
			let translate = |pre: (u64, u64, u32)| -> Option<(u64, Weight, u32)> {
51
				weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
52
				let translated = (pre.0, Weight::from_parts(pre.1, DEFAULT_PROOF_SIZE), pre.2);
53
				log::info!("Migrated VersionNotifyTarget {:?} to {:?}", pre, translated);
54
				Some(translated)
55
			};
56

            
57
			VersionNotifyTargets::<T>::translate_values(translate);
58

            
59
			log::info!("v1 applied successfully");
60
			weight.saturating_accrue(T::DbWeight::get().writes(1));
61
			StorageVersion::new(1).put::<Pallet<T>>();
62
			weight
63
		}
64
	}
65

            
66
	/// Version checked migration to v1.
67
	///
68
	/// Wrapped in [`frame_support::migrations::VersionedMigration`] so the pre/post checks don't
69
	/// begin failing after the upgrade is enacted on-chain.
70
	pub type MigrateToV1<T> = frame_support::migrations::VersionedMigration<
71
		0,
72
		1,
73
		VersionUncheckedMigrateToV1<T>,
74
		crate::pallet::Pallet<T>,
75
		<T as frame_system::Config>::DbWeight,
76
	>;
77
}
78

            
79
/// When adding a new XCM version, we need to run this migration for `pallet_xcm` to ensure that all
80
/// previously stored data with subkey prefix `XCM_VERSION-1` (and below) are migrated to the
81
/// `XCM_VERSION`.
82
///
83
/// NOTE: This migration can be permanently added to the runtime migrations.
84
pub struct MigrateToLatestXcmVersion<T>(core::marker::PhantomData<T>);
85
impl<T: Config> OnRuntimeUpgrade for MigrateToLatestXcmVersion<T> {
86
	fn on_runtime_upgrade() -> Weight {
87
		CurrentMigration::<T>::put(VersionMigrationStage::default());
88
		T::DbWeight::get().writes(1)
89
	}
90
}