referrerpolicy=no-referrer-when-downgrade

pallet_delegated_staking/
migration.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
18use super::*;
19use frame_support::traits::OnRuntimeUpgrade;
20
21#[cfg(feature = "try-runtime")]
22use sp_runtime::TryRuntimeError;
23
24pub mod unversioned {
25	use super::*;
26	#[cfg(feature = "try-runtime")]
27	use alloc::vec::Vec;
28	use sp_runtime::traits::AccountIdConversion;
29
30	/// Migrates `ProxyDelegator` accounts with better entropy than the old logic which didn't take
31	/// into account all the bytes of the agent account ID.
32	pub struct ProxyDelegatorMigration<T, MaxAgents>(PhantomData<(T, MaxAgents)>);
33
34	impl<T: Config, MaxAgents: Get<u32>> OnRuntimeUpgrade for ProxyDelegatorMigration<T, MaxAgents> {
35		fn on_runtime_upgrade() -> Weight {
36			let mut weight = Weight::zero();
37			let old_proxy_delegator = |agent: T::AccountId| {
38				T::PalletId::get()
39					.into_sub_account_truncating((AccountType::ProxyDelegator, agent.clone()))
40			};
41
42			Agents::<T>::iter_keys().take(MaxAgents::get() as usize).for_each(|agent| {
43				weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 0));
44				let old_proxy = old_proxy_delegator(agent.clone());
45
46				// if delegation does not exist, it does not need to be migrated.
47				if let Some(delegation) = Delegation::<T>::get(&old_proxy) {
48					weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 0));
49
50					let new_proxy =
51						Pallet::<T>::generate_proxy_delegator(Agent::from(agent.clone()));
52
53					// accrue read writes for `do_migrate_delegation`
54					weight.saturating_accrue(T::DbWeight::get().reads_writes(8, 8));
55					let _ = Pallet::<T>::do_migrate_delegation(
56						Delegator::from(old_proxy.clone()),
57						new_proxy.clone(),
58						delegation.amount,
59					)
60					.map_err(|e| {
61						log!(
62							error,
63							"Failed to migrate old proxy delegator {:?} to new proxy {:?} for agent {:?} with error: {:?}",
64								old_proxy,
65								new_proxy,
66								agent,
67								e,
68						);
69					});
70				};
71			});
72
73			log!(info, "Finished migrating old proxy delegator accounts to new ones");
74			weight
75		}
76
77		#[cfg(feature = "try-runtime")]
78		fn post_upgrade(_data: Vec<u8>) -> Result<(), TryRuntimeError> {
79			let mut unmigrated_count = 0;
80			let old_proxy_delegator = |agent: T::AccountId| {
81				T::PalletId::get()
82					.into_sub_account_truncating((AccountType::ProxyDelegator, agent.clone()))
83			};
84
85			Agents::<T>::iter_keys().take(MaxAgents::get() as usize).for_each(|agent| {
86				let old_proxy: T::AccountId = old_proxy_delegator(agent.clone());
87				let held_balance = Pallet::<T>::held_balance_of(Delegator::from(old_proxy.clone()));
88				let delegation = Delegation::<T>::get(&old_proxy);
89				if delegation.is_some() || !held_balance.is_zero() {
90					log!(
91						error,
92						"Old proxy delegator {:?} for agent {:?} is not migrated.",
93						old_proxy,
94						agent,
95					);
96					unmigrated_count += 1;
97				}
98			});
99
100			if unmigrated_count > 0 {
101				Err(TryRuntimeError::Other("Some old proxy delegator accounts are not migrated."))
102			} else {
103				Ok(())
104			}
105		}
106	}
107}