referrerpolicy=no-referrer-when-downgrade

pallet_recovery/migrations/
v0.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//! V0 storage definitions for the recovery pallet.
19//!
20//! These represent the old storage layout before the v1 migration.
21
22use codec::{Decode, Encode, MaxEncodedLen};
23use frame::{
24	deps::{
25		sp_io::hashing::blake2_256,
26		sp_runtime::traits::{One, TrailingZeroInput},
27	},
28	traits::BlockNumberProvider,
29};
30use frame_support::{
31	storage_alias,
32	traits::{Currency, ReservableCurrency},
33	Blake2_128Concat, BoundedVec, Twox64Concat,
34};
35use scale_info::TypeInfo;
36
37/// Extended migration config for the migration.
38pub trait MigrationConfig: crate::pallet::Config {
39	/// The currency to unreserve deposits.
40	type Currency: ReservableCurrency<Self::AccountId, Balance = crate::BalanceOf<Self>>;
41}
42
43/// Derive a multi-account ID from the sorted list of accounts and the threshold.
44///
45/// This is used to compute the inheritor for migrated recovery configs - the inheritor
46/// will be the multisig account that the friends can control together.
47///
48/// NOTE: `who` must be sorted. If it is not, then you'll get the wrong answer.
49pub fn multi_account_id<AccountId: Encode + Decode>(
50	who: &[AccountId],
51	threshold: u16,
52) -> AccountId {
53	let entropy = (b"modlpy/utilisuba", who, threshold).using_encoded(blake2_256);
54	Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref()))
55		.expect("infinite length input; no invalid inputs for type; qed")
56}
57
58/// Balance type for v0 storage.
59pub type BalanceOf<T> =
60	<<T as MigrationConfig>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
61
62/// Block number type from provider.
63pub type BlockNumberFromProviderOf<T> =
64	<<T as crate::pallet::Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
65
66/// Friends bounded vec type.
67pub type FriendsOf<T> = BoundedVec<
68	<T as frame_system::Config>::AccountId,
69	<T as crate::pallet::Config>::MaxFriendsPerConfig,
70>;
71
72/// Recovery configuration structure from v0.
73#[derive(Clone, Eq, PartialEq, Encode, Decode, Default, TypeInfo, MaxEncodedLen)]
74pub struct RecoveryConfig<BlockNumber, Balance, Friends> {
75	/// The minimum number of blocks since the start of the recovery process before the
76	/// account can be recovered.
77	pub delay_period: BlockNumber,
78	/// The amount held in reserve of the `depositor`,
79	/// to be returned once this configuration is removed.
80	pub deposit: Balance,
81	/// The list of friends which can help recover an account. Always sorted.
82	pub friends: Friends,
83	/// The number of approving friends needed to recover an account.
84	pub threshold: u16,
85}
86
87impl<BlockNumber: Clone + Ord + One, Balance, Friends>
88	RecoveryConfig<BlockNumber, Balance, Friends>
89{
90	/// Convert to a V1 `FriendGroup`.
91	pub fn into_v1_friend_group<AccountId>(
92		self,
93		inheritor: AccountId,
94	) -> crate::FriendGroup<BlockNumber, AccountId, Friends> {
95		crate::FriendGroup {
96			friends: self.friends,
97			friends_needed: self.threshold as u32,
98			inheritor,
99			inheritance_delay: self.delay_period.clone(),
100			inheritance_priority: 0,
101			// At least one block delay to prevent mempool frontrunning
102			cancel_delay: self.delay_period.max(One::one()),
103		}
104	}
105}
106
107/// Old active recovery structure from v0.
108#[derive(Clone, Eq, PartialEq, Encode, Decode, Default, TypeInfo, MaxEncodedLen)]
109pub struct ActiveRecovery<BlockNumber, Balance, Friends> {
110	/// The block number when the recovery process started.
111	pub created: BlockNumber,
112	/// The amount held in reserve of the `depositor`,
113	/// to be returned once this recovery process is closed.
114	pub deposit: Balance,
115	/// The friends which have vouched so far. Always sorted.
116	pub friends: Friends,
117}
118
119/// The set of recoverable accounts and their recovery configuration.
120#[storage_alias]
121pub type Recoverable<T: MigrationConfig> = StorageMap<
122	crate::pallet::Pallet<T>,
123	Twox64Concat,
124	<T as frame_system::Config>::AccountId,
125	RecoveryConfig<BlockNumberFromProviderOf<T>, BalanceOf<T>, FriendsOf<T>>,
126>;
127
128/// Old storage: Active recovery attempts.
129///
130/// First account is the account to be recovered, and the second account
131/// is the user trying to recover the account.
132#[storage_alias]
133pub type ActiveRecoveries<T: MigrationConfig> = StorageDoubleMap<
134	crate::pallet::Pallet<T>,
135	Twox64Concat,
136	<T as frame_system::Config>::AccountId,
137	Twox64Concat,
138	<T as frame_system::Config>::AccountId,
139	ActiveRecovery<BlockNumberFromProviderOf<T>, BalanceOf<T>, FriendsOf<T>>,
140>;
141
142/// Old storage: The list of allowed proxy accounts.
143///
144/// Map from the user who can access it to the recovered account.
145#[storage_alias]
146pub type Proxy<T: MigrationConfig> = StorageMap<
147	crate::pallet::Pallet<T>,
148	Blake2_128Concat,
149	<T as frame_system::Config>::AccountId,
150	<T as frame_system::Config>::AccountId,
151>;