Skip to content

Commit 36e268b

Browse files
authored
Merge pull request #1302 from opentensor/hotfix/fix-sn-owner-unlimited-keys
[Fix] Modify SN creator hotkey immunity
2 parents 0784e5a + af41fa9 commit 36e268b

File tree

2 files changed

+192
-2
lines changed

2 files changed

+192
-2
lines changed

pallets/subtensor/src/subnets/registration.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
use sp_core::{H256, U256};
33
use sp_io::hashing::{keccak_256, sha2_256};
44
use sp_runtime::Saturating;
5+
use substrate_fixed::types::I64F64;
56
use system::pallet_prelude::BlockNumberFor;
67

78
const LOG_TARGET: &str = "runtime::subtensor::registration";
@@ -424,13 +425,32 @@ impl<T: Config> Pallet<T> {
424425
return 0; // If there are no neurons in this network.
425426
}
426427

428+
// Get SN owner top stake hotkey
429+
let mut top_stake_sn_owner_hotkey: Option<T::AccountId> = None;
430+
let mut max_stake_weight: I64F64 = I64F64::from_num(-1);
427431
for neuron_uid in 0..neurons_n {
428-
// Do not deregister the owner
429432
if let Ok(hotkey) = Self::get_hotkey_for_net_and_uid(netuid, neuron_uid) {
430433
let coldkey = Self::get_owning_coldkey_for_hotkey(&hotkey);
431-
if Self::get_subnet_owner(netuid) == coldkey {
434+
if Self::get_subnet_owner(netuid) != coldkey {
432435
continue;
433436
}
437+
438+
let stake_weights = Self::get_stake_weights_for_hotkey_on_subnet(&hotkey, netuid);
439+
if stake_weights.0 > max_stake_weight {
440+
max_stake_weight = stake_weights.0;
441+
top_stake_sn_owner_hotkey = Some(hotkey);
442+
}
443+
}
444+
}
445+
446+
for neuron_uid in 0..neurons_n {
447+
// Do not deregister the owner's top-stake hotkey
448+
if let Ok(hotkey) = Self::get_hotkey_for_net_and_uid(netuid, neuron_uid) {
449+
if let Some(ref top_sn_owner_hotkey) = top_stake_sn_owner_hotkey {
450+
if top_sn_owner_hotkey == &hotkey {
451+
continue;
452+
}
453+
}
434454
}
435455

436456
let pruning_score: u16 = Self::get_pruning_score_for_uid(netuid, neuron_uid);

pallets/subtensor/src/tests/uids.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::*;
55
use frame_support::{assert_err, assert_ok};
66
use frame_system::Config;
77
use sp_core::U256;
8+
use substrate_fixed::types::I64F64;
89

910
/********************************************
1011
tests for uids.rs file
@@ -335,3 +336,172 @@ fn test_get_neuron_to_prune_owner_not_pruned() {
335336
);
336337
});
337338
}
339+
340+
#[test]
341+
fn test_get_neuron_to_prune_owner_pruned_if_not_top_stake_owner_hotkey() {
342+
new_test_ext(1).execute_with(|| {
343+
let owner_hotkey = U256::from(123);
344+
let owner_coldkey = U256::from(999);
345+
let other_owner_hotkey = U256::from(456);
346+
347+
let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey);
348+
349+
SubtensorModule::set_max_registrations_per_block(netuid, 100);
350+
SubtensorModule::set_target_registrations_per_interval(netuid, 100);
351+
SubnetOwner::<Test>::insert(netuid, owner_coldkey);
352+
353+
let owner_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &owner_hotkey)
354+
.expect("Owner neuron should already be registered by add_dynamic_network");
355+
356+
// Register another hotkey for the owner
357+
register_ok_neuron(netuid, other_owner_hotkey, owner_coldkey, 0);
358+
let other_owner_uid =
359+
SubtensorModule::get_uid_for_net_and_hotkey(netuid, &other_owner_hotkey)
360+
.expect("Should be registered");
361+
362+
let additional_hotkey_1 = U256::from(1000);
363+
let additional_coldkey_1 = U256::from(2000);
364+
365+
let additional_hotkey_2 = U256::from(1001);
366+
let additional_coldkey_2 = U256::from(2001);
367+
368+
register_ok_neuron(netuid, additional_hotkey_1, additional_coldkey_1, 1);
369+
let uid_2 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &additional_hotkey_1)
370+
.expect("Should be registered");
371+
372+
register_ok_neuron(netuid, additional_hotkey_2, additional_coldkey_2, 2);
373+
let uid_3 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &additional_hotkey_2)
374+
.expect("Should be registered");
375+
376+
SubtensorModule::set_pruning_score_for_uid(netuid, owner_uid, 0);
377+
// Other owner key has pruning score not worse than the owner's first hotkey, but worse than the additional hotkeys
378+
SubtensorModule::set_pruning_score_for_uid(netuid, other_owner_uid, 1);
379+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_2, 2);
380+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_3, 3);
381+
382+
let pruned_uid = SubtensorModule::get_neuron_to_prune(netuid);
383+
assert_eq!(pruned_uid, other_owner_uid, "Should prune the owner");
384+
385+
// Give the owner's other hotkey some stake
386+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
387+
&other_owner_hotkey,
388+
&owner_coldkey,
389+
netuid,
390+
1000,
391+
);
392+
393+
// Reset pruning scores
394+
SubtensorModule::set_pruning_score_for_uid(netuid, owner_uid, 0);
395+
SubtensorModule::set_pruning_score_for_uid(netuid, other_owner_uid, 1);
396+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_2, 2);
397+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_3, 3);
398+
399+
let pruned_uid = SubtensorModule::get_neuron_to_prune(netuid);
400+
401+
// - The pruned UID must be `uid_1` (score=1).
402+
// - The owner's UID remains unpruned.
403+
assert_eq!(
404+
pruned_uid, owner_uid,
405+
"Should prune the owner, not the top-stake owner hotkey and not the additional hotkeys"
406+
);
407+
});
408+
}
409+
410+
#[test]
411+
fn test_get_neuron_to_prune_owner_pruned_if_not_top_stake_owner_hotkey_chk() {
412+
new_test_ext(1).execute_with(|| {
413+
let owner_hotkey = U256::from(123);
414+
let owner_coldkey = U256::from(999);
415+
let other_owner_hotkey = U256::from(456);
416+
let parent_hotkey = U256::from(4567);
417+
let parent_coldkey = U256::from(4568);
418+
419+
let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey);
420+
421+
SubtensorModule::set_max_registrations_per_block(netuid, 100);
422+
SubtensorModule::set_target_registrations_per_interval(netuid, 100);
423+
SubnetOwner::<Test>::insert(netuid, owner_coldkey);
424+
425+
let owner_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &owner_hotkey)
426+
.expect("Owner neuron should already be registered by add_dynamic_network");
427+
428+
// Register another hotkey for the owner
429+
register_ok_neuron(netuid, other_owner_hotkey, owner_coldkey, 0);
430+
let other_owner_uid =
431+
SubtensorModule::get_uid_for_net_and_hotkey(netuid, &other_owner_hotkey)
432+
.expect("Should be registered");
433+
434+
let additional_hotkey_1 = U256::from(1000);
435+
let additional_coldkey_1 = U256::from(2000);
436+
437+
let additional_hotkey_2 = U256::from(1001);
438+
let additional_coldkey_2 = U256::from(2001);
439+
440+
register_ok_neuron(netuid, additional_hotkey_1, additional_coldkey_1, 1);
441+
let uid_2 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &additional_hotkey_1)
442+
.expect("Should be registered");
443+
444+
register_ok_neuron(netuid, additional_hotkey_2, additional_coldkey_2, 2);
445+
let uid_3 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &additional_hotkey_2)
446+
.expect("Should be registered");
447+
448+
register_ok_neuron(netuid, parent_hotkey, parent_coldkey, 3);
449+
let uid_4: u16 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &parent_hotkey)
450+
.expect("Should be registered");
451+
452+
// Give parent key some stake
453+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
454+
&parent_hotkey,
455+
&parent_coldkey,
456+
netuid,
457+
10_000_000,
458+
);
459+
460+
SubtensorModule::set_pruning_score_for_uid(netuid, owner_uid, 0);
461+
// Other owner key has pruning score not worse than the owner's first hotkey, but worse than the additional hotkeys
462+
SubtensorModule::set_pruning_score_for_uid(netuid, other_owner_uid, 1);
463+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_2, 2);
464+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_3, 3);
465+
466+
// Ensure parent key is not pruned
467+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_4, 10_000);
468+
469+
let pruned_uid = SubtensorModule::get_neuron_to_prune(netuid);
470+
assert_eq!(
471+
pruned_uid, other_owner_uid,
472+
"Should prune the owner's other hotkey"
473+
);
474+
475+
// Give the owner's other hotkey some CHK stake; Doesn't need to be much
476+
mock_set_children_no_epochs(
477+
netuid,
478+
&parent_hotkey,
479+
&[(
480+
I64F64::saturating_from_num(0.1)
481+
.saturating_mul(I64F64::saturating_from_num(u64::MAX))
482+
.saturating_to_num::<u64>(),
483+
other_owner_hotkey,
484+
)],
485+
);
486+
// Check stake weight of other_owner_hotkey
487+
let stake_weight =
488+
SubtensorModule::get_stake_weights_for_hotkey_on_subnet(&other_owner_hotkey, netuid);
489+
assert!(stake_weight.0 > 0);
490+
491+
// Reset pruning scores
492+
SubtensorModule::set_pruning_score_for_uid(netuid, owner_uid, 0);
493+
SubtensorModule::set_pruning_score_for_uid(netuid, other_owner_uid, 1);
494+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_2, 2);
495+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_3, 3);
496+
SubtensorModule::set_pruning_score_for_uid(netuid, uid_4, 10_000);
497+
498+
let pruned_uid = SubtensorModule::get_neuron_to_prune(netuid);
499+
500+
// - The pruned UID must be `uid_1` (score=1).
501+
// - The owner's UID remains unpruned.
502+
assert_eq!(
503+
pruned_uid, owner_uid,
504+
"Should prune the owner, not the top-stake owner hotkey and not the additional hotkeys"
505+
);
506+
});
507+
}

0 commit comments

Comments
 (0)