Skip to content

Commit d16929a

Browse files
authored
Merge pull request #890 from opentensor/fix/reapply-hotkey-swap-fix-childkeys
Reapply hotkey swap fix for childkey's parents
2 parents 69b7ac8 + dbb3ad3 commit d16929a

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,20 @@ impl<T: Config> Pallet<T> {
337337
// Remove the old hotkey's child entries
338338
ChildKeys::<T>::remove(old_hotkey, netuid);
339339
// Insert the same child entries for the new hotkey
340-
ChildKeys::<T>::insert(new_hotkey, netuid, my_children);
340+
ChildKeys::<T>::insert(new_hotkey, netuid, my_children.clone());
341+
for (_, child_key_i) in my_children {
342+
// For each child, update their parent list
343+
let mut child_parents: Vec<(u64, T::AccountId)> =
344+
ParentKeys::<T>::get(child_key_i.clone(), netuid);
345+
for parent in child_parents.iter_mut() {
346+
// If the parent is the old hotkey, replace it with the new hotkey
347+
if parent.1 == *old_hotkey {
348+
parent.1 = new_hotkey.clone();
349+
}
350+
}
351+
// Update the child's parent list
352+
ParentKeys::<T>::insert(child_key_i, netuid, child_parents);
353+
}
341354
}
342355

343356
// 13. Swap ParentKeys.

pallets/subtensor/tests/swap_hotkey.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,3 +1148,100 @@ fn test_swap_complex_parent_child_structure() {
11481148
);
11491149
});
11501150
}
1151+
1152+
#[test]
1153+
fn test_swap_parent_hotkey_childkey_maps() {
1154+
new_test_ext(1).execute_with(|| {
1155+
let netuid: u16 = 1;
1156+
let parent_old = U256::from(1);
1157+
let coldkey = U256::from(2);
1158+
let child = U256::from(3);
1159+
let parent_new = U256::from(4);
1160+
add_network(netuid, 0, 0);
1161+
SubtensorModule::create_account_if_non_existent(&coldkey, &parent_old);
1162+
1163+
// Set child and verify state maps
1164+
assert_ok!(SubtensorModule::do_set_children(
1165+
RuntimeOrigin::signed(coldkey),
1166+
parent_old,
1167+
netuid,
1168+
vec![(u64::MAX, child)]
1169+
));
1170+
assert_eq!(
1171+
ParentKeys::<Test>::get(child, netuid),
1172+
vec![(u64::MAX, parent_old)]
1173+
);
1174+
assert_eq!(
1175+
ChildKeys::<Test>::get(parent_old, netuid),
1176+
vec![(u64::MAX, child)]
1177+
);
1178+
1179+
// Swap
1180+
let mut weight = Weight::zero();
1181+
assert_ok!(SubtensorModule::perform_hotkey_swap(
1182+
&parent_old,
1183+
&parent_new,
1184+
&coldkey,
1185+
&mut weight
1186+
));
1187+
1188+
// Verify parent and child keys updates
1189+
assert_eq!(
1190+
ParentKeys::<Test>::get(child, netuid),
1191+
vec![(u64::MAX, parent_new)]
1192+
);
1193+
assert_eq!(
1194+
ChildKeys::<Test>::get(parent_new, netuid),
1195+
vec![(u64::MAX, child)]
1196+
);
1197+
})
1198+
}
1199+
1200+
#[test]
1201+
fn test_swap_child_hotkey_childkey_maps() {
1202+
new_test_ext(1).execute_with(|| {
1203+
let netuid: u16 = 1;
1204+
let parent = U256::from(1);
1205+
let coldkey = U256::from(2);
1206+
let child_old = U256::from(3);
1207+
let child_new = U256::from(4);
1208+
add_network(netuid, 0, 0);
1209+
SubtensorModule::create_account_if_non_existent(&coldkey, &child_old);
1210+
SubtensorModule::create_account_if_non_existent(&coldkey, &parent);
1211+
1212+
// Set child and verify state maps
1213+
assert_ok!(SubtensorModule::do_set_children(
1214+
RuntimeOrigin::signed(coldkey),
1215+
parent,
1216+
netuid,
1217+
vec![(u64::MAX, child_old)]
1218+
));
1219+
assert_eq!(
1220+
ParentKeys::<Test>::get(child_old, netuid),
1221+
vec![(u64::MAX, parent)]
1222+
);
1223+
assert_eq!(
1224+
ChildKeys::<Test>::get(parent, netuid),
1225+
vec![(u64::MAX, child_old)]
1226+
);
1227+
1228+
// Swap
1229+
let mut weight = Weight::zero();
1230+
assert_ok!(SubtensorModule::perform_hotkey_swap(
1231+
&child_old,
1232+
&child_new,
1233+
&coldkey,
1234+
&mut weight
1235+
));
1236+
1237+
// Verify parent and child keys updates
1238+
assert_eq!(
1239+
ParentKeys::<Test>::get(child_new, netuid),
1240+
vec![(u64::MAX, parent)]
1241+
);
1242+
assert_eq!(
1243+
ChildKeys::<Test>::get(parent, netuid),
1244+
vec![(u64::MAX, child_new)]
1245+
);
1246+
})
1247+
}

0 commit comments

Comments
 (0)