Skip to content

Commit d61f180

Browse files
committed
Check for childkey stake in check_weights_min_stake
1 parent dfbed6b commit d61f180

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,9 +1309,17 @@ 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 {
1313+
let parent_stake = Self::get_total_stake_for_hotkey(hotkey);
1314+
let total_stake = ChildKeys::<T>::get(hotkey, netuid).into_iter().fold(
1315+
parent_stake,
1316+
|acc, (_, child_key)| {
1317+
let child_stake = Self::get_total_stake_for_hotkey(&child_key);
1318+
acc.saturating_add(child_stake)
1319+
},
1320+
);
13131321
// Blacklist weights transactions for low stake peers.
1314-
Self::get_total_stake_for_hotkey(hotkey) >= Self::get_weights_min_stake()
1322+
total_stake >= Self::get_weights_min_stake()
13151323
}
13161324

13171325
/// Helper function to check if register is allowed
@@ -1404,8 +1412,8 @@ where
14041412
Pallet::<T>::get_priority_set_weights(who, netuid)
14051413
}
14061414

1407-
pub fn check_weights_min_stake(who: &T::AccountId) -> bool {
1408-
Pallet::<T>::check_weights_min_stake(who)
1415+
pub fn check_weights_min_stake(who: &T::AccountId, netuid: u16) -> bool {
1416+
Pallet::<T>::check_weights_min_stake(who, netuid)
14091417
}
14101418
}
14111419

@@ -1443,7 +1451,7 @@ where
14431451
) -> TransactionValidity {
14441452
match call.is_sub_type() {
14451453
Some(Call::commit_weights { netuid, .. }) => {
1446-
if Self::check_weights_min_stake(who) {
1454+
if Self::check_weights_min_stake(who, *netuid) {
14471455
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14481456
Ok(ValidTransaction {
14491457
priority,
@@ -1455,7 +1463,7 @@ where
14551463
}
14561464
}
14571465
Some(Call::reveal_weights { netuid, .. }) => {
1458-
if Self::check_weights_min_stake(who) {
1466+
if Self::check_weights_min_stake(who, *netuid) {
14591467
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14601468
Ok(ValidTransaction {
14611469
priority,
@@ -1467,7 +1475,7 @@ where
14671475
}
14681476
}
14691477
Some(Call::batch_reveal_weights { netuid, .. }) => {
1470-
if Self::check_weights_min_stake(who) {
1478+
if Self::check_weights_min_stake(who, *netuid) {
14711479
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14721480
Ok(ValidTransaction {
14731481
priority,
@@ -1479,7 +1487,7 @@ where
14791487
}
14801488
}
14811489
Some(Call::set_weights { netuid, .. }) => {
1482-
if Self::check_weights_min_stake(who) {
1490+
if Self::check_weights_min_stake(who, *netuid) {
14831491
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14841492
Ok(ValidTransaction {
14851493
priority,
@@ -1491,7 +1499,7 @@ where
14911499
}
14921500
}
14931501
Some(Call::set_root_weights { netuid, hotkey, .. }) => {
1494-
if Self::check_weights_min_stake(hotkey) {
1502+
if Self::check_weights_min_stake(hotkey, *netuid) {
14951503
let priority: u64 = Self::get_priority_set_weights(hotkey, *netuid);
14961504
Ok(ValidTransaction {
14971505
priority,

pallets/subtensor/src/subnets/weights.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ impl<T: Config> Pallet<T> {
527527

528528
// --- 6. Check to see if the hotkey has enought 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/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)