Skip to content

Commit 789b3c7

Browse files
authored
Merge pull request #1142 from opentensor/fix/fix-hotkey-swap-alpha
Fix hotkey swap alpha
2 parents d9279fb + f3ed7d9 commit 789b3c7

File tree

2 files changed

+154
-71
lines changed

2 files changed

+154
-71
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,36 @@ impl<T: Config> Pallet<T> {
303303
}
304304
});
305305

306+
// 11. Swap Alpha
307+
// Alpha( hotkey, coldkey, netuid ) -> alpha
308+
let old_alpha_values: Vec<((T::AccountId, u16), U64F64)> =
309+
Alpha::<T>::iter_prefix((old_hotkey,)).collect();
310+
// Clear the entire old prefix here.
311+
let _ = Alpha::<T>::clear_prefix((old_hotkey,), old_alpha_values.len() as u32, None);
312+
weight.saturating_accrue(T::DbWeight::get().reads(old_alpha_values.len() as u64));
313+
weight.saturating_accrue(T::DbWeight::get().writes(old_alpha_values.len() as u64));
314+
315+
// Insert the new alpha values.
316+
for ((coldkey, netuid), alpha) in old_alpha_values {
317+
let new_alpha = Alpha::<T>::get((new_hotkey, &coldkey, netuid));
318+
Alpha::<T>::insert(
319+
(new_hotkey, &coldkey, netuid),
320+
new_alpha.saturating_add(alpha),
321+
);
322+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
323+
324+
// Swap StakingHotkeys.
325+
// StakingHotkeys( coldkey ) --> Vec<hotkey> -- the hotkeys that the coldkey stakes.
326+
let mut staking_hotkeys = StakingHotkeys::<T>::get(&coldkey);
327+
weight.saturating_accrue(T::DbWeight::get().reads(1));
328+
if staking_hotkeys.contains(&old_hotkey) {
329+
staking_hotkeys.retain(|hk| *hk != *old_hotkey && *hk != *new_hotkey);
330+
staking_hotkeys.push(new_hotkey.clone());
331+
StakingHotkeys::<T>::insert(&coldkey, staking_hotkeys);
332+
weight.saturating_accrue(T::DbWeight::get().writes(1));
333+
}
334+
}
335+
306336
// 11. Swap Stake.
307337
// Stake( hotkey, coldkey ) -> stake -- the stake that the hotkey controls on behalf of the coldkey.
308338
let stakes: Vec<(T::AccountId, u64)> = Stake::<T>::iter_prefix(old_hotkey).collect();
@@ -322,30 +352,7 @@ impl<T: Config> Pallet<T> {
322352
&coldkey,
323353
new_stake_value.saturating_add(old_stake_amount),
324354
);
325-
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
326-
327-
// 3.1 Swap Alpha
328-
for netuid in Self::get_all_subnet_netuids() {
329-
// Get the stake on the old (hot,coldkey) account.
330-
let old_alpha: U64F64 = Alpha::<T>::get((&old_hotkey, coldkey.clone(), netuid));
331-
// Get the stake on the new (hot,coldkey) account.
332-
let new_alpha: U64F64 = Alpha::<T>::get((&new_hotkey, coldkey.clone(), netuid));
333-
// Add the stake to new account.
334-
Alpha::<T>::insert(
335-
(&new_hotkey, coldkey.clone(), netuid),
336-
new_alpha.saturating_add(old_alpha),
337-
);
338-
// Remove the value from the old account.
339-
Alpha::<T>::remove((&old_hotkey, coldkey.clone(), netuid));
340-
}
341-
342-
// Swap StakingHotkeys.
343-
// StakingHotkeys( coldkey ) --> Vec<hotkey> -- the hotkeys that the coldkey stakes.
344-
let mut staking_hotkeys = StakingHotkeys::<T>::get(coldkey.clone());
345-
staking_hotkeys.retain(|hk| *hk != *old_hotkey && *hk != *new_hotkey);
346-
staking_hotkeys.push(new_hotkey.clone());
347-
StakingHotkeys::<T>::insert(coldkey.clone(), staking_hotkeys);
348-
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
355+
weight.saturating_accrue(T::DbWeight::get().writes(1));
349356
}
350357

351358
// 12. Swap ChildKeys.

pallets/subtensor/src/tests/swap_hotkey.rs

Lines changed: 123 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -613,56 +613,132 @@ fn test_swap_hotkey_with_no_stake() {
613613
});
614614
}
615615

616-
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_with_multiple_coldkeys_and_subnets --exact --nocapture
616+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::swap_hotkey::test_swap_hotkey_with_multiple_coldkeys_and_subnets --exact --show-output
617617
#[test]
618618
fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
619619
new_test_ext(1).execute_with(|| {
620-
assert!(false);
621-
622-
// let old_hotkey = U256::from(1);
623-
// let new_hotkey = U256::from(2);
624-
// let coldkey1 = U256::from(3);
625-
// let coldkey2 = U256::from(4);
626-
// let netuid1 = 0;
627-
// let netuid2 = 1;
628-
// let mut weight = Weight::zero();
629-
630-
// // Set up initial state
631-
// add_network(netuid1, 0, 1);
632-
// add_network(netuid2, 0, 1);
633-
// Owner::<Test>::insert(old_hotkey, coldkey1);
634-
// Stake::<Test>::insert(old_hotkey, coldkey1, 100);
635-
// Stake::<Test>::insert(old_hotkey, coldkey2, 200);
636-
// IsNetworkMember::<Test>::insert(old_hotkey, netuid1, true);
637-
// IsNetworkMember::<Test>::insert(old_hotkey, netuid2, true);
638-
// TotalHotkeyStake::<Test>::insert(old_hotkey, 300);
639-
640-
// assert_ok!(SubtensorModule::perform_hotkey_swap(
641-
// &old_hotkey,
642-
// &new_hotkey,
643-
// &coldkey1,
644-
// &mut weight
645-
// ));
646-
647-
// // Check ownership transfer
648-
// assert!(!Owner::<Test>::contains_key(old_hotkey));
649-
// assert_eq!(Owner::<Test>::get(new_hotkey), coldkey1);
650-
651-
// // Check stake transfer
652-
// assert_eq!(Stake::<Test>::get(new_hotkey, coldkey1), 100);
653-
// assert_eq!(Stake::<Test>::get(new_hotkey, coldkey2), 200);
654-
// assert!(!Stake::<Test>::contains_key(old_hotkey, coldkey1));
655-
// assert!(!Stake::<Test>::contains_key(old_hotkey, coldkey2));
656-
657-
// // Check subnet membership transfer
658-
// assert!(IsNetworkMember::<Test>::get(new_hotkey, netuid1));
659-
// assert!(IsNetworkMember::<Test>::get(new_hotkey, netuid2));
660-
// assert!(!IsNetworkMember::<Test>::get(old_hotkey, netuid1));
661-
// assert!(!IsNetworkMember::<Test>::get(old_hotkey, netuid2));
662-
663-
// // Check total stake transfer
664-
// assert_eq!(TotalHotkeyStake::<Test>::get(new_hotkey), 300);
665-
// assert!(!TotalHotkeyStake::<Test>::contains_key(old_hotkey));
620+
let old_hotkey = U256::from(1);
621+
let new_hotkey = U256::from(2);
622+
let coldkey1 = U256::from(3);
623+
let coldkey2 = U256::from(4);
624+
let netuid1 = 1;
625+
let netuid2 = 2;
626+
let stake = 1_000_000_u64;
627+
let mut weight = Weight::zero();
628+
629+
// Set up initial state
630+
add_network(netuid1, 0, 1);
631+
add_network(netuid2, 0, 1);
632+
register_ok_neuron(netuid1, old_hotkey, coldkey1, 1234);
633+
register_ok_neuron(netuid2, old_hotkey, coldkey1, 1234);
634+
635+
// Add balance to both coldkeys
636+
SubtensorModule::add_balance_to_coldkey_account(&coldkey1, stake + 1_000);
637+
SubtensorModule::add_balance_to_coldkey_account(&coldkey2, stake + 1_000);
638+
639+
// Stake with coldkey1
640+
assert_ok!(SubtensorModule::add_stake(
641+
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
642+
old_hotkey,
643+
netuid1,
644+
stake
645+
));
646+
647+
// Stake with coldkey2 also
648+
assert_ok!(SubtensorModule::add_stake(
649+
<<Test as Config>::RuntimeOrigin>::signed(coldkey2),
650+
old_hotkey,
651+
netuid2,
652+
stake
653+
));
654+
655+
let ck1_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
656+
&old_hotkey,
657+
&coldkey1,
658+
netuid1,
659+
);
660+
let ck2_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
661+
&old_hotkey,
662+
&coldkey2,
663+
netuid2,
664+
);
665+
assert!(ck1_stake > 0);
666+
assert!(ck2_stake > 0);
667+
let total_hk_stake = SubtensorModule::get_total_stake_for_hotkey(&old_hotkey);
668+
assert!(total_hk_stake > 0);
669+
670+
assert_ok!(SubtensorModule::perform_hotkey_swap(
671+
&old_hotkey,
672+
&new_hotkey,
673+
&coldkey1,
674+
&mut weight
675+
));
676+
677+
// Check ownership transfer
678+
assert_eq!(
679+
SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkey),
680+
coldkey1
681+
);
682+
assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&new_hotkey));
683+
684+
// Check stake transfer
685+
assert_eq!(
686+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
687+
&new_hotkey,
688+
&coldkey1,
689+
netuid1
690+
),
691+
ck1_stake
692+
);
693+
assert_eq!(
694+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
695+
&new_hotkey,
696+
&coldkey2,
697+
netuid2
698+
),
699+
ck2_stake
700+
);
701+
assert_eq!(
702+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
703+
&old_hotkey,
704+
&coldkey1,
705+
netuid1
706+
),
707+
0
708+
);
709+
assert_eq!(
710+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
711+
&old_hotkey,
712+
&coldkey2,
713+
netuid2
714+
),
715+
0
716+
);
717+
718+
// Check subnet membership transfer
719+
assert!(SubtensorModule::is_hotkey_registered_on_network(
720+
netuid1,
721+
&new_hotkey
722+
));
723+
assert!(SubtensorModule::is_hotkey_registered_on_network(
724+
netuid2,
725+
&new_hotkey
726+
));
727+
assert!(!SubtensorModule::is_hotkey_registered_on_network(
728+
netuid1,
729+
&old_hotkey
730+
));
731+
assert!(!SubtensorModule::is_hotkey_registered_on_network(
732+
netuid2,
733+
&old_hotkey
734+
));
735+
736+
// Check total stake transfer
737+
assert_eq!(
738+
SubtensorModule::get_total_stake_for_hotkey(&new_hotkey),
739+
total_hk_stake
740+
);
741+
assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), 0);
666742
});
667743
}
668744

0 commit comments

Comments
 (0)