diff --git a/pallets/subtensor/src/coinbase/root.rs b/pallets/subtensor/src/coinbase/root.rs index fee6e672b..c4fc1a9f9 100644 --- a/pallets/subtensor/src/coinbase/root.rs +++ b/pallets/subtensor/src/coinbase/root.rs @@ -527,6 +527,7 @@ impl Pallet { // --- 7. Remove incentive mechanism memory. let _ = Uids::::clear_prefix(netuid, u32::MAX, None); + let keys = Keys::::iter_prefix(netuid).collect::>(); let _ = Keys::::clear_prefix(netuid, u32::MAX, None); let _ = Bonds::::clear_prefix(netuid, u32::MAX, None); @@ -564,6 +565,10 @@ impl Pallet { ValidatorPermit::::remove(netuid); ValidatorTrust::::remove(netuid); + for key in keys { + IsNetworkMember::::remove(key, netuid); + } + // --- 11. Erase network parameters. Tempo::::remove(netuid); Kappa::::remove(netuid); diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index bf255c1c7..2a43238ab 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -74,7 +74,9 @@ mod hooks { // Migrate Commit-Reval 2.0 .saturating_add(migrations::migrate_commit_reveal_v2::migrate_commit_reveal_2::()) // Migrate to RAO - .saturating_add(migrations::migrate_rao::migrate_rao::()); + .saturating_add(migrations::migrate_rao::migrate_rao::()) + // Fix the IsNetworkMember map to be consistent with other storage maps + .saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::()); weight } diff --git a/pallets/subtensor/src/migrations/migrate_fix_is_network_member.rs b/pallets/subtensor/src/migrations/migrate_fix_is_network_member.rs new file mode 100644 index 000000000..0225cd8fb --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_fix_is_network_member.rs @@ -0,0 +1,58 @@ +use super::*; +use alloc::string::String; +use frame_support::{traits::Get, weights::Weight}; +use log; + +pub fn migrate_fix_is_network_member() -> Weight { + let migration_name = b"migrate_fix_is_network_member".to_vec(); + + // Initialize the weight with one read operation. + let mut weight = T::DbWeight::get().reads(1); + + // Check if the migration has 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) + ); + + weight = do_fix_is_network_member::(weight); + + // Mark the migration as completed + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + "Migration '{:?}' completed. Storage version set to 7.", + String::from_utf8_lossy(&migration_name) + ); + + // Return the migration weight. + weight +} + +fn do_fix_is_network_member(weight: Weight) -> Weight { + // Clear the IsNetworkMember storage + let mut curr = IsNetworkMember::::clear(U32::MAX, None); + weight = weight.saturating_add(T::DbWeight::get().reads_writes(curr.loops, curr.unique)); + while curr.maybe_cursor.is_some() { + // Clear until empty + curr = IsNetworkMember::::clear(U32::MAX, curr.maybe_cursor); + weight = weight.saturating_add(T::DbWeight::get().reads_writes(curr.loops, curr.unique)); + } + // Repopulate the IsNetworkMember storage using the Keys map + let netuids = Subtensor::::get_all_subnet_netuids(); + for netuid in netuids { + for key in Keys::::iter_prefix(netuid) { + IsNetworkMember::::insert(key, netuid, true); + } + } + + weight +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index dc9011f3e..6cf358c4d 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -4,6 +4,7 @@ pub mod migrate_commit_reveal_v2; pub mod migrate_create_root_network; pub mod migrate_delete_subnet_21; pub mod migrate_delete_subnet_3; +pub mod migrate_fix_is_network_member; pub mod migrate_fix_total_coldkey_stake; pub mod migrate_init_total_issuance; pub mod migrate_populate_owned_hotkeys;