referrerpolicy=no-referrer-when-downgrade

frame_system/migrations/
mod.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//! Migrate the reference counting state.
19
20use super::LOG_TARGET;
21use crate::{Config, Pallet};
22use codec::{Decode, Encode, FullCodec};
23use frame_support::{
24	pallet_prelude::ValueQuery, traits::PalletInfoAccess, weights::Weight, Blake2_128Concat,
25};
26use sp_runtime::RuntimeDebug;
27
28/// Type used to encode the number of references an account has.
29type RefCount = u32;
30
31/// Information of an account.
32#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)]
33struct AccountInfo<Nonce, AccountData> {
34	nonce: Nonce,
35	consumers: RefCount,
36	providers: RefCount,
37	sufficients: RefCount,
38	data: AccountData,
39}
40
41/// Trait to implement to give information about types used for migration
42pub trait V2ToV3 {
43	/// The system pallet.
44	type Pallet: 'static + PalletInfoAccess;
45
46	/// System config account id
47	type AccountId: 'static + FullCodec;
48
49	/// System config nonce
50	type Nonce: 'static + FullCodec + Copy;
51
52	/// System config account data
53	type AccountData: 'static + FullCodec;
54}
55
56#[frame_support::storage_alias]
57type UpgradedToU32RefCount<T: Config> = StorageValue<Pallet<T>, bool, ValueQuery>;
58
59#[frame_support::storage_alias]
60type UpgradedToTripleRefCount<T: Config> = StorageValue<Pallet<T>, bool, ValueQuery>;
61
62#[frame_support::storage_alias]
63type Account<V, T: Config> = StorageMap<
64	Pallet<T>,
65	Blake2_128Concat,
66	<V as V2ToV3>::AccountId,
67	AccountInfo<<V as V2ToV3>::Nonce, <V as V2ToV3>::AccountData>,
68>;
69
70/// Migrate from unique `u8` reference counting to triple `u32` reference counting.
71pub fn migrate_from_single_u8_to_triple_ref_count<V: V2ToV3, T: Config>() -> Weight {
72	let mut translated: usize = 0;
73	<Account<V, T>>::translate::<(V::Nonce, u8, V::AccountData), _>(|_key, (nonce, rc, data)| {
74		translated += 1;
75		Some(AccountInfo { nonce, consumers: rc as RefCount, providers: 1, sufficients: 0, data })
76	});
77	log::info!(
78		target: LOG_TARGET,
79		"Applied migration from single u8 to triple reference counting to {:?} elements.",
80		translated
81	);
82	<UpgradedToU32RefCount<T>>::put(true);
83	<UpgradedToTripleRefCount<T>>::put(true);
84	Weight::MAX
85}
86
87/// Migrate from unique `u32` reference counting to triple `u32` reference counting.
88pub fn migrate_from_single_to_triple_ref_count<V: V2ToV3, T: Config>() -> Weight {
89	let mut translated: usize = 0;
90	<Account<V, T>>::translate::<(V::Nonce, RefCount, V::AccountData), _>(
91		|_key, (nonce, consumers, data)| {
92			translated += 1;
93			Some(AccountInfo { nonce, consumers, providers: 1, sufficients: 0, data })
94		},
95	);
96	log::info!(
97		target: LOG_TARGET,
98		"Applied migration from single to triple reference counting to {:?} elements.",
99		translated
100	);
101	<UpgradedToTripleRefCount<T>>::put(true);
102	Weight::MAX
103}
104
105/// Migrate from dual `u32` reference counting to triple `u32` reference counting.
106pub fn migrate_from_dual_to_triple_ref_count<V: V2ToV3, T: Config>() -> Weight {
107	let mut translated: usize = 0;
108	<Account<V, T>>::translate::<(V::Nonce, RefCount, RefCount, V::AccountData), _>(
109		|_key, (nonce, consumers, providers, data)| {
110			translated += 1;
111			Some(AccountInfo { nonce, consumers, providers, sufficients: 0, data })
112		},
113	);
114	log::info!(
115		target: LOG_TARGET,
116		"Applied migration from dual to triple reference counting to {:?} elements.",
117		translated
118	);
119	<UpgradedToTripleRefCount<T>>::put(true);
120	Weight::MAX
121}