From 8c27fe2ab6368f539e985705b91a060e0d7df2ab Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 9 Jan 2025 22:56:50 +0800 Subject: [PATCH] Fix test_childkey_set_weights_single_parent --- pallets/subtensor/src/lib.rs | 3 +- pallets/subtensor/src/staking/stake_utils.rs | 27 +++- pallets/subtensor/src/tests/children.rs | 162 +++++++++---------- 3 files changed, 109 insertions(+), 83 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 099e659c5..ef2a84624 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1474,7 +1474,8 @@ pub mod pallet { /// Is the caller allowed to set weights pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool { // Blacklist weights transactions for low stake peers. - Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_stake_threshold() + let (total_stake, _, _) = Self::get_stake_weights_for_hotkey_on_subnet(hotkey, netuid); + total_stake >= Self::get_stake_threshold() } /// Helper function to check if register is allowed diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 89693feed..0d0cb2ec3 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -94,10 +94,35 @@ impl Pallet { TaoWeight::::set(weight); } + /// Calculates the weighted combination of alpha and global tao for a single hotkey onet a subnet. + /// + pub fn get_stake_weights_for_hotkey_on_subnet( + hotkey: &T::AccountId, + netuid: u16, + ) -> (I64F64, I64F64, I64F64) { + // Retrieve the global tao weight. + let tao_weight = I64F64::from_num(Self::get_tao_weight()); + log::debug!("tao_weight: {:?}", tao_weight); + + // Step 1: Get stake of hotkey (neuron) + let alpha_stake = I64F64::from_num(Self::get_inherited_for_hotkey_on_subnet(&hotkey, netuid)); + log::trace!("alpha_stake: {:?}", alpha_stake); + + // Step 2: Get the global tao stake for the hotkey + let tao_stake = I64F64::from_num(Self::get_inherited_for_hotkey_on_subnet(&hotkey, 0)); + log::trace!("tao_stake: {:?}", tao_stake); + + // Step 3: Combine alpha and tao stakes + let total_stake = alpha_stake.saturating_add(tao_stake.saturating_mul(tao_weight)); + log::trace!("total_stake: {:?}", total_stake); + + (total_stake, alpha_stake, tao_stake) + } + /// Calculates the weighted combination of alpha and global tao for hotkeys on a subnet. /// pub fn get_stake_weights_for_network(netuid: u16) -> (Vec, Vec, Vec) { - // Retrieve the global global weight. + // Retrieve the global tao weight. let tao_weight: I64F64 = I64F64::from_num(Self::get_tao_weight()); log::debug!("tao_weight: {:?}", tao_weight); diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index b735e8fc1..27f0b9a95 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -2630,105 +2630,105 @@ fn test_set_children_rate_limit_fail_then_succeed() { #[test] fn test_childkey_set_weights_single_parent() { new_test_ext(1).execute_with(|| { - assert!(false); + let netuid: u16 = 1; + add_network(netuid, 1, 0); - // let netuid: u16 = 1; - // add_network(netuid, 1, 0); + // Define hotkeys + let parent: U256 = U256::from(1); + let child: U256 = U256::from(2); + let weight_setter: U256 = U256::from(3); - // // Define hotkeys - // let parent: U256 = U256::from(1); - // let child: U256 = U256::from(2); - // let weight_setter: U256 = U256::from(3); + // Define coldkeys with more readable names + let coldkey_parent: U256 = U256::from(100); + let coldkey_child: U256 = U256::from(101); + let coldkey_weight_setter: U256 = U256::from(102); - // // Define coldkeys with more readable names - // let coldkey_parent: U256 = U256::from(100); - // let coldkey_child: U256 = U256::from(101); - // let coldkey_weight_setter: U256 = U256::from(102); + let stake_to_give_child = 109_999; - // let stake_to_give_child = 109_999; + // Register parent with minimal stake and child with high stake + SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 1); + SubtensorModule::add_balance_to_coldkey_account(&coldkey_child, stake_to_give_child + 10); + SubtensorModule::add_balance_to_coldkey_account(&coldkey_weight_setter, 1_000_000); - // // Register parent with minimal stake and child with high stake - // SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 1); - // SubtensorModule::add_balance_to_coldkey_account(&coldkey_child, stake_to_give_child + 10); - // SubtensorModule::add_balance_to_coldkey_account(&coldkey_weight_setter, 1_000_000); + // Add neurons for parent, child and weight_setter + register_ok_neuron(netuid, parent, coldkey_parent, 1); + register_ok_neuron(netuid, child, coldkey_child, 1); + register_ok_neuron(netuid, weight_setter, coldkey_weight_setter, 1); - // // Add neurons for parent, child and weight_setter - // register_ok_neuron(netuid, parent, coldkey_parent, 1); - // register_ok_neuron(netuid, child, coldkey_child, 1); - // register_ok_neuron(netuid, weight_setter, coldkey_weight_setter, 1); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &parent, + &coldkey_parent, + netuid, + stake_to_give_child, + ); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &weight_setter, + &coldkey_weight_setter, + netuid, + 1_000_000, + ); - // SubtensorModule::increase_stake_on_coldkey_hotkey_account( - // &coldkey_parent, - // &parent, - // stake_to_give_child, - // ); - // SubtensorModule::increase_stake_on_coldkey_hotkey_account( - // &coldkey_weight_setter, - // &weight_setter, - // 1_000_000, - // ); + SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // SubtensorModule::set_weights_set_rate_limit(netuid, 0); + // Set parent-child relationship + mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX, child)]); - // // Set parent-child relationship - // mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX, child)]); + step_block(7200 + 1); + // Set weights on the child using the weight_setter account + let origin = RuntimeOrigin::signed(weight_setter); + let uids: Vec = vec![1]; // Only set weight for the child (UID 1) + let values: Vec = vec![u16::MAX]; // Use maximum value for u16 + let version_key = SubtensorModule::get_weights_version_key(netuid); + assert_ok!(SubtensorModule::set_weights( + origin, + netuid, + uids.clone(), + values.clone(), + version_key + )); - // step_block(7200 + 1); - // // Set weights on the child using the weight_setter account - // let origin = RuntimeOrigin::signed(weight_setter); - // let uids: Vec = vec![1]; // Only set weight for the child (UID 1) - // let values: Vec = vec![u16::MAX]; // Use maximum value for u16 - // let version_key = SubtensorModule::get_weights_version_key(netuid); - // assert_ok!(SubtensorModule::set_weights( - // origin, - // netuid, - // uids.clone(), - // values.clone(), - // version_key - // )); + // Set the min stake very high + SubtensorModule::set_stake_threshold(stake_to_give_child * 5); - // // Set the min stake very high - // SubtensorModule::set_stake_threshold(stake_to_give_child * 5); + // Check the child has less stake than required + assert!( + SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid) + < SubtensorModule::get_stake_threshold() + ); - // // Check the child has less stake than required - // assert!( - // SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid) - // < SubtensorModule::get_stake_threshold() - // ); + // Check the child cannot set weights + assert_noop!( + SubtensorModule::set_weights( + RuntimeOrigin::signed(child), + netuid, + uids.clone(), + values.clone(), + version_key + ), + Error::::NotEnoughStakeToSetWeights + ); - // // Check the child cannot set weights - // assert_noop!( - // SubtensorModule::set_weights( - // RuntimeOrigin::signed(child), - // netuid, - // uids.clone(), - // values.clone(), - // version_key - // ), - // Error::::NotEnoughStakeToSetWeights - // ); + assert!(!SubtensorModule::check_weights_min_stake(&child, netuid)); - // assert!(!SubtensorModule::check_weights_min_stake(&child, netuid)); + // Set a minimum stake to set weights + SubtensorModule::set_stake_threshold(stake_to_give_child - 5); - // // Set a minimum stake to set weights - // SubtensorModule::set_stake_threshold(stake_to_give_child - 5); + let (total_stakes, _, _) = SubtensorModule::get_stake_weights_for_network(netuid); + let child_stake = total_stakes[child.as_usize()]; - // // Check if the stake for the child is above - // assert!( - // SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid) - // >= SubtensorModule::get_stake_threshold() - // ); + // Check if the stake for the child is above + assert!(child_stake >= SubtensorModule::get_stake_threshold()); - // // Check the child can set weights - // assert_ok!(SubtensorModule::set_weights( - // RuntimeOrigin::signed(child), - // netuid, - // uids, - // values, - // version_key - // )); + // Check the child can set weights + assert_ok!(SubtensorModule::set_weights( + RuntimeOrigin::signed(child), + netuid, + uids, + values, + version_key + )); - // assert!(SubtensorModule::check_weights_min_stake(&child, netuid)); + assert!(SubtensorModule::check_weights_min_stake(&child, netuid)); }); }