Skip to content

Commit a62975c

Browse files
Merge pull request #614 from opentensor/feat/cold_key_swap
adds tests fixes remove hotkey
2 parents a3ef6e0 + 5a26348 commit a62975c

File tree

5 files changed

+70
-80
lines changed

5 files changed

+70
-80
lines changed

pallets/subtensor/src/events.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ mod events {
145145
current_coldkey: T::AccountId,
146146
/// The account ID of the new coldkey
147147
new_coldkey: T::AccountId,
148-
/// The account ID of the hotkey
149-
hotkey: T::AccountId,
150-
/// The current stake of the hotkey
151-
current_stake: u64,
152148
/// The total balance of the hotkey
153149
total_balance: <<T as Config>::Currency as fungible::Inspect<
154150
<T as frame_system::Config>::AccountId,

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,11 +2030,10 @@ pub mod pallet {
20302030
.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
20312031
pub fn unstake_all_and_transfer_to_new_coldkey(
20322032
origin: OriginFor<T>,
2033-
hotkey: T::AccountId,
20342033
new_coldkey: T::AccountId,
20352034
) -> DispatchResult {
20362035
let current_coldkey = ensure_signed(origin)?;
2037-
Self::do_unstake_all_and_transfer_to_new_coldkey(current_coldkey, hotkey, new_coldkey)
2036+
Self::do_unstake_all_and_transfer_to_new_coldkey(current_coldkey, new_coldkey)
20382037
}
20392038

20402039
// ---- SUDO ONLY FUNCTIONS ------------------------------------------------------------

pallets/subtensor/src/staking.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,6 @@ impl<T: Config> Pallet<T> {
842842
/// # Arguments
843843
///
844844
/// * `current_coldkey` - The AccountId of the current coldkey.
845-
/// * `hotkey` - The AccountId of the hotkey whose balance is being unstaked and transferred.
846845
/// * `new_coldkey` - The AccountId of the new coldkey to receive the unstaked tokens.
847846
///
848847
/// # Returns
@@ -865,36 +864,38 @@ impl<T: Config> Pallet<T> {
865864
///
866865
pub fn do_unstake_all_and_transfer_to_new_coldkey(
867866
current_coldkey: T::AccountId,
868-
hotkey: T::AccountId,
869867
new_coldkey: T::AccountId,
870868
) -> DispatchResult {
871-
// Ensure the hotkey exists and is owned by the current coldkey
872-
ensure!(
873-
Self::hotkey_account_exists(&hotkey),
874-
Error::<T>::HotKeyAccountNotExists
875-
);
876-
ensure!(
877-
Self::coldkey_owns_hotkey(&current_coldkey, &hotkey),
878-
Error::<T>::NonAssociatedColdKey
879-
);
880-
881869
// Ensure the new coldkey is different from the current one
882870
ensure!(current_coldkey != new_coldkey, Error::<T>::SameColdkey);
883871

884-
// Get the current stake
885-
let current_stake: u64 = Self::get_stake_for_coldkey_and_hotkey(&current_coldkey, &hotkey);
872+
// Get all the hotkeys associated with this coldkey
873+
let hotkeys: Vec<T::AccountId> = OwnedHotkeys::<T>::get(&current_coldkey);
886874

887-
// Unstake all balance if there's any stake
888-
if current_stake > 0 {
889-
Self::do_remove_stake(
890-
RawOrigin::Signed(current_coldkey.clone()).into(),
891-
hotkey.clone(),
892-
current_stake,
893-
)?;
894-
}
875+
// iterate over all hotkeys.
876+
for next_hotkey in hotkeys {
877+
ensure!(
878+
Self::hotkey_account_exists(&next_hotkey),
879+
Error::<T>::HotKeyAccountNotExists
880+
);
881+
ensure!(
882+
Self::coldkey_owns_hotkey(&current_coldkey, &next_hotkey),
883+
Error::<T>::NonAssociatedColdKey
884+
);
895885

896-
// Get the total balance of the current coldkey account
897-
// let total_balance: <<T as Config>::Currency as fungible::Inspect<<T as system::Config>::AccountId>>::Balance = T::Currency::total_balance(&current_coldkey);
886+
// Get the current stake
887+
let current_stake: u64 =
888+
Self::get_stake_for_coldkey_and_hotkey(&current_coldkey, &next_hotkey);
889+
890+
// Unstake all balance if there's any stake
891+
if current_stake > 0 {
892+
Self::do_remove_stake(
893+
RawOrigin::Signed(current_coldkey.clone()).into(),
894+
next_hotkey.clone(),
895+
current_stake,
896+
)?;
897+
}
898+
}
898899

899900
let total_balance = Self::get_coldkey_balance(&current_coldkey);
900901
log::info!("Total Bank Balance: {:?}", total_balance);
@@ -914,8 +915,6 @@ impl<T: Config> Pallet<T> {
914915
Self::deposit_event(Event::AllBalanceUnstakedAndTransferredToNewColdkey {
915916
current_coldkey: current_coldkey.clone(),
916917
new_coldkey: new_coldkey.clone(),
917-
hotkey: hotkey.clone(),
918-
current_stake,
919918
total_balance,
920919
});
921920

pallets/subtensor/tests/staking.rs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,7 +3160,6 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_success() {
31603160

31613161
assert_ok!(SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
31623162
current_coldkey,
3163-
hotkey,
31643163
new_coldkey
31653164
));
31663165

@@ -3175,59 +3174,21 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_success() {
31753174
Event::AllBalanceUnstakedAndTransferredToNewColdkey {
31763175
current_coldkey,
31773176
new_coldkey,
3178-
hotkey,
3179-
current_stake: 500,
31803177
total_balance: 1000,
31813178
}
31823179
.into(),
31833180
);
31843181
});
31853182
}
31863183

3187-
#[test]
3188-
fn test_do_unstake_all_and_transfer_to_new_coldkey_hotkey_not_exists() {
3189-
new_test_ext(1).execute_with(|| {
3190-
let current_coldkey = U256::from(1);
3191-
let hotkey = U256::from(2);
3192-
let new_coldkey = U256::from(3);
3193-
3194-
assert_err!(
3195-
SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
3196-
current_coldkey,
3197-
hotkey,
3198-
new_coldkey
3199-
),
3200-
Error::<Test>::HotKeyAccountNotExists
3201-
);
3202-
});
3203-
}
3204-
3205-
#[test]
3206-
fn test_do_unstake_all_and_transfer_to_new_coldkey_non_associated_coldkey() {
3207-
new_test_ext(1).execute_with(|| {
3208-
let (_, hotkey, new_coldkey) = setup_test_environment();
3209-
let wrong_coldkey = U256::from(4);
3210-
3211-
assert_noop!(
3212-
SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
3213-
wrong_coldkey,
3214-
hotkey,
3215-
new_coldkey
3216-
),
3217-
Error::<Test>::NonAssociatedColdKey
3218-
);
3219-
});
3220-
}
3221-
32223184
#[test]
32233185
fn test_do_unstake_all_and_transfer_to_new_coldkey_same_coldkey() {
32243186
new_test_ext(1).execute_with(|| {
3225-
let (current_coldkey, hotkey, _) = setup_test_environment();
3187+
let (current_coldkey, _hotkey, _) = setup_test_environment();
32263188

32273189
assert_noop!(
32283190
SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
32293191
current_coldkey,
3230-
hotkey,
32313192
current_coldkey
32323193
),
32333194
Error::<Test>::SameColdkey
@@ -3270,7 +3231,6 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_no_balance() {
32703231
// Try to unstake and transfer
32713232
let result = SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
32723233
current_coldkey,
3273-
hotkey,
32743234
new_coldkey,
32753235
);
32763236

@@ -3343,7 +3303,6 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_with_no_stake() {
33433303
// Perform unstake and transfer
33443304
assert_ok!(SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
33453305
current_coldkey,
3346-
hotkey,
33473306
new_coldkey
33483307
));
33493308

@@ -3370,8 +3329,6 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_with_no_stake() {
33703329
Event::AllBalanceUnstakedAndTransferredToNewColdkey {
33713330
current_coldkey,
33723331
new_coldkey,
3373-
hotkey,
3374-
current_stake: 0,
33753332
total_balance: initial_balance,
33763333
}
33773334
.into(),
@@ -3394,7 +3351,6 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_with_multiple_stakes() {
33943351

33953352
assert_ok!(SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
33963353
current_coldkey,
3397-
hotkey,
33983354
new_coldkey
33993355
));
34003356

@@ -3409,8 +3365,48 @@ fn test_do_unstake_all_and_transfer_to_new_coldkey_with_multiple_stakes() {
34093365
Event::AllBalanceUnstakedAndTransferredToNewColdkey {
34103366
current_coldkey,
34113367
new_coldkey,
3412-
hotkey,
3413-
current_stake: 800,
3368+
total_balance: 1000,
3369+
}
3370+
.into(),
3371+
);
3372+
});
3373+
}
3374+
3375+
#[test]
3376+
fn test_do_unstake_all_and_transfer_to_new_coldkey_with_multiple_stakes_multiple() {
3377+
new_test_ext(1).execute_with(|| {
3378+
// Register the neuron to a new network
3379+
let netuid = 1;
3380+
let hotkey0 = U256::from(1);
3381+
let hotkey2 = U256::from(2);
3382+
let current_coldkey = U256::from(3);
3383+
let new_coldkey = U256::from(4);
3384+
add_network(netuid, 0, 0);
3385+
register_ok_neuron(1, hotkey0, current_coldkey, 0);
3386+
register_ok_neuron(1, hotkey2, current_coldkey, 0);
3387+
SubtensorModule::set_target_stakes_per_interval(10);
3388+
SubtensorModule::add_balance_to_coldkey_account(&current_coldkey, 1000);
3389+
assert_ok!(SubtensorModule::add_stake(
3390+
RuntimeOrigin::signed(current_coldkey),
3391+
hotkey0,
3392+
500
3393+
));
3394+
assert_ok!(SubtensorModule::add_stake(
3395+
RuntimeOrigin::signed(current_coldkey),
3396+
hotkey2,
3397+
300
3398+
));
3399+
assert_ok!(SubtensorModule::do_unstake_all_and_transfer_to_new_coldkey(
3400+
current_coldkey,
3401+
new_coldkey
3402+
));
3403+
assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey0), 0);
3404+
assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey2), 0);
3405+
assert_eq!(SubtensorModule::get_coldkey_balance(&new_coldkey), 1000);
3406+
System::assert_last_event(
3407+
Event::AllBalanceUnstakedAndTransferredToNewColdkey {
3408+
current_coldkey,
3409+
new_coldkey,
34143410
total_balance: 1000,
34153411
}
34163412
.into(),

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
139139
// `spec_version`, and `authoring_version` are the same between Wasm and native.
140140
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
141141
// the compatible custom types.
142-
spec_version: 156,
142+
spec_version: 158,
143143
impl_version: 1,
144144
apis: RUNTIME_API_VERSIONS,
145145
transaction_version: 1,

0 commit comments

Comments
 (0)