use super::*; use frame_support::{ storage_alias, traits::{Get, GetStorageVersion, StorageVersion}, weights::Weight, }; use log::info; use sp_std::vec::Vec; use subtensor_runtime_common::{NetUid, NetUidStorageIndex}; /// Constant for logging purposes const LOG_TARGET: &str = "migrate_delete_subnet_21"; /// Module containing deprecated storage format pub mod deprecated_loaded_emission_format { use super::*; #[storage_alias] pub(super) type LoadedEmission = StorageMap, Identity, u16, Vec<(AccountIdOf, u64)>, OptionQuery>; } /// Migrates the storage to delete subnet 21 /// /// This function performs the following steps: /// 1. Checks if the migration is necessary /// 2. Removes all storage related to subnet 21 /// 3. Updates the storage version /// /// # Arguments /// /// * `T` - The Config trait of the pallet /// /// # Returns /// /// * `Weight` - The computational weight of this operation /// /// # Example /// /// ```ignore /// let weight = migrate_delete_subnet_21::(); /// ``` pub fn migrate_delete_subnet_21() -> Weight { let new_storage_version = 4; // Setup migration weight let mut weight = T::DbWeight::get().reads(1); // Grab current version let onchain_version = Pallet::::on_chain_storage_version(); // Only runs if we haven't already updated version past above new_storage_version and subnet 21 exists. if onchain_version < new_storage_version && Pallet::::if_subnet_exist(NetUid::from(21)) { info!(target: LOG_TARGET, ">>> Removing subnet 21 {onchain_version:?}"); let netuid = NetUid::from(21); // We do this all manually as we don't want to call code related to giving subnet owner back their locked token cost. // Remove network count SubnetworkN::::remove(netuid); // Remove netuid from added networks NetworksAdded::::remove(netuid); // Decrement the network counter TotalNetworks::::mutate(|n| *n = n.saturating_sub(1)); // Remove network registration time NetworkRegisteredAt::::remove(netuid); weight.saturating_accrue(T::DbWeight::get().writes(5)); // Remove incentive mechanism memory let _ = Uids::::clear_prefix(netuid, u32::MAX, None); let _ = Keys::::clear_prefix(netuid, u32::MAX, None); let _ = Bonds::::clear_prefix(NetUidStorageIndex::from(netuid), u32::MAX, None); let _ = Weights::::clear_prefix(NetUidStorageIndex::from(netuid), u32::MAX, None); weight.saturating_accrue(T::DbWeight::get().writes(4)); // Remove various network-related parameters Active::::remove(netuid); Emission::::remove(netuid); Incentive::::remove(NetUidStorageIndex::from(netuid)); Consensus::::remove(netuid); Dividends::::remove(netuid); LastUpdate::::remove(NetUidStorageIndex::from(netuid)); ValidatorPermit::::remove(netuid); ValidatorTrust::::remove(netuid); weight.saturating_accrue(T::DbWeight::get().writes(8)); // Erase network parameters Tempo::::remove(netuid); Kappa::::remove(netuid); Difficulty::::remove(netuid); MaxAllowedUids::::remove(netuid); ImmunityPeriod::::remove(netuid); ActivityCutoff::::remove(netuid); MinAllowedWeights::::remove(netuid); RegistrationsThisInterval::::remove(netuid); POWRegistrationsThisInterval::::remove(netuid); BurnRegistrationsThisInterval::::remove(netuid); weight.saturating_accrue(T::DbWeight::get().writes(10)); // Update storage version StorageVersion::new(new_storage_version).put::>(); weight.saturating_accrue(T::DbWeight::get().writes(1)); weight } else { info!(target: LOG_TARGET, "Migration to v4 already done or subnet 21 doesn't exist!"); Weight::zero() } } // TODO: Add unit tests for this migration // TODO: Consider adding error handling for storage operations // TODO: Verify that all relevant storage items for subnet 21 are removed