Skip to content

Commit 7e13ad5

Browse files
committed
also swap if in another hotkey's pending CHK
1 parent 45480ee commit 7e13ad5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ impl<T: Config> Pallet<T> {
364364
ChildKeys::<T>::remove(old_hotkey, netuid);
365365
// Insert the same child entries for the new hotkey
366366
ChildKeys::<T>::insert(new_hotkey, netuid, my_children.clone());
367+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
367368
for (_, child_key_i) in my_children {
368369
// For each child, update their parent list
369370
let mut child_parents: Vec<(u64, T::AccountId)> =
@@ -376,6 +377,7 @@ impl<T: Config> Pallet<T> {
376377
}
377378
// Update the child's parent list
378379
ParentKeys::<T>::insert(child_key_i, netuid, child_parents);
380+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
379381
}
380382
}
381383

@@ -388,6 +390,7 @@ impl<T: Config> Pallet<T> {
388390
ParentKeys::<T>::remove(old_hotkey, netuid);
389391
// Insert the same parent entries for the new hotkey
390392
ParentKeys::<T>::insert(new_hotkey, netuid, parents.clone());
393+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
391394
for (_, parent_key_i) in parents {
392395
// For each parent, update their children list
393396
let mut parent_children: Vec<(u64, T::AccountId)> =
@@ -400,16 +403,37 @@ impl<T: Config> Pallet<T> {
400403
}
401404
// Update the parent's children list
402405
ChildKeys::<T>::insert(parent_key_i, netuid, parent_children);
406+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
403407
}
404408
}
405409

406410
// 14. Swap PendingChildKeys.
407411
// PendingChildKeys( netuid, parent ) --> Vec<(proportion,child), cool_down_block>
408412
for netuid in Self::get_all_subnet_netuids() {
413+
weight.saturating_accrue(T::DbWeight::get().reads(1));
409414
if PendingChildKeys::<T>::contains_key(netuid, old_hotkey) {
410415
let (children, cool_down_block) = PendingChildKeys::<T>::get(netuid, old_hotkey);
411416
PendingChildKeys::<T>::remove(netuid, old_hotkey);
412417
PendingChildKeys::<T>::insert(netuid, new_hotkey, (children, cool_down_block));
418+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
419+
}
420+
421+
// Also check for others with our hotkey as a child
422+
for (hotkey, (children, cool_down_block)) in PendingChildKeys::<T>::iter_prefix(netuid)
423+
{
424+
weight.saturating_accrue(T::DbWeight::get().reads(1));
425+
426+
if let Some(potential_idx) =
427+
children.iter().position(|(_, child)| *child == *old_hotkey)
428+
{
429+
let mut new_children = children.clone();
430+
let entry_to_remove = new_children.remove(potential_idx);
431+
new_children.push((entry_to_remove.0, new_hotkey.clone())); // Keep the proportion.
432+
433+
PendingChildKeys::<T>::remove(netuid, hotkey.clone());
434+
PendingChildKeys::<T>::insert(netuid, hotkey, (new_children, cool_down_block));
435+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
436+
}
413437
}
414438
}
415439

0 commit comments

Comments
 (0)