|
15 | 15 | // along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
|
16 | 16 |
|
17 | 17 | //! # Migrations
|
| 18 | +//! |
| 19 | +#[allow(unused_imports)] |
| 20 | +use crate::*; |
| 21 | + |
| 22 | +// Substrate |
| 23 | +use frame_support::traits::{ |
| 24 | + fungible::{InspectHold, MutateHold}, |
| 25 | + Currency, Get, LockIdentifier, LockableCurrency, ReservableCurrency, |
| 26 | +}; |
| 27 | +#[allow(unused_imports)] |
| 28 | +use frame_support::{dispatch::DispatchError, log, migration, storage::unhashed}; |
| 29 | +use parity_scale_codec::Encode; |
| 30 | +use sp_core::hexdisplay::HexDisplay; |
| 31 | +#[allow(unused_imports)] |
| 32 | +use sp_std::vec::Vec; |
| 33 | + |
| 34 | +// Lock Identifiers used in the old version of the pallet. |
| 35 | +const COLLATOR_LOCK_ID: LockIdentifier = *b"stkngcol"; |
| 36 | +const DELEGATOR_LOCK_ID: LockIdentifier = *b"stkngdel"; |
| 37 | + |
| 38 | +pub struct CustomOnRuntimeUpgrade<T, OldCurrency> |
| 39 | +where |
| 40 | + T: Config, |
| 41 | + OldCurrency: 'static |
| 42 | + + LockableCurrency<<T as frame_system::Config>::AccountId> |
| 43 | + + Currency<<T as frame_system::Config>::AccountId> |
| 44 | + + ReservableCurrency<<T as frame_system::Config>::AccountId>, |
| 45 | +{ |
| 46 | + _phantom: sp_std::marker::PhantomData<(T, OldCurrency)>, |
| 47 | +} |
| 48 | + |
| 49 | +impl<T, OldCurrency> frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade<T, OldCurrency> |
| 50 | +where |
| 51 | + T: Config, |
| 52 | + OldCurrency: 'static |
| 53 | + + LockableCurrency<<T as frame_system::Config>::AccountId> |
| 54 | + + Currency<<T as frame_system::Config>::AccountId> |
| 55 | + + ReservableCurrency<<T as frame_system::Config>::AccountId>, |
| 56 | + BalanceOf<T>: From<OldCurrency::Balance>, |
| 57 | +{ |
| 58 | + #[cfg(feature = "try-runtime")] |
| 59 | + fn pre_upgrade() -> Result<Vec<u8>, DispatchError> { |
| 60 | + let active_collators = CandidatePool::<T>::get().0; |
| 61 | + |
| 62 | + for bond_info in active_collators { |
| 63 | + let owner = bond_info.owner; |
| 64 | + let balance = OldCurrency::free_balance(&owner); |
| 65 | + log::info!( |
| 66 | + "Collator: {:?} OldCurrency::free_balance pre_upgrade {:?}", |
| 67 | + HexDisplay::from(&owner.encode()), |
| 68 | + balance |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + Ok(Vec::new()) |
| 73 | + } |
| 74 | + |
| 75 | + #[cfg(feature = "try-runtime")] |
| 76 | + fn post_upgrade(_state: Vec<u8>) -> Result<(), DispatchError> { |
| 77 | + let active_collators = CandidatePool::<T>::get().0; |
| 78 | + |
| 79 | + for bond_info in active_collators { |
| 80 | + let owner = bond_info.owner; |
| 81 | + let balance = OldCurrency::free_balance(&owner); |
| 82 | + log::info!( |
| 83 | + "Collator: {:?} OldCurrency::free_balance post_upgrade {:?}", |
| 84 | + HexDisplay::from(&owner.encode()), |
| 85 | + balance |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + |
| 92 | + fn on_runtime_upgrade() -> frame_support::weights::Weight { |
| 93 | + log::info!("Parachain Staking: on_runtime_upgrade"); |
| 94 | + let mut read_ops = 0u64; |
| 95 | + let mut write_ops = 0u64; |
| 96 | + |
| 97 | + // Get all the active collators |
| 98 | + let active_collators = CandidatePool::<T>::get().0; |
| 99 | + read_ops += 1; |
| 100 | + |
| 101 | + for bond_info in active_collators { |
| 102 | + let owner = bond_info.owner; |
| 103 | + log::info!("Parachain Staking: migrating collator {:?}", HexDisplay::from(&owner.encode())); |
| 104 | + |
| 105 | + let candidate_info = CandidateInfo::<T>::get(&owner).unwrap(); |
| 106 | + let bond_amount = candidate_info.bond; |
| 107 | + read_ops += 1; |
| 108 | + log::info!("Parachain Staking: bond_amount {:?}", bond_amount); |
| 109 | + |
| 110 | + let already_held: <T as Config>::Balance = |
| 111 | + T::Currency::balance_on_hold(&HoldReason::StakingCollator.into(), &owner); |
| 112 | + read_ops += 1; |
| 113 | + |
| 114 | + // Check if the lock is already held, to make migration idempotent |
| 115 | + if already_held == bond_amount { |
| 116 | + log::info!("Parachain Staking: already held {:?}", already_held); |
| 117 | + } else { |
| 118 | + // Remove the lock from the old currency |
| 119 | + OldCurrency::remove_lock(COLLATOR_LOCK_ID, &owner); |
| 120 | + write_ops += 1; |
| 121 | + |
| 122 | + // Hold the new currency |
| 123 | + T::Currency::hold(&HoldReason::StakingCollator.into(), &owner, bond_amount).unwrap_or_else(|err| { |
| 124 | + log::error!("Failed to add lock to parachain staking currency: {:?}", err); |
| 125 | + }); |
| 126 | + write_ops += 1; |
| 127 | + |
| 128 | + // Get all the delegations for the collator |
| 129 | + if let Some(delegations) = TopDelegations::<T>::get(&owner) { |
| 130 | + read_ops += 1; |
| 131 | + for delegation in delegations.delegations { |
| 132 | + // Process each delegation |
| 133 | + log::info!( |
| 134 | + "Delegator: {:?}, Amount: {:?}", |
| 135 | + HexDisplay::from(&delegation.owner.encode()), |
| 136 | + delegation.amount |
| 137 | + ); |
| 138 | + |
| 139 | + // Remove the lock from the old currency |
| 140 | + OldCurrency::remove_lock(DELEGATOR_LOCK_ID, &delegation.owner); |
| 141 | + write_ops += 1; |
| 142 | + |
| 143 | + // Hold the new currency |
| 144 | + T::Currency::hold(&HoldReason::StakingDelegator.into(), &delegation.owner, delegation.amount) |
| 145 | + .unwrap_or_else(|err| { |
| 146 | + log::error!("Failed to add lock to parachain staking currency: {:?}", err); |
| 147 | + }); |
| 148 | + write_ops += 1; |
| 149 | + } |
| 150 | + } else { |
| 151 | + // Handle the case where there are no delegations for the account |
| 152 | + log::info!("No delegations found for the given account."); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + log::info!("Parachain Staking: read_ops {:?} | write_ops: {:?}", read_ops, write_ops); |
| 158 | + |
| 159 | + <T as frame_system::Config>::DbWeight::get().reads_writes(read_ops, write_ops) |
| 160 | + } |
| 161 | +} |
0 commit comments