use super::*; use frame_support::pallet_prelude::{Identity, OptionQuery, Weight}; use frame_support::storage_alias; use sp_std::vec::Vec; // TODO: Implement comprehensive tests for this migration /// Module containing deprecated storage format for LoadedEmission pub mod deprecated_loaded_emission_format { use super::*; #[storage_alias] pub(super) type LoadedEmission = StorageMap, Identity, u16, Vec<(AccountIdOf, u64)>, OptionQuery>; } /// This on-going migration is disabled as part of imbalances work. pub(crate) fn migrate_init_total_issuance() -> Weight { // let subnets_len = crate::NetworksAdded::::iter().count() as u64; // // Retrieve the total balance of all accounts // let total_account_balances = <::Currency as fungible::Inspect< // ::AccountId, // >>::total_issuance(); // // Get the total stake from the system // let prev_total_stake = crate::TotalStake::::get(); // // Calculate new total stake using the sum of all subnet TAO // let total_subnet_tao = // crate::SubnetTAO::::iter().fold(TaoBalance::ZERO, |acc, (_, v)| acc.saturating_add(v)); // let total_stake = total_subnet_tao; // // Update the total stake in storage // crate::TotalStake::::put(total_stake); // log::info!( // "Subtensor Pallet Total Stake Updated: previous: {prev_total_stake:?}, new: {total_stake:?}" // ); // // Retrieve the previous total issuance for logging purposes // let prev_total_issuance = crate::TotalIssuance::::get(); // // Calculate the new total issuance // let new_total_issuance: TaoBalance = total_account_balances.saturating_add(total_stake).into(); // // Update the total issuance in storage // crate::TotalIssuance::::put(new_total_issuance); // // Log the change in total issuance // log::info!( // "Subtensor Pallet Total Issuance Updated: previous: {prev_total_issuance:?}, new: {new_total_issuance:?}" // ); // // Return the weight of the operation // // We performed subnets_len + 5 reads and 1 write // ::DbWeight::get().reads_writes(subnets_len.saturating_add(5), 2) log::info!("Subtensor Pallet Total Issuance ongoing update migration is disabled."); T::DbWeight::get().reads(0) } /// This on-going migration is disabled as part of imbalances work. pub(crate) fn migrate_init_total_issuance_once() -> Weight { let migration_name = b"migrate_init_total_issuance_once".to_vec(); let weight = T::DbWeight::get().reads(1); if HasMigrationRun::::get(&migration_name) { log::info!( "Migration '{:?}' has already run. Skipping.", String::from_utf8_lossy(&migration_name) ); return weight; } log::info!( "Running migration '{}'", String::from_utf8_lossy(&migration_name) ); //////////////////////////////////////////////////////// // Actual migration let subnets_len = crate::NetworksAdded::::iter().count() as u64; // Retrieve the total balance of all accounts let total_account_balances = <::Currency as fungible::Inspect< ::AccountId, >>::total_issuance(); // Get the total stake from the system let prev_total_stake = crate::TotalStake::::get(); // Calculate new total stake using the sum of all subnet TAO let total_subnet_tao = crate::SubnetTAO::::iter().fold(TaoBalance::ZERO, |acc, (_, v)| acc.saturating_add(v)); let total_stake = total_subnet_tao; // Update the total stake in storage crate::TotalStake::::put(total_stake); log::info!( "Subtensor Pallet Total Stake Updated: previous: {prev_total_stake:?}, new: {total_stake:?}" ); // Retrieve the previous total issuance for logging purposes let prev_total_issuance = crate::TotalIssuance::::get(); // Calculate the new total issuance let new_total_issuance: TaoBalance = total_account_balances.saturating_add(total_stake).into(); // Update the total issuance in storage crate::TotalIssuance::::put(new_total_issuance); // Log the change in total issuance log::info!( "Subtensor Pallet Total Issuance Updated: previous: {prev_total_issuance:?}, new: {new_total_issuance:?}" ); //////////////////////////////////////////////////////// HasMigrationRun::::insert(&migration_name, true); log::info!( target: "runtime", "Migration '{}' completed successfully.", String::from_utf8_lossy(&migration_name) ); // Return the weight of the operation // We performed subnets_len + 5 reads and 1 write ::DbWeight::get().reads_writes(subnets_len.saturating_add(6), 3) } pub mod initialise_total_issuance { use frame_support::pallet_prelude::Weight; use frame_support::traits::OnRuntimeUpgrade; use crate::*; pub struct Migration(PhantomData); impl OnRuntimeUpgrade for Migration { /// Performs the migration to initialize and update the total issuance. /// /// This function does the following: /// 1. Calculates the total locked tokens across all subnets /// 2. Retrieves the total account balances and total stake /// 3. Computes and updates the new total issuance /// /// Returns the weight of the migration operation. fn on_runtime_upgrade() -> Weight { let mut weight = super::migrate_init_total_issuance::(); weight.saturating_accrue(super::migrate_init_total_issuance_once::()); weight } /// Performs post-upgrade checks to ensure the migration was successful. /// /// This function is only compiled when the "try-runtime" feature is enabled. #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { Ok(()) } } }