pallet_delegated_staking/
migration.rs1use 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 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 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 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}