Skip to content

Commit e21ba0d

Browse files
camfairchildkeithtensor
authored andcommitted
respect chk for set weights min stake filter (#935)
* respect chk for set weights min stake filter * use for test func * respect chk in set weights call also * add tests back * fix weights min stake tests also
1 parent a308cd4 commit e21ba0d

File tree

5 files changed

+207
-16
lines changed

5 files changed

+207
-16
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ pub mod pallet {
12991299
/// Returns the transaction priority for setting weights.
13001300
pub fn get_priority_set_weights(hotkey: &T::AccountId, netuid: u16) -> u64 {
13011301
if let Ok(uid) = Self::get_uid_for_net_and_hotkey(netuid, hotkey) {
1302-
let _stake = Self::get_total_stake_for_hotkey(hotkey);
1302+
let _stake = Self::get_stake_for_hotkey_on_subnet(hotkey, netuid);
13031303
let current_block_number: u64 = Self::get_current_block_as_u64();
13041304
let default_priority: u64 =
13051305
current_block_number.saturating_sub(Self::get_last_update_for_uid(netuid, uid));
@@ -1309,9 +1309,9 @@ pub mod pallet {
13091309
}
13101310

13111311
/// Is the caller allowed to set weights
1312-
pub fn check_weights_min_stake(hotkey: &T::AccountId) -> bool {
1312+
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
13131313
// Blacklist weights transactions for low stake peers.
1314-
Self::get_total_stake_for_hotkey(hotkey) >= Self::get_weights_min_stake()
1314+
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
13151315
}
13161316

13171317
/// Helper function to check if register is allowed
@@ -1404,8 +1404,8 @@ where
14041404
Pallet::<T>::get_priority_set_weights(who, netuid)
14051405
}
14061406

1407-
pub fn check_weights_min_stake(who: &T::AccountId) -> bool {
1408-
Pallet::<T>::check_weights_min_stake(who)
1407+
pub fn check_weights_min_stake(who: &T::AccountId, netuid: u16) -> bool {
1408+
Pallet::<T>::check_weights_min_stake(who, netuid)
14091409
}
14101410
}
14111411

@@ -1443,7 +1443,7 @@ where
14431443
) -> TransactionValidity {
14441444
match call.is_sub_type() {
14451445
Some(Call::commit_weights { netuid, .. }) => {
1446-
if Self::check_weights_min_stake(who) {
1446+
if Self::check_weights_min_stake(who, *netuid) {
14471447
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14481448
Ok(ValidTransaction {
14491449
priority,
@@ -1455,7 +1455,7 @@ where
14551455
}
14561456
}
14571457
Some(Call::reveal_weights { netuid, .. }) => {
1458-
if Self::check_weights_min_stake(who) {
1458+
if Self::check_weights_min_stake(who, *netuid) {
14591459
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14601460
Ok(ValidTransaction {
14611461
priority,
@@ -1467,7 +1467,7 @@ where
14671467
}
14681468
}
14691469
Some(Call::batch_reveal_weights { netuid, .. }) => {
1470-
if Self::check_weights_min_stake(who) {
1470+
if Self::check_weights_min_stake(who, *netuid) {
14711471
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14721472
Ok(ValidTransaction {
14731473
priority,
@@ -1479,7 +1479,7 @@ where
14791479
}
14801480
}
14811481
Some(Call::set_weights { netuid, .. }) => {
1482-
if Self::check_weights_min_stake(who) {
1482+
if Self::check_weights_min_stake(who, *netuid) {
14831483
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14841484
Ok(ValidTransaction {
14851485
priority,
@@ -1491,7 +1491,7 @@ where
14911491
}
14921492
}
14931493
Some(Call::set_root_weights { netuid, hotkey, .. }) => {
1494-
if Self::check_weights_min_stake(hotkey) {
1494+
if Self::check_weights_min_stake(hotkey, *netuid) {
14951495
let priority: u64 = Self::get_priority_set_weights(hotkey, *netuid);
14961496
Ok(ValidTransaction {
14971497
priority,

pallets/subtensor/src/subnets/uids.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<T: Config> Pallet<T> {
120120
///
121121
pub fn get_stake_for_uid_and_subnetwork(netuid: u16, neuron_uid: u16) -> u64 {
122122
if let Ok(hotkey) = Self::get_hotkey_for_net_and_uid(netuid, neuron_uid) {
123-
Self::get_total_stake_for_hotkey(&hotkey)
123+
Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid)
124124
} else {
125125
0
126126
}

pallets/subtensor/src/subnets/weights.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ impl<T: Config> Pallet<T> {
525525
Error::<T>::HotKeyNotRegisteredInSubNet
526526
);
527527

528-
// --- 6. Check to see if the hotkey has enought stake to set weights.
528+
// --- 6. Check to see if the hotkey has enough stake to set weights.
529529
ensure!(
530-
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_weights_min_stake(),
530+
Self::check_weights_min_stake(&hotkey, netuid),
531531
Error::<T>::NotEnoughStakeToSetWeights
532532
);
533533

pallets/subtensor/tests/children.rs

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,3 +3237,194 @@ fn test_rank_trust_incentive_calculation_with_parent_child() {
32373237

32383238
});
32393239
}
3240+
3241+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --test children -- test_childkey_set_weights_single_parent --exact --nocapture
3242+
#[test]
3243+
fn test_childkey_set_weights_single_parent() {
3244+
new_test_ext(1).execute_with(|| {
3245+
let netuid: u16 = 1;
3246+
add_network(netuid, 1, 0);
3247+
3248+
// Define hotkeys
3249+
let parent: U256 = U256::from(1);
3250+
let child: U256 = U256::from(2);
3251+
let weight_setter: U256 = U256::from(3);
3252+
3253+
// Define coldkeys with more readable names
3254+
let coldkey_parent: U256 = U256::from(100);
3255+
let coldkey_child: U256 = U256::from(101);
3256+
let coldkey_weight_setter: U256 = U256::from(102);
3257+
3258+
let stake_to_give_child = 109_999;
3259+
3260+
// Register parent with minimal stake and child with high stake
3261+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 1);
3262+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_child, stake_to_give_child + 10);
3263+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_weight_setter, 1_000_000);
3264+
3265+
// Add neurons for parent, child and weight_setter
3266+
register_ok_neuron(netuid, parent, coldkey_parent, 1);
3267+
register_ok_neuron(netuid, child, coldkey_child, 1);
3268+
register_ok_neuron(netuid, weight_setter, coldkey_weight_setter, 1);
3269+
3270+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(
3271+
&coldkey_parent,
3272+
&parent,
3273+
stake_to_give_child,
3274+
);
3275+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(
3276+
&coldkey_weight_setter,
3277+
&weight_setter,
3278+
1_000_000,
3279+
);
3280+
3281+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
3282+
3283+
// Set parent-child relationship
3284+
assert_ok!(SubtensorModule::do_set_children(
3285+
RuntimeOrigin::signed(coldkey_parent),
3286+
parent,
3287+
netuid,
3288+
vec![(u64::MAX, child)]
3289+
));
3290+
step_block(7200 + 1);
3291+
// Set weights on the child using the weight_setter account
3292+
let origin = RuntimeOrigin::signed(weight_setter);
3293+
let uids: Vec<u16> = vec![1]; // Only set weight for the child (UID 1)
3294+
let values: Vec<u16> = vec![u16::MAX]; // Use maximum value for u16
3295+
let version_key = SubtensorModule::get_weights_version_key(netuid);
3296+
assert_ok!(SubtensorModule::set_weights(
3297+
origin,
3298+
netuid,
3299+
uids.clone(),
3300+
values.clone(),
3301+
version_key
3302+
));
3303+
3304+
// Set the min stake very high
3305+
SubtensorModule::set_weights_min_stake(stake_to_give_child * 5);
3306+
3307+
// Check the child has less stake than required
3308+
assert!(
3309+
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
3310+
< SubtensorModule::get_weights_min_stake()
3311+
);
3312+
3313+
// Check the child cannot set weights
3314+
assert_noop!(
3315+
SubtensorModule::set_weights(
3316+
RuntimeOrigin::signed(child),
3317+
netuid,
3318+
uids.clone(),
3319+
values.clone(),
3320+
version_key
3321+
),
3322+
Error::<Test>::NotEnoughStakeToSetWeights
3323+
);
3324+
3325+
assert!(!SubtensorModule::check_weights_min_stake(&child, netuid));
3326+
3327+
// Set a minimum stake to set weights
3328+
SubtensorModule::set_weights_min_stake(stake_to_give_child - 5);
3329+
3330+
// Check if the stake for the child is above
3331+
assert!(
3332+
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
3333+
>= SubtensorModule::get_weights_min_stake()
3334+
);
3335+
3336+
// Check the child can set weights
3337+
assert_ok!(SubtensorModule::set_weights(
3338+
RuntimeOrigin::signed(child),
3339+
netuid,
3340+
uids,
3341+
values,
3342+
version_key
3343+
));
3344+
3345+
assert!(SubtensorModule::check_weights_min_stake(&child, netuid));
3346+
});
3347+
}
3348+
3349+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --test children -- test_set_weights_no_parent --exact --nocapture
3350+
#[test]
3351+
fn test_set_weights_no_parent() {
3352+
// Verify that a regular key without a parent delegation is effected by the minimum stake requirements
3353+
new_test_ext(1).execute_with(|| {
3354+
let netuid: u16 = 1;
3355+
add_network(netuid, 1, 0);
3356+
3357+
let hotkey: U256 = U256::from(2);
3358+
let spare_hk: U256 = U256::from(3);
3359+
3360+
let coldkey: U256 = U256::from(101);
3361+
let spare_ck = U256::from(102);
3362+
3363+
let stake_to_give_child = 109_999;
3364+
3365+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, stake_to_give_child + 10);
3366+
3367+
// Is registered
3368+
register_ok_neuron(netuid, hotkey, coldkey, 1);
3369+
// Register a spare key
3370+
register_ok_neuron(netuid, spare_hk, spare_ck, 1);
3371+
3372+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(
3373+
&coldkey,
3374+
&hotkey,
3375+
stake_to_give_child,
3376+
);
3377+
3378+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
3379+
3380+
// Has stake and no parent
3381+
step_block(7200 + 1);
3382+
3383+
let uids: Vec<u16> = vec![1]; // Set weights on the other hotkey
3384+
let values: Vec<u16> = vec![u16::MAX]; // Use maximum value for u16
3385+
let version_key = SubtensorModule::get_weights_version_key(netuid);
3386+
3387+
// Set the min stake very high
3388+
SubtensorModule::set_weights_min_stake(stake_to_give_child * 5);
3389+
3390+
// Check the key has less stake than required
3391+
assert!(
3392+
SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey, netuid)
3393+
< SubtensorModule::get_weights_min_stake()
3394+
);
3395+
3396+
// Check the hotkey cannot set weights
3397+
assert_noop!(
3398+
SubtensorModule::set_weights(
3399+
RuntimeOrigin::signed(hotkey),
3400+
netuid,
3401+
uids.clone(),
3402+
values.clone(),
3403+
version_key
3404+
),
3405+
Error::<Test>::NotEnoughStakeToSetWeights
3406+
);
3407+
3408+
assert!(!SubtensorModule::check_weights_min_stake(&hotkey, netuid));
3409+
3410+
// Set a minimum stake to set weights
3411+
SubtensorModule::set_weights_min_stake(stake_to_give_child - 5);
3412+
3413+
// Check if the stake for the hotkey is above
3414+
assert!(
3415+
SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey, netuid)
3416+
>= SubtensorModule::get_weights_min_stake()
3417+
);
3418+
3419+
// Check the hotkey can set weights
3420+
assert_ok!(SubtensorModule::set_weights(
3421+
RuntimeOrigin::signed(hotkey),
3422+
netuid,
3423+
uids,
3424+
values,
3425+
version_key
3426+
));
3427+
3428+
assert!(SubtensorModule::check_weights_min_stake(&hotkey, netuid));
3429+
});
3430+
}

pallets/subtensor/tests/weights.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,11 @@ fn test_set_weights_min_stake_failed() {
481481

482482
// Check the signed extension function.
483483
assert_eq!(SubtensorModule::get_weights_min_stake(), 20_000_000_000_000);
484-
assert!(!SubtensorModule::check_weights_min_stake(&hotkey));
484+
assert!(!SubtensorModule::check_weights_min_stake(&hotkey, netuid));
485485
SubtensorModule::increase_stake_on_hotkey_account(&hotkey, 19_000_000_000_000);
486-
assert!(!SubtensorModule::check_weights_min_stake(&hotkey));
486+
assert!(!SubtensorModule::check_weights_min_stake(&hotkey, netuid));
487487
SubtensorModule::increase_stake_on_hotkey_account(&hotkey, 20_000_000_000_000);
488-
assert!(SubtensorModule::check_weights_min_stake(&hotkey));
488+
assert!(SubtensorModule::check_weights_min_stake(&hotkey, netuid));
489489

490490
// Check that it fails at the pallet level.
491491
SubtensorModule::set_weights_min_stake(100_000_000_000_000);

0 commit comments

Comments
 (0)