|
| 1 | +use super::*; |
| 2 | +use frame_support::{storage_alias, traits::Get, weights::Weight}; |
| 3 | +use substrate_fixed::types::U96F32; |
| 4 | + |
| 5 | +pub mod deprecated_pending_emission_format { |
| 6 | + use super::*; |
| 7 | + |
| 8 | + #[storage_alias] |
| 9 | + pub(super) type PendingEmission<T: Config> = |
| 10 | + StorageMap<Pallet<T>, Identity, NetUid, AlphaCurrency, ValueQuery>; |
| 11 | +} |
| 12 | + |
| 13 | +pub fn migrate_pending_emissions<T: Config>() -> Weight { |
| 14 | + let migration_name = b"migrate_pending_emissions".to_vec(); |
| 15 | + let mut weight: Weight = T::DbWeight::get().reads(1); |
| 16 | + |
| 17 | + // Skip if already executed |
| 18 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 19 | + log::info!( |
| 20 | + target: "runtime", |
| 21 | + "Migration '{}' already run - skipping.", |
| 22 | + String::from_utf8_lossy(&migration_name) |
| 23 | + ); |
| 24 | + return weight; |
| 25 | + } |
| 26 | + log::info!( |
| 27 | + "Running migration '{}'", |
| 28 | + String::from_utf8_lossy(&migration_name) |
| 29 | + ); |
| 30 | + |
| 31 | + // Pull from PendingEmission and distribute to PendingValidatorEmission and PendingServerEmission |
| 32 | + for (netuid, pending_emission) in |
| 33 | + deprecated_pending_emission_format::PendingEmission::<T>::iter() |
| 34 | + { |
| 35 | + // Split up the pending emission into server and validator emission |
| 36 | + // Server emission is pending+root_alpha times the 50% miner cut. |
| 37 | + let root_alpha: U96F32 = |
| 38 | + U96F32::saturating_from_num(PendingRootAlphaDivs::<T>::get(netuid).to_u64()); |
| 39 | + let server_emission_float: U96F32 = U96F32::saturating_from_num(pending_emission.to_u64()) |
| 40 | + .saturating_add(root_alpha) |
| 41 | + .saturating_div(U96F32::saturating_from_num(2)); |
| 42 | + let server_emission: AlphaCurrency = |
| 43 | + server_emission_float.saturating_to_num::<u64>().into(); |
| 44 | + let validator_emission = pending_emission.saturating_sub(server_emission); |
| 45 | + |
| 46 | + PendingValidatorEmission::<T>::mutate(netuid, |total| { |
| 47 | + *total = total.saturating_add(validator_emission) |
| 48 | + }); |
| 49 | + PendingServerEmission::<T>::mutate(netuid, |total| { |
| 50 | + *total = total.saturating_add(server_emission) |
| 51 | + }); |
| 52 | + |
| 53 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)); |
| 54 | + } |
| 55 | + |
| 56 | + // Kill the map |
| 57 | + let removal_result = |
| 58 | + deprecated_pending_emission_format::PendingEmission::<T>::clear(u32::MAX, None); |
| 59 | + weight = weight.saturating_add( |
| 60 | + T::DbWeight::get().reads_writes(removal_result.loops as u64, removal_result.backend as u64), |
| 61 | + ); |
| 62 | + |
| 63 | + // Mark Migration as Completed |
| 64 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 65 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 66 | + |
| 67 | + log::info!( |
| 68 | + "Migration '{:?}' completed successfully.", |
| 69 | + String::from_utf8_lossy(&migration_name) |
| 70 | + ); |
| 71 | + |
| 72 | + weight |
| 73 | +} |
0 commit comments