diff --git a/pallets/subtensor/src/macros/dispatches.rs b/pallets/subtensor/src/macros/dispatches.rs index aea2eac3f7..35439479ab 100644 --- a/pallets/subtensor/src/macros/dispatches.rs +++ b/pallets/subtensor/src/macros/dispatches.rs @@ -693,7 +693,7 @@ mod dispatches { /// - Attempting to set prometheus information withing the rate limit min. /// #[pallet::call_index(4)] - #[pallet::weight((Weight::from_parts(43_680_000, 0) + #[pallet::weight((Weight::from_parts(33_010_000, 0) .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))] pub fn serve_axon( @@ -2201,7 +2201,7 @@ mod dispatches { /// * commit_reveal_version (`u16`): /// - The client (bittensor-drand) version #[pallet::call_index(113)] - #[pallet::weight((Weight::from_parts(81_920_000, 0) + #[pallet::weight((Weight::from_parts(64_530_000, 0) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))] pub fn commit_timelocked_weights( diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index 99d103e829..bec3155f2a 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -131,7 +131,9 @@ mod hooks { // Migrate CRV3 add commit_block .saturating_add(migrations::migrate_crv3_commits_add_block::migrate_crv3_commits_add_block::()) //Migrate CRV3 to TimelockedCommits - .saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::()); + .saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::()) + // Migrate to fix root counters + .saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::()); weight } diff --git a/pallets/subtensor/src/migrations/migrate_fix_root_tao_and_alpha_in.rs b/pallets/subtensor/src/migrations/migrate_fix_root_tao_and_alpha_in.rs new file mode 100644 index 0000000000..f20d71c302 --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_fix_root_tao_and_alpha_in.rs @@ -0,0 +1,57 @@ +use super::migrate_init_total_issuance::migrate_init_total_issuance; +use super::*; +use alloc::string::String; + +pub fn migrate_fix_root_tao_and_alpha_in() -> Weight { + let migration_name = b"migrate_fix_root_tao_and_alpha_in".to_vec(); + let mut 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) + ); + + // Update counters (unstaked more than stake) + let total_staked = 2_109_761_275_100_688_u64; + let total_unstaked = 2_179_659_173_851_658_u64; + let reserve_diff = total_unstaked.saturating_sub(total_staked); + let volume_diff = (total_unstaked as u128).saturating_add(total_staked as u128); + SubnetTAO::::mutate(NetUid::ROOT, |amount| { + *amount = amount.saturating_sub(TaoCurrency::from(reserve_diff)); + }); + SubnetAlphaIn::::mutate(NetUid::ROOT, |amount| { + *amount = amount.saturating_add(AlphaCurrency::from(reserve_diff)); + }); + SubnetAlphaOut::::mutate(NetUid::ROOT, |amount| { + *amount = amount.saturating_sub(AlphaCurrency::from(reserve_diff)); + }); + SubnetVolume::::mutate(NetUid::ROOT, |amount| { + *amount = amount.saturating_add(volume_diff); + }); + TotalStake::::mutate(|amount| { + *amount = amount.saturating_sub(TaoCurrency::from(reserve_diff)); + }); + + weight = weight.saturating_add(T::DbWeight::get().writes(5)); + + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + target: "runtime", + "Migration '{}' completed successfully.", + String::from_utf8_lossy(&migration_name) + ); + + // We need to run the total issuance migration to update the total issuance + // when the root subnet TAO has been updated. + migrate_init_total_issuance::().saturating_add(weight) +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index 76834110e2..c2109eb9e6 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -15,6 +15,7 @@ pub mod migrate_delete_subnet_3; pub mod migrate_disable_commit_reveal; pub mod migrate_fix_is_network_member; pub mod migrate_fix_root_subnet_tao; +pub mod migrate_fix_root_tao_and_alpha_in; pub mod migrate_identities_v2; pub mod migrate_init_total_issuance; pub mod migrate_orphaned_storage_items; diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index f1b220dbd4..fbe8825a5f 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -64,7 +64,7 @@ fn test_migration_transfer_nets_to_foundation() { add_network(11.into(), 1, 0); log::info!("{:?}", SubtensorModule::get_subnet_owner(1.into())); - //assert_eq!(SubtensorModule::::get_subnet_owner(1), ); + //assert_eq!(SubtensorModule::::get_subnet_owner(1), ); // Run the migration to transfer ownership let hex = @@ -862,6 +862,45 @@ fn test_migrate_fix_root_subnet_tao() { }); } +// cargo test --package pallet-subtensor --lib -- tests::migration::test_migrate_fix_root_tao_and_alpha_in --exact --show-output +#[test] +fn test_migrate_fix_root_tao_and_alpha_in() { + new_test_ext(1).execute_with(|| { + const MIGRATION_NAME: &str = "migrate_fix_root_tao_and_alpha_in"; + + // Set counters initially + let initial_value = 1_000_000_000_000; + SubnetTAO::::insert(NetUid::ROOT, TaoCurrency::from(initial_value)); + SubnetAlphaIn::::insert(NetUid::ROOT, AlphaCurrency::from(initial_value)); + SubnetAlphaOut::::insert(NetUid::ROOT, AlphaCurrency::from(initial_value)); + SubnetVolume::::insert(NetUid::ROOT, initial_value as u128); + TotalStake::::set(TaoCurrency::from(initial_value)); + + assert!( + !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), + "Migration should not have run yet" + ); + + // Run the migration + let weight = + crate::migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::(); + + // Verify the migration ran correctly + assert!( + HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), + "Migration should be marked as run" + ); + assert!(!weight.is_zero(), "Migration weight should be non-zero"); + + // Verify counters have changed + assert!(SubnetTAO::::get(NetUid::ROOT) != initial_value.into()); + assert!(SubnetAlphaIn::::get(NetUid::ROOT) != initial_value.into()); + assert!(SubnetAlphaOut::::get(NetUid::ROOT) != initial_value.into()); + assert!(SubnetVolume::::get(NetUid::ROOT) != initial_value as u128); + assert!(TotalStake::::get() != initial_value.into()); + }); +} + #[test] fn test_migrate_subnet_symbols() { new_test_ext(1).execute_with(|| {