referrerpolicy=no-referrer-when-downgrade

pallet_multisig/
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// Migrations for Multisig Pallet
19
20use crate::*;
21use frame::prelude::*;
22
23pub mod v1 {
24	use super::*;
25
26	type OpaqueCall<T> = frame::traits::WrapperKeepOpaque<<T as Config>::RuntimeCall>;
27
28	#[frame::storage_alias]
29	type Calls<T: Config> = StorageMap<
30		Pallet<T>,
31		Identity,
32		[u8; 32],
33		(OpaqueCall<T>, <T as frame_system::Config>::AccountId, BalanceOf<T>),
34	>;
35
36	pub struct MigrateToV1<T>(core::marker::PhantomData<T>);
37	impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
38		#[cfg(feature = "try-runtime")]
39		fn pre_upgrade() -> Result<Vec<u8>, frame::try_runtime::TryRuntimeError> {
40			log!(info, "Number of calls to refund and delete: {}", Calls::<T>::iter().count());
41
42			Ok(Vec::new())
43		}
44
45		fn on_runtime_upgrade() -> Weight {
46			use frame::traits::ReservableCurrency as _;
47			let current = Pallet::<T>::in_code_storage_version();
48			let onchain = Pallet::<T>::on_chain_storage_version();
49
50			if onchain > 0 {
51				log!(info, "MigrateToV1 should be removed");
52				return T::DbWeight::get().reads(1)
53			}
54
55			let mut call_count = 0u64;
56			Calls::<T>::drain().for_each(|(_call_hash, (_data, caller, deposit))| {
57				T::Currency::unreserve(&caller, deposit);
58				call_count.saturating_inc();
59			});
60
61			current.put::<Pallet<T>>();
62
63			T::DbWeight::get().reads_writes(
64				// Reads: Get Calls + Get Version
65				call_count.saturating_add(1),
66				// Writes: Drain Calls + Unreserves + Set version
67				call_count.saturating_mul(2).saturating_add(1),
68			)
69		}
70
71		#[cfg(feature = "try-runtime")]
72		fn post_upgrade(_state: Vec<u8>) -> Result<(), frame::try_runtime::TryRuntimeError> {
73			ensure!(
74				Calls::<T>::iter().count() == 0,
75				"there are some dangling calls that need to be destroyed and refunded"
76			);
77			Ok(())
78		}
79	}
80}