From 1428d2400d67f19f2b7f359a380d79afa0dccd69 Mon Sep 17 00:00:00 2001 From: John Reed <87283488+JohnReedV@users.noreply.github.com> Date: Mon, 24 Mar 2025 10:55:22 -0700 Subject: [PATCH 1/5] remove `TotalHotkeyAlpha` balances that drop to 0 --- pallets/subtensor/src/staking/recycle_alpha.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/staking/recycle_alpha.rs b/pallets/subtensor/src/staking/recycle_alpha.rs index 109d26c850..cb5e740e84 100644 --- a/pallets/subtensor/src/staking/recycle_alpha.rs +++ b/pallets/subtensor/src/staking/recycle_alpha.rs @@ -42,7 +42,14 @@ impl Pallet { Error::::InsufficientLiquidity ); - TotalHotkeyAlpha::::mutate(&hotkey, netuid, |v| *v = v.saturating_sub(amount)); + if TotalHotkeyAlpha::::mutate(&hotkey, netuid, |v| { + *v = v.saturating_sub(amount); + *v + }) == 0 + { + TotalHotkeyAlpha::::remove(&hotkey, netuid); + } + SubnetAlphaOut::::mutate(netuid, |total| { *total = total.saturating_sub(amount); }); @@ -92,7 +99,13 @@ impl Pallet { Error::::InsufficientLiquidity ); - TotalHotkeyAlpha::::mutate(&hotkey, netuid, |v| *v = v.saturating_sub(amount)); + if TotalHotkeyAlpha::::mutate(&hotkey, netuid, |v| { + *v = v.saturating_sub(amount); + *v + }) == 0 + { + TotalHotkeyAlpha::::remove(&hotkey, netuid); + } // Deposit event Self::deposit_event(Event::AlphaBurned(coldkey, hotkey, amount, netuid)); From c862c4685a097bfaf1a5bd12e3fdac2eab661508 Mon Sep 17 00:00:00 2001 From: John Reed <87283488+JohnReedV@users.noreply.github.com> Date: Mon, 24 Mar 2025 11:05:06 -0700 Subject: [PATCH 2/5] add migration to remove existing zero value items --- pallets/subtensor/src/macros/hooks.rs | 4 +- .../migrate_remove_zero_total_hotkey_alpha.rs | 61 +++++++++++++++++++ pallets/subtensor/src/migrations/mod.rs | 1 + 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index 3203ae312c..834e6c86bb 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -83,7 +83,9 @@ mod hooks { // Remove unused maps entries .saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::()) // Set last emission block number for all existed subnets before start call feature applied - .saturating_add(migrations::migrate_set_first_emission_block_number::migrate_set_first_emission_block_number::()); + .saturating_add(migrations::migrate_set_first_emission_block_number::migrate_set_first_emission_block_number::()) + // Remove all zero value entries in TotalHotkeyAlpha + .saturating_add(migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::()); weight } diff --git a/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs b/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs new file mode 100644 index 0000000000..c6ca619b48 --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs @@ -0,0 +1,61 @@ +use super::*; +use frame_support::{traits::Get, weights::Weight}; +use scale_info::prelude::string::String; +use log; + + +pub fn migrate_remove_zero_total_hotkey_alpha() -> Weight { + let migration_name = b"migrate_remove_zero_total_hotkey_alpha".to_vec(); + let mut weight = T::DbWeight::get().reads(1); + + // ------------------------------ + // Step 0: Check if already run + // ------------------------------ + if HasMigrationRun::::get(&migration_name) { + log::info!( + "Migration '{:?}' has already run. Skipping.", + migration_name + ); + return weight; + } + + log::info!( + "Running migration '{}'", + String::from_utf8_lossy(&migration_name) + ); + + // ------------------------------ + // Step 1: Remove any zero entries in TotalHotkeyAlpha + // ------------------------------ + + let mut removed_entries_count = 0u64; + + // For each (hotkey, netuid, alpha) entry, remove if alpha == 0 + for (hotkey, netuid, alpha) in TotalHotkeyAlpha::::iter() { + if alpha == 0 { + TotalHotkeyAlpha::::remove(&hotkey, netuid); + removed_entries_count += 1; + } + } + + weight = weight.saturating_add(T::DbWeight::get().reads(removed_entries_count)); + weight = weight.saturating_add(T::DbWeight::get().writes(removed_entries_count)); + + log::info!( + "Removed {} zero entries from TotalHotkeyAlpha.", + removed_entries_count + ); + + // ------------------------------ + // Step 2: Mark Migration as Completed + // ------------------------------ + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + "Migration '{:?}' completed successfully.", + String::from_utf8_lossy(&migration_name) + ); + + weight +} \ No newline at end of file diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index 19e057e3ec..c9108b4a5b 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -20,3 +20,4 @@ pub mod migrate_to_v1_separate_emission; pub mod migrate_to_v2_fixed_total_stake; pub mod migrate_total_issuance; pub mod migrate_transfer_ownership_to_foundation; +pub mod migrate_remove_zero_total_hotkey_alpha; From b663a4fdddc1fc533eabf585ced948f346ef0837 Mon Sep 17 00:00:00 2001 From: John Reed <87283488+JohnReedV@users.noreply.github.com> Date: Mon, 24 Mar 2025 11:24:56 -0700 Subject: [PATCH 3/5] add migration test --- pallets/subtensor/src/tests/migration.rs | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index 2fac8c5db5..0ecbfb927d 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -438,3 +438,45 @@ fn test_migrate_set_first_emission_block_number() { } }); } + +#[test] +fn test_migrate_remove_zero_total_hotkey_alpha() { + new_test_ext(1).execute_with(|| { + const MIGRATION_NAME: &str = "migrate_remove_zero_total_hotkey_alpha"; + let netuid = 1u16; + + let hotkey_zero = U256::from(100u64); + let hotkey_nonzero = U256::from(101u64); + + // Insert one zero-alpha entry and one non-zero entry + TotalHotkeyAlpha::::insert(hotkey_zero, netuid, 0u64); + TotalHotkeyAlpha::::insert(hotkey_nonzero, netuid, 123u64); + + assert_eq!(TotalHotkeyAlpha::::get(hotkey_zero, netuid), 0u64); + assert_eq!(TotalHotkeyAlpha::::get(hotkey_nonzero, netuid), 123u64); + + assert!( + !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), + "Migration should not have run yet." + ); + + let weight = crate::migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::(); + + assert!( + HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), + "Migration should be marked as run." + ); + + assert!( + !TotalHotkeyAlpha::::contains_key(hotkey_zero, netuid), + "Zero-alpha entry should have been removed." + ); + + assert_eq!(TotalHotkeyAlpha::::get(hotkey_nonzero, netuid), 123u64); + + assert!( + !weight.is_zero(), + "Migration weight should be non-zero." + ); + }); +} \ No newline at end of file From 280079d92ad0f5599eef07cf602c315bcd26d52f Mon Sep 17 00:00:00 2001 From: John Reed <87283488+JohnReedV@users.noreply.github.com> Date: Mon, 24 Mar 2025 11:25:53 -0700 Subject: [PATCH 4/5] fmt --- .../src/migrations/migrate_remove_zero_total_hotkey_alpha.rs | 5 ++--- pallets/subtensor/src/migrations/mod.rs | 2 +- pallets/subtensor/src/tests/migration.rs | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs b/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs index c6ca619b48..5e88c0efcd 100644 --- a/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs +++ b/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs @@ -1,8 +1,7 @@ use super::*; use frame_support::{traits::Get, weights::Weight}; -use scale_info::prelude::string::String; use log; - +use scale_info::prelude::string::String; pub fn migrate_remove_zero_total_hotkey_alpha() -> Weight { let migration_name = b"migrate_remove_zero_total_hotkey_alpha".to_vec(); @@ -58,4 +57,4 @@ pub fn migrate_remove_zero_total_hotkey_alpha() -> Weight { ); weight -} \ No newline at end of file +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index c9108b4a5b..b342d54979 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -11,6 +11,7 @@ pub mod migrate_populate_owned_hotkeys; pub mod migrate_rao; pub mod migrate_remove_stake_map; pub mod migrate_remove_unused_maps_and_values; +pub mod migrate_remove_zero_total_hotkey_alpha; pub mod migrate_set_first_emission_block_number; pub mod migrate_set_min_burn; pub mod migrate_set_min_difficulty; @@ -20,4 +21,3 @@ pub mod migrate_to_v1_separate_emission; pub mod migrate_to_v2_fixed_total_stake; pub mod migrate_total_issuance; pub mod migrate_transfer_ownership_to_foundation; -pub mod migrate_remove_zero_total_hotkey_alpha; diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index 0ecbfb927d..0628127413 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -461,7 +461,7 @@ fn test_migrate_remove_zero_total_hotkey_alpha() { ); let weight = crate::migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::(); - + assert!( HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), "Migration should be marked as run." @@ -479,4 +479,4 @@ fn test_migrate_remove_zero_total_hotkey_alpha() { "Migration weight should be non-zero." ); }); -} \ No newline at end of file +} From 554a7e8614c041fa9caf9693f8e35726c3d9550f Mon Sep 17 00:00:00 2001 From: John Reed <87283488+JohnReedV@users.noreply.github.com> Date: Mon, 24 Mar 2025 11:30:07 -0700 Subject: [PATCH 5/5] clippy --- .../src/migrations/migrate_remove_zero_total_hotkey_alpha.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs b/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs index 5e88c0efcd..3b45615bf4 100644 --- a/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs +++ b/pallets/subtensor/src/migrations/migrate_remove_zero_total_hotkey_alpha.rs @@ -33,7 +33,7 @@ pub fn migrate_remove_zero_total_hotkey_alpha() -> Weight { for (hotkey, netuid, alpha) in TotalHotkeyAlpha::::iter() { if alpha == 0 { TotalHotkeyAlpha::::remove(&hotkey, netuid); - removed_entries_count += 1; + removed_entries_count = removed_entries_count.saturating_add(1); } }