Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to fix IsNetworkMember #1155

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ impl<T: Config> Pallet<T> {

// --- 7. Remove incentive mechanism memory.
let _ = Uids::<T>::clear_prefix(netuid, u32::MAX, None);
let keys = Keys::<T>::iter_prefix(netuid).collect::<Vec<_>>();
let _ = Keys::<T>::clear_prefix(netuid, u32::MAX, None);
let _ = Bonds::<T>::clear_prefix(netuid, u32::MAX, None);

Expand Down Expand Up @@ -564,6 +565,10 @@ impl<T: Config> Pallet<T> {
ValidatorPermit::<T>::remove(netuid);
ValidatorTrust::<T>::remove(netuid);

for (_uid, key) in keys {
IsNetworkMember::<T>::remove(key, netuid);
}

// --- 11. Erase network parameters.
Tempo::<T>::remove(netuid);
Kappa::<T>::remove(netuid);
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ mod hooks {
// Migrate Commit-Reval 2.0
.saturating_add(migrations::migrate_commit_reveal_v2::migrate_commit_reveal_2::<T>())
// Migrate to RAO
.saturating_add(migrations::migrate_rao::migrate_rao::<T>());
.saturating_add(migrations::migrate_rao::migrate_rao::<T>())
// Fix the IsNetworkMember map to be consistent with other storage maps
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>());
weight
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;
use alloc::string::String;
use frame_support::{traits::Get, weights::Weight};
use log;

pub fn migrate_fix_is_network_member<T: Config>() -> 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::<T>::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::<T>(weight);

// Mark the migration as completed
HasMigrationRun::<T>::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<T: Config>(weight: Weight) -> Weight {
let mut weight = weight;
// Clear the IsNetworkMember storage
let mut curr = IsNetworkMember::<T>::clear(u32::MAX, None);
weight = weight
.saturating_add(T::DbWeight::get().reads_writes(curr.loops as u64, curr.unique as u64));
while curr.maybe_cursor.is_some() {
// Clear until empty
curr = IsNetworkMember::<T>::clear(u32::MAX, curr.maybe_cursor.as_deref());
weight = weight
.saturating_add(T::DbWeight::get().reads_writes(curr.loops as u64, curr.unique as u64));
}
// Repopulate the IsNetworkMember storage using the Keys map
for (netuid, _uid, key) in Keys::<T>::iter() {
IsNetworkMember::<T>::insert(key, netuid, true);
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
}

weight
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading