referrerpolicy=no-referrer-when-downgrade

pallet_vesting/
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
18//! Storage migrations for the vesting pallet.
19
20use super::*;
21use alloc::vec;
22
23// Migration from single schedule to multiple schedules.
24pub mod v1 {
25	use super::*;
26
27	#[cfg(feature = "try-runtime")]
28	pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
29		assert!(StorageVersion::<T>::get() == Releases::V0, "Storage version too high.");
30
31		log::debug!(
32			target: "runtime::vesting",
33			"migration: Vesting storage version v1 PRE migration checks successful!"
34		);
35
36		Ok(())
37	}
38
39	/// Migrate from single schedule to multi schedule storage.
40	/// WARNING: This migration will delete schedules if `MaxVestingSchedules < 1`.
41	pub fn migrate<T: Config>() -> Weight {
42		let mut reads_writes = 0;
43
44		Vesting::<T>::translate::<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>, _>(
45			|_key, vesting_info| {
46				reads_writes += 1;
47				let v: Option<
48					BoundedVec<
49						VestingInfo<BalanceOf<T>, BlockNumberFor<T>>,
50						MaxVestingSchedulesGet<T>,
51					>,
52				> = vec![vesting_info].try_into().ok();
53
54				if v.is_none() {
55					log::warn!(
56						target: "runtime::vesting",
57						"migration: Failed to move a vesting schedule into a BoundedVec"
58					);
59				}
60
61				v
62			},
63		);
64
65		T::DbWeight::get().reads_writes(reads_writes, reads_writes)
66	}
67
68	#[cfg(feature = "try-runtime")]
69	pub fn post_migrate<T: Config>() -> Result<(), &'static str> {
70		assert_eq!(StorageVersion::<T>::get(), Releases::V1);
71
72		for (_key, schedules) in Vesting::<T>::iter() {
73			assert!(
74				schedules.len() >= 1,
75				"A bounded vec with incorrect count of items was created."
76			);
77
78			for s in schedules {
79				// It is ok if this does not pass, but ideally pre-existing schedules would pass
80				// this validation logic so we can be more confident about edge cases.
81				if !s.is_valid() {
82					log::warn!(
83						target: "runtime::vesting",
84						"migration: A schedule does not pass new validation logic.",
85					)
86				}
87			}
88		}
89
90		log::debug!(
91			target: "runtime::vesting",
92			"migration: Vesting storage version v1 POST migration checks successful!"
93		);
94		Ok(())
95	}
96}