Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,36 @@ pub fn migrate_fix_subnet_hotkey_lock_swaps<T: Config>() -> Weight {
};
let netuid = NetUid::from(fix.netuid);

let indexed_coldkeys: u64;
let locks_to_fix: Vec<(T::AccountId, LockState)> = if let Some(coldkey) = fix.coldkey {
let Some(coldkey) = decode_account_id32::<T>(coldkey) else {
log::error!("Failed to decode coldkey: {}", coldkey);
continue;
};
indexed_coldkeys = 1;
LockingColdkeys::<T>::remove((netuid, old_hotkey.clone(), coldkey.clone()));
Lock::<T>::take((coldkey.clone(), netuid, old_hotkey.clone()))
.map(|lock| vec![(coldkey, lock)])
.unwrap_or_default()
} else {
let locks: Vec<(T::AccountId, LockState)> = Lock::<T>::iter()
.filter_map(|((coldkey, lock_netuid, hotkey), lock)| {
(lock_netuid == netuid && hotkey == old_hotkey).then_some((coldkey, lock))
let coldkeys: Vec<T::AccountId> =
LockingColdkeys::<T>::iter_prefix((netuid, old_hotkey.clone()))
.map(|(coldkey, ())| coldkey)
.collect();
indexed_coldkeys = coldkeys.len() as u64;

coldkeys
.into_iter()
.filter_map(|coldkey| {
LockingColdkeys::<T>::remove((netuid, old_hotkey.clone(), coldkey.clone()));
Lock::<T>::take((coldkey.clone(), netuid, old_hotkey.clone()))
.map(|lock| (coldkey, lock))
})
.collect();
for (coldkey, _) in &locks {
Lock::<T>::remove((coldkey.clone(), netuid, old_hotkey.clone()));
}
locks
.collect()
};
let locks_to_fix_count = locks_to_fix.len() as u64;
weight = weight.saturating_add(
T::DbWeight::get()
.reads_writes(locks_to_fix_count.saturating_add(1), locks_to_fix_count),
T::DbWeight::get().reads_writes(locks_to_fix_count.saturating_add(1), indexed_coldkeys),
);
Comment thread
gztensor marked this conversation as resolved.
Outdated

if locks_to_fix.is_empty() {
Expand Down Expand Up @@ -323,7 +330,8 @@ pub fn migrate_fix_subnet_hotkey_lock_swaps<T: Config>() -> Weight {
}

Lock::<T>::insert((coldkey.clone(), netuid, new_hotkey.clone()), lock.clone());
weight = weight.saturating_add(T::DbWeight::get().writes(1));
LockingColdkeys::<T>::insert((netuid, new_hotkey.clone(), coldkey.clone()), ());
weight = weight.saturating_add(T::DbWeight::get().writes(2));

if !new_hotkey_is_owner {
add_to_aggregate::<T>(&coldkey, netuid, &new_hotkey, &lock);
Expand Down
42 changes: 42 additions & 0 deletions pallets/subtensor/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,17 @@ fn test_migrate_fix_subnet_hotkey_lock_swaps_moves_or_discards_conflicts() {
(coldkey_to_move, netuid, old_hotkey),
moved_lock.clone(),
);
LockingColdkeys::<Test>::insert((netuid, old_hotkey, coldkey_to_move), ());
Lock::<Test>::insert(
(coldkey_with_conflict, netuid, old_hotkey),
discarded_lock.clone(),
);
LockingColdkeys::<Test>::insert((netuid, old_hotkey, coldkey_with_conflict), ());
Lock::<Test>::insert(
(coldkey_with_conflict, netuid, new_hotkey),
existing_destination_lock.clone(),
);
LockingColdkeys::<Test>::insert((netuid, new_hotkey, coldkey_with_conflict), ());
DecayingLock::<Test>::insert(coldkey_to_move, netuid, false);
DecayingLock::<Test>::insert(coldkey_with_conflict, netuid, false);
DecayingLock::<Test>::insert(chained_coldkey, chained_netuid, false);
Expand All @@ -148,6 +151,10 @@ fn test_migrate_fix_subnet_hotkey_lock_swaps_moves_or_discards_conflicts() {
(chained_coldkey, chained_netuid, chained_first_hotkey),
chained_lock.clone(),
);
LockingColdkeys::<Test>::insert(
(chained_netuid, chained_first_hotkey, chained_coldkey),
(),
);
HotkeyLock::<Test>::insert(chained_netuid, chained_first_hotkey, chained_lock.clone());

let weight =
Expand All @@ -157,14 +164,34 @@ fn test_migrate_fix_subnet_hotkey_lock_swaps_moves_or_discards_conflicts() {
assert!(HasMigrationRun::<Test>::get(&migration_name));
assert!(Lock::<Test>::get((coldkey_to_move, netuid, old_hotkey)).is_none());
assert!(Lock::<Test>::get((coldkey_with_conflict, netuid, old_hotkey)).is_none());
assert!(!LockingColdkeys::<Test>::contains_key((
netuid,
old_hotkey,
coldkey_to_move
)));
assert!(!LockingColdkeys::<Test>::contains_key((
netuid,
old_hotkey,
coldkey_with_conflict
)));
assert_eq!(
Lock::<Test>::get((coldkey_to_move, netuid, new_hotkey)),
Some(moved_lock.clone())
);
assert!(LockingColdkeys::<Test>::contains_key((
netuid,
new_hotkey,
coldkey_to_move
)));
assert_eq!(
Lock::<Test>::get((coldkey_with_conflict, netuid, new_hotkey)),
Some(existing_destination_lock.clone())
);
assert!(LockingColdkeys::<Test>::contains_key((
netuid,
new_hotkey,
coldkey_with_conflict
)));
assert!(HotkeyLock::<Test>::get(netuid, old_hotkey).is_none());

let new_aggregate = HotkeyLock::<Test>::get(netuid, new_hotkey)
Expand Down Expand Up @@ -193,10 +220,25 @@ fn test_migrate_fix_subnet_hotkey_lock_swaps_moves_or_discards_conflicts() {
chained_middle_hotkey
))
.is_none());
assert!(!LockingColdkeys::<Test>::contains_key((
chained_netuid,
chained_first_hotkey,
chained_coldkey
)));
assert!(!LockingColdkeys::<Test>::contains_key((
chained_netuid,
chained_middle_hotkey,
chained_coldkey
)));
assert_eq!(
Lock::<Test>::get((chained_coldkey, chained_netuid, chained_final_hotkey)),
Some(chained_lock.clone())
);
assert!(LockingColdkeys::<Test>::contains_key((
chained_netuid,
chained_final_hotkey,
chained_coldkey
)));
assert!(HotkeyLock::<Test>::get(chained_netuid, chained_first_hotkey).is_none());
assert!(HotkeyLock::<Test>::get(chained_netuid, chained_middle_hotkey).is_none());
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 417,
spec_version: 418,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading