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

Reapply hotkey swap fix for childkey's parents #890

Merged
merged 1 commit into from
Oct 30, 2024
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
15 changes: 14 additions & 1 deletion pallets/subtensor/src/swap/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,20 @@ impl<T: Config> Pallet<T> {
// Remove the old hotkey's child entries
ChildKeys::<T>::remove(old_hotkey, netuid);
// Insert the same child entries for the new hotkey
ChildKeys::<T>::insert(new_hotkey, netuid, my_children);
ChildKeys::<T>::insert(new_hotkey, netuid, my_children.clone());
for (_, child_key_i) in my_children {
// For each child, update their parent list
let mut child_parents: Vec<(u64, T::AccountId)> =
ParentKeys::<T>::get(child_key_i.clone(), netuid);
for parent in child_parents.iter_mut() {
// If the parent is the old hotkey, replace it with the new hotkey
if parent.1 == *old_hotkey {
parent.1 = new_hotkey.clone();
}
}
// Update the child's parent list
ParentKeys::<T>::insert(child_key_i, netuid, child_parents);
}
}

// 13. Swap ParentKeys.
Expand Down
97 changes: 97 additions & 0 deletions pallets/subtensor/tests/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,3 +1148,100 @@ fn test_swap_complex_parent_child_structure() {
);
});
}

#[test]
fn test_swap_parent_hotkey_childkey_maps() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let parent_old = U256::from(1);
let coldkey = U256::from(2);
let child = U256::from(3);
let parent_new = U256::from(4);
add_network(netuid, 0, 0);
SubtensorModule::create_account_if_non_existent(&coldkey, &parent_old);

// Set child and verify state maps
assert_ok!(SubtensorModule::do_set_children(
RuntimeOrigin::signed(coldkey),
parent_old,
netuid,
vec![(u64::MAX, child)]
));
assert_eq!(
ParentKeys::<Test>::get(child, netuid),
vec![(u64::MAX, parent_old)]
);
assert_eq!(
ChildKeys::<Test>::get(parent_old, netuid),
vec![(u64::MAX, child)]
);

// Swap
let mut weight = Weight::zero();
assert_ok!(SubtensorModule::perform_hotkey_swap(
&parent_old,
&parent_new,
&coldkey,
&mut weight
));

// Verify parent and child keys updates
assert_eq!(
ParentKeys::<Test>::get(child, netuid),
vec![(u64::MAX, parent_new)]
);
assert_eq!(
ChildKeys::<Test>::get(parent_new, netuid),
vec![(u64::MAX, child)]
);
})
}

#[test]
fn test_swap_child_hotkey_childkey_maps() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let parent = U256::from(1);
let coldkey = U256::from(2);
let child_old = U256::from(3);
let child_new = U256::from(4);
add_network(netuid, 0, 0);
SubtensorModule::create_account_if_non_existent(&coldkey, &child_old);
SubtensorModule::create_account_if_non_existent(&coldkey, &parent);

// Set child and verify state maps
assert_ok!(SubtensorModule::do_set_children(
RuntimeOrigin::signed(coldkey),
parent,
netuid,
vec![(u64::MAX, child_old)]
));
assert_eq!(
ParentKeys::<Test>::get(child_old, netuid),
vec![(u64::MAX, parent)]
);
assert_eq!(
ChildKeys::<Test>::get(parent, netuid),
vec![(u64::MAX, child_old)]
);

// Swap
let mut weight = Weight::zero();
assert_ok!(SubtensorModule::perform_hotkey_swap(
&child_old,
&child_new,
&coldkey,
&mut weight
));

// Verify parent and child keys updates
assert_eq!(
ParentKeys::<Test>::get(child_new, netuid),
vec![(u64::MAX, parent)]
);
assert_eq!(
ChildKeys::<Test>::get(parent, netuid),
vec![(u64::MAX, child_new)]
);
})
}
Loading