Skip to content

Commit 578263b

Browse files
authored
Merge pull request #1196 from opentensor/fix/swap-pending-childkeys-on-hk-swap
Fix/swap pending childkeys on hk swap
2 parents 948afbb + 5122fa7 commit 578263b

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 33 additions & 2 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,11 +403,39 @@ 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

406-
// 14. Swap Stake Delta for all coldkeys.
407-
// DEPRECATED
410+
// 14. Swap PendingChildKeys.
411+
// PendingChildKeys( netuid, parent ) --> Vec<(proportion,child), cool_down_block>
412+
for netuid in Self::get_all_subnet_netuids() {
413+
weight.saturating_accrue(T::DbWeight::get().reads(1));
414+
if PendingChildKeys::<T>::contains_key(netuid, old_hotkey) {
415+
let (children, cool_down_block) = PendingChildKeys::<T>::get(netuid, old_hotkey);
416+
PendingChildKeys::<T>::remove(netuid, old_hotkey);
417+
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+
}
437+
}
438+
}
408439

409440
// Return successful after swapping all the relevant terms.
410441
Ok(())

pallets/subtensor/src/tests/swap_hotkey.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,12 +1257,18 @@ fn test_swap_parent_hotkey_childkey_maps() {
12571257
let parent_old = U256::from(1);
12581258
let coldkey = U256::from(2);
12591259
let child = U256::from(3);
1260+
let child_other = U256::from(4);
12601261
let parent_new = U256::from(4);
12611262
add_network(netuid, 1, 0);
12621263
SubtensorModule::create_account_if_non_existent(&coldkey, &parent_old);
12631264

12641265
// Set child and verify state maps
12651266
mock_set_children(&coldkey, &parent_old, netuid, &[(u64::MAX, child)]);
1267+
// Wait rate limit
1268+
step_rate_limit(&TransactionType::SetChildren, netuid);
1269+
// Schedule some pending child keys.
1270+
mock_schedule_children(&coldkey, &parent_old, netuid, &[(u64::MAX, child_other)]);
1271+
12661272
assert_eq!(
12671273
ParentKeys::<Test>::get(child, netuid),
12681274
vec![(u64::MAX, parent_old)]
@@ -1271,6 +1277,8 @@ fn test_swap_parent_hotkey_childkey_maps() {
12711277
ChildKeys::<Test>::get(parent_old, netuid),
12721278
vec![(u64::MAX, child)]
12731279
);
1280+
let existing_pending_child_keys = PendingChildKeys::<Test>::get(netuid, parent_old);
1281+
assert_eq!(existing_pending_child_keys.0, vec![(u64::MAX, child_other)]);
12741282

12751283
// Swap
12761284
let mut weight = Weight::zero();
@@ -1290,6 +1298,10 @@ fn test_swap_parent_hotkey_childkey_maps() {
12901298
ChildKeys::<Test>::get(parent_new, netuid),
12911299
vec![(u64::MAX, child)]
12921300
);
1301+
assert_eq!(
1302+
PendingChildKeys::<Test>::get(netuid, parent_new),
1303+
existing_pending_child_keys // Entry under new hotkey.
1304+
);
12931305
})
12941306
}
12951307

@@ -1307,6 +1319,10 @@ fn test_swap_child_hotkey_childkey_maps() {
13071319

13081320
// Set child and verify state maps
13091321
mock_set_children(&coldkey, &parent, netuid, &[(u64::MAX, child_old)]);
1322+
// Wait rate limit
1323+
step_rate_limit(&TransactionType::SetChildren, netuid);
1324+
// Schedule some pending child keys.
1325+
mock_schedule_children(&coldkey, &parent, netuid, &[(u64::MAX, child_old)]);
13101326

13111327
assert_eq!(
13121328
ParentKeys::<Test>::get(child_old, netuid),
@@ -1316,6 +1332,8 @@ fn test_swap_child_hotkey_childkey_maps() {
13161332
ChildKeys::<Test>::get(parent, netuid),
13171333
vec![(u64::MAX, child_old)]
13181334
);
1335+
let existing_pending_child_keys = PendingChildKeys::<Test>::get(netuid, parent);
1336+
assert_eq!(existing_pending_child_keys.0, vec![(u64::MAX, child_old)]);
13191337

13201338
// Swap
13211339
let mut weight = Weight::zero();
@@ -1335,5 +1353,9 @@ fn test_swap_child_hotkey_childkey_maps() {
13351353
ChildKeys::<Test>::get(parent, netuid),
13361354
vec![(u64::MAX, child_new)]
13371355
);
1356+
assert_eq!(
1357+
PendingChildKeys::<Test>::get(netuid, parent),
1358+
(vec![(u64::MAX, child_new)], existing_pending_child_keys.1) // Same cooldown block.
1359+
);
13381360
})
13391361
}

0 commit comments

Comments
 (0)